Back to index

lightning-sunbird  0.9+nobinonly
nsDragService.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is mozilla.org code.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 1998
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *   Lars Knoll <knoll@kde.org>
00024  *   Zack Rusin <zack@kde.org>
00025  *   Pierre Phaneuf <pp@ludusdesign.com>
00026  *   Denis Issoupov <denis@macadamian.com>
00027  *   John C. Griggs <johng@corel.com>
00028  *
00029  * Alternatively, the contents of this file may be used under the terms of
00030  * either the GNU General Public License Version 2 or later (the "GPL"), or
00031  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00032  * in which case the provisions of the GPL or the LGPL are applicable instead
00033  * of those above. If you wish to allow use of your version of this file only
00034  * under the terms of either the GPL or the LGPL, and not to allow others to
00035  * use your version of this file under the terms of the MPL, indicate your
00036  * decision by deleting the provisions above and replace them with the notice
00037  * and other provisions required by the GPL or the LGPL. If you do not delete
00038  * the provisions above, a recipient may use your version of this file under
00039  * the terms of any one of the MPL, the GPL or the LGPL.
00040  *
00041  * ***** END LICENSE BLOCK ***** */
00042 #ifdef NDEBUG
00043 #define NO_DEBUG
00044 #endif
00045 
00046 #include "nsDragService.h"
00047 #include "nsIServiceManager.h"
00048 #include "nsXPCOM.h"
00049 #include "nsISupportsPrimitives.h"
00050 #include "nsCOMPtr.h"
00051 #include "nsXPIDLString.h"
00052 #include "nsPrimitiveHelpers.h"
00053 #include "nsMime.h"
00054 #include "nsWidgetsCID.h"
00055 #include "nsString.h"
00056 
00057 // static NS_DEFINE_IID(kIDragServiceIID,   NS_IDRAGSERVICE_IID);
00058 // static NS_DEFINE_IID(kIDragSessionQtIID, NS_IDRAGSESSIONQT_IID);
00059 // static NS_DEFINE_CID(kCDragServiceCID,   NS_DRAGSERVICE_CID);
00060 
00061 NS_IMPL_ADDREF_INHERITED(nsDragService, nsBaseDragService)
00062 NS_IMPL_RELEASE_INHERITED(nsDragService, nsBaseDragService)
00063 NS_IMPL_QUERY_INTERFACE4(nsDragService,
00064                          nsIDragService,
00065                          nsIDragService_1_8_BRANCH,
00066                          nsIDragSession,
00067                          nsIDragSessionQt )
00068 
00069 //-------------------------------------------------------------------------
00070 // static variables
00071 //-------------------------------------------------------------------------
00072 static PRBool gHaveDrag = PR_FALSE;
00073 
00074 //-------------------------------------------------------------------------
00075 //
00076 // DragService constructor
00077 //
00078 //-------------------------------------------------------------------------
00079 nsDragService::nsDragService()
00080 {
00081   // our hidden source widget
00082   mHiddenWidget = new QWidget(0,QWidget::tr("DragDrop"),0);
00083 }
00084 
00085 //-------------------------------------------------------------------------
00086 //
00087 // DragService destructor
00088 //
00089 //-------------------------------------------------------------------------
00090 nsDragService::~nsDragService()
00091 {
00092   delete mHiddenWidget;
00093 }
00094 
00095 //---------------------------------------------------------
00096 NS_IMETHODIMP
00097 nsDragService::InvokeDragSession(nsIDOMNode *aDOMNode,
00098                                  nsISupportsArray *aArrayTransferables,
00099                                  nsIScriptableRegion *aRegion,
00100                                  PRUint32 aActionType)
00101 {
00102   PRUint32 numItemsToDrag = 0;
00103 
00104   nsresult rv = nsBaseDragService::InvokeDragSession(aDOMNode,
00105                                                      aArrayTransferables,
00106                                                      aRegion, aActionType);
00107   NS_ENSURE_SUCCESS(rv, rv);
00108 
00109   // make sure that we have an array of transferables to use
00110   if (!aArrayTransferables) {
00111     return NS_ERROR_INVALID_ARG;
00112   }
00113   // set our reference to the transferables.  this will also addref
00114   // the transferables since we're going to hang onto this beyond the
00115   // length of this call
00116   mSourceDataItems = aArrayTransferables;
00117 
00118   mSourceDataItems->Count(&numItemsToDrag);
00119   if (!numItemsToDrag) {
00120     return NS_ERROR_FAILURE;
00121   }
00122   if (numItemsToDrag > 1) {
00123     return NS_ERROR_FAILURE;
00124   }
00125   nsCOMPtr<nsISupports> genericItem;
00126 
00127   mSourceDataItems->GetElementAt(0,getter_AddRefs(genericItem));
00128 
00129   nsCOMPtr<nsITransferable> transferable(do_QueryInterface(genericItem));
00130 
00131   mDragObject = RegisterDragFlavors(transferable);
00132   gHaveDrag = PR_TRUE;
00133 
00134   if (aActionType == DRAGDROP_ACTION_MOVE)
00135     mDragObject->dragMove();
00136   else
00137     mDragObject->dragCopy();
00138 
00139   gHaveDrag = PR_FALSE;
00140   mDragObject = 0;
00141   return NS_OK;
00142 }
00143 
00144 QDragObject *nsDragService::RegisterDragFlavors(nsITransferable *transferable)
00145 {
00146     nsMimeStore *pMimeStore = new nsMimeStore();
00147     nsCOMPtr<nsISupportsArray> flavorList;
00148 
00149     if (NS_SUCCEEDED(transferable->FlavorsTransferableCanExport(getter_AddRefs(flavorList)))) {
00150         PRUint32 numFlavors;
00151 
00152         flavorList->Count(&numFlavors);
00153 
00154         for (PRUint32 flavorIndex = 0; flavorIndex < numFlavors; ++flavorIndex) {
00155             nsCOMPtr<nsISupports> genericWrapper;
00156 
00157             flavorList->GetElementAt(flavorIndex,getter_AddRefs(genericWrapper));
00158 
00159             nsCOMPtr<nsISupportsCString> currentFlavor(do_QueryInterface(genericWrapper));
00160 
00161             if (currentFlavor) {
00162                 nsXPIDLCString flavorStr;
00163 
00164                 currentFlavor->ToString(getter_Copies(flavorStr));
00165 
00166                 PRUint32   len;
00167                 nsCOMPtr<nsISupports> clip;
00168 
00169                 transferable->GetTransferData(flavorStr,getter_AddRefs(clip),&len);
00170 
00171                 nsCOMPtr<nsISupportsString> wideString;
00172                 wideString = do_QueryInterface(clip);
00173                 if (!wideString)
00174                     continue;
00175 
00176                 nsAutoString ucs2string;
00177                 wideString->GetData(ucs2string);
00178                 QString str = QString::fromUcs2(ucs2string.get());
00179 
00180                 pMimeStore->AddFlavorData(flavorStr,str.utf8());
00181             }
00182         } // foreach flavor in item
00183     } // if valid flavor list
00184 #ifdef NS_DEBUG
00185     else
00186         printf(" DnD ERROR: cannot export any flavor\n");
00187 #endif
00188     return new nsDragObject(pMimeStore,mHiddenWidget);
00189 } // RegisterDragItemsAndFlavors
00190 
00191 NS_IMETHODIMP nsDragService::StartDragSession()
00192 {
00193 #ifdef NS_DEBUG
00194   printf(" DnD: StartDragSession\n");
00195 #endif
00196   return nsBaseDragService::StartDragSession();
00197 }
00198 
00199 NS_IMETHODIMP nsDragService::EndDragSession()
00200 {
00201 #ifdef NS_DEBUG
00202   printf(" DnD: EndDragSession\n");
00203 #endif
00204   mDragObject = 0;
00205   return nsBaseDragService::EndDragSession();
00206 }
00207 
00208 // nsIDragSession
00209 NS_IMETHODIMP nsDragService::SetCanDrop(PRBool aCanDrop)
00210 {
00211   mCanDrop = aCanDrop;
00212   return NS_OK;
00213 }
00214 
00215 NS_IMETHODIMP nsDragService::GetCanDrop(PRBool *aCanDrop)
00216 {
00217   *aCanDrop = mCanDrop;
00218   return NS_OK;
00219 }
00220 
00221 NS_IMETHODIMP nsDragService::GetNumDropItems(PRUint32 *aNumItems)
00222 {
00223   *aNumItems = 1;
00224   return NS_OK;
00225 }
00226 
00227 NS_IMETHODIMP nsDragService::GetData(nsITransferable *aTransferable,
00228                                      PRUint32 aItemIndex)
00229 {
00230   // make sure that we have a transferable
00231   if (!aTransferable)
00232     return NS_ERROR_INVALID_ARG;
00233 
00234   nsresult rv = NS_ERROR_FAILURE;
00235   nsCOMPtr<nsISupportsArray> flavorList;
00236 
00237   rv = aTransferable->FlavorsTransferableCanImport(getter_AddRefs(flavorList));
00238   if (NS_FAILED(rv))
00239     return rv;
00240 
00241   // count the number of flavors
00242   PRUint32 cnt;
00243 
00244   flavorList->Count(&cnt);
00245 
00246   // Now walk down the list of flavors. When we find one that is
00247   // actually present, copy out the data into the transferable in that
00248   // format. SetTransferData() implicitly handles conversions.
00249   for (unsigned int i = 0; i < cnt; ++i) {
00250     nsCAutoString foundFlavor;
00251     nsCOMPtr<nsISupports> genericWrapper;
00252 
00253     flavorList->GetElementAt(i,getter_AddRefs(genericWrapper));
00254 
00255     nsCOMPtr<nsISupportsCString> currentFlavor;
00256 
00257     currentFlavor = do_QueryInterface(genericWrapper);
00258     if (currentFlavor) {
00259       nsXPIDLCString flavorStr;
00260 
00261       currentFlavor->ToString(getter_Copies(flavorStr));
00262       foundFlavor = nsCAutoString(flavorStr);
00263 
00264       if (mDragObject && mDragObject->provides(flavorStr)) {
00265        QByteArray ba = mDragObject->encodedData((const char*)flavorStr);
00266         nsCOMPtr<nsISupports> genericDataWrapper;
00267        PRUint32 len = (PRUint32)ba.count();
00268 
00269         nsPrimitiveHelpers::CreatePrimitiveForData(foundFlavor.get(),
00270                                              (void*)ba.data(),len,
00271                                                    getter_AddRefs(genericDataWrapper));
00272 
00273         aTransferable->SetTransferData(foundFlavor.get(),genericDataWrapper,len);
00274       }
00275     }
00276   }
00277   return NS_OK;
00278 }
00279 
00280 NS_IMETHODIMP nsDragService::IsDataFlavorSupported(const char *aDataFlavor,
00281                                                    PRBool *_retval)
00282 {
00283   if (!_retval)
00284     return NS_ERROR_INVALID_ARG;
00285 
00286   *_retval = PR_FALSE;
00287 
00288   if (mDragObject)
00289      *_retval = mDragObject->provides(aDataFlavor);
00290 
00291 #ifdef NS_DEBUG
00292   if (!*_retval)
00293     printf("nsDragService::IsDataFlavorSupported not provides [%s] \n", aDataFlavor);
00294 #endif
00295   return NS_OK;
00296 }
00297 
00298 NS_IMETHODIMP nsDragService::SetDragReference(QMimeSource* aDragRef)
00299 {
00300    nsMimeStore*  pMimeStore = new nsMimeStore();
00301    int c = 0;
00302    const char* format;
00303 
00304    while ((format = aDragRef->format(c++)) != 0) {
00305      // this is usualy between different processes
00306      // so, we need to copy datafrom one to onother
00307 
00308      QByteArray ba = aDragRef->encodedData(format);
00309      pMimeStore->AddFlavorData(format,ba);
00310    }
00311    mDragObject = new nsDragObject(pMimeStore,mHiddenWidget);
00312    return NS_OK;
00313 }