blob: 8de7e0b1352c8206133649245a74062afbe5c9f2 [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
23#include <vector>
24#include <QFileSystemWatcher>
Alexander Afanasyev9ca444e2013-01-25 16:29:35 -080025#include <boost/filesystem.hpp>
Zhenkai Zhud1756272013-02-01 17:02:18 -080026#include <sqlite3.h>
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 Afanasyev9ca444e2013-01-25 16:29:35 -080035 typedef boost::function<void (const boost::filesystem::path &)> LocalFile_Change_Callback;
36
Alexander Afanasyev9e5a4702013-01-24 13:15:23 -080037 // constructor
Alexander Afanasyev9ca444e2013-01-25 16:29:35 -080038 FsWatcher (QString dirPath,
39 LocalFile_Change_Callback onChange, LocalFile_Change_Callback onDelete,
40 QObject* parent = 0);
Alexander Afanasyev9e5a4702013-01-24 13:15:23 -080041
42 // destructor
43 ~FsWatcher ();
44
45private 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
57private:
58 // handle callback from the watcher
Alexander Afanasyev9e5a4702013-01-24 13:15:23 -080059 // scan directory and notify callback about any file changes
60 void
Zhenkai Zhud1756272013-02-01 17:02:18 -080061 ScanDirectory_NotifyUpdates_Execute (QString dirPath);
Alexander Afanasyev0a30a0c2013-01-29 17:25:42 -080062
63 void
Zhenkai Zhud1756272013-02-01 17:02:18 -080064 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 Afanasyev9e5a4702013-01-24 13:15:23 -080080
81private:
82 QFileSystemWatcher* m_watcher; // filesystem watcher
Alexander Afanasyev583449a2013-01-28 17:04:06 -080083 SchedulerPtr m_scheduler;
Alexander Afanasyev9e5a4702013-01-24 13:15:23 -080084
85 QString m_dirPath; // monitored path
Alexander Afanasyev9ca444e2013-01-25 16:29:35 -080086
87 LocalFile_Change_Callback m_onChange;
88 LocalFile_Change_Callback m_onDelete;
Alexander Afanasyev0a30a0c2013-01-29 17:25:42 -080089
Zhenkai Zhud1756272013-02-01 17:02:18 -080090 sqlite3 *m_db;
Alexander Afanasyev9e5a4702013-01-24 13:15:23 -080091};
92
93#endif // FILESYSTEMWATCHER_H