digiKam
coredb.h
Go to the documentation of this file.
1 /* ============================================================
2  *
3  * This file is a part of digiKam project
4  * https://www.digikam.org
5  *
6  * Date : 2004-06-18
7  * Description : Core database interface.
8  *
9  * Copyright (C) 2004-2005 by Renchi Raju <renchi dot raju at gmail dot com>
10  * Copyright (C) 2006-2022 by Gilles Caulier <caulier dot gilles at gmail dot com>
11  * Copyright (C) 2006-2012 by Marcel Wiesweg <marcel dot wiesweg at gmx dot de>
12  * Copyright (C) 2012 by Andi Clemens <andi dot clemens at gmail dot com>
13  *
14  * This program is free software; you can redistribute it
15  * and/or modify it under the terms of the GNU General
16  * Public License as published by the Free Software Foundation;
17  * either version 2, or (at your option)
18  * any later version.
19  *
20  * This program is distributed in the hope that it will be useful,
21  * but WITHOUT ANY WARRANTY; without even the implied warranty of
22  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23  * GNU General Public License for more details.
24  *
25  * ============================================================ */
26 
27 #ifndef DIGIKAM_CORE_DB_H
28 #define DIGIKAM_CORE_DB_H
29 
30 // Qt includes
31 
32 #include <QStringList>
33 #include <QDateTime>
34 #include <QVariant>
35 #include <QString>
36 #include <QList>
37 #include <QPair>
38 #include <QUuid>
39 #include <QMap>
40 
41 // Local includes
42 
43 #include "coredbalbuminfo.h"
44 #include "coredbfields.h"
45 #include "coredbaccess.h"
46 #include "coredbconstants.h"
47 #include "digikam_export.h"
48 #include "dbenginesqlquery.h"
49 #include "album.h"
50 
51 namespace Digikam
52 {
53 
54 class CoreDbBackend;
55 
56 class DIGIKAM_DATABASE_EXPORT CoreDB
57 {
58 public:
59 
67  void setSetting(const QString& keyword, const QString& value);
68 
76  QString getSetting(const QString& keyword) const;
77 
84  void getFilterSettings(QStringList* imageFilter, QStringList* videoFilter, QStringList* audioFilter);
85 
90  void getUserFilterSettings(QString* imageFilterString, QString* videoFilterString, QString* audioFilterString);
91 
95  void setFilterSettings(const QStringList& imageFilter, const QStringList& videoFilter, const QStringList& audioFilter);
96 
102  void setUserFilterSettings(const QStringList& imageFilter, const QStringList& videoFilter, const QStringList& audioFilter);
103 
104  void setIgnoreDirectoryFilterSettings(const QStringList& ignoreDirectoryFilter);
105 
106  void setUserIgnoreDirectoryFilterSettings(const QStringList& ignoreDirectoryFilters);
107 
108  void getUserIgnoreDirectoryFilterSettings(QString* ignoreDirectoryFilterString);
109 
110  void getIgnoreDirectoryFilterSettings(QStringList* ignoreDirectoryFilter);
111 
116  QUuid databaseUuid();
117 
122  int getUniqueHashVersion() const;
123 
124  void setUniqueHashVersion(int version);
125 
126  bool isUniqueHashV2() const;
127 
128  // ----------- AlbumRoot operations -----------
129 
134  QList<AlbumRootInfo> getAlbumRoots() const;
135 
143  int addAlbumRoot(AlbumRoot::Type type, const QString& identifier,
144  const QString& specificPath, const QString& label) const;
145 
150  void deleteAlbumRoot(int rootId);
151 
157  void migrateAlbumRoot(int rootId, const QString& identifier);
158 
164  void setAlbumRootLabel(int rootId, const QString& newLabel);
165 
171  void setAlbumRootPath(int rootId, const QString& newPath);
172 
176  void changeAlbumRootType(int rootId, AlbumRoot::Type newType);
177 
178  // ----------- Album Listing operations -----------
183  AlbumInfo::List scanAlbums() const;
184 
189  TagInfo::List scanTags() const;
190  TagInfo getTagInfo(int tagId) const;
191 
196  SearchInfo::List scanSearches() const;
197 
202  QList<AlbumShortInfo> getAlbumShortInfos() const;
203 
208  QList<TagShortInfo> getTagShortInfos() const;
209 
210  // ----------- Operations on PAlbums -----------
211 
221  int addAlbum(int albumRootId, const QString& relativePath,
222  const QString& caption,
223  const QDate& date, const QString& collection) const;
224 
234  int getAlbumForPath(int albumRootId, const QString& relativePath, bool create = true) const;
235 
242  QList<int> getAlbumAndSubalbumsForPath(int albumRootId, const QString& relativePath) const;
243 
248  QList<int> getAlbumsOnAlbumRoot(int albumRootId) const;
249 
255  void deleteAlbum(int albumID);
256 
260  void makeStaleAlbum(int albumID);
261 
266  void deleteStaleAlbums();
267 
273  bool copyAlbumProperties(int srcAlbumID, int dstAlbumID) const;
274 
278  void renameAlbum(int albumID, int newAlbumRootId, const QString& newRelativePath);
279 
285  void setAlbumCaption(int albumID, const QString& caption);
286 
292  void setAlbumCategory(int albumID, const QString& category);
293 
299  void setAlbumDate(int albumID, const QDate& date);
300 
306  void setAlbumModificationDate(int albumID, const QDateTime& modificationDate);
307 
313  void setAlbumIcon(int albumID, qlonglong iconID);
314 
320  int getAlbumRootId(int albumID) const;
321 
328  QString getAlbumRelativePath(int albumID) const;
329 
335  QDate getAlbumLowestDate(int albumID) const;
336 
342  QDate getAlbumHighestDate(int albumID) const;
343 
349  QDate getAlbumAverageDate(int albumID) const;
350 
356  QPair<int, int> getNumberOfAllItemsAndAlbums(int albumID) const;
357 
362  int getNumberOfItemsInAlbum(int albumID) const;
363 
368  QDateTime getAlbumModificationDate(int albumID) const;
369 
374  QMap<QString, QDateTime> getAlbumModificationMap(int albumRootId) const;
375 
380  QMap<int, int> getNumberOfImagesInAlbums() const;
381 
382  // ----------- Operations on TAlbums -----------
383 
394  int addTag(int parentTagID, const QString& name,
395  const QString& iconKDE, qlonglong iconID) const;
396 
401  // TODO move to other place (AlbumManager)
402  QList<int> getRecentlyAssignedTags() const;
403 
409  void deleteTag(int tagID);
410 
416  void setTagName(int tagID, const QString& name);
417 
426  void setTagIcon(int tagID, const QString& iconKDE, qlonglong iconID);
427 
434  void setTagParentID(int tagID, int newParentTagID);
435 
439  QList<TagProperty> getTagProperties() const;
440 
444  QList<TagProperty> getTagProperties(int tagID) const;
445 
449  QList<TagProperty> getTagProperties(const QString& property) const;
450 
456  void addTagProperty(int tagId, const QString& property, const QString& value);
457  void addTagProperty(const TagProperty& property);
458 
465  void removeTagProperties(int tagId, const QString& property = QString(), const QString& value = QString());
466 
471  QList<int> getTagsWithProperty(const QString& property) const;
472 
473  // ----------- Operations on SAlbums -----------
474 
482  int addSearch(DatabaseSearch::Type type, const QString& name, const QString& query) const;
483 
490  void updateSearch(int searchID, DatabaseSearch::Type type,
491  const QString& name, const QString& query);
492 
497  void deleteSearch(int searchID);
498 
502  void deleteSearches(DatabaseSearch::Type type);
503 
507  SearchInfo getSearchInfo(int searchId) const;
508 
512  QString getSearchQuery(int searchId) const;
513 
514  // ----------- Adding and deleting Items -----------
519  qlonglong addItem(int albumID, const QString& name,
520  DatabaseItem::Status status,
521  DatabaseItem::Category category,
522  const QDateTime& modificationDate,
523  qlonglong fileSize,
524  const QString& uniqueHash) const;
525 
531  void deleteItem(int albumID, const QString& file);
532 
538  void deleteItem(qlonglong imageId);
539 
544  void deleteObsoleteItem(qlonglong imageId);
545 
556  void removeItemsFromAlbum(int albumID, const QList<qlonglong>& ids_forInformation = QList<qlonglong>());
557 
566  void removeItems(QList<qlonglong> itemIDs, const QList<int>& albumIDs = QList<int>());
567 
576  void removeItemsPermanently(QList<qlonglong> itemIDs, const QList<int>& albumIDs = QList<int>());
577 
582  void deleteRemovedItems();
583 
584  // ----------- Finding items -----------
585 
592  qlonglong getImageId(int albumID, const QString& name) const;
593 
601  QList<qlonglong> getImageIds(int albumID, const QString& name,
602  DatabaseItem::Status status) const;
603 
609  QList<qlonglong> getImageIds(DatabaseItem::Status status) const;
610 
617  QList<qlonglong> getImageIds(DatabaseItem::Status status,
618  DatabaseItem::Category category) const;
619 
630  qlonglong findImageId(int albumID, const QString& name,
631  DatabaseItem::Status status,
632  DatabaseItem::Category category,
633  qlonglong fileSize,
634  const QString& uniqueHash) const;
635 
637  {
642  ByItemRating
643  };
644 
653  QStringList getItemNamesInAlbum(int albumID, bool recursive = false) const;
654 
658  QList<qlonglong> getAllItems() const;
659 
665  QHash<qlonglong, QPair<int, int> > getAllItemsWithAlbum() const;
666 
674  qlonglong getItemFromAlbum(int albumID, const QString& fileName) const;
675 
680  QList<ItemScanInfo> getItemScanInfos(int albumID) const;
681 
690  QStringList getItemURLsInAlbum(int albumID, ItemSortOrder order = NoItemSorting) const;
691 
697  QList<qlonglong> getItemIDsInAlbum(int albumID) const;
698 
706  QMap<qlonglong, QString> getItemIDsAndURLsInAlbum(int albumID) const;
707 
716  QStringList getItemURLsInTag(int tagID, bool recursive = false) const;
717 
724  QList<qlonglong> getItemIDsInTag(int tagID, bool recursive = false) const;
725 
729  QVariantList getAllCreationDates() const;
730 
734  QList<qlonglong> getObsoleteItemIds() const;
735 
736  // ----------- Item properties -----------
737 
743  int getItemAlbum(qlonglong imageID) const;
744 
750  QString getItemName(qlonglong imageID) const;
751 
755  ItemShortInfo getItemShortInfo(qlonglong imageID) const;
756 
760  ItemShortInfo getItemShortInfo(int albumRootId, const QString& relativePath,
761  const QString& name) const;
762 
766  ItemScanInfo getItemScanInfo(qlonglong imageID) const;
767 
773  void updateItem(qlonglong imageID,
774  DatabaseItem::Category category,
775  const QDateTime& modificationDate,
776  qlonglong fileSize,
777  const QString& uniqueHash);
778 
783  void setItemStatus(qlonglong imageID, DatabaseItem::Status status);
784 
790  void setItemAlbum(qlonglong imageID, qlonglong albumId);
791 
795  void setItemManualOrder(qlonglong imageID, qlonglong value);
796 
800  void setItemModificationDate(qlonglong imageID, const QDateTime& modificationDate);
801 
806  void renameItem(qlonglong imageID, const QString& newName);
807 
820  QVariantList getImagesFields(qlonglong imageID,
821  DatabaseFields::Images imagesFields) const;
822 
840  void addItemInformation(qlonglong imageID, const QVariantList& infos,
841  DatabaseFields::ItemInformation fields = DatabaseFields::ItemInformationAll);
842 
849  void changeItemInformation(qlonglong imageID, const QVariantList& infos,
850  DatabaseFields::ItemInformation fields = DatabaseFields::ItemInformationAll);
851 
855  QVariantList getItemInformation(qlonglong imageID,
856  DatabaseFields::ItemInformation infoFields
858 
883  void addImageMetadata(qlonglong imageID, const QVariantList& infos,
884  DatabaseFields::ImageMetadata fields = DatabaseFields::ImageMetadataAll);
885 
891  void changeImageMetadata(qlonglong imageID, const QVariantList& infos,
892  DatabaseFields::ImageMetadata fields = DatabaseFields::ImageMetadataAll);
893 
897  QVariantList getImageMetadata(qlonglong imageID,
898  DatabaseFields::ImageMetadata metadataFields = DatabaseFields::ImageMetadataAll) const;
899 
914  void addVideoMetadata(qlonglong imageID, const QVariantList& infos,
915  DatabaseFields::VideoMetadata fields = DatabaseFields::VideoMetadataAll);
916 
922  void changeVideoMetadata(qlonglong imageID, const QVariantList& infos,
923  DatabaseFields::VideoMetadata fields = DatabaseFields::VideoMetadataAll);
924 
928  QVariantList getVideoMetadata(qlonglong imageID,
929  DatabaseFields::VideoMetadata metadataFields = DatabaseFields::VideoMetadataAll) const;
930 
948  void addItemPosition(qlonglong imageID, const QVariantList& infos,
949  DatabaseFields::ItemPositions fields = DatabaseFields::ItemPositionsAll);
950 
956  void changeItemPosition(qlonglong imageID, const QVariantList& infos,
957  DatabaseFields::ItemPositions fields = DatabaseFields::ItemPositionsAll);
958 
962  QVariantList getItemPosition(qlonglong imageID,
963  DatabaseFields::ItemPositions positionFields = DatabaseFields::ItemPositionsAll) const;
964 
965  QVariantList getItemPositions(QList<qlonglong> imageIDs, DatabaseFields::ItemPositions fields) const;
966 
970  void removeItemPosition(qlonglong imageid);
971 
975  void removeItemPositionAltitude(qlonglong imageid);
976 
980  QList<CommentInfo> getItemComments(qlonglong imageID) const;
981 
997  int setImageComment(qlonglong imageID, const QString& comment, DatabaseComment::Type type,
998  const QString& language = QString(), const QString& author = QString(),
999  const QDateTime& date = QDateTime()) const;
1000 
1010  void changeImageComment(int commentId, qlonglong imageID, const QVariantList& infos,
1011  DatabaseFields::ItemComments fields = DatabaseFields::ItemCommentsAll);
1012 
1016  void removeImageComment(int commentId, qlonglong imageid);
1017 
1021  QString getImageProperty(qlonglong imageID, const QString& property) const;
1022 
1026  void setImageProperty(qlonglong imageID, const QString& property, const QString& value);
1027  void removeImageProperty(qlonglong imageID, const QString& property);
1028  void removeImagePropertyByName(const QString& property);
1029 
1034  QList<CopyrightInfo> getItemCopyright(qlonglong imageID, const QString& property = QString()) const;
1035 
1037  {
1040  PropertyNoConstraint
1041  };
1042 
1046  void setItemCopyrightProperty(qlonglong imageID, const QString& property,
1047  const QString& value, const QString& extraValue = QString(),
1048  CopyrightPropertyUnique uniqueness = PropertyUnique);
1049 
1056  void removeItemCopyrightProperties(qlonglong imageID, const QString& property = QString(),
1057  const QString& extraValue = QString(),
1058  const QString& value = QString() /* NOTE parameter order */);
1059 
1063  QList<qlonglong> findByNameAndCreationDate(const QString& fileName, const QDateTime& creationDate) const;
1064 
1068  ImageHistoryEntry getItemHistory(qlonglong imageId) const;
1069 
1073  QString getImageUuid(qlonglong imageId) const;
1074 
1078  QList<qlonglong> getItemsForUuid(const QString& uuid) const;
1079 
1083  void setItemHistory(qlonglong imageId, const QString& history);
1084  void setImageUuid(qlonglong imageId, const QString& uuid);
1085 
1090  bool hasImageHistory(qlonglong imageId) const;
1091 
1095  void addImageRelation(qlonglong subjectId, qlonglong objectId, DatabaseRelation::Type type);
1096  void addImageRelation(const ImageRelation& relation);
1097 
1101  void addImageRelations(const QList<qlonglong>& subjectIds, const QList<qlonglong>& objectIds,
1102  DatabaseRelation::Type type);
1103 
1108  void removeImageRelation(qlonglong subjectId, qlonglong objectId, DatabaseRelation::Type type);
1109  void removeImageRelation(const ImageRelation& relation);
1110 
1111  QList<qlonglong> removeAllImageRelationsTo(qlonglong objectId, DatabaseRelation::Type type) const;
1112  QList<qlonglong> removeAllImageRelationsFrom(qlonglong subjectId, DatabaseRelation::Type type) const;
1113 
1119  QList<qlonglong> getImagesRelatedFrom(qlonglong subjectId,
1121  bool hasImagesRelatedFrom(qlonglong subjectId,
1123  QVector<QList<qlonglong> > getImagesRelatedFrom(const QList<qlonglong>& subjectIds,
1130  QList<qlonglong> getImagesRelatingTo(qlonglong objectId,
1132  bool hasImagesRelatingTo(qlonglong objectId,
1134  QVector<QList<qlonglong> > getImagesRelatingTo(const QList<qlonglong>& objectIds,
1136 
1143  QList<QPair<qlonglong, qlonglong> > getRelationCloud(qlonglong imageId,
1146 
1151  QList<qlonglong> getOneRelatedImageEach(const QList<qlonglong>& ids,
1153 
1157  QList<qlonglong> getRelatedImagesToByType(DatabaseRelation::Type type) const;
1158 
1165  QStringList getDirtyOrMissingFaceImageUrls() const;
1166 
1175  QList<ItemScanInfo> getIdenticalFiles(qlonglong id) const;
1176  QList<ItemScanInfo> getIdenticalFiles(const QString& uniqueHash, qlonglong fileSize, qlonglong sourceId = -1) const;
1177 
1182  QStringList getItemsURLsWithTag(int tagId) const;
1183 
1184  // ----------- Items and their tags -----------
1185 
1192  void addItemTag(qlonglong imageID, int tagID, bool newTag = false);
1193 
1200  void addItemTag(int albumID, const QString& name, int tagID);
1201 
1206  void addTagsToItems(QList<qlonglong> imageIDs, QList<int> tagIDs);
1207 
1213  void removeItemTag(qlonglong imageID, int tagID);
1214 
1220  void removeItemAllTags(qlonglong imageID, const QList<int>& currentTagIds);
1221 
1226  void removeTagsFromItems(QList<qlonglong> imageIDs, const QList<int>& tagIDs);
1227 
1233  QStringList getItemTagNames(qlonglong imageID) const;
1234 
1240  QList<int> getItemTagIDs(qlonglong imageID) const;
1241 
1246  QVector<QList<int> > getItemsTagIDs(const QList<qlonglong>& imageIds) const;
1247 
1252  QList<ImageTagProperty> getImageTagProperties(qlonglong imageId, int tagId = -1) const;
1253 
1257  QList<int> getTagIdsWithProperties(qlonglong imageId) const;
1258 
1264  void addImageTagProperty(qlonglong imageId, int tagId, const QString& property, const QString& value);
1265  void addImageTagProperty(const ImageTagProperty& property);
1266 
1275  void removeImageTagProperties(qlonglong imageId, int tagId = -1, const QString& property = QString(),
1276  const QString& value = QString());
1277 
1284  bool hasTags(const QList<qlonglong>& imageIDList) const;
1285 
1292  QList<int> getItemCommonTagIDs(const QList<qlonglong>& imageIDList) const;
1293 
1298  QMap<int, int> getNumberOfImagesInTags() const;
1299 
1304  QMap<int, int> getNumberOfImagesInTagProperties(const QString& property) const;
1305 
1309  int getNumberOfImagesInTagProperties(int tagId, const QString& property) const;
1310 
1314  QList<qlonglong> getImagesWithImageTagProperty(int tagId, const QString& property) const;
1315 
1319  QList<qlonglong> getImagesWithProperty(const QString& property) const;
1320 
1325  QMap<QString, int> getFormatStatistics() const;
1326  QMap<QString, int> getFormatStatistics(DatabaseItem::Category category) const;
1327 
1331  QStringList getListFromImageMetadata(DatabaseFields::ImageMetadata field) const;
1332 
1333  // ----------- Moving and Copying Items -----------
1334 
1343  void moveItem(int srcAlbumID, const QString& srcName,
1344  int dstAlbumID, const QString& dstName);
1345 
1356  int copyItem(int srcAlbumID, const QString& srcName,
1357  int dstAlbumID, const QString& dstName);
1358 
1362  void copyImageAttributes(qlonglong srcId, qlonglong destId);
1363 
1367  void copyImageProperties(qlonglong srcId, qlonglong dstId);
1368 
1372  void copyImageTags(qlonglong srcId, qlonglong dstId);
1373 
1374  // ------------ Clear all Item Metadata -----------
1375 
1380  void clearMetadataFromImage(qlonglong imageID);
1381 
1382  // ----------- Download history methods -----------
1383 
1388  int findInDownloadHistory(const QString& identifier, const QString& name,
1389  qlonglong fileSize, const QDateTime& date) const;
1390 
1395  int addToDownloadHistory(const QString& identifier, const QString& name,
1396  qlonglong fileSize, const QDateTime& date) const;
1397 
1398  QList<QVariant> getImageIdsFromArea(qreal lat1, qreal lat2, qreal lng1, qreal lng2,
1399  int sortMode, const QString& sortBy) const;
1400 
1401  // ----------- Database shrinking methods ----------
1402 
1406  bool integrityCheck() const;
1407 
1411  void vacuum();
1412 
1413  // ----------- Static helper methods for constructing SQL queries -----------
1414 
1415  static QStringList imagesFieldList(DatabaseFields::Images fields);
1416  static QStringList imageInformationFieldList(DatabaseFields::ItemInformation fields);
1417  static QStringList videoMetadataFieldList(DatabaseFields::VideoMetadata fields);
1418  static QStringList imageMetadataFieldList(DatabaseFields::ImageMetadata fields);
1419  static QStringList imagePositionsFieldList(DatabaseFields::ItemPositions fields);
1420  static QStringList imageCommentsFieldList(DatabaseFields::ItemComments fields);
1421  static void addBoundValuePlaceholders(QString& query, int count);
1422 
1423 public:
1424 
1426 
1430  explicit CoreDB(CoreDbBackend* const backend);
1431 
1435  ~CoreDB();
1436 
1437 protected:
1438 
1439  QList<qlonglong> getRelatedImages(qlonglong id, bool fromOrTo,
1440  DatabaseRelation::Type type, bool boolean) const;
1441  QVector<QList<qlonglong> > getRelatedImages(QList<qlonglong> ids, bool fromOrTo,
1442  DatabaseRelation::Type type, bool boolean) const;
1443 
1444 private:
1445 
1446  // Disable
1447  CoreDB(const CoreDB&) = delete;
1448  CoreDB& operator=(const CoreDB&) = delete;
1449 
1450  void readSettings();
1451  void writeSettings();
1452 
1453 private:
1454 
1455  class Private;
1456  Private* const d;
1457 };
1458 
1459 } // namespace Digikam
1460 
1461 #endif // DIGIKAM_CORE_DB_H
Definition: coredb.h:57
CopyrightPropertyUnique
Definition: coredb.h:1037
@ PropertyUnique
Definition: coredb.h:1038
@ PropertyExtraValueUnique
Definition: coredb.h:1039
ItemSortOrder
Definition: coredb.h:637
@ ByItemPath
Definition: coredb.h:640
@ ByItemDate
Definition: coredb.h:641
@ NoItemSorting
Definition: coredb.h:638
@ ByItemName
Definition: coredb.h:639
Definition: coredbaccess.h:56
CoreDbBackend * backend() const
Definition: coredbaccess.cpp:164
Definition: coredbbackend.h:42
Definition: coredbalbuminfo.h:362
Definition: coredbalbuminfo.h:385
Definition: coredbalbuminfo.h:428
Definition: coredbalbuminfo.h:277
Definition: coredbalbuminfo.h:250
Definition: coredbalbuminfo.h:165
Definition: coredbalbuminfo.h:124
Definition: coredbalbuminfo.h:405
Definition: piwigotalker.h:48
qulonglong value
Definition: itemviewutilities.cpp:592
Type
Definition: coredbconstants.h:44
Type
Definition: coredbconstants.h:128
@ ItemInformationAll
Definition: coredbfields.h:94
@ ItemCommentsAll
Definition: coredbfields.h:189
@ VideoMetadataAll
Definition: coredbfields.h:225
@ ItemPositionsAll
Definition: coredbfields.h:165
@ ImageMetadataAll
Definition: coredbfields.h:130
Category
Definition: coredbconstants.h:95
Status
Definition: coredbconstants.h:85
Type
Definition: coredbconstants.h:112
@ UndefinedType
Definition: coredbconstants.h:113
Type
Definition: coredbconstants.h:60
Definition: datefolderview.cpp:43