SVN_SILENT made messages (.desktop file)
[kdegraphics.git] / gwenview / app / fileoperations.cpp
blobbb9d53dcc110566ea490d060f4b386c8cc6f5a70
1 // vim: set tabstop=4 shiftwidth=4 noexpandtab:
2 /*
3 Gwenview: an image viewer
4 Copyright 2007 Aurélien Gâteau <aurelien.gateau@free.fr>
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.
21 // Self
22 #include "fileoperations.h"
24 // KDE
25 #include <kdebug.h>
26 #include <kfiledialog.h>
27 #include <kfileitem.h>
28 #include <kinputdialog.h>
29 #include <kpushbutton.h>
30 #include <kio/copyjob.h>
31 #include <kio/deletejob.h>
32 #include <kio/job.h>
33 #include <kio/jobuidelegate.h>
34 #include <klocale.h>
36 namespace Gwenview {
38 namespace FileOperations {
41 // Taken from KonqOperations::askDeleteConfirmation
42 // Forked because KonqOperations is in kdebase/apps/lib/konq, and kdegraphics
43 // can't depend on kdebase.
44 enum Operation { TRASH, DEL, COPY, MOVE, LINK, EMPTYTRASH, STAT, MKDIR, RESTORE, UNKNOWN };
45 enum ConfirmationType { DEFAULT_CONFIRMATION, SKIP_CONFIRMATION, FORCE_CONFIRMATION };
46 static bool askDeleteConfirmation( const KUrl::List & selectedUrls, Operation operation, ConfirmationType confirmation, QWidget* widget )
48 KIO::JobUiDelegate::DeletionType deletionType = operation == DEL ? KIO::JobUiDelegate::Delete : KIO::JobUiDelegate::Trash;
49 KIO::JobUiDelegate::ConfirmationType confirmationType = confirmation == FORCE_CONFIRMATION ? KIO::JobUiDelegate::ForceConfirmation : KIO::JobUiDelegate::DefaultConfirmation;
50 KIO::JobUiDelegate uiDelegate;
51 uiDelegate.setWindow(widget);
52 return uiDelegate.askDeleteConfirmation(selectedUrls, deletionType, confirmationType);
56 static void copyMoveOrLink(Operation operation, const KUrl::List& urlList, QWidget* parent) {
57 Q_ASSERT(urlList.count() > 0);
59 KFileDialog dialog(
60 KUrl("kfiledialog:///<copyMoveOrLink>"),
61 QString() /* filter */,
62 parent);
63 dialog.setOperationMode(KFileDialog::Saving);
64 switch (operation) {
65 case COPY:
66 dialog.setCaption(i18n("Copy To"));
67 dialog.okButton()->setText(i18nc("@action:button", "Copy"));
68 break;
69 case MOVE:
70 dialog.setCaption(i18n("Move To"));
71 dialog.okButton()->setText(i18nc("@action:button", "Move"));
72 break;
73 case LINK:
74 dialog.setCaption(i18n("Link To"));
75 dialog.okButton()->setText(i18nc("@action:button", "Link"));
76 break;
77 default:
78 Q_ASSERT(0);
80 if (urlList.count() == 1) {
81 dialog.setMode(KFile::File);
82 dialog.setSelection(urlList[0].fileName());
83 } else {
84 dialog.setMode(KFile::ExistingOnly | KFile::Directory);
86 if (!dialog.exec()) {
87 return;
90 KUrl destUrl = dialog.selectedUrl();
91 switch (operation) {
92 case COPY:
93 KIO::copy(urlList, destUrl);
94 break;
96 case MOVE:
97 KIO::move(urlList, destUrl);
98 break;
100 case LINK:
101 KIO::link(urlList, destUrl);
102 break;
104 default:
105 Q_ASSERT(0);
110 static void delOrTrash(Operation operation, const KUrl::List& urlList, QWidget* parent) {
111 Q_ASSERT(urlList.count() > 0);
113 if (!askDeleteConfirmation(urlList, operation, DEFAULT_CONFIRMATION, parent)) {
114 return;
117 switch (operation) {
118 case TRASH:
119 KIO::trash(urlList);
120 break;
122 case DEL:
123 KIO::del(urlList);
124 break;
126 default:
127 kWarning() << "Unknown operation " << operation ;
128 break;
132 void copyTo(const KUrl::List& urlList, QWidget* parent) {
133 copyMoveOrLink(COPY, urlList, parent);
137 void moveTo(const KUrl::List& urlList, QWidget* parent) {
138 copyMoveOrLink(MOVE, urlList, parent);
142 void linkTo(const KUrl::List& urlList, QWidget* parent) {
143 copyMoveOrLink(LINK, urlList, parent);
147 void trash(const KUrl::List& urlList, QWidget* parent) {
148 delOrTrash(TRASH, urlList, parent);
152 void del(const KUrl::List& urlList, QWidget* parent) {
153 delOrTrash(DEL, urlList, parent);
157 void createFolder(const KUrl& parentUrl, QWidget* parent) {
158 bool ok;
159 QString name = KInputDialog::getText(
160 i18n("Create Folder"),
161 i18n("Enter the name of the folder to create:"),
162 QString(),
163 &ok,
164 parent);
166 if (!ok) {
167 return;
170 KUrl url = parentUrl;
171 url.addPath(name);
173 KIO::mkdir(url);
177 } // namespace
179 } // namespace