1*84b2179eSNicolas Bonnefon /* 2*84b2179eSNicolas Bonnefon * Copyright (C) 2010, 2014 Nicolas Bonnefon and other contributors 3*84b2179eSNicolas Bonnefon * 4*84b2179eSNicolas Bonnefon * This file is part of glogg. 5*84b2179eSNicolas Bonnefon * 6*84b2179eSNicolas Bonnefon * glogg is free software: you can redistribute it and/or modify 7*84b2179eSNicolas Bonnefon * it under the terms of the GNU General Public License as published by 8*84b2179eSNicolas Bonnefon * the Free Software Foundation, either version 3 of the License, or 9*84b2179eSNicolas Bonnefon * (at your option) any later version. 10*84b2179eSNicolas Bonnefon * 11*84b2179eSNicolas Bonnefon * glogg is distributed in the hope that it will be useful, 12*84b2179eSNicolas Bonnefon * but WITHOUT ANY WARRANTY; without even the implied warranty of 13*84b2179eSNicolas Bonnefon * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14*84b2179eSNicolas Bonnefon * GNU General Public License for more details. 15*84b2179eSNicolas Bonnefon * 16*84b2179eSNicolas Bonnefon * You should have received a copy of the GNU General Public License 17*84b2179eSNicolas Bonnefon * along with glogg. If not, see <http://www.gnu.org/licenses/>. 18*84b2179eSNicolas Bonnefon */ 19*84b2179eSNicolas Bonnefon 20*84b2179eSNicolas Bonnefon #include "platformfilewatcher.h" 21*84b2179eSNicolas Bonnefon 22*84b2179eSNicolas Bonnefon #ifdef _WIN32 23*84b2179eSNicolas Bonnefon #include "winwatchtower.h" 24*84b2179eSNicolas Bonnefon #else 25*84b2179eSNicolas Bonnefon #include "inotifywatchtower.h" 26*84b2179eSNicolas Bonnefon #endif 27*84b2179eSNicolas Bonnefon 28*84b2179eSNicolas Bonnefon #include "log.h" 29*84b2179eSNicolas Bonnefon 30*84b2179eSNicolas Bonnefon std::shared_ptr<WatchTower> PlatformFileWatcher::watch_tower_; 31*84b2179eSNicolas Bonnefon 32*84b2179eSNicolas Bonnefon PlatformFileWatcher::PlatformFileWatcher() : FileWatcher() 33*84b2179eSNicolas Bonnefon { 34*84b2179eSNicolas Bonnefon // Caution, this is NOT thread-safe or re-entrant! 35*84b2179eSNicolas Bonnefon if ( !watch_tower_ ) 36*84b2179eSNicolas Bonnefon { 37*84b2179eSNicolas Bonnefon #ifdef _WIN32 38*84b2179eSNicolas Bonnefon watch_tower_ = std::make_shared<WinWatchTower>(); 39*84b2179eSNicolas Bonnefon #else 40*84b2179eSNicolas Bonnefon watch_tower_ = std::make_shared<INotifyWatchTower>(); 41*84b2179eSNicolas Bonnefon #endif 42*84b2179eSNicolas Bonnefon } 43*84b2179eSNicolas Bonnefon } 44*84b2179eSNicolas Bonnefon 45*84b2179eSNicolas Bonnefon PlatformFileWatcher::~PlatformFileWatcher() 46*84b2179eSNicolas Bonnefon { 47*84b2179eSNicolas Bonnefon } 48*84b2179eSNicolas Bonnefon 49*84b2179eSNicolas Bonnefon void PlatformFileWatcher::addFile( const QString& fileName ) 50*84b2179eSNicolas Bonnefon { 51*84b2179eSNicolas Bonnefon LOG(logDEBUG) << "FileWatcher::addFile " << fileName.toStdString(); 52*84b2179eSNicolas Bonnefon 53*84b2179eSNicolas Bonnefon watched_file_name_ = fileName; 54*84b2179eSNicolas Bonnefon 55*84b2179eSNicolas Bonnefon notification_ = std::make_shared<WatchTower::Registration>( 56*84b2179eSNicolas Bonnefon watch_tower_->addFile( fileName.toStdString(), [this, fileName] { 57*84b2179eSNicolas Bonnefon emit fileChanged( fileName ); } ) ); 58*84b2179eSNicolas Bonnefon } 59*84b2179eSNicolas Bonnefon 60*84b2179eSNicolas Bonnefon void PlatformFileWatcher::removeFile( const QString& fileName ) 61*84b2179eSNicolas Bonnefon { 62*84b2179eSNicolas Bonnefon LOG(logDEBUG) << "FileWatcher::removeFile " << fileName.toStdString(); 63*84b2179eSNicolas Bonnefon 64*84b2179eSNicolas Bonnefon notification_ = nullptr; 65*84b2179eSNicolas Bonnefon } 66