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        : 2013-08-14
0007  * Description : Thread actions task for thumbs generator.
0008  *
0009  * SPDX-FileCopyrightText: 2013-2024 by Gilles Caulier <caulier dot gilles at gmail dot com>
0010  *
0011  * SPDX-License-Identifier: GPL-2.0-or-later
0012  *
0013  * ============================================================ */
0014 
0015 #include "thumbstask.h"
0016 
0017 // Qt includes
0018 #include <QMutex>
0019 #include <QWaitCondition>
0020 
0021 // Local includes
0022 
0023 #include "digikam_debug.h"
0024 #include "thumbnailloadthread.h"
0025 #include "thumbnailsize.h"
0026 #include "maintenancedata.h"
0027 
0028 namespace Digikam
0029 {
0030 
0031 class Q_DECL_HIDDEN ThumbsTask::Private
0032 {
0033 public:
0034 
0035     explicit Private()
0036         : thread (nullptr),
0037           data   (nullptr)
0038     {
0039     }
0040 
0041     ThumbnailLoadThread* thread;
0042 
0043     MaintenanceData*     data;
0044 
0045     QImage               image;
0046 
0047     QMutex               mutex;
0048     QWaitCondition       condVar;
0049 };
0050 
0051 // -------------------------------------------------------
0052 
0053 ThumbsTask::ThumbsTask()
0054     : ActionJob(),
0055       d        (new Private)
0056 {
0057     d->thread = new ThumbnailLoadThread;
0058     d->thread->setPixmapRequested(false);
0059     d->thread->setThumbnailSize(ThumbnailLoadThread::maximumThumbnailSize());
0060 
0061     connect(d->thread, SIGNAL(signalThumbnailLoaded(LoadingDescription,QImage)),
0062             this, SLOT(slotThumbnailLoaded(LoadingDescription,QImage)),
0063             Qt::DirectConnection);
0064 }
0065 
0066 ThumbsTask::~ThumbsTask()
0067 {
0068     cancel();
0069 
0070     {
0071         QMutexLocker locker(&d->mutex);
0072         d->condVar.wakeAll();
0073     }
0074 
0075     d->thread->stopAllTasks();
0076     d->thread->wait();
0077 
0078     delete d->thread;
0079     delete d;
0080 }
0081 
0082 void ThumbsTask::setMaintenanceData(MaintenanceData* const data)
0083 {
0084     d->data = data;
0085 }
0086 
0087 void ThumbsTask::run()
0088 {
0089     // While we have data (using this as check for non-null)
0090 
0091     while (d->data)
0092     {
0093         if (m_cancel)
0094         {
0095             return;
0096         }
0097 
0098         QString path = d->data->getImagePath();
0099 
0100         if (path.isEmpty())
0101         {
0102             break;
0103         }
0104 
0105         ItemInfo info = ItemInfo::fromLocalFile(path);
0106 
0107         if (!m_cancel && !info.isNull())
0108         {
0109             d->thread->deleteThumbnail(info.filePath());
0110             d->thread->find(info.thumbnailIdentifier());
0111 
0112             if (!m_cancel && d->image.isNull())
0113             {
0114                 QMutexLocker locker(&d->mutex);
0115                 d->condVar.wait(&d->mutex);
0116             }
0117 
0118             Q_EMIT signalFinished(d->image);
0119             d->image = QImage();
0120         }
0121     }
0122 
0123     Q_EMIT signalDone();
0124 }
0125 
0126 void ThumbsTask::slotThumbnailLoaded(const LoadingDescription&, const QImage& image)
0127 {
0128     QMutexLocker locker(&d->mutex);
0129 
0130     d->image = image;
0131     d->condVar.wakeAll();
0132 }
0133 
0134 } // namespace Digikam
0135 
0136 #include "moc_thumbstask.cpp"