blob: e68920c3564bc7ab946dbca719ad6396886224f8 [file] [log] [blame]
Alexander Afanasyevfa2f6622016-12-25 12:28:00 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2013-2016, 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 */
20#ifndef FS_WATCHER_H
21#define FS_WATCHER_H
22
Alexander Afanasyev9ca444e2013-01-25 16:29:35 -080023#include <boost/filesystem.hpp>
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080024#include <QFileSystemWatcher>
Zhenkai Zhud1756272013-02-01 17:02:18 -080025#include <sqlite3.h>
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080026#include <vector>
Alexander Afanasyev9e5a4702013-01-24 13:15:23 -080027
Alexander Afanasyevf4cde4e2016-12-25 13:42:57 -080028#include "scheduler.hpp"
Alexander Afanasyev9e5a4702013-01-24 13:15:23 -080029
30class FsWatcher : public QObject
31{
32 Q_OBJECT
33
34public:
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080035 typedef boost::function<void(const boost::filesystem::path&)> LocalFile_Change_Callback;
Alexander Afanasyev9ca444e2013-01-25 16:29:35 -080036
Alexander Afanasyev9e5a4702013-01-24 13:15:23 -080037 // constructor
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080038 FsWatcher(QString dirPath, LocalFile_Change_Callback onChange, LocalFile_Change_Callback onDelete,
39 QObject* parent = 0);
Alexander Afanasyev9e5a4702013-01-24 13:15:23 -080040
41 // destructor
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080042 ~FsWatcher();
Alexander Afanasyev9e5a4702013-01-24 13:15:23 -080043
44private slots:
45 // handle callback from watcher
46 void
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080047 DidDirectoryChanged(QString dirPath);
Alexander Afanasyev9e5a4702013-01-24 13:15:23 -080048
49 /**
50 * @brief This even will be triggered either by actual file change or via directory change event
51 * (i.e., can happen twice in a row, as well as trigger false alarm)
52 */
53 void
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080054 DidFileChanged(QString filePath);
Alexander Afanasyev9e5a4702013-01-24 13:15:23 -080055
56private:
57 // handle callback from the watcher
Alexander Afanasyev9e5a4702013-01-24 13:15:23 -080058 // scan directory and notify callback about any file changes
59 void
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080060 ScanDirectory_NotifyUpdates_Execute(QString dirPath);
Alexander Afanasyev0a30a0c2013-01-29 17:25:42 -080061
62 void
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080063 ScanDirectory_NotifyRemovals_Execute(QString dirPath);
Zhenkai Zhud1756272013-02-01 17:02:18 -080064
65 void
66 initFileStateDb();
67
68 bool
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080069 fileExists(const boost::filesystem::path& filename);
Zhenkai Zhud1756272013-02-01 17:02:18 -080070
71 void
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080072 addFile(const boost::filesystem::path& filename);
Zhenkai Zhud1756272013-02-01 17:02:18 -080073
74 void
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080075 deleteFile(const boost::filesystem::path& filename);
Zhenkai Zhud1756272013-02-01 17:02:18 -080076
77 void
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080078 getFilesInDir(const boost::filesystem::path& dir, std::vector<std::string>& files);
Alexander Afanasyev9e5a4702013-01-24 13:15:23 -080079
80private:
81 QFileSystemWatcher* m_watcher; // filesystem watcher
Alexander Afanasyev583449a2013-01-28 17:04:06 -080082 SchedulerPtr m_scheduler;
Alexander Afanasyev9e5a4702013-01-24 13:15:23 -080083
84 QString m_dirPath; // monitored path
Alexander Afanasyev9ca444e2013-01-25 16:29:35 -080085
86 LocalFile_Change_Callback m_onChange;
87 LocalFile_Change_Callback m_onDelete;
Alexander Afanasyev0a30a0c2013-01-29 17:25:42 -080088
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080089 sqlite3* m_db;
Alexander Afanasyev9e5a4702013-01-24 13:15:23 -080090};
91
92#endif // FILESYSTEMWATCHER_H