-
Notifications
You must be signed in to change notification settings - Fork 26
/
filesystemwatcher.cpp
150 lines (124 loc) · 4.59 KB
/
filesystemwatcher.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
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
#include "filesystemwatcher.h"
#include <QDebug>
#include <QDir>
#include <QFileInfo>
#include <QMainWindow>
#include "mainwindow.h"
#include "ui_mainwindow.h"
extern bool ReLoad;
extern MainWindow* mw_one;
extern QVector<QString> openFileList;
FileSystemWatcher* FileSystemWatcher::m_pInstance = NULL;
FileSystemWatcher::FileSystemWatcher(QObject* parent) : QObject(parent) {}
// 监控文件或目录
void FileSystemWatcher::addWatchPath(QString path) {
// qDebug() << QString("Add to watch: %1").arg(path);
if (m_pInstance == NULL) {
m_pInstance = new FileSystemWatcher();
m_pInstance->m_pSystemWatcher = new QFileSystemWatcher();
// 连接QFileSystemWatcher的directoryChanged和fileChanged信号到相应的槽
connect(m_pInstance->m_pSystemWatcher, SIGNAL(directoryChanged(QString)),
m_pInstance, SLOT(directoryUpdated(QString)));
connect(m_pInstance->m_pSystemWatcher, SIGNAL(fileChanged(QString)),
m_pInstance, SLOT(fileUpdated(QString)));
}
// 添加监控路径
m_pInstance->m_pSystemWatcher->addPath(path);
// 如果添加路径是一个目录,保存当前内容列表
QFileInfo file(path);
if (file.isDir()) {
const QDir dirw(path);
m_pInstance->m_currentContentsMap[path] = dirw.entryList(
QDir::NoDotAndDotDot | QDir::AllDirs | QDir::Files, QDir::DirsFirst);
}
}
void FileSystemWatcher::removeWatchPath(QString path) {
m_pInstance->m_pSystemWatcher->removePath(path);
}
// 只要任何监控的目录更新(添加、删除、重命名),就会调用。
void FileSystemWatcher::directoryUpdated(const QString& path) {
qDebug() << QString("Directory updated: %1").arg(path);
// 比较最新的内容和保存的内容找出区别(变化)
QStringList currEntryList = m_currentContentsMap[path];
const QDir dir(path);
QStringList newEntryList = dir.entryList(
QDir::NoDotAndDotDot | QDir::AllDirs | QDir::Files, QDir::DirsFirst);
QSet<QString> newDirSet;
QSet<QString> currentDirSet;
#if (QT_VERSION <= QT_VERSION_CHECK(5, 9, 9))
{
newDirSet = QSet<QString>::fromList(newEntryList); //旧
}
#endif
#if (QT_VERSION >= QT_VERSION_CHECK(5, 15, 0))
{
newDirSet = QSet<QString>(newEntryList.begin(),
newEntryList.end()); //新
}
#endif
#if (QT_VERSION <= QT_VERSION_CHECK(5, 9, 9))
{
currentDirSet = QSet<QString>::fromList(currEntryList); //旧
}
#endif
#if (QT_VERSION >= QT_VERSION_CHECK(5, 15, 0))
{
currentDirSet = QSet<QString>(currEntryList.begin(),
currEntryList.end()); //新
}
#endif
// 添加了文件
QSet<QString> newFiles = newDirSet - currentDirSet;
QStringList newFile = newFiles.values();
// 文件已被移除
QSet<QString> deletedFiles = currentDirSet - newDirSet;
QStringList deleteFile = deletedFiles.values();
// 更新当前设置
m_currentContentsMap[path] = newEntryList;
if (!newFile.isEmpty() && !deleteFile.isEmpty()) {
// 文件/目录重命名
if ((newFile.count() == 1) && (deleteFile.count() == 1)) {
qDebug() << QString("File Renamed from %1 to %2")
.arg(deleteFile.first())
.arg(newFile.first());
}
} else {
// 添加新文件/目录至Dir
if (!newFile.isEmpty()) {
qDebug() << "New Files/Dirs added: " << newFile;
foreach (QString file, newFile) {
// 处理操作每个新文件....
}
}
// 从Dir中删除文件/目录
if (!deleteFile.isEmpty()) {
qDebug() << "Files/Dirs deleted: " << deleteFile;
foreach (QString file, deleteFile) {
// 处理操作每个被删除的文件....
}
}
}
}
// 文件修改时调用
void FileSystemWatcher::fileUpdated(const QString& path) {
// qDebug() << QString("The file %1 at path %2 is
// updated").arg(strName).arg(strPath);
if (mw_one->getMD5(path) == mw_one->getMD5FromList(path)) return;
QFileInfo fi(path);
if (fi.exists()) {
mw_one->ui->lblFileName->setText(tr("The file has been modified by another "
"program. Do you want to reload?") +
"\n\n" + path +
"\nMD5: " + mw_one->getMD5FromList(path) +
" -> " + mw_one->getMD5(path));
mw_one->ui->frameTip->setHidden(false);
ReLoad = true;
mw_one->strModiFile = path;
bool re = false;
for (int i = 0; i < mw_one->reLoadByModiList.count(); i++) {
if (mw_one->reLoadByModiList.at(i) == path) re = true;
}
if (!re) mw_one->reLoadByModiList.append(path);
}
mw_one->addFilesWatch();
}