javaextensions/pim/framework/src.s60/cpimserializer.cpp
branchRCL_3
changeset 19 04becd199f91
equal deleted inserted replaced
16:f5050f1da672 19:04becd199f91
       
     1 /*
       
     2 * Copyright (c) 2008 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:  Versit serializer native side.
       
    15  *
       
    16 */
       
    17 
       
    18 
       
    19 // INCLUDE FILES
       
    20 #include "cpimserializer.h"
       
    21 #include "cpimmanager.h"
       
    22 #include "cpimversit.h"
       
    23 #include "cpimitem.h"
       
    24 #include "cpimeventitem.h"
       
    25 #include "pimbaserepeatrule.h"
       
    26 #include "pimjnitools.h"
       
    27 #include "pimutils.h"
       
    28 #include "logger.h"
       
    29 
       
    30 // CONSTANTS
       
    31 const Versit::TVersitCharSet KCharSetMapping[] =
       
    32     { Versit::EUTF8CharSet, Versit::EISO88591CharSet };
       
    33 
       
    34 // ============================ MEMBER FUNCTIONS ===============================
       
    35 
       
    36 // -----------------------------------------------------------------------------
       
    37 // CPIMSerializer::getInstance
       
    38 // Two-phased constructor for JNI
       
    39 // -----------------------------------------------------------------------------
       
    40 //
       
    41 pimbaseserializer* pimbaseserializer::getInstance(pimbasemanager* aManager) // used for getting validators
       
    42 {
       
    43     JELOG2(EPim);
       
    44     CPIMManager* manager = static_cast<CPIMManager*>(aManager);
       
    45     TInt error = KErrNone;
       
    46     pimbaseserializer *serializer = NULL;
       
    47     TRAP(error, serializer = CPIMSerializer::NewL(manager));
       
    48     if (error != KErrNone)
       
    49     {
       
    50         throw error;
       
    51     }
       
    52     return serializer;
       
    53 }
       
    54 
       
    55 // -----------------------------------------------------------------------------
       
    56 // CPIMSerializer::NewL
       
    57 // Two-phased constructor.
       
    58 // -----------------------------------------------------------------------------
       
    59 //
       
    60 CPIMSerializer* CPIMSerializer::NewL(CPIMManager* aManager)
       
    61 {
       
    62     JELOG2(EPim);
       
    63     CPIMSerializer* self = new(ELeave) CPIMSerializer;
       
    64 
       
    65     CleanupStack::PushL(self);
       
    66     self->ConstructL(aManager);
       
    67     CleanupStack::Pop();
       
    68 
       
    69     return self;
       
    70 }
       
    71 
       
    72 // Destructor
       
    73 CPIMSerializer::~CPIMSerializer()
       
    74 {
       
    75     JELOG2(EPim);
       
    76     delete iVersit;
       
    77 }
       
    78 
       
    79 // -----------------------------------------------------------------------------
       
    80 // CPIMSerializer::FromSerialFormatL
       
    81 // forwards the request to the versit module
       
    82 // -----------------------------------------------------------------------------
       
    83 //
       
    84 jintArray CPIMSerializer::FromSerialFormatL(JNIEnv* aJniEnv,
       
    85         const TDesC8& aBuffer, TEncoding aEncoding)
       
    86 {
       
    87     JELOG2(EPim);
       
    88     Versit::TVersitCharSet charSet = MapEncodingL(aEncoding);
       
    89     RPointerArray<CPIMItem>* itemArray = iVersit->StringToItemL(aBuffer,
       
    90                                          charSet);
       
    91     CleanupDeletePushL(itemArray);
       
    92     CleanupClosePushL(*itemArray);
       
    93 
       
    94     // calculate the length of the needed array
       
    95     TInt arrayLength = 0;
       
    96     TInt itemCount = itemArray->Count();
       
    97     TInt i = 0;
       
    98     for (i = 0; i < itemCount; i++)
       
    99     {
       
   100         switch ((*itemArray)[i]->ItemType())
       
   101         {
       
   102         case EPIMContactList:
       
   103         case EPIMToDoList:
       
   104         {
       
   105             arrayLength += 2;
       
   106             break;
       
   107         }
       
   108         case EPIMEventList:
       
   109         {
       
   110             arrayLength += 4;
       
   111             break;
       
   112         }
       
   113         default:
       
   114         {
       
   115             User::Leave(KErrNoMemory);
       
   116         }
       
   117         }
       
   118     }
       
   119 
       
   120     jintArray itemHandles = aJniEnv->NewIntArray(arrayLength);
       
   121     if (!itemHandles)
       
   122     {
       
   123         User::Leave(KErrNoMemory);
       
   124     }
       
   125     jint* handles = aJniEnv->GetIntArrayElements(itemHandles, 0);
       
   126     jint* handlePointer = handles;
       
   127     for (i = 0; i < itemCount; i++)
       
   128     {
       
   129         pimbaseitem* item = (*itemArray)[i];
       
   130         TInt itemHandle(reinterpret_cast<int>(item));
       
   131         TInt itemType = ((*itemArray)[i])->ItemType();
       
   132         *handlePointer++ = itemType;
       
   133         *handlePointer++ = itemHandle;
       
   134         if (itemType == EPIMEventList)
       
   135         {
       
   136 
       
   137             int baseRepeatRuleHandle = item->getRepeatHandle();
       
   138             if (baseRepeatRuleHandle < KErrNone)
       
   139                 throw baseRepeatRuleHandle;
       
   140 
       
   141             *handlePointer++ = baseRepeatRuleHandle;
       
   142             handlePointer++;
       
   143         }
       
   144     }
       
   145     aJniEnv->ReleaseIntArrayElements(itemHandles, handles, 0);
       
   146 
       
   147     CleanupStack::PopAndDestroy(2);
       
   148     return itemHandles;
       
   149 }
       
   150 
       
   151 jintArray CPIMSerializer::fromSerialFormat(JNIEnv* aJniEnv, jbyteArray aBytes,
       
   152         int aByteLength, int aEncoding, jintArray aError)
       
   153 {
       
   154     JELOG2(EPim);
       
   155     jbyte* bytes = aJniEnv->GetByteArrayElements(aBytes, 0);
       
   156     TUint8* temp = reinterpret_cast<TUint8*>(bytes);
       
   157     const TPtrC8 versitString(temp, aByteLength);
       
   158     TInt error = KErrNone;
       
   159     jintArray itemArray = NULL;
       
   160     TRAP(error, itemArray = FromSerialFormatL(aJniEnv, versitString,
       
   161                             static_cast<TEncoding>(aEncoding)));
       
   162     aJniEnv->ReleaseByteArrayElements(aBytes, bytes, JNI_ABORT);
       
   163     if (error != KErrNone)
       
   164     {
       
   165         SetJavaErrorCode(aJniEnv, aError, error);
       
   166         return NULL;
       
   167     }
       
   168 
       
   169     if (!itemArray)
       
   170     {
       
   171         SetJavaErrorCode(aJniEnv, aError, KErrNoMemory);
       
   172         return NULL;
       
   173     }
       
   174 
       
   175     SetJavaErrorCode(aJniEnv, aError, KErrNone);
       
   176 
       
   177     return itemArray;
       
   178 }
       
   179 
       
   180 // -----------------------------------------------------------------------------
       
   181 // CPIMSerializer::ToSerialFormatL
       
   182 // forwards the request to the versit module
       
   183 // -----------------------------------------------------------------------------
       
   184 //
       
   185 HBufC8* CPIMSerializer::ToSerialFormatL(const CPIMItem& aItem,
       
   186                                         TEncoding aEncoding)
       
   187 {
       
   188     JELOG2(EPim);
       
   189     Versit::TVersitCharSet charSet = MapEncodingL(aEncoding);
       
   190     return iVersit->ItemToStringL(aItem, charSet);
       
   191 }
       
   192 
       
   193 jbyteArray CPIMSerializer::toSerialFormat(pimbaseitem* aItem, int aEncoding,
       
   194         JNIEnv* aJniEnv, jintArray aError)
       
   195 {
       
   196     JELOG2(EPim);
       
   197     HBufC8* versitObject = NULL;
       
   198     CPIMItem* item = static_cast<CPIMItem *>(aItem);
       
   199 
       
   200     TInt error = 0;
       
   201     TRAP(error, versitObject = ToSerialFormatL(*item,
       
   202                                static_cast<TEncoding>(aEncoding)));
       
   203     if (error != KErrNone)
       
   204     {
       
   205         SetJavaErrorCode(aJniEnv, aError, error);
       
   206         return NULL;
       
   207     }
       
   208 
       
   209     if (!versitObject)
       
   210     {
       
   211         SetJavaErrorCode(aJniEnv, aError, KErrNoMemory);
       
   212         return NULL;
       
   213     }
       
   214 
       
   215     TInt versitLength = versitObject->Length();
       
   216     jbyteArray retVal = aJniEnv->NewByteArray(versitLength);
       
   217     if (!retVal)
       
   218     {
       
   219         // creating a new byte array failed
       
   220         SetJavaErrorCode(aJniEnv, aError, KErrNoMemory);
       
   221         delete versitObject;
       
   222         return NULL;
       
   223     }
       
   224     jbyte* bytes = aJniEnv->GetByteArrayElements(retVal, 0);
       
   225     const TUint8* versitPtr = versitObject->Ptr();
       
   226     for (int i = 0; i < versitLength; i++)
       
   227     {
       
   228         bytes[i] = versitPtr[i];
       
   229     }
       
   230     aJniEnv->ReleaseByteArrayElements(retVal, bytes, 0);
       
   231     SetJavaErrorCode(aJniEnv, aError, KErrNone);
       
   232     delete versitObject;
       
   233     return retVal;
       
   234 }
       
   235 
       
   236 // -----------------------------------------------------------------------------
       
   237 // CPIMSerializer::SupportedSerialFormatsL
       
   238 // forwards the request to the versit module
       
   239 // -----------------------------------------------------------------------------
       
   240 //
       
   241 const CDesCArray& CPIMSerializer::SupportedSerialFormatsL(
       
   242     TPIMListType aPimListType)
       
   243 {
       
   244     JELOG2(EPim);
       
   245     return iVersit->SupportedSerialFormatsL(aPimListType);
       
   246 }
       
   247 
       
   248 jobjectArray CPIMSerializer::supportedSerialFormats(TPIMListType aPimListType,
       
   249         JNIEnv* aJniEnv, jintArray aError)
       
   250 {
       
   251     JELOG2(EPim);
       
   252     const CDesCArray* desArray = NULL;
       
   253     TInt error = KErrNone;
       
   254     TRAP(error, desArray = &(SupportedSerialFormatsL(aPimListType)));
       
   255     if (error != KErrNone)
       
   256     {
       
   257         SetJavaErrorCode(aJniEnv, aError, error);
       
   258         return NULL;
       
   259     }
       
   260 
       
   261     if (!desArray)
       
   262     {
       
   263         SetJavaErrorCode(aJniEnv, aError, KErrNoMemory);
       
   264         return NULL;
       
   265     }
       
   266 
       
   267     TInt desCount = desArray->Count();
       
   268     jobjectArray retVal = aJniEnv->NewObjectArray(desCount, aJniEnv->FindClass(
       
   269                               "java/lang/String"), NULL);
       
   270     if (retVal == NULL)
       
   271     {
       
   272         SetJavaErrorCode(aJniEnv, aError, KErrNoMemory);
       
   273         return NULL;
       
   274     }
       
   275 
       
   276     for (TInt i = 0; i < desCount; i++)
       
   277     {
       
   278         TPtrC epocString = (*desArray)[i];
       
   279         jstring javaString = aJniEnv->NewString(epocString.Ptr(),
       
   280                                                 epocString.Length());
       
   281         if (javaString == NULL)
       
   282         {
       
   283             SetJavaErrorCode(aJniEnv, aError, KErrNoMemory);
       
   284             return NULL;
       
   285         }
       
   286         aJniEnv->SetObjectArrayElement(retVal, i, javaString);
       
   287 
       
   288         // Avoid exceeding max local references (5)
       
   289         aJniEnv->DeleteLocalRef(javaString);
       
   290     }
       
   291     // note that desArray is owned by the adapter, and not deleted here
       
   292     SetJavaErrorCode(aJniEnv, aError, KErrNone);
       
   293     return retVal;
       
   294 }
       
   295 
       
   296 // -----------------------------------------------------------------------------
       
   297 // CPIMSerializer::CPIMSerializer
       
   298 // C++ default constructor can NOT contain any code, that
       
   299 // might leave.
       
   300 // -----------------------------------------------------------------------------
       
   301 //
       
   302 CPIMSerializer::CPIMSerializer()
       
   303 {
       
   304     JELOG2(EPim);
       
   305 }
       
   306 
       
   307 // -----------------------------------------------------------------------------
       
   308 // CPIMSerializer::ConstructL
       
   309 // Symbian 2nd phase constructor can leave.
       
   310 // -----------------------------------------------------------------------------
       
   311 //
       
   312 void CPIMSerializer::ConstructL(CPIMManager* aManager)
       
   313 {
       
   314     JELOG2(EPim);
       
   315     iVersit = CPIMVersit::NewL(aManager);
       
   316 }
       
   317 
       
   318 // -----------------------------------------------------------------------------
       
   319 // CPIMSerializer::MapEncodingL
       
   320 // Maps the encoding from TEncoding to Versit::TVersitCharSet
       
   321 // -----------------------------------------------------------------------------
       
   322 //
       
   323 Versit::TVersitCharSet CPIMSerializer::MapEncodingL(TEncoding aEncoding)
       
   324 {
       
   325     JELOG2(EPim);
       
   326     if ((aEncoding < 0) || (aEncoding > EISO88591))
       
   327     {
       
   328         User::Leave(KErrArgument);
       
   329     }
       
   330     return KCharSetMapping[aEncoding];
       
   331 }
       
   332