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