]> cloud.milkyroute.net Git - dolphin.git/blob - src/draganddrophelper.cpp
Refactor the folder expansion system. Main effect: instead of having a list of m_exp...
[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
84 m_dragSource = itemView;
85 drag->exec(supportedActions, Qt::IgnoreAction);
86 m_dragSource = 0;
87 }
88 }
89
90 bool DragAndDropHelper::isDragSource(QAbstractItemView* itemView)
91 {
92 return (m_dragSource != 0) && (m_dragSource == itemView);
93 }
94
95 void DragAndDropHelper::dropUrls(const KFileItem& destItem,
96 const KUrl& destPath,
97 QDropEvent* event,
98 QWidget* widget)
99 {
100 const bool dropToItem = !destItem.isNull() && (destItem.isDir() || destItem.isDesktopFile());
101 const KUrl destination = dropToItem ? destItem.url() : destPath;
102
103 const QMimeData* mimeData = event->mimeData();
104 if (mimeData->hasFormat("application/x-kde-dndextract")) {
105 QString remoteDBusClient = mimeData->data("application/x-kde-dndextract");
106 QDBusMessage message = QDBusMessage::createMethodCall(remoteDBusClient, "/DndExtract",
107 "org.kde.DndExtract", "extractSelectedFilesTo");
108 message.setArguments(QVariantList() << destination.path());
109 QDBusConnection::sessionBus().call(message);
110 } else {
111 const KUrl::List urls = KUrl::List::fromMimeData(event->mimeData());
112 const int urlsCount = urls.count();
113 if (urlsCount == 0) {
114 // TODO: handle dropping of other data
115 } else if ((urlsCount == 1) && (urls.first() == destination)) {
116 emit errorMessage(i18nc("@info:status", "A folder cannot be dropped into itself"));
117 } else if (dropToItem) {
118 KonqOperations::doDrop(destItem, destination, event, widget);
119 } else {
120 KonqOperations::doDrop(KFileItem(), destination, event, widget);
121 }
122 }
123 }
124
125 DragAndDropHelper::DragAndDropHelper()
126 : m_dragSource(0)
127 {
128 }
129
130 #include "draganddrophelper.moc"