Debian Codegraph @

core/libs/album/manager/albummanager_palbum.cpp

raw
1 /* ============================================================
2 *
3 * This file is a part of digiKam project
4 * https://www.digikam.org
5 *
6 * Date : 2004-06-15
7 * Description : Albums manager interface - Physical Album helpers.
8 *
9 * SPDX-FileCopyrightText: 2006-2026 by Gilles Caulier <caulier dot gilles at gmail dot com>
10 * SPDX-FileCopyrightText: 2006-2011 by Marcel Wiesweg <marcel dot wiesweg at gmx dot de>
11 * SPDX-FileCopyrightText: 2015 by Mohamed_Anwer <m_dot_anwer at gmx dot com>
12 *
13 * SPDX-License-Identifier: GPL-2.0-or-later
14 *
15 * ============================================================ */
16
17 #include "albummanager_p.h"
18
19 namespace Digikam
20 {
21
22 void AlbumManager::scanPAlbums()
23 {
24 d->scanPAlbumsTimer->stop();
25
26 // first insert all the current normal PAlbums into a map for quick lookup
27
28 QHash<int, PAlbum*> oldAlbums;
29 AlbumIterator it(d->rootPAlbum);
30
31 while (it.current())
32 {
33 PAlbum* const a = (PAlbum*)(*it);
34 oldAlbums[a->id()] = a;
35 ++it;
36 }
37
38 // scan db and get a list of all albums
39
40 QList<AlbumInfo> currAlbums = CoreDbAccess().db()->scanAlbums();
41
42 // sort by relative path so that parents are created before children
43
44 std::sort(currAlbums.begin(), currAlbums.end());
45
46 QList<AlbumInfo> newAlbums;
47
48 // go through all the Albums and see which ones are already present
49
50 for (const AlbumInfo& info : std::as_const(currAlbums))
51 {
52 // check that location of album is available
53
54 if (
55 d->showOnlyAvailableAlbums &&
56 !CollectionManager::instance()->locationForAlbumRootId(info.albumRootId).isAvailable()
57 )
58 {
59 continue;
60 }
61
62 if (oldAlbums.contains(info.id))
63 {
64 oldAlbums.remove(info.id);
65 }
66 else
67 {
68 newAlbums << info;
69 }
70 }
71
72 // now oldAlbums contains all the deleted albums and
73 // newAlbums contains all the new albums
74
75 // delete old albums, informing all frontends
76
77 // The albums have to be removed with children being removed first,
78 // removePAlbum takes care of that.
79 // So we only feed it the albums from oldAlbums topmost in hierarchy.
80
81 QSet<PAlbum*> topMostOldAlbums;
82
83 for (PAlbum* const album : std::as_const(oldAlbums)) // cppcheck-suppress constVariablePointer
84 {
85 if (album->isTrashAlbum())
86 {
87 continue;
88 }
89
90 if (!album->parent() || !oldAlbums.contains(album->parent()->id()))
91 {
92 topMostOldAlbums << album;
93 }
94 }
95
96 for (PAlbum* const album : std::as_const(topMostOldAlbums))
97 {
98 // recursively removes all children and the album
99
100 removePAlbum(album);
101 }
102
103 // sort by relative path so that parents are created before children
104
105 std::sort(newAlbums.begin(), newAlbums.end());
106
107 // create all new albums
108
109 for (const AlbumInfo& info : std::as_const(newAlbums))
110 {
111 if (info.relativePath.isEmpty())
112 {
113 continue;
114 }
115
116 PAlbum* album = nullptr;
117 PAlbum* parent = nullptr;
118
119 if (info.relativePath == QLatin1String("/"))
120 {
121 // Albums that represent the root directory of an album root
122 // We have them as here new albums first time after their creation
123
124 parent = d->rootPAlbum;
125 album = d->albumRootAlbumHash.value(info.albumRootId);
126
127 if (!album)
128 {
129 qCDebug(DIGIKAM_GENERAL_LOG) << "Did not find album root album in hash";
130 continue;
131 }
132
133 // it has been created from the collection location
134 // with album root id, parentPath "/" and a name, but no album id yet.
135
136 album->m_id = info.id;
137 }
138 else
139 {
140 // last section, no slash
141
142 QString name = info.relativePath.section(QLatin1Char('/'), -1, -1);
143
144 // all but last sections, leading slash, no trailing slash
145
146 QString parentPath = info.relativePath.section(QLatin1Char('/'), 0, -2);
147
148 if (parentPath.isEmpty())
149 {
150 parent = d->albumRootAlbumHash.value(info.albumRootId);
151 }
152 else
153 {
154 parent = d->albumPathHash.value(PAlbumPath(info.albumRootId, parentPath));
155 }
156
157 if (!parent)
158 {
159 qCDebug(DIGIKAM_GENERAL_LOG) << "Could not find parent with url: "
160 << parentPath << " for: "
161 << info.relativePath;
162 continue;
163 }
164
165 // Create the new album
166
167 album = new PAlbum(info.albumRootId, parentPath, name, info.id);
168 }
169
170 album->m_caption = info.caption;
171 album->m_category = info.category;
172 album->m_date = info.date;
173 album->m_iconId = info.iconId;
174
175 insertPAlbum(album, parent);
176
177 if (album->isAlbumRoot())
178 {
179 // Inserting virtual Trash PAlbum for AlbumsRootAlbum using special constructor
180
181 PAlbum* trashAlbum = new PAlbum(album->title(), album->id());
182 insertPAlbum(trashAlbum, album);
183 }
184 }
185
186 if (!topMostOldAlbums.isEmpty() || !newAlbums.isEmpty())
187 {
188 Q_EMIT signalAlbumsUpdated(Album::PHYSICAL);
189 }
190
191 getAlbumItemsCount();
192 }
193
194 void AlbumManager::updateChangedPAlbums()
195 {
196 d->updatePAlbumsTimer->stop();
197
198 // scan db and get a list of all albums
199
200 const auto currAlbums = CoreDbAccess().db()->scanAlbums();
201 const auto changedList = d->changedPAlbums;
202 bool needScanPAlbums = false;
203
204 // Find the AlbumInfo for each id in changedPAlbums
205
206 for (int id : changedList)
207 {
208 for (const AlbumInfo& info : currAlbums)
209 {
210 if (info.id == id)
211 {
212 d->changedPAlbums.remove(info.id);
213
214 PAlbum* album = findPAlbum(info.id);
215
216 if (album)
217 {
218 // Renamed?
219
220 if (info.relativePath != QLatin1String("/"))
221 {
222 // Handle rename of album name
223 // last section, no slash
224
225 QString name = info.relativePath.section(QLatin1Char('/'), -1, -1);
226 QString parentPath = info.relativePath;
227 parentPath.chop(name.length());
228
229 if ((parentPath != album->m_parentPath) || (info.albumRootId != album->albumRootId()))
230 {
231 // Handle actual move operations: trigger ScanPAlbums
232
233 needScanPAlbums = true;
234 removePAlbum(album);
235 break;
236 }
237 else if (name != album->title())
238 {
239 album->setTitle(name);
240 updateAlbumPathHash();
241
242 Q_EMIT signalAlbumRenamed(album);
243 }
244 }
245
246 // Update caption, collection, date
247
248 album->m_caption = info.caption;
249 album->m_category = info.category;
250 album->m_date = info.date;
251
252 // Icon changed?
253
254 if (album->m_iconId != info.iconId)
255 {
256 album->m_iconId = info.iconId;
257
258 Q_EMIT signalAlbumIconChanged(album);
259 }
260 }
261 }
262 }
263 }
264
265 if (needScanPAlbums)
266 {
267 scanPAlbums();
268 }
269 }
270
271 AlbumList AlbumManager::allPAlbums() const
272 {
273 AlbumList list;
274
275 if (d->rootPAlbum)
276 {
277 list.append(d->rootPAlbum);
278 }
279
280 AlbumIterator it(d->rootPAlbum);
281
282 while (it.current())
283 {
284 list.append(*it);
285 ++it;
286 }
287
288 return list;
289 }
290
291 PAlbum* AlbumManager::currentPAlbum() const
292 {
293 /**
294 * Temporary fix, to return multiple items,
295 * iterate and cast each element
296 */
297 if (!d->currentAlbums.isEmpty())
298 {
299 return dynamic_cast<PAlbum*>(d->currentAlbums.first());
300 }
301 else
302 {
303 return nullptr;
304 }
305 }
306
307 PAlbum* AlbumManager::findPAlbum(const QUrl& url) const
308 {
309 CollectionLocation location = CollectionManager::instance()->locationForUrl(url);
310
311 if (location.isNull())
312 {
313 return nullptr;
314 }
315
316 return d->albumPathHash.value(PAlbumPath(location.id(),
317 CollectionManager::instance()->album(location, url)));
318 }
319
320 PAlbum* AlbumManager::findPAlbum(int id) const
321 {
322 if (!d->rootPAlbum)
323 {
324 return nullptr;
325 }
326
327 int gid = d->rootPAlbum->globalID() + id;
328
329 return static_cast<PAlbum*>((d->allAlbumsIdHash.value(gid)));
330 }
331
332
333 PAlbum* AlbumManager::createPAlbum(const QString& albumRootPath, const QString& name,
334 const QString& caption, const QDate& date,
335 const QString& category,
336 QString& errMsg)
337 {
338 CollectionLocation location = CollectionManager::instance()->locationForAlbumRootPath(albumRootPath);
339
340 return createPAlbum(location, name, caption, date, category, errMsg);
341 }
342
343 PAlbum* AlbumManager::createPAlbum(const CollectionLocation& location, const QString& name,
344 const QString& caption, const QDate& date,
345 const QString& category,
346 QString& errMsg)
347 {
348 if (location.isNull() || !location.isAvailable())
349 {
350 errMsg = i18n("The collection location supplied is invalid or currently not available.");
351 return nullptr;
352 }
353
354 PAlbum* const album = d->albumRootAlbumHash.value(location.id());
355
356 if (!album)
357 {
358 errMsg = i18n("No album for collection location: Internal error");
359 return nullptr;
360 }
361
362 return createPAlbum(album, name, caption, date, category, errMsg);
363 }
364
365 PAlbum* AlbumManager::createPAlbum(PAlbum* parent,
366 const QString& name,
367 const QString& caption,
368 const QDate& date,
369 const QString& category,
370 QString& errMsg)
371 {
372 if (!parent)
373 {
374 errMsg = i18n("No parent found for album.");
375 return nullptr;
376 }
377
378 // sanity checks
379
380 if (name.isEmpty())
381 {
382 errMsg = i18n("Album name cannot be empty.");
383 return nullptr;
384 }
385
386 if (name.contains(QLatin1Char('/')))
387 {
388 errMsg = i18n("Album name cannot contain '/'.");
389 return nullptr;
390 }
391
392 if (parent->isRoot())
393 {
394 errMsg = i18n("createPAlbum does not accept the root album as parent.");
395 return nullptr;
396 }
397
398 QString albumPath = parent->isAlbumRoot() ? QString(QLatin1Char('/') + name)
399 : QString(parent->albumPath() + QLatin1Char('/') + name);
400 int albumRootId = parent->albumRootId();
401
402 // first check if we have a sibling album with the same name
403
404 const PAlbum* child = static_cast<PAlbum*>(parent->firstChild());
405
406 while (child)
407 {
408 if ((child->albumRootId() == albumRootId) && (child->albumPath() == albumPath))
409 {
410 errMsg = i18n("An existing album has the same name.");
411 return nullptr;
412 }
413
414 child = static_cast<PAlbum*>(child->next());
415 }
416
417 CoreDbUrl url = parent->databaseUrl();
418 url = url.adjusted(QUrl::StripTrailingSlash);
419 url.setPath(url.path() + QLatin1Char('/') + name);
420 QUrl fileUrl = url.fileUrl();
421
422 bool ret = QDir().mkpath(fileUrl.toLocalFile());
423
424 if (!ret)
425 {
426 errMsg = i18n("Failed to create directory '%1'", fileUrl.toString()); // TODO add tags?
427 return nullptr;
428 }
429
430 ChangingDB changing(d);
431 int id = CoreDbAccess().db()->addAlbum(albumRootId, albumPath, caption, date, category);
432
433 if (id == -1)
434 {
435 errMsg = i18n("Failed to add album to database");
436 return nullptr;
437 }
438
439 QString parentPath;
440
441 if (!parent->isAlbumRoot())
442 {
443 parentPath = parent->albumPath();
444 }
445
446 PAlbum* const album = new PAlbum(albumRootId, parentPath, name, id);
447 album->m_caption = caption;
448 album->m_category = category;
449 album->m_date = date;
450
451 insertPAlbum(album, parent);
452 Q_EMIT signalAlbumsUpdated(Album::PHYSICAL);
453
454 return album;
455 }
456
457 bool AlbumManager::renamePAlbum(PAlbum* album, const QString& newName,
458 QString& errMsg)
459 {
460 if (!album)
461 {
462 errMsg = i18n("No such album");
463 return false;
464 }
465
466 if (album == d->rootPAlbum)
467 {
468 errMsg = i18n("Cannot rename root album");
469 return false;
470 }
471
472 if (album->isAlbumRoot())
473 {
474 errMsg = i18n("Cannot rename album root album");
475 return false;
476 }
477
478 if (newName.contains(QLatin1Char('/')))
479 {
480 errMsg = i18n("Album name cannot contain '/'");
481 return false;
482 }
483
484 // first check if we have another sibling with the same name
485
486 if (hasDirectChildAlbumWithTitle(album->m_parent, newName))
487 {
488 errMsg = i18n("Another album with the same name already exists.\n"
489 "Please choose another name.");
490 return false;
491 }
492
493 d->albumWatch->removeWatchedPAlbums(album);
494
495 // We use a private shortcut around collection scanner noticing our changes,
496 // we rename them directly. Faster.
497
498 ScanController::instance()->suspendCollectionScan();
499
500 QDir dir(album->albumRootPath() + album->m_parentPath);
501 bool ret = dir.rename(album->title(), newName);
502
503 if (!ret)
504 {
505 ScanController::instance()->resumeCollectionScan();
506
507 errMsg = i18n("Failed to rename Album");
508 return false;
509 }
510
511 QString oldAlbumPath = album->albumPath();
512 album->setTitle(newName);
513 album->m_path = newName;
514 QString newAlbumPath = album->albumPath();
515
516 // now rename the album and subalbums in the database
517 {
518 CoreDbAccess access;
519 ChangingDB changing(d);
520 access.db()->renameAlbum(album->id(), album->albumRootId(), album->albumPath());
521
522 PAlbum* subAlbum = nullptr;
523 AlbumIterator it(album);
524
525 while ((subAlbum = static_cast<PAlbum*>(it.current())) != nullptr)
526 {
527 subAlbum->m_parentPath = newAlbumPath + subAlbum->m_parentPath.mid(oldAlbumPath.length());
528 access.db()->renameAlbum(subAlbum->id(), album->albumRootId(), subAlbum->albumPath());
529
530 Q_EMIT signalAlbumNewPath(subAlbum);
531
532 ++it;
533 }
534 }
535
536 updateAlbumPathHash();
537
538 Q_EMIT signalAlbumRenamed(album);
539
540 ScanController::instance()->resumeCollectionScan();
541
542 return true;
543 }
544
545 bool AlbumManager::updatePAlbumIcon(PAlbum* album, qlonglong iconID, QString& errMsg)
546 {
547 if (!album)
548 {
549 errMsg = i18n("No such album");
550 return false;
551 }
552
553 if (album == d->rootPAlbum)
554 {
555 errMsg = i18n("Cannot edit root album");
556 return false;
557 }
558
559 {
560 CoreDbAccess access;
561 ChangingDB changing(d);
562 access.db()->setAlbumIcon(album->id(), iconID);
563 album->m_iconId = iconID;
564 }
565
566 Q_EMIT signalAlbumIconChanged(album);
567
568 return true;
569 }
570
571 QHash<int, int> AlbumManager::getPAlbumsCount() const
572 {
573 return d->pAlbumsCount;
574 }
575
576 void AlbumManager::insertPAlbum(PAlbum* album, PAlbum* parent)
577 {
578 if (!album)
579 {
580 return;
581 }
582
583 Q_EMIT signalAlbumAboutToBeAdded(album, parent, parent ? parent->lastChild() : nullptr);
584
585 if (parent)
586 {
587 album->setParent(parent);
588 }
589
590 d->albumPathHash[PAlbumPath(album)] = album;
591 d->allAlbumsIdHash[album->globalID()] = album;
592
593 Q_EMIT signalAlbumAdded(album);
594 }
595
596 void AlbumManager::removePAlbum(PAlbum* album)
597 {
598 if (!album)
599 {
600 return;
601 }
602
603 // remove all children of this album
604
605 Album* child = album->firstChild();
606 PAlbum* toBeRemoved = nullptr;
607
608 while (child)
609 {
610 Album* const next = child->next();
611 toBeRemoved = dynamic_cast<PAlbum*>(child);
612
613 if (toBeRemoved)
614 {
615 removePAlbum(toBeRemoved);
616 toBeRemoved = nullptr;
617 }
618
619 child = next;
620 }
621
622 Q_EMIT signalAlbumAboutToBeDeleted(album);
623
624 d->albumPathHash.remove(PAlbumPath(album));
625 d->allAlbumsIdHash.remove(album->globalID());
626
627 CoreDbUrl url = album->databaseUrl();
628
629 if (!d->currentAlbums.isEmpty())
630 {
631 if (album == d->currentAlbums.first())
632 {
633 d->currentAlbums.clear();
634
635 Q_EMIT signalAlbumCurrentChanged(d->currentAlbums);
636 }
637 }
638
639 if (album->isAlbumRoot())
640 {
641 d->albumRootAlbumHash.remove(album->albumRootId());
642 }
643
644 Q_EMIT signalAlbumDeleted(album);
645
646 album->prepareForDeletion();
647
648 Q_EMIT signalAlbumHasBeenDeleted(album);
649
650 delete album;
651 }
652
653 void AlbumManager::removeWatchedPAlbums(const PAlbum* const album)
654 {
655 d->albumWatch->removeWatchedPAlbums(album);
656 }
657
658 } // namespace Digikam