File indexing completed on 2024-07-14 03:53:58

0001 /*
0002     This file is part of the KDE libraries
0003     SPDX-FileCopyrightText: 2014 David Faure <faure@kde.org>
0004 
0005     SPDX-License-Identifier: LGPL-2.0-or-later
0006 */
0007 
0008 #include "restorejob.h"
0009 
0010 #include "job_p.h"
0011 #include "kiocoredebug.h"
0012 #include <kdirnotify.h>
0013 
0014 #include <QTimer>
0015 
0016 using namespace KIO;
0017 
0018 class KIO::RestoreJobPrivate : public KIO::JobPrivate
0019 {
0020 public:
0021     RestoreJobPrivate(const QList<QUrl> &urls, JobFlags flags)
0022         : JobPrivate()
0023         , m_urls(urls)
0024         , m_urlsIterator(m_urls.constBegin())
0025         , m_progress(0)
0026         , m_flags(flags)
0027     {
0028     }
0029     QList<QUrl> m_urls;
0030     QList<QUrl>::const_iterator m_urlsIterator;
0031     int m_progress;
0032     JobFlags m_flags;
0033 
0034     void slotStart();
0035     Q_DECLARE_PUBLIC(RestoreJob)
0036 
0037     static inline RestoreJob *newJob(const QList<QUrl> &urls, JobFlags flags)
0038     {
0039         RestoreJob *job = new RestoreJob(*new RestoreJobPrivate(urls, flags));
0040         job->setUiDelegate(KIO::createDefaultJobUiDelegate());
0041         if (!(flags & HideProgressInfo)) {
0042             KIO::getJobTracker()->registerJob(job);
0043         }
0044         return job;
0045     }
0046 };
0047 
0048 RestoreJob::RestoreJob(RestoreJobPrivate &dd)
0049     : Job(dd)
0050 {
0051     Q_D(RestoreJob);
0052     QTimer::singleShot(0, this, [d]() {
0053         d->slotStart();
0054     });
0055 }
0056 
0057 RestoreJob::~RestoreJob()
0058 {
0059 }
0060 
0061 void RestoreJobPrivate::slotStart()
0062 {
0063     Q_Q(RestoreJob);
0064     if (m_urlsIterator == m_urls.constBegin()) { // first time: emit total
0065         q->setTotalAmount(KJob::Files, m_urls.count());
0066     }
0067 
0068     if (m_urlsIterator != m_urls.constEnd()) {
0069         const QUrl &url = *m_urlsIterator;
0070         Q_ASSERT(url.scheme() == QLatin1String("trash"));
0071         QByteArray packedArgs;
0072         QDataStream stream(&packedArgs, QIODevice::WriteOnly);
0073         stream << int(3) << url;
0074         KIO::Job *job = KIO::special(url, packedArgs, m_flags);
0075         q->addSubjob(job);
0076         q->setProcessedAmount(KJob::Files, q->processedAmount(KJob::Files) + 1);
0077     } else {
0078 #ifndef KIO_ANDROID_STUB
0079         org::kde::KDirNotify::emitFilesRemoved(m_urls);
0080 #endif
0081         q->emitResult();
0082     }
0083 }
0084 
0085 void RestoreJob::slotResult(KJob *job)
0086 {
0087     Q_D(RestoreJob);
0088     if (job->error()) {
0089         qCDebug(KIO_CORE) << job->errorString();
0090         KIO::Job::slotResult(job); // will set the error and emit result(this)
0091         return;
0092     }
0093     removeSubjob(job);
0094     // Move on to next one
0095     ++d->m_urlsIterator;
0096     ++d->m_progress;
0097     emitPercent(d->m_progress, d->m_urls.count());
0098     d->slotStart();
0099 }
0100 
0101 RestoreJob *KIO::restoreFromTrash(const QList<QUrl> &urls, JobFlags flags)
0102 {
0103     return RestoreJobPrivate::newJob(urls, flags);
0104 }
0105 
0106 #include "moc_restorejob.cpp"