javaextensions/iapinfo/src.s60/iapinfoimpl.cpp
changeset 21 2a9601315dfc
equal deleted inserted replaced
18:e8e63152f320 21:2a9601315dfc
       
     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:
       
    15  *
       
    16 */
       
    17 
       
    18 
       
    19 #include <cmmanager.h>
       
    20 #include <cmdestination.h>
       
    21 #include "jni.h"
       
    22 #include "stringbuffer.h"
       
    23 #include "com_nokia_mid_iapinfo_IAPInfoImpl.h"
       
    24 #include "logger.h"
       
    25 
       
    26 using namespace CommsDat;
       
    27 using namespace CMManager;
       
    28 using namespace java::util;
       
    29 
       
    30 _LIT(KSelector, ";");
       
    31 _LIT(KIDSelector, ",");
       
    32 
       
    33 jstring CreateJavaString(JNIEnv* aJNI, const HBufC* aString);
       
    34 jobjectArray GetDestinationNetworksL(JNIEnv * aJNI);
       
    35 jstring GetDestinationNetworkByIdL(JNIEnv* aJNI, jint aID);
       
    36 jstring GetDestinationNetworkByNameL(JNIEnv *aJNI, jstring aJName);
       
    37 void throwException(JNIEnv* aJNI, TInt aErrorCode);
       
    38 
       
    39 ///////////////////////////////////////////////////////////////////////////////
       
    40 //  Method:    _getDestinationNetworks
       
    41 //  Signature: ()I
       
    42 ///////////////////////////////////////////////////////////////////////////////
       
    43 JNIEXPORT jobjectArray JNICALL Java_com_nokia_mid_iapinfo_IAPInfoImpl__1getDestinationNetworks
       
    44 (JNIEnv * aJNI, jclass)
       
    45 {
       
    46     jobjectArray Object = NULL;
       
    47     TInt err = KErrNone;
       
    48     TRAP(err, Object = GetDestinationNetworksL(aJNI));
       
    49     if (err != KErrNone)
       
    50     {
       
    51         throwException(aJNI, err);
       
    52         return NULL;
       
    53     }
       
    54     return Object;
       
    55 }
       
    56 
       
    57 ///////////////////////////////////////////////////////////////////////////////
       
    58 //GetDestinationNetworksL
       
    59 // @param JNIEnv
       
    60 ///////////////////////////////////////////////////////////////////////////////
       
    61 jobjectArray GetDestinationNetworksL(JNIEnv * aJNI)
       
    62 {
       
    63 
       
    64     RCmManager localCmManagerExt;
       
    65     LOG(EJavaIapInfo, EInfo,"using RCMManager");
       
    66     localCmManagerExt.OpenL();
       
    67     RArray<TUint32> destIdArray;
       
    68     localCmManagerExt.AllDestinationsL(destIdArray);
       
    69 
       
    70     //allocate the String array
       
    71     TInt array_size = destIdArray.Count();
       
    72     jobjectArray jarray = aJNI->NewObjectArray(array_size, aJNI->FindClass(
       
    73                               "java/lang/String"), aJNI->NewStringUTF(""));
       
    74     for (TInt i = 0; i < destIdArray.Count(); i++)
       
    75     {
       
    76         StringBuffer* Buffer;
       
    77         Buffer = StringBuffer::NewL();
       
    78         CleanupStack::PushL(Buffer);
       
    79         RCmDestination dest = localCmManagerExt.DestinationL(destIdArray[i]);
       
    80         CleanupClosePushL(dest);
       
    81         TUint32 ID = dest.Id();
       
    82         Buffer->Append(ID);
       
    83         Buffer->Append(KSelector);
       
    84         HBufC* destName = dest.NameLC();
       
    85         Buffer->Append(destName);
       
    86         Buffer->Append(KSelector);
       
    87         for (TInt j = 0; j < dest.ConnectionMethodCount(); j++)
       
    88         {
       
    89             TUint32 iapId =
       
    90                 dest.ConnectionMethodL(j).GetIntAttributeL(ECmIapId);
       
    91             Buffer->Append(iapId);
       
    92             Buffer->Append(KIDSelector);
       
    93         }
       
    94         aJNI->SetObjectArrayElement(jarray, i, CreateJavaString(aJNI,
       
    95                                     Buffer->GetString()));
       
    96         CleanupStack::PopAndDestroy(2);//destName, dest
       
    97         CleanupStack::PopAndDestroy(1); // destroy the Buffer object
       
    98     }//end for
       
    99     localCmManagerExt.Close();
       
   100 
       
   101     return jarray;
       
   102 }
       
   103 
       
   104 ///////////////////////////////////////////////////////////////////////////////
       
   105 // Class:     com_nokia_mid_iapinfo_IAPInfoImpl
       
   106 // Method:    _getDestinationNetworkById
       
   107 // Signature: ()I
       
   108 ///////////////////////////////////////////////////////////////////////////////
       
   109 JNIEXPORT jstring JNICALL Java_com_nokia_mid_iapinfo_IAPInfoImpl__1getDestinationNetworkById
       
   110 (JNIEnv* aJNI, jclass, jint aID)
       
   111 {
       
   112     jstring Object = NULL;
       
   113     TInt err = KErrNone;
       
   114     TRAP(err, Object = GetDestinationNetworkByIdL(aJNI, aID));
       
   115     if (err != KErrNone)
       
   116     {
       
   117         throwException(aJNI, err);
       
   118         return NULL;
       
   119     }
       
   120     return Object;
       
   121 }
       
   122 
       
   123 ///////////////////////////////////////////////////////////////////////////////
       
   124 // GetDestinationNetworkByIdL
       
   125 //
       
   126 // @param JNIEnv
       
   127 // @param jint aID
       
   128 //
       
   129 ///////////////////////////////////////////////////////////////////////////////
       
   130 jstring GetDestinationNetworkByIdL(JNIEnv* aJNI, jint aID)
       
   131 {
       
   132     RCmManager localCmManagerExt;
       
   133     LOG(EJavaIapInfo, EInfo,"using RCMManager");
       
   134     localCmManagerExt.OpenL();
       
   135     //read all destinations
       
   136     RArray<TUint32> destIdArray;
       
   137     localCmManagerExt.AllDestinationsL(destIdArray);
       
   138     StringBuffer* Buffer = StringBuffer::NewL();
       
   139     CleanupStack::PushL(Buffer);
       
   140     for (TInt i = 0; i < destIdArray.Count(); i++)
       
   141     {
       
   142         RCmDestination DestinationNetwork = localCmManagerExt.DestinationL(
       
   143                                                 destIdArray[i]);
       
   144         CleanupClosePushL(DestinationNetwork);
       
   145         //check the id.
       
   146         TUint32 ID = DestinationNetwork.Id();
       
   147         if (ID == aID)
       
   148         {
       
   149             Buffer->Append(ID);
       
   150             Buffer->Append(KSelector);
       
   151             //Destination network is found, add value to stringbuffer
       
   152             HBufC* Name = DestinationNetwork.NameLC();
       
   153             Buffer->Append(Name);
       
   154             Buffer->Append(KSelector);
       
   155             //get the ids of the Access points
       
   156             for (TInt j = 0; j < DestinationNetwork.ConnectionMethodCount(); j++)
       
   157             {
       
   158                 TUint32
       
   159                 iapId =
       
   160                     DestinationNetwork.ConnectionMethodL(j).GetIntAttributeL(
       
   161                         ECmIapId);
       
   162                 Buffer->Append(iapId);
       
   163                 Buffer->Append(KIDSelector);
       
   164             }
       
   165             CleanupStack::PopAndDestroy(1);//Name
       
   166             i = destIdArray.Count(); //to exit
       
   167         }
       
   168         CleanupStack::PopAndDestroy(1); // DestinationNetwork
       
   169     }
       
   170 
       
   171     jstring ReturnString = CreateJavaString(aJNI, Buffer->GetString());
       
   172     CleanupStack::PopAndDestroy(1);//Buffer
       
   173     localCmManagerExt.Close();
       
   174 
       
   175     return ReturnString;
       
   176 }
       
   177 /*
       
   178  * Class:     com_nokia_mid_iapinfo_IAPInfoImpl
       
   179  * Method:    _getDestinationNetworkByName
       
   180  */
       
   181 JNIEXPORT jstring JNICALL Java_com_nokia_mid_iapinfo_IAPInfoImpl__1getDestinationNetworkByName
       
   182 (JNIEnv *aJNI, jclass, jstring aJName)
       
   183 {
       
   184     jstring Object = NULL;
       
   185     TInt err = KErrNone;
       
   186     TRAP(err, Object = GetDestinationNetworkByNameL(aJNI, aJName));
       
   187     if (err != KErrNone)
       
   188     {
       
   189         throwException(aJNI, err);
       
   190         return NULL;
       
   191     }
       
   192     return Object;
       
   193 }
       
   194 
       
   195 ///////////////////////////////////////////////////////////////////////////////
       
   196 // GetDestinationNetworkByNameL
       
   197 //
       
   198 // @param JNIEnv
       
   199 // @param aJName
       
   200 //
       
   201 ///////////////////////////////////////////////////////////////////////////////
       
   202 jstring GetDestinationNetworkByNameL(JNIEnv *aJNI, jstring aJName)
       
   203 {
       
   204     RCmManager localCmManagerExt;
       
   205     LOG(EJavaIapInfo, EInfo,"using RCMManager");
       
   206     localCmManagerExt.OpenL();
       
   207     //read all destinations
       
   208     RArray<TUint32> destIdArray;
       
   209     localCmManagerExt.AllDestinationsL(destIdArray);
       
   210     StringBuffer* Buffer = StringBuffer::NewL();
       
   211     CleanupStack::PushL(Buffer);
       
   212     HBufC* SearchString = Buffer->CreateHBufCFromJavaStringLC(aJNI, aJName);
       
   213     for (TInt i = 0; i < destIdArray.Count(); i++)
       
   214     {
       
   215         RCmDestination DestinationNetwork = localCmManagerExt.DestinationL(
       
   216                                                 destIdArray[i]);
       
   217         CleanupClosePushL(DestinationNetwork);
       
   218         //check the id.
       
   219         TUint32 ID = DestinationNetwork.Id();
       
   220         HBufC* DNName = DestinationNetwork.NameLC();
       
   221         if (DNName->Des().Compare(SearchString->Des()) == 0)
       
   222         {
       
   223             Buffer->Append(ID);
       
   224             Buffer->Append(KSelector);
       
   225             //Destination network is found, add value to stringbuffer
       
   226             Buffer->Append(DNName);
       
   227             Buffer->Append(KSelector);
       
   228             //get the ids of the Access points
       
   229             for (TInt j = 0; j < DestinationNetwork.ConnectionMethodCount(); j++)
       
   230             {
       
   231                 TUint32
       
   232                 iapId =
       
   233                     DestinationNetwork.ConnectionMethodL(j).GetIntAttributeL(
       
   234                         ECmIapId);
       
   235                 Buffer->Append(iapId);
       
   236                 Buffer->Append(KIDSelector);
       
   237             }//end for
       
   238             i = destIdArray.Count();//to terminate for
       
   239         }//end if
       
   240         CleanupStack::PopAndDestroy(1);// DNName
       
   241         CleanupStack::PopAndDestroy(1); // DestinationNetwork
       
   242     }//end for
       
   243     jstring ReturnString = CreateJavaString(aJNI, Buffer->GetString());
       
   244     CleanupStack::PopAndDestroy(1);// SearchString
       
   245     CleanupStack::PopAndDestroy(1);// Buffer
       
   246     localCmManagerExt.Close();
       
   247 
       
   248     return ReturnString;
       
   249 }
       
   250 
       
   251 ///////////////////////////////////////////////////////////////////////////////
       
   252 // CreateJavaString Creates a java string from HBufC object
       
   253 //
       
   254 // @param JNIEnv
       
   255 // @param HBufC
       
   256 ///////////////////////////////////////////////////////////////////////////////
       
   257 jstring CreateJavaString(JNIEnv* aJNI, const HBufC* aString)
       
   258 {
       
   259     jstring str = aJNI->NewString(aString->Ptr(), aString->Length());
       
   260     return str;
       
   261 }
       
   262 
       
   263 ///////////////////////////////////////////////////////////////////////////////
       
   264 // CreateJavaString Method to throw a java exception
       
   265 // @param JNIEnv* aJNI
       
   266 // @param TInt aErrorCode The error code
       
   267 ///////////////////////////////////////////////////////////////////////////////
       
   268 void throwException(JNIEnv* aJNI, TInt aErrorCode)
       
   269 {
       
   270     jclass clsException = aJNI->FindClass(
       
   271                               "com/nokia/mid/iapinfo/IAPInfoException");
       
   272     TBuf8<40> buf;
       
   273     buf.Append(_L("Native error: "));
       
   274     buf.AppendNum(aErrorCode);
       
   275     const char *temp = (const char *) buf.PtrZ();
       
   276     aJNI->ThrowNew(clsException, temp);
       
   277 }