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