File indexing completed on 2025-01-19 03:59:32

0001 /* ============================================================
0002  *
0003  * This file is a part of digiKam project
0004  * https://www.digikam.org
0005  *
0006  * Date        : 2023-21-06
0007  * Description : Thread actions task for metadata remover.
0008  *
0009  * SPDX-FileCopyrightText: 2013-2024 by Gilles Caulier <caulier dot gilles at gmail dot com>
0010  * SPDX-FileCopyrightText: 2023      by Maik Qualmann <metzpinguin at gmail dot com>
0011  *
0012  * SPDX-License-Identifier: GPL-2.0-or-later
0013  *
0014  * ============================================================ */
0015 
0016 #include "metadataremovetask.h"
0017 
0018 // Local includes
0019 
0020 #include "digikam_debug.h"
0021 #include "coredboperationgroup.h"
0022 #include "maintenancedata.h"
0023 #include "scancontroller.h"
0024 #include "facetagseditor.h"
0025 #include "metadatahub.h"
0026 #include "faceutils.h"
0027 #include "tagscache.h"
0028 #include "iteminfo.h"
0029 
0030 namespace Digikam
0031 {
0032 
0033 class Q_DECL_HIDDEN MetadataRemoveTask::Private
0034 {
0035 public:
0036 
0037     explicit Private()
0038         : removeAction (MetadataRemover::None),
0039           data         (nullptr)
0040     {
0041     }
0042 
0043     MetadataRemover::RemoveAction removeAction;
0044 
0045     MaintenanceData*              data;
0046 };
0047 
0048 // -------------------------------------------------------
0049 
0050 MetadataRemoveTask::MetadataRemoveTask()
0051     : ActionJob(),
0052       d        (new Private)
0053 {
0054 }
0055 
0056 MetadataRemoveTask::~MetadataRemoveTask()
0057 {
0058     cancel();
0059     delete d;
0060 }
0061 
0062 void MetadataRemoveTask::setRemoveAction(MetadataRemover::RemoveAction action)
0063 {
0064     d->removeAction = action;
0065 }
0066 
0067 void MetadataRemoveTask::setMaintenanceData(MaintenanceData* const data)
0068 {
0069     d->data = data;
0070 }
0071 
0072 void MetadataRemoveTask::run()
0073 {
0074     // While we have data (using this as check for non-null)
0075 
0076     while (d->data)
0077     {
0078         if (m_cancel)
0079         {
0080             return;
0081         }
0082 
0083         ItemInfo item = d->data->getItemInfo();
0084 
0085         // If the item is null, we are done.
0086 
0087         if (item.isNull())
0088         {
0089             break;
0090         }
0091 
0092         if      (d->removeAction == MetadataRemover::Faces)
0093         {
0094             if (FaceTagsEditor().databaseFaces(item.id()).size() > 0)
0095             {
0096                 FaceUtils().removeAllFaces(item.id());
0097             }
0098         }
0099         else if (d->removeAction == MetadataRemover::Tags)
0100         {
0101             const QList<int>& tagIds = TagsCache::instance()->publicTags(item.tagIds());
0102 
0103             if (!tagIds.isEmpty())
0104             {
0105                 bool tagRemoved = false;
0106                 QList<int> confirmedFaceTags;
0107 
0108                 Q_FOREACH (const FaceTagsIface& face, FaceTagsEditor().confirmedFaceTagsIfaces(item.id()))
0109                 {
0110                     confirmedFaceTags << face.tagId();
0111                 }
0112 
0113                 {
0114                     CoreDbOperationGroup group;
0115                     group.setMaximumTime(200);
0116 
0117                     Q_FOREACH (int tag, tagIds)
0118                     {
0119                         if (!confirmedFaceTags.contains(tag)  &&
0120                             !FaceTags::isSystemPersonTagId(tag))
0121                         {
0122                             item.removeTag(tag);
0123                             group.allowLift();
0124 
0125                             tagRemoved = true;
0126                         }
0127                     }
0128                 }
0129 
0130                 if (tagRemoved)
0131                 {
0132                     MetadataHub hub;
0133                     hub.load(item);
0134 
0135                     ScanController::FileMetadataWrite writeScope(item);
0136                     writeScope.changed(hub.writeToMetadata(item, MetadataHub::WRITE_TAGS));
0137                 }
0138             }
0139         }
0140 
0141         Q_EMIT signalFinished(QImage());
0142     }
0143 
0144     Q_EMIT signalDone();
0145 }
0146 
0147 } // namespace Digikam
0148 
0149 #include "moc_metadataremovetask.cpp"