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

0001 /* ============================================================
0002  *
0003  * This file is a part of digiKam project
0004  * https://www.digikam.org
0005  *
0006  * Date        : 2006-22-01
0007  * Description : interface to get item info from database.
0008  *
0009  * SPDX-FileCopyrightText: 2006-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 "iteminfojob.h"
0016 
0017 // Qt includes
0018 
0019 #include <QString>
0020 #include <QDataStream>
0021 #include <QUrl>
0022 
0023 // Local includes
0024 
0025 #include "digikam_debug.h"
0026 #include "itemlister.h"
0027 #include "dnotificationwrapper.h"
0028 #include "digikamapp.h"
0029 #include "dbjobsmanager.h"
0030 
0031 namespace Digikam
0032 {
0033 
0034 class Q_DECL_HIDDEN ItemInfoJob::Private
0035 {
0036 public:
0037 
0038     explicit Private()
0039       : jobThread   (nullptr)
0040     {
0041     }
0042 
0043     DBJobsThread* jobThread;
0044 };
0045 
0046 ItemInfoJob::ItemInfoJob()
0047     : d(new Private)
0048 {
0049 }
0050 
0051 ItemInfoJob::~ItemInfoJob()
0052 {
0053     if (d->jobThread)
0054     {
0055         d->jobThread->cancel();
0056     }
0057 
0058     delete d;
0059 }
0060 
0061 void ItemInfoJob::allItemsFromAlbum(Album* const album)
0062 {
0063     if (d->jobThread)
0064     {
0065         d->jobThread->cancel();
0066         d->jobThread = nullptr;
0067     }
0068 
0069     if (!album)
0070     {
0071         return;
0072     }
0073 
0074     // TODO: Drop Database Url usage
0075 
0076     CoreDbUrl url = album->databaseUrl();
0077 
0078     if      (album->type() == Album::DATE)
0079     {
0080         DatesDBJobInfo jobInfo;
0081         jobInfo.setStartDate(url.startDate());
0082         jobInfo.setEndDate(url.endDate());
0083 
0084         d->jobThread = DBJobsManager::instance()->startDatesJobThread(jobInfo);
0085     }
0086     else if (album->type() == Album::TAG)
0087     {
0088         TagsDBJobInfo jobInfo;
0089 
0090         // If we want to search for images with this tag, we only want the tag and not
0091         // all images in the tag path.
0092 
0093         jobInfo.setTagsIds(QList<int>() << url.tagId());
0094 
0095         d->jobThread = DBJobsManager::instance()->startTagsJobThread(jobInfo);
0096     }
0097     else if (album->type() == Album::PHYSICAL)
0098     {
0099         AlbumsDBJobInfo jobInfo;
0100         jobInfo.setAlbumRootId(url.albumRootId());
0101         jobInfo.setAlbum(url.album());
0102 
0103         d->jobThread = DBJobsManager::instance()->startAlbumsJobThread(jobInfo);
0104     }
0105     else if (album->type() == Album::SEARCH)
0106     {
0107         QList<int> searchIds = QList<int>() << url.searchId();
0108         SearchesDBJobInfo jobInfo(std::move(searchIds));
0109 
0110         d->jobThread = DBJobsManager::instance()->startSearchesJobThread(jobInfo);
0111     }
0112 
0113     connect(d->jobThread, SIGNAL(finished()),
0114             this, SLOT(slotResult()));
0115 
0116     connect(d->jobThread, SIGNAL(data(QList<ItemListerRecord>)),
0117             this, SLOT(slotData(QList<ItemListerRecord>)));
0118 }
0119 
0120 void ItemInfoJob::stop()
0121 {
0122     if (d->jobThread)
0123     {
0124         d->jobThread->cancel();
0125         d->jobThread = nullptr;
0126     }
0127 }
0128 
0129 bool ItemInfoJob::isRunning() const
0130 {
0131     return d->jobThread;
0132 }
0133 
0134 void ItemInfoJob::slotResult()
0135 {
0136     if (d->jobThread != sender())
0137     {
0138         return;
0139     }
0140 
0141     if (d->jobThread->hasErrors())
0142     {
0143         qCWarning(DIGIKAM_GENERAL_LOG) << "Failed to list url: " << d->jobThread->errorsList().first();
0144 
0145         // Pop-up a message about the error.
0146 
0147         DNotificationWrapper(QString(), d->jobThread->errorsList().first(),
0148                              DigikamApp::instance(), DigikamApp::instance()->windowTitle());
0149     }
0150 
0151     d->jobThread = nullptr;
0152 
0153     Q_EMIT signalCompleted();
0154 }
0155 
0156 void ItemInfoJob::slotData(const QList<ItemListerRecord>& records)
0157 {
0158     if (records.isEmpty())
0159     {
0160         return;
0161     }
0162 
0163     ItemInfoList itemsList;
0164 
0165     Q_FOREACH (const ItemListerRecord& record, records)
0166     {
0167         ItemInfo info(record);
0168         itemsList.append(info);
0169     }
0170 
0171     // Sort the itemList based on name
0172 
0173     std::sort(itemsList.begin(), itemsList.end(), ItemInfoList::namefileLessThan);
0174 
0175     Q_EMIT signalItemsInfo(itemsList);
0176 }
0177 
0178 } // namespace Digikam
0179 
0180 #include "moc_iteminfojob.cpp"