KIO

pastejob.cpp
1 /*
2  This file is part of the KDE libraries
3  SPDX-FileCopyrightText: 2014 David Faure <[email protected]>
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(0, this, [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(m_mimeData);
49  KIO::Job *job = nullptr;
50  if (m_mimeData->hasUrls()) {
52  if (!urls.isEmpty()) {
53  KIO::CopyJob *copyJob;
54  if (move) {
55  copyJob = KIO::move(urls, m_destDir, m_flags);
56  } else {
57  copyJob = KIO::copy(urls, m_destDir, m_flags);
58  }
59  QObject::connect(copyJob, &KIO::CopyJob::copyingDone, q, [this](KIO::Job *job, const QUrl &src, const QUrl &dest) {
60  slotCopyingDone(job, src, dest);
61  });
62 
63  QObject::connect(copyJob, &KIO::CopyJob::copyingLinkDone, q, [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(move ? KIO::FileUndoManager::Move : KIO::FileUndoManager::Copy, QList<QUrl>(), m_destDir, 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(m_mimeData, m_destDir, dialogText, KJobWidgets::window(q), m_clipboard);
73  if (KIO::SimpleJob *simpleJob = qobject_cast<KIO::SimpleJob *>(job)) {
75  }
76  }
77  if (job) {
78  q->addSubjob(job);
79  } else {
80  q->setError(KIO::ERR_NO_CONTENT);
81  q->emitResult();
82  }
83 }
84 
85 void PasteJob::slotResult(KJob *job)
86 {
87  if (job->error()) {
88  KIO::Job::slotResult(job); // will set the error and emit result(this)
89  return;
90  }
91  KIO::SimpleJob *simpleJob = qobject_cast<KIO::SimpleJob *>(job);
92  if (simpleJob) {
93  Q_EMIT itemCreated(simpleJob->url());
94  }
95 
96  removeSubjob(job);
97  emitResult();
98 }
99 
100 PasteJob *KIO::paste(const QMimeData *mimeData, const QUrl &destDir, JobFlags flags)
101 {
102  return PasteJobPrivate::newJob(mimeData, destDir, flags, true /*clipboard*/);
103 }
104 
105 #include "moc_pastejob.cpp"
KIOCORE_EXPORT CopyJob * copy(const QUrl &src, const QUrl &dest, JobFlags flags=DefaultFlags)
Copy a file or directory src into the destination dest, which can be a file (including the final file...
Definition: copyjob.cpp:2627
Q_EMITQ_EMIT
KIOWIDGETS_EXPORT bool isClipboardDataCut(const QMimeData *mimeData)
Returns true if the URLs in mimeData were cut by the user.
Definition: paste.cpp:358
void copyingLinkDone(KIO::Job *job, const QUrl &from, const QString &target, const QUrl &to)
The job is copying or moving a symbolic link, that points to target.
const QUrl & url() const
Returns the SimpleJob's URL.
Definition: simplejob.cpp:70
void itemCreated(const QUrl &url)
Signals that a file or directory was created.
QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
static FileUndoManager * self()
@ Put
Represents the creation of a file from data in memory. Used when pasting data from clipboard or drag-...
QString i18n(const char *text, const TYPE &arg...)
void copyingDone(KIO::Job *job, const QUrl &from, const QUrl &to, const QDateTime &mtime, bool directory, bool renamed)
The job emits this signal when copying or moving a file or directory successfully finished.
void recordJob(CommandType op, const QList< QUrl > &src, const QUrl &dst, KIO::Job *job)
Record this job while it's happening and add a command for it so that the user can undo it.
KIOCORE_EXPORT CopyJob * move(const QUrl &src, const QUrl &dest, JobFlags flags=DefaultFlags)
Moves a file or directory src to the given destination dest.
Definition: copyjob.cpp:2649
bool isEmpty() const const
A namespace for KIO globals.
virtual void slotResult(KJob *job)
@ ERR_NO_CONTENT
Action succeeded but no content will follow.
Definition: global.h:269
KCOREADDONS_EXPORT QList< QUrl > urlsFromMimeData(const QMimeData *mimeData, DecodeOptions decodeOptions=PreferKdeUrls, MetaDataMap *metaData=nullptr)
void emitResult()
int error() const
KJOBWIDGETS_EXPORT QWidget * window(KJob *job)
KIOWIDGETS_EXPORT PasteJob * paste(const QMimeData *mimeData, const QUrl &destDir, JobFlags flags=DefaultFlags)
Pastes the clipboard contents.
Definition: pastejob.cpp:100
bool removeSubjob(KJob *job) override
Mark a sub job as being done.
Definition: job.cpp:87
Q_D(Todo)
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Tue Nov 28 2023 03:55:14 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.