Debian Codegraph @

core/libs/album/engine/album.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 : digiKam album types
8 *
9 * SPDX-FileCopyrightText: 2004-2005 by Renchi Raju <renchi dot raju at gmail dot com>
10 * SPDX-FileCopyrightText: 2006-2026 by Gilles Caulier <caulier dot gilles at gmail dot com>
11 * SPDX-FileCopyrightText: 2014-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 "album.h"
18
19 // KDE includes
20
21 #include <klocalizedstring.h>
22
23 // Local includes
24
25 #include "digikam_debug.h"
26 #include "dtrash.h"
27 #include "coredb.h"
28 #include "coredburl.h"
29 #include "coredbaccess.h"
30 #include "albummanager.h"
31 #include "collectionmanager.h"
32 #include "tagscache.h"
33
34 namespace Digikam
35 {
36
37 Album::Album(Album::Type type, int id, bool root)
38 : m_root (root),
39 m_id (id),
40 m_type (type)
41 {
42 }
43
44 Album::~Album()
45 {
46 prepareForDeletion();
47
48 AlbumManager::instance()->notifyAlbumDeletion(this);
49 }
50
51 void Album::setParent(Album* const _parent)
52 {
53 if (_parent)
54 {
55 m_parent = _parent;
56 m_parent->insertChild(this);
57 }
58 }
59
60 Album* Album::parent() const
61 {
62 return m_parent;
63 }
64
65 Album* Album::firstChild() const
66 {
67 QReadLocker locker(&m_cacheLock);
68
69 if (m_childCache.isEmpty())
70 {
71 return nullptr;
72 }
73
74 return m_childCache.constFirst();
75 }
76
77 Album* Album::lastChild() const
78 {
79 QReadLocker locker(&m_cacheLock);
80
81 if (m_childCache.isEmpty())
82 {
83 return nullptr;
84 }
85
86 return m_childCache.constLast();
87 }
88
89 Album* Album::next() const
90 {
91 if (!m_parent)
92 {
93 return nullptr;
94 }
95
96 QReadLocker parentLocker(&m_parent->m_cacheLock);
97
98 const int row = m_row + 1;
99
100 if (row >= m_parent->m_childCache.size())
101 {
102 return nullptr;
103 }
104
105 return m_parent->m_childCache.at(row);
106 }
107
108 Album* Album::prev() const
109 {
110 if (!m_parent)
111 {
112 return nullptr;
113 }
114
115 QReadLocker parentLocker(&m_parent->m_cacheLock);
116
117 const int row = m_row - 1;
118
119 if ((row < 0) || (row >= m_parent->m_childCache.size()))
120 {
121 return nullptr;
122 }
123
124 return m_parent->m_childCache.at(row);
125 }
126
127 Album* Album::childAtRow(int row) const
128 {
129 QReadLocker locker(&m_cacheLock);
130
131 if ((row < 0) || (row >= m_childCache.size()))
132 {
133 return nullptr;
134 }
135
136 return m_childCache.at(row);
137 }
138
139 AlbumList Album::childAlbums(bool recursive)
140 {
141 AlbumList childList;
142
143 QReadLocker locker(&m_cacheLock);
144
145 QList<Album*>::const_iterator it = m_childCache.constBegin();
146
147 while (it != m_childCache.constEnd())
148 {
149 childList << (*it);
150
151 if (recursive)
152 {
153 childList << (*it)->childAlbums(recursive);
154 }
155
156 ++it;
157 }
158
159 return childList;
160 }
161
162 QList<int> Album::childAlbumIds(bool recursive)
163 {
164 QList<int> ids;
165
166 AlbumList childList = childAlbums(recursive);
167
168 QListIterator<Album*> it(childList);
169
170 while (it.hasNext())
171 {
172 ids << it.next()->id();
173 }
174
175 return ids;
176 }
177
178 // cppcheck-suppress constParameterPointer
179 void Album::insertChild(Album* const child)
180 {
181 if (!child)
182 {
183 return;
184 }
185
186 QWriteLocker locker(&m_cacheLock);
187
188 m_childCache.append(child);
189 child->m_row = m_childCache.size() - 1;
190 }
191
192 void Album::removeChild(Album* const child)
193 {
194 if (!child)
195 {
196 return;
197 }
198
199 QWriteLocker locker(&m_cacheLock);
200
201 for (auto it = m_childCache.begin(); it != m_childCache.end(); ++it)
202 {
203 if ((*it) == child)
204 {
205 (*it)->m_row = 0;
206 it = m_childCache.erase(it);
207
208 for ( ; it != m_childCache.end(); ++it)
209 {
210 (*it)->m_row -= 1;
211 }
212
213 break;
214 }
215 }
216 }
217
218 void Album::clear()
219 {
220 QWriteLocker locker(&m_cacheLock);
221
222 while (!m_childCache.isEmpty())
223 {
224 Album* const child = m_childCache.takeLast();
225 child->m_parent = nullptr;
226
227 delete child;
228 }
229 }
230
231 int Album::globalID() const
232 {
233 return globalID(m_type, m_id);
234 }
235
236 int Album::globalID(Type _type, int _id)
237 {
238 switch (_type)
239 {
240 // Use the upper bits to create unique ids.
241
242 case (PHYSICAL):
243 {
244 return _id;
245 }
246
247 case (TAG):
248 {
249 return (_id | (1 << 27));
250 }
251
252 case (DATE):
253 {
254 return (_id | (1 << 28));
255 }
256
257 case (SEARCH):
258 {
259 return (_id | (1 << 29));
260 }
261
262 case (FACE):
263 {
264 return (_id | (1 << 30));
265 }
266
267 default:
268 {
269 qCDebug(DIGIKAM_GENERAL_LOG) << "Unknown album type";
270
271 return -1;
272 }
273 }
274 }
275
276 int Album::id() const
277 {
278 return m_id;
279 }
280
281 int Album::childCount() const
282 {
283 QReadLocker locker(&m_cacheLock);
284
285 return m_childCache.size();
286 }
287
288 int Album::rowFromAlbum() const
289 {
290 return m_row;
291 }
292
293 void Album::setTitle(const QString& _title)
294 {
295 m_title = _title;
296 }
297
298 QString Album::title() const
299 {
300 return m_title;
301 }
302
303 Album::Type Album::type() const
304 {
305 return m_type;
306 }
307
308 // cppcheck-suppress constParameterPointer
309 void Album::setExtraData(const void* const key, void* const value)
310 {
311 m_extraMap.insert(key, value);
312 }
313
314 void Album::removeExtraData(const void* const key)
315 {
316 m_extraMap.remove(key);
317 }
318
319 void* Album::extraData(const void* const key) const
320 {
321 return m_extraMap.value(key, nullptr);
322 }
323
324 bool Album::isRoot() const
325 {
326 return m_root;
327 }
328
329 bool Album::isAncestorOf(const Album* const album) const
330 {
331 bool val = false;
332 const Album* a = album;
333
334 while (a && !a->isRoot())
335 {
336 if (a == this)
337 {
338 val = true;
339 break;
340 }
341
342 a = a->parent();
343 }
344
345 return val;
346 }
347
348 bool Album::isUsedByLabelsTree() const
349 {
350 return m_usedByLabelsTree;
351 }
352
353 bool Album::isTrashAlbum() const
354 {
355 if ((m_id < -1) && (m_type == PHYSICAL))
356 {
357 return true;
358 }
359
360 return false;
361 }
362
363 void Album::setUsedByLabelsTree(bool isUsed)
364 {
365 m_usedByLabelsTree = isUsed;
366 }
367
368 void Album::prepareForDeletion()
369 {
370 if (m_parent)
371 {
372 m_parent->removeChild(this);
373 }
374
375 clear();
376 }
377
378 // ------------------------------------------------------------------------------
379
380 int PAlbum::m_uniqueTrashId = -2;
381
382 PAlbum::PAlbum(const QString& title)
383 : Album(Album::PHYSICAL, 0, true)
384 {
385 setTitle(title);
386 m_path.clear();
387 }
388
389 PAlbum::PAlbum(int albumRoot, const QString& label)
390 : Album (Album::PHYSICAL, -1, false),
391 m_isAlbumRootAlbum(true),
392 m_albumRootId (albumRoot)
393 {
394 // set the id to -1 (line above). AlbumManager may change that later.
395
396 setTitle(label);
397 m_path.clear();
398 }
399
400 PAlbum::PAlbum(int albumRoot, const QString& parentPath, const QString& title, int id)
401 : Album (Album::PHYSICAL, id, false),
402 m_albumRootId (albumRoot),
403 m_path (title),
404 m_parentPath (parentPath + QLatin1Char('/')),
405 m_date (QDate::currentDate())
406 {
407 // If path is /holidays/2007, title is only "2007", path is "/holidays"
408
409 setTitle(title);
410 }
411
412 PAlbum::PAlbum(const QString& parentPath, int albumRoot)
413 : Album (Album::PHYSICAL, m_uniqueTrashId--, false),
414 m_albumRootId (albumRoot),
415 m_path (DTrash::TRASH_FOLDER),
416 m_parentPath (parentPath + QLatin1Char('/'))
417 {
418 setTitle(i18n("Trash"));
419 }
420
421 bool PAlbum::isAlbumRoot() const
422 {
423 return m_isAlbumRootAlbum;
424 }
425
426 void PAlbum::setCaption(const QString& _caption)
427 {
428 m_caption = _caption;
429
430 CoreDbAccess access;
431 access.db()->setAlbumCaption(id(), m_caption);
432 }
433
434 void PAlbum::setCategory(const QString& _category)
435 {
436 m_category = _category;
437
438 CoreDbAccess access;
439 access.db()->setAlbumCategory(id(), m_category);
440 }
441
442 void PAlbum::setDate(const QDate& _date)
443 {
444 m_date = _date;
445
446 CoreDbAccess access;
447 access.db()->setAlbumDate(id(), m_date);
448 }
449
450 QString PAlbum::albumRootPath() const
451 {
452 return CollectionManager::instance()->albumRootPath(m_albumRootId);
453 }
454
455 QString PAlbum::albumRootLabel() const
456 {
457 return CollectionManager::instance()->albumRootLabel(m_albumRootId);
458 }
459
460 int PAlbum::albumRootId() const
461 {
462 return m_albumRootId;
463 }
464
465 QString PAlbum::caption() const
466 {
467 return m_caption;
468 }
469
470 QString PAlbum::category() const
471 {
472 return m_category;
473 }
474
475 QDate PAlbum::date() const
476 {
477 return m_date;
478 }
479
480 QString PAlbum::albumPath() const
481 {
482 return (m_parentPath + m_path);
483 }
484
485 CoreDbUrl PAlbum::databaseUrl() const
486 {
487 return CoreDbUrl::fromAlbumAndName(QString(), albumPath(),
488 QUrl::fromLocalFile(albumRootPath()), m_albumRootId);
489 }
490
491 QString PAlbum::prettyUrl() const
492 {
493 QString u = i18n("Albums") + QLatin1Char('/') +
494 albumRootLabel() +
495 albumPath();
496
497 if (u.endsWith(QLatin1Char('/')))
498 {
499 u.chop(1);
500 }
501
502 return u;
503 }
504
505 qlonglong PAlbum::iconId() const
506 {
507 return m_iconId;
508 }
509
510 QUrl PAlbum::fileUrl() const
511 {
512 return databaseUrl().fileUrl();
513 }
514
515 QString PAlbum::folderPath() const
516 {
517 return databaseUrl().fileUrl().toLocalFile();
518 }
519
520 // --------------------------------------------------------------------------
521
522 TAlbum::TAlbum(const QString& title, int id, bool root)
523 : Album(Album::TAG, id, root)
524 {
525 setTitle(title);
526 }
527
528 QString TAlbum::tagPath(bool leadingSlash) const
529 {
530 if (isRoot())
531 {
532 return (leadingSlash ? QLatin1String("/") : QLatin1String(""));
533 }
534
535 QString u;
536
537 if (parent())
538 {
539 u = (static_cast<TAlbum*>(parent()))->tagPath(leadingSlash);
540
541 if (!parent()->isRoot())
542 {
543 u += QLatin1Char('/');
544 }
545 }
546
547 u += title();
548
549 return u;
550 }
551
552 QString TAlbum::standardIconName() const
553 {
554 return (hasProperty(TagPropertyName::person()) ? QLatin1String("smiley")
555 : QLatin1String("tag"));
556 }
557
558 QString TAlbum::prettyUrl() const
559 {
560 return (i18n("Tags") + tagPath(true));
561 }
562
563 CoreDbUrl TAlbum::databaseUrl() const
564 {
565 return CoreDbUrl::fromTagIds(tagIDs());
566 }
567
568 QList<int> TAlbum::tagIDs() const
569 {
570 if (isRoot())
571 {
572 return QList<int>();
573 }
574 else if (parent())
575 {
576 return (static_cast<TAlbum*>(parent())->tagIDs() << id());
577 }
578 else
579 {
580 return (QList<int>() << id());
581 }
582 }
583
584 QString TAlbum::icon() const
585 {
586 return m_icon;
587 }
588
589 bool TAlbum::isInternalTag() const
590 {
591 return TagsCache::instance()->isInternalTag(id());
592 }
593
594 qlonglong TAlbum::iconId() const
595 {
596 return m_iconId;
597 }
598
599 bool TAlbum::hasProperty(const QString& key) const
600 {
601 return TagsCache::instance()->hasProperty(id(), key);
602 }
603
604 QString TAlbum::property(const QString& key) const
605 {
606 return TagsCache::instance()->propertyValue(id(), key);
607 }
608
609 QMap<QString, QString> TAlbum::properties() const
610 {
611 return TagsCache::instance()->properties(id());
612 }
613
614 // --------------------------------------------------------------------------
615
616 int DAlbum::m_uniqueID = 0;
617
618 DAlbum::DAlbum(const QDate& date, bool root, Range range)
619 : Album (Album::DATE, root ? 0 : ++m_uniqueID, root),
620 m_date (date),
621 m_range(range)
622 {
623 // Set the name of the date album
624
625 QString dateTitle;
626
627 if (m_range == Month)
628 {
629 dateTitle = m_date.toString(QLatin1String("MMMM yyyy"));
630 }
631 else
632 {
633 dateTitle = m_date.toString(QLatin1String("yyyy"));
634 }
635
636 setTitle(dateTitle);
637 }
638
639 QDate DAlbum::date() const
640 {
641 return m_date;
642 }
643
644 DAlbum::Range DAlbum::range() const
645 {
646 return m_range;
647 }
648
649 CoreDbUrl DAlbum::databaseUrl() const
650 {
651 if (m_range == Month)
652 {
653 return CoreDbUrl::fromDateForMonth(m_date);
654 }
655
656 return CoreDbUrl::fromDateForYear(m_date);
657 }
658
659 // --------------------------------------------------------------------------
660
661 SAlbum::SAlbum(const QString& title, int id, bool root)
662 : Album(Album::SEARCH, id, root)
663 {
664 setTitle(title);
665 }
666
667 void SAlbum::setSearch(DatabaseSearch::Type _type, const QString& _query)
668 {
669 m_searchType = _type;
670 m_query = _query;
671 }
672
673 CoreDbUrl SAlbum::databaseUrl() const
674 {
675 return CoreDbUrl::searchUrl(id());
676 }
677
678 QString SAlbum::query() const
679 {
680 return m_query;
681 }
682
683 DatabaseSearch::Type SAlbum::searchType() const
684 {
685 return m_searchType;
686 }
687
688 bool SAlbum::isNormalSearch() const
689 {
690 switch (m_searchType)
691 {
692 case DatabaseSearch::KeywordSearch:
693 case DatabaseSearch::AdvancedSearch:
694 case DatabaseSearch::LegacyUrlSearch:
695 {
696 return true;
697 }
698
699 default:
700 {
701 return false;
702 }
703 }
704 }
705
706 bool SAlbum::isAdvancedSearch() const
707 {
708 return (m_searchType == DatabaseSearch::AdvancedSearch);
709 }
710
711 bool SAlbum::isKeywordSearch() const
712 {
713 return (m_searchType == DatabaseSearch::KeywordSearch);
714 }
715
716 bool SAlbum::isTimelineSearch() const
717 {
718 return (m_searchType == DatabaseSearch::TimeLineSearch);
719 }
720
721 bool SAlbum::isHaarSearch() const
722 {
723 return (m_searchType == DatabaseSearch::HaarSearch);
724 }
725
726 bool SAlbum::isMapSearch() const
727 {
728 return (m_searchType == DatabaseSearch::MapSearch);
729 }
730
731 bool SAlbum::isDuplicatesSearch() const
732 {
733 return (m_searchType == DatabaseSearch::DuplicatesSearch);
734 }
735
736 bool SAlbum::isTemporarySearch() const
737 {
738 if (isHaarSearch())
739 {
740 return (
741 (title() == getTemporaryHaarTitle(DatabaseSearch::HaarImageSearch))) ||
742 (title() == getTemporaryHaarTitle(DatabaseSearch::HaarSketchSearch)
743 );
744 }
745
746 return (title() == getTemporaryTitle(m_searchType));
747 }
748
749 QString SAlbum::displayTitle() const
750 {
751 if (isTemporarySearch())
752 {
753 switch (m_searchType)
754 {
755 case DatabaseSearch::TimeLineSearch:
756 {
757 return i18n("Current Timeline Search");
758 }
759
760 case DatabaseSearch::HaarSearch:
761 {
762 if (title() == getTemporaryHaarTitle(DatabaseSearch::HaarImageSearch))
763 {
764 return i18n("Current Fuzzy Image Search");
765 }
766 else if (title() == getTemporaryHaarTitle(DatabaseSearch::HaarSketchSearch))
767 {
768 return i18n("Current Fuzzy Sketch Search");
769 }
770
771 break;
772 }
773
774 case DatabaseSearch::MapSearch:
775 {
776 return i18n("Current Map Search");
777 }
778
779 case DatabaseSearch::KeywordSearch:
780 case DatabaseSearch::AdvancedSearch:
781 case DatabaseSearch::LegacyUrlSearch:
782 {
783 return i18n("Current Search");
784 }
785
786 case DatabaseSearch::DuplicatesSearch:
787 {
788 return i18n("Current Duplicates Search");
789 }
790
791 case DatabaseSearch::UndefinedType:
792 {
793 break;
794 }
795 }
796 }
797
798 return title();
799 }
800
801 QString SAlbum::getTemporaryTitle(DatabaseSearch::Type type, DatabaseSearch::HaarSearchType haarType)
802 {
803 switch (type)
804 {
805 case DatabaseSearch::TimeLineSearch:
806 {
807 return QLatin1String("_Current_Time_Line_Search_");
808 }
809
810 case DatabaseSearch::HaarSearch:
811 {
812 return getTemporaryHaarTitle(haarType);
813 }
814
815 case DatabaseSearch::MapSearch:
816 {
817 return QLatin1String("_Current_Map_Search_");
818 }
819
820 case DatabaseSearch::KeywordSearch:
821 case DatabaseSearch::AdvancedSearch:
822 case DatabaseSearch::LegacyUrlSearch:
823 {
824 return QLatin1String("_Current_Search_View_Search_");
825 }
826
827 case DatabaseSearch::DuplicatesSearch:
828 {
829 return QLatin1String("_Current_Duplicates_Search_");
830 }
831
832 default:
833 {
834 qCDebug(DIGIKAM_GENERAL_LOG) << "Untreated temporary search type " << type;
835
836 return QLatin1String("_Current_Unknown_Search_");
837 }
838 }
839 }
840
841 QString SAlbum::getTemporaryHaarTitle(DatabaseSearch::HaarSearchType haarType)
842 {
843 switch (haarType)
844 {
845 case DatabaseSearch::HaarImageSearch:
846 {
847 return QLatin1String("_Current_Fuzzy_Image_Search_");
848 }
849
850 case DatabaseSearch::HaarSketchSearch:
851 {
852 return QLatin1String("_Current_Fuzzy_Sketch_Search_");
853 }
854
855 default:
856 {
857 qCDebug(DIGIKAM_GENERAL_LOG) << "Untreated temporary haar search type " << haarType;
858
859 return QLatin1String("_Current_Unknown_Haar_Search_");
860 }
861 }
862 }
863
864 // --------------------------------------------------------------------------
865
866 AlbumIterator::AlbumIterator(Album* const album)
867 : m_current(album ? album->firstChild() : nullptr),
868 m_root (album)
869 {
870 }
871
872 AlbumIterator& AlbumIterator::operator++()
873 {
874 if (!m_current)
875 {
876 return *this;
877 }
878
879 Album* album = m_current->firstChild();
880
881 if (!album)
882 {
883 while ((album = m_current->next()) == nullptr)
884 {
885 m_current = m_current->parent();
886
887 if (m_current == m_root)
888 {
889 // we have reached the root.
890 // that means no more children
891
892 m_current = nullptr;
893 break;
894 }
895
896 if (m_current == nullptr)
897 {
898 break;
899 }
900 }
901 }
902
903 m_current = album;
904
905 return *this;
906 }
907
908 Album* AlbumIterator::operator*()
909 {
910 return m_current;
911 }
912
913 Album* AlbumIterator::current() const
914 {
915 return m_current;
916 }
917
918 } // namespace Digikam