Back to index

lightning-sunbird  0.9+nobinonly
nsClipboard.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
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  *   Pierre Phaneuf <pp@ludusdesign.com>
00024  *   Takashi Toyoshima <toyoshim@be-in.org>
00025  *
00026  * Alternatively, the contents of this file may be used under the terms of
00027  * either the GNU General Public License Version 2 or later (the "GPL"), or
00028  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00029  * in which case the provisions of the GPL or the LGPL are applicable instead
00030  * of those above. If you wish to allow use of your version of this file only
00031  * under the terms of either the GPL or the LGPL, and not to allow others to
00032  * use your version of this file under the terms of the MPL, indicate your
00033  * decision by deleting the provisions above and replace them with the notice
00034  * and other provisions required by the GPL or the LGPL. If you do not delete
00035  * the provisions above, a recipient may use your version of this file under
00036  * the terms of any one of the MPL, the GPL or the LGPL.
00037  *
00038  * ***** END LICENSE BLOCK ***** */
00039 
00040 #include "nsClipboard.h"
00041 #include "nsCOMPtr.h"
00042 #include "nsITransferable.h"
00043 #include "nsWidgetsCID.h"
00044 #include "nsXPIDLString.h"
00045 #include "nsPrimitiveHelpers.h"
00046 #include "nsXPCOM.h"
00047 #include "nsISupportsPrimitives.h"
00048 #include "nsString.h"
00049 
00050 #include <View.h>
00051 #include <Clipboard.h>
00052 #include <Message.h>
00053 
00054 // The class statics:
00055 BView *nsClipboard::sView = 0;
00056 
00057 #if defined(DEBUG_akkana) || defined(DEBUG_mcafee) || defined(DEBUG_toyoshim)
00058 #  define DEBUG_CLIPBOARD
00059 #endif
00060  
00061 
00062 //-------------------------------------------------------------------------
00063 //
00064 // nsClipboard constructor
00065 //
00066 //-------------------------------------------------------------------------
00067 nsClipboard::nsClipboard() : nsBaseClipboard()
00068 {
00069 #ifdef DEBUG_CLIPBOARD
00070   printf("  nsClipboard::nsClipboard()\n");
00071 #endif /* DEBUG_CLIPBOARD */
00072 
00073   mIgnoreEmptyNotification = PR_FALSE;
00074   mClipboardOwner = nsnull;
00075   mTransferable   = nsnull;
00076 }
00077 
00078 //-------------------------------------------------------------------------
00079 //
00080 // nsClipboard destructor
00081 //
00082 //-------------------------------------------------------------------------
00083 nsClipboard::~nsClipboard()
00084 {
00085 #ifdef DEBUG_CLIPBOARD
00086   printf("  nsClipboard::~nsClipboard()\n");  
00087 #endif /* DEBUG_CLIPBOARD */
00088 }
00089 
00090 void nsClipboard::SetTopLevelView(BView *v)
00091 {
00092   // Don't set up any more event handlers if we're being called twice
00093   // for the same toplevel widget
00094   if (sView == v)
00095     return;
00096 
00097   if (sView != 0 && sView->Window() != 0)
00098     return;
00099 
00100   if(v == 0 || v->Window() == 0)
00101   {
00102 #ifdef DEBUG_CLIPBOARD
00103     printf("  nsClipboard::SetTopLevelView: widget passed in is null or has no window!\n");
00104 #endif /* DEBUG_CLIPBOARD */
00105     return;
00106   }
00107 
00108 #ifdef DEBUG_CLIPBOARD
00109   printf("  nsClipboard::SetTopLevelView\n");
00110 #endif /* DEBUG_CLIPBOARD */
00111 }
00112 
00113 //
00114 // The copy routine
00115 //
00116 NS_IMETHODIMP nsClipboard::SetNativeClipboardData(PRInt32 aWhichClipboard)
00117 {
00118   mIgnoreEmptyNotification = PR_TRUE;
00119 
00120 #ifdef DEBUG_CLIPBOARD
00121   printf("  nsClipboard::SetNativeClipboardData()\n");
00122 #endif /* DEBUG_CLIPBOARD */
00123 
00124   // make sure we have a good transferable
00125   if (nsnull == mTransferable) {
00126 #ifdef DEBUG_CLIPBOARD
00127     printf("  SetNativeClipboardData: no transferable!\n");
00128 #endif /* DEBUG_CLIPBOARD */
00129     return NS_ERROR_FAILURE;
00130   }
00131 
00132   // lock the native clipboard
00133   if (!be_clipboard->Lock())
00134     return NS_ERROR_FAILURE;
00135 
00136   // clear the native clipboard
00137   nsresult rv = NS_OK;
00138   if (B_OK == be_clipboard->Clear()) {
00139     // set data to the native clipboard
00140     if (BMessage *msg = be_clipboard->Data()) {
00141       // Get the transferable list of data flavors
00142       nsCOMPtr<nsISupportsArray> dfList;
00143       mTransferable->FlavorsTransferableCanExport(getter_AddRefs(dfList));
00144 
00145       // Walk through flavors that contain data and register them
00146       // into the BMessage as supported flavors
00147       PRUint32 i;
00148       PRUint32 cnt;
00149       dfList->Count(&cnt);
00150       for (i = 0; i < cnt && rv == NS_OK; i++) {
00151         nsCOMPtr<nsISupports> genericFlavor;
00152         dfList->GetElementAt(i, getter_AddRefs(genericFlavor));
00153         nsCOMPtr<nsISupportsCString> currentFlavor (do_QueryInterface(genericFlavor));
00154         if (currentFlavor) {
00155           nsXPIDLCString flavorStr;
00156           currentFlavor->ToString(getter_Copies(flavorStr));
00157 
00158 #ifdef DEBUG_CLIPBOARD
00159           printf("nsClipboard: %d/%d = %s\n", i, cnt, (const char *)flavorStr);
00160 #endif /* DEBUG_CLIPBOARD */
00161           if (0 == strncmp(flavorStr, "text/", 5)) {
00162             // [NS] text/ * => [Be] text/ *
00163             void *data = nsnull;
00164             PRUint32 dataSize = 0;
00165             nsCOMPtr<nsISupports> genericDataWrapper;
00166             rv = mTransferable->GetTransferData(flavorStr, getter_AddRefs(genericDataWrapper), &dataSize);
00167             nsPrimitiveHelpers::CreateDataFromPrimitive(flavorStr, genericDataWrapper, &data, dataSize);
00168 #ifdef DEBUG_CLIPBOARD
00169             if (NS_FAILED(rv))
00170               printf("nsClipboard: Error getting data from transferable\n");
00171 #endif /* DEBUG_CLIPBOARD */
00172             if (dataSize && data != nsnull) {
00173               NS_ConvertUCS2toUTF8 cv((const PRUnichar *)data, (PRUint32)dataSize / 2);
00174               const char *utf8Str = cv.get();
00175               uint32 utf8Len = strlen(utf8Str);
00176 #ifdef DEBUG_CLIPBOARD
00177               if (0 == strcmp(flavorStr, kUnicodeMime))
00178                 printf(" => [%s]%s\n", kTextMime, utf8Str);
00179               else
00180                 printf(" => [%s]%s\n", (const char *)flavorStr, utf8Str);
00181 #endif /* DEBUG_CLIPBOARD */
00182               status_t rc;
00183               if (0 == strcmp(flavorStr, kUnicodeMime)) {
00184                 // [NS] text/unicode => [Be] text/plain
00185                 rc = msg->AddData(kTextMime, B_MIME_TYPE, (void *)utf8Str, utf8Len);
00186               } else {
00187                 // [NS] text/ * => [Be] text/ *
00188                 rc = msg->AddData((const char *)flavorStr, B_MIME_TYPE, (void *)utf8Str, utf8Len);
00189               }
00190               if (rc != B_OK)
00191                 rv = NS_ERROR_FAILURE;
00192             } else {
00193 #ifdef DEBUG_CLIPBOARD
00194               printf("nsClipboard: Error null data from transferable\n");
00195 #endif /* DEBUG_CLIPBOARD */
00196                 // not fatal. force to continue...
00197                 rv = NS_OK;
00198             }
00199           } else {
00200             // [NS] * / * => [Be] * / *
00201             void *data = nsnull;
00202             PRUint32 dataSize = 0;
00203             nsCOMPtr<nsISupports> genericDataWrapper;
00204             rv = mTransferable->GetTransferData(flavorStr, getter_AddRefs(genericDataWrapper), &dataSize);
00205             nsPrimitiveHelpers::CreateDataFromPrimitive(flavorStr, genericDataWrapper, &data, dataSize);
00206 #ifdef DEBUG_CLIPBOARD
00207             if (NS_FAILED(rv))
00208               printf("nsClipboard: Error getting data from transferable\n");
00209 #endif /* DEBUG_CLIPBOARD */
00210             if (dataSize && data != nsnull) {
00211 #ifdef DEBUG_CLIPBOARD
00212               printf("[%s](binary)\n", (const char *)flavorStr);
00213 #endif /* DEBUG_CLIPBOARD */
00214               if (B_OK != msg->AddData((const char *)flavorStr, B_MIME_TYPE, data, dataSize))
00215                 rv = NS_ERROR_FAILURE;
00216             }
00217           }
00218         } else {
00219 #ifdef DEBUG_CLIPBOARD
00220           printf("nsClipboard: Error getting flavor\n");
00221 #endif /* DEBUG_CLIPBOARD */
00222           rv = NS_ERROR_FAILURE;
00223         }
00224       } /* for */
00225     } else {
00226       rv = NS_ERROR_FAILURE;
00227     }
00228   } else {
00229     rv = NS_ERROR_FAILURE;
00230   }
00231   if (B_OK != be_clipboard->Commit())
00232     rv = NS_ERROR_FAILURE;
00233   be_clipboard->Unlock();
00234 
00235   mIgnoreEmptyNotification = PR_FALSE;
00236 
00237   return rv;
00238 }
00239 
00240 
00241 //
00242 // The blocking Paste routine
00243 //
00244 NS_IMETHODIMP
00245 nsClipboard::GetNativeClipboardData(nsITransferable * aTransferable, PRInt32 aWhichClipboard )
00246 {
00247 #ifdef DEBUG_CLIPBOARD
00248   printf("  nsClipboard::GetNativeClipboardData()\n");
00249 #endif /* DEBUG_CLIPBOARD */
00250 
00251   // make sure we have a good transferable
00252   if (nsnull == aTransferable) {
00253     printf("  GetNativeClipboardData: Transferable is null!\n");
00254     return NS_ERROR_FAILURE;
00255   }
00256 
00257   // get flavor list
00258   nsresult rv;
00259   nsCOMPtr<nsISupportsArray> flavorList;
00260   rv = aTransferable->FlavorsTransferableCanImport(getter_AddRefs(flavorList));
00261   if (NS_FAILED(rv))
00262     return NS_ERROR_FAILURE;
00263 
00264   // get native clipboard data
00265   if (!be_clipboard->Lock())
00266     return NS_ERROR_FAILURE;
00267 
00268   BMessage *msg = be_clipboard->Data();
00269   if (!msg)
00270     return NS_ERROR_FAILURE;  
00271 #ifdef DEBUG_CLIPBOARD
00272   msg->PrintToStream();
00273 #endif /* DEBUG_CLIPBOARD */
00274 
00275   PRUint32 cnt;
00276   flavorList->Count(&cnt);
00277   for (PRUint32 i = 0; i < cnt; i++) {
00278     nsCOMPtr<nsISupports> genericFlavor;
00279     flavorList->GetElementAt(i, getter_AddRefs(genericFlavor));
00280     nsCOMPtr<nsISupportsCString> currentFlavor(do_QueryInterface(genericFlavor));
00281     if (currentFlavor) {
00282       nsXPIDLCString flavorStr;
00283       currentFlavor->ToString(getter_Copies(flavorStr));
00284 
00285 #ifdef DEBUG_CLIPBOARD
00286       printf("nsClipboard: %d/%d = %s\n", i, cnt, (const char *)flavorStr);
00287 #endif /* DEBUG_CLIPBOARD */
00288       const void *data;
00289       ssize_t size;
00290       if (0 == strncmp(flavorStr, "text/", 5)) {
00291         // [Be] text/ * => [NS] text/ *
00292         status_t rc;
00293         if (0 == strcmp(flavorStr, kUnicodeMime))
00294           rc = msg->FindData(kTextMime, B_MIME_TYPE, &data, &size);
00295         else
00296           rc = msg->FindData(flavorStr, B_MIME_TYPE, &data, &size);
00297         if (rc != B_OK || !data || !size) {
00298 #ifdef DEBUG_CLIPBOARD
00299           printf("nsClipboard: not found in BMessage\n");
00300 #endif /* DEBUG_CLIPBOARD */
00301         } else {
00302           NS_ConvertUTF8toUTF16 ucs2Str((const char *)data, (PRUint32)size);
00303           nsCOMPtr<nsISupports> genericDataWrapper;
00304           nsPrimitiveHelpers::CreatePrimitiveForData(flavorStr, (void *)ucs2Str.get(), ucs2Str.Length() * 2, getter_AddRefs(genericDataWrapper));
00305           rv = aTransferable->SetTransferData(flavorStr, genericDataWrapper, ucs2Str.Length() * 2);
00306         }
00307       } else {
00308         // [Be] * / * => [NS] * / *
00309         if (B_OK != msg->FindData(flavorStr, B_MIME_TYPE, &data, &size)) {
00310 #ifdef DEBUG_CLIPBOARD
00311           printf("nsClipboard: not found in BMessage\n");
00312 #endif /* DEBUG_CLIPBOARD */
00313         } else {
00314           nsCOMPtr<nsISupports> genericDataWrapper;
00315           nsPrimitiveHelpers::CreatePrimitiveForData(flavorStr, (void *)data, (PRUint32)size, getter_AddRefs(genericDataWrapper));
00316           rv = aTransferable->SetTransferData(flavorStr, genericDataWrapper, size);
00317         }
00318       }
00319 #ifdef DEBUG_CLIPBOARD
00320       if (NS_FAILED(rv))
00321         printf("nsClipboard: Error SetTransferData\n");
00322 #endif /* DEBUG_CLIPBOARD */
00323     } else {
00324       rv = NS_ERROR_FAILURE;
00325 #ifdef DEBUG_CLIPBOARD
00326       printf("nsClipboard: Error gerring flavor");
00327 #endif /* DEBUG_CLIPBOARD */
00328     }
00329     if (rv != NS_OK)
00330       break;
00331   } /* for */
00332 
00333   be_clipboard->Unlock();
00334 
00335   return rv;
00336 }