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

0001 /* ============================================================
0002  *
0003  * This file is a part of digiKam project
0004  * https://www.digikam.org
0005  *
0006  * Date        : 2005-04-21
0007  * Description : Handling access to one item and associated data - Labels
0008  *
0009  * SPDX-FileCopyrightText: 2005      by Renchi Raju <renchi dot raju at gmail dot com>
0010  * SPDX-FileCopyrightText: 2007-2013 by Marcel Wiesweg <marcel dot wiesweg at gmx dot de>
0011  * SPDX-FileCopyrightText: 2009-2024 by Gilles Caulier <caulier dot gilles at gmail dot com>
0012  * SPDX-FileCopyrightText: 2013      by Michael G. Hansen <mike at mghansen dot de>
0013  *
0014  * SPDX-License-Identifier: GPL-2.0-or-later
0015  *
0016  * ============================================================ */
0017 
0018 #include "iteminfo_p.h"
0019 
0020 namespace Digikam
0021 {
0022 
0023 int ItemInfo::pickLabel() const
0024 {
0025     if (!m_data)
0026     {
0027         return NoPickLabel;
0028     }
0029 
0030     RETURN_IF_CACHED(pickLabel)
0031 
0032     int pickLabel = TagsCache::instance()->pickLabelFromTags(tagIds());
0033 
0034     ItemInfoWriteLocker lock;
0035     m_data.data()->pickLabel       = (pickLabel == -1) ? NoPickLabel : pickLabel;
0036     m_data.data()->pickLabelCached = true;
0037 
0038     return m_data->pickLabel;
0039 }
0040 
0041 int ItemInfo::colorLabel() const
0042 {
0043     if (!m_data)
0044     {
0045         return NoColorLabel;
0046     }
0047 
0048     RETURN_IF_CACHED(colorLabel)
0049 
0050     int colorLabel = TagsCache::instance()->colorLabelFromTags(tagIds());
0051 
0052     ItemInfoWriteLocker lock;
0053     m_data.data()->colorLabel       = (colorLabel == -1) ? NoColorLabel : colorLabel;
0054     m_data.data()->colorLabelCached = true;
0055 
0056     return m_data->colorLabel;
0057 }
0058 
0059 int ItemInfo::rating() const
0060 {
0061     if (!m_data)
0062     {
0063         return 0;
0064     }
0065 
0066     RETURN_IF_CACHED(rating)
0067 
0068     QVariantList values = CoreDbAccess().db()->getItemInformation(m_data->id, DatabaseFields::Rating);
0069 
0070     STORE_IN_CACHE_AND_RETURN(rating, values.first().toLongLong())
0071 }
0072 
0073 void ItemInfo::setPickLabel(int pickId)
0074 {
0075     if (!m_data || (pickId < FirstPickLabel) || (pickId > LastPickLabel))
0076     {
0077         return;
0078     }
0079 
0080     QList<int> currentTagIds   = tagIds();
0081     QVector<int> pickLabelTags = TagsCache::instance()->pickLabelTags();
0082 
0083     {
0084         ItemInfoWriteLocker lock;
0085         m_data->pickLabel       = pickId;
0086         m_data->pickLabelCached = true;
0087     }
0088 
0089     // Pick Label is an exclusive tag.
0090     // Perform "switch" operation atomic
0091 
0092     {
0093         CoreDbAccess access;
0094 
0095         Q_FOREACH (int tagId, currentTagIds)
0096         {
0097             if (pickLabelTags.contains(tagId))
0098             {
0099                 removeTag(tagId);
0100             }
0101         }
0102 
0103         setTag(pickLabelTags[pickId]);
0104     }
0105 }
0106 
0107 void ItemInfo::setColorLabel(int colorId)
0108 {
0109     if (!m_data || (colorId < FirstColorLabel) || (colorId > LastColorLabel))
0110     {
0111         return;
0112     }
0113 
0114     QList<int> currentTagIds    = tagIds();
0115     QVector<int> colorLabelTags = TagsCache::instance()->colorLabelTags();
0116 
0117     {
0118         ItemInfoWriteLocker lock;
0119         m_data->colorLabel       = colorId;
0120         m_data->colorLabelCached = true;
0121     }
0122 
0123     // Color Label is an exclusive tag.
0124     // Perform "switch" operation atomic
0125 
0126     {
0127         CoreDbAccess access;
0128 
0129         Q_FOREACH (int tagId, currentTagIds)
0130         {
0131             if (colorLabelTags.contains(tagId))
0132             {
0133                 removeTag(tagId);
0134             }
0135         }
0136 
0137         setTag(colorLabelTags[colorId]);
0138     }
0139 }
0140 
0141 void ItemInfo::setRating(int value)
0142 {
0143     if (!m_data)
0144     {
0145         return;
0146     }
0147 
0148     {
0149         ItemInfoWriteLocker lock;
0150         m_data->rating       = value;
0151         m_data->ratingCached = true;
0152     }
0153 
0154     CoreDbAccess().db()->changeItemInformation(m_data->id, QVariantList() << value, DatabaseFields::Rating);
0155 }
0156 
0157 } // namespace Digikam