1 : /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 : *
3 : * ***** BEGIN LICENSE BLOCK *****
4 : * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 : *
6 : * The contents of this file are subject to the Mozilla Public License Version
7 : * 1.1 (the "License"); you may not use this file except in compliance with
8 : * the License. You may obtain a copy of the License at
9 : * http://www.mozilla.org/MPL/
10 : *
11 : * Software distributed under the License is distributed on an "AS IS" basis,
12 : * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13 : * for the specific language governing rights and limitations under the
14 : * License.
15 : *
16 : * The Original Code is the Mozilla browser.
17 : *
18 : * The Initial Developer of the Original Code is
19 : * Netscape Communications Corp.
20 : * Portions created by the Initial Developer are Copyright (C) 2001
21 : * the Initial Developer. All Rights Reserved.
22 : *
23 : * Contributor(s):
24 : *
25 : * Alternatively, the contents of this file may be used under the terms of
26 : * either the GNU General Public License Version 2 or later (the "GPL"), or
27 : * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28 : * in which case the provisions of the GPL or the LGPL are applicable instead
29 : * of those above. If you wish to allow use of your version of this file only
30 : * under the terms of either the GPL or the LGPL, and not to allow others to
31 : * use your version of this file under the terms of the MPL, indicate your
32 : * decision by deleting the provisions above and replace them with the notice
33 : * and other provisions required by the GPL or the LGPL. If you do not delete
34 : * the provisions above, a recipient may use your version of this file under
35 : * the terms of any one of the MPL, the GPL or the LGPL.
36 : *
37 : * ***** END LICENSE BLOCK ***** */
38 :
39 : #include "nsClipboardHelper.h"
40 :
41 : // basics
42 : #include "nsCOMPtr.h"
43 : #include "nsXPCOM.h"
44 : #include "nsISupportsPrimitives.h"
45 : #include "nsIServiceManager.h"
46 :
47 : // helpers
48 : #include "nsIClipboard.h"
49 : #include "nsITransferable.h"
50 : #include "nsReadableUtils.h"
51 :
52 0 : NS_IMPL_ISUPPORTS1(nsClipboardHelper, nsIClipboardHelper)
53 :
54 : /*****************************************************************************
55 : * nsClipboardHelper ctor / dtor
56 : *****************************************************************************/
57 :
58 0 : nsClipboardHelper::nsClipboardHelper()
59 : {
60 0 : }
61 :
62 0 : nsClipboardHelper::~nsClipboardHelper()
63 : {
64 : // no members, nothing to destroy
65 0 : }
66 :
67 : /*****************************************************************************
68 : * nsIClipboardHelper methods
69 : *****************************************************************************/
70 :
71 : NS_IMETHODIMP
72 0 : nsClipboardHelper::CopyStringToClipboard(const nsAString& aString,
73 : PRInt32 aClipboardID)
74 : {
75 : nsresult rv;
76 :
77 : // get the clipboard
78 : nsCOMPtr<nsIClipboard>
79 0 : clipboard(do_GetService("@mozilla.org/widget/clipboard;1", &rv));
80 0 : NS_ENSURE_SUCCESS(rv, rv);
81 0 : NS_ENSURE_TRUE(clipboard, NS_ERROR_FAILURE);
82 :
83 : // don't go any further if they're asking for the selection
84 : // clipboard on a platform which doesn't support it (i.e., unix)
85 0 : if (nsIClipboard::kSelectionClipboard == aClipboardID) {
86 : bool clipboardSupported;
87 0 : rv = clipboard->SupportsSelectionClipboard(&clipboardSupported);
88 0 : NS_ENSURE_SUCCESS(rv, rv);
89 0 : if (!clipboardSupported)
90 0 : return NS_ERROR_FAILURE;
91 : }
92 :
93 : // create a transferable for putting data on the clipboard
94 : nsCOMPtr<nsITransferable>
95 0 : trans(do_CreateInstance("@mozilla.org/widget/transferable;1", &rv));
96 0 : NS_ENSURE_SUCCESS(rv, rv);
97 0 : NS_ENSURE_TRUE(trans, NS_ERROR_FAILURE);
98 :
99 : // Add the text data flavor to the transferable
100 0 : rv = trans->AddDataFlavor(kUnicodeMime);
101 0 : NS_ENSURE_SUCCESS(rv, rv);
102 :
103 : // get wStrings to hold clip data
104 : nsCOMPtr<nsISupportsString>
105 0 : data(do_CreateInstance("@mozilla.org/supports-string;1", &rv));
106 0 : NS_ENSURE_SUCCESS(rv, rv);
107 0 : NS_ENSURE_TRUE(data, NS_ERROR_FAILURE);
108 :
109 : // populate the string
110 0 : rv = data->SetData(aString);
111 0 : NS_ENSURE_SUCCESS(rv, rv);
112 :
113 : // qi the data object an |nsISupports| so that when the transferable holds
114 : // onto it, it will addref the correct interface.
115 0 : nsCOMPtr<nsISupports> genericData(do_QueryInterface(data, &rv));
116 0 : NS_ENSURE_SUCCESS(rv, rv);
117 0 : NS_ENSURE_TRUE(genericData, NS_ERROR_FAILURE);
118 :
119 : // set the transfer data
120 0 : rv = trans->SetTransferData(kUnicodeMime, genericData,
121 0 : aString.Length() * 2);
122 0 : NS_ENSURE_SUCCESS(rv, rv);
123 :
124 : // put the transferable on the clipboard
125 0 : rv = clipboard->SetData(trans, nsnull, aClipboardID);
126 0 : NS_ENSURE_SUCCESS(rv, rv);
127 :
128 0 : return NS_OK;
129 : }
130 :
131 : NS_IMETHODIMP
132 0 : nsClipboardHelper::CopyString(const nsAString& aString)
133 : {
134 : nsresult rv;
135 :
136 : // copy to the global clipboard. it's bad if this fails in any way.
137 0 : rv = CopyStringToClipboard(aString, nsIClipboard::kGlobalClipboard);
138 0 : NS_ENSURE_SUCCESS(rv, rv);
139 :
140 : // unix also needs us to copy to the selection clipboard. this will
141 : // fail in CopyStringToClipboard if we're not on a platform that
142 : // supports the selection clipboard. (this could have been #ifdef
143 : // XP_UNIX, but using the SupportsSelectionClipboard call is the
144 : // more correct thing to do.
145 : //
146 : // if this fails in any way other than "not being unix", we'll get
147 : // the assertion we need in CopyStringToClipboard, and we needn't
148 : // assert again here.
149 0 : CopyStringToClipboard(aString, nsIClipboard::kSelectionClipboard);
150 :
151 0 : return NS_OK;
152 : }
|