1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /* vim:expandtab:shiftwidth=4:tabstop=4:
4 /* This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
10 #include "qapplication.h"
13 #include "nsDragService.h"
14 #include "nsISupportsPrimitives.h"
15 #include "nsXPIDLString.h"
16 #include "nsIDOMMouseEvent.h"
18 NS_IMPL_ADDREF_INHERITED(nsDragService
, nsBaseDragService
)
19 NS_IMPL_RELEASE_INHERITED(nsDragService
, nsBaseDragService
)
20 NS_IMPL_QUERY_INTERFACE2(nsDragService
, nsIDragService
, nsIDragSession
)
22 nsDragService::nsDragService() : mDrag(NULL
), mHiddenWidget(NULL
)
26 nsDragService::~nsDragService()
33 nsDragService::SetDropActionType( uint32_t aActionType
)
35 mDropAction
= Qt::IgnoreAction
;
37 if (aActionType
& DRAGDROP_ACTION_COPY
)
39 mDropAction
|= Qt::CopyAction
;
41 if (aActionType
& DRAGDROP_ACTION_MOVE
)
43 mDropAction
|= Qt::MoveAction
;
45 if (aActionType
& DRAGDROP_ACTION_LINK
)
47 mDropAction
|= Qt::LinkAction
;
54 nsDragService::SetupDragSession(
55 nsISupportsArray
*aTransferables
,
58 uint32_t itemCount
= 0;
59 aTransferables
->Count(&itemCount
);
62 NS_WARNING("No items to drag?");
63 return NS_ERROR_FAILURE
;
68 NS_WARNING("Dragging more than one item, cannot do (yet?)");
69 return NS_ERROR_NOT_IMPLEMENTED
;
72 SetDropActionType(aActionType
);
74 QMimeData
*mimeData
= new QMimeData
;
76 nsCOMPtr
<nsISupports
> genericItem
;
77 aTransferables
->GetElementAt(0, getter_AddRefs(genericItem
));
78 nsCOMPtr
<nsITransferable
> transferable(do_QueryInterface(genericItem
));
82 nsCOMPtr
<nsISupportsArray
> flavorList
;
83 transferable
->FlavorsTransferableCanExport(getter_AddRefs(flavorList
));
88 flavorList
->Count( &flavorCount
);
90 for (uint32_t flavor
=0; flavor
< flavorCount
; flavor
++)
92 nsCOMPtr
<nsISupports
> genericWrapper
;
93 flavorList
->GetElementAt(flavor
, getter_AddRefs(genericWrapper
));
94 nsCOMPtr
<nsISupportsCString
> currentFlavor
;
95 currentFlavor
= do_QueryInterface(genericWrapper
);
99 nsCOMPtr
<nsISupports
> data
;
100 uint32_t dataLen
= 0;
101 nsXPIDLCString flavorStr
;
102 currentFlavor
->ToString(getter_Copies(flavorStr
));
104 // Is it some flavor we think we could support?
105 if (!strcmp(kURLMime
, flavorStr
.get())
106 || !strcmp(kURLDataMime
, flavorStr
.get())
107 || !strcmp(kURLDescriptionMime
, flavorStr
.get())
108 || !strcmp(kHTMLMime
, flavorStr
.get())
109 || !strcmp(kUnicodeMime
, flavorStr
.get())
112 transferable
->GetTransferData(flavorStr
,getter_AddRefs(data
),&dataLen
);
114 nsCOMPtr
<nsISupportsString
> wideString
;
115 wideString
= do_QueryInterface(data
);
118 return NS_ERROR_FAILURE
;
121 nsAutoString utf16string
;
122 wideString
->GetData(utf16string
);
123 QByteArray
ba((const char*) utf16string
.get(), dataLen
);
125 mimeData
->setData(flavorStr
.get(), ba
);
132 if (qApp
->thread() != QThread::currentThread()) {
133 NS_WARNING("Cannot initialize drag session in non main thread");
137 if (!mHiddenWidget
) {
138 mHiddenWidget
= new QWidget();
140 mDrag
= new QDrag( mHiddenWidget
); // TODO: Better drag source here?
141 mDrag
->setMimeData(mimeData
);
143 // mDrag and mimeData SHOULD NOT be destroyed. They are destroyed by QT.
148 /* void invokeDragSession (in nsIDOMNode aDOMNode, in nsISupportsArray aTransferables, in nsIScriptableRegion aRegion, in unsigned long aActionType); */
150 nsDragService::InvokeDragSession(
151 nsIDOMNode
*aDOMNode
,
152 nsISupportsArray
*aTransferables
,
153 nsIScriptableRegion
*aRegion
,
154 uint32_t aActionType
)
156 nsBaseDragService::InvokeDragSession(
162 SetupDragSession( aTransferables
, aActionType
);
168 nsDragService::ExecuteDrag()
170 if (qApp
->thread() == QThread::currentThread()) {
171 mDrag
->exec(mDropAction
);
177 /* void invokeDragSessionWithImage ( nsIDOMNode DOMNode , nsISupportsArray transferableArray , nsIScriptableRegion region , uint32_t actionType , nsIDOMNode image , int32_t imageX , int32_t imageY , nsIDOMMouseEvent dragEvent ); */
179 nsDragService::InvokeDragSessionWithImage(
180 nsIDOMNode
*aDOMNode
,
181 nsISupportsArray
*aTransferables
,
182 nsIScriptableRegion
* aRegion
,
183 uint32_t aActionType
,
187 nsIDOMDragEvent
* aDragEvent
,
188 nsIDOMDataTransfer
* aDataTransfer
)
190 nsBaseDragService::InvokeDragSessionWithImage(
191 aDOMNode
, aTransferables
,
192 aRegion
, aActionType
,
198 SetupDragSession( aTransferables
, aActionType
);
200 // Setup Image, and other stuff
203 // Use the custom image
204 // (aImageX,aImageY) specifies the offset "within the image where
205 // the cursor would be positioned"
207 // So, convert the aImage to QPixmap and X and Y to q QPoint
209 // mDrag->setPixmap( pixmap ) or mDrag->setDragCursor( pixmap );
210 // mDrad->setHotSpot( point );
211 // TODO: Not implemented yet as this cannot be currently tested
212 NS_WARNING("Support for drag image not implemented");
215 return ExecuteDrag();
219 nsDragService::InvokeDragSessionWithSelection(nsISelection
* aSelection
,
220 nsISupportsArray
* aTransferableArray
,
221 uint32_t aActionType
,
222 nsIDOMDragEvent
* aDragEvent
,
223 nsIDOMDataTransfer
* aDataTransfer
)
225 nsBaseDragService::InvokeDragSessionWithSelection(
232 SetupDragSession( aTransferableArray
, aActionType
);
234 // Setup selection related properties
235 // There is however nothing that needs to be set
237 return ExecuteDrag();
240 /* nsIDragSession getCurrentSession (); */
242 nsDragService::GetCurrentSession(nsIDragSession
**_retval
)
244 return NS_ERROR_NOT_IMPLEMENTED
;
247 /* void startDragSession (); */
249 nsDragService::StartDragSession()
251 return nsBaseDragService::StartDragSession();
254 /* void endDragSession (in bool aDoneDrag); */
256 nsDragService::EndDragSession(bool aDoneDrag
)
258 return nsBaseDragService::EndDragSession(aDoneDrag
);
261 /* void fireDragEventAtSource (in unsigned long aMsg); */
263 nsDragService::FireDragEventAtSource(uint32_t aMsg
)
265 return nsBaseDragService::FireDragEventAtSource(aMsg
);
268 /* TODO: What is this? */
270 nsDragService::Suppress()
272 return nsBaseDragService::Suppress();
275 /* TODO: What is this? */
277 nsDragService::Unsuppress()
279 return nsBaseDragService::Unsuppress();
283 nsDragService::DragMoved(int32_t aX
, int32_t aY
)
285 return nsBaseDragService::DragMoved(aX
, aY
);