xref: /glogg/src/platformfilewatcher.h (revision c633ced33b4f2c2cf77c0c80a15fa73a7f13ad9f)
1 /*
2  * Copyright (C) 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 #ifndef PLATFORMFILEWATCHER_H
21 #define PLATFORMFILEWATCHER_H
22 
23 #include "filewatcher.h"
24 
25 #include <memory>
26 
27 #ifdef _WIN32
28 #  include "winwatchtowerdriver.h"
29 #else
30 #  include "inotifywatchtowerdriver.h"
31 #endif
32 
33 #include "watchtower.h"
34 
35 class INotifyWatchTower;
36 
37 // Please note that due to the implementation of the constructor
38 // this class is not thread safe and shall always be used from the main UI thread.
39 class PlatformFileWatcher : public FileWatcher {
40   Q_OBJECT
41 
42   public:
43     // Create the empty object
44     PlatformFileWatcher();
45     // Destroy the object
46     ~PlatformFileWatcher();
47 
48     void addFile( const QString& fileName );
49     void removeFile( const QString& fileName );
50 
51     // Set the polling interval (0 means disabled)
52     void setPollingInterval( uint32_t interval_ms );
53 
54   signals:
55     void fileChanged( const QString& );
56 
57   private:
58 #ifdef _WIN32
59 #  ifdef HAS_TEMPLATE_ALIASES
60 using PlatformWatchTower = WatchTower<WinWatchTowerDriver>;
61 #  else
62 typedef WatchTower<WinWatchTowerDriver> PlatformWatchTower;
63 #  endif
64 #else
65 #  ifdef HAS_TEMPLATE_ALIASES
66 using PlatformWatchTower = WatchTower<INotifyWatchTowerDriver>;
67 #  else
68 typedef WatchTower<INotifyWatchTowerDriver> PlatformWatchTower;
69 #  endif
70 #endif
71 
72     // The following variables are protected by watched_files_mutex_
73     QString watched_file_name_;
74 
75     // Reference to the (unique) watchtower.
76     static std::shared_ptr<PlatformWatchTower> watch_tower_;
77 
78     std::shared_ptr<Registration> notification_;
79 };
80 
81 #endif
82