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

0001 /* ============================================================
0002  *
0003  * This file is a part of digiKam project
0004  * https://www.digikam.org
0005  *
0006  * Date        : 2012-02-02
0007  * Description : maintenance tool
0008  *
0009  * SPDX-FileCopyrightText: 2012-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 "maintenancetool.h"
0017 
0018 // Qt includes
0019 
0020 #include <QTime>
0021 #include <QTimer>
0022 #include <QElapsedTimer>
0023 #include <QApplication>
0024 
0025 // KDE includes
0026 
0027 #include <klocalizedstring.h>
0028 
0029 // Local includes
0030 
0031 #include "digikam_debug.h"
0032 #include "digikam_config.h"
0033 #include "dnotificationwrapper.h"
0034 
0035 namespace Digikam
0036 {
0037 
0038 class Q_DECL_HIDDEN MaintenanceTool::Private
0039 {
0040 public:
0041 
0042     explicit Private()
0043       : notification(true)
0044     {
0045     }
0046 
0047     bool          notification;
0048     QElapsedTimer duration;
0049 };
0050 
0051 MaintenanceTool::MaintenanceTool(const QString& id, ProgressItem* const parent)
0052     : ProgressItem(parent, id, QString(), QString(), true, true),
0053       d(new Private)
0054 {
0055     // --- NOTE: use dynamic binding as slotCancel() is a virtual method which can be re-implemented in derived classes.
0056 
0057 #ifdef Q_OS_WIN
0058 
0059     connect(this, SIGNAL(progressItemCanceledById(QString)),
0060             this, SLOT(slotCancel()));
0061 
0062 #else
0063 
0064     connect(this, static_cast<void (ProgressItem::*)(const QString&)>(&ProgressItem::progressItemCanceledById),
0065             this, &MaintenanceTool::slotCancel);
0066 
0067 #endif
0068 
0069 }
0070 
0071 MaintenanceTool::~MaintenanceTool()
0072 {
0073     delete d;
0074 }
0075 
0076 void MaintenanceTool::setNotificationEnabled(bool b)
0077 {
0078     d->notification = b;
0079 }
0080 
0081 void MaintenanceTool::start()
0082 {
0083     if (ProgressManager::instance()->findItembyId(id()))
0084     {
0085         QTimer::singleShot(2000, this, SLOT(start()));
0086 
0087         return;
0088     }
0089 
0090     // We delay start to be sure that eventloop
0091     // connect signals and slots in top level.
0092 
0093     QTimer::singleShot(0, this, SLOT(slotStart()));
0094 }
0095 
0096 void MaintenanceTool::slotStart()
0097 {
0098     d->duration.start();
0099 }
0100 
0101 void MaintenanceTool::slotDone()
0102 {
0103     QTime t = QTime::fromMSecsSinceStartOfDay(d->duration.elapsed());
0104 
0105     if (d->notification)
0106     {
0107         // Pop-up a message to bring user when all is done.
0108 
0109         DNotificationWrapper(id(),
0110                              i18n("Process is done.\nDuration: %1", t.toString()),
0111                              qApp->activeWindow(), label());
0112     }
0113 
0114     Q_EMIT signalComplete();
0115 
0116     setComplete();
0117 }
0118 
0119 void MaintenanceTool::slotCancel()
0120 {
0121     setComplete();
0122     Q_EMIT signalCanceled();
0123 }
0124 
0125 } // namespace Digikam
0126 
0127 #include "moc_maintenancetool.cpp"