Alexander Afanasyev | fa2f662 | 2016-12-25 12:28:00 -0800 | [diff] [blame^] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (c) 2013-2016, Regents of the University of California. |
Alexander Afanasyev | 9e5a470 | 2013-01-24 13:15:23 -0800 | [diff] [blame] | 4 | * |
Alexander Afanasyev | fa2f662 | 2016-12-25 12:28:00 -0800 | [diff] [blame^] | 5 | * This file is part of ChronoShare, a decentralized file sharing application over NDN. |
Alexander Afanasyev | 9e5a470 | 2013-01-24 13:15:23 -0800 | [diff] [blame] | 6 | * |
Alexander Afanasyev | fa2f662 | 2016-12-25 12:28:00 -0800 | [diff] [blame^] | 7 | * ChronoShare is free software: you can redistribute it and/or modify it under the terms |
| 8 | * of the GNU General Public License as published by the Free Software Foundation, either |
| 9 | * version 3 of the License, or (at your option) any later version. |
Alexander Afanasyev | 9e5a470 | 2013-01-24 13:15:23 -0800 | [diff] [blame] | 10 | * |
Alexander Afanasyev | fa2f662 | 2016-12-25 12:28:00 -0800 | [diff] [blame^] | 11 | * ChronoShare is distributed in the hope that it will be useful, but WITHOUT ANY |
| 12 | * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A |
| 13 | * PARTICULAR PURPOSE. See the GNU General Public License for more details. |
Alexander Afanasyev | 9e5a470 | 2013-01-24 13:15:23 -0800 | [diff] [blame] | 14 | * |
Alexander Afanasyev | fa2f662 | 2016-12-25 12:28:00 -0800 | [diff] [blame^] | 15 | * You should have received copies of the GNU General Public License along with |
| 16 | * ChronoShare, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
| 17 | * |
| 18 | * See AUTHORS.md for complete list of ChronoShare authors and contributors. |
Alexander Afanasyev | 9e5a470 | 2013-01-24 13:15:23 -0800 | [diff] [blame] | 19 | */ |
| 20 | #ifndef FS_WATCHER_H |
| 21 | #define FS_WATCHER_H |
| 22 | |
| 23 | #include <vector> |
| 24 | #include <QFileSystemWatcher> |
Alexander Afanasyev | 9ca444e | 2013-01-25 16:29:35 -0800 | [diff] [blame] | 25 | #include <boost/filesystem.hpp> |
Zhenkai Zhu | d175627 | 2013-02-01 17:02:18 -0800 | [diff] [blame] | 26 | #include <sqlite3.h> |
Alexander Afanasyev | 9e5a470 | 2013-01-24 13:15:23 -0800 | [diff] [blame] | 27 | |
Alexander Afanasyev | f4cde4e | 2016-12-25 13:42:57 -0800 | [diff] [blame] | 28 | #include "scheduler.hpp" |
Alexander Afanasyev | 9e5a470 | 2013-01-24 13:15:23 -0800 | [diff] [blame] | 29 | |
| 30 | class FsWatcher : public QObject |
| 31 | { |
| 32 | Q_OBJECT |
| 33 | |
| 34 | public: |
Alexander Afanasyev | 9ca444e | 2013-01-25 16:29:35 -0800 | [diff] [blame] | 35 | typedef boost::function<void (const boost::filesystem::path &)> LocalFile_Change_Callback; |
| 36 | |
Alexander Afanasyev | 9e5a470 | 2013-01-24 13:15:23 -0800 | [diff] [blame] | 37 | // constructor |
Alexander Afanasyev | 9ca444e | 2013-01-25 16:29:35 -0800 | [diff] [blame] | 38 | FsWatcher (QString dirPath, |
| 39 | LocalFile_Change_Callback onChange, LocalFile_Change_Callback onDelete, |
| 40 | QObject* parent = 0); |
Alexander Afanasyev | 9e5a470 | 2013-01-24 13:15:23 -0800 | [diff] [blame] | 41 | |
| 42 | // destructor |
| 43 | ~FsWatcher (); |
| 44 | |
| 45 | private slots: |
| 46 | // handle callback from watcher |
| 47 | void |
| 48 | DidDirectoryChanged (QString dirPath); |
| 49 | |
| 50 | /** |
| 51 | * @brief This even will be triggered either by actual file change or via directory change event |
| 52 | * (i.e., can happen twice in a row, as well as trigger false alarm) |
| 53 | */ |
| 54 | void |
| 55 | DidFileChanged (QString filePath); |
| 56 | |
| 57 | private: |
| 58 | // handle callback from the watcher |
Alexander Afanasyev | 9e5a470 | 2013-01-24 13:15:23 -0800 | [diff] [blame] | 59 | // scan directory and notify callback about any file changes |
| 60 | void |
Zhenkai Zhu | d175627 | 2013-02-01 17:02:18 -0800 | [diff] [blame] | 61 | ScanDirectory_NotifyUpdates_Execute (QString dirPath); |
Alexander Afanasyev | 0a30a0c | 2013-01-29 17:25:42 -0800 | [diff] [blame] | 62 | |
| 63 | void |
Zhenkai Zhu | d175627 | 2013-02-01 17:02:18 -0800 | [diff] [blame] | 64 | ScanDirectory_NotifyRemovals_Execute (QString dirPath); |
| 65 | |
| 66 | void |
| 67 | initFileStateDb(); |
| 68 | |
| 69 | bool |
| 70 | fileExists(const boost::filesystem::path &filename); |
| 71 | |
| 72 | void |
| 73 | addFile(const boost::filesystem::path &filename); |
| 74 | |
| 75 | void |
| 76 | deleteFile(const boost::filesystem::path &filename); |
| 77 | |
| 78 | void |
| 79 | getFilesInDir(const boost::filesystem::path &dir, std::vector<std::string> &files); |
Alexander Afanasyev | 9e5a470 | 2013-01-24 13:15:23 -0800 | [diff] [blame] | 80 | |
| 81 | private: |
| 82 | QFileSystemWatcher* m_watcher; // filesystem watcher |
Alexander Afanasyev | 583449a | 2013-01-28 17:04:06 -0800 | [diff] [blame] | 83 | SchedulerPtr m_scheduler; |
Alexander Afanasyev | 9e5a470 | 2013-01-24 13:15:23 -0800 | [diff] [blame] | 84 | |
| 85 | QString m_dirPath; // monitored path |
Alexander Afanasyev | 9ca444e | 2013-01-25 16:29:35 -0800 | [diff] [blame] | 86 | |
| 87 | LocalFile_Change_Callback m_onChange; |
| 88 | LocalFile_Change_Callback m_onDelete; |
Alexander Afanasyev | 0a30a0c | 2013-01-29 17:25:42 -0800 | [diff] [blame] | 89 | |
Zhenkai Zhu | d175627 | 2013-02-01 17:02:18 -0800 | [diff] [blame] | 90 | sqlite3 *m_db; |
Alexander Afanasyev | 9e5a470 | 2013-01-24 13:15:23 -0800 | [diff] [blame] | 91 | }; |
| 92 | |
| 93 | #endif // FILESYSTEMWATCHER_H |