Since I had a request,

Here is my fileWatcher implementation.

It uses Pimpl and some custom functions, it won't work out of the box.

qkFileWatcher.h :
Qt Code:
  1. #ifndef QKFILEWATCHER_H
  2. #define QKFILEWATCHER_H
  3.  
  4. #include <qkGlobalHeader.h>
  5.  
  6. // Qt includes
  7. #include <qobject>
  8. #include <qdateTime>
  9.  
  10. class qkFileWatcherPrivate;
  11.  
  12. class qkFileWatcher;
  13.  
  14. class QKCORE_EXPORT qkFileWatch
  15. {
  16. public: // Enums
  17. enum FileType
  18. {
  19. invalidType = 0,
  20. fileType,
  21. folderType
  22. };
  23.  
  24. public: // Ctor
  25. qkFileWatch(qkFileWatcher * watcher);
  26. qkFileWatch(const QString & path, qkFileWatcher * watcher);
  27. qkFileWatch(FileType type, const QString & path, qkFileWatcher * watcher);
  28. virtual ~qkFileWatch() {}
  29.  
  30. public: // Interface
  31. virtual void checkChange();
  32. void refreshDate();
  33.  
  34. protected: // Functions
  35. void request_fileModified(const QString parentPath, const QString fileName);
  36. void request_filesCreated(const QString parentPath, const QStringList fileNames);
  37. void request_filesDeleted(const QString parentPath, const QStringList fileNames);
  38.  
  39. void request_folderModified(const QString parentPath, const QString folderName);
  40. void request_foldersCreated(const QString parentPath, const QStringList folderNames);
  41. void request_foldersDeleted(const QString parentPath, const QStringList folderNames);
  42.  
  43. protected: // Variables
  44. QString mPath;
  45. QString mAbsolutePath;
  46. QString mName;
  47.  
  48. FileType mType;
  49.  
  50. QDateTime mLastModified;
  51.  
  52. qint64 mSize;
  53.  
  54. qkFileWatcher * mWatcher;
  55.  
  56. public: // Properties
  57. QString path() const;
  58. QString absolutePath() const;
  59. QString name() const;
  60.  
  61. bool isValid() const;
  62.  
  63. bool isFile() const;
  64. bool isDirectory() const;
  65. bool exists() const;
  66.  
  67. FileType type() const;
  68. };
  69.  
  70. class QKCORE_EXPORT qkFolderWatch : public qkFileWatch
  71. {
  72. public: // Ctor
  73. qkFolderWatch(const QString & path, qkFileWatcher * watcher);
  74.  
  75. public: // qkFileWatch interface reimplementation
  76. bool contains(const QString & path);
  77. virtual void checkChange();
  78.  
  79. private: // Functions
  80. void recurseDirectories();
  81. void checkDeleted();
  82.  
  83. int getFolderIndex_from_path(const QString & path);
  84. int getFileIndex_from_path(const QString & path);
  85.  
  86. private: // Variables
  87. QList<qkFileWatch> mFileWatchs;
  88. QList<qkFolderWatch> mFolderWatchs;
  89. };
  90.  
  91. class QKCORE_EXPORT qkFileWatcher : public QObject, public qkPrivatable
  92. {
  93. Q_OBJECT
  94. friend class qkFileWatch;
  95. friend class qkFolderWatch;
  96. private:
  97. QK_DECLARE_PRIVATE(qkFileWatcher);
  98.  
  99. public:
  100. qkFileWatcher(QObject * parent = 0);
  101. virtual ~qkFileWatcher();
  102.  
  103. public: // Interface
  104. void addPath(const QString & path);
  105. void removePath(const QString & path);
  106.  
  107. bool contains(const QString & path);
  108.  
  109. private slots:
  110. void onCheckForChange();
  111.  
  112. private: // Functions
  113. void request_folderModified(const QString parentPath, const QString folderName);
  114. void request_foldersCreated(const QString parentPath, const QStringList folderNames);
  115. void request_foldersDeleted(const QString parentPath, const QStringList folderNames);
  116.  
  117. void request_fileModified(const QString parentPath, const QString fileName);
  118. void request_filesCreated(const QString parentPath, const QStringList fileNames);
  119. void request_filesDeleted(const QString parentPath, const QStringList fileNames);
  120.  
  121. signals:
  122. // Folder
  123. void folderModified(const QString parentPath, const QString folderName);
  124. void foldersCreated(const QString parentPath, const QStringList folderNames);
  125. void foldersDeleted(const QString parentPath, const QStringList folderNames);
  126.  
  127. // File
  128. void fileModified(const QString parentPath, const QString fileName);
  129. void filesCreated(const QString parentPath, const QStringList fileNames);
  130. void filesDeleted(const QString parentPath, const QStringList fileNames);
  131. };
  132.  
  133. #endif // QKFILEWATCHER_H
To copy to clipboard, switch view to plain text mode 

qkFileWatcher.cpp part1 :

