blob: d94eed82bbcb43f16f1a1854a4bfb67351069887 [file] [log] [blame]
Alexander Afanasyevfa2f6622016-12-25 12:28:00 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Lijing Wange0dd63e2015-05-31 16:25:16 -07003 * Copyright (c) 2013-2017, Regents of the University of California.
Alexander Afanasyev9e5a4702013-01-24 13:15:23 -08004 *
Alexander Afanasyevfa2f6622016-12-25 12:28:00 -08005 * This file is part of ChronoShare, a decentralized file sharing application over NDN.
Alexander Afanasyev9e5a4702013-01-24 13:15:23 -08006 *
Alexander Afanasyevfa2f6622016-12-25 12:28:00 -08007 * 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 Afanasyev9e5a4702013-01-24 13:15:23 -080010 *
Alexander Afanasyevfa2f6622016-12-25 12:28:00 -080011 * 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 Afanasyev9e5a4702013-01-24 13:15:23 -080014 *
Alexander Afanasyevfa2f6622016-12-25 12:28:00 -080015 * 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 Afanasyev9e5a4702013-01-24 13:15:23 -080019 */
Alexander Afanasyev9e5a4702013-01-24 13:15:23 -080020
Lijing Wange0dd63e2015-05-31 16:25:16 -070021#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 Afanasyeveda3b7a2016-12-25 11:26:40 -080027#include <QFileSystemWatcher>
Zhenkai Zhud1756272013-02-01 17:02:18 -080028#include <sqlite3.h>
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080029#include <vector>
Alexander Afanasyev9e5a4702013-01-24 13:15:23 -080030
Lijing Wange0dd63e2015-05-31 16:25:16 -070031#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
37namespace ndn {
38namespace chronoshare {
Alexander Afanasyev9e5a4702013-01-24 13:15:23 -080039
40class FsWatcher : public QObject
41{
42 Q_OBJECT
43
44public:
Lijing Wange0dd63e2015-05-31 16:25:16 -070045 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 Afanasyev9ca444e2013-01-25 16:29:35 -080055
Alexander Afanasyev9e5a4702013-01-24 13:15:23 -080056 // constructor
Lijing Wange0dd63e2015-05-31 16:25:16 -070057 FsWatcher(boost::asio::io_service& io, QString dirPath, LocalFile_Change_Callback onChange,
58 LocalFile_Change_Callback onDelete, QObject* parent = 0);
Alexander Afanasyev9e5a4702013-01-24 13:15:23 -080059
60 // destructor
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080061 ~FsWatcher();
Alexander Afanasyev9e5a4702013-01-24 13:15:23 -080062
63private slots:
64 // handle callback from watcher
65 void
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080066 DidDirectoryChanged(QString dirPath);
Alexander Afanasyev9e5a4702013-01-24 13:15:23 -080067
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 Afanasyeveda3b7a2016-12-25 11:26:40 -080073 DidFileChanged(QString filePath);
Alexander Afanasyev9e5a4702013-01-24 13:15:23 -080074
75private:
76 // handle callback from the watcher
Alexander Afanasyev9e5a4702013-01-24 13:15:23 -080077 // scan directory and notify callback about any file changes
78 void
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080079 ScanDirectory_NotifyUpdates_Execute(QString dirPath);
Alexander Afanasyev0a30a0c2013-01-29 17:25:42 -080080
81 void
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080082 ScanDirectory_NotifyRemovals_Execute(QString dirPath);
Zhenkai Zhud1756272013-02-01 17:02:18 -080083
84 void
85 initFileStateDb();
86
87 bool
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080088 fileExists(const boost::filesystem::path& filename);
Zhenkai Zhud1756272013-02-01 17:02:18 -080089
90 void
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080091 addFile(const boost::filesystem::path& filename);
Zhenkai Zhud1756272013-02-01 17:02:18 -080092
93 void
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080094 deleteFile(const boost::filesystem::path& filename);
Zhenkai Zhud1756272013-02-01 17:02:18 -080095
96 void
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080097 getFilesInDir(const boost::filesystem::path& dir, std::vector<std::string>& files);
Alexander Afanasyev9e5a4702013-01-24 13:15:23 -080098
Lijing Wange0dd63e2015-05-31 16:25:16 -070099 void
100 rescheduleEvent(const std::string& eventType, const std::string& dirPath,
101 const time::milliseconds& period, const Scheduler::Event& callback);
102
Alexander Afanasyev9e5a4702013-01-24 13:15:23 -0800103private:
104 QFileSystemWatcher* m_watcher; // filesystem watcher
Lijing Wange0dd63e2015-05-31 16:25:16 -0700105 Scheduler m_scheduler;
Alexander Afanasyev9e5a4702013-01-24 13:15:23 -0800106
107 QString m_dirPath; // monitored path
Alexander Afanasyev9ca444e2013-01-25 16:29:35 -0800108
109 LocalFile_Change_Callback m_onChange;
110 LocalFile_Change_Callback m_onDelete;
Alexander Afanasyev0a30a0c2013-01-29 17:25:42 -0800111
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800112 sqlite3* m_db;
Lijing Wange0dd63e2015-05-31 16:25:16 -0700113
114 std::map<std::string, util::scheduler::ScopedEventId> m_events;
Alexander Afanasyev9e5a4702013-01-24 13:15:23 -0800115};
116
Lijing Wange0dd63e2015-05-31 16:25:16 -0700117} // namespace chronoshare
118} // namespace ndn
119
120#endif // CHRONOSHARE_FS_WATCHER_FS_WATCHER_HPP