xref: /glogg/src/qtfilewatcher.cpp (revision 6a12446ea5d0b31e9ae87ae6561fe4e6f45da437)
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 #include "log.h"
21 
22 #include "qtfilewatcher.h"
23 
24 #include <QStringList>
25 #include <QFileInfo>
26 
QtFileWatcher()27 QtFileWatcher::QtFileWatcher() : FileWatcher(), qtFileWatcher_( this )
28 {
29     monitoringState_ = None;
30 
31     connect( &qtFileWatcher_, SIGNAL( fileChanged( const QString& ) ),
32             this, SLOT( fileChangedOnDisk( const QString& ) ) );
33 
34     connect( &qtFileWatcher_, SIGNAL( directoryChanged( const QString& ) ),
35             this, SLOT( directoryChangedOnDisk( const QString& ) ) );
36 }
37 
~QtFileWatcher()38 QtFileWatcher::~QtFileWatcher()
39 {
40     disconnect( &qtFileWatcher_ );
41 }
42 
addFile(const QString & fileName)43 void QtFileWatcher::addFile( const QString& fileName )
44 {
45     LOG(logDEBUG) << "QtFileWatcher::addFile " << fileName.toStdString();
46 
47     QFileInfo fileInfo = QFileInfo( fileName );
48 
49     if ( fileMonitored_.isEmpty() ) {
50         fileMonitored_ = fileName;
51 
52         // Initialise the Qt file watcher
53         qtFileWatcher_.addPath( fileInfo.path() );
54 
55         if ( fileInfo.exists() ) {
56             LOG(logDEBUG) << "QtFileWatcher::addFile: file exists.";
57             qtFileWatcher_.addPath( fileName );
58             monitoringState_ = FileExists;
59         }
60         else {
61             LOG(logDEBUG) << "QtFileWatcher::addFile: file doesn't exist.";
62             monitoringState_ = FileRemoved;
63         }
64     }
65     else {
66         LOG(logWARNING) << "QtFileWatcher::addFile " << fileName.toStdString()
67             << "- Already watching a file (" << fileMonitored_.toStdString()
68             << ")!";
69     }
70 }
71 
removeFile(const QString & fileName)72 void QtFileWatcher::removeFile( const QString& fileName )
73 {
74     LOG(logDEBUG) << "QtFileWatcher::removeFile " << fileName.toStdString();
75 
76     QFileInfo fileInfo = QFileInfo( fileName );
77 
78     if ( fileName == fileMonitored_ ) {
79         if ( monitoringState_ == FileExists )
80             qtFileWatcher_.removePath( fileName );
81         qtFileWatcher_.removePath( fileInfo.path() );
82         fileMonitored_.clear();
83         monitoringState_ = None;
84     }
85     else {
86         LOG(logWARNING) << "QtFileWatcher::removeFile - The file is not watched!";
87     }
88 
89     // For debug purpose:
90     foreach (QString str, qtFileWatcher_.files()) {
91         LOG(logERROR) << "File still watched: " << str.toStdString();
92     }
93     foreach (QString str, qtFileWatcher_.directories()) {
94         LOG(logERROR) << "Directories still watched: " << str.toStdString();
95     }
96 }
97 
98 //
99 // Slots
100 //
101 
fileChangedOnDisk(const QString & filename)102 void QtFileWatcher::fileChangedOnDisk( const QString& filename )
103 {
104     LOG(logDEBUG) << "QtFileWatcher::fileChangedOnDisk " << filename.toStdString();
105 
106     if ( ( monitoringState_ == FileExists ) && ( filename == fileMonitored_ ) )
107     {
108         emit fileChanged( filename );
109 
110         // If the file has been removed...
111         if ( !QFileInfo( filename ).exists() )
112             monitoringState_ = FileRemoved;
113     }
114     else
115         LOG(logWARNING) << "QtFileWatcher::fileChangedOnDisk - call from Qt but no file monitored";
116 }
117 
directoryChangedOnDisk(const QString & filename)118 void QtFileWatcher::directoryChangedOnDisk( const QString& filename )
119 {
120     LOG(logDEBUG) << "QtFileWatcher::directoryChangedOnDisk " << filename.toStdString();
121 
122     if ( monitoringState_ == FileRemoved ) {
123         if ( QFileInfo( fileMonitored_ ).exists() ) {
124             LOG(logDEBUG) << "QtFileWatcher::directoryChangedOnDisk - our file reappeared!";
125 
126             // The file has been recreated, we have to watch it again.
127             monitoringState_ = FileExists;
128 
129             // Restore the Qt file watcher (automatically cancelled
130             // when the file is deleted)
131             qtFileWatcher_.addPath( fileMonitored_ );
132 
133             emit fileChanged( fileMonitored_ );
134         }
135         else {
136             LOG(logWARNING) << "QtFileWatcher::directoryChangedOnDisk - not the file we are watching";
137         }
138     }
139     else if ( monitoringState_ == FileExists )
140     {
141         if ( ! QFileInfo( fileMonitored_ ).exists() ) {
142             LOG(logDEBUG) << "QtFileWatcher::directoryChangedOnDisk - our file disappeared!";
143 
144             monitoringState_ = FileRemoved;
145 
146             emit fileChanged( filename );
147         }
148         else {
149             LOG(logWARNING) << "QtFileWatcher::directoryChangedOnDisk - not the file we are watching";
150         }
151     }
152 
153 }
154