1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
|
/*
* Copyright (C) 2010 Nicolas Bonnefon and other contributors
*
* This file is part of glogg.
*
* glogg is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* glogg is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with glogg. If not, see <http://www.gnu.org/licenses/>.
*/
#include "log.h"
#include "filewatcher.h"
#include <QStringList>
#include <QFileInfo>
FileWatcher::FileWatcher() : qtFileWatcher_( this )
{
monitoringState_ = None;
connect( &qtFileWatcher_, SIGNAL( fileChanged( const QString& ) ),
this, SLOT( fileChangedOnDisk( const QString& ) ) );
connect( &qtFileWatcher_, SIGNAL( directoryChanged( const QString& ) ),
this, SLOT( directoryChangedOnDisk( const QString& ) ) );
}
FileWatcher::~FileWatcher()
{
disconnect( &qtFileWatcher_ );
}
void FileWatcher::addFile( const QString& fileName )
{
LOG(logDEBUG) << "FileWatcher::addFile " << fileName.toStdString();
QFileInfo fileInfo = QFileInfo( fileName );
if ( fileMonitored_.isEmpty() ) {
fileMonitored_ = fileName;
// Initialise the Qt file watcher
qtFileWatcher_.addPath( fileInfo.path() );
if ( fileInfo.exists() ) {
LOG(logDEBUG) << "FileWatcher::addFile: file exists.";
qtFileWatcher_.addPath( fileName );
monitoringState_ = FileExists;
}
else {
LOG(logDEBUG) << "FileWatcher::addFile: file doesn't exist.";
monitoringState_ = FileRemoved;
}
}
else {
LOG(logWARNING) << "FileWatcher::addFile " << fileName.toStdString()
<< "- Already watching a file (" << fileMonitored_.toStdString()
<< ")!";
}
}
void FileWatcher::removeFile( const QString& fileName )
{
LOG(logDEBUG) << "FileWatcher::removeFile " << fileName.toStdString();
QFileInfo fileInfo = QFileInfo( fileName );
if ( fileName == fileMonitored_ ) {
if ( monitoringState_ == FileExists )
qtFileWatcher_.removePath( fileName );
qtFileWatcher_.removePath( fileInfo.path() );
fileMonitored_.clear();
monitoringState_ = None;
}
else {
LOG(logWARNING) << "FileWatcher::removeFile - The file is not watched!";
}
// For debug purpose:
foreach (QString str, qtFileWatcher_.files()) {
LOG(logERROR) << "File still watched: " << str.toStdString();
}
foreach (QString str, qtFileWatcher_.directories()) {
LOG(logERROR) << "Directories still watched: " << str.toStdString();
}
}
//
// Slots
//
void FileWatcher::fileChangedOnDisk( const QString& filename )
{
LOG(logDEBUG) << "FileWatcher::fileChangedOnDisk " << filename.toStdString();
if ( ( monitoringState_ == FileExists ) && ( filename == fileMonitored_ ) )
{
emit fileChanged( filename );
// If the file has been removed...
if ( !QFileInfo( filename ).exists() )
monitoringState_ = FileRemoved;
}
else
LOG(logWARNING) << "FileWatcher::fileChangedOnDisk - call from Qt but no file monitored";
}
void FileWatcher::directoryChangedOnDisk( const QString& filename )
{
LOG(logDEBUG) << "FileWatcher::directoryChangedOnDisk " << filename.toStdString();
if ( monitoringState_ == FileRemoved ) {
if ( QFileInfo( fileMonitored_ ).exists() ) {
LOG(logDEBUG) << "FileWatcher::directoryChangedOnDisk - our file reappeared!";
// The file has been recreated, we have to watch it again.
monitoringState_ = FileExists;
// Restore the Qt file watcher (automatically cancelled
// when the file is deleted)
qtFileWatcher_.addPath( fileMonitored_ );
emit fileChanged( fileMonitored_ );
}
else {
LOG(logWARNING) << "FileWatcher::directoryChangedOnDisk - not the file we are watching";
}
}
else if ( monitoringState_ == FileExists )
{
if ( ! QFileInfo( fileMonitored_ ).exists() ) {
LOG(logDEBUG) << "FileWatcher::directoryChangedOnDisk - our file disappeared!";
monitoringState_ = FileRemoved;
emit fileChanged( filename );
}
else {
LOG(logWARNING) << "FileWatcher::directoryChangedOnDisk - not the file we are watching";
}
}
}
|