KIO

restorejob.cpp
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
16using namespace KIO;
17
18class KIO::RestoreJobPrivate : public KIO::JobPrivate
19{
20public:
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));
41 if (!(flags & HideProgressInfo)) {
43 }
44 return job;
45 }
46};
47
48RestoreJob::RestoreJob(RestoreJobPrivate &dd)
49 : Job(dd)
50{
52 QTimer::singleShot(0, this, [d]() {
53 d->slotStart();
54 });
55}
56
57RestoreJob::~RestoreJob()
58{
59}
60
61void RestoreJobPrivate::slotStart()
62{
63 Q_Q(RestoreJob);
64 if (m_urlsIterator == m_urls.constBegin()) { // first time: emit total
65 q->setTotalAmount(KJob::Files, 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, packedArgs, m_flags);
75 q->addSubjob(job);
76 q->setProcessedAmount(KJob::Files, q->processedAmount(KJob::Files) + 1);
77 } else {
78#ifdef WITH_QTDBUS
79 org::kde::KDirNotify::emitFilesRemoved(m_urls);
80#endif
81 q->emitResult();
82 }
83}
84
85void RestoreJob::slotResult(KJob *job)
86{
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(d->m_progress, d->m_urls.count());
98 d->slotStart();
99}
100
102{
103 return RestoreJobPrivate::newJob(urls, flags);
104}
105
106#include "moc_restorejob.cpp"
virtual void slotResult(KJob *job)
The base class for all jobs.
bool removeSubjob(KJob *job) override
Mark a sub job as being done.
Definition job.cpp:80
RestoreJob is used to restore files from the trash.
virtual void registerJob(KJob *job)
virtual QString errorString() const
int error() const
void emitPercent(qulonglong processedAmount, qulonglong totalAmount)
void setUiDelegate(KJobUiDelegate *delegate)
A namespace for KIO globals.
KIOCORE_EXPORT SimpleJob * special(const QUrl &url, const QByteArray &data, JobFlags flags=DefaultFlags)
Execute any command that is specific to one worker (protocol).
KIOCORE_EXPORT RestoreJob * restoreFromTrash(const QList< QUrl > &urls, JobFlags flags=DefaultFlags)
Restore a set of trashed files or directories.
KIOCORE_EXPORT KJobUiDelegate * createDefaultJobUiDelegate()
Convenience method: use default factory, if there's one, to create a delegate and return it.
@ HideProgressInfo
Hide progress information dialog, i.e. don't show a GUI.
Definition job_base.h:251
KIOCORE_EXPORT KJobTrackerInterface * getJobTracker()
Returns the job tracker to be used by all KIO jobs (in which HideProgressInfo is not set)
const_iterator constBegin() const const
const_iterator constEnd() const const
qsizetype count() const const
QString scheme() const const
Q_D(Todo)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Nov 22 2024 12:04:58 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.