-
Notifications
You must be signed in to change notification settings - Fork 0
/
watch.cpp
57 lines (47 loc) · 1.89 KB
/
watch.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include "watch.h"
Watch::Watch(QObject *parent) :
QObject(parent)
{
root_path= QString("/Users/tudalex/testing");
mirror_path = QString("/Users/tudalex/testing2");
//TODO: Only works wit absolute paths
m_tray = new QSystemTrayIcon();
m_tray->show();
fswatch = new QFileSystemWatcher();
fswatch->addPaths(recurse_get(root_path));
connect(fswatch, SIGNAL(fileChanged(QString)),this, SLOT(fileChanged(QString)));
connect(fswatch, SIGNAL(directoryChanged(QString)), this, SLOT(folderChanged(QString)));
}
void Watch::fileChanged(QString file)
{
m_tray->showMessage("File Changed", file);
qDebug()<<"File changed: "<<file;
QString relative_path = QString(file).remove(0,root_path.size());
qDebug()<<"file: "<<file<<"relative path: "<<relative_path <<"full new path: "<< QString(mirror_path).append(relative_path);
QFile::remove(QString(mirror_path).append(relative_path));
qDebug()<<"Copying returned: "<<QFile::copy(file,QString(mirror_path).append(relative_path));
}
void Watch::folderChanged(QString folder)
{
m_tray->showMessage("Folder Changed", folder );
fswatch->addPaths(recurse_get(folder, false));
qDebug()<<"Folder changed: "<<folder;
}
QStringList Watch::recurse_get(const QString &path, bool recurse )
{
QStringList list;
QDir dir(path);
if (recurse)
foreach (QString s, dir.entryList(QDir::Dirs|QDir::NoDotAndDotDot))
{
QStringList temp = recurse_get(dir.absoluteFilePath(s));
list<<temp; //Processing all folders first
qDebug()<<"Folder: "<<s<<" Path: "<<dir.absoluteFilePath(s);
}
foreach (QString s, dir.entryList(QDir::Files|QDir::Dirs|QDir::NoDotAndDotDot))
{
list<<dir.absoluteFilePath(s);
qDebug()<<"File: "<<s<<" Path: "<<dir.absoluteFilePath(s);
}
return list<<path;
}