Back to index

lightning-sunbird  0.9+nobinonly
nsClipboardHelper.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
00002  *
00003  * ***** BEGIN LICENSE BLOCK *****
00004  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00005  *
00006  * The contents of this file are subject to the Mozilla Public License Version
00007  * 1.1 (the "License"); you may not use this file except in compliance with
00008  * the License. You may obtain a copy of the License at
00009  * http://www.mozilla.org/MPL/
00010  *
00011  * Software distributed under the License is distributed on an "AS IS" basis,
00012  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00013  * for the specific language governing rights and limitations under the
00014  * License.
00015  *
00016  * The Original Code is the Mozilla browser.
00017  *
00018  * The Initial Developer of the Original Code is
00019  * Netscape Communications Corp.
00020  * Portions created by the Initial Developer are Copyright (C) 2001
00021  * the Initial Developer. All Rights Reserved.
00022  *
00023  * Contributor(s):
00024  *
00025  * Alternatively, the contents of this file may be used under the terms of
00026  * either the GNU General Public License Version 2 or later (the "GPL"), or
00027  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00028  * in which case the provisions of the GPL or the LGPL are applicable instead
00029  * of those above. If you wish to allow use of your version of this file only
00030  * under the terms of either the GPL or the LGPL, and not to allow others to
00031  * use your version of this file under the terms of the MPL, indicate your
00032  * decision by deleting the provisions above and replace them with the notice
00033  * and other provisions required by the GPL or the LGPL. If you do not delete
00034  * the provisions above, a recipient may use your version of this file under
00035  * the terms of any one of the MPL, the GPL or the LGPL.
00036  *
00037  * ***** END LICENSE BLOCK ***** */
00038 
00039 #include "nsClipboardHelper.h"
00040 
00041 // basics
00042 #include "nsCOMPtr.h"
00043 #include "nsXPCOM.h"
00044 #include "nsISupportsPrimitives.h"
00045 #include "nsIServiceManager.h"
00046 
00047 // helpers
00048 #include "nsIClipboard.h"
00049 #include "nsITransferable.h"
00050 #include "nsReadableUtils.h"
00051 
00052 NS_IMPL_ISUPPORTS1(nsClipboardHelper, nsIClipboardHelper)
00053 
00054 /*****************************************************************************
00055  * nsClipboardHelper ctor / dtor
00056  *****************************************************************************/
00057 
00058 nsClipboardHelper::nsClipboardHelper()
00059 {
00060 }
00061 
00062 nsClipboardHelper::~nsClipboardHelper()
00063 {
00064   // no members, nothing to destroy
00065 }
00066 
00067 /*****************************************************************************
00068  * nsIClipboardHelper methods
00069  *****************************************************************************/
00070 
00071 NS_IMETHODIMP
00072 nsClipboardHelper::CopyStringToClipboard(const nsAString& aString,
00073                                          PRInt32 aClipboardID)
00074 {
00075   nsresult rv;
00076 
00077   // get the clipboard
00078   nsCOMPtr<nsIClipboard>
00079     clipboard(do_GetService("@mozilla.org/widget/clipboard;1", &rv));
00080   NS_ENSURE_SUCCESS(rv, rv);
00081   NS_ENSURE_TRUE(clipboard, NS_ERROR_FAILURE);
00082 
00083   // don't go any further if they're asking for the selection
00084   // clipboard on a platform which doesn't support it (i.e., unix)
00085   if (nsIClipboard::kSelectionClipboard == aClipboardID) {
00086     PRBool clipboardSupported;
00087     rv = clipboard->SupportsSelectionClipboard(&clipboardSupported);
00088     NS_ENSURE_SUCCESS(rv, rv);
00089     if (!clipboardSupported)
00090       return NS_ERROR_FAILURE;
00091   }
00092 
00093   // create a transferable for putting data on the clipboard
00094   nsCOMPtr<nsITransferable>
00095     trans(do_CreateInstance("@mozilla.org/widget/transferable;1", &rv));
00096   NS_ENSURE_SUCCESS(rv, rv);
00097   NS_ENSURE_TRUE(trans, NS_ERROR_FAILURE);
00098 
00099   // Add the text data flavor to the transferable
00100   rv = trans->AddDataFlavor(kUnicodeMime);
00101   NS_ENSURE_SUCCESS(rv, rv);
00102 
00103   // get wStrings to hold clip data
00104   nsCOMPtr<nsISupportsString>
00105     data(do_CreateInstance("@mozilla.org/supports-string;1", &rv));
00106   NS_ENSURE_SUCCESS(rv, rv);
00107   NS_ENSURE_TRUE(data, NS_ERROR_FAILURE);
00108 
00109   // populate the string
00110   rv = data->SetData(aString);
00111   NS_ENSURE_SUCCESS(rv, rv);
00112 
00113   // qi the data object an |nsISupports| so that when the transferable holds
00114   // onto it, it will addref the correct interface.
00115   nsCOMPtr<nsISupports> genericData(do_QueryInterface(data, &rv));
00116   NS_ENSURE_SUCCESS(rv, rv);
00117   NS_ENSURE_TRUE(genericData, NS_ERROR_FAILURE);
00118 
00119   // set the transfer data
00120   rv = trans->SetTransferData(kUnicodeMime, genericData,
00121                               aString.Length() * 2);
00122   NS_ENSURE_SUCCESS(rv, rv);
00123 
00124   // put the transferable on the clipboard
00125   rv = clipboard->SetData(trans, nsnull, aClipboardID);
00126   NS_ENSURE_SUCCESS(rv, rv);
00127 
00128   return NS_OK;
00129 }
00130 
00131 NS_IMETHODIMP
00132 nsClipboardHelper::CopyString(const nsAString& aString)
00133 {
00134   nsresult rv;
00135 
00136   // copy to the global clipboard. it's bad if this fails in any way.
00137   rv = CopyStringToClipboard(aString, nsIClipboard::kGlobalClipboard);
00138   NS_ENSURE_SUCCESS(rv, rv);
00139 
00140   // unix also needs us to copy to the selection clipboard. this will
00141   // fail in CopyStringToClipboard if we're not on a platform that
00142   // supports the selection clipboard. (this could have been #ifdef
00143   // XP_UNIX, but using the SupportsSelectionClipboard call is the
00144   // more correct thing to do.
00145   //
00146   // if this fails in any way other than "not being unix", we'll get
00147   // the assertion we need in CopyStringToClipboard, and we needn't
00148   // assert again here.
00149   CopyStringToClipboard(aString, nsIClipboard::kSelectionClipboard);
00150 
00151   return NS_OK;
00152 }