blob: 543fba6f34c9c55c9d3d965b6b5dfec2f11839c2 [file] [log] [blame]
Jared Lindblom2eead682013-01-12 20:26:21 -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 */
jareda9541812013-01-09 00:08:46 -080020#ifndef FILESYSTEMWATCHER_H
21#define FILESYSTEMWATCHER_H
22
Jared Lindblom2eead682013-01-12 20:26:21 -080023#include <QFileSystemWatcher>
24#include <QCryptographicHash>
25#include <QDirIterator>
26#include <QFileInfo>
27#include <QDateTime>
28#include <QTimer>
Jared Lindblom4d1d00a2013-01-11 01:14:23 -080029#include <QDebug>
30#include <QHash>
Jared Lindblom2eead682013-01-12 20:26:21 -080031#include <structs.h>
Jared Lindblomc1259722013-01-12 16:47:32 -080032
33#define DEBUG 1
Jared Lindblom264310d2013-01-12 01:18:58 -080034
Jared Lindblom2eead682013-01-12 20:26:21 -080035class FileSystemWatcher : public QObject
jareda9541812013-01-09 00:08:46 -080036{
37 Q_OBJECT
Jared Lindblom4d1d00a2013-01-11 01:14:23 -080038
jareda9541812013-01-09 00:08:46 -080039public:
40 // constructor
Jared Lindblom2eead682013-01-12 20:26:21 -080041 FileSystemWatcher(QString dirPath, QObject* parent = 0);
jareda9541812013-01-09 00:08:46 -080042
43 // destructor
Jared Lindblom4d1d00a2013-01-11 01:14:23 -080044 ~FileSystemWatcher();
jareda9541812013-01-09 00:08:46 -080045
Jared Lindblomc1259722013-01-12 16:47:32 -080046signals:
47 // directory event signal
48 void dirEventSignal(std::vector<sEventInfo> dirChanges);
49
jareda9541812013-01-09 00:08:46 -080050private slots:
Jared Lindblomf8d32ad2013-01-12 11:32:27 -080051 // handle callback from watcher
52 void watcherCallbackSlot(QString dirPath);
53
54 // handle callback from timer
55 void timerCallbackSlot();
Jared Lindblom4d1d00a2013-01-11 01:14:23 -080056
Jared Lindblom2eead682013-01-12 20:26:21 -080057 // bootstrap
58 void bootstrap();
59
jareda9541812013-01-09 00:08:46 -080060private:
Jared Lindblomf8d32ad2013-01-12 11:32:27 -080061 // handle callback from either the watcher or timer
62 void handleCallback(QString dirPath);
63
Jared Lindblom4d1d00a2013-01-11 01:14:23 -080064 // scan directory and populate file list
Jared Lindblom8278b982013-01-12 15:45:09 -080065 QHash<QString, qint64> scanDirectory(QString dirPath);
Jared Lindblom4d1d00a2013-01-11 01:14:23 -080066
67 // reconcile directory, find changes
Jared Lindblomc1259722013-01-12 16:47:32 -080068 std::vector<sEventInfo> reconcileDirectory(QHash<QString, qint64> fileList, QString dirPath);
Jared Lindblomf8d32ad2013-01-12 11:32:27 -080069
70 // calculate checksum
71 QByteArray calcChecksum(QString absFilePath);
72
Jared Lindblomc1259722013-01-12 16:47:32 -080073 // print Changes (DEBUG)
74 void printChanges(std::vector<sEventInfo> dirChanges);
Jared Lindblom4d1d00a2013-01-11 01:14:23 -080075
76private:
jareda9541812013-01-09 00:08:46 -080077 QFileSystemWatcher* m_watcher; // filesystem watcher
Jared Lindbloma9996302013-01-12 00:25:16 -080078 QTimer* m_timer; // timer
Jared Lindblom4d1d00a2013-01-11 01:14:23 -080079
Jared Lindbloma9996302013-01-12 00:25:16 -080080 QString m_dirPath; // monitored path
Jared Lindblom8278b982013-01-12 15:45:09 -080081 QHash<QString, qint64> m_storedState; // stored state of directory
jareda9541812013-01-09 00:08:46 -080082};
83
84#endif // FILESYSTEMWATCHER_H