1 /***************************************************************************
2 * Copyright (C) 2007 by Peter Penz <peter.penz@gmx.at> *
3 * Copyright (C) 2007 by David Faure <faure@kde.org> *
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. *
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. *
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 ***************************************************************************/
21 #include "draganddrophelper.h"
22 #include "dolphiniconsview.h"
23 #include "dolphincontroller.h"
25 #include <kdirmodel.h>
26 #include <kfileitem.h>
30 #include <konq_operations.h>
32 #include <QAbstractItemView>
33 #include <QAbstractProxyModel>
37 class DragAndDropHelperSingleton
40 DragAndDropHelper instance
;
42 K_GLOBAL_STATIC(DragAndDropHelperSingleton
, s_dragAndDropHelper
)
44 DragAndDropHelper
& DragAndDropHelper::instance()
46 return s_dragAndDropHelper
->instance
;
49 bool DragAndDropHelper::isMimeDataSupported(const QMimeData
* mimeData
) const
51 return mimeData
->hasUrls() || mimeData
->hasFormat("application/x-kde-dndextract");
54 void DragAndDropHelper::startDrag(QAbstractItemView
* itemView
,
55 Qt::DropActions supportedActions
,
56 DolphinController
* controller
)
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;
66 QModelIndexList indexes
= itemView
->selectionModel()->selectedIndexes();
67 if (indexes
.count() > 0) {
68 QMimeData
*data
= itemView
->model()->mimeData(indexes
);
73 if (controller
!= 0) {
74 controller
->emitHideToolTip();
77 QDrag
* drag
= new QDrag(itemView
);
79 if (indexes
.count() == 1) {
80 QAbstractProxyModel
* proxyModel
= static_cast<QAbstractProxyModel
*>(itemView
->model());
81 KDirModel
* dirModel
= static_cast<KDirModel
*>(proxyModel
->sourceModel());
82 const QModelIndex index
= proxyModel
->mapToSource(indexes
.first());
84 const KFileItem item
= dirModel
->itemForIndex(index
);
85 pixmap
= item
.pixmap(KIconLoader::SizeMedium
, KIconLoader::SizeMedium
);
87 pixmap
= KIcon("document-multiple").pixmap(KIconLoader::SizeMedium
, KIconLoader::SizeMedium
);
89 drag
->setPixmap(pixmap
);
90 drag
->setMimeData(data
);
92 m_dragSource
= itemView
;
93 drag
->exec(supportedActions
, Qt::IgnoreAction
);
99 bool DragAndDropHelper::isDragSource(QAbstractItemView
* itemView
)
101 return (m_dragSource
!= 0) && (m_dragSource
== itemView
);
104 void DragAndDropHelper::dropUrls(const KFileItem
& destItem
,
105 const KUrl
& destPath
,
109 const bool dropToItem
= !destItem
.isNull() && (destItem
.isDir() || destItem
.isDesktopFile());
110 const KUrl destination
= dropToItem
? destItem
.url() : destPath
;
112 const QMimeData
* mimeData
= event
->mimeData();
113 if (mimeData
->hasFormat("application/x-kde-dndextract")) {
114 QString remoteDBusClient
= mimeData
->data("application/x-kde-dndextract");
115 QDBusMessage message
= QDBusMessage::createMethodCall(remoteDBusClient
, "/DndExtract",
116 "org.kde.DndExtract", "extractSelectedFilesTo");
117 message
.setArguments(QVariantList() << destination
.path());
118 QDBusConnection::sessionBus().call(message
);
120 const KUrl::List urls
= KUrl::List::fromMimeData(event
->mimeData());
121 const int urlsCount
= urls
.count();
122 if (urlsCount
== 0) {
123 // TODO: handle dropping of other data
124 } else if ((urlsCount
== 1) && (urls
.first() == destination
)) {
125 emit
errorMessage(i18nc("@info:status", "A folder cannot be dropped into itself"));
126 } else if (dropToItem
) {
127 KonqOperations::doDrop(destItem
, destination
, event
, widget
);
129 KonqOperations::doDrop(KFileItem(), destination
, event
, widget
);
134 DragAndDropHelper::DragAndDropHelper()
139 #include "draganddrophelper.moc"