Debian Codegraph @

core/libs/album/engine/albumwatch.cpp

raw
1 /* ============================================================
2 *
3 * This file is a part of digiKam project
4 * https://www.digikam.org
5 *
6 * Date : 2011-11-07
7 * Description : Directory watch interface
8 *
9 * SPDX-FileCopyrightText: 2011 by Marcel Wiesweg <marcel dot wiesweg at gmx dot de>
10 * SPDX-FileCopyrightText: 2015-2026 by Gilles Caulier <caulier dot gilles at gmail dot com>
11 *
12 * SPDX-License-Identifier: GPL-2.0-or-later
13 *
14 * ============================================================ */
15
16 #include "albumwatch.h"
17
18 // Qt includes
19
20 #include <QFileSystemWatcher>
21 #include <QDateTime>
22 #include <QFileInfo>
23 #include <QDir>
24
25 // Local includes
26
27 #include "digikam_debug.h"
28 #include "album.h"
29 #include "albummanager.h"
30 #include "collectionlocation.h"
31 #include "collectionmanager.h"
32 #include "dbengineparameters.h"
33 #include "applicationsettings.h"
34 #include "scancontroller.h"
35 #include "dio.h"
36
37 namespace Digikam
38 {
39
40 class Q_DECL_HIDDEN AlbumWatch::Private
41 {
42 public:
43
44 Private() = default;
45
46 bool inBlackList(const QString& path) const;
47 bool inDirWatchParametersBlackList(const QFileInfo& info, const QString& path);
48 QList<QDateTime> buildDirectoryModList(const QFileInfo& dbFile) const;
49
50 public:
51
52 QFileSystemWatcher* dirWatch = nullptr;
53
54 DbEngineParameters params;
55 QStringList fileNameBlackList;
56 QList<QDateTime> dbPathModificationDateList;
57 };
58
59 bool AlbumWatch::Private::inBlackList(const QString& path) const
60 {
61 // Filter out dirty signals triggered by changes on the database file
62
63 for (const QString& bannedFile : std::as_const(fileNameBlackList))
64 {
65 if (path.endsWith(bannedFile))
66 {
67 return true;
68 }
69 }
70
71 return false;
72 }
73
74 bool AlbumWatch::Private::inDirWatchParametersBlackList(const QFileInfo& info, const QString& path)
75 {
76 if (params.isSQLite())
77 {
78 QDir dir;
79
80 if (info.isDir())
81 {
82 dir = QDir(path);
83 }
84 else
85 {
86 dir = info.dir();
87 }
88
89 QFileInfo dbFile(params.SQLiteDatabaseFile());
90
91 // is the signal for the directory containing the database file?
92
93 if (dbFile.dir() == dir)
94 {
95 // retrieve modification dates
96
97 QList<QDateTime> modList = buildDirectoryModList(dbFile);
98
99 // check for equality
100
101 if (modList == dbPathModificationDateList)
102 {
103 //qCDebug(DIGIKAM_GENERAL_LOG) << "Filtering out db-file-triggered dir watch signal";
104
105 // we can skip the signal
106
107 return true;
108 }
109
110 // set new list
111
112 dbPathModificationDateList = modList;
113 }
114 }
115
116 return false;
117 }
118
119 QList<QDateTime> AlbumWatch::Private::buildDirectoryModList(const QFileInfo& dbFile) const
120 {
121 // Retrieve modification dates
122
123 QList<QDateTime> modList;
124 QFileInfoList fileInfoList = dbFile.dir().entryInfoList(QDir::Dirs |
125 QDir::Files |
126 QDir::NoDotAndDotDot);
127
128 // Build the list
129
130 for (const QFileInfo& info : std::as_const(fileInfoList))
131 {
132 // Ignore digikam4.db and journal and other temporary files
133
134 if (!fileNameBlackList.contains(info.fileName()))
135 {
136 modList << info.lastModified();
137 }
138 }
139
140 return modList;
141 }
142
143 // -------------------------------------------------------------------------------------
144
145 AlbumWatch::AlbumWatch(AlbumManager* const parent)
146 : QObject(parent),
147 d (new Private)
148 {
149 d->dirWatch = new QFileSystemWatcher(this);
150
151 if (ApplicationSettings::instance()->getAlbumMonitoring())
152 {
153 qCDebug(DIGIKAM_GENERAL_LOG) << "AlbumWatch use QFileSystemWatcher";
154
155 connect(d->dirWatch, SIGNAL(directoryChanged(QString)),
156 this, SLOT(slotQFSWatcherDirty(QString)));
157
158 connect(d->dirWatch, SIGNAL(fileChanged(QString)),
159 this, SLOT(slotQFSWatcherDirty(QString)));
160
161 connect(parent, SIGNAL(signalAlbumAdded(Album*)),
162 this, SLOT(slotAlbumAdded(Album*)));
163
164 connect(parent, SIGNAL(signalAlbumRenamed(Album*)),
165 this, SLOT(slotAlbumAdded(Album*)));
166
167 connect(parent, SIGNAL(signalAlbumNewPath(Album*)),
168 this, SLOT(slotAlbumAdded(Album*)));
169
170 connect(parent, SIGNAL(signalAlbumAboutToBeDeleted(Album*)),
171 this, SLOT(slotAlbumAboutToBeDeleted(Album*)));
172 }
173 else
174 {
175 qCDebug(DIGIKAM_GENERAL_LOG) << "AlbumWatch is disabled";
176 }
177 }
178
179 AlbumWatch::~AlbumWatch()
180 {
181 delete d;
182 }
183
184 void AlbumWatch::clear()
185 {
186 if (d->dirWatch && !d->dirWatch->directories().isEmpty())
187 {
188 d->dirWatch->removePaths(d->dirWatch->directories());
189 }
190 }
191
192 void AlbumWatch::removeWatchedPAlbums(const PAlbum* const album)
193 {
194 if (!album || d->dirWatch->directories().isEmpty())
195 {
196 return;
197 }
198
199 const auto dirs = d->dirWatch->directories();
200
201 for (const QString& dir : dirs)
202 {
203 if (dir.startsWith(album->folderPath()))
204 {
205 d->dirWatch->removePath(dir);
206 }
207 }
208 }
209
210 void AlbumWatch::setDbEngineParameters(const DbEngineParameters& params)
211 {
212 d->params = params;
213
214 d->fileNameBlackList.clear();
215
216 // filter out notifications caused by database operations
217
218 if (params.isSQLite())
219 {
220 d->fileNameBlackList << QLatin1String("thumbnails-digikam.db")
221 << QLatin1String("thumbnails-digikam.db-journal");
222 d->fileNameBlackList << QLatin1String("recognition.db")
223 << QLatin1String("recognition.db-journal");
224
225 QFileInfo dbFile(params.SQLiteDatabaseFile());
226 d->fileNameBlackList << dbFile.fileName()
227 << dbFile.fileName() + QLatin1String("-journal");
228
229 // ensure this is done after setting up the black list
230
231 d->dbPathModificationDateList = d->buildDirectoryModList(dbFile);
232 }
233 }
234
235 void AlbumWatch::slotAlbumAdded(Album* a)
236 {
237 if (a->isRoot() || a->isTrashAlbum() || (a->type() != Album::PHYSICAL))
238 {
239 return;
240 }
241
242 const PAlbum* const album = static_cast<PAlbum*>(a);
243 CollectionLocation location = CollectionManager::instance()->locationForAlbumRootId(album->albumRootId());
244
245 if (!location.isAvailable())
246 {
247 return;
248 }
249
250 QString dir = album->folderPath();
251
252 if (dir.isEmpty())
253 {
254 return;
255 }
256
257 d->dirWatch->addPath(dir);
258 }
259
260 void AlbumWatch::slotAlbumAboutToBeDeleted(Album* a)
261 {
262 if (a->isRoot() || a->isTrashAlbum() || (a->type() != Album::PHYSICAL))
263 {
264 return;
265 }
266
267 const PAlbum* const album = static_cast<PAlbum*>(a);
268 QString dir = album->folderPath();
269
270 if (dir.isEmpty())
271 {
272 return;
273 }
274
275 d->dirWatch->removePath(dir);
276 }
277
278 void AlbumWatch::rescanDirectory(const QString& dir)
279 {
280 if (DIO::itemsUnderProcessing())
281 {
282 return;
283 }
284
285 qCDebug(DIGIKAM_GENERAL_LOG) << "Detected change, triggering rescan of" << dir;
286
287 ScanController::instance()->scheduleCollectionScanExternal(dir);
288 }
289
290 void AlbumWatch::slotQFSWatcherDirty(const QString& path)
291 {
292 if (d->inBlackList(path))
293 {
294 return;
295 }
296
297 QFileInfo info(path);
298
299 if (d->inDirWatchParametersBlackList(info, path))
300 {
301 return;
302 }
303
304 if (info.isDir())
305 {
306 rescanDirectory(path);
307 }
308 else
309 {
310 rescanDirectory(info.path());
311 }
312 }
313
314 } // namespace Digikam
315
316 #include "moc_albumwatch.cpp"