File indexing completed on 2025-01-05 03:54:17

0001 /* ============================================================
0002  *
0003  * This file is a part of digiKam project
0004  * https://www.digikam.org
0005  *
0006  * Date        : 2005-10-28
0007  * Description : scan item controller - progress operations.
0008  *
0009  * SPDX-FileCopyrightText: 2005-2006 by Tom Albers <tomalbers at kde dot nl>
0010  * SPDX-FileCopyrightText: 2006-2024 by Gilles Caulier <caulier dot gilles at gmail dot com>
0011  * SPDX-FileCopyrightText: 2007-2013 by Marcel Wiesweg <marcel dot wiesweg at gmx dot de>
0012  *
0013  * SPDX-License-Identifier: GPL-2.0-or-later
0014  *
0015  * ============================================================ */
0016 
0017 #include "scancontroller_p.h"
0018 
0019 namespace Digikam
0020 {
0021 
0022 void ScanController::slotTotalFilesToScan(int count)
0023 {
0024     if (d->progressDialog)
0025     {
0026         d->progressDialog->incrementMaximum(count);
0027     }
0028 
0029     d->totalFilesToScan += count;
0030     Q_EMIT totalFilesToScan(d->totalFilesToScan);
0031 }
0032 
0033 void ScanController::slotScannedFiles(int scanned)
0034 {
0035     if (d->progressDialog)
0036     {
0037         d->progressDialog->advance(scanned);
0038     }
0039 
0040     if (d->totalFilesToScan)
0041     {
0042         Q_EMIT filesScanned(scanned);
0043         Q_EMIT scanningProgress(double(scanned) / double(d->totalFilesToScan));
0044     }
0045 }
0046 
0047 /// implementing InitializationObserver
0048 void ScanController::moreSchemaUpdateSteps(int numberOfSteps)
0049 {
0050     // not from main thread
0051 
0052     Q_EMIT triggerShowProgressDialog();
0053     Q_EMIT incrementProgressDialog(numberOfSteps);
0054 }
0055 
0056 /// implementing InitializationObserver
0057 void ScanController::schemaUpdateProgress(const QString& message, int numberOfSteps)
0058 {
0059     // not from main thread
0060 
0061     Q_EMIT progressFromInitialization(message, numberOfSteps);
0062 }
0063 
0064 void ScanController::slotProgressFromInitialization(const QString& message, int numberOfSteps)
0065 {
0066     // main thread
0067 
0068     if (d->progressDialog)
0069     {
0070         d->progressDialog->addedAction(d->actionPixmap(), message);
0071         d->progressDialog->advance(numberOfSteps);
0072     }
0073 }
0074 
0075 /// implementing InitializationObserver
0076 void ScanController::error(const QString& errorMessage)
0077 {
0078     // not from main thread
0079 
0080     Q_EMIT errorFromInitialization(errorMessage);
0081 }
0082 
0083 void ScanController::slotErrorFromInitialization(const QString& errorMessage)
0084 {
0085     // main thread
0086 
0087     QString message = i18n("Error");
0088 
0089     if (d->progressDialog)
0090     {
0091         d->progressDialog->addedAction(d->errorPixmap(), message);
0092     }
0093 
0094     QMessageBox::critical(d->progressDialog, qApp->applicationName(), errorMessage);
0095 }
0096 
0097 void ScanController::slotShowProgressDialog()
0098 {
0099     if (d->progressDialog)
0100     {
0101 /*
0102         if (!CollectionScanner::databaseInitialScanDone())
0103 */
0104         {
0105             d->progressDialog->show();
0106         }
0107     }
0108 }
0109 
0110 AlbumCopyMoveHint ScanController::hintForAlbum(const PAlbum* const album,
0111                                                int dstAlbumRootId,
0112                                                const QString& relativeDstPath,
0113                                                const QString& albumName)
0114 {
0115     QString dstAlbumPath;
0116 
0117     if (relativeDstPath == QLatin1String("/"))
0118     {
0119         dstAlbumPath = relativeDstPath + albumName;
0120     }
0121     else
0122     {
0123         dstAlbumPath = relativeDstPath + QLatin1Char('/') + albumName;
0124     }
0125 
0126     return AlbumCopyMoveHint(album->albumRootId(),
0127                              album->id(),
0128                              dstAlbumRootId,
0129                              dstAlbumPath);
0130 }
0131 
0132 QList<AlbumCopyMoveHint> ScanController::hintsForAlbum(const PAlbum* const album,
0133                                                        int dstAlbumRootId,
0134                                                        const QString& relativeDstPath,
0135                                                        const QString& albumName)
0136 {
0137     QList<AlbumCopyMoveHint> newHints;
0138 
0139     newHints << hintForAlbum(album, dstAlbumRootId, relativeDstPath, albumName);
0140     QString parentAlbumPath = album->albumPath();
0141 
0142     if (parentAlbumPath == QLatin1String("/"))
0143     {
0144         parentAlbumPath.clear();    // do not cut away a "/" in mid() below
0145     }
0146 
0147     for (AlbumIterator it(const_cast<PAlbum*>(album)); *it; ++it)
0148     {
0149         PAlbum* const a        = (PAlbum*)*it;
0150         QString childAlbumPath = a->albumPath();
0151         newHints << hintForAlbum(a,
0152                                  dstAlbumRootId,
0153                                  relativeDstPath,
0154                                  albumName + childAlbumPath.mid(parentAlbumPath.length()));
0155     }
0156 
0157     return newHints;
0158 }
0159 
0160 void ScanController::hintAtMoveOrCopyOfAlbum(const PAlbum* const album,
0161                                              const QString& dstPath,
0162                                              const QString& newAlbumName)
0163 {
0164     // get album root and album from dst path
0165 
0166     CollectionLocation location = CollectionManager::instance()->locationForPath(dstPath);
0167 
0168     if (location.isNull())
0169     {
0170         qCWarning(DIGIKAM_DATABASE_LOG) << "hintAtMoveOrCopyOfAlbum: Destination path" << dstPath
0171                                         << "does not point to an available location.";
0172         return;
0173     }
0174 
0175     QString relativeDstPath           = CollectionManager::instance()->album(location, dstPath);
0176 
0177     QList<AlbumCopyMoveHint> newHints = hintsForAlbum(album,
0178                                                       location.id(),
0179                                                       relativeDstPath,
0180                                                       newAlbumName.isNull() ? album->title()
0181                                                                             : newAlbumName);
0182 
0183 /*
0184     QMutexLocker lock(&d->mutex);
0185     d->albumHints << newHints;
0186 */
0187     d->hints->recordHints(newHints);
0188 }
0189 
0190 void ScanController::hintAtMoveOrCopyOfAlbum(const PAlbum* const album,
0191                                              const PAlbum* const dstAlbum,
0192                                              const QString& newAlbumName)
0193 {
0194     QList<AlbumCopyMoveHint> newHints = hintsForAlbum(album,
0195                                                       dstAlbum->albumRootId(),
0196                                                       dstAlbum->albumPath(),
0197                                                       newAlbumName.isNull() ? album->title()
0198                                                                             : newAlbumName);
0199 
0200 /*
0201     QMutexLocker lock(&d->mutex);
0202     d->albumHints << newHints;
0203 */
0204     d->hints->recordHints(newHints);
0205 }
0206 
0207 void ScanController::hintAtMoveOrCopyOfItems(const QList<qlonglong>& ids,
0208                                              const PAlbum* const dstAlbum,
0209                                              const QStringList& itemNames)
0210 {
0211     ItemCopyMoveHint hint(ids,
0212                           dstAlbum->albumRootId(),
0213                           dstAlbum->id(),
0214                           itemNames);
0215 
0216     d->garbageCollectHints(true);
0217 /*
0218     d->itemHints << hint;
0219 */
0220     d->hints->recordHints(QList<ItemCopyMoveHint>() << hint);
0221 }
0222 
0223 void ScanController::hintAtMoveOrCopyOfItem(qlonglong id,
0224                                             const PAlbum* const dstAlbum,
0225                                             const QString& itemName)
0226 {
0227     ItemCopyMoveHint hint(QList<qlonglong>() << id,
0228                           dstAlbum->albumRootId(),
0229                           dstAlbum->id(),
0230                           QStringList() << itemName);
0231 
0232     d->garbageCollectHints(true);
0233 /*
0234     d->itemHints << hint;
0235 */
0236     d->hints->recordHints(QList<ItemCopyMoveHint>() << hint);
0237 }
0238 
0239 void ScanController::hintAtModificationOfItems(const QList<qlonglong>& ids)
0240 {
0241     ItemChangeHint hint(ids, ItemChangeHint::ItemModified);
0242 
0243     d->garbageCollectHints(true);
0244 /*
0245     d->itemHints << hint;
0246 */
0247     d->hints->recordHints(QList<ItemChangeHint>() << hint);
0248 }
0249 
0250 void ScanController::hintAtModificationOfItem(qlonglong id)
0251 {
0252     ItemChangeHint hint(QList<qlonglong>() << id, ItemChangeHint::ItemModified);
0253 
0254     d->garbageCollectHints(true);
0255 /*
0256     d->itemHints << hint;
0257 */
0258     d->hints->recordHints(QList<ItemChangeHint>() << hint);
0259 }
0260 
0261 void ScanController::slotTriggerShowProgressDialog()
0262 {
0263     if (d->progressDialog && !d->showTimer->isActive() && !d->progressDialog->isVisible())
0264     {
0265         d->showTimer->start(300);
0266     }
0267 }
0268 
0269 } // namespace Digikam