1 /* 2 * Copyright (C) 2010 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 FILEWATCHER_H 21 #define FILEWATCHER_H 22 23 #include <QObject> 24 #include <QFileSystemWatcher> 25 26 // This class encapsulate Qt's QFileSystemWatcher and additionally support 27 // watching a file that doesn't exist yet (the class will watch the owning 28 // directory) 29 // Only supports one file at the moment. 30 class FileWatcher : public QObject { 31 Q_OBJECT 32 33 public: 34 // Create an empty object 35 FileWatcher(); 36 // Destroy the object 37 ~FileWatcher(); 38 39 // Adds the file to the list of file to watch 40 // (do nothing if a file is already monitored) 41 void addFile( const QString& fileName ); 42 // Removes the file to the list of file to watch 43 // (do nothing if said file is not monitored) 44 void removeFile( const QString& fileName ); 45 46 signals: 47 // Sent when the file on disk has changed in any way. 48 void fileChanged( const QString& ); 49 50 private slots: 51 void fileChangedOnDisk( const QString& filename ); 52 void directoryChangedOnDisk( const QString& filename ); 53 54 private: 55 enum MonitoringState { None, FileExists, FileRemoved }; 56 57 QFileSystemWatcher qtFileWatcher_; 58 QString fileMonitored_; 59 MonitoringState monitoringState_; 60 }; 61 62 #endif 63