2001-05-10 02:35:00 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
|
|
|
|
*
|
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/. */
|
2001-05-10 02:35:00 +00:00
|
|
|
|
|
|
|
#include "nsClipboardHelper.h"
|
|
|
|
|
|
|
|
// basics
|
|
|
|
#include "nsCOMPtr.h"
|
2002-09-03 23:36:13 +00:00
|
|
|
#include "nsXPCOM.h"
|
2001-05-10 02:35:00 +00:00
|
|
|
#include "nsISupportsPrimitives.h"
|
|
|
|
#include "nsIServiceManager.h"
|
|
|
|
|
|
|
|
// helpers
|
|
|
|
#include "nsIClipboard.h"
|
2013-08-22 06:30:55 +00:00
|
|
|
#include "nsIDocument.h"
|
|
|
|
#include "nsIDOMDocument.h"
|
2001-05-10 02:35:00 +00:00
|
|
|
#include "nsITransferable.h"
|
|
|
|
#include "nsReadableUtils.h"
|
|
|
|
|
2014-04-27 07:06:00 +00:00
|
|
|
NS_IMPL_ISUPPORTS(nsClipboardHelper, nsIClipboardHelper)
|
2001-05-10 02:35:00 +00:00
|
|
|
|
|
|
|
/*****************************************************************************
|
|
|
|
* nsClipboardHelper ctor / dtor
|
|
|
|
*****************************************************************************/
|
|
|
|
|
|
|
|
nsClipboardHelper::nsClipboardHelper()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
nsClipboardHelper::~nsClipboardHelper()
|
|
|
|
{
|
|
|
|
// no members, nothing to destroy
|
2003-09-08 00:31:50 +00:00
|
|
|
}
|
2001-05-10 02:35:00 +00:00
|
|
|
|
|
|
|
/*****************************************************************************
|
|
|
|
* nsIClipboardHelper methods
|
|
|
|
*****************************************************************************/
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2002-03-23 22:26:36 +00:00
|
|
|
nsClipboardHelper::CopyStringToClipboard(const nsAString& aString,
|
2012-08-22 15:56:38 +00:00
|
|
|
int32_t aClipboardID,
|
2012-07-01 18:18:23 +00:00
|
|
|
nsIDOMDocument* aDocument)
|
2001-05-10 02:35:00 +00:00
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
|
|
|
|
// get the clipboard
|
|
|
|
nsCOMPtr<nsIClipboard>
|
|
|
|
clipboard(do_GetService("@mozilla.org/widget/clipboard;1", &rv));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
NS_ENSURE_TRUE(clipboard, NS_ERROR_FAILURE);
|
|
|
|
|
2014-02-28 15:07:30 +00:00
|
|
|
bool clipboardSupported;
|
2001-05-10 02:35:00 +00:00
|
|
|
// don't go any further if they're asking for the selection
|
|
|
|
// clipboard on a platform which doesn't support it (i.e., unix)
|
|
|
|
if (nsIClipboard::kSelectionClipboard == aClipboardID) {
|
|
|
|
rv = clipboard->SupportsSelectionClipboard(&clipboardSupported);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
if (!clipboardSupported)
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
2014-02-28 15:07:30 +00:00
|
|
|
// don't go any further if they're asking for the find clipboard on a platform
|
|
|
|
// which doesn't support it (i.e., non-osx)
|
|
|
|
if (nsIClipboard::kFindClipboard == aClipboardID) {
|
|
|
|
rv = clipboard->SupportsFindClipboard(&clipboardSupported);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
if (!clipboardSupported)
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
2001-05-10 02:35:00 +00:00
|
|
|
// create a transferable for putting data on the clipboard
|
|
|
|
nsCOMPtr<nsITransferable>
|
|
|
|
trans(do_CreateInstance("@mozilla.org/widget/transferable;1", &rv));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
NS_ENSURE_TRUE(trans, NS_ERROR_FAILURE);
|
|
|
|
|
2012-04-17 02:14:01 +00:00
|
|
|
nsCOMPtr<nsIDocument> doc = do_QueryInterface(aDocument);
|
2012-07-30 14:20:58 +00:00
|
|
|
nsILoadContext* loadContext = doc ? doc->GetLoadContext() : nullptr;
|
2012-04-17 02:14:01 +00:00
|
|
|
trans->Init(loadContext);
|
|
|
|
|
2001-05-10 02:35:00 +00:00
|
|
|
// Add the text data flavor to the transferable
|
|
|
|
rv = trans->AddDataFlavor(kUnicodeMime);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
// get wStrings to hold clip data
|
2002-08-06 00:53:19 +00:00
|
|
|
nsCOMPtr<nsISupportsString>
|
|
|
|
data(do_CreateInstance("@mozilla.org/supports-string;1", &rv));
|
2001-05-10 02:35:00 +00:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
NS_ENSURE_TRUE(data, NS_ERROR_FAILURE);
|
|
|
|
|
|
|
|
// populate the string
|
2002-08-26 21:20:34 +00:00
|
|
|
rv = data->SetData(aString);
|
2001-05-10 02:35:00 +00:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
// qi the data object an |nsISupports| so that when the transferable holds
|
|
|
|
// onto it, it will addref the correct interface.
|
|
|
|
nsCOMPtr<nsISupports> genericData(do_QueryInterface(data, &rv));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
NS_ENSURE_TRUE(genericData, NS_ERROR_FAILURE);
|
|
|
|
|
|
|
|
// set the transfer data
|
|
|
|
rv = trans->SetTransferData(kUnicodeMime, genericData,
|
|
|
|
aString.Length() * 2);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
// put the transferable on the clipboard
|
2012-07-30 14:20:58 +00:00
|
|
|
rv = clipboard->SetData(trans, nullptr, aClipboardID);
|
2001-05-10 02:35:00 +00:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-04-17 02:14:01 +00:00
|
|
|
nsClipboardHelper::CopyString(const nsAString& aString, nsIDOMDocument* aDocument)
|
2001-05-10 02:35:00 +00:00
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
|
|
|
|
// copy to the global clipboard. it's bad if this fails in any way.
|
2012-07-01 18:18:23 +00:00
|
|
|
rv = CopyStringToClipboard(aString, nsIClipboard::kGlobalClipboard, aDocument);
|
2001-05-10 02:35:00 +00:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
// unix also needs us to copy to the selection clipboard. this will
|
|
|
|
// fail in CopyStringToClipboard if we're not on a platform that
|
|
|
|
// supports the selection clipboard. (this could have been #ifdef
|
|
|
|
// XP_UNIX, but using the SupportsSelectionClipboard call is the
|
|
|
|
// more correct thing to do.
|
|
|
|
//
|
|
|
|
// if this fails in any way other than "not being unix", we'll get
|
|
|
|
// the assertion we need in CopyStringToClipboard, and we needn't
|
|
|
|
// assert again here.
|
2012-07-01 18:18:23 +00:00
|
|
|
CopyStringToClipboard(aString, nsIClipboard::kSelectionClipboard, aDocument);
|
2001-05-10 02:35:00 +00:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|