Jared Lindblom | 2eead68 | 2013-01-12 20:26:21 -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 | */ |
jared | a954181 | 2013-01-09 00:08:46 -0800 | [diff] [blame] | 20 | #ifndef FILESYSTEMWATCHER_H |
| 21 | #define FILESYSTEMWATCHER_H |
| 22 | |
Jared Lindblom | 2eead68 | 2013-01-12 20:26:21 -0800 | [diff] [blame^] | 23 | #include <QFileSystemWatcher> |
| 24 | #include <QCryptographicHash> |
| 25 | #include <QDirIterator> |
| 26 | #include <QFileInfo> |
| 27 | #include <QDateTime> |
| 28 | #include <QTimer> |
Jared Lindblom | 4d1d00a | 2013-01-11 01:14:23 -0800 | [diff] [blame] | 29 | #include <QDebug> |
| 30 | #include <QHash> |
Jared Lindblom | 2eead68 | 2013-01-12 20:26:21 -0800 | [diff] [blame^] | 31 | #include <structs.h> |
Jared Lindblom | c125972 | 2013-01-12 16:47:32 -0800 | [diff] [blame] | 32 | |
| 33 | #define DEBUG 1 |
Jared Lindblom | 264310d | 2013-01-12 01:18:58 -0800 | [diff] [blame] | 34 | |
Jared Lindblom | 2eead68 | 2013-01-12 20:26:21 -0800 | [diff] [blame^] | 35 | class FileSystemWatcher : public QObject |
jared | a954181 | 2013-01-09 00:08:46 -0800 | [diff] [blame] | 36 | { |
| 37 | Q_OBJECT |
Jared Lindblom | 4d1d00a | 2013-01-11 01:14:23 -0800 | [diff] [blame] | 38 | |
jared | a954181 | 2013-01-09 00:08:46 -0800 | [diff] [blame] | 39 | public: |
| 40 | // constructor |
Jared Lindblom | 2eead68 | 2013-01-12 20:26:21 -0800 | [diff] [blame^] | 41 | FileSystemWatcher(QString dirPath, QObject* parent = 0); |
jared | a954181 | 2013-01-09 00:08:46 -0800 | [diff] [blame] | 42 | |
| 43 | // destructor |
Jared Lindblom | 4d1d00a | 2013-01-11 01:14:23 -0800 | [diff] [blame] | 44 | ~FileSystemWatcher(); |
jared | a954181 | 2013-01-09 00:08:46 -0800 | [diff] [blame] | 45 | |
Jared Lindblom | c125972 | 2013-01-12 16:47:32 -0800 | [diff] [blame] | 46 | signals: |
| 47 | // directory event signal |
| 48 | void dirEventSignal(std::vector<sEventInfo> dirChanges); |
| 49 | |
jared | a954181 | 2013-01-09 00:08:46 -0800 | [diff] [blame] | 50 | private slots: |
Jared Lindblom | f8d32ad | 2013-01-12 11:32:27 -0800 | [diff] [blame] | 51 | // handle callback from watcher |
| 52 | void watcherCallbackSlot(QString dirPath); |
| 53 | |
| 54 | // handle callback from timer |
| 55 | void timerCallbackSlot(); |
Jared Lindblom | 4d1d00a | 2013-01-11 01:14:23 -0800 | [diff] [blame] | 56 | |
Jared Lindblom | 2eead68 | 2013-01-12 20:26:21 -0800 | [diff] [blame^] | 57 | // bootstrap |
| 58 | void bootstrap(); |
| 59 | |
jared | a954181 | 2013-01-09 00:08:46 -0800 | [diff] [blame] | 60 | private: |
Jared Lindblom | f8d32ad | 2013-01-12 11:32:27 -0800 | [diff] [blame] | 61 | // handle callback from either the watcher or timer |
| 62 | void handleCallback(QString dirPath); |
| 63 | |
Jared Lindblom | 4d1d00a | 2013-01-11 01:14:23 -0800 | [diff] [blame] | 64 | // scan directory and populate file list |
Jared Lindblom | 8278b98 | 2013-01-12 15:45:09 -0800 | [diff] [blame] | 65 | QHash<QString, qint64> scanDirectory(QString dirPath); |
Jared Lindblom | 4d1d00a | 2013-01-11 01:14:23 -0800 | [diff] [blame] | 66 | |
| 67 | // reconcile directory, find changes |
Jared Lindblom | c125972 | 2013-01-12 16:47:32 -0800 | [diff] [blame] | 68 | std::vector<sEventInfo> reconcileDirectory(QHash<QString, qint64> fileList, QString dirPath); |
Jared Lindblom | f8d32ad | 2013-01-12 11:32:27 -0800 | [diff] [blame] | 69 | |
| 70 | // calculate checksum |
| 71 | QByteArray calcChecksum(QString absFilePath); |
| 72 | |
Jared Lindblom | c125972 | 2013-01-12 16:47:32 -0800 | [diff] [blame] | 73 | // print Changes (DEBUG) |
| 74 | void printChanges(std::vector<sEventInfo> dirChanges); |
Jared Lindblom | 4d1d00a | 2013-01-11 01:14:23 -0800 | [diff] [blame] | 75 | |
| 76 | private: |
jared | a954181 | 2013-01-09 00:08:46 -0800 | [diff] [blame] | 77 | QFileSystemWatcher* m_watcher; // filesystem watcher |
Jared Lindblom | a999630 | 2013-01-12 00:25:16 -0800 | [diff] [blame] | 78 | QTimer* m_timer; // timer |
Jared Lindblom | 4d1d00a | 2013-01-11 01:14:23 -0800 | [diff] [blame] | 79 | |
Jared Lindblom | a999630 | 2013-01-12 00:25:16 -0800 | [diff] [blame] | 80 | QString m_dirPath; // monitored path |
Jared Lindblom | 8278b98 | 2013-01-12 15:45:09 -0800 | [diff] [blame] | 81 | QHash<QString, qint64> m_storedState; // stored state of directory |
jared | a954181 | 2013-01-09 00:08:46 -0800 | [diff] [blame] | 82 | }; |
| 83 | |
| 84 | #endif // FILESYSTEMWATCHER_H |