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. *
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. *
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 ***************************************************************************/
20 #include "draganddrophelper.h"
21 #include "dolphiniconsview.h"
22 #include "dolphincontroller.h"
24 #include <kdirmodel.h>
25 #include <kfileitem.h>
28 #include <konq_operations.h>
30 #include <QAbstractItemView>
31 #include <QAbstractProxyModel>
35 class DragAndDropHelperSingleton
38 DragAndDropHelper instance
;
40 K_GLOBAL_STATIC(DragAndDropHelperSingleton
, s_dragAndDropHelper
)
42 DragAndDropHelper
& DragAndDropHelper::instance()
44 return s_dragAndDropHelper
->instance
;
47 bool DragAndDropHelper::isMimeDataSupported(const QMimeData
* mimeData
) const
49 return mimeData
->hasUrls() || mimeData
->hasFormat("application/x-kde-dndextract");
52 void DragAndDropHelper::startDrag(QAbstractItemView
* itemView
,
53 Qt::DropActions supportedActions
,
54 DolphinController
* controller
)
56 QModelIndexList indexes
= itemView
->selectionModel()->selectedIndexes();
57 if (indexes
.count() > 0) {
58 QMimeData
*data
= itemView
->model()->mimeData(indexes
);
63 if (controller
!= 0) {
64 controller
->emitHideToolTip();
67 QDrag
* drag
= new QDrag(itemView
);
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());
74 const KFileItem item
= dirModel
->itemForIndex(index
);
75 pixmap
= item
.pixmap(KIconLoader::SizeMedium
, KIconLoader::SizeMedium
);
77 pixmap
= KIcon("document-multiple").pixmap(KIconLoader::SizeMedium
, KIconLoader::SizeMedium
);
79 drag
->setPixmap(pixmap
);
80 drag
->setMimeData(data
);
81 drag
->exec(supportedActions
, Qt::IgnoreAction
);
85 void DragAndDropHelper::dropUrls(const KFileItem
& destItem
,
90 const bool dropToItem
= !destItem
.isNull() && (destItem
.isDir() || destItem
.isDesktopFile());
91 const KUrl destination
= dropToItem
? destItem
.url() : destPath
;
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
);
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
);
112 KonqOperations::doDrop(KFileItem(), destination
, event
, widget
);
117 DragAndDropHelper::DragAndDropHelper()
121 #include "draganddrophelper.moc"