-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQFile_Explorer.cpp
More file actions
155 lines (105 loc) · 3.62 KB
/
QFile_Explorer.cpp
File metadata and controls
155 lines (105 loc) · 3.62 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
#include "QFile_Explorer.h"
QFile_Explorer::QFile_Explorer(QWidget* parent) : QMainWindow(parent)
{
ui->setupUi(this);
// Connect button signal to appropriate slot
connect(ui->Action_Btn, &QPushButton::released, this, &QFile_Explorer::EnterPath);
connect(ui->Action_Btn_2, &QPushButton::released, this, &QFile_Explorer::SaveLogs);
// Inicialize file Monitoring System
watcher = new QFileSystemWatcher(this);
QObject::connect(watcher, SIGNAL(directoryChanged(QString)), this, SLOT(NotifieChanegs()));
QObject::connect(watcher, SIGNAL(fileChanged(QString)), this, SLOT(NotifieChanegs()));
}
QFile_Explorer::~QFile_Explorer() {
delete ui;
}
void QFile_Explorer::EnterPath(){
ui->Output_Wnd->clear();
watcher->removePaths(oldList);
oldList.clear();
//display the input window & set file path to top input_wnd
dirPath = QFileDialog::getExistingDirectory(this, " Choose the directory to be monitored : ", QDir::homePath());
ui->Input_Wnd->setText(dirPath);
// Refrresh contents to be monitored & add paths to watcher
oldList = RefreshContents(dirPath);
oldList.sort();
ui->Output_Wnd->append(" ---------------------------[ Monitored Content ]--------------------------- ");
//Dispalay contents in sorted order,
auto moniDirs = watcher->directories();
moniDirs.sort();
auto moniFiles = watcher->files();
moniFiles.sort();
auto monitored = moniDirs + moniFiles;
Q_FOREACH(QString dirs, monitored)
ui->Output_Wnd->append(dirs);
ui->Output_Wnd->append(" ---------------------------[ MSGS ]--------------------------- ");
}
void QFile_Explorer::NotifieChanegs()
{
dirPath = ui->Input_Wnd->text();
newList = RefreshContents(dirPath);
newList.sort();
QSet<QString> oldSet(oldList.begin(), oldList.end());
QSet<QString> newSet(newList.begin(), newList.end());
if (oldList < newList) {
// file was delited
auto intersection = oldSet.subtract(newSet);
foreach(QString name, intersection)
{
ui->Output_Wnd->setTextColor(Qt::red);
ui->Output_Wnd->append(" [!] File was delited : " + name);
}
}
else if (oldList > newList) {
// file was created
auto intersection = newSet.subtract(oldSet);
foreach (QString name ,intersection)
{
ui->Output_Wnd->setTextColor(Qt::darkBlue);
ui->Output_Wnd->append(" [!] File was created : " + name);
}
}
else{
ui->Output_Wnd->setTextColor(Qt::darkGreen);
ui->Output_Wnd->append(" [!] File was modified ");
}
ui->Output_Wnd->setTextColor(Qt::black);
oldList = newList;
}
QStringList QFile_Explorer::RefreshContents(QString& dirpath) {
QStringList list;
QDir dir = dir.toNativeSeparators(dirpath);
dir.setSorting(QDir::SortFlag::DirsFirst | QDir::SortFlag::Name);
dir.setFilter(QDir::Files | QDir::NoDotAndDotDot);
// Iterate throught folder contents and add paths to the monitoring system ;
// Add main directory to watch
watcher->addPath(dirpath);
list.append(dirpath);
QDirIterator folderIt(dir);
while (folderIt.hasNext()) {
QFile file(folderIt.next());
filePath = file.fileName();
// add dirrectory files to wach
if (QFile::exists(filePath)) {
watcher->addPath(filePath);
list.append(filePath);
}
}
return list;
}
void QFile_Explorer::SaveLogs()
{
auto logpath = QFileDialog::getExistingDirectory(this, " Choose the directory to be monitored : ", QDir::homePath());
ui->Input_Wnd_2->setText(logpath);
auto TableContents = ui->Output_Wnd->toPlainText();
QString filename = logpath + "/LOG.txt";
QFile file(filename);
if (!file.open(QIODevice::WriteOnly)) {
file.close();
}
else {
file.write(TableContents.toUtf8());
ui->Output_Wnd->append(" The log file was created at :" + filename);
file.close();
}
}