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