1
// vim: set tabstop=4 shiftwidth=4 noexpandtab:
3
Gwenview: an image viewer
4
Copyright 2007 Aurélien Gâteau <agateau@kde.org>
6
This program is free software; you can redistribute it and/or
7
modify it under the terms of the GNU General Public License
8
as published by the Free Software Foundation; either version 2
9
of the License, or (at your option) any later version.
11
This program is distributed in the hope that it will be useful,
12
but WITHOUT ANY WARRANTY; without even the implied warranty of
13
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
GNU General Public License for more details.
16
You should have received a copy of the GNU General Public License
17
along with this program; if not, write to the Free Software
18
Foundation, Inc., 51 Franklin Street, Fifth Floor, Cambridge, MA 02110-1301, USA.
22
#include "fileoperations.h"
26
#include <kfiledialog.h>
27
#include <kfileitem.h>
29
#include <kinputdialog.h>
30
#include <kpushbutton.h>
31
#include <kio/copyjob.h>
32
#include <kio/deletejob.h>
34
#include <kio/jobuidelegate.h>
35
#include <kio/netaccess.h>
37
#include <konq_operations.h>
40
#include <lib/document/documentfactory.h>
41
#include <lib/thumbnailloadjob.h>
45
namespace FileOperations {
47
static void copyMoveOrLink(KonqOperations::Operation operation, const KUrl::List& urlList, QWidget* parent) {
48
Q_ASSERT(urlList.count() > 0);
51
KUrl("kfiledialog:///<copyMoveOrLink>"),
52
QString() /* filter */,
54
dialog.setOperationMode(KFileDialog::Saving);
56
case KonqOperations::COPY:
57
dialog.setCaption(i18nc("@title:window", "Copy To"));
58
dialog.okButton()->setText(i18nc("@action:button", "Copy"));
60
case KonqOperations::MOVE:
61
dialog.setCaption(i18nc("@title:window", "Move To"));
62
dialog.okButton()->setText(i18nc("@action:button", "Move"));
64
case KonqOperations::LINK:
65
dialog.setCaption(i18nc("@title:window", "Link To"));
66
dialog.okButton()->setText(i18nc("@action:button", "Link"));
71
if (urlList.count() == 1) {
72
dialog.setMode(KFile::File);
73
dialog.setSelection(urlList[0].fileName());
75
dialog.setMode(KFile::ExistingOnly | KFile::Directory);
81
KUrl destUrl = dialog.selectedUrl();
82
KonqOperations::copy(parent, operation, urlList, destUrl);
86
static void delOrTrash(KonqOperations::Operation operation, const KUrl::List& urlList, QWidget* parent) {
87
Q_ASSERT(urlList.count() > 0);
89
if (!KonqOperations::askDeleteConfirmation(urlList, operation, KonqOperations::DEFAULT_CONFIRMATION, parent)) {
94
// KonqOperations::delOrTrash() handles the confirmation and does not provide
95
// a way to know if the deletion has been accepted.
96
// We need to know about the confirmation so that DocumentFactory can forget
97
// about the deleted urls. That's why we can't use KonqOperations::delOrTrash()
99
case KonqOperations::TRASH:
100
job = KIO::trash(urlList);
103
case KonqOperations::DEL:
104
job = KIO::del(urlList);
108
kWarning() << "Unknown operation" << operation;
112
job->ui()->setWindow(parent);
114
Q_FOREACH(const KUrl& url, urlList) {
115
DocumentFactory::instance()->forget(url);
120
void copyTo(const KUrl::List& urlList, QWidget* parent) {
121
copyMoveOrLink(KonqOperations::COPY, urlList, parent);
125
void moveTo(const KUrl::List& urlList, QWidget* parent) {
126
copyMoveOrLink(KonqOperations::MOVE, urlList, parent);
130
void linkTo(const KUrl::List& urlList, QWidget* parent) {
131
copyMoveOrLink(KonqOperations::LINK, urlList, parent);
135
void trash(const KUrl::List& urlList, QWidget* parent) {
136
delOrTrash(KonqOperations::TRASH, urlList, parent);
140
void del(const KUrl::List& urlList, QWidget* parent) {
141
delOrTrash(KonqOperations::DEL, urlList, parent);
145
void showMenuForDroppedUrls(QWidget* parent, const KUrl::List& urlList, const KUrl& destUrl) {
146
if (urlList.isEmpty()) {
147
kWarning() << "urlList is empty!";
151
if (!destUrl.isValid()) {
152
kWarning() << "destUrl is not valid!";
157
QAction* moveAction = menu.addAction(
160
QAction* copyAction = menu.addAction(
163
QAction* linkAction = menu.addAction(
168
KIcon("process-stop"),
171
QAction* action = menu.exec(QCursor::pos());
174
if (action == moveAction) {
175
job = KIO::move(urlList, destUrl);
176
} else if (action == copyAction) {
177
job = KIO::copy(urlList, destUrl);
178
} else if (action == linkAction) {
179
job = KIO::link(urlList, destUrl);
184
job->ui()->setWindow(parent);
188
void rename(const KUrl& oldUrl, QWidget* parent) {
189
QString name = KInputDialog::getText(
190
i18nc("@title:window", "Rename") /* caption */,
191
i18n("Rename <filename>%1</filename> to:", oldUrl.fileName()) /* label */,
192
oldUrl.fileName() /* value */,
196
if (name.isEmpty() || name == oldUrl.fileName()) {
200
KUrl newUrl = oldUrl;
201
newUrl.setFileName(name);
202
KIO::SimpleJob* job = KIO::rename(oldUrl, newUrl, KIO::HideProgressInfo);
203
if (!KIO::NetAccess::synchronousRun(job, parent)) {
204
job->ui()->showErrorMessage();
207
ThumbnailLoadJob::moveThumbnail(oldUrl, newUrl);