File indexing completed on 2025-01-19 03:55:49

0001 /* ============================================================
0002  *
0003  * This file is a part of digiKam project
0004  * https://www.digikam.org
0005  *
0006  * Date        : 2015-08-21
0007  * Description : metadatahub manager
0008  *
0009  * SPDX-FileCopyrightText: 2015 by Veaceslav Munteanu <veaceslav dot munteanu90 at gmail dot com>
0010  *
0011  * SPDX-License-Identifier: GPL-2.0-or-later
0012  *
0013  * ============================================================ */
0014 
0015 #include "metadatahubmngr.h"
0016 
0017 // Qt includes
0018 
0019 #include <QMutex>
0020 #include <QDebug>
0021 #include <QProgressDialog>
0022 
0023 // KDE includes
0024 
0025 #include <klocalizedstring.h>
0026 
0027 // Local includes
0028 
0029 #include "iteminfo.h"
0030 #include "metadatahub.h"
0031 #include "iteminfolist.h"
0032 #include "metadatasynchronizer.h"
0033 
0034 namespace Digikam
0035 {
0036 
0037 QPointer<MetadataHubMngr> MetadataHubMngr::internalPtr = QPointer<MetadataHubMngr>();
0038 
0039 class Q_DECL_HIDDEN MetadataHubMngr::Private
0040 {
0041 public:
0042 
0043     explicit Private()
0044     {
0045     }
0046 
0047     QList<qlonglong> pendingItemIds;
0048     QMutex           mutex;
0049 };
0050 
0051 MetadataHubMngr::MetadataHubMngr()
0052     : d(new Private())
0053 {
0054 }
0055 
0056 MetadataHubMngr::~MetadataHubMngr()
0057 {
0058     delete d;
0059 }
0060 
0061 MetadataHubMngr* MetadataHubMngr::instance()
0062 {
0063     if (internalPtr.isNull())
0064     {
0065         internalPtr = new MetadataHubMngr();
0066     }
0067 
0068     return internalPtr;
0069 }
0070 
0071 bool MetadataHubMngr::isCreated()
0072 {
0073     return (!internalPtr.isNull());
0074 }
0075 
0076 void MetadataHubMngr::addPendingIds(const QList<qlonglong>& imageIds)
0077 {
0078     QMutexLocker locker(&d->mutex);
0079 
0080     Q_FOREACH (const qlonglong& id, imageIds)
0081     {
0082         if (!d->pendingItemIds.contains(id))
0083         {
0084             d->pendingItemIds.append(id);
0085         }
0086    }
0087 
0088    Q_EMIT signalPendingMetadata(d->pendingItemIds.size());
0089 }
0090 
0091 void MetadataHubMngr::addPending(const ItemInfo& info)
0092 {
0093     QMutexLocker locker(&d->mutex);
0094 
0095     if (!d->pendingItemIds.contains(info.id()))
0096     {
0097         d->pendingItemIds.append(info.id());
0098     }
0099 
0100     Q_EMIT signalPendingMetadata(d->pendingItemIds.size());
0101 }
0102 
0103 void MetadataHubMngr::slotApplyPending()
0104 {
0105     QMutexLocker lock(&d->mutex);
0106 
0107     if (d->pendingItemIds.isEmpty())
0108     {
0109         return;
0110     }
0111 
0112     ItemInfoList infos;
0113 
0114     Q_FOREACH (const qlonglong& id, d->pendingItemIds)
0115     {
0116         ItemInfo info(id);
0117 
0118         if (!info.isNull() && !info.isRemoved())
0119         {
0120             infos.append(info);
0121         }
0122     }
0123 
0124     d->pendingItemIds.clear();
0125 
0126     Q_EMIT signalPendingMetadata(0);
0127 
0128     if (infos.isEmpty())
0129     {
0130         return;
0131     }
0132 
0133     MetadataSynchronizer* const tool = new MetadataSynchronizer(infos,
0134                                            MetadataSynchronizer::WriteFromDatabaseToFile);
0135     tool->start();
0136 }
0137 
0138 void MetadataHubMngr::requestShutDown()
0139 {
0140     QMutexLocker lock(&d->mutex);
0141 
0142     if (d->pendingItemIds.isEmpty())
0143     {
0144         return;
0145     }
0146 
0147     ItemInfoList infos;
0148 
0149     Q_FOREACH (const qlonglong& id, d->pendingItemIds)
0150     {
0151         ItemInfo info(id);
0152 
0153         if (!info.isNull() && !info.isRemoved())
0154         {
0155             infos.append(info);
0156         }
0157     }
0158 
0159     d->pendingItemIds.clear();
0160 
0161     Q_EMIT signalPendingMetadata(0);
0162 
0163     if (infos.isEmpty())
0164     {
0165         return;
0166     }
0167 
0168     QPointer<QProgressDialog> dialog = new QProgressDialog;
0169     dialog->setMinimum(0);
0170     dialog->setMaximum(0);
0171     dialog->setMinimumDuration(100);
0172     dialog->setLabelText(i18nc("@label", "Apply pending changes to metadata"));
0173 
0174     MetadataSynchronizer* const tool = new MetadataSynchronizer(infos,
0175                                            MetadataSynchronizer::WriteFromDatabaseToFile);
0176 
0177     connect(tool, SIGNAL(signalComplete()),
0178             dialog, SLOT(accept()));
0179 
0180     tool->start();
0181 
0182     dialog->exec();
0183 }
0184 
0185 } // namespace Digikam
0186 
0187 #include "moc_metadatahubmngr.cpp"