-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmainwindow.cpp
More file actions
165 lines (142 loc) · 4.95 KB
/
mainwindow.cpp
File metadata and controls
165 lines (142 loc) · 4.95 KB
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::changeEvent(QEvent *event)
{
QMainWindow::changeEvent(event);
switch (event->type()) {
case QEvent::LanguageChange:
ui->retranslateUi(this);
break;
default:
break;
}
}
// execute when user click on the Browse button
void MainWindow::on_btnBrowse_clicked()
{
Path = QFileDialog::getExistingDirectory(this,tr("Open Directory"),
".",QFileDialog::ShowDirsOnly|QFileDialog::DontResolveSymlinks);
ui->txtPath->setText(Path);
ui->m_folderList->clear();
watcher = new QFileSystemWatcher(this);
watcher->addPath(Path);
disconnect(watcher,&QFileSystemWatcher::directoryChanged,
this,&MainWindow::NotifyChanges);
disconnect(watcher,&QFileSystemWatcher::fileChanged,
this,&MainWindow::NotifyChanges);
QDir dir(Path);
// for more inforation please refer to Qt Documentation
dir.setFilter(QDir::Dirs|QDir::Files|QDir::NoDotAndDotDot);
dir.setSorting(QDir::Size|QDir::Reversed);
// now lets get the files and directories information in the folder
QFileInfoList list = dir.entryInfoList();
for (int i = 0; i < list.size(); ++i)
{
QListWidgetItem *wi = new QListWidgetItem;
wi->setText(list.at(i).fileName());
if(QFileInfo(list.at(i).filePath()).isDir())
wi->setForeground(Qt::blue);
ui->m_folderList->addItem(wi);
}
// now we are connecting the watcherr object to it's signals
connect(watcher,&QFileSystemWatcher::directoryChanged,
this,&MainWindow::NotifyChanges);
connect(watcher,&QFileSystemWatcher::fileChanged,
this,&MainWindow::NotifyChanges);
}
// implementation of our function
void MainWindow::NotifyChanges(const QString &path)
{
qDebug() << "Signal Received !" << path;
QStringList oldList, newList;
//getting the old list from QListView widget and
// the new list which is the current files in the folder
//then compare them together
QMap<QString,QString> map_old;
for (int i = 0; i < ui->m_folderList->count(); ++i) {
QListWidgetItem *item = ui->m_folderList->item(i);
oldList << item->text();
map_old[item->text()] = Path + "/" + item->text();
}
qSort(oldList);
// now getting new status of the folder
QDir dir(path);
// for more inforation please refer to Qt Documentation
dir.setFilter(QDir::Dirs|QDir::Files|QDir::NoDotAndDotDot);
dir.setSorting(QDir::Size|QDir::Reversed);
// now lets get the files and directories information in the folder
QMap<QString,QString> map;
QFileInfoList list = dir.entryInfoList();
for (int i = 0; i < list.size(); ++i)
{
newList << list.at(i).fileName();
map[list.at(i).fileName()] = list.at(i).filePath();
}
qSort(newList);
//Differences between two lists;
QSet<QString> intersection;
if(oldList.count() > newList.count())
{
ui->m_folderList->clear();
intersection = oldList.toSet().intersect(newList.toSet());
foreach (const QString &value, intersection)
{
QListWidgetItem *wi = new QListWidgetItem;
wi->setText(value);
if(QFileInfo(map[value]).isDir())
wi->setForeground(Qt::blue);
ui->m_folderList->addItem(wi);
}
this->statusBar()->showMessage("[!] File Removed");
}
else if(oldList.count() < newList.count())
{
intersection = newList.toSet().subtract(oldList.toSet());
foreach (const QString &value, intersection)
{
QListWidgetItem *wi = new QListWidgetItem;
wi->setText(value);
if(QFileInfo(map[value]).isDir())
wi->setForeground(Qt::red);
else
wi->setForeground(Qt::green);
ui->m_folderList->addItem(wi);
}
ui->m_folderList->sortItems();
qDebug() << intersection.toList();
this->statusBar()->showMessage("[!] File Added!");
}
else
{
ui->m_folderList->clear();
intersection = oldList.toSet().subtract(newList.toSet());
foreach (const QString &value, newList)
{
QListWidgetItem *wi = new QListWidgetItem;
wi->setText(value);
if(map_old.find(value) == map_old.end())
{
if(QFileInfo(map[value]).isDir())
wi->setForeground(Qt::red);
else
wi->setForeground(Qt::green);
}
else {
if(QFileInfo(map[value]).isDir())
wi->setForeground(Qt::blue);
}
ui->m_folderList->addItem(wi);
}
this->statusBar()->showMessage("[!] File Modified!");
}
}