File indexing completed on 2024-12-22 05:05:31

0001 /*
0002    SPDX-FileCopyrightText: 2015-2024 Laurent Montel <montel@kde.org>
0003 
0004    SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include "fullsynchronizeresourcesjob.h"
0008 
0009 #include <KLocalizedString>
0010 
0011 #include "synchronizeresourcejob.h"
0012 #include <QProgressDialog>
0013 
0014 FullSynchronizeResourcesJob::FullSynchronizeResourcesJob(QObject *parent)
0015     : QObject(parent)
0016 {
0017 }
0018 
0019 FullSynchronizeResourcesJob::~FullSynchronizeResourcesJob() = default;
0020 
0021 void FullSynchronizeResourcesJob::setResources(const QStringList &lst)
0022 {
0023     if (lst.isEmpty()) {
0024         Q_EMIT synchronizeFinished();
0025         deleteLater();
0026     } else {
0027         mResources = lst;
0028     }
0029 }
0030 
0031 void FullSynchronizeResourcesJob::setWindowParent(QWidget *parent)
0032 {
0033     mWindowParent = parent;
0034 }
0035 
0036 void FullSynchronizeResourcesJob::start()
0037 {
0038     if (!mProgressDialog) {
0039         mProgressDialog = new QProgressDialog(QString(), i18n("Cancel"), 0, mResources.count(), mWindowParent);
0040     }
0041     mProgressDialog->setWindowTitle(i18nc("@title:window", "Synchronize resources"));
0042     mProgressDialog->setLabelText(i18n("Synchronize resources... It can take some time."));
0043     mProgressDialog->setWindowModality(Qt::WindowModal);
0044     // Disable cancel button.
0045     mProgressDialog->setCancelButton(nullptr);
0046 
0047     auto job = new SynchronizeResourceJob(this);
0048     // Full sync
0049     job->setSynchronizeOnlyCollection(false);
0050     job->setListResources(mResources);
0051     connect(job, &SynchronizeResourceJob::synchronizationFinished, this, &FullSynchronizeResourcesJob::slotSynchronizeFinished);
0052     connect(job, &SynchronizeResourceJob::synchronizationInstanceDone, this, &FullSynchronizeResourcesJob::slotSynchronizeInstanceDone);
0053     connect(job, &SynchronizeResourceJob::synchronizationInstanceFailed, this, &FullSynchronizeResourcesJob::slotSynchronizeInstanceFailed);
0054     job->start();
0055 }
0056 
0057 void FullSynchronizeResourcesJob::slotSynchronizeInstanceDone(const QString &identifier)
0058 {
0059     Q_EMIT synchronizeInstanceDone(identifier);
0060     mProgressDialog->setValue(mProgressDialog->value() + 1);
0061 }
0062 
0063 void FullSynchronizeResourcesJob::slotSynchronizeInstanceFailed(const QString &identifier)
0064 {
0065     Q_EMIT synchronizeInstanceFailed(identifier);
0066     mProgressDialog->setValue(mProgressDialog->value() + 1);
0067 }
0068 
0069 void FullSynchronizeResourcesJob::slotSynchronizeFinished()
0070 {
0071     mProgressDialog->setValue(mProgressDialog->value() + 1);
0072     Q_EMIT synchronizeFinished();
0073     deleteLater();
0074 }
0075 
0076 #include "moc_fullsynchronizeresourcesjob.cpp"