File indexing completed on 2024-04-21 05:01:40

0001 /*
0002     This is the new synchronizer of Smb4K.
0003 
0004     SPDX-FileCopyrightText: 2011-2023 Alexander Reinholdt <alexander.reinholdt@kdemail.net>
0005     SPDX-License-Identifier: GPL-2.0-or-later
0006 */
0007 
0008 // application specific includes
0009 #include "smb4ksynchronizer.h"
0010 #include "smb4kglobal.h"
0011 #include "smb4knotification.h"
0012 #include "smb4kshare.h"
0013 #include "smb4ksynchronizer_p.h"
0014 
0015 // Qt includes
0016 #include <QCoreApplication>
0017 #include <QDebug>
0018 #include <QTimer>
0019 
0020 using namespace Smb4KGlobal;
0021 
0022 Q_GLOBAL_STATIC(Smb4KSynchronizerStatic, p);
0023 
0024 Smb4KSynchronizer::Smb4KSynchronizer(QObject *parent)
0025     : KCompositeJob(parent)
0026     , d(new Smb4KSynchronizerPrivate)
0027 {
0028     setAutoDelete(false);
0029     connect(QCoreApplication::instance(), SIGNAL(aboutToQuit()), SLOT(slotAboutToQuit()));
0030 }
0031 
0032 Smb4KSynchronizer::~Smb4KSynchronizer()
0033 {
0034 }
0035 
0036 Smb4KSynchronizer *Smb4KSynchronizer::self()
0037 {
0038     return &p->instance;
0039 }
0040 
0041 void Smb4KSynchronizer::synchronize(const QUrl &sourceUrl, const QUrl &destinationUrl)
0042 {
0043     if (!isRunning(sourceUrl)) {
0044         Smb4KSyncJob *job = new Smb4KSyncJob(this);
0045         job->setObjectName(QStringLiteral("SyncJob_") + sourceUrl.toLocalFile());
0046         job->setupSynchronization(sourceUrl, destinationUrl);
0047 
0048         connect(job, &Smb4KSyncJob::result, this, &Smb4KSynchronizer::slotJobFinished);
0049         connect(job, &Smb4KSyncJob::aboutToStart, this, &Smb4KSynchronizer::aboutToStart);
0050         connect(job, &Smb4KSyncJob::finished, this, &Smb4KSynchronizer::finished);
0051 
0052         addSubjob(job);
0053 
0054         job->start();
0055     }
0056 }
0057 
0058 bool Smb4KSynchronizer::isRunning()
0059 {
0060     return hasSubjobs();
0061 }
0062 
0063 bool Smb4KSynchronizer::isRunning(const QUrl &sourceUrl)
0064 {
0065     bool running = false;
0066 
0067     QListIterator<KJob *> it(subjobs());
0068 
0069     while (it.hasNext()) {
0070         if (it.next()->objectName() == QStringLiteral("SyncJob_") + sourceUrl.toLocalFile()) {
0071             running = true;
0072             break;
0073         }
0074     }
0075 
0076     return running;
0077 }
0078 
0079 void Smb4KSynchronizer::abort(const QUrl &sourceUrl)
0080 {
0081     if (!sourceUrl.isEmpty() && sourceUrl.isValid()) {
0082         QListIterator<KJob *> it(subjobs());
0083 
0084         while (it.hasNext()) {
0085             KJob *job = it.next();
0086 
0087             if (QStringLiteral("SyncJob_") + sourceUrl.toLocalFile() == job->objectName()) {
0088                 job->kill(KJob::EmitResult);
0089                 break;
0090             }
0091         }
0092     } else {
0093         QListIterator<KJob *> it(subjobs());
0094 
0095         while (it.hasNext()) {
0096             it.next()->kill(KJob::EmitResult);
0097         }
0098     }
0099 }
0100 
0101 void Smb4KSynchronizer::start()
0102 {
0103     QTimer::singleShot(0, this, SLOT(slotStartJobs()));
0104 }
0105 
0106 /////////////////////////////////////////////////////////////////////////////
0107 //   SLOT IMPLEMENTATIONS
0108 /////////////////////////////////////////////////////////////////////////////
0109 
0110 void Smb4KSynchronizer::slotStartJobs()
0111 {
0112     // FIXME: Not implemented yet. I do not see a use case at the moment.
0113 }
0114 
0115 void Smb4KSynchronizer::slotJobFinished(KJob *job)
0116 {
0117     // Remove the job.
0118     removeSubjob(job);
0119 }
0120 
0121 void Smb4KSynchronizer::slotAboutToQuit()
0122 {
0123     abort();
0124 }