| /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| /** |
| * Copyright (c) 2013-2017, Regents of the University of California. |
| * |
| * This file is part of ChronoShare, a decentralized file sharing application over NDN. |
| * |
| * ChronoShare is free software: you can redistribute it and/or modify it under the terms |
| * of the GNU General Public License as published by the Free Software Foundation, either |
| * version 3 of the License, or (at your option) any later version. |
| * |
| * ChronoShare is distributed in the hope that it will be useful, but WITHOUT ANY |
| * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A |
| * PARTICULAR PURPOSE. See the GNU General Public License for more details. |
| * |
| * You should have received copies of the GNU General Public License along with |
| * ChronoShare, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
| * |
| * See AUTHORS.md for complete list of ChronoShare authors and contributors. |
| */ |
| |
| #ifndef CHRONOSHARE_FS_WATCHER_FS_WATCHER_HPP |
| #define CHRONOSHARE_FS_WATCHER_FS_WATCHER_HPP |
| |
| #include "db-helper.hpp" |
| #include "core/chronoshare-common.hpp" |
| |
| #include <QFileSystemWatcher> |
| #include <sqlite3.h> |
| #include <vector> |
| |
| #include <ndn-cxx/util/scheduler-scoped-event-id.hpp> |
| #include <ndn-cxx/util/scheduler.hpp> |
| |
| #include <boost/asio/io_service.hpp> |
| #include <boost/filesystem.hpp> |
| |
| namespace ndn { |
| namespace chronoshare { |
| |
| class FsWatcher : public QObject |
| { |
| Q_OBJECT |
| |
| public: |
| class Error : public DbHelper::Error |
| { |
| public: |
| explicit Error(const std::string& what) |
| : DbHelper::Error(what) |
| { |
| } |
| }; |
| |
| typedef std::function<void(const boost::filesystem::path&)> LocalFile_Change_Callback; |
| |
| // constructor |
| FsWatcher(boost::asio::io_service& io, QString dirPath, LocalFile_Change_Callback onChange, |
| LocalFile_Change_Callback onDelete, QObject* parent = 0); |
| |
| // destructor |
| ~FsWatcher(); |
| |
| private slots: |
| // handle callback from watcher |
| void |
| DidDirectoryChanged(QString dirPath); |
| |
| /** |
| * @brief This even will be triggered either by actual file change or via directory change event |
| * (i.e., can happen twice in a row, as well as trigger false alarm) |
| */ |
| void |
| DidFileChanged(QString filePath); |
| |
| private: |
| // handle callback from the watcher |
| // scan directory and notify callback about any file changes |
| void |
| ScanDirectory_NotifyUpdates_Execute(QString dirPath); |
| |
| void |
| ScanDirectory_NotifyRemovals_Execute(QString dirPath); |
| |
| void |
| initFileStateDb(); |
| |
| bool |
| fileExists(const boost::filesystem::path& filename); |
| |
| void |
| addFile(const boost::filesystem::path& filename); |
| |
| void |
| deleteFile(const boost::filesystem::path& filename); |
| |
| void |
| getFilesInDir(const boost::filesystem::path& dir, std::vector<std::string>& files); |
| |
| void |
| rescheduleEvent(const std::string& eventType, const std::string& dirPath, |
| const time::milliseconds& period, const Scheduler::Event& callback); |
| |
| private: |
| QFileSystemWatcher* m_watcher; // filesystem watcher |
| Scheduler m_scheduler; |
| |
| QString m_dirPath; // monitored path |
| |
| LocalFile_Change_Callback m_onChange; |
| LocalFile_Change_Callback m_onDelete; |
| |
| sqlite3* m_db; |
| |
| std::map<std::string, util::scheduler::ScopedEventId> m_events; |
| }; |
| |
| } // namespace chronoshare |
| } // namespace ndn |
| |
| #endif // CHRONOSHARE_FS_WATCHER_FS_WATCHER_HPP |