~ubuntu-branches/ubuntu/trusty/gwenview/trusty

« back to all changes in this revision

Viewing changes to app/fileoperations.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Rohan Garg
  • Date: 2011-07-20 13:46:34 UTC
  • Revision ID: james.westby@ubuntu.com-20110720134634-92930fdjeed4gdc9
Tags: upstream-4.6.90+repack
Import upstream version 4.6.90+repack

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// vim: set tabstop=4 shiftwidth=4 noexpandtab:
 
2
/*
 
3
Gwenview: an image viewer
 
4
Copyright 2007 Aurélien Gâteau <agateau@kde.org>
 
5
 
 
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.
 
10
 
 
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.
 
15
 
 
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.
 
19
 
 
20
*/
 
21
// Self
 
22
#include "fileoperations.h"
 
23
 
 
24
// KDE
 
25
#include <kdebug.h>
 
26
#include <kfiledialog.h>
 
27
#include <kfileitem.h>
 
28
#include <kmenu.h>
 
29
#include <kinputdialog.h>
 
30
#include <kpushbutton.h>
 
31
#include <kio/copyjob.h>
 
32
#include <kio/deletejob.h>
 
33
#include <kio/job.h>
 
34
#include <kio/jobuidelegate.h>
 
35
#include <kio/netaccess.h>
 
36
#include <klocale.h>
 
37
#include <konq_operations.h>
 
38
 
 
39
// Local
 
40
#include <lib/document/documentfactory.h>
 
41
#include <lib/thumbnailloadjob.h>
 
42
 
 
43
namespace Gwenview {
 
44
 
 
45
namespace FileOperations {
 
46
 
 
47
static void copyMoveOrLink(KonqOperations::Operation operation, const KUrl::List& urlList, QWidget* parent) {
 
48
        Q_ASSERT(urlList.count() > 0);
 
49
 
 
50
        KFileDialog dialog(
 
51
                KUrl("kfiledialog:///<copyMoveOrLink>"),
 
52
                QString() /* filter */,
 
53
                parent);
 
54
        dialog.setOperationMode(KFileDialog::Saving);
 
55
        switch (operation) {
 
56
        case KonqOperations::COPY:
 
57
                dialog.setCaption(i18nc("@title:window", "Copy To"));
 
58
                dialog.okButton()->setText(i18nc("@action:button", "Copy"));
 
59
                break;
 
60
        case KonqOperations::MOVE:
 
61
                dialog.setCaption(i18nc("@title:window", "Move To"));
 
62
                dialog.okButton()->setText(i18nc("@action:button", "Move"));
 
63
                break;
 
64
        case KonqOperations::LINK:
 
65
                dialog.setCaption(i18nc("@title:window", "Link To"));
 
66
                dialog.okButton()->setText(i18nc("@action:button", "Link"));
 
67
                break;
 
68
        default:
 
69
                Q_ASSERT(0);
 
70
        }
 
71
        if (urlList.count() == 1) {
 
72
                dialog.setMode(KFile::File);
 
73
                dialog.setSelection(urlList[0].fileName());
 
74
        } else {
 
75
                dialog.setMode(KFile::ExistingOnly | KFile::Directory);
 
76
        }
 
77
        if (!dialog.exec()) {
 
78
                return;
 
79
        }
 
80
 
 
81
        KUrl destUrl = dialog.selectedUrl();
 
82
        KonqOperations::copy(parent, operation, urlList, destUrl);
 
83
}
 
84
 
 
85
 
 
86
static void delOrTrash(KonqOperations::Operation operation, const KUrl::List& urlList, QWidget* parent) {
 
87
        Q_ASSERT(urlList.count() > 0);
 
88
 
 
89
        if (!KonqOperations::askDeleteConfirmation(urlList, operation, KonqOperations::DEFAULT_CONFIRMATION, parent)) {
 
90
                return;
 
91
        }
 
92
 
 
93
        KIO::Job* job = 0;
 
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()
 
98
        switch (operation) {
 
99
        case KonqOperations::TRASH:
 
100
                job = KIO::trash(urlList);
 
101
                break;
 
102
 
 
103
        case KonqOperations::DEL:
 
104
                job = KIO::del(urlList);
 
105
                break;
 
106
 
 
107
        default:
 
108
                kWarning() << "Unknown operation" << operation;
 
109
                return;
 
110
        }
 
111
        Q_ASSERT(job);
 
112
        job->ui()->setWindow(parent);
 
113
 
 
114
        Q_FOREACH(const KUrl& url, urlList) {
 
115
                DocumentFactory::instance()->forget(url);
 
116
        }
 
117
}
 
118
 
 
119
 
 
120
void copyTo(const KUrl::List& urlList, QWidget* parent) {
 
121
        copyMoveOrLink(KonqOperations::COPY, urlList, parent);
 
122
}
 
123
 
 
124
 
 
125
void moveTo(const KUrl::List& urlList, QWidget* parent) {
 
126
        copyMoveOrLink(KonqOperations::MOVE, urlList, parent);
 
127
}
 
128
 
 
129
 
 
130
void linkTo(const KUrl::List& urlList, QWidget* parent) {
 
131
        copyMoveOrLink(KonqOperations::LINK, urlList, parent);
 
132
}
 
133
 
 
134
 
 
135
void trash(const KUrl::List& urlList, QWidget* parent) {
 
136
        delOrTrash(KonqOperations::TRASH, urlList, parent);
 
137
}
 
138
 
 
139
 
 
140
void del(const KUrl::List& urlList, QWidget* parent) {
 
141
        delOrTrash(KonqOperations::DEL, urlList, parent);
 
142
}
 
143
 
 
144
 
 
145
void showMenuForDroppedUrls(QWidget* parent, const KUrl::List& urlList, const KUrl& destUrl) {
 
146
        if (urlList.isEmpty()) {
 
147
                kWarning() << "urlList is empty!";
 
148
                return;
 
149
        }
 
150
 
 
151
        if (!destUrl.isValid()) {
 
152
                kWarning() << "destUrl is not valid!";
 
153
                return;
 
154
        }
 
155
 
 
156
        KMenu menu(parent);
 
157
        QAction* moveAction = menu.addAction(
 
158
                KIcon("go-jump"),
 
159
                i18n("Move Here"));
 
160
        QAction* copyAction = menu.addAction(
 
161
                KIcon("edit-copy"),
 
162
                i18n("Copy Here"));
 
163
        QAction* linkAction = menu.addAction(
 
164
                KIcon("edit-link"),
 
165
                i18n("Link Here"));
 
166
        menu.addSeparator();
 
167
        menu.addAction(
 
168
                KIcon("process-stop"),
 
169
                i18n("Cancel"));
 
170
 
 
171
        QAction* action = menu.exec(QCursor::pos());
 
172
 
 
173
        KIO::Job* job = 0;
 
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);
 
180
        } else {
 
181
                return;
 
182
        }
 
183
        Q_ASSERT(job);
 
184
        job->ui()->setWindow(parent);
 
185
}
 
186
 
 
187
 
 
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 */,
 
193
                0 /* ok */,
 
194
                parent
 
195
                );
 
196
        if (name.isEmpty() || name == oldUrl.fileName()) {
 
197
                return;
 
198
        }
 
199
 
 
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();
 
205
                return;
 
206
        }
 
207
        ThumbnailLoadJob::moveThumbnail(oldUrl, newUrl);
 
208
}
 
209
 
 
210
 
 
211
} // namespace
 
212
 
 
213
} // namespace