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

0001 /* ============================================================
0002  *
0003  * This file is a part of digiKam project
0004  * https://www.digikam.org
0005  *
0006  * Date        : 2004-10-04
0007  * Description : synchronize Input/Output jobs.
0008  *
0009  * SPDX-FileCopyrightText: 2006-2013 by Marcel Wiesweg <marcel dot wiesweg at gmx dot de>
0010  * SPDX-FileCopyrightText: 2006-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 "syncjob.h"
0017 
0018 // Qt includes
0019 
0020 #include <QEventLoop>
0021 
0022 // Local includes
0023 
0024 #include "applicationsettings.h"
0025 #include "thumbnailsize.h"
0026 #include "albumthumbnailloader.h"
0027 #include "album.h"
0028 
0029 namespace Digikam
0030 {
0031 
0032 class Q_DECL_HIDDEN SyncJob::Private
0033 {
0034 public:
0035 
0036     explicit Private()
0037       : waitingLoop(nullptr),
0038         thumbnail  (nullptr),
0039         album      (nullptr)
0040     {
0041     }
0042 
0043     QEventLoop* waitingLoop;
0044     QPixmap*    thumbnail;
0045 
0046     Album*      album;
0047 };
0048 
0049 SyncJob::SyncJob()
0050     : d(new Private)
0051 {
0052     d->waitingLoop = new QEventLoop(this);
0053 }
0054 
0055 SyncJob::~SyncJob()
0056 {
0057     delete d->thumbnail;
0058     d->thumbnail = nullptr;
0059     delete d;
0060 }
0061 
0062 void SyncJob::enterWaitingLoop() const
0063 {
0064     d->waitingLoop->exec(QEventLoop::ExcludeUserInputEvents);
0065 }
0066 
0067 void SyncJob::quitWaitingLoop() const
0068 {
0069     d->waitingLoop->quit();
0070 }
0071 
0072 // ---------------------------------------------------------------
0073 
0074 QPixmap SyncJob::getTagThumbnail(TAlbum* const album)
0075 {
0076     SyncJob sj;
0077 
0078     return sj.getTagThumbnailPriv(album);
0079 }
0080 
0081 QPixmap SyncJob::getTagThumbnailPriv(TAlbum* const album) const
0082 {
0083     delete d->thumbnail;
0084 
0085     d->thumbnail                       = new QPixmap();
0086     AlbumThumbnailLoader* const loader = AlbumThumbnailLoader::instance();
0087 
0088     connect(loader, SIGNAL(signalThumbnail(Album*,QPixmap)),
0089             this, SLOT(slotGotThumbnailFromIcon(Album*,QPixmap)),
0090             Qt::QueuedConnection);
0091 
0092     connect(loader, SIGNAL(signalFailed(Album*)),
0093             this, SLOT(slotLoadThumbnailFailed(Album*)),
0094             Qt::QueuedConnection);
0095 
0096     if (!loader->getTagThumbnail(album, *d->thumbnail))
0097     {
0098         if (d->thumbnail->isNull())
0099         {
0100             if (album->hasProperty(TagPropertyName::person()))
0101             {
0102                 return loader->getStandardFaceIcon(album);
0103             }
0104             else
0105             {
0106                 return loader->getStandardTagIcon(album);
0107             }
0108         }
0109     }
0110     else
0111     {
0112         d->album = album;
0113         enterWaitingLoop();
0114     }
0115 
0116     return *d->thumbnail;
0117 }
0118 
0119 void SyncJob::slotLoadThumbnailFailed(Album* album)
0120 {
0121     if (album == d->album)
0122     {
0123         quitWaitingLoop();
0124     }
0125 }
0126 
0127 void SyncJob::slotGotThumbnailFromIcon(Album* album, const QPixmap& pix)
0128 {
0129     if (album == d->album)
0130     {
0131         *d->thumbnail = pix;
0132         quitWaitingLoop();
0133     }
0134 }
0135 
0136 } // namespace Digikam
0137 
0138 #include "moc_syncjob.cpp"