blob: f10850c246a4dca1e3f9cc5d7a03e96a9dfa7a8c [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>
Alexander Afanasyev9e5a4702013-01-24 13:15:23 -080028
Alexander Afanasyev583449a2013-01-28 17:04:06 -080029#include "scheduler.h"
Alexander Afanasyev0a30a0c2013-01-29 17:25:42 -080030#include "file-state.h"
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,
Alexander Afanasyev0a30a0c2013-01-29 17:25:42 -080042 FileState *fileState,
Alexander Afanasyev9ca444e2013-01-25 16:29:35 -080043 QObject* parent = 0);
Alexander Afanasyev9e5a4702013-01-24 13:15:23 -080044
45 // destructor
46 ~FsWatcher ();
47
48private slots:
49 // handle callback from watcher
50 void
51 DidDirectoryChanged (QString dirPath);
52
53 /**
54 * @brief This even will be triggered either by actual file change or via directory change event
55 * (i.e., can happen twice in a row, as well as trigger false alarm)
56 */
57 void
58 DidFileChanged (QString filePath);
59
60private:
61 // handle callback from the watcher
Alexander Afanasyev9e5a4702013-01-24 13:15:23 -080062 // scan directory and notify callback about any file changes
63 void
Alexander Afanasyevc80207d2013-01-29 20:07:39 -080064 ScanDirectory_NotifyUpdates_Execute (QString dirPath, bool notifyCallbacks);
Alexander Afanasyev0a30a0c2013-01-29 17:25:42 -080065
66 void
Alexander Afanasyev7a647002013-01-30 11:54:52 -080067 ScanDirectory_NotifyRemovals_Execute (QString dirPath, bool removeIncomplete);
Alexander Afanasyev9e5a4702013-01-24 13:15:23 -080068
69private:
70 QFileSystemWatcher* m_watcher; // filesystem watcher
Alexander Afanasyev583449a2013-01-28 17:04:06 -080071 SchedulerPtr m_scheduler;
Alexander Afanasyev9e5a4702013-01-24 13:15:23 -080072
73 QString m_dirPath; // monitored path
Alexander Afanasyev9ca444e2013-01-25 16:29:35 -080074
75 LocalFile_Change_Callback m_onChange;
76 LocalFile_Change_Callback m_onDelete;
Alexander Afanasyev0a30a0c2013-01-29 17:25:42 -080077
78 FileState *m_fileState;
Alexander Afanasyev9e5a4702013-01-24 13:15:23 -080079};
80
81#endif // FILESYSTEMWATCHER_H