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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316 | /* ============================================================
*
* This file is a part of digiKam project
* https://www.digikam.org
*
* Date : 2011-11-07
* Description : Directory watch interface
*
* SPDX-FileCopyrightText: 2011 by Marcel Wiesweg <marcel dot wiesweg at gmx dot de>
* SPDX-FileCopyrightText: 2015-2025 by Gilles Caulier <caulier dot gilles at gmail dot com>
*
* SPDX-License-Identifier: GPL-2.0-or-later
*
* ============================================================ */
#include "albumwatch.h"
// Qt includes
#include <QFileSystemWatcher>
#include <QDateTime>
#include <QFileInfo>
#include <QDir>
// Local includes
#include "digikam_debug.h"
#include "album.h"
#include "albummanager.h"
#include "collectionlocation.h"
#include "collectionmanager.h"
#include "dbengineparameters.h"
#include "applicationsettings.h"
#include "scancontroller.h"
#include "dio.h"
namespace Digikam
{
class Q_DECL_HIDDEN AlbumWatch::Private
{
public:
Private() = default;
bool inBlackList(const QString& path) const;
bool inDirWatchParametersBlackList(const QFileInfo& info, const QString& path);
QList<QDateTime> buildDirectoryModList(const QFileInfo& dbFile) const;
public:
QFileSystemWatcher* dirWatch = nullptr;
DbEngineParameters params;
QStringList fileNameBlackList;
QList<QDateTime> dbPathModificationDateList;
};
bool AlbumWatch::Private::inBlackList(const QString& path) const
{
// Filter out dirty signals triggered by changes on the database file
for (const QString& bannedFile : std::as_const(fileNameBlackList))
{
if (path.endsWith(bannedFile))
{
return true;
}
}
return false;
}
bool AlbumWatch::Private::inDirWatchParametersBlackList(const QFileInfo& info, const QString& path)
{
if (params.isSQLite())
{
QDir dir;
if (info.isDir())
{
dir = QDir(path);
}
else
{
dir = info.dir();
}
QFileInfo dbFile(params.SQLiteDatabaseFile());
// is the signal for the directory containing the database file?
if (dbFile.dir() == dir)
{
// retrieve modification dates
QList<QDateTime> modList = buildDirectoryModList(dbFile);
// check for equality
if (modList == dbPathModificationDateList)
{
//qCDebug(DIGIKAM_GENERAL_LOG) << "Filtering out db-file-triggered dir watch signal";
// we can skip the signal
return true;
}
// set new list
dbPathModificationDateList = modList;
}
}
return false;
}
QList<QDateTime> AlbumWatch::Private::buildDirectoryModList(const QFileInfo& dbFile) const
{
// Retrieve modification dates
QList<QDateTime> modList;
QFileInfoList fileInfoList = dbFile.dir().entryInfoList(QDir::Dirs |
QDir::Files |
QDir::NoDotAndDotDot);
// Build the list
for (const QFileInfo& info : std::as_const(fileInfoList))
{
// Ignore digikam4.db and journal and other temporary files
if (!fileNameBlackList.contains(info.fileName()))
{
modList << info.lastModified();
}
}
return modList;
}
// -------------------------------------------------------------------------------------
AlbumWatch::AlbumWatch(AlbumManager* const parent)
: QObject(parent),
d (new Private)
{
d->dirWatch = new QFileSystemWatcher(this);
if (ApplicationSettings::instance()->getAlbumMonitoring())
{
qCDebug(DIGIKAM_GENERAL_LOG) << "AlbumWatch use QFileSystemWatcher";
connect(d->dirWatch, SIGNAL(directoryChanged(QString)),
this, SLOT(slotQFSWatcherDirty(QString)));
connect(d->dirWatch, SIGNAL(fileChanged(QString)),
this, SLOT(slotQFSWatcherDirty(QString)));
connect(parent, SIGNAL(signalAlbumAdded(Album*)),
this, SLOT(slotAlbumAdded(Album*)));
connect(parent, SIGNAL(signalAlbumRenamed(Album*)),
this, SLOT(slotAlbumAdded(Album*)));
connect(parent, SIGNAL(signalAlbumNewPath(Album*)),
this, SLOT(slotAlbumAdded(Album*)));
connect(parent, SIGNAL(signalAlbumAboutToBeDeleted(Album*)),
this, SLOT(slotAlbumAboutToBeDeleted(Album*)));
}
else
{
qCDebug(DIGIKAM_GENERAL_LOG) << "AlbumWatch is disabled";
}
}
AlbumWatch::~AlbumWatch()
{
delete d;
}
void AlbumWatch::clear()
{
if (d->dirWatch && !d->dirWatch->directories().isEmpty())
{
d->dirWatch->removePaths(d->dirWatch->directories());
}
}
void AlbumWatch::removeWatchedPAlbums(const PAlbum* const album)
{
if (!album || d->dirWatch->directories().isEmpty())
{
return;
}
const auto dirs = d->dirWatch->directories();
for (const QString& dir : dirs)
{
if (dir.startsWith(album->folderPath()))
{
d->dirWatch->removePath(dir);
}
}
}
void AlbumWatch::setDbEngineParameters(const DbEngineParameters& params)
{
d->params = params;
d->fileNameBlackList.clear();
// filter out notifications caused by database operations
if (params.isSQLite())
{
d->fileNameBlackList << QLatin1String("thumbnails-digikam.db")
<< QLatin1String("thumbnails-digikam.db-journal");
d->fileNameBlackList << QLatin1String("recognition.db")
<< QLatin1String("recognition.db-journal");
QFileInfo dbFile(params.SQLiteDatabaseFile());
d->fileNameBlackList << dbFile.fileName()
<< dbFile.fileName() + QLatin1String("-journal");
// ensure this is done after setting up the black list
d->dbPathModificationDateList = d->buildDirectoryModList(dbFile);
}
}
void AlbumWatch::slotAlbumAdded(Album* a)
{
if (a->isRoot() || a->isTrashAlbum() || (a->type() != Album::PHYSICAL))
{
return;
}
PAlbum* const album = static_cast<PAlbum*>(a);<--- Variable 'album' can be declared as pointer to const
CollectionLocation location = CollectionManager::instance()->locationForAlbumRootId(album->albumRootId());
if (!location.isAvailable())
{
return;
}
QString dir = album->folderPath();
if (dir.isEmpty())
{
return;
}
d->dirWatch->addPath(dir);
}
void AlbumWatch::slotAlbumAboutToBeDeleted(Album* a)
{
if (a->isRoot() || a->isTrashAlbum() || (a->type() != Album::PHYSICAL))
{
return;
}
PAlbum* const album = static_cast<PAlbum*>(a);<--- Variable 'album' can be declared as pointer to const
QString dir = album->folderPath();
if (dir.isEmpty())
{
return;
}
d->dirWatch->removePath(dir);
}
void AlbumWatch::rescanDirectory(const QString& dir)
{
if (DIO::itemsUnderProcessing())
{
return;
}
qCDebug(DIGIKAM_GENERAL_LOG) << "Detected change, triggering rescan of" << dir;
ScanController::instance()->scheduleCollectionScanExternal(dir);
}
void AlbumWatch::slotQFSWatcherDirty(const QString& path)
{
if (d->inBlackList(path))
{
return;
}
QFileInfo info(path);
if (d->inDirWatchParametersBlackList(info, path))
{
return;
}
if (info.isDir())
{
rescanDirectory(path);
}
else
{
rescanDirectory(info.path());
}
}
} // namespace Digikam
#include "moc_albumwatch.cpp"
|