Line data Source code
1 : /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 : *
3 : * This Source Code Form is subject to the terms of the Mozilla Public
4 : * License, v. 2.0. If a copy of the MPL was not distributed with this
5 : * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 :
7 : #include "nsClipboardHelper.h"
8 :
9 : // basics
10 : #include "nsCOMPtr.h"
11 : #include "nsXPCOM.h"
12 : #include "nsISupportsPrimitives.h"
13 : #include "nsIServiceManager.h"
14 :
15 : // helpers
16 : #include "nsIClipboard.h"
17 : #include "nsIDocument.h"
18 : #include "nsIDOMDocument.h"
19 : #include "nsITransferable.h"
20 : #include "nsReadableUtils.h"
21 :
22 0 : NS_IMPL_ISUPPORTS(nsClipboardHelper, nsIClipboardHelper)
23 :
24 : /*****************************************************************************
25 : * nsClipboardHelper ctor / dtor
26 : *****************************************************************************/
27 :
28 0 : nsClipboardHelper::nsClipboardHelper()
29 : {
30 0 : }
31 :
32 0 : nsClipboardHelper::~nsClipboardHelper()
33 : {
34 : // no members, nothing to destroy
35 0 : }
36 :
37 : /*****************************************************************************
38 : * nsIClipboardHelper methods
39 : *****************************************************************************/
40 :
41 : NS_IMETHODIMP
42 0 : nsClipboardHelper::CopyStringToClipboard(const nsAString& aString,
43 : int32_t aClipboardID)
44 : {
45 : nsresult rv;
46 :
47 : // get the clipboard
48 : nsCOMPtr<nsIClipboard>
49 0 : clipboard(do_GetService("@mozilla.org/widget/clipboard;1", &rv));
50 0 : NS_ENSURE_SUCCESS(rv, rv);
51 0 : NS_ENSURE_TRUE(clipboard, NS_ERROR_FAILURE);
52 :
53 : bool clipboardSupported;
54 : // don't go any further if they're asking for the selection
55 : // clipboard on a platform which doesn't support it (i.e., unix)
56 0 : if (nsIClipboard::kSelectionClipboard == aClipboardID) {
57 0 : rv = clipboard->SupportsSelectionClipboard(&clipboardSupported);
58 0 : NS_ENSURE_SUCCESS(rv, rv);
59 0 : if (!clipboardSupported)
60 0 : return NS_ERROR_FAILURE;
61 : }
62 :
63 : // don't go any further if they're asking for the find clipboard on a platform
64 : // which doesn't support it (i.e., non-osx)
65 0 : if (nsIClipboard::kFindClipboard == aClipboardID) {
66 0 : rv = clipboard->SupportsFindClipboard(&clipboardSupported);
67 0 : NS_ENSURE_SUCCESS(rv, rv);
68 0 : if (!clipboardSupported)
69 0 : return NS_ERROR_FAILURE;
70 : }
71 :
72 : // create a transferable for putting data on the clipboard
73 : nsCOMPtr<nsITransferable>
74 0 : trans(do_CreateInstance("@mozilla.org/widget/transferable;1", &rv));
75 0 : NS_ENSURE_SUCCESS(rv, rv);
76 0 : NS_ENSURE_TRUE(trans, NS_ERROR_FAILURE);
77 :
78 0 : trans->Init(nullptr);
79 :
80 : // Add the text data flavor to the transferable
81 0 : rv = trans->AddDataFlavor(kUnicodeMime);
82 0 : NS_ENSURE_SUCCESS(rv, rv);
83 :
84 : // get wStrings to hold clip data
85 : nsCOMPtr<nsISupportsString>
86 0 : data(do_CreateInstance("@mozilla.org/supports-string;1", &rv));
87 0 : NS_ENSURE_SUCCESS(rv, rv);
88 0 : NS_ENSURE_TRUE(data, NS_ERROR_FAILURE);
89 :
90 : // populate the string
91 0 : rv = data->SetData(aString);
92 0 : NS_ENSURE_SUCCESS(rv, rv);
93 :
94 : // qi the data object an |nsISupports| so that when the transferable holds
95 : // onto it, it will addref the correct interface.
96 0 : nsCOMPtr<nsISupports> genericData(do_QueryInterface(data, &rv));
97 0 : NS_ENSURE_SUCCESS(rv, rv);
98 0 : NS_ENSURE_TRUE(genericData, NS_ERROR_FAILURE);
99 :
100 : // set the transfer data
101 0 : rv = trans->SetTransferData(kUnicodeMime, genericData,
102 0 : aString.Length() * 2);
103 0 : NS_ENSURE_SUCCESS(rv, rv);
104 :
105 : // put the transferable on the clipboard
106 0 : rv = clipboard->SetData(trans, nullptr, aClipboardID);
107 0 : NS_ENSURE_SUCCESS(rv, rv);
108 :
109 0 : return NS_OK;
110 : }
111 :
112 : NS_IMETHODIMP
113 0 : nsClipboardHelper::CopyString(const nsAString& aString)
114 : {
115 : nsresult rv;
116 :
117 : // copy to the global clipboard. it's bad if this fails in any way.
118 0 : rv = CopyStringToClipboard(aString, nsIClipboard::kGlobalClipboard);
119 0 : NS_ENSURE_SUCCESS(rv, rv);
120 :
121 : // unix also needs us to copy to the selection clipboard. this will
122 : // fail in CopyStringToClipboard if we're not on a platform that
123 : // supports the selection clipboard. (this could have been #ifdef
124 : // XP_UNIX, but using the SupportsSelectionClipboard call is the
125 : // more correct thing to do.
126 : //
127 : // if this fails in any way other than "not being unix", we'll get
128 : // the assertion we need in CopyStringToClipboard, and we needn't
129 : // assert again here.
130 0 : CopyStringToClipboard(aString, nsIClipboard::kSelectionClipboard);
131 :
132 0 : return NS_OK;
133 : }
|