1 // vim: set tabstop=4 shiftwidth=4 noexpandtab:
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.
22 #include "fileoperations.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>
33 #include <kio/jobuidelegate.h>
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);
60 KUrl("kfiledialog:///<copyMoveOrLink>"),
61 QString() /* filter */,
63 dialog
.setOperationMode(KFileDialog::Saving
);
66 dialog
.setCaption(i18n("Copy To"));
67 dialog
.okButton()->setText(i18nc("@action:button", "Copy"));
70 dialog
.setCaption(i18n("Move To"));
71 dialog
.okButton()->setText(i18nc("@action:button", "Move"));
74 dialog
.setCaption(i18n("Link To"));
75 dialog
.okButton()->setText(i18nc("@action:button", "Link"));
80 if (urlList
.count() == 1) {
81 dialog
.setMode(KFile::File
);
82 dialog
.setSelection(urlList
[0].fileName());
84 dialog
.setMode(KFile::ExistingOnly
| KFile::Directory
);
90 KUrl destUrl
= dialog
.selectedUrl();
93 KIO::copy(urlList
, destUrl
);
97 KIO::move(urlList
, destUrl
);
101 KIO::link(urlList
, destUrl
);
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
)) {
127 kWarning() << "Unknown operation " << operation
;
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
) {
159 QString name
= KInputDialog::getText(
160 i18n("Create Folder"),
161 i18n("Enter the name of the folder to create:"),
170 KUrl url
= parentUrl
;