xref: /glogg/src/platformfilewatcher.cpp (revision 9f850936f193b6e0057829e50d76cbf76e3a62c7) !
184b2179eSNicolas Bonnefon /*
280bca0a3SNicolas Bonnefon  * Copyright (C) 2010, 2014, 2015 Nicolas Bonnefon and other contributors
384b2179eSNicolas Bonnefon  *
484b2179eSNicolas Bonnefon  * This file is part of glogg.
584b2179eSNicolas Bonnefon  *
684b2179eSNicolas Bonnefon  * glogg is free software: you can redistribute it and/or modify
784b2179eSNicolas Bonnefon  * it under the terms of the GNU General Public License as published by
884b2179eSNicolas Bonnefon  * the Free Software Foundation, either version 3 of the License, or
984b2179eSNicolas Bonnefon  * (at your option) any later version.
1084b2179eSNicolas Bonnefon  *
1184b2179eSNicolas Bonnefon  * glogg is distributed in the hope that it will be useful,
1284b2179eSNicolas Bonnefon  * but WITHOUT ANY WARRANTY; without even the implied warranty of
1384b2179eSNicolas Bonnefon  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1484b2179eSNicolas Bonnefon  * GNU General Public License for more details.
1584b2179eSNicolas Bonnefon  *
1684b2179eSNicolas Bonnefon  * You should have received a copy of the GNU General Public License
1784b2179eSNicolas Bonnefon  * along with glogg.  If not, see <http://www.gnu.org/licenses/>.
1884b2179eSNicolas Bonnefon  */
1984b2179eSNicolas Bonnefon 
2084b2179eSNicolas Bonnefon #include "platformfilewatcher.h"
2184b2179eSNicolas Bonnefon 
2284b2179eSNicolas Bonnefon #include "log.h"
2384b2179eSNicolas Bonnefon 
24f09fa651SNicolas Bonnefon std::shared_ptr<PlatformFileWatcher::PlatformWatchTower> PlatformFileWatcher::watch_tower_;
2584b2179eSNicolas Bonnefon 
PlatformFileWatcher()2684b2179eSNicolas Bonnefon PlatformFileWatcher::PlatformFileWatcher() : FileWatcher()
2784b2179eSNicolas Bonnefon {
2884b2179eSNicolas Bonnefon     // Caution, this is NOT thread-safe or re-entrant!
2984b2179eSNicolas Bonnefon     if ( !watch_tower_ )
3084b2179eSNicolas Bonnefon     {
3196bde7d5SNicolas Bonnefon         watch_tower_ = std::make_shared<PlatformWatchTower>();
3284b2179eSNicolas Bonnefon     }
3384b2179eSNicolas Bonnefon }
3484b2179eSNicolas Bonnefon 
~PlatformFileWatcher()3584b2179eSNicolas Bonnefon PlatformFileWatcher::~PlatformFileWatcher()
3684b2179eSNicolas Bonnefon {
3784b2179eSNicolas Bonnefon }
3884b2179eSNicolas Bonnefon 
addFile(const QString & fileName)3984b2179eSNicolas Bonnefon void PlatformFileWatcher::addFile( const QString& fileName )
4084b2179eSNicolas Bonnefon {
4184b2179eSNicolas Bonnefon     LOG(logDEBUG) << "FileWatcher::addFile " << fileName.toStdString();
4284b2179eSNicolas Bonnefon 
4384b2179eSNicolas Bonnefon     watched_file_name_ = fileName;
4484b2179eSNicolas Bonnefon 
45*9f850936SNicolas Bonnefon     removeFile( fileName );
4696bde7d5SNicolas Bonnefon     notification_ = std::make_shared<Registration>(
4784b2179eSNicolas Bonnefon             watch_tower_->addFile( fileName.toStdString(), [this, fileName] {
4884b2179eSNicolas Bonnefon                 emit fileChanged( fileName ); } ) );
4984b2179eSNicolas Bonnefon }
5084b2179eSNicolas Bonnefon 
removeFile(const QString & fileName)5184b2179eSNicolas Bonnefon void PlatformFileWatcher::removeFile( const QString& fileName )
5284b2179eSNicolas Bonnefon {
5384b2179eSNicolas Bonnefon     LOG(logDEBUG) << "FileWatcher::removeFile " << fileName.toStdString();
5484b2179eSNicolas Bonnefon 
5584b2179eSNicolas Bonnefon     notification_ = nullptr;
5684b2179eSNicolas Bonnefon }
5780bca0a3SNicolas Bonnefon 
setPollingInterval(uint32_t interval_ms)5880bca0a3SNicolas Bonnefon void PlatformFileWatcher::setPollingInterval( uint32_t interval_ms )
5980bca0a3SNicolas Bonnefon {
6080bca0a3SNicolas Bonnefon     watch_tower_->setPollingInterval( interval_ms );
6180bca0a3SNicolas Bonnefon }
62