Alexander Afanasyev | 9e5a470 | 2013-01-24 13:15:23 -0800 | [diff] [blame] | 1 | /* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil -*- */ |
| 2 | /* |
| 3 | * Copyright (c) 2012-2013 University of California, Los Angeles |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License version 2 as |
| 7 | * published by the Free Software Foundation; |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 17 | * |
| 18 | * Author: Jared Lindblom <lindblom@cs.ucla.edu> |
| 19 | * Alexander Afanasyev <alexander.afanasyev@ucla.edu> |
| 20 | * Zhenkai Zhu <zhenkai@cs.ucla.edu> |
| 21 | */ |
| 22 | #ifndef FS_WATCHER_H |
| 23 | #define FS_WATCHER_H |
| 24 | |
| 25 | #include <vector> |
| 26 | #include <QFileSystemWatcher> |
Alexander Afanasyev | 9ca444e | 2013-01-25 16:29:35 -0800 | [diff] [blame] | 27 | #include <boost/filesystem.hpp> |
Alexander Afanasyev | 9e5a470 | 2013-01-24 13:15:23 -0800 | [diff] [blame] | 28 | |
| 29 | #include "executor.h" |
| 30 | |
| 31 | class FsWatcher : public QObject |
| 32 | { |
| 33 | Q_OBJECT |
| 34 | |
| 35 | public: |
Alexander Afanasyev | 9ca444e | 2013-01-25 16:29:35 -0800 | [diff] [blame] | 36 | typedef boost::function<void (const boost::filesystem::path &)> LocalFile_Change_Callback; |
| 37 | |
Alexander Afanasyev | 9e5a470 | 2013-01-24 13:15:23 -0800 | [diff] [blame] | 38 | // constructor |
Alexander Afanasyev | 9ca444e | 2013-01-25 16:29:35 -0800 | [diff] [blame] | 39 | FsWatcher (QString dirPath, |
| 40 | LocalFile_Change_Callback onChange, LocalFile_Change_Callback onDelete, |
| 41 | QObject* parent = 0); |
Alexander Afanasyev | 9e5a470 | 2013-01-24 13:15:23 -0800 | [diff] [blame] | 42 | |
| 43 | // destructor |
| 44 | ~FsWatcher (); |
| 45 | |
| 46 | private slots: |
| 47 | // handle callback from watcher |
| 48 | void |
| 49 | DidDirectoryChanged (QString dirPath); |
| 50 | |
| 51 | /** |
| 52 | * @brief This even will be triggered either by actual file change or via directory change event |
| 53 | * (i.e., can happen twice in a row, as well as trigger false alarm) |
| 54 | */ |
| 55 | void |
| 56 | DidFileChanged (QString filePath); |
| 57 | |
| 58 | private: |
| 59 | // handle callback from the watcher |
| 60 | void |
| 61 | DidDirectoryChanged_Execute (QString dirPath); |
| 62 | |
| 63 | // scan directory and notify callback about any file changes |
| 64 | void |
| 65 | ScanDirectory_Notify_Execute (QString dirPath); |
| 66 | |
| 67 | // // reconcile directory, find changes |
| 68 | // std::vector<sEventInfo> |
| 69 | // reconcileDirectory (QHash<QString, qint64> fileList, QString dirPath); |
| 70 | |
| 71 | // // calculate checksum |
| 72 | // QByteArray calcChecksum(QString absFilePath); |
| 73 | |
| 74 | // // print Changes (DEBUG) |
| 75 | // void printChanges(std::vector<sEventInfo> dirChanges); |
| 76 | |
| 77 | private: |
| 78 | QFileSystemWatcher* m_watcher; // filesystem watcher |
| 79 | Executor m_executor; |
| 80 | |
| 81 | QString m_dirPath; // monitored path |
Alexander Afanasyev | 9ca444e | 2013-01-25 16:29:35 -0800 | [diff] [blame] | 82 | |
| 83 | LocalFile_Change_Callback m_onChange; |
| 84 | LocalFile_Change_Callback m_onDelete; |
Alexander Afanasyev | 9e5a470 | 2013-01-24 13:15:23 -0800 | [diff] [blame] | 85 | }; |
| 86 | |
| 87 | #endif // FILESYSTEMWATCHER_H |