-
Notifications
You must be signed in to change notification settings - Fork 0
/
savegamelistener.cpp
74 lines (68 loc) · 2.55 KB
/
savegamelistener.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
#include "savegamelistener.h"
#include <QDir>
#include <QFile>
SaveGameListener::SaveGameListener()
{
m_fileWatcher = new QFileSystemWatcher(this);
QObject::connect(m_fileWatcher,
&QFileSystemWatcher::fileChanged,
this,
&SaveGameListener::onfileChanged);
QObject::connect(m_fileWatcher,
&QFileSystemWatcher::directoryChanged,
this,
&SaveGameListener::onDirectoryChanged);
}
QUrl SaveGameListener::listeningFolder() const
{
return m_listeningFolder;
}
void SaveGameListener::setListeningFolder(const QUrl &newListeningFolder)
{
if (m_listeningFolder == newListeningFolder) {
return;
}
qDebug() << newListeningFolder;
m_listeningFolder = newListeningFolder;
m_fileWatcher->removePaths(m_fileWatcher->directories());
m_fileWatcher->addPath(m_listeningFolder.toLocalFile());
qDebug() << QDir(m_listeningFolder.toLocalFile()).entryList();
QFileInfoList files = QDir(m_listeningFolder.toLocalFile()).entryInfoList();
for (int i = 0; i < files.size(); ++i) {
if(!files.at(i).isFile()) {
continue;
}
m_fileWatcher->addPath(files.at(i).filePath());
}
qDebug() << m_fileWatcher->files();
emit listeningFolderChanged();
}
void SaveGameListener::onfileChanged(const QString &path)
{
if(!m_fileWatcher->files().contains(path) && !QFileInfo::exists(path)) {
m_fileWatcher->addPath(path);
}
// qDebug() << QFileInfo(path).lastModified() << QDateTime::currentDateTime();
// qDebug() << QFileInfo(path).lastModified().msecsTo(QDateTime::currentDateTime());
// if(QFileInfo(path).lastModified().msecsTo(QDateTime::currentDateTime()) > 0) {
emit this->fileChanged(path);
qInfo() << "file change:" << path;
// }
}
void SaveGameListener::onDirectoryChanged(const QString &path)
{
QFileInfoList fileInfoList = QDir(m_listeningFolder.toLocalFile()).entryInfoList();
for (int i = 0; i < fileInfoList.size(); ++i) {
if(!fileInfoList.at(i).isFile()) {
continue;
}
if (m_fileWatcher->files().contains(fileInfoList.at(i).filePath())) {
continue;
}
if(fileInfoList.at(i).filePath().endsWith("_temp.hoi4")) {
continue;
}
qInfo() << "add file: " << path << " : " << m_fileWatcher->addPath(fileInfoList.at(i).filePath());
emit this->fileChanged(fileInfoList.at(i).filePath());
}
}