xref: /glogg/src/platformfilewatcher.h (revision 96bde7d5e1c8c71a3ad8ee8e2641c176dc312047)
1 /*
2  * Copyright (C) 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 #ifndef PLATFORMFILEWATCHER_H
21 #define PLATFORMFILEWATCHER_H
22 
23 #include "filewatcher.h"
24 
25 #include <memory>
26 
27 #include "watchtower.h"
28 
29 class INotifyWatchTower;
30 
31 // An implementation of FileWatcher, as an adapter to INotifyWatchTower.
32 // This is Linux only, and require a recent version of the kernel.
33 
34 // Please note that due to the implementation of the constructor
35 // this class is not thread safe and shall always be used from the main UI thread.
36 class PlatformFileWatcher : public FileWatcher {
37   Q_OBJECT
38 
39   public:
40     // Create the empty object
41     PlatformFileWatcher();
42     // Destroy the object
43     ~PlatformFileWatcher() override;
44 
45     void addFile( const QString& fileName ) override;
46     void removeFile( const QString& fileName ) override;
47 
48   signals:
49     void fileChanged( const QString& );
50 
51   private:
52     using PlatformWatchTower = WatchTower<INotifyWatchTowerDriver>;
53 
54     // The following variables are protected by watched_files_mutex_
55     QString watched_file_name_;
56 
57     // Reference to the (unique) watchtower.
58     static std::shared_ptr<PlatformWatchTower> watch_tower_;
59 
60     std::shared_ptr<Registration> notification_;
61 };
62 
63 #endif
64