xref: /glogg/src/platformfilewatcher.cpp (revision dc7f591602f71a2ebc33e33129d447b789c2f683)
1 /*
2  * Copyright (C) 2010, 2014 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 #ifdef _WIN32
23 #include "winwatchtower.h"
24 #else
25 #include "inotifywatchtowerdriver.h"
26 #endif
27 
28 #include "log.h"
29 
30 #if __GNUC_MINOR__ < 7
31 typedef WatchTower<INotifyWatchTowerDriver> PlatformWatchTower;
32 #else
33 using PlatformWatchTower = WatchTower<INotifyWatchTowerDriver>;
34 #endif
35 
36 std::shared_ptr<PlatformWatchTower> PlatformFileWatcher::watch_tower_;
37 
38 PlatformFileWatcher::PlatformFileWatcher() : FileWatcher()
39 {
40     // Caution, this is NOT thread-safe or re-entrant!
41     if ( !watch_tower_ )
42     {
43 #ifdef _WIN32
44         watch_tower_ = std::make_shared<WinWatchTower>();
45 #else
46         watch_tower_ = std::make_shared<PlatformWatchTower>();
47 #endif
48     }
49 }
50 
51 PlatformFileWatcher::~PlatformFileWatcher()
52 {
53 }
54 
55 void PlatformFileWatcher::addFile( const QString& fileName )
56 {
57     LOG(logDEBUG) << "FileWatcher::addFile " << fileName.toStdString();
58 
59     watched_file_name_ = fileName;
60 
61     notification_ = std::make_shared<Registration>(
62             watch_tower_->addFile( fileName.toStdString(), [this, fileName] {
63                 emit fileChanged( fileName ); } ) );
64 }
65 
66 void PlatformFileWatcher::removeFile( const QString& fileName )
67 {
68     LOG(logDEBUG) << "FileWatcher::removeFile " << fileName.toStdString();
69 
70     notification_ = nullptr;
71 }
72