blob: b5413828c83ddc9a09e7e97155ed87005ae523a5 [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 Afanasyev0a30a0c2013-01-29 17:25:42 -080064 ScanDirectory_NotifyUpdates_Execute (QString dirPath);
65
66 void
67 ScanDirectory_NotifyRemovals_Execute (QString dirPath);
Alexander Afanasyev9e5a4702013-01-24 13:15:23 -080068
69 // // reconcile directory, find changes
70 // std::vector<sEventInfo>
71 // reconcileDirectory (QHash<QString, qint64> fileList, QString dirPath);
72
73 // // calculate checksum
74 // QByteArray calcChecksum(QString absFilePath);
75
76 // // print Changes (DEBUG)
77 // void printChanges(std::vector<sEventInfo> dirChanges);
78
79private:
80 QFileSystemWatcher* m_watcher; // filesystem watcher
Alexander Afanasyev583449a2013-01-28 17:04:06 -080081 SchedulerPtr m_scheduler;
Alexander Afanasyev9e5a4702013-01-24 13:15:23 -080082
83 QString m_dirPath; // monitored path
Alexander Afanasyev9ca444e2013-01-25 16:29:35 -080084
85 LocalFile_Change_Callback m_onChange;
86 LocalFile_Change_Callback m_onDelete;
Alexander Afanasyev0a30a0c2013-01-29 17:25:42 -080087
88 FileState *m_fileState;
Alexander Afanasyev9e5a4702013-01-24 13:15:23 -080089};
90
91#endif // FILESYSTEMWATCHER_H