KIO

pastejob.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 "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
22using namespace KIO;
23
24extern KIO::Job *pasteMimeDataImpl(const QMimeData *mimeData, const QUrl &destUrl, const QString &dialogText, QWidget *widget, bool clipboard);
25
26PasteJob::PasteJob(PasteJobPrivate &dd)
27 : Job(dd)
28{
30
31 QTimer::singleShot(0, this, [d]() {
32 d->slotStart();
33 });
34}
35
36PasteJob::~PasteJob()
37{
38}
39
40void 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 KIO::CopyJob *copyJob = nullptr;
51 if (m_mimeData->hasUrls()) {
53 if (!urls.isEmpty()) {
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 if (copyJob) {
80 Q_EMIT q->copyJobStarted(copyJob);
81 }
82 } else {
83 q->setError(KIO::ERR_NO_CONTENT);
84 q->emitResult();
85 }
86}
87
88void 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 *>(job);
95 if (simpleJob) {
96 Q_EMIT itemCreated(simpleJob->url());
97 }
98
99 removeSubjob(job);
100 emitResult();
101}
102
103PasteJob *KIO::paste(const QMimeData *mimeData, const QUrl &destDir, JobFlags flags)
104{
105 return PasteJobPrivate::newJob(mimeData, destDir, flags, true /*clipboard*/);
106}
107
108#include "moc_pastejob.cpp"
virtual void slotResult(KJob *job)
CopyJob is used to move, copy or symlink files and directories.
Definition copyjob.h:41
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.
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.
FileUndoManager: makes it possible to undo kio jobs.
static FileUndoManager * self()
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.
@ Put
Represents the creation of a file from data in memory. Used when pasting data from clipboard or drag-...
The base class for all jobs.
Definition job_base.h:45
bool removeSubjob(KJob *job) override
Mark a sub job as being done.
Definition job.cpp:80
A KIO job that handles pasting the clipboard contents.
Definition pastejob.h:35
void itemCreated(const QUrl &url)
Signals that a file or directory was created.
A simple job (one url and one command).
Definition simplejob.h:27
const QUrl & url() const
Returns the SimpleJob's URL.
Definition simplejob.cpp:70
void emitResult()
int error() const
QString i18n(const char *text, const TYPE &arg...)
A namespace for KIO globals.
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:2626
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:2604
KIOWIDGETS_EXPORT bool isClipboardDataCut(const QMimeData *mimeData)
Returns true if the URLs in mimeData were cut by the user.
Definition paste.cpp:273
@ ERR_NO_CONTENT
Action succeeded but no content will follow.
Definition global.h:172
KIOWIDGETS_EXPORT PasteJob * paste(const QMimeData *mimeData, const QUrl &destDir, JobFlags flags=DefaultFlags)
Pastes the clipboard contents.
Definition pastejob.cpp:103
QWidget * window(QObject *job)
KCOREADDONS_EXPORT QList< QUrl > urlsFromMimeData(const QMimeData *mimeData, DecodeOptions decodeOptions=PreferKdeUrls, MetaDataMap *metaData=nullptr)
bool isEmpty() const const
Q_EMITQ_EMIT
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
Q_D(Todo)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Jun 14 2024 11:51:27 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.