xref: /glogg/src/filewatcher.h (revision bb02e0acf44ddb4e4f83d6127a1e488789162922)
1*bb02e0acSNicolas Bonnefon /*
2*bb02e0acSNicolas Bonnefon  * Copyright (C) 2010 Nicolas Bonnefon and other contributors
3*bb02e0acSNicolas Bonnefon  *
4*bb02e0acSNicolas Bonnefon  * This file is part of glogg.
5*bb02e0acSNicolas Bonnefon  *
6*bb02e0acSNicolas Bonnefon  * glogg is free software: you can redistribute it and/or modify
7*bb02e0acSNicolas Bonnefon  * it under the terms of the GNU General Public License as published by
8*bb02e0acSNicolas Bonnefon  * the Free Software Foundation, either version 3 of the License, or
9*bb02e0acSNicolas Bonnefon  * (at your option) any later version.
10*bb02e0acSNicolas Bonnefon  *
11*bb02e0acSNicolas Bonnefon  * glogg is distributed in the hope that it will be useful,
12*bb02e0acSNicolas Bonnefon  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13*bb02e0acSNicolas Bonnefon  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14*bb02e0acSNicolas Bonnefon  * GNU General Public License for more details.
15*bb02e0acSNicolas Bonnefon  *
16*bb02e0acSNicolas Bonnefon  * You should have received a copy of the GNU General Public License
17*bb02e0acSNicolas Bonnefon  * along with glogg.  If not, see <http://www.gnu.org/licenses/>.
18*bb02e0acSNicolas Bonnefon  */
19*bb02e0acSNicolas Bonnefon 
20*bb02e0acSNicolas Bonnefon #ifndef FILEWATCHER_H
21*bb02e0acSNicolas Bonnefon #define FILEWATCHER_H
22*bb02e0acSNicolas Bonnefon 
23*bb02e0acSNicolas Bonnefon #include <QObject>
24*bb02e0acSNicolas Bonnefon #include <QFileSystemWatcher>
25*bb02e0acSNicolas Bonnefon 
26*bb02e0acSNicolas Bonnefon // This class encapsulate Qt's QFileSystemWatcher and additionally support
27*bb02e0acSNicolas Bonnefon // watching a file that doesn't exist yet (the class will watch the owning
28*bb02e0acSNicolas Bonnefon // directory)
29*bb02e0acSNicolas Bonnefon // Only supports one file at the moment.
30*bb02e0acSNicolas Bonnefon class FileWatcher : public QObject {
31*bb02e0acSNicolas Bonnefon   Q_OBJECT
32*bb02e0acSNicolas Bonnefon 
33*bb02e0acSNicolas Bonnefon   public:
34*bb02e0acSNicolas Bonnefon     // Create an empty object
35*bb02e0acSNicolas Bonnefon     FileWatcher();
36*bb02e0acSNicolas Bonnefon     // Destroy the object
37*bb02e0acSNicolas Bonnefon     ~FileWatcher();
38*bb02e0acSNicolas Bonnefon 
39*bb02e0acSNicolas Bonnefon     // Adds the file to the list of file to watch
40*bb02e0acSNicolas Bonnefon     // (do nothing if a file is already monitored)
41*bb02e0acSNicolas Bonnefon     void addFile( const QString& fileName );
42*bb02e0acSNicolas Bonnefon     // Removes the file to the list of file to watch
43*bb02e0acSNicolas Bonnefon     // (do nothing if said file is not monitored)
44*bb02e0acSNicolas Bonnefon     void removeFile( const QString& fileName );
45*bb02e0acSNicolas Bonnefon 
46*bb02e0acSNicolas Bonnefon   signals:
47*bb02e0acSNicolas Bonnefon     // Sent when the file on disk has changed in any way.
48*bb02e0acSNicolas Bonnefon     void fileChanged( const QString& );
49*bb02e0acSNicolas Bonnefon 
50*bb02e0acSNicolas Bonnefon   private slots:
51*bb02e0acSNicolas Bonnefon     void fileChangedOnDisk( const QString& filename );
52*bb02e0acSNicolas Bonnefon     void directoryChangedOnDisk( const QString& filename );
53*bb02e0acSNicolas Bonnefon 
54*bb02e0acSNicolas Bonnefon   private:
55*bb02e0acSNicolas Bonnefon     enum MonitoringState { None, FileExists, FileRemoved };
56*bb02e0acSNicolas Bonnefon 
57*bb02e0acSNicolas Bonnefon     QFileSystemWatcher qtFileWatcher_;
58*bb02e0acSNicolas Bonnefon     QString fileMonitored_;
59*bb02e0acSNicolas Bonnefon     MonitoringState monitoringState_;
60*bb02e0acSNicolas Bonnefon };
61*bb02e0acSNicolas Bonnefon 
62*bb02e0acSNicolas Bonnefon #endif
63