]> cloud.milkyroute.net Git - dolphin.git/blob - src/draganddrophelper.cpp
Fixed incorrect grouping of dates. Thanks to Jacopo De Simoi for the patch!
[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 //do not start a new drag until the previous one has been finished
59 //This is a (possibly temporary) fix for bug #187884
60 static bool isDragging = false;
61 if (isDragging) return;
62 isDragging = true;
63
64 QModelIndexList indexes = itemView->selectionModel()->selectedIndexes();
65 if (indexes.count() > 0) {
66 QMimeData *data = itemView->model()->mimeData(indexes);
67 if (data == 0) {
68 return;
69 }
70
71 if (controller != 0) {
72 controller->emitHideToolTip();
73 }
74
75 QDrag* drag = new QDrag(itemView);
76 QPixmap pixmap;
77 if (indexes.count() == 1) {
78 QAbstractProxyModel* proxyModel = static_cast<QAbstractProxyModel*>(itemView->model());
79 KDirModel* dirModel = static_cast<KDirModel*>(proxyModel->sourceModel());
80 const QModelIndex index = proxyModel->mapToSource(indexes.first());
81
82 const KFileItem item = dirModel->itemForIndex(index);
83 pixmap = item.pixmap(KIconLoader::SizeMedium, KIconLoader::SizeMedium);
84 } else {
85 pixmap = KIcon("document-multiple").pixmap(KIconLoader::SizeMedium, KIconLoader::SizeMedium);
86 }
87 drag->setPixmap(pixmap);
88 drag->setMimeData(data);
89
90 m_dragSource = itemView;
91 drag->exec(supportedActions, Qt::IgnoreAction);
92 m_dragSource = 0;
93 }
94 isDragging = false;
95 }
96
97 bool DragAndDropHelper::isDragSource(QAbstractItemView* itemView)
98 {
99 return (m_dragSource != 0) && (m_dragSource == itemView);
100 }
101
102 void DragAndDropHelper::dropUrls(const KFileItem& destItem,
103 const KUrl& destPath,
104 QDropEvent* event,
105 QWidget* widget)
106 {
107 const bool dropToItem = !destItem.isNull() && (destItem.isDir() || destItem.isDesktopFile());
108 const KUrl destination = dropToItem ? destItem.url() : destPath;
109
110 const QMimeData* mimeData = event->mimeData();
111 if (mimeData->hasFormat("application/x-kde-dndextract")) {
112 QString remoteDBusClient = mimeData->data("application/x-kde-dndextract");
113 QDBusMessage message = QDBusMessage::createMethodCall(remoteDBusClient, "/DndExtract",
114 "org.kde.DndExtract", "extractSelectedFilesTo");
115 message.setArguments(QVariantList() << destination.path());
116 QDBusConnection::sessionBus().call(message);
117 } else {
118 const KUrl::List urls = KUrl::List::fromMimeData(event->mimeData());
119 const int urlsCount = urls.count();
120 if (urlsCount == 0) {
121 // TODO: handle dropping of other data
122 } else if ((urlsCount == 1) && (urls.first() == destination)) {
123 emit errorMessage(i18nc("@info:status", "A folder cannot be dropped into itself"));
124 } else if (dropToItem) {
125 KonqOperations::doDrop(destItem, destination, event, widget);
126 } else {
127 KonqOperations::doDrop(KFileItem(), destination, event, widget);
128 }
129 }
130 }
131
132 DragAndDropHelper::DragAndDropHelper()
133 : m_dragSource(0)
134 {
135 }
136
137 #include "draganddrophelper.moc"