Qt Code:
  1. #include "qkFileWatcher.h"
  2.  
  3. // Qt includes
  4. #include <qtimer>
  5.  
  6. // qk includes
  7. #include <qkFileController.h>
  8.  
  9. //=============================================================================
  10. // qkFileWatch
  11. //=============================================================================
  12.  
  13. qkFileWatch::qkFileWatch(FileType type, const QString & path, qkFileWatcher * watcher)
  14. {
  15. QFileInfo info(path);
  16.  
  17. mWatcher = watcher;
  18.  
  19. mPath = path;
  20. mAbsolutePath = info.absolutePath();
  21. mName = info.fileName();
  22. mType = type;
  23. mSize = -1;
  24.  
  25. refreshDate();
  26. }
  27.  
  28. qkFileWatch::qkFileWatch(const QString & path, qkFileWatcher * watcher)
  29. {
  30. QFileInfo info(path);
  31.  
  32. mWatcher = watcher;
  33.  
  34. mPath = path;
  35. mAbsolutePath = info.absolutePath();
  36. mName = info.fileName();
  37. mType = fileType;
  38. mSize = -1;
  39.  
  40. refreshDate();
  41. }
  42.  
  43. qkFileWatch::qkFileWatch(qkFileWatcher * watcher)
  44. {
  45. mWatcher = watcher;
  46.  
  47. mType = invalidType;
  48. mSize = -1;
  49.  
  50. refreshDate();
  51. }
  52.  
  53. //=============================================================================
  54.  
  55. void qkFileWatch::request_fileModified(const QString parentPath, const QString fileName)
  56. {
  57. mWatcher->request_fileModified(parentPath, fileName);
  58. }
  59.  
  60. void qkFileWatch::request_filesCreated(const QString parentPath, const QStringList fileNames)
  61. {
  62. mWatcher->request_filesCreated(parentPath, fileNames);
  63. }
  64.  
  65. void qkFileWatch::request_filesDeleted(const QString parentPath, const QStringList fileNames)
  66. {
  67. mWatcher->request_filesDeleted(parentPath, fileNames);
  68. }
  69.  
  70. //=============================================================================
  71.  
  72. void qkFileWatch::request_folderModified(const QString parentPath, const QString folderName)
  73. {
  74. mWatcher->request_folderModified(parentPath, folderName);
  75. }
  76.  
  77. void qkFileWatch::request_foldersCreated(const QString parentPath, const QStringList folderNames)
  78. {
  79. mWatcher->request_foldersCreated(parentPath, folderNames);
  80. }
  81.  
  82. void qkFileWatch::request_foldersDeleted(const QString parentPath, const QStringList folderNames)
  83. {
  84. mWatcher->request_foldersDeleted(parentPath, folderNames);
  85. }
  86.  
  87. //=============================================================================
  88.  
  89. void qkFileWatch::refreshDate()
  90. {
  91. QFileInfo info(mPath);
  92. if (info.exists() == false)
  93. {
  94. qkDebug("Deleted %s !", mPath.C_STR);
  95.  
  96. mSize = -1;
  97. mLastModified = QDateTime();
  98.  
  99. return;
  100. }
  101.  
  102. mSize = info.size();
  103. mLastModified = info.lastModified();
  104. }
  105.  
  106. /* virtual */ void qkFileWatch::checkChange()
  107. {
  108. QDateTime oldModified = mLastModified;
  109. qint64 oldSize = mSize;
  110.  
  111. refreshDate();
  112.  
  113. // We check both modified date and size for greater precision
  114. if (oldModified < mLastModified || oldSize != mSize)
  115. {
  116. if (mType == qkFileWatch::folderType)
  117. {
  118. request_folderModified(mAbsolutePath, mName);
  119. }
  120. else if (mType == qkFileWatch::fileType)
  121. {
  122. request_fileModified(mAbsolutePath, mName);
  123. }
  124. }
  125. }
  126.  
  127. //=============================================================================
  128.  
  129. QString qkFileWatch::path() const { return mPath; }
  130.  
  131. QString qkFileWatch::absolutePath() const { return mAbsolutePath; }
  132.  
  133. QString qkFileWatch::name() const { return mName; }
  134.  
  135. bool qkFileWatch::isValid() const
  136. {
  137. if (mType == invalidType) return false;
  138. else return true;
  139. }
  140.  
  141. bool qkFileWatch::isFile() const
  142. {
  143. if (mType == fileType) return true;
  144. else return false;
  145. }
  146.  
  147. bool qkFileWatch::isDirectory() const
  148. {
  149. if (mType == folderType) return true;
  150. else return false;
  151. }
  152.  
  153. bool qkFileWatch::exists() const
  154. {
  155. if (mSize == -1) return false;
  156. else return true;
  157. }
  158.  
  159. qkFileWatch::FileType qkFileWatch::type() const
  160. {
  161. return mType;
  162. }
  163.  
  164. //=============================================================================
  165. // qkFolderWatch
  166. //=============================================================================
  167.  
  168. qkFolderWatch::qkFolderWatch(const QString & path, qkFileWatcher * watcher)
  169. : qkFileWatch(folderType, path, watcher)
  170. {
  171. recurseDirectories();
  172. }
  173.  
  174. //=============================================================================
  175.  
  176. bool qkFolderWatch::contains(const QString & path)
  177. {
  178. for (int i = 0; i < mFolderWatchs.size(); i++)
  179. {
  180. if (mFolderWatchs[i].path() == path) return true;
  181. }
  182.  
  183. for (int i = 0; i < mFileWatchs.size(); i++)
  184. {
  185. if (mFileWatchs[i].path() == path) return true;
  186. }
  187.  
  188. return false;
  189. }
  190.  
  191. void qkFolderWatch::checkChange()
  192. {
  193. QDateTime oldModified = mLastModified;
  194.  
  195. qkFileWatch::checkChange();
  196.  
  197. // Checking all sub-directories
  198. for (int i = 0; i < mFolderWatchs.size(); i++)
  199. {
  200. mFolderWatchs[i].checkChange();
  201. }
  202.  
  203. // Note: on windows we have to check files even if the directory has not changed
  204. for (int i = 0; i < mFileWatchs.size(); i++)
  205. {
  206. mFileWatchs[i].checkChange();
  207. }
  208.  
  209. // Checking files only if the directory has been modified
  210. if (oldModified != mLastModified)
  211. {
  212. // Checking for deleted files
  213. checkDeleted();
  214.  
  215. // Recurse for new files
  216. recurseDirectories();
  217. }
  218. }
To copy to clipboard, switch view to plain text mode