File indexing completed on 2025-01-05 03:59:45

0001 /* ============================================================
0002  *
0003  * This file is a part of digiKam project
0004  * https://www.digikam.org
0005  *
0006  * Date        : 2009-10-16
0007  * Description : history updater thread for importui
0008  *
0009  * SPDX-FileCopyrightText: 2009-2011 by Andi Clemens <andi dot clemens at gmail dot com>
0010  * SPDX-FileCopyrightText: 2009-2024 by Gilles Caulier <caulier dot gilles at gmail dot com>
0011  *
0012  * SPDX-License-Identifier: GPL-2.0-or-later
0013  *
0014  * ============================================================ */
0015 
0016 #include "camerahistoryupdater.h"
0017 
0018 // Qt includes
0019 
0020 #include <QList>
0021 #include <QMutex>
0022 #include <QVariant>
0023 #include <QWaitCondition>
0024 #include <QWidget>
0025 
0026 // Local includes
0027 
0028 #include "digikam_debug.h"
0029 #include "coredbdownloadhistory.h"
0030 
0031 namespace Digikam
0032 {
0033 
0034 class Q_DECL_HIDDEN CameraHistoryUpdater::Private
0035 {
0036 
0037 public:
0038 
0039     typedef QList<CHUpdateItem> CHUpdateItemsList;
0040 
0041 public:
0042 
0043     explicit Private()
0044       : canceled(false),
0045         running (false)
0046     {
0047     }
0048 
0049     bool              canceled;
0050     bool              running;
0051 
0052     QMutex            mutex;
0053     QWaitCondition    condVar;
0054     CHUpdateItemsList updateItems;
0055 };
0056 
0057 // --------------------------------------------------------
0058 
0059 CameraHistoryUpdater::CameraHistoryUpdater(QWidget* const parent)
0060     : QThread(parent),
0061       d      (new Private)
0062 {
0063     qRegisterMetaType<CHUpdateItemMap>("CHUpdateItemMap");
0064 }
0065 
0066 CameraHistoryUpdater::~CameraHistoryUpdater()
0067 {
0068     // clear updateItems, stop processing
0069 
0070     slotCancel();
0071 
0072     // stop thread
0073     {
0074         QMutexLocker lock(&d->mutex);
0075         d->running = false;
0076         d->condVar.wakeAll();
0077     }
0078 
0079     wait();
0080 
0081     delete d;
0082 }
0083 
0084 void CameraHistoryUpdater::slotCancel()
0085 {
0086     d->canceled = true;
0087     QMutexLocker lock(&d->mutex);
0088     d->updateItems.clear();
0089 }
0090 
0091 void CameraHistoryUpdater::run()
0092 {
0093     while (d->running)
0094     {
0095         CHUpdateItem item;
0096 
0097         QMutexLocker lock(&d->mutex);
0098 
0099         if (!d->updateItems.isEmpty())
0100         {
0101             item = d->updateItems.takeFirst();
0102             sendBusy(true);
0103             proccessMap(item.first, item.second);
0104         }
0105         else
0106         {
0107             sendBusy(false);
0108             d->condVar.wait(&d->mutex);
0109             continue;
0110         }
0111     }
0112 
0113     sendBusy(false);
0114 }
0115 
0116 void CameraHistoryUpdater::sendBusy(bool val)
0117 {
0118     Q_EMIT signalBusy(val);
0119 }
0120 
0121 void CameraHistoryUpdater::addItems(const QByteArray& id, CHUpdateItemMap& map)
0122 {
0123     if (map.isEmpty())
0124     {
0125         return;
0126     }
0127 
0128     qCDebug(DIGIKAM_IMPORTUI_LOG) << "Check download state from DB for " << map.count() << " items";
0129 
0130     QMutexLocker lock(&d->mutex);
0131     d->running  = true;
0132     d->canceled = false;
0133     d->updateItems << CHUpdateItem(id, map);
0134 
0135     if (!isRunning())
0136     {
0137         start(LowPriority);
0138     }
0139 
0140     d->condVar.wakeAll();
0141 }
0142 
0143 void CameraHistoryUpdater::proccessMap(const QByteArray& id, CHUpdateItemMap& map)
0144 {
0145     CHUpdateItemMap::iterator it = map.begin();
0146 
0147     do
0148     {
0149         // We query database to check if (*it).have been already downloaded from camera.
0150 
0151         switch (CoreDbDownloadHistory::status(QString::fromUtf8(id), (*it).name, (*it).size, (*it).ctime))
0152         {
0153             case CoreDbDownloadHistory::NotDownloaded:
0154                 (*it).downloaded = CamItemInfo::NewPicture;
0155                 break;
0156 
0157             case CoreDbDownloadHistory::Downloaded:
0158                 (*it).downloaded = CamItemInfo::DownloadedYes;
0159                 break;
0160 
0161             default: // CoreDbDownloadHistory::StatusUnknown
0162                 (*it).downloaded = CamItemInfo::DownloadUnknown;
0163                 break;
0164         }
0165 
0166         ++it;
0167     }
0168     while (it != map.end() && !d->canceled);
0169 
0170     Q_EMIT signalHistoryMap(map);
0171 }
0172 
0173 } // namespace Digikam
0174 
0175 #include "moc_camerahistoryupdater.cpp"