]> cloud.milkyroute.net Git - dolphin.git/blob - src/draganddrophelper.cpp
used 18ncp() in a wrong manner: for a single selection a different number of paramete...
[dolphin.git] / src / draganddrophelper.cpp
1 /***************************************************************************
2 * Copyright (C) 2007 by Peter Penz <peter.penz@gmx.at> *
3 * Copyright (C) 2007 by David Faure <faure@kde.org> *
4 * *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
9 * *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
14 * *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
19 ***************************************************************************/
20
21 #include "draganddrophelper.h"
22 #include "dolphiniconsview.h"
23 #include "dolphincontroller.h"
24
25 #include <kdirmodel.h>
26 #include <kfileitem.h>
27 #include <kicon.h>
28 #include <klocale.h>
29 #include <kdebug.h>
30 #include <konq_operations.h>
31
32 #include <QAbstractItemView>
33 #include <QAbstractProxyModel>
34 #include <QtDBus>
35 #include <QDrag>
36
37 class DragAndDropHelperSingleton
38 {
39 public:
40 DragAndDropHelper instance;
41 };
42 K_GLOBAL_STATIC(DragAndDropHelperSingleton, s_dragAndDropHelper)
43
44 DragAndDropHelper& DragAndDropHelper::instance()
45 {
46 return s_dragAndDropHelper->instance;
47 }
48
49 bool DragAndDropHelper::isMimeDataSupported(const QMimeData* mimeData) const
50 {
51 return mimeData->hasUrls() || mimeData->hasFormat("application/x-kde-dndextract");
52 }
53
54 void DragAndDropHelper::startDrag(QAbstractItemView* itemView,
55 Qt::DropActions supportedActions,
56 DolphinController* controller)
57 {
58 QModelIndexList indexes = itemView->selectionModel()->selectedIndexes();
59 if (indexes.count() > 0) {
60 QMimeData *data = itemView->model()->mimeData(indexes);
61 if (data == 0) {
62 return;
63 }
64
65 if (controller != 0) {
66 controller->emitHideToolTip();
67 }
68
69 QDrag* drag = new QDrag(itemView);
70 QPixmap pixmap;
71 if (indexes.count() == 1) {
72 QAbstractProxyModel* proxyModel = static_cast<QAbstractProxyModel*>(itemView->model());
73 KDirModel* dirModel = static_cast<KDirModel*>(proxyModel->sourceModel());
74 const QModelIndex index = proxyModel->mapToSource(indexes.first());
75
76 const KFileItem item = dirModel->itemForIndex(index);
77 pixmap = item.pixmap(KIconLoader::SizeMedium, KIconLoader::SizeMedium);
78 } else {
79 pixmap = KIcon("document-multiple").pixmap(KIconLoader::SizeMedium, KIconLoader::SizeMedium);
80 }
81 drag->setPixmap(pixmap);
82 drag->setMimeData(data);
83 drag->exec(supportedActions, Qt::IgnoreAction);
84 }
85 }
86
87 void DragAndDropHelper::dropUrls(const KFileItem& destItem,
88 const KUrl& destPath,
89 QDropEvent* event,
90 QWidget* widget)
91 {
92 const bool dropToItem = !destItem.isNull() && (destItem.isDir() || destItem.isDesktopFile());
93 const KUrl destination = dropToItem ? destItem.url() : destPath;
94
95 const QMimeData* mimeData = event->mimeData();
96 if (mimeData->hasFormat("application/x-kde-dndextract")) {
97 QString remoteDBusClient = mimeData->data("application/x-kde-dndextract");
98 QDBusMessage message = QDBusMessage::createMethodCall(remoteDBusClient, "/DndExtract",
99 "org.kde.DndExtract", "extractFilesTo");
100 message.setArguments(QVariantList() << destination.path());
101 QDBusConnection::sessionBus().call(message);
102 } else {
103 const KUrl::List urls = KUrl::List::fromMimeData(event->mimeData());
104 const KUrl source = urls.first();
105 const KUrl sourceDir = KUrl(source.directory());
106 const bool singleSelection = (urls.count() == 1);
107
108 if (singleSelection && (source == destination)) {
109 emit errorMessage(i18nc("@info:status", "A folder cannot dropped on to itself"));
110 } else if (sourceDir == destination) {
111 QString msg;
112 if (singleSelection) {
113 msg = i18nc("@info:status",
114 "The dropped item <filename>%1</filename> is already inside "
115 "the folder <filename>%2</filename>", source.fileName(), destination.fileName());
116 } else {
117 msg = i18nc("@info:status",
118 "The dropped items are already inside the folder <filename>%1</filename>",
119 destination.fileName());
120 }
121 emit errorMessage(msg);
122 } else if (dropToItem) {
123 KonqOperations::doDrop(destItem, destination, event, widget);
124 } else {
125 KonqOperations::doDrop(KFileItem(), destination, event, widget);
126 }
127 }
128 }
129
130 DragAndDropHelper::DragAndDropHelper()
131 {
132 }
133
134 #include "draganddrophelper.moc"