Back to index

scribus-ng  1.3.4.dfsg+svn20071115
filewatcher.cpp
Go to the documentation of this file.
00001 /*
00002 For general Scribus (>=1.3.2) copyright and licensing information please refer
00003 to the COPYING file provided with the program. Following this notice may exist
00004 a copyright and/or license notice that predates the release of Scribus 1.3.2
00005 for which a new license (GPL+exception) is in place.
00006 */
00007 #include "filewatcher.h"
00008 #include "filewatcher.moc"
00009 
00010 #include "scconfig.h"
00011 
00012 #ifdef HAVE_UNISTD_H
00013 #include <unistd.h>
00014 #endif
00015 
00016 #ifdef _WIN32
00017 #include <windows.h>
00018 #endif
00019 
00020 FileWatcher::FileWatcher( QObject* parent) : QObject(parent)
00021 {
00022        m_timeOut=10000;
00023        watchedFiles.clear();
00024        watchTimer = new QTimer(this);
00025        connect(watchTimer, SIGNAL(timeout()), this, SLOT(checkFiles()));
00026        watchTimer->start(m_timeOut, true);
00027        blockAddRemove = false;
00028        stopped = false;
00029 }
00030 
00031 FileWatcher::~FileWatcher()
00032 {
00033        watchTimer->stop();
00034        disconnect(watchTimer, SIGNAL(timeout()), this, SLOT(checkFiles()));
00035        watchedFiles.clear();
00036        delete watchTimer;
00037 }
00038 
00039 void FileWatcher::setTimeOut(const int newTimeOut, const bool restartTimer)
00040 {
00041        m_timeOut=newTimeOut;
00042        if (restartTimer)
00043               start();
00044 }
00045 
00046 const int FileWatcher::timeOut()
00047 {
00048        return m_timeOut;
00049 }
00050 
00051 void FileWatcher::addFile(QString fileName)
00052 {
00053        watchTimer->stop();
00054        if (!watchedFiles.contains(fileName))
00055        {
00056               struct fileMod fi;
00057               fi.info = QFileInfo(fileName);
00058               fi.timeInfo = fi.info.lastModified();
00059               fi.pendingCount = 0;
00060               fi.pending = false;
00061               watchedFiles.insert(fileName, fi);
00062        }
00063        if (!stopped)
00064               watchTimer->start(m_timeOut, true);
00065 }
00066 
00067 void FileWatcher::removeFile(QString fileName)
00068 {
00069        watchTimer->stop();
00070        if (watchedFiles.contains(fileName))
00071               watchedFiles.remove(fileName);
00072        if (!stopped)
00073               watchTimer->start(m_timeOut, true);
00074 }
00075 
00076 void FileWatcher::addDir(QString fileName)
00077 {
00078        addFile(fileName);
00079 }
00080 
00081 void FileWatcher::removeDir(QString fileName)
00082 {
00083        removeFile(fileName);
00084 }
00085 
00086 void FileWatcher::start()
00087 {
00088        watchTimer->stop();
00089        stopped = false;
00090        watchTimer->start(m_timeOut, true);
00091 }
00092 
00093 void FileWatcher::stop()
00094 {
00095        watchTimer->stop();
00096        stopped = true;
00097 }
00098 
00099 void FileWatcher::forceScan()
00100 {
00101        checkFiles();
00102 }
00103 
00104 bool FileWatcher::isActive()
00105 {
00106        return blockAddRemove;
00107 }
00108 
00109 QValueList<QString> FileWatcher::files()
00110 {
00111        return watchedFiles.keys();
00112 }
00113 
00114 void FileWatcher::checkFiles()
00115 {
00116        blockAddRemove = true;
00117        watchTimer->stop();
00118        stopped = true;
00119        QDateTime time;
00120        QStringList toRemove;
00121        QMap<QString, fileMod>::Iterator it;
00122        for ( it = watchedFiles.begin(); it != watchedFiles.end(); ++it )
00123        {
00124               it.data().info.refresh();
00125               if (!it.data().info.exists())
00126               {
00127                      if (!it.data().pending)
00128                      {
00129                             it.data().pendingCount = 5;
00130                             it.data().pending = true;
00131                             emit statePending(it.key());
00132                             continue;
00133                      }
00134                      else
00135                      {
00136                             if (it.data().pendingCount != 0)
00137                             {
00138                                    it.data().pendingCount--;
00139                                    continue;
00140                             }
00141                             else
00142                             {
00143                                    it.data().pending = false;
00144                                    if (it.data().info.isDir())
00145                                           emit dirDeleted(it.key());
00146                                    else
00147                                           emit fileDeleted(it.key());
00148                                    toRemove.append(it.key());
00149                                    continue;
00150                             }
00151                      }
00152               }
00153               else
00154                {
00155                      it.data().pending = false;
00156                      time = it.data().info.lastModified();
00157                      if (time != it.data().timeInfo)
00158                      {
00159                             if (it.data().info.isDir())
00160                                    emit dirChanged(it.key());
00161                             else
00162                             {
00163                                    uint sizeo = it.data().info.size();
00164                      #ifndef _WIN32
00165                                    usleep(100);
00166                      #else
00167                                    Sleep(1);
00168                      #endif
00169                                    it.data().info.refresh();
00170                                    uint sizen = it.data().info.size();
00171                                    while (sizen != sizeo)
00172                                    {
00173                                           sizeo = sizen;
00174                             #ifndef _WIN32
00175                                           usleep(100);
00176                             #else
00177                                           Sleep(1);
00178                             #endif
00179                                           it.data().info.refresh();
00180                                           sizen = it.data().info.size();
00181                                    }
00182                                    it.data().timeInfo = time;
00183                                    emit fileChanged(it.key());
00184                             }
00185                      }
00186               }
00187        }
00188        for(unsigned int i=0; i<toRemove.count(); ++i)
00189               watchedFiles.remove(toRemove[i]);
00190        blockAddRemove = false;
00191        stopped = false;
00192        watchTimer->start(m_timeOut, true);
00193 }
00194