| 1 | /* |
| 2 | This file is part of the KDE libraries |
| 3 | SPDX-FileCopyrightText: 2014 David Faure <faure@kde.org> |
| 4 | |
| 5 | SPDX-License-Identifier: LGPL-2.0-or-later |
| 6 | */ |
| 7 | |
| 8 | #include "restorejob.h" |
| 9 | |
| 10 | #include "job_p.h" |
| 11 | #include "kiocoredebug.h" |
| 12 | #include <kdirnotify.h> |
| 13 | |
| 14 | #include <QTimer> |
| 15 | |
| 16 | using namespace KIO; |
| 17 | |
| 18 | class KIO::RestoreJobPrivate : public KIO::JobPrivate |
| 19 | { |
| 20 | public: |
| 21 | RestoreJobPrivate(const QList<QUrl> &urls, JobFlags flags) |
| 22 | : JobPrivate() |
| 23 | , m_urls(urls) |
| 24 | , m_urlsIterator(m_urls.constBegin()) |
| 25 | , m_progress(0) |
| 26 | , m_flags(flags) |
| 27 | { |
| 28 | } |
| 29 | QList<QUrl> m_urls; |
| 30 | QList<QUrl>::const_iterator m_urlsIterator; |
| 31 | int m_progress; |
| 32 | JobFlags m_flags; |
| 33 | |
| 34 | void slotStart(); |
| 35 | Q_DECLARE_PUBLIC(RestoreJob) |
| 36 | |
| 37 | static inline RestoreJob *newJob(const QList<QUrl> &urls, JobFlags flags) |
| 38 | { |
| 39 | RestoreJob *job = new RestoreJob(*new RestoreJobPrivate(urls, flags)); |
| 40 | job->setUiDelegate(KIO::createDefaultJobUiDelegate()); |
| 41 | if (!(flags & HideProgressInfo)) { |
| 42 | KIO::getJobTracker()->registerJob(job); |
| 43 | } |
| 44 | return job; |
| 45 | } |
| 46 | }; |
| 47 | |
| 48 | RestoreJob::RestoreJob(RestoreJobPrivate &dd) |
| 49 | : Job(dd) |
| 50 | { |
| 51 | Q_D(RestoreJob); |
| 52 | QTimer::singleShot(interval: 0, receiver: this, slot: [d]() { |
| 53 | d->slotStart(); |
| 54 | }); |
| 55 | } |
| 56 | |
| 57 | RestoreJob::~RestoreJob() |
| 58 | { |
| 59 | } |
| 60 | |
| 61 | void RestoreJobPrivate::slotStart() |
| 62 | { |
| 63 | Q_Q(RestoreJob); |
| 64 | if (m_urlsIterator == m_urls.constBegin()) { // first time: emit total |
| 65 | q->setTotalAmount(unit: KJob::Files, amount: m_urls.count()); |
| 66 | } |
| 67 | |
| 68 | if (m_urlsIterator != m_urls.constEnd()) { |
| 69 | const QUrl &url = *m_urlsIterator; |
| 70 | Q_ASSERT(url.scheme() == QLatin1String("trash" )); |
| 71 | QByteArray packedArgs; |
| 72 | QDataStream stream(&packedArgs, QIODevice::WriteOnly); |
| 73 | stream << int(3) << url; |
| 74 | KIO::Job *job = KIO::special(url, data: packedArgs, flags: m_flags); |
| 75 | q->addSubjob(job); |
| 76 | q->setProcessedAmount(unit: KJob::Files, amount: q->processedAmount(unit: KJob::Files) + 1); |
| 77 | } else { |
| 78 | #ifdef WITH_QTDBUS |
| 79 | org::kde::KDirNotify::emitFilesRemoved(fileList: m_urls); |
| 80 | #endif |
| 81 | q->emitResult(); |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | void RestoreJob::slotResult(KJob *job) |
| 86 | { |
| 87 | Q_D(RestoreJob); |
| 88 | if (job->error()) { |
| 89 | qCDebug(KIO_CORE) << job->errorString(); |
| 90 | KIO::Job::slotResult(job); // will set the error and emit result(this) |
| 91 | return; |
| 92 | } |
| 93 | removeSubjob(job); |
| 94 | // Move on to next one |
| 95 | ++d->m_urlsIterator; |
| 96 | ++d->m_progress; |
| 97 | emitPercent(processedAmount: d->m_progress, totalAmount: d->m_urls.count()); |
| 98 | d->slotStart(); |
| 99 | } |
| 100 | |
| 101 | RestoreJob *KIO::restoreFromTrash(const QList<QUrl> &urls, JobFlags flags) |
| 102 | { |
| 103 | return RestoreJobPrivate::newJob(urls, flags); |
| 104 | } |
| 105 | |
| 106 | #include "moc_restorejob.cpp" |
| 107 | |