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> |
Zhenkai Zhu | d175627 | 2013-02-01 17:02:18 -0800 | [diff] [blame] | 28 | #include <sqlite3.h> |
Alexander Afanasyev | 9e5a470 | 2013-01-24 13:15:23 -0800 | [diff] [blame] | 29 | |
Alexander Afanasyev | 583449a | 2013-01-28 17:04:06 -0800 | [diff] [blame] | 30 | #include "scheduler.h" |
Alexander Afanasyev | 9e5a470 | 2013-01-24 13:15:23 -0800 | [diff] [blame] | 31 | |
| 32 | class FsWatcher : public QObject |
| 33 | { |
| 34 | Q_OBJECT |
| 35 | |
| 36 | public: |
Alexander Afanasyev | 9ca444e | 2013-01-25 16:29:35 -0800 | [diff] [blame] | 37 | typedef boost::function<void (const boost::filesystem::path &)> LocalFile_Change_Callback; |
| 38 | |
Alexander Afanasyev | 9e5a470 | 2013-01-24 13:15:23 -0800 | [diff] [blame] | 39 | // constructor |
Alexander Afanasyev | 9ca444e | 2013-01-25 16:29:35 -0800 | [diff] [blame] | 40 | FsWatcher (QString dirPath, |
| 41 | LocalFile_Change_Callback onChange, LocalFile_Change_Callback onDelete, |
| 42 | QObject* parent = 0); |
Alexander Afanasyev | 9e5a470 | 2013-01-24 13:15:23 -0800 | [diff] [blame] | 43 | |
| 44 | // destructor |
| 45 | ~FsWatcher (); |
| 46 | |
| 47 | private slots: |
| 48 | // handle callback from watcher |
| 49 | void |
| 50 | DidDirectoryChanged (QString dirPath); |
| 51 | |
| 52 | /** |
| 53 | * @brief This even will be triggered either by actual file change or via directory change event |
| 54 | * (i.e., can happen twice in a row, as well as trigger false alarm) |
| 55 | */ |
| 56 | void |
| 57 | DidFileChanged (QString filePath); |
| 58 | |
| 59 | private: |
| 60 | // handle callback from the watcher |
Alexander Afanasyev | 9e5a470 | 2013-01-24 13:15:23 -0800 | [diff] [blame] | 61 | // scan directory and notify callback about any file changes |
| 62 | void |
Zhenkai Zhu | d175627 | 2013-02-01 17:02:18 -0800 | [diff] [blame] | 63 | ScanDirectory_NotifyUpdates_Execute (QString dirPath); |
Alexander Afanasyev | 0a30a0c | 2013-01-29 17:25:42 -0800 | [diff] [blame] | 64 | |
| 65 | void |
Zhenkai Zhu | d175627 | 2013-02-01 17:02:18 -0800 | [diff] [blame] | 66 | ScanDirectory_NotifyRemovals_Execute (QString dirPath); |
| 67 | |
| 68 | void |
| 69 | initFileStateDb(); |
| 70 | |
| 71 | bool |
| 72 | fileExists(const boost::filesystem::path &filename); |
| 73 | |
| 74 | void |
| 75 | addFile(const boost::filesystem::path &filename); |
| 76 | |
| 77 | void |
| 78 | deleteFile(const boost::filesystem::path &filename); |
| 79 | |
| 80 | void |
| 81 | getFilesInDir(const boost::filesystem::path &dir, std::vector<std::string> &files); |
Alexander Afanasyev | 9e5a470 | 2013-01-24 13:15:23 -0800 | [diff] [blame] | 82 | |
| 83 | private: |
| 84 | QFileSystemWatcher* m_watcher; // filesystem watcher |
Alexander Afanasyev | 583449a | 2013-01-28 17:04:06 -0800 | [diff] [blame] | 85 | SchedulerPtr m_scheduler; |
Alexander Afanasyev | 9e5a470 | 2013-01-24 13:15:23 -0800 | [diff] [blame] | 86 | |
| 87 | QString m_dirPath; // monitored path |
Alexander Afanasyev | 9ca444e | 2013-01-25 16:29:35 -0800 | [diff] [blame] | 88 | |
| 89 | LocalFile_Change_Callback m_onChange; |
| 90 | LocalFile_Change_Callback m_onDelete; |
Alexander Afanasyev | 0a30a0c | 2013-01-29 17:25:42 -0800 | [diff] [blame] | 91 | |
Zhenkai Zhu | d175627 | 2013-02-01 17:02:18 -0800 | [diff] [blame] | 92 | sqlite3 *m_db; |
Alexander Afanasyev | 9e5a470 | 2013-01-24 13:15:23 -0800 | [diff] [blame] | 93 | }; |
| 94 | |
| 95 | #endif // FILESYSTEMWATCHER_H |