javauis/lcdui_akn/javalcdui/src.nokialcdui/Clipboard.cpp
branchRCL_3
changeset 21 4376525cdefb
parent 14 04becd199f91
equal deleted inserted replaced
19:71c436fe3ce0 21:4376525cdefb
    23 #include <CMIDToolkit.h>
    23 #include <CMIDToolkit.h>
    24 #include <jutils.h>
    24 #include <jutils.h>
    25 #include <jdebug.h>
    25 #include <jdebug.h>
    26 #include <txtetext.h>
    26 #include <txtetext.h>
    27 #include <baclipb.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 }
    28 
    76 
    29 /**
    77 /**
    30  * Local helper function for inserting the text to clipboard.
    78  * Local helper function for inserting the text to clipboard.
    31  *
    79  *
    32  * @param aText The text to be copied to the clipboard.
    80  * @param aText The text to be copied to the clipboard.
    33  */
    81  */
    34 LOCAL_C void CopyToClipboardL(const TDesC* aText)
    82 LOCAL_C void CopyToClipboardL(const TDesC* aText)
    35 {
    83 {
    36     CClipboard* clipboard = CClipboard::NewForWritingLC(
    84     if (aText == &KNullDesC)
    37                                 CCoeEnv::Static()->FsSession());
    85     {
    38 
    86         CClipboard::Clear(CCoeEnv::Static()->FsSession());
    39     CPlainText* text = CPlainText::NewL();
    87     }
    40     CleanupStack::PushL(text);
    88     else
    41     text->InsertL(0, *aText);
    89     {
    42 
    90         CClipboard* clipboard = CClipboard::NewForWritingLC(
    43     text->CopyToStoreL(clipboard->Store(), clipboard->StreamDictionary(), 0,
    91                                     CCoeEnv::Static()->FsSession());
    44                        text->DocumentLength());
    92 
    45     CleanupStack::PopAndDestroy(); // text
    93         CPlainText* text = CPlainText::NewL();
    46     clipboard->CommitL();
    94         CleanupStack::PushL(text);
    47     CleanupStack::PopAndDestroy();   // clipboard;
    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     }
    48 }
   102 }
    49 
   103 
    50 /*
   104 /*
    51  * Class:     com_nokia_mid_ui_Clipboard
   105  * Class:     com_nokia_mid_ui_Clipboard
    52  * Method:    _copyToClipboard
   106  * Method:    _copyToClipboard
    62     CMIDToolkit* toolkit = JavaUnhand< CMIDToolkit >(aToolkitHandle);
   116     CMIDToolkit* toolkit = JavaUnhand< CMIDToolkit >(aToolkitHandle);
    63 
   117 
    64     // Convert the content to a native descriptor. NULL indicates
   118     // Convert the content to a native descriptor. NULL indicates
    65     // that an empty string is set as content.
   119     // that an empty string is set as content.
    66     const RJString content(*aJniEnv, aText);
   120     const RJString content(*aJniEnv, aText);
       
   121 
    67     const TDesC* nativeContent = (aText ? &content : &KNullDesC);
   122     const TDesC* nativeContent = (aText ? &content : &KNullDesC);
    68 
   123 
    69     jint error = toolkit->ExecuteTrap(&CopyToClipboardL,
   124     jint error = toolkit->ExecuteTrap(&CopyToClipboardL,
    70                                       nativeContent);
   125                                       nativeContent);
    71     DEBUG("Clipboard.cpp - copyToClipboard -");
   126     DEBUG("Clipboard.cpp - copyToClipboard -");
    72     return error;
   127     return error;
    73 }
   128 }
    74 
   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 
    75 // End of file
   217 // End of file