66
|
1 |
/*
|
|
2 |
* Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
|
|
3 |
* All rights reserved.
|
|
4 |
* This component and the accompanying materials are made available
|
|
5 |
* under the terms of "Eclipse Public License v1.0"
|
|
6 |
* which accompanies this distribution, and is available
|
|
7 |
* at the URL "http://www.eclipse.org/legal/epl-v10.html".
|
|
8 |
*
|
|
9 |
* Initial Contributors:
|
|
10 |
* Nokia Corporation - initial contribution.
|
|
11 |
*
|
|
12 |
* Contributors:
|
|
13 |
*
|
|
14 |
* Description: JNI implementation of Clipboard class
|
|
15 |
*
|
|
16 |
*/
|
|
17 |
|
|
18 |
|
|
19 |
// INTERNAL INCLUDES
|
|
20 |
#include "com_nokia_mid_ui_Clipboard.h"
|
|
21 |
|
|
22 |
// EXTERNAL INCLUDES
|
|
23 |
#include <CMIDToolkit.h>
|
|
24 |
#include <jutils.h>
|
|
25 |
#include <jdebug.h>
|
|
26 |
#include <txtetext.h>
|
|
27 |
#include <baclipb.h>
|
|
28 |
#include <s32mem.h>
|
|
29 |
#include <s32ucmp.h>
|
|
30 |
|
|
31 |
#ifdef SYMBIAN_ENABLE_SPLIT_HEADERS
|
|
32 |
#include <txtclipboard.h>
|
|
33 |
#endif
|
|
34 |
|
|
35 |
/**
|
|
36 |
* Local helper method for handling native strings and converting them to
|
|
37 |
* Java strings.
|
|
38 |
*
|
|
39 |
* Error is set also in this method.
|
|
40 |
*
|
|
41 |
* @param aJniEnv A valid reference JNI environment.
|
|
42 |
* @param aText The native descriptor to be converted.
|
|
43 |
* @param aError The return value from ExecuteTrap.
|
|
44 |
* @param aReturnError On return, contains an error code.
|
|
45 |
* @return The converted Java string or NULL if the conversion failed.
|
|
46 |
*/
|
|
47 |
LOCAL_C jstring HandleString(
|
|
48 |
JNIEnv& aJniEnv,
|
|
49 |
const TDesC& aText,
|
|
50 |
TInt aError,
|
|
51 |
jintArray aReturnError)
|
|
52 |
{
|
|
53 |
DEBUG("TextEditor.cpp - HandleString +");
|
|
54 |
|
|
55 |
jstring javaText = NULL;
|
|
56 |
|
|
57 |
// Create Java string if the operation was successful.
|
|
58 |
if (aError == KErrNone)
|
|
59 |
{
|
|
60 |
javaText = CreateJavaString(aJniEnv, aText);
|
|
61 |
|
|
62 |
// If NULL is returned, it indicates that the creation failed.
|
|
63 |
if (!javaText)
|
|
64 |
{
|
|
65 |
aError = KErrNoMemory;
|
|
66 |
}
|
|
67 |
}
|
|
68 |
|
|
69 |
jint errorArray[ 1 ] = { aError };
|
|
70 |
aJniEnv.SetIntArrayRegion(aReturnError, 0, 1, errorArray);
|
|
71 |
|
|
72 |
DEBUG_INT("TextEditor.cpp - HandleString -, aError=%d", aError);
|
|
73 |
|
|
74 |
return javaText;
|
|
75 |
}
|
|
76 |
|
|
77 |
/**
|
|
78 |
* Local helper function for inserting the text to clipboard.
|
|
79 |
*
|
|
80 |
* @param aText The text to be copied to the clipboard.
|
|
81 |
*/
|
|
82 |
LOCAL_C void CopyToClipboardL(const TDesC* aText)
|
|
83 |
{
|
|
84 |
if (aText == &KNullDesC)
|
|
85 |
{
|
|
86 |
CClipboard::Clear(CCoeEnv::Static()->FsSession());
|
|
87 |
}
|
|
88 |
else
|
|
89 |
{
|
|
90 |
CClipboard* clipboard = CClipboard::NewForWritingLC(
|
|
91 |
CCoeEnv::Static()->FsSession());
|
|
92 |
|
|
93 |
CPlainText* text = CPlainText::NewL();
|
|
94 |
CleanupStack::PushL(text);
|
|
95 |
text->InsertL(0, *aText);
|
|
96 |
text->CopyToStoreL(clipboard->Store(), clipboard->StreamDictionary(), 0,
|
|
97 |
text->DocumentLength());
|
|
98 |
CleanupStack::PopAndDestroy(); // text
|
|
99 |
clipboard->CommitL();
|
|
100 |
CleanupStack::PopAndDestroy(); // clipboard;
|
|
101 |
}
|
|
102 |
}
|
|
103 |
|
|
104 |
/*
|
|
105 |
* Class: com_nokia_mid_ui_Clipboard
|
|
106 |
* Method: _copyToClipboard
|
|
107 |
* Signature: (IIZ)I
|
|
108 |
*/
|
|
109 |
JNIEXPORT jint JNICALL Java_com_nokia_mid_ui_Clipboard__1copyToClipboard(
|
|
110 |
JNIEnv* aJniEnv,
|
|
111 |
jclass /* aPeer */,
|
|
112 |
jint aToolkitHandle,
|
|
113 |
jstring aText)
|
|
114 |
{
|
|
115 |
DEBUG("Clipboard.cpp - copyToClipboard +");
|
|
116 |
CMIDToolkit* toolkit = JavaUnhand< CMIDToolkit >(aToolkitHandle);
|
|
117 |
|
|
118 |
// Convert the content to a native descriptor. NULL indicates
|
|
119 |
// that an empty string is set as content.
|
|
120 |
const RJString content(*aJniEnv, aText);
|
|
121 |
|
|
122 |
const TDesC* nativeContent = (aText ? &content : &KNullDesC);
|
|
123 |
|
|
124 |
jint error = toolkit->ExecuteTrap(&CopyToClipboardL,
|
|
125 |
nativeContent);
|
|
126 |
DEBUG("Clipboard.cpp - copyToClipboard -");
|
|
127 |
return error;
|
|
128 |
}
|
|
129 |
|
|
130 |
/**
|
|
131 |
* Local helper function for retrieving the text from clipboard.
|
|
132 |
*
|
|
133 |
* @return The text copied from the clipboard.
|
|
134 |
*/
|
|
135 |
LOCAL_C void CopyFromClipboardL(HBufC** aText)
|
|
136 |
{
|
|
137 |
CClipboard* cb = NULL;
|
|
138 |
TRAPD(error, (cb = CClipboard::NewForReadingL(
|
|
139 |
CCoeEnv::Static()->FsSession()))); // codescanner::eikonenvstatic
|
|
140 |
// Nothing in the clipboard, not an error
|
|
141 |
if ((error != KErrPathNotFound) && (error != KErrNotFound))
|
|
142 |
{
|
|
143 |
TStreamId id(cb->StreamDictionary().At(KClipboardUidTypePlainText));
|
|
144 |
if (id != KNullStreamId)
|
|
145 |
{
|
|
146 |
CleanupStack::PushL(cb);
|
|
147 |
|
|
148 |
RStoreReadStream stream;
|
|
149 |
stream.OpenLC(cb->Store(), id);
|
|
150 |
TInt length = stream.ReadInt32L();
|
|
151 |
CBufFlat* buffer = CBufFlat::NewL(length);
|
|
152 |
CleanupStack::PushL(buffer);
|
|
153 |
|
|
154 |
RBufWriteStream bufStream(*buffer);
|
|
155 |
CleanupClosePushL(bufStream);
|
|
156 |
TMemoryStreamUnicodeSink sink(bufStream);
|
|
157 |
TUnicodeExpander e;
|
|
158 |
e.ExpandL(sink, stream, length);
|
|
159 |
bufStream.CommitL();
|
|
160 |
|
|
161 |
CleanupStack::PopAndDestroy(); // bufStream
|
|
162 |
CleanupStack::Pop(buffer);
|
|
163 |
CleanupStack::PopAndDestroy(2, cb); // stream, cb
|
|
164 |
|
|
165 |
CleanupStack::PushL(buffer);
|
|
166 |
|
|
167 |
TPtrC8 ptr8(buffer->Ptr(0));
|
|
168 |
|
|
169 |
TPtr ptr = TPtr16(reinterpret_cast<TText16*>(const_cast<TText8*>(ptr8.Ptr())),
|
|
170 |
length,
|
|
171 |
length);
|
|
172 |
|
|
173 |
if (ptr.Length() != 0)
|
|
174 |
{
|
|
175 |
*aText = ptr.AllocL();
|
|
176 |
CleanupStack::PopAndDestroy(); // ptr;
|
|
177 |
}
|
|
178 |
}
|
|
179 |
}
|
|
180 |
}
|
|
181 |
|
|
182 |
/*
|
|
183 |
* Class: com_nokia_mid_ui_Clipboard
|
|
184 |
* Method: _copyFromClipboard
|
|
185 |
* Signature: (II)Z
|
|
186 |
*/
|
|
187 |
JNIEXPORT jstring JNICALL Java_com_nokia_mid_ui_Clipboard__1copyFromClipboard(
|
|
188 |
JNIEnv* aJniEnv,
|
|
189 |
jclass /* aPeer */,
|
|
190 |
jint aToolkitHandle,
|
|
191 |
jintArray aError)
|
|
192 |
{
|
|
193 |
DEBUG("Clipboard.cpp - copyFromClipboard +");
|
|
194 |
|
|
195 |
CMIDToolkit* toolkit = JavaUnhand< CMIDToolkit >(aToolkitHandle);
|
|
196 |
|
|
197 |
HBufC* text = NULL;
|
|
198 |
|
|
199 |
jstring content = NULL;
|
|
200 |
|
|
201 |
TInt error = toolkit->ExecuteTrap(
|
|
202 |
&CopyFromClipboardL,
|
|
203 |
&text);
|
|
204 |
if (text != NULL)
|
|
205 |
{
|
|
206 |
// Handle text conversion an errors.
|
|
207 |
content = HandleString(*aJniEnv, *text, error, aError);
|
|
208 |
|
|
209 |
// Text is not needed anymore.
|
|
210 |
delete text;
|
|
211 |
}
|
|
212 |
|
|
213 |
DEBUG("Clipboard.cpp - copyFromClipboard -");
|
|
214 |
return content;
|
|
215 |
}
|
|
216 |
|
|
217 |
// End of file
|