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 "pastejob.h" |
9 | #include "pastejob_p.h" |
10 | |
11 | #include "paste.h" |
12 | |
13 | #include <QMimeData> |
14 | #include <QTimer> |
15 | |
16 | #include <KIO/CopyJob> |
17 | #include <KIO/FileUndoManager> |
18 | #include <KJobWidgets> |
19 | #include <KLocalizedString> |
20 | #include <KUrlMimeData> |
21 | |
22 | using namespace KIO; |
23 | |
24 | extern KIO::Job *pasteMimeDataImpl(const QMimeData *mimeData, const QUrl &destUrl, const QString &dialogText, QWidget *widget, bool clipboard); |
25 | |
26 | PasteJob::PasteJob(PasteJobPrivate &dd) |
27 | : Job(dd) |
28 | { |
29 | Q_D(PasteJob); |
30 | |
31 | QTimer::singleShot(interval: 0, receiver: this, slot: [d]() { |
32 | d->slotStart(); |
33 | }); |
34 | } |
35 | |
36 | PasteJob::~PasteJob() |
37 | { |
38 | } |
39 | |
40 | void PasteJobPrivate::slotStart() |
41 | { |
42 | Q_Q(PasteJob); |
43 | if (!m_mimeData) { |
44 | q->setError(KIO::ERR_NO_CONTENT); |
45 | q->emitResult(); |
46 | return; |
47 | } |
48 | const bool move = KIO::isClipboardDataCut(mimeData: m_mimeData); |
49 | KIO::Job *job = nullptr; |
50 | KIO::CopyJob *copyJob = nullptr; |
51 | if (m_mimeData->hasUrls()) { |
52 | const QList<QUrl> urls = KUrlMimeData::urlsFromMimeData(mimeData: m_mimeData, decodeOptions: KUrlMimeData::PreferLocalUrls); |
53 | if (!urls.isEmpty()) { |
54 | if (move) { |
55 | copyJob = KIO::move(src: urls, dest: m_destDir, flags: m_flags); |
56 | } else { |
57 | copyJob = KIO::copy(src: urls, dest: m_destDir, flags: m_flags); |
58 | } |
59 | QObject::connect(sender: copyJob, signal: &KIO::CopyJob::copyingDone, context: q, slot: [this](KIO::Job *job, const QUrl &src, const QUrl &dest) { |
60 | slotCopyingDone(job, src, to: dest); |
61 | }); |
62 | |
63 | QObject::connect(sender: copyJob, signal: &KIO::CopyJob::copyingLinkDone, context: q, slot: [this](KIO::Job *job, const QUrl &from, const QString &target, const QUrl &to) { |
64 | slotCopyingLinkDone(job, from, target, to); |
65 | }); |
66 | |
67 | KIO::FileUndoManager::self()->recordJob(op: move ? KIO::FileUndoManager::Move : KIO::FileUndoManager::Copy, src: QList<QUrl>(), dst: m_destDir, job: copyJob); |
68 | job = copyJob; |
69 | } |
70 | } else { |
71 | const QString dialogText = m_clipboard ? i18n("Filename for clipboard content:" ) : i18n("Filename for dropped contents:" ); |
72 | job = pasteMimeDataImpl(mimeData: m_mimeData, destUrl: m_destDir, dialogText, widget: KJobWidgets::window(job: q), clipboard: m_clipboard); |
73 | if (KIO::SimpleJob *simpleJob = qobject_cast<KIO::SimpleJob *>(object: job)) { |
74 | KIO::FileUndoManager::self()->recordJob(op: KIO::FileUndoManager::Put, src: QList<QUrl>(), dst: simpleJob->url(), job); |
75 | } |
76 | } |
77 | if (job) { |
78 | q->addSubjob(job); |
79 | if (copyJob) { |
80 | Q_EMIT q->copyJobStarted(job: copyJob); |
81 | } |
82 | } else { |
83 | q->setError(KIO::ERR_NO_CONTENT); |
84 | q->emitResult(); |
85 | } |
86 | } |
87 | |
88 | void PasteJob::slotResult(KJob *job) |
89 | { |
90 | if (job->error()) { |
91 | KIO::Job::slotResult(job); // will set the error and emit result(this) |
92 | return; |
93 | } |
94 | KIO::SimpleJob *simpleJob = qobject_cast<KIO::SimpleJob *>(object: job); |
95 | if (simpleJob) { |
96 | Q_EMIT itemCreated(url: simpleJob->url()); |
97 | } |
98 | |
99 | removeSubjob(job); |
100 | emitResult(); |
101 | } |
102 | |
103 | PasteJob *KIO::paste(const QMimeData *mimeData, const QUrl &destDir, JobFlags flags) |
104 | { |
105 | return PasteJobPrivate::newJob(mimeData, destDir, flags, clipboard: true /*clipboard*/); |
106 | } |
107 | |
108 | #include "moc_pastejob.cpp" |
109 | |