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        : 2006-30-08
0007  * Description : batch thumbnails generator
0008  *
0009  * SPDX-FileCopyrightText: 2006-2024 by Gilles Caulier <caulier dot gilles at gmail dot com>
0010  * SPDX-FileCopyrightText: 2012      by Andi Clemens <andi dot clemens at gmail dot com>
0011  *
0012  * SPDX-License-Identifier: GPL-2.0-or-later
0013  *
0014  * ============================================================ */
0015 
0016 #include "thumbsgenerator.h"
0017 
0018 // Qt includes
0019 
0020 #include <QApplication>
0021 #include <QString>
0022 #include <QTimer>
0023 #include <QDir>
0024 #include <QFileInfo>
0025 #include <QPixmap>
0026 
0027 // KDE includes
0028 
0029 #include <klocalizedstring.h>
0030 
0031 // Local includes
0032 
0033 #include "coredb.h"
0034 #include "coredbalbuminfo.h"
0035 #include "albummanager.h"
0036 #include "applicationsettings.h"
0037 #include "coredbaccess.h"
0038 #include "iteminfo.h"
0039 #include "thumbsdbaccess.h"
0040 #include "thumbsdb.h"
0041 #include "maintenancethread.h"
0042 #include "digikam_config.h"
0043 
0044 namespace Digikam
0045 {
0046 
0047 class Q_DECL_HIDDEN ThumbsGenerator::Private
0048 {
0049 public:
0050 
0051     explicit Private()
0052       : rebuildAll(true),
0053         thread(nullptr)
0054     {
0055     }
0056 
0057     bool               rebuildAll;
0058 
0059     AlbumList          albumList;
0060 
0061     QStringList        allPicturesPath;
0062 
0063     MaintenanceThread* thread;
0064 };
0065 
0066 ThumbsGenerator::ThumbsGenerator(const bool rebuildAll, const AlbumList& list, ProgressItem* const parent)
0067     : MaintenanceTool(QLatin1String("ThumbsGenerator"), parent),
0068       d(new Private)
0069 {
0070     d->albumList = list;
0071     init(rebuildAll);
0072 }
0073 
0074 ThumbsGenerator::ThumbsGenerator(const bool rebuildAll, int albumId, ProgressItem* const parent)
0075     : MaintenanceTool(QLatin1String("ThumbsGenerator"), parent),
0076       d(new Private)
0077 {
0078     d->albumList.append(AlbumManager::instance()->findPAlbum(albumId));
0079     init(rebuildAll);
0080 }
0081 
0082 ThumbsGenerator::~ThumbsGenerator()
0083 {
0084     delete d;
0085 }
0086 
0087 void ThumbsGenerator::init(const bool rebuildAll)
0088 {
0089     d->rebuildAll = rebuildAll;
0090     d->thread     = new MaintenanceThread(this);
0091 
0092     connect(d->thread, SIGNAL(signalCompleted()),
0093             this, SLOT(slotDone()));
0094 
0095     connect(d->thread, SIGNAL(signalAdvance(QImage)),
0096             this, SLOT(slotAdvance(QImage)));
0097 }
0098 
0099 void ThumbsGenerator::setUseMultiCoreCPU(bool b)
0100 {
0101     d->thread->setUseMultiCore(b);
0102 }
0103 
0104 void ThumbsGenerator::slotCancel()
0105 {
0106     d->thread->cancel();
0107     MaintenanceTool::slotCancel();
0108 }
0109 
0110 void ThumbsGenerator::slotStart()
0111 {
0112     MaintenanceTool::slotStart();
0113 
0114     setLabel(i18n("Thumbs"));
0115 
0116     ProgressManager::addProgressItem(this);
0117 
0118     QApplication::setOverrideCursor(Qt::WaitCursor);
0119 
0120     if (d->albumList.isEmpty())
0121     {
0122         d->albumList = AlbumManager::instance()->allPAlbums();
0123     }
0124 
0125     for (AlbumList::const_iterator it = d->albumList.constBegin();
0126          !canceled() && (it != d->albumList.constEnd()); ++it)
0127     {
0128         if (!(*it))
0129         {
0130             continue;
0131         }
0132 
0133         if ((*it)->type() == Album::PHYSICAL)
0134         {
0135             Q_FOREACH (const QString& path, CoreDbAccess().db()->getItemURLsInAlbum((*it)->id()))
0136             {
0137                 if (!d->allPicturesPath.contains(path))
0138                 {
0139                     d->allPicturesPath << path;
0140                 }
0141             }
0142         }
0143         else if ((*it)->type() == Album::TAG)
0144         {
0145             Q_FOREACH (const QString& path, CoreDbAccess().db()->getItemURLsInTag((*it)->id()))
0146             {
0147                 if (!d->allPicturesPath.contains(path))
0148                 {
0149                     d->allPicturesPath << path;
0150                 }
0151             }
0152         }
0153     }
0154 
0155     if (!d->rebuildAll)
0156     {
0157         QHash<QString, int> filePaths = ThumbsDbAccess().db()->getFilePathsWithThumbnail();
0158         QStringList::iterator it      = d->allPicturesPath.begin();
0159 
0160         while (it != d->allPicturesPath.end())
0161         {
0162             if (filePaths.contains(*it))
0163             {
0164                 it = d->allPicturesPath.erase(it);
0165             }
0166             else
0167             {
0168                 ++it;
0169             }
0170         }
0171     }
0172 
0173     // remove non-image or video files from the list
0174 
0175     QStringList::iterator it = d->allPicturesPath.begin();
0176 
0177     while (it != d->allPicturesPath.end())
0178     {
0179         ItemInfo info = ItemInfo::fromLocalFile(*it);
0180 
0181         if ((info.category() != DatabaseItem::Image) &&
0182             (info.category() != DatabaseItem::Video) &&
0183             (info.category() != DatabaseItem::Audio))
0184         {
0185             it = d->allPicturesPath.erase(it);
0186         }
0187         else
0188         {
0189             ++it;
0190         }
0191     }
0192 
0193     QApplication::restoreOverrideCursor();
0194 
0195     if (d->allPicturesPath.isEmpty())
0196     {
0197         slotDone();
0198         return;
0199     }
0200 
0201     setTotalItems(d->allPicturesPath.count());
0202 
0203     d->thread->generateThumbs(d->allPicturesPath);
0204     d->thread->start();
0205 }
0206 
0207 void ThumbsGenerator::slotAdvance(const QImage& img)
0208 {
0209     setThumbnail(QPixmap::fromImage(img));
0210     advance(1);
0211 }
0212 
0213 } // namespace Digikam
0214 
0215 #include "moc_thumbsgenerator.cpp"