2008-04-19 15:41:02 +00:00
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/* vim:expandtab:shiftwidth=4:tabstop=4:
*/
2012-05-21 11:12:37 +00:00
/* This Source Code Form is subject to the terms of the Mozilla Public
* License , v . 2.0 . If a copy of the MPL was not distributed with this
* file , You can obtain one at http : //mozilla.org/MPL/2.0/. */
2008-04-19 15:41:02 +00:00
2010-03-04 21:51:42 +00:00
# include "qmimedata.h"
# include "qwidget.h"
2012-05-03 10:22:33 +00:00
# include "qapplication.h"
# include "qthread.h"
2010-03-04 21:51:42 +00:00
2008-04-19 15:41:02 +00:00
# include "nsDragService.h"
2008-04-19 15:43:32 +00:00
# include "nsISupportsPrimitives.h"
# include "nsXPIDLString.h"
2008-08-28 10:55:40 +00:00
# include "nsIDOMMouseEvent.h"
2008-04-19 15:41:02 +00:00
2008-04-19 15:43:32 +00:00
NS_IMPL_ADDREF_INHERITED ( nsDragService , nsBaseDragService )
NS_IMPL_RELEASE_INHERITED ( nsDragService , nsBaseDragService )
NS_IMPL_QUERY_INTERFACE2 ( nsDragService , nsIDragService , nsIDragSession )
2013-10-08 18:47:49 +00:00
nsDragService : : nsDragService ( ) : mDrag ( nullptr ) , mHiddenWidget ( nullptr )
2008-04-19 15:41:02 +00:00
{
}
nsDragService : : ~ nsDragService ( )
{
/* destructor code */
2008-04-19 15:43:32 +00:00
delete mHiddenWidget ;
}
2008-04-19 15:43:56 +00:00
NS_IMETHODIMP
2012-08-22 15:56:38 +00:00
nsDragService : : SetDropActionType ( uint32_t aActionType )
2008-04-19 15:43:32 +00:00
{
mDropAction = Qt : : IgnoreAction ;
if ( aActionType & DRAGDROP_ACTION_COPY )
{
mDropAction | = Qt : : CopyAction ;
}
if ( aActionType & DRAGDROP_ACTION_MOVE )
{
mDropAction | = Qt : : MoveAction ;
}
if ( aActionType & DRAGDROP_ACTION_LINK )
{
mDropAction | = Qt : : LinkAction ;
}
return NS_OK ;
}
2008-04-19 15:43:56 +00:00
NS_IMETHODIMP
nsDragService : : SetupDragSession (
2008-04-19 15:43:32 +00:00
nsISupportsArray * aTransferables ,
2012-08-22 15:56:38 +00:00
uint32_t aActionType )
2008-04-19 15:43:32 +00:00
{
2012-08-22 15:56:38 +00:00
uint32_t itemCount = 0 ;
2008-04-19 15:43:32 +00:00
aTransferables - > Count ( & itemCount ) ;
if ( 0 = = itemCount )
{
2010-08-26 15:18:06 +00:00
NS_WARNING ( " No items to drag? " ) ;
2008-04-19 15:43:32 +00:00
return NS_ERROR_FAILURE ;
}
if ( 1 ! = itemCount )
{
2010-08-26 15:18:06 +00:00
NS_WARNING ( " Dragging more than one item, cannot do (yet?) " ) ;
2008-04-19 15:43:32 +00:00
return NS_ERROR_NOT_IMPLEMENTED ;
}
SetDropActionType ( aActionType ) ;
QMimeData * mimeData = new QMimeData ;
nsCOMPtr < nsISupports > genericItem ;
aTransferables - > GetElementAt ( 0 , getter_AddRefs ( genericItem ) ) ;
nsCOMPtr < nsITransferable > transferable ( do_QueryInterface ( genericItem ) ) ;
if ( transferable )
{
nsCOMPtr < nsISupportsArray > flavorList ;
transferable - > FlavorsTransferableCanExport ( getter_AddRefs ( flavorList ) ) ;
if ( flavorList )
{
2012-08-22 15:56:38 +00:00
uint32_t flavorCount ;
2008-04-19 15:43:32 +00:00
flavorList - > Count ( & flavorCount ) ;
2012-08-22 15:56:38 +00:00
for ( uint32_t flavor = 0 ; flavor < flavorCount ; flavor + + )
2008-04-19 15:43:32 +00:00
{
nsCOMPtr < nsISupports > genericWrapper ;
flavorList - > GetElementAt ( flavor , getter_AddRefs ( genericWrapper ) ) ;
nsCOMPtr < nsISupportsCString > currentFlavor ;
currentFlavor = do_QueryInterface ( genericWrapper ) ;
if ( currentFlavor )
{
nsCOMPtr < nsISupports > data ;
2012-08-22 15:56:38 +00:00
uint32_t dataLen = 0 ;
2008-04-19 15:43:32 +00:00
nsXPIDLCString flavorStr ;
currentFlavor - > ToString ( getter_Copies ( flavorStr ) ) ;
// Is it some flavor we think we could support?
if ( ! strcmp ( kURLMime , flavorStr . get ( ) )
| | ! strcmp ( kURLDataMime , flavorStr . get ( ) )
| | ! strcmp ( kURLDescriptionMime , flavorStr . get ( ) )
| | ! strcmp ( kHTMLMime , flavorStr . get ( ) )
| | ! strcmp ( kUnicodeMime , flavorStr . get ( ) )
)
{
transferable - > GetTransferData ( flavorStr , getter_AddRefs ( data ) , & dataLen ) ;
nsCOMPtr < nsISupportsString > wideString ;
wideString = do_QueryInterface ( data ) ;
if ( ! wideString )
{
return NS_ERROR_FAILURE ;
}
nsAutoString utf16string ;
wideString - > GetData ( utf16string ) ;
QByteArray ba ( ( const char * ) utf16string . get ( ) , dataLen ) ;
mimeData - > setData ( flavorStr . get ( ) , ba ) ;
}
}
}
}
}
2012-05-03 10:22:33 +00:00
if ( qApp - > thread ( ) ! = QThread : : currentThread ( ) ) {
NS_WARNING ( " Cannot initialize drag session in non main thread " ) ;
return NS_OK ;
}
2011-10-07 15:16:36 +00:00
if ( ! mHiddenWidget ) {
mHiddenWidget = new QWidget ( ) ;
}
2008-04-19 15:43:32 +00:00
mDrag = new QDrag ( mHiddenWidget ) ; // TODO: Better drag source here?
mDrag - > setMimeData ( mimeData ) ;
// mDrag and mimeData SHOULD NOT be destroyed. They are destroyed by QT.
return NS_OK ;
2008-04-19 15:41:02 +00:00
}
/* void invokeDragSession (in nsIDOMNode aDOMNode, in nsISupportsArray aTransferables, in nsIScriptableRegion aRegion, in unsigned long aActionType); */
NS_IMETHODIMP
2008-04-19 15:43:56 +00:00
nsDragService : : InvokeDragSession (
nsIDOMNode * aDOMNode ,
nsISupportsArray * aTransferables ,
nsIScriptableRegion * aRegion ,
2012-08-22 15:56:38 +00:00
uint32_t aActionType )
2008-04-19 15:41:02 +00:00
{
2008-04-19 15:43:32 +00:00
nsBaseDragService : : InvokeDragSession (
aDOMNode ,
aTransferables ,
aRegion ,
aActionType ) ;
SetupDragSession ( aTransferables , aActionType ) ;
return NS_OK ;
}
NS_IMETHODIMP
nsDragService : : ExecuteDrag ( )
{
2012-05-03 10:22:33 +00:00
if ( qApp - > thread ( ) = = QThread : : currentThread ( ) ) {
mDrag - > exec ( mDropAction ) ;
}
2008-04-19 15:43:32 +00:00
return NS_OK ;
2008-04-19 15:41:02 +00:00
}
2012-08-22 15:56:38 +00:00
/* void invokeDragSessionWithImage ( nsIDOMNode DOMNode , nsISupportsArray transferableArray , nsIScriptableRegion region , uint32_t actionType , nsIDOMNode image , int32_t imageX , int32_t imageY , nsIDOMMouseEvent dragEvent ); */
2008-04-19 15:41:02 +00:00
NS_IMETHODIMP
2008-04-19 15:43:32 +00:00
nsDragService : : InvokeDragSessionWithImage (
nsIDOMNode * aDOMNode ,
nsISupportsArray * aTransferables ,
nsIScriptableRegion * aRegion ,
2012-08-22 15:56:38 +00:00
uint32_t aActionType ,
2008-04-19 15:43:32 +00:00
nsIDOMNode * aImage ,
2012-08-22 15:56:38 +00:00
int32_t aImageX ,
int32_t aImageY ,
2008-08-28 10:55:40 +00:00
nsIDOMDragEvent * aDragEvent ,
nsIDOMDataTransfer * aDataTransfer )
2008-04-19 15:41:02 +00:00
{
2008-04-19 15:43:32 +00:00
nsBaseDragService : : InvokeDragSessionWithImage (
aDOMNode , aTransferables ,
aRegion , aActionType ,
aImage ,
aImageX , aImageY ,
2008-08-28 10:55:40 +00:00
aDragEvent ,
aDataTransfer ) ;
2008-04-19 15:43:32 +00:00
SetupDragSession ( aTransferables , aActionType ) ;
// Setup Image, and other stuff
if ( aImage )
{
// Use the custom image
// (aImageX,aImageY) specifies the offset "within the image where
// the cursor would be positioned"
// So, convert the aImage to QPixmap and X and Y to q QPoint
// and then:
// mDrag->setPixmap( pixmap ) or mDrag->setDragCursor( pixmap );
// mDrad->setHotSpot( point );
// TODO: Not implemented yet as this cannot be currently tested
2010-08-26 15:18:06 +00:00
NS_WARNING ( " Support for drag image not implemented " ) ;
2008-04-19 15:43:32 +00:00
}
return ExecuteDrag ( ) ;
2008-04-19 15:41:02 +00:00
}
NS_IMETHODIMP
2008-08-28 10:55:40 +00:00
nsDragService : : InvokeDragSessionWithSelection ( nsISelection * aSelection ,
nsISupportsArray * aTransferableArray ,
2012-08-22 15:56:38 +00:00
uint32_t aActionType ,
2008-08-28 10:55:40 +00:00
nsIDOMDragEvent * aDragEvent ,
nsIDOMDataTransfer * aDataTransfer )
2008-04-19 15:41:02 +00:00
{
2008-04-19 15:43:32 +00:00
nsBaseDragService : : InvokeDragSessionWithSelection (
aSelection ,
2008-08-28 10:55:40 +00:00
aTransferableArray ,
2008-04-19 15:43:32 +00:00
aActionType ,
2008-08-28 10:55:40 +00:00
aDragEvent ,
aDataTransfer ) ;
2008-04-19 15:43:32 +00:00
2008-08-28 10:55:40 +00:00
SetupDragSession ( aTransferableArray , aActionType ) ;
2008-04-19 15:43:32 +00:00
// Setup selection related properties
// There is however nothing that needs to be set
return ExecuteDrag ( ) ;
2008-04-19 15:41:02 +00:00
}
/* nsIDragSession getCurrentSession (); */
NS_IMETHODIMP
nsDragService : : GetCurrentSession ( nsIDragSession * * _retval )
{
return NS_ERROR_NOT_IMPLEMENTED ;
}
/* void startDragSession (); */
NS_IMETHODIMP
nsDragService : : StartDragSession ( )
{
2008-04-19 15:43:56 +00:00
return nsBaseDragService : : StartDragSession ( ) ;
2008-04-19 15:41:02 +00:00
}
2011-09-29 06:19:26 +00:00
/* void endDragSession (in bool aDoneDrag); */
2008-04-19 15:41:02 +00:00
NS_IMETHODIMP
2011-09-29 06:19:26 +00:00
nsDragService : : EndDragSession ( bool aDoneDrag )
2008-04-19 15:41:02 +00:00
{
2008-04-19 15:43:56 +00:00
return nsBaseDragService : : EndDragSession ( aDoneDrag ) ;
2008-04-19 15:41:02 +00:00
}
/* void fireDragEventAtSource (in unsigned long aMsg); */
NS_IMETHODIMP
2012-08-22 15:56:38 +00:00
nsDragService : : FireDragEventAtSource ( uint32_t aMsg )
2008-04-19 15:41:02 +00:00
{
2008-04-19 15:43:56 +00:00
return nsBaseDragService : : FireDragEventAtSource ( aMsg ) ;
2008-04-19 15:41:02 +00:00
}
2008-04-19 15:43:56 +00:00
/* TODO: What is this? */
2008-04-19 15:41:02 +00:00
NS_IMETHODIMP
nsDragService : : Suppress ( )
{
2008-04-19 15:43:56 +00:00
return nsBaseDragService : : Suppress ( ) ;
2008-04-19 15:41:02 +00:00
}
2008-04-19 15:43:56 +00:00
/* TODO: What is this? */
2008-04-19 15:41:02 +00:00
NS_IMETHODIMP
nsDragService : : Unsuppress ( )
{
2008-04-19 15:43:56 +00:00
return nsBaseDragService : : Unsuppress ( ) ;
2008-04-19 15:41:02 +00:00
}
2011-08-23 13:30:39 +00:00
NS_IMETHODIMP
2012-08-22 15:56:38 +00:00
nsDragService : : DragMoved ( int32_t aX , int32_t aY )
2011-08-23 13:30:39 +00:00
{
return nsBaseDragService : : DragMoved ( aX , aY ) ;
}