blob: 6976cbdd88944760be0ff1b426d0753f9a7a283d [file] [log] [blame]
Alexander Afanasyev9e5a4702013-01-24 13:15:23 -08001/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil -*- */
2/*
3 * Copyright (c) 2012-2013 University of California, Los Angeles
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation;
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 *
18 * Author: Jared Lindblom <lindblom@cs.ucla.edu>
19 * Alexander Afanasyev <alexander.afanasyev@ucla.edu>
20 * Zhenkai Zhu <zhenkai@cs.ucla.edu>
21 */
22#ifndef FS_WATCHER_H
23#define FS_WATCHER_H
24
25#include <vector>
26#include <QFileSystemWatcher>
Alexander Afanasyev9ca444e2013-01-25 16:29:35 -080027#include <boost/filesystem.hpp>
Zhenkai Zhud1756272013-02-01 17:02:18 -080028#include <sqlite3.h>
Alexander Afanasyev9e5a4702013-01-24 13:15:23 -080029
Alexander Afanasyevf4cde4e2016-12-25 13:42:57 -080030#include "scheduler.hpp"
Alexander Afanasyev9e5a4702013-01-24 13:15:23 -080031
32class FsWatcher : public QObject
33{
34 Q_OBJECT
35
36public:
Alexander Afanasyev9ca444e2013-01-25 16:29:35 -080037 typedef boost::function<void (const boost::filesystem::path &)> LocalFile_Change_Callback;
38
Alexander Afanasyev9e5a4702013-01-24 13:15:23 -080039 // constructor
Alexander Afanasyev9ca444e2013-01-25 16:29:35 -080040 FsWatcher (QString dirPath,
41 LocalFile_Change_Callback onChange, LocalFile_Change_Callback onDelete,
42 QObject* parent = 0);
Alexander Afanasyev9e5a4702013-01-24 13:15:23 -080043
44 // destructor
45 ~FsWatcher ();
46
47private slots:
48 // handle callback from watcher
49 void
50 DidDirectoryChanged (QString dirPath);
51
52 /**
53 * @brief This even will be triggered either by actual file change or via directory change event
54 * (i.e., can happen twice in a row, as well as trigger false alarm)
55 */
56 void
57 DidFileChanged (QString filePath);
58
59private:
60 // handle callback from the watcher
Alexander Afanasyev9e5a4702013-01-24 13:15:23 -080061 // scan directory and notify callback about any file changes
62 void
Zhenkai Zhud1756272013-02-01 17:02:18 -080063 ScanDirectory_NotifyUpdates_Execute (QString dirPath);
Alexander Afanasyev0a30a0c2013-01-29 17:25:42 -080064
65 void
Zhenkai Zhud1756272013-02-01 17:02:18 -080066 ScanDirectory_NotifyRemovals_Execute (QString dirPath);
67
68 void
69 initFileStateDb();
70
71 bool
72 fileExists(const boost::filesystem::path &filename);
73
74 void
75 addFile(const boost::filesystem::path &filename);
76
77 void
78 deleteFile(const boost::filesystem::path &filename);
79
80 void
81 getFilesInDir(const boost::filesystem::path &dir, std::vector<std::string> &files);
Alexander Afanasyev9e5a4702013-01-24 13:15:23 -080082
83private:
84 QFileSystemWatcher* m_watcher; // filesystem watcher
Alexander Afanasyev583449a2013-01-28 17:04:06 -080085 SchedulerPtr m_scheduler;
Alexander Afanasyev9e5a4702013-01-24 13:15:23 -080086
87 QString m_dirPath; // monitored path
Alexander Afanasyev9ca444e2013-01-25 16:29:35 -080088
89 LocalFile_Change_Callback m_onChange;
90 LocalFile_Change_Callback m_onDelete;
Alexander Afanasyev0a30a0c2013-01-29 17:25:42 -080091
Zhenkai Zhud1756272013-02-01 17:02:18 -080092 sqlite3 *m_db;
Alexander Afanasyev9e5a4702013-01-24 13:15:23 -080093};
94
95#endif // FILESYSTEMWATCHER_H