defaultapplicationsettings/services_db/src/services_db.cpp
branchRCL_3
changeset 35 5f281e37a2f5
parent 0 254040eb3b7d
equal deleted inserted replaced
34:90fe62538f66 35:5f281e37a2f5
       
     1 /*
       
     2 * Copyright (c) 2005-2006 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:  Implements the Services DB API
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include <e32std.h>
       
    20 #include <bautils.h>
       
    21 #include <StringLoader.h>
       
    22 
       
    23 #include <services_db.h>
       
    24 
       
    25 // ======== MEMBER FUNCTIONS ========
       
    26 
       
    27 // ---------------------------------------------------------------------------
       
    28 // CServicesDB::NewLC()
       
    29 //
       
    30 //
       
    31 // ---------------------------------------------------------------------------
       
    32 //
       
    33 EXPORT_C CServicesDB* CServicesDB::NewLC( TResourceReader* aResReader )
       
    34     {
       
    35     CServicesDB* self = new (ELeave) CServicesDB();
       
    36     CleanupStack::PushL(self);
       
    37     self->ConstructL(aResReader);
       
    38     return self;
       
    39     }
       
    40 
       
    41 // ---------------------------------------------------------------------------
       
    42 // CServicesDB::NewL()
       
    43 //
       
    44 //
       
    45 // ---------------------------------------------------------------------------
       
    46 //    
       
    47 EXPORT_C CServicesDB* CServicesDB::NewL( TResourceReader* aResReader )
       
    48     {
       
    49     CServicesDB* self = CServicesDB::NewLC(aResReader);
       
    50     CleanupStack::Pop(self);
       
    51     return self;
       
    52     }
       
    53 
       
    54 // ---------------------------------------------------------------------------
       
    55 // CServicesDB::CServicesDB()
       
    56 //
       
    57 //
       
    58 // ---------------------------------------------------------------------------
       
    59 //
       
    60 CServicesDB::CServicesDB()
       
    61     {
       
    62     //no implementation necessary
       
    63     }
       
    64 
       
    65 // ---------------------------------------------------------------------------
       
    66 // CServicesDB::~CServicesDB()
       
    67 //
       
    68 //
       
    69 // ---------------------------------------------------------------------------
       
    70 //    
       
    71 EXPORT_C CServicesDB::~CServicesDB()
       
    72     {
       
    73     iServiceUids.Close();
       
    74     iServiceNames.ResetAndDestroy();
       
    75     iServiceLoc.ResetAndDestroy();
       
    76     }
       
    77 
       
    78 // ---------------------------------------------------------------------------
       
    79 // The class is constructed by reading the data from the specified resource id into memory
       
    80 // ---------------------------------------------------------------------------
       
    81 //
       
    82 void CServicesDB::ConstructL( TResourceReader* aResReader )
       
    83     {
       
    84     TInt i;
       
    85     TInt count = aResReader->ReadInt16();
       
    86     for ( i = 0; i < count; i++ )
       
    87         {
       
    88         //read the service uid
       
    89         TInt uid=aResReader->ReadInt32();
       
    90         if(uid == 0)User::Leave(KErrArgument);
       
    91         //read the service name
       
    92         HBufC* service=aResReader->ReadHBufCL();
       
    93         if(!service)User::Leave(KErrArgument);
       
    94         //read the localized string
       
    95         HBufC* serviceLoc=aResReader->ReadHBufCL();
       
    96         if(!serviceLoc)User::Leave(KErrArgument);
       
    97         
       
    98         //append things
       
    99         iServiceUids.Append(uid);
       
   100         iServiceNames.Append(service);//takes ownership
       
   101         iServiceLoc.Append(serviceLoc);//takes ownership
       
   102         }
       
   103     //done
       
   104     }            
       
   105 
       
   106 // ---------------------------------------------------------------------------
       
   107 // Returns the number of available services 
       
   108 // (the number of services read from the resource during construction)
       
   109 // ---------------------------------------------------------------------------
       
   110 //
       
   111 EXPORT_C TInt CServicesDB::Count() const
       
   112     {
       
   113     return iServiceUids.Count();
       
   114     }
       
   115 
       
   116 // ---------------------------------------------------------------------------
       
   117 // Function to return the Uid of a service (by index).
       
   118 // ---------------------------------------------------------------------------
       
   119 //
       
   120 EXPORT_C TUid CServicesDB::ServiceUidL(TInt aIndex) const
       
   121     {
       
   122     if(aIndex<0 || aIndex>=iServiceUids.Count())
       
   123         User::Leave(KErrArgument);
       
   124     return TUid::Uid(iServiceUids[aIndex]);
       
   125     }
       
   126     
       
   127     
       
   128 // ---------------------------------------------------------------------------
       
   129 // Function to return the localized name associated with a service. If the returned 
       
   130 // strings accepts a parameter, this can be specified as aParam
       
   131 // ---------------------------------------------------------------------------
       
   132 //
       
   133 EXPORT_C HBufC* CServicesDB::ServiceStringLC(TInt aIndex, const TDes& aParam) const
       
   134     {
       
   135     TInt size=aParam.Size();
       
   136     HBufC *string=NULL;
       
   137     
       
   138     //check parameters
       
   139     if(aIndex<0 || aIndex>=iServiceUids.Count())
       
   140         User::Leave(KErrArgument);
       
   141     
       
   142     //get the size of the new string
       
   143     size+=iServiceLoc[aIndex]->Size();
       
   144     //allocate the string
       
   145     string = HBufC::NewLC(size);
       
   146     //create string content
       
   147     TPtr ptr=string->Des();
       
   148     if(aParam.Size())
       
   149         {
       
   150         //we have something in aParam
       
   151         //ptr.Format(*iServiceLoc[aIndex],&aParam);
       
   152         StringLoader::Format(ptr,*iServiceLoc[aIndex],-1,aParam);
       
   153         }
       
   154     else
       
   155         {
       
   156         //nothing in aParam, just copy the localized string to string
       
   157         ptr.Copy(*iServiceLoc[aIndex]);
       
   158         }
       
   159     //done, return
       
   160     return string; //string is on the stack, too
       
   161     }
       
   162     
       
   163 // ---------------------------------------------------------------------------
       
   164 // Function to return the localized name associated with a service. If the returned 
       
   165 // strings accepts a parameter, this can be specified as aParam
       
   166 // ---------------------------------------------------------------------------
       
   167 //
       
   168 EXPORT_C HBufC* CServicesDB::ServiceStringLC(TUid aServiceUid, const TDes& aParam) const
       
   169     {
       
   170     HBufC* string=NULL;
       
   171     TInt i;
       
   172     
       
   173     //look for the service UIDs
       
   174     for(i=0; i<iServiceUids.Count(); i++)
       
   175         {
       
   176         if(iServiceUids[i] == aServiceUid.iUid)
       
   177             {
       
   178             string = ServiceStringLC(i,aParam);
       
   179             break;//service found
       
   180             }
       
   181         }
       
   182     if(!string)CleanupStack::PushL(string);//otherwise, string is already on the stack!
       
   183     return string;
       
   184     }
       
   185     
       
   186 // ---------------------------------------------------------------------------
       
   187 // Function to return the generic name of a service. 
       
   188 // ---------------------------------------------------------------------------
       
   189 //
       
   190 EXPORT_C HBufC* CServicesDB::ServiceNameLC(TInt aIndex) const
       
   191     {
       
   192     HBufC* string=NULL;
       
   193     TInt size=0;
       
   194      
       
   195     //check parameters
       
   196     if(aIndex<0 || aIndex>=iServiceUids.Count())
       
   197         User::Leave(KErrArgument);
       
   198     
       
   199     //get the size of the new string
       
   200     size=iServiceNames[aIndex]->Size();
       
   201     //allocate the string
       
   202     string = HBufC::NewLC(size);
       
   203     //create string content
       
   204     TPtr ptr=string->Des();
       
   205     ptr.Copy(*iServiceNames[aIndex]);
       
   206     return string;
       
   207     }
       
   208 
       
   209 // ---------------------------------------------------------------------------
       
   210 // Function to return the generic name of a service. 
       
   211 // ---------------------------------------------------------------------------
       
   212 //
       
   213 EXPORT_C HBufC* CServicesDB::ServiceNameLC(TUid aServiceUid) const
       
   214     {
       
   215     HBufC* string=NULL;
       
   216     TInt i;
       
   217     
       
   218     //loc for the service UIDs
       
   219     for(i=0; i<iServiceUids.Count(); i++)
       
   220         {
       
   221         if(iServiceUids[i] == aServiceUid.iUid)
       
   222             {
       
   223             string = ServiceNameLC(i);
       
   224             break;//service found
       
   225             }
       
   226         }
       
   227     if(!string)CleanupStack::PushL(string);//otherwise, string is already on the stack!
       
   228     return string;
       
   229     }
       
   230