xref: /glogg/src/platformfilewatcher.cpp (revision b278d1839115e4ea1ab008ee6886473f3dccbe8b)
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 std::shared_ptr<WatchTower> PlatformFileWatcher::watch_tower_;
31 
32 PlatformFileWatcher::PlatformFileWatcher() : FileWatcher()
33 {
34     // Caution, this is NOT thread-safe or re-entrant!
35     if ( !watch_tower_ )
36     {
37 #ifdef _WIN32
38         watch_tower_ = std::make_shared<WinWatchTower>();
39 #else
40         watch_tower_ = std::make_shared<WatchTower>();
41 #endif
42     }
43 }
44 
45 PlatformFileWatcher::~PlatformFileWatcher()
46 {
47 }
48 
49 void PlatformFileWatcher::addFile( const QString& fileName )
50 {
51     LOG(logDEBUG) << "FileWatcher::addFile " << fileName.toStdString();
52 
53     watched_file_name_ = fileName;
54 
55     notification_ = std::make_shared<WatchTower::Registration>(
56             watch_tower_->addFile( fileName.toStdString(), [this, fileName] {
57                 emit fileChanged( fileName ); } ) );
58 }
59 
60 void PlatformFileWatcher::removeFile( const QString& fileName )
61 {
62     LOG(logDEBUG) << "FileWatcher::removeFile " << fileName.toStdString();
63 
64     notification_ = nullptr;
65 }
66