Alexander Afanasyev | fa2f662 | 2016-12-25 12:28:00 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Lijing Wang | e0dd63e | 2015-05-31 16:25:16 -0700 | [diff] [blame] | 3 | * Copyright (c) 2013-2017, 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 | */ |
Alexander Afanasyev | 9e5a470 | 2013-01-24 13:15:23 -0800 | [diff] [blame] | 20 | |
Lijing Wang | e0dd63e | 2015-05-31 16:25:16 -0700 | [diff] [blame] | 21 | #ifndef CHRONOSHARE_FS_WATCHER_FS_WATCHER_HPP |
| 22 | #define CHRONOSHARE_FS_WATCHER_FS_WATCHER_HPP |
| 23 | |
| 24 | #include "db-helper.hpp" |
| 25 | #include "core/chronoshare-common.hpp" |
| 26 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 27 | #include <QFileSystemWatcher> |
Zhenkai Zhu | d175627 | 2013-02-01 17:02:18 -0800 | [diff] [blame] | 28 | #include <sqlite3.h> |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 29 | #include <vector> |
Alexander Afanasyev | 9e5a470 | 2013-01-24 13:15:23 -0800 | [diff] [blame] | 30 | |
Lijing Wang | e0dd63e | 2015-05-31 16:25:16 -0700 | [diff] [blame] | 31 | #include <ndn-cxx/util/scheduler-scoped-event-id.hpp> |
| 32 | #include <ndn-cxx/util/scheduler.hpp> |
| 33 | |
| 34 | #include <boost/asio/io_service.hpp> |
| 35 | #include <boost/filesystem.hpp> |
| 36 | |
| 37 | namespace ndn { |
| 38 | namespace chronoshare { |
Alexander Afanasyev | 9e5a470 | 2013-01-24 13:15:23 -0800 | [diff] [blame] | 39 | |
| 40 | class FsWatcher : public QObject |
| 41 | { |
| 42 | Q_OBJECT |
| 43 | |
| 44 | public: |
Lijing Wang | e0dd63e | 2015-05-31 16:25:16 -0700 | [diff] [blame] | 45 | class Error : public DbHelper::Error |
| 46 | { |
| 47 | public: |
| 48 | explicit Error(const std::string& what) |
| 49 | : DbHelper::Error(what) |
| 50 | { |
| 51 | } |
| 52 | }; |
| 53 | |
| 54 | typedef std::function<void(const boost::filesystem::path&)> LocalFile_Change_Callback; |
Alexander Afanasyev | 9ca444e | 2013-01-25 16:29:35 -0800 | [diff] [blame] | 55 | |
Alexander Afanasyev | 9e5a470 | 2013-01-24 13:15:23 -0800 | [diff] [blame] | 56 | // constructor |
Lijing Wang | e0dd63e | 2015-05-31 16:25:16 -0700 | [diff] [blame] | 57 | FsWatcher(boost::asio::io_service& io, QString dirPath, LocalFile_Change_Callback onChange, |
| 58 | LocalFile_Change_Callback onDelete, QObject* parent = 0); |
Alexander Afanasyev | 9e5a470 | 2013-01-24 13:15:23 -0800 | [diff] [blame] | 59 | |
| 60 | // destructor |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 61 | ~FsWatcher(); |
Alexander Afanasyev | 9e5a470 | 2013-01-24 13:15:23 -0800 | [diff] [blame] | 62 | |
| 63 | private slots: |
| 64 | // handle callback from watcher |
| 65 | void |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 66 | DidDirectoryChanged(QString dirPath); |
Alexander Afanasyev | 9e5a470 | 2013-01-24 13:15:23 -0800 | [diff] [blame] | 67 | |
| 68 | /** |
| 69 | * @brief This even will be triggered either by actual file change or via directory change event |
| 70 | * (i.e., can happen twice in a row, as well as trigger false alarm) |
| 71 | */ |
| 72 | void |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 73 | DidFileChanged(QString filePath); |
Alexander Afanasyev | 9e5a470 | 2013-01-24 13:15:23 -0800 | [diff] [blame] | 74 | |
| 75 | private: |
| 76 | // handle callback from the watcher |
Alexander Afanasyev | 9e5a470 | 2013-01-24 13:15:23 -0800 | [diff] [blame] | 77 | // scan directory and notify callback about any file changes |
| 78 | void |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 79 | ScanDirectory_NotifyUpdates_Execute(QString dirPath); |
Alexander Afanasyev | 0a30a0c | 2013-01-29 17:25:42 -0800 | [diff] [blame] | 80 | |
| 81 | void |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 82 | ScanDirectory_NotifyRemovals_Execute(QString dirPath); |
Zhenkai Zhu | d175627 | 2013-02-01 17:02:18 -0800 | [diff] [blame] | 83 | |
| 84 | void |
| 85 | initFileStateDb(); |
| 86 | |
| 87 | bool |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 88 | fileExists(const boost::filesystem::path& filename); |
Zhenkai Zhu | d175627 | 2013-02-01 17:02:18 -0800 | [diff] [blame] | 89 | |
| 90 | void |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 91 | addFile(const boost::filesystem::path& filename); |
Zhenkai Zhu | d175627 | 2013-02-01 17:02:18 -0800 | [diff] [blame] | 92 | |
| 93 | void |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 94 | deleteFile(const boost::filesystem::path& filename); |
Zhenkai Zhu | d175627 | 2013-02-01 17:02:18 -0800 | [diff] [blame] | 95 | |
| 96 | void |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 97 | getFilesInDir(const boost::filesystem::path& dir, std::vector<std::string>& files); |
Alexander Afanasyev | 9e5a470 | 2013-01-24 13:15:23 -0800 | [diff] [blame] | 98 | |
Lijing Wang | e0dd63e | 2015-05-31 16:25:16 -0700 | [diff] [blame] | 99 | void |
| 100 | rescheduleEvent(const std::string& eventType, const std::string& dirPath, |
| 101 | const time::milliseconds& period, const Scheduler::Event& callback); |
| 102 | |
Alexander Afanasyev | 9e5a470 | 2013-01-24 13:15:23 -0800 | [diff] [blame] | 103 | private: |
| 104 | QFileSystemWatcher* m_watcher; // filesystem watcher |
Lijing Wang | e0dd63e | 2015-05-31 16:25:16 -0700 | [diff] [blame] | 105 | Scheduler m_scheduler; |
Alexander Afanasyev | 9e5a470 | 2013-01-24 13:15:23 -0800 | [diff] [blame] | 106 | |
| 107 | QString m_dirPath; // monitored path |
Alexander Afanasyev | 9ca444e | 2013-01-25 16:29:35 -0800 | [diff] [blame] | 108 | |
| 109 | LocalFile_Change_Callback m_onChange; |
| 110 | LocalFile_Change_Callback m_onDelete; |
Alexander Afanasyev | 0a30a0c | 2013-01-29 17:25:42 -0800 | [diff] [blame] | 111 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 112 | sqlite3* m_db; |
Lijing Wang | e0dd63e | 2015-05-31 16:25:16 -0700 | [diff] [blame] | 113 | |
| 114 | std::map<std::string, util::scheduler::ScopedEventId> m_events; |
Alexander Afanasyev | 9e5a470 | 2013-01-24 13:15:23 -0800 | [diff] [blame] | 115 | }; |
| 116 | |
Lijing Wang | e0dd63e | 2015-05-31 16:25:16 -0700 | [diff] [blame] | 117 | } // namespace chronoshare |
| 118 | } // namespace ndn |
| 119 | |
| 120 | #endif // CHRONOSHARE_FS_WATCHER_FS_WATCHER_HPP |