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