blob: 6ba6671d05b5bde6bf3d9d1d90f055087923a547 [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
29#include "executor.h"
30
31class FsWatcher : public QObject
32{
33 Q_OBJECT
34
35public:
Alexander Afanasyev9ca444e2013-01-25 16:29:35 -080036 typedef boost::function<void (const boost::filesystem::path &)> LocalFile_Change_Callback;
37
Alexander Afanasyev9e5a4702013-01-24 13:15:23 -080038 // constructor
Alexander Afanasyev9ca444e2013-01-25 16:29:35 -080039 FsWatcher (QString dirPath,
40 LocalFile_Change_Callback onChange, LocalFile_Change_Callback onDelete,
41 QObject* parent = 0);
Alexander Afanasyev9e5a4702013-01-24 13:15:23 -080042
43 // destructor
44 ~FsWatcher ();
45
46private slots:
47 // handle callback from watcher
48 void
49 DidDirectoryChanged (QString dirPath);
50
51 /**
52 * @brief This even will be triggered either by actual file change or via directory change event
53 * (i.e., can happen twice in a row, as well as trigger false alarm)
54 */
55 void
56 DidFileChanged (QString filePath);
57
58private:
59 // handle callback from the watcher
60 void
61 DidDirectoryChanged_Execute (QString dirPath);
62
63 // scan directory and notify callback about any file changes
64 void
65 ScanDirectory_Notify_Execute (QString dirPath);
66
67 // // reconcile directory, find changes
68 // std::vector<sEventInfo>
69 // reconcileDirectory (QHash<QString, qint64> fileList, QString dirPath);
70
71 // // calculate checksum
72 // QByteArray calcChecksum(QString absFilePath);
73
74 // // print Changes (DEBUG)
75 // void printChanges(std::vector<sEventInfo> dirChanges);
76
77private:
78 QFileSystemWatcher* m_watcher; // filesystem watcher
79 Executor m_executor;
80
81 QString m_dirPath; // monitored path
Alexander Afanasyev9ca444e2013-01-25 16:29:35 -080082
83 LocalFile_Change_Callback m_onChange;
84 LocalFile_Change_Callback m_onDelete;
Alexander Afanasyev9e5a4702013-01-24 13:15:23 -080085};
86
87#endif // FILESYSTEMWATCHER_H