• Skip to content
  • Skip to link menu
  • KDE API Reference
  • kdelibs-4.14.38 API Reference
  • KDE Home
  • Contact Us
 

KIO

  • kio
  • kio
clipboardupdater.cpp
Go to the documentation of this file.
1/* This file is part of the KDE libraries
2 Copyright (C) 2013 Dawit Alemayehu <adawit@kde.org>
3
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public
6 License as published by the Free Software Foundation; either
7 version 2 of the License, or (at your option) any later version.
8
9 This library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Library General Public License for more details.
13
14 You should have received a copy of the GNU Library General Public License
15 along with this library; see the file COPYING.LIB. If not, write to
16 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 Boston, MA 02110-1301, USA.
18*/
19
20#include "clipboardupdater_p.h"
21#include "jobclasses.h"
22#include "copyjob.h"
23#include "deletejob.h"
24
25#include <QApplication>
26#include <QMimeSource>
27#include <QClipboard>
28
29using namespace KIO;
30
31
32static bool canUseClipboard()
33{
34 return QApplication::type() != QApplication::Tty;
35}
36
37static void overwriteUrlsInClipboard(KJob* job)
38{
39 CopyJob* copyJob = qobject_cast<CopyJob*>(job);
40 FileCopyJob* fileCopyJob = qobject_cast<FileCopyJob*>(job);
41
42 if (!copyJob && !fileCopyJob) {
43 return;
44 }
45
46 KUrl::List newUrls;
47
48 if (copyJob) {
49 Q_FOREACH(const KUrl& url, copyJob->srcUrls()) {
50 KUrl dUrl = copyJob->destUrl();
51 dUrl.addPath(url.fileName());
52 newUrls.append(dUrl);
53 }
54 } else if (fileCopyJob) {
55 newUrls << fileCopyJob->destUrl();
56 }
57
58 QMimeData* mime = new QMimeData();
59 newUrls.populateMimeData(mime);
60 QApplication::clipboard()->setMimeData(mime);
61}
62
63static void updateUrlsInClipboard(KJob* job)
64{
65 CopyJob* copyJob = qobject_cast<CopyJob*>(job);
66 FileCopyJob* fileCopyJob = qobject_cast<FileCopyJob*>(job);
67
68 if (!copyJob && !fileCopyJob) {
69 return;
70 }
71
72 QClipboard* clipboard = QApplication::clipboard();
73 KUrl::List clipboardUrls = KUrl::List::fromMimeData(clipboard->mimeData());
74 bool update = false;
75
76 if (copyJob) {
77 Q_FOREACH(const KUrl& url, copyJob->srcUrls()) {
78 const int index = clipboardUrls.indexOf(url);
79 if (index > -1) {
80 KUrl dUrl = copyJob->destUrl();
81 dUrl.addPath(url.fileName());
82 clipboardUrls.replace(index, dUrl);
83 update = true;
84 }
85 }
86 } else if (fileCopyJob) {
87 const int index = clipboardUrls.indexOf(fileCopyJob->srcUrl());
88 if (index > -1) {
89 clipboardUrls.replace(index, fileCopyJob->destUrl());
90 update = true;
91 }
92 }
93
94 if (update) {
95 QMimeData* mime = new QMimeData();
96 clipboardUrls.populateMimeData(mime);
97 clipboard->setMimeData(mime);
98 }
99}
100
101static void removeUrlsFromClipboard(KJob* job)
102{
103 SimpleJob* simpleJob = qobject_cast<SimpleJob*>(job);
104 DeleteJob* deleteJob = qobject_cast<DeleteJob*>(job);
105
106 if (!simpleJob && !deleteJob) {
107 return;
108 }
109
110 KUrl::List deletedUrls;
111 if (simpleJob) {
112 deletedUrls << simpleJob->url();
113 } else if (deleteJob) {
114 deletedUrls << deleteJob->urls();
115 }
116
117 if (deletedUrls.isEmpty()) {
118 return;
119 }
120
121 QClipboard* clipboard = QApplication::clipboard();
122 KUrl::List clipboardUrls = KUrl::List::fromMimeData(clipboard->mimeData());
123 quint32 removedCount = 0;
124
125 Q_FOREACH(const KUrl& url, deletedUrls) {
126 removedCount += clipboardUrls.removeAll(url);
127 }
128
129 if (removedCount > 0) {
130 QMimeData* mime = new QMimeData();
131 if (!clipboardUrls.isEmpty()) {
132 clipboardUrls.populateMimeData(mime);
133 }
134 clipboard->setMimeData(mime);
135 }
136}
137
138void ClipboardUpdater::slotResult(KJob* job)
139{
140 if (job->error()) {
141 return;
142 }
143
144 switch (m_mode) {
145 case UpdateContent:
146 updateUrlsInClipboard(job);
147 break;
148 case OverwriteContent:
149 overwriteUrlsInClipboard(job);
150 break;
151 case RemoveContent:
152 removeUrlsFromClipboard(job);
153 break;
154 }
155}
156
157void ClipboardUpdater::setMode(ClipboardUpdater::Mode mode)
158{
159 m_mode = mode;
160}
161
162ClipboardUpdater* ClipboardUpdater::create(Job* job, ClipboardUpdater::Mode mode)
163{
164 if (canUseClipboard()) {
165 return new ClipboardUpdater(job, mode);
166 }
167
168 return 0;
169}
170
171void ClipboardUpdater::update(const KUrl& srcUrl, const KUrl& destUrl)
172{
173 if (!canUseClipboard()) {
174 return;
175 }
176
177 QClipboard* clipboard = QApplication::clipboard();
178 if (clipboard->mimeData()->hasUrls()) {
179 KUrl::List clipboardUrls = KUrl::List::fromMimeData(clipboard->mimeData());
180 const int index = clipboardUrls.indexOf(srcUrl);
181 if (index > -1) {
182 clipboardUrls.replace(index, destUrl);
183 QMimeData* mime = new QMimeData();
184 clipboardUrls.populateMimeData(mime);
185 clipboard->setMimeData(mime);
186 }
187 }
188}
189
190ClipboardUpdater::ClipboardUpdater(Job* job, Mode mode)
191 :QObject(job),
192 m_mode(mode)
193{
194 Q_ASSERT(job);
195 connect(job, SIGNAL(result(KJob*)), this, SLOT(slotResult(KJob*)));
196}
KIO::ClipboardUpdater
Updates the clipboard when it is affected by KIO operations.
Definition: clipboardupdater_p.h:50
KIO::ClipboardUpdater::Mode
Mode
Definition: clipboardupdater_p.h:54
KIO::ClipboardUpdater::RemoveContent
@ RemoveContent
Definition: clipboardupdater_p.h:57
KIO::ClipboardUpdater::UpdateContent
@ UpdateContent
Definition: clipboardupdater_p.h:55
KIO::ClipboardUpdater::OverwriteContent
@ OverwriteContent
Definition: clipboardupdater_p.h:56
KIO::ClipboardUpdater::create
static ClipboardUpdater * create(Job *job, Mode mode)
Returns an instance of clipboard updater if QApplication::type() does not return a tty.
Definition: clipboardupdater.cpp:162
KIO::ClipboardUpdater::setMode
void setMode(Mode m)
Sets the mode.
Definition: clipboardupdater.cpp:157
KIO::ClipboardUpdater::update
static void update(const KUrl &srcUrl, const KUrl &destUrl)
Convenience function that allows renaming of a single url in the clipboard.
Definition: clipboardupdater.cpp:171
KIO::CopyJob
CopyJob is used to move, copy or symlink files and directories.
Definition: copyjob.h:65
KIO::CopyJob::srcUrls
KUrl::List srcUrls() const
Returns the list of source URLs.
Definition: copyjob.cpp:267
KIO::CopyJob::destUrl
KUrl destUrl() const
Returns the destination URL.
Definition: copyjob.cpp:272
KIO::DeleteJob
A more complex Job to delete files and directories.
Definition: deletejob.h:43
KIO::DeleteJob::urls
KUrl::List urls() const
Returns the list of URLs.
Definition: deletejob.cpp:132
KIO::FileCopyJob
The FileCopyJob copies data from one place to another.
Definition: jobclasses.h:856
KIO::FileCopyJob::destUrl
KUrl destUrl() const
Returns the destination URL.
Definition: job.cpp:2087
KIO::FileCopyJob::srcUrl
KUrl srcUrl() const
Returns the source URL.
Definition: job.cpp:2082
KIO::Job
The base class for all jobs.
Definition: jobclasses.h:94
KIO::SimpleJob
A simple job (one url and one command).
Definition: jobclasses.h:322
KIO::SimpleJob::url
const KUrl & url() const
Returns the SimpleJob's URL.
Definition: job.cpp:341
KJob
KJob::error
int error() const
KUrl::List
KUrl::List::populateMimeData
void populateMimeData(const KUrl::List &mostLocalUrls, QMimeData *mimeData, const KUrl::MetaDataMap &metaData=MetaDataMap(), MimeDataFlags flags=DefaultMimeDataFlags) const
KUrl::List::fromMimeData
static KUrl::List fromMimeData(const QMimeData *mimeData, DecodeOptions decodeOptions, KUrl::MetaDataMap *metaData=0)
KUrl
KUrl::fileName
QString fileName(const DirectoryOptions &options=IgnoreTrailingSlash) const
KUrl::addPath
void addPath(const QString &txt)
QObject
canUseClipboard
static bool canUseClipboard()
Definition: clipboardupdater.cpp:32
updateUrlsInClipboard
static void updateUrlsInClipboard(KJob *job)
Definition: clipboardupdater.cpp:63
overwriteUrlsInClipboard
static void overwriteUrlsInClipboard(KJob *job)
Definition: clipboardupdater.cpp:37
removeUrlsFromClipboard
static void removeUrlsFromClipboard(KJob *job)
Definition: clipboardupdater.cpp:101
clipboardupdater_p.h
copyjob.h
deletejob.h
jobclasses.h
KIO
A namespace for KIO globals.
Definition: kbookmarkmenu.h:55
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Mon Feb 20 2023 00:00:00 by doxygen 1.9.6 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

KIO

Skip menu "KIO"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

kdelibs-4.14.38 API Reference

Skip menu "kdelibs-4.14.38 API Reference"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDEWebKit
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUnitConversion
  • KUtils
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver
Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal