File indexing completed on 2025-01-19 03:53:36

0001 /* ============================================================
0002  *
0003  * This file is a part of digiKam project
0004  * https://www.digikam.org
0005  *
0006  * Date        : 2009-07-04
0007  * Description : Access to extended properties of an item in the database
0008  *
0009  * SPDX-FileCopyrightText: 2009      by Marcel Wiesweg <marcel dot wiesweg at gmx dot de>
0010  * SPDX-FileCopyrightText: 2009-2024 by Gilles Caulier <caulier dot gilles at gmail dot com>
0011  * SPDX-FileCopyrightText: 2017-2018 by Mario Frank    <mario dot frank at uni minus potsdam dot de>
0012  *
0013  * SPDX-License-Identifier: GPL-2.0-or-later
0014  *
0015  * ============================================================ */
0016 
0017 #include "itemextendedproperties.h"
0018 
0019 // Local includes
0020 
0021 #include "coredb.h"
0022 #include "coredbaccess.h"
0023 #include "itemscanner.h"
0024 #include "similaritydbaccess.h"
0025 #include "similaritydb.h"
0026 
0027 namespace Digikam
0028 {
0029 
0030 ItemExtendedProperties::ItemExtendedProperties(qlonglong imageid)
0031     : m_id(imageid)
0032 {
0033 }
0034 
0035 ItemExtendedProperties::ItemExtendedProperties()
0036     : m_id(0)
0037 {
0038 }
0039 
0040 QString ItemExtendedProperties::intellectualGenre()
0041 {
0042     return readProperty(ItemScanner::iptcCorePropertyName(MetadataInfo::IptcCoreIntellectualGenre));
0043 }
0044 
0045 void ItemExtendedProperties::setIntellectualGenre(const QString& intellectualGenre)
0046 {
0047     setProperty(ItemScanner::iptcCorePropertyName(MetadataInfo::IptcCoreIntellectualGenre), intellectualGenre);
0048 }
0049 
0050 void ItemExtendedProperties::removeIntellectualGenre()
0051 {
0052     setIntellectualGenre(QString());
0053 }
0054 
0055 QString ItemExtendedProperties::jobId()
0056 {
0057     return readProperty(ItemScanner::iptcCorePropertyName(MetadataInfo::IptcCoreJobID));
0058 }
0059 
0060 void ItemExtendedProperties::setJobId(const QString& jobId)
0061 {
0062     setProperty(ItemScanner::iptcCorePropertyName(MetadataInfo::IptcCoreJobID), jobId);
0063 }
0064 
0065 void ItemExtendedProperties::removeJobId()
0066 {
0067     setJobId(QString());
0068 }
0069 
0070 double ItemExtendedProperties::similarityTo(const qlonglong imageId)
0071 {
0072     // TODO: extend for additional algorithms
0073     double similarity = SimilarityDbAccess().db()->getImageSimilarity(m_id, imageId);
0074 
0075     return (similarity > 0) ? similarity : 0.0;
0076 }
0077 
0078 void ItemExtendedProperties::setSimilarityTo(const qlonglong imageId, const double value)
0079 {
0080     // TODO: extend for additional algorithms
0081     SimilarityDbAccess().db()->setImageSimilarity(m_id, imageId, value);
0082 }
0083 
0084 void ItemExtendedProperties::removeSimilarityTo(const qlonglong imageId)
0085 {
0086     // TODO: extend for additional algorithms
0087     SimilarityDbAccess().db()->removeImageSimilarity(m_id, imageId);
0088 }
0089 
0090 QStringList ItemExtendedProperties::scene()
0091 {
0092     return readFakeListProperty(ItemScanner::iptcCorePropertyName(MetadataInfo::IptcCoreScene));
0093 }
0094 
0095 void ItemExtendedProperties::setScene(const QStringList& scene)
0096 {
0097     setFakeListProperty(ItemScanner::iptcCorePropertyName(MetadataInfo::IptcCoreScene), scene);
0098 }
0099 
0100 void ItemExtendedProperties::removeScene()
0101 {
0102     setScene(QStringList());
0103 }
0104 
0105 QStringList ItemExtendedProperties::subjectCode()
0106 {
0107     return readFakeListProperty(ItemScanner::iptcCorePropertyName(MetadataInfo::IptcCoreSubjectCode));
0108 }
0109 
0110 void ItemExtendedProperties::setSubjectCode(const QStringList& subjectCode)
0111 {
0112     setFakeListProperty(ItemScanner::iptcCorePropertyName(MetadataInfo::IptcCoreSubjectCode), subjectCode);
0113 }
0114 
0115 void ItemExtendedProperties::removeSubjectCode()
0116 {
0117     setSubjectCode(QStringList());
0118 }
0119 
0120 IptcCoreLocationInfo ItemExtendedProperties::location()
0121 {
0122     IptcCoreLocationInfo location;
0123     location.country       = readProperty(ItemScanner::iptcCorePropertyName(MetadataInfo::IptcCoreCountry));
0124     location.countryCode   = readProperty(ItemScanner::iptcCorePropertyName(MetadataInfo::IptcCoreCountryCode));
0125     location.city          = readProperty(ItemScanner::iptcCorePropertyName(MetadataInfo::IptcCoreCity));
0126     location.location      = readProperty(ItemScanner::iptcCorePropertyName(MetadataInfo::IptcCoreLocation));
0127     location.provinceState = readProperty(ItemScanner::iptcCorePropertyName(MetadataInfo::IptcCoreProvinceState));
0128 
0129     return location;
0130 }
0131 
0132 void ItemExtendedProperties::setLocation(const IptcCoreLocationInfo& location)
0133 {
0134     setProperty(ItemScanner::iptcCorePropertyName(MetadataInfo::IptcCoreCountry), location.country);
0135     setProperty(ItemScanner::iptcCorePropertyName(MetadataInfo::IptcCoreCountryCode), location.countryCode);
0136     setProperty(ItemScanner::iptcCorePropertyName(MetadataInfo::IptcCoreCity), location.city);
0137     setProperty(ItemScanner::iptcCorePropertyName(MetadataInfo::IptcCoreLocation), location.location);
0138     setProperty(ItemScanner::iptcCorePropertyName(MetadataInfo::IptcCoreProvinceState), location.provinceState);
0139 }
0140 
0141 void ItemExtendedProperties::removeLocation()
0142 {
0143     setLocation(IptcCoreLocationInfo());
0144 }
0145 
0146 QString ItemExtendedProperties::readProperty(const QString& property)
0147 {
0148     return CoreDbAccess().db()->getImageProperty(m_id, property);
0149 }
0150 
0151 void ItemExtendedProperties::setProperty(const QString& property, const QString& value)
0152 {
0153     if (value.isNull()) // there is a NOT NULL restriction on the table.
0154     {
0155         removeProperty(property);
0156     }
0157     else
0158     {
0159         CoreDbAccess().db()->setImageProperty(m_id, property, value);
0160     }
0161 }
0162 
0163 QStringList ItemExtendedProperties::readFakeListProperty(const QString& property)
0164 {
0165     QString value = CoreDbAccess().db()->getImageProperty(m_id, property);
0166 
0167     return value.split(QLatin1Char(';'), QT_SKIP_EMPTY_PARTS);
0168 }
0169 
0170 void ItemExtendedProperties::setFakeListProperty(const QString& property, const QStringList& value)
0171 {
0172     if (value.isEmpty())
0173     {
0174         removeProperty(property);
0175     }
0176     else
0177     {
0178         CoreDbAccess().db()->setImageProperty(m_id, property, value.join(QLatin1Char(';')));
0179     }
0180 }
0181 
0182 void ItemExtendedProperties::removeProperty(const QString& property)
0183 {
0184     CoreDbAccess().db()->removeImageProperty(m_id, property);
0185 }
0186 
0187 } // namespace Digikam