Back to index

lightning-sunbird  0.9+nobinonly
nsClipboard.cpp
Go to the documentation of this file.
00001 /* ***** BEGIN LICENSE BLOCK *****
00002  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00003  *
00004  * The contents of this file are subject to the Mozilla Public License Version
00005  * 1.1 (the "License"); you may not use this file except in compliance with
00006  * the License. You may obtain a copy of the License at
00007  * http://www.mozilla.org/MPL/
00008  *
00009  * Software distributed under the License is distributed on an "AS IS" basis,
00010  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00011  * for the specific language governing rights and limitations under the
00012  * License.
00013  *
00014  * The Original Code is mozilla.org code.
00015  *
00016  * The Initial Developer of the Original Code is
00017  * Netscape Communications Corporation.
00018  * Portions created by the Initial Developer are Copyright (C) 1998
00019  * the Initial Developer. All Rights Reserved.
00020  *
00021  * Contributor(s):
00022  *   Lars Knoll <knoll@kde.org>
00023  *   Zack Rusin <zack@kde.org>
00024  *   Denis Issoupov <denis@macadamian.com>
00025  *   John C. Griggs <johng@corel.com>
00026  *   Dan Rosen <dr@netscape.com>
00027  *
00028  * Alternatively, the contents of this file may be used under the terms of
00029  * either the GNU General Public License Version 2 or later (the "GPL"), or
00030  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00031  * in which case the provisions of the GPL or the LGPL are applicable instead
00032  * of those above. If you wish to allow use of your version of this file only
00033  * under the terms of either the GPL or the LGPL, and not to allow others to
00034  * use your version of this file under the terms of the MPL, indicate your
00035  * decision by deleting the provisions above and replace them with the notice
00036  * and other provisions required by the GPL or the LGPL. If you do not delete
00037  * the provisions above, a recipient may use your version of this file under
00038  * the terms of any one of the MPL, the GPL or the LGPL.
00039  *
00040  * ***** END LICENSE BLOCK ***** */
00041 
00042 #include "nsClipboard.h"
00043 #include "nsMime.h"
00044 #include "nsCOMPtr.h"
00045 #include "nsCRT.h"
00046 #include "nsString.h"
00047 #include "nsISupportsArray.h"
00048 #include "nsXPCOM.h"
00049 #include "nsISupportsPrimitives.h"
00050 #include "nsXPIDLString.h"
00051 #include "nsPrimitiveHelpers.h"
00052 #include "nsIComponentManager.h"
00053 #include "nsWidgetsCID.h"
00054 
00055 #include <qapplication.h>
00056 #include <qclipboard.h>
00057 #include <qdragobject.h>
00058 
00059 NS_IMPL_ISUPPORTS1(nsClipboard, nsIClipboard)
00060 
00061 //-------------------------------------------------------------------------
00062 //
00063 // nsClipboard constructor
00064 //
00065 //-------------------------------------------------------------------------
00066 nsClipboard::nsClipboard()
00067     : nsIClipboard(),
00068       mIgnoreEmptyNotification(PR_FALSE),
00069       mSelectionOwner(nsnull),
00070       mGlobalOwner(nsnull),
00071       mSelectionTransferable(nsnull),
00072       mGlobalTransferable(nsnull)
00073 {
00074     qDebug("###############################################################s");
00075 }
00076 
00077 //-------------------------------------------------------------------------
00078 //
00079 // nsClipboard destructor
00080 //
00081 //-------------------------------------------------------------------------
00082 nsClipboard::~nsClipboard()
00083 {
00084 }
00085 
00086 
00087 NS_IMETHODIMP
00088 nsClipboard::SetNativeClipboardData(PRInt32 aWhichClipboard)
00089 {
00090     qDebug("SetNativeClipboardData");
00091     mIgnoreEmptyNotification = PR_TRUE;
00092 
00093     nsCOMPtr<nsITransferable> transferable(
00094         getter_AddRefs(GetTransferable(aWhichClipboard)));
00095 
00096     // make sure we have a good transferable
00097     if (nsnull == transferable) {
00098         qDebug("nsClipboard::SetNativeClipboardData(): no transferable!\n");
00099         return NS_ERROR_FAILURE;
00100     }
00101     // get flavor list that includes all flavors that can be written (including
00102     // ones obtained through conversion)
00103     nsCOMPtr<nsISupportsArray> flavorList;
00104     nsresult errCode = transferable->FlavorsTransferableCanExport(
00105         getter_AddRefs(flavorList));
00106 
00107     if (NS_FAILED(errCode)) {
00108         qDebug("nsClipboard::SetNativeClipboardData(): no FlavorsTransferable !\n");
00109         return NS_ERROR_FAILURE;
00110     }
00111     QClipboard *cb = QApplication::clipboard();
00112     nsMimeStore *mimeStore =  new nsMimeStore();
00113     PRUint32 cnt;
00114 
00115     flavorList->Count(&cnt);
00116     for (PRUint32 i = 0; i < cnt; ++i) {
00117         nsCOMPtr<nsISupports> genericFlavor;
00118 
00119         flavorList->GetElementAt(i,getter_AddRefs(genericFlavor));
00120 
00121         nsCOMPtr<nsISupportsCString> currentFlavor(do_QueryInterface(genericFlavor));
00122 
00123         if (currentFlavor) {
00124             nsXPIDLCString flavorStr;
00125 
00126             currentFlavor->ToString(getter_Copies(flavorStr));
00127 
00128             // add these types as selection targets
00129             PRUint32   len;
00130             nsCOMPtr<nsISupports> clip;
00131 
00132             transferable->GetTransferData(flavorStr,getter_AddRefs(clip),&len);
00133 
00134             nsCOMPtr<nsISupportsString> wideString;
00135             wideString = do_QueryInterface(clip);
00136             if (!wideString)
00137                 return NS_ERROR_FAILURE;
00138 
00139             nsAutoString ucs2string;
00140             wideString->GetData(ucs2string);
00141             QString str = QString::fromUcs2(ucs2string.get());
00142             qDebug("HERE %s '%s'", flavorStr.get(), str.latin1());
00143 
00144             mimeStore->AddFlavorData(flavorStr,str.utf8());
00145         }
00146     }
00147     cb->setData(mimeStore);
00148     mIgnoreEmptyNotification = PR_FALSE;
00149     return NS_OK;
00150 }
00151 
00152 NS_IMETHODIMP
00153 nsClipboard::GetNativeClipboardData(nsITransferable *aTransferable,
00154                                     PRInt32 aWhichClipboard)
00155 {
00156     qDebug("GetNativeClipboardData");
00157     // make sure we have a good transferable
00158     if (nsnull == aTransferable) {
00159         qDebug("  GetNativeClipboardData: Transferable is null!\n");
00160         return NS_ERROR_FAILURE;
00161     }
00162     // get flavor list that includes all acceptable flavors (including
00163     // ones obtained through conversion)
00164     nsCOMPtr<nsISupportsArray> flavorList;
00165     nsresult errCode = aTransferable->FlavorsTransferableCanImport(
00166         getter_AddRefs(flavorList));
00167 
00168     if (NS_FAILED(errCode)) {
00169         qDebug("nsClipboard::GetNativeClipboardData(): no FlavorsTransferable %i !\n",
00170                errCode);
00171         return NS_ERROR_FAILURE;
00172     }
00173     QClipboard *cb = QApplication::clipboard();
00174     QMimeSource *ms = cb->data();
00175 
00176     // Walk through flavors and see which flavor matches the one being pasted:
00177     PRUint32 cnt;
00178 
00179     flavorList->Count(&cnt);
00180 
00181     nsCAutoString foundFlavor;
00182     for (PRUint32 i = 0; i < cnt; ++i) {
00183         nsCOMPtr<nsISupports> genericFlavor;
00184 
00185         flavorList->GetElementAt(i,getter_AddRefs(genericFlavor));
00186         nsCOMPtr<nsISupportsCString> currentFlavor(do_QueryInterface(
00187                                                        genericFlavor));
00188 
00189         if (currentFlavor) {
00190             nsXPIDLCString flavorStr;
00191 
00192             currentFlavor->ToString(getter_Copies(flavorStr));
00193             foundFlavor = nsCAutoString(flavorStr);
00194 
00195             if (ms->provides((const char*)flavorStr)) {
00196                 QByteArray ba = ms->encodedData((const char*)flavorStr);
00197                 nsCOMPtr<nsISupports> genericDataWrapper;
00198                 PRUint32 len = (PRUint32)ba.count();
00199 
00200                 nsPrimitiveHelpers::CreatePrimitiveForData(
00201                     foundFlavor.get(),
00202                     (void*)ba.data(),len,
00203                     getter_AddRefs(genericDataWrapper));
00204 
00205                 aTransferable->SetTransferData(foundFlavor.get(),
00206                                                genericDataWrapper,len);
00207             }
00208         }
00209     }
00210     return NS_OK;
00211 }
00212 
00213 /* inline */
00214 nsITransferable *
00215 nsClipboard::GetTransferable(PRInt32 aWhichClipboard)
00216 {
00217     qDebug("GetTransferable");
00218     nsITransferable *retval;
00219 
00220     if (aWhichClipboard == kSelectionClipboard)
00221         retval = mSelectionTransferable.get();
00222     else
00223         retval = mGlobalTransferable.get();
00224 
00225     return retval;
00226 }
00227 
00228 //-------------------------------------------------------------------------
00229 NS_IMETHODIMP
00230 nsClipboard::HasDataMatchingFlavors(nsISupportsArray *aFlavorList,
00231                                     PRInt32 aWhichClipboard,
00232                                     PRBool           *_retval)
00233 {
00234     qDebug("HasDataMatchingFlavors");
00235     *_retval = PR_FALSE;
00236     if (aWhichClipboard != kGlobalClipboard)
00237         return NS_OK;
00238 
00239     QClipboard *cb = QApplication::clipboard();
00240     QMimeSource *ms = cb->data();
00241     PRUint32 cnt;
00242 
00243     aFlavorList->Count(&cnt);
00244     for (PRUint32 i = 0;i < cnt; ++i) {
00245         nsCOMPtr<nsISupports> genericFlavor;
00246 
00247         aFlavorList->GetElementAt(i,getter_AddRefs(genericFlavor));
00248 
00249         nsCOMPtr<nsISupportsCString> currentFlavor(do_QueryInterface(genericFlavor));
00250         if (currentFlavor) {
00251             nsXPIDLCString flavorStr;
00252 
00253             currentFlavor->ToString(getter_Copies(flavorStr));
00254 
00255             if (strcmp(flavorStr,kTextMime) == 0)
00256                 NS_WARNING("DO NOT USE THE text/plain DATA FLAVOR ANY MORE. USE text/unicode INSTEAD");
00257 
00258             if (ms->provides((const char*)flavorStr)) {
00259                 *_retval = PR_TRUE;
00260                 qDebug("GetFormat %s\n",(const char*)flavorStr);
00261                 break;
00262             }
00263         }
00264     }
00265     return NS_OK;
00266 }
00267 
00271 NS_IMETHODIMP
00272 nsClipboard::SetData(nsITransferable *aTransferable,
00273                      nsIClipboardOwner *anOwner,
00274                      PRInt32 aWhichClipboard)
00275 {
00276     qDebug("SetData");
00277     if ((aTransferable == mGlobalTransferable.get()
00278          && anOwner == mGlobalOwner.get()
00279          && aWhichClipboard == kGlobalClipboard)
00280         || (aTransferable == mSelectionTransferable.get()
00281             && anOwner == mSelectionOwner.get()
00282             && aWhichClipboard == kSelectionClipboard)) {
00283         return NS_OK;
00284     }
00285     EmptyClipboard(aWhichClipboard);
00286 
00287     switch (aWhichClipboard) {
00288     case kSelectionClipboard:
00289         mSelectionOwner = anOwner;
00290         mSelectionTransferable = aTransferable;
00291         break;
00292 
00293     case kGlobalClipboard:
00294         mGlobalOwner = anOwner;
00295         mGlobalTransferable = aTransferable;
00296         break;
00297     }
00298     QApplication::clipboard()->clear();
00299     return SetNativeClipboardData(aWhichClipboard);
00300 }
00301 
00305 NS_IMETHODIMP
00306 nsClipboard::GetData(nsITransferable *aTransferable,PRInt32 aWhichClipboard)
00307 {
00308     qDebug("GetData");
00309     if (nsnull != aTransferable) {
00310         return GetNativeClipboardData(aTransferable,aWhichClipboard);
00311     } else {
00312         qDebug("  nsClipboard::GetData(), aTransferable is NULL.\n");
00313     }
00314     return NS_ERROR_FAILURE;
00315 }
00316 
00317 NS_IMETHODIMP
00318 nsClipboard::EmptyClipboard(PRInt32 aWhichClipboard)
00319 {
00320     qDebug("EmptyClipoard");
00321     if (mIgnoreEmptyNotification) {
00322         return NS_OK;
00323     }
00324     switch(aWhichClipboard) {
00325     case kSelectionClipboard:
00326         if (mSelectionOwner) {
00327             mSelectionOwner->LosingOwnership(mSelectionTransferable);
00328             mSelectionOwner = nsnull;
00329         }
00330         mSelectionTransferable = nsnull;
00331         break;
00332 
00333     case kGlobalClipboard:
00334         if (mGlobalOwner) {
00335             mGlobalOwner->LosingOwnership(mGlobalTransferable);
00336             mGlobalOwner = nsnull;
00337         }
00338         mGlobalTransferable = nsnull;
00339         break;
00340     }
00341     return NS_OK;
00342 }
00343 
00344 NS_IMETHODIMP
00345 nsClipboard::SupportsSelectionClipboard(PRBool *_retval)
00346 {
00347     qDebug("SuppotsSelectionClipboard");
00348     NS_ENSURE_ARG_POINTER(_retval);
00349 
00350     *_retval = PR_TRUE; // we support the selection clipboard on unix.
00351     return NS_OK;
00352 }