imservices/xmppsettingsapi/src/xmppsettingsapi.cpp
changeset 0 e6b17d312c8b
equal deleted inserted replaced
-1:000000000000 0:e6b17d312c8b
       
     1 /*
       
     2 * Copyright (c) 2007 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:  This class handles dynamic name-value pair storing.
       
    15 *                Notice that "setting id"-is parameter used by users of this
       
    16 *                lib. The "Setting key" is used for Central Repository
       
    17 *                operations.
       
    18 *
       
    19 */
       
    20 
       
    21 
       
    22 
       
    23 #include <centralrepository.h>
       
    24 
       
    25 //XMPP includes
       
    26 // #include "GFLOGger.h"
       
    27 #include "xmppservicesettingsapi.h"
       
    28 
       
    29 #include "xmppsettingsapicommon.h"
       
    30 #include "xmppsettingscolumn.h"
       
    31 #include "xmppsettingsrecord.h"
       
    32 
       
    33 const TUint32 KMaxDescLength = 255;
       
    34 
       
    35 // ======== MEMBER FUNCTIONS ========
       
    36 
       
    37 
       
    38 CXmppSettingsApi::CXmppSettingsApi()
       
    39     {
       
    40 
       
    41     }
       
    42 
       
    43 
       
    44 void CXmppSettingsApi::ConstructL()
       
    45     {
       
    46     iRepository = CRepository::NewL( KXmppSettingsApiCenRep );
       
    47     iRecords = CXmppSettingsRecord::NewL( *iRepository );
       
    48     iColumns = CXmppSettingsColumn::NewL( *iRepository );
       
    49     }
       
    50 
       
    51 
       
    52 EXPORT_C CXmppSettingsApi* CXmppSettingsApi::NewL()
       
    53     {
       
    54     CXmppSettingsApi* self = CXmppSettingsApi::NewLC();
       
    55     CleanupStack::Pop( self );
       
    56     return self;
       
    57     }
       
    58 
       
    59 
       
    60 EXPORT_C CXmppSettingsApi* CXmppSettingsApi::NewLC()
       
    61     {
       
    62     CXmppSettingsApi* self = new( ELeave ) CXmppSettingsApi;
       
    63     CleanupStack::PushL( self );
       
    64     self->ConstructL();
       
    65     return self;
       
    66     }
       
    67 
       
    68 
       
    69 EXPORT_C CXmppSettingsApi::~CXmppSettingsApi()
       
    70     {
       
    71     delete iRepository;
       
    72     delete iRecords;
       
    73     delete iColumns;
       
    74     }
       
    75 
       
    76 
       
    77 // ---------------------------------------------------------------------------
       
    78 // This method creates new setting record.
       
    79 // ---------------------------------------------------------------------------
       
    80 //
       
    81 EXPORT_C void CXmppSettingsApi::CreateSettingsRecordL(
       
    82     TUint32& aSettingsRecordId )
       
    83     {
       
    84    // GFLOG1("CXmppSettingsApi::CreateSettingsRecordL - IN");
       
    85 
       
    86     RArray<TUint32> array;
       
    87     CleanupClosePushL( array );
       
    88 
       
    89     iRecords->GetSettingsRecordkeysL( array );
       
    90 
       
    91     if( 0 == array.Count() )
       
    92         {
       
    93         RBuf tmp;
       
    94         tmp.CreateL( KMaxDescLength );
       
    95 
       
    96         TInt error = iRepository->Get( KXmppKeyColumnTable,
       
    97                                        tmp );
       
    98         tmp.Close();
       
    99        // GFLOG2("CXmppSettingsApi::CreateSettingsrecord - error: %d",error);
       
   100         if ( error )
       
   101             {
       
   102            // GFLOG1("CXmppSettingsApi::CreateSettingsrecord - first time ever!");
       
   103            // GFLOG1("CXmppSettingsApi::CreateSettingsrecord - creating KColSettingId");
       
   104 
       
   105             // create column for KColSettingId.
       
   106             User::LeaveIfError( iRepository->Create( KXmppKeyColumnTable,
       
   107                                                      KColSettingId() ) );
       
   108 
       
   109            // GFLOG1("CXmppSettingsApi::CreateSettingsrecord - Initialize settingId record");
       
   110             // Initialize settingId record. Initial value is zero
       
   111             User::LeaveIfError( iRepository->Create( KCurrentSettingIdIndex,
       
   112                                                      0 ) );
       
   113             }
       
   114         }
       
   115 
       
   116     iRecords->CreateSettingsRecordIdL( aSettingsRecordId );
       
   117 
       
   118     CleanupStack::PopAndDestroy( &array );
       
   119 
       
   120    // GFLOG1("CXmppSettingsApi::CreateSettingsRecordL - OUT");
       
   121     }
       
   122 
       
   123 // ---------------------------------------------------------------------------
       
   124 // This method removes settings record from the central repository.
       
   125 // ---------------------------------------------------------------------------
       
   126 //
       
   127 EXPORT_C void CXmppSettingsApi::RemoveSettingsRecordL(
       
   128     TUint32 aSettingsRecordId )
       
   129     {
       
   130    // GFLOG1("CXmppSettingsApi::RemoveSettingsRecordL - IN");
       
   131 
       
   132     TUint32 settingKey( 0 );
       
   133     // First, check that this setting id does exist
       
   134     iRecords->FindSettingsRecordKeyL( aSettingsRecordId,
       
   135                                       settingKey );
       
   136 
       
   137     TUint32 key( KXmppKeyDataTable | settingKey );
       
   138 
       
   139     TUint32 errorKey(0);
       
   140 
       
   141     User::LeaveIfError( iRepository->Delete( key, KXmppMaskColumns, errorKey ) );
       
   142 
       
   143    // GFLOG2("CXmppSettingsApi::RemoveSettingStorageL - errorKey: 0x%08x",errorKey);
       
   144    // GFLOG2("CXmppSettingsApi::RemoveSettingStorageL - errorKey: %d",errorKey);
       
   145    // GFLOG1("CXmppSettingsApi::RemoveSettingsRecordL - OUT");
       
   146     }
       
   147 
       
   148 // ---------------------------------------------------------------------------
       
   149 // This method returns the default settings record (the first).
       
   150 // ---------------------------------------------------------------------------
       
   151 //
       
   152 EXPORT_C void CXmppSettingsApi::DefaultSettingsRecordL(
       
   153     TUint32& aSettingsRecordId)
       
   154     {
       
   155    // GFLOG1("CXmppSettingsApi::DefaultSettingsRecordL - IN");
       
   156     RArray<TUint32> crArray;
       
   157     CleanupClosePushL( crArray );
       
   158 
       
   159     iRecords->GetSettingsRecordkeysL( crArray );
       
   160 
       
   161     TInt settingId( 0 );
       
   162 
       
   163     if( crArray.Count() > 0 )
       
   164         {
       
   165         User::LeaveIfError( iRepository->Get( crArray[0], settingId ) );
       
   166         }
       
   167     
       
   168 
       
   169     CleanupStack::PopAndDestroy( &crArray );
       
   170 
       
   171     aSettingsRecordId = settingId;
       
   172 
       
   173    // GFLOG1("CXmppSettingsApi::DefaultSettingsRecordL - OUT");
       
   174     }
       
   175 
       
   176 // ---------------------------------------------------------------------------
       
   177 // This method returns all setting id.s
       
   178 // ---------------------------------------------------------------------------
       
   179 //
       
   180 EXPORT_C void CXmppSettingsApi::GetSettingsRecordIdArrayL(
       
   181     RArray<TUint32>& aArray )
       
   182     {
       
   183    // GFLOG1("CXmppSettingsApi::GetSettingsRecordIdArrayL - IN");
       
   184 
       
   185     RArray<TUint32> crArray;
       
   186     CleanupClosePushL( crArray );
       
   187 
       
   188     iRecords->GetSettingsRecordkeysL( crArray );
       
   189 
       
   190     TInt id( 0 );
       
   191     for ( TInt i=0; i < crArray.Count(); i++ )
       
   192         {
       
   193         User::LeaveIfError( iRepository->Get( crArray[i], id ) );
       
   194         aArray.Append( id );
       
   195         }
       
   196 
       
   197     CleanupStack::PopAndDestroy( &crArray );
       
   198    // GFLOG1("CXmppSettingsApi::GetSettingsRecordIdArrayL - OUT");
       
   199     }
       
   200 
       
   201 // ---------------------------------------------------------------------------
       
   202 // This method sets the parameter. If column does not exist. it creates new
       
   203 // one.
       
   204 // ---------------------------------------------------------------------------
       
   205 //
       
   206 EXPORT_C void CXmppSettingsApi::SetParamL(
       
   207     TUint32 aSettingsRecordId,
       
   208     const TDesC& aAttrName,
       
   209     const TDesC& aAttrValue )
       
   210     {
       
   211    // GFLOG1("CXmppSettingsApi::SetParamL - IN");
       
   212     // GFLOG4("    CXmppSettingsApi::SetParamL - aSettingsRecordId: %d: PARAM[ %S ][ %S ]",aSettingsRecordId,&aAttrName,&aAttrValue);
       
   213     TUint32 theKey( 0 );
       
   214 
       
   215     GetColumnKeyL( aSettingsRecordId,
       
   216                   aAttrName,
       
   217                   ETrue, // create column if not found.
       
   218                   theKey );
       
   219 
       
   220     RBuf buf;
       
   221     buf.CreateL( KMaxDescLength );
       
   222 
       
   223     // We do this just make sure that the key exists. Overflow is just one of
       
   224     // the indicators.
       
   225     TInt error = iRepository->Get( theKey, buf );
       
   226 
       
   227     buf.Close();
       
   228 
       
   229     // If column is not created for this setting id, then create it (to SETTING-table).
       
   230     if ( error != KErrNotFound &&
       
   231          error != KErrNone &&
       
   232          error != KErrOverflow )
       
   233         {
       
   234        // GFLOG1("CXmppSettingsApi::SetParamL - leaving...");
       
   235         User::Leave( error );
       
   236         }
       
   237     else if ( error == KErrNotFound )
       
   238         {
       
   239        // GFLOG1("CXmppSettingsApi::SetParamL - Creating new column..");
       
   240         User::LeaveIfError( iRepository->Create( theKey, aAttrValue ) );
       
   241         }
       
   242     else
       
   243         {
       
   244        // GFLOG1("CXmppSettingsApi::SetParamL - Setting value to the column..");
       
   245         // And finally, set the value to the column id in the setting id's row ( SETTING-table).
       
   246         User::LeaveIfError( iRepository->Set( theKey, aAttrValue ) );
       
   247         }
       
   248 
       
   249    // GFLOG1("CXmppSettingsApi::SetParamL - OUT");
       
   250     }
       
   251 
       
   252 // ---------------------------------------------------------------------------
       
   253 // This method sets the parameter. If column does not exist. it creates new
       
   254 // one.
       
   255 // ---------------------------------------------------------------------------
       
   256 //
       
   257 EXPORT_C void CXmppSettingsApi::SetParamL(
       
   258     TUint32 aSettingsRecordId,
       
   259     const TDesC& aAttrName,
       
   260     TInt aAttrValue )
       
   261     {
       
   262    // GFLOG1("CXmppSettingsApi::SetParamL - IN");
       
   263     // GFLOG4("    CXmppSettingsApi::SetParamL - aSettingsRecordId: %d: PARAM[ %S ][ %d ]",aSettingsRecordId,&aAttrName,aAttrValue);
       
   264     TUint32 theKey( 0 );
       
   265 
       
   266     GetColumnKeyL( aSettingsRecordId,
       
   267                   aAttrName,
       
   268                   ETrue,
       
   269                   theKey );
       
   270 
       
   271     TInt paramValue( 0 );
       
   272 
       
   273     TInt error = iRepository->Get( theKey, paramValue );
       
   274 
       
   275     // If column is not created for this setting id, then create it (to SETTING-table).
       
   276     if ( error != KErrNotFound &&
       
   277          error != KErrNone )
       
   278         {
       
   279        // GFLOG1("CXmppSettingsApi::SetParamL - leaving...");
       
   280         User::Leave( error );
       
   281         }
       
   282     else if ( error == KErrNotFound )
       
   283         {
       
   284        // GFLOG1("CXmppSettingsApi::SetParamL - Creating new column..");
       
   285         User::LeaveIfError( iRepository->Create( theKey, aAttrValue ) );
       
   286         }
       
   287     else
       
   288         {
       
   289        // GFLOG1("CXmppSettingsApi::SetParamL - Setting value to the column..");
       
   290         // And finally, set the value to the column id in the setting id's row ( SETTING-table).
       
   291         User::LeaveIfError( iRepository->Set( theKey, aAttrValue ) );
       
   292         }
       
   293    // GFLOG1("CXmppSettingsApi::SetParamL - OUT");
       
   294     }
       
   295 
       
   296 // ---------------------------------------------------------------------------
       
   297 // This method gets the value. If column does not exits it leaves.
       
   298 // ---------------------------------------------------------------------------
       
   299 //
       
   300 EXPORT_C void CXmppSettingsApi::GetParamL(
       
   301     TUint32 aSettingsRecordId,
       
   302     const TDesC& aAttrName,
       
   303     TDes& aAttrValue )
       
   304     {
       
   305    // GFLOG1("CXmppSettingsApi::GetParamL - IN");
       
   306     // GFLOG3("    CXmppSettingsApi::GetParamL - aSettingsRecordId: %d: PARAM[ %S ]",aSettingsRecordId,&aAttrName);
       
   307     TUint32 theKey( 0 );
       
   308 
       
   309     GetColumnKeyL( aSettingsRecordId,
       
   310                   aAttrName,
       
   311                   EFalse, // Don't create column but leave if not found.
       
   312                   theKey );
       
   313 
       
   314     User::LeaveIfError( iRepository->Get( theKey, aAttrValue ) );
       
   315    // GFLOG1("CXmppSettingsApi::GetParamL - OUT");
       
   316     }
       
   317 
       
   318 // ---------------------------------------------------------------------------
       
   319 // This method gets the value. If column does not exits it leaves.
       
   320 // ---------------------------------------------------------------------------
       
   321 //
       
   322 EXPORT_C void CXmppSettingsApi::GetParamL(
       
   323     TUint32 aSettingsRecordId,
       
   324     const TDesC& aAttrName,
       
   325     TInt& aAttrValue )
       
   326     {
       
   327    // GFLOG1("CXmppSettingsApi::GetParamL - IN");
       
   328     // GFLOG3("    CXmppSettingsApi::GetParamL - aSettingsRecordId: %d: PARAM[ %S ]",aSettingsRecordId,&aAttrName);
       
   329     TUint32 theKey( 0 );
       
   330 
       
   331     GetColumnKeyL( aSettingsRecordId,
       
   332                   aAttrName,
       
   333                   EFalse, // Don't create column but leave if not found.
       
   334                   theKey );
       
   335 
       
   336     User::LeaveIfError( iRepository->Get( theKey, aAttrValue ) );
       
   337    // GFLOG1("CXmppSettingsApi::GetParamL - OUT");
       
   338     }
       
   339 
       
   340 // ---------------------------------------------------------------------------
       
   341 // This method removes the value of the param from given settings record.
       
   342 // ---------------------------------------------------------------------------
       
   343 //
       
   344 EXPORT_C void CXmppSettingsApi::RemoveParamL(
       
   345     TUint32 aSettingsRecordId,
       
   346     const TDesC& aAttrName )
       
   347     {
       
   348    // GFLOG1("CXmppSettingsApi::RemoveParamL - IN");
       
   349     TUint32 settingKey( 0 );
       
   350     // First, check that this setting id does exist
       
   351     iRecords->FindSettingsRecordKeyL( aSettingsRecordId,
       
   352                                       settingKey );
       
   353 
       
   354     TUint32 colKey( 0 );
       
   355 
       
   356     // Find out the id of the column (and does the column exist)
       
   357     iColumns->FindByNameL( aAttrName,
       
   358                                  colKey );
       
   359 
       
   360    // GFLOG1("CXmppSettingsApi::RemoveParamL - Delete item");
       
   361     TUint32 removeKey( settingKey | colKey );
       
   362 
       
   363    // GFLOG2("CXmppSettingsApi::RemoveParamL - colKey:        0x%08x", colKey );
       
   364    // GFLOG2("CXmppSettingsApi::RemoveParamL - settingKey:    0x%08x", settingKey );
       
   365    // GFLOG2("CXmppSettingsApi::RemoveParamL - removeKey:     0x%08x", removeKey );
       
   366 
       
   367     User::LeaveIfError( iRepository->Delete( removeKey ) );
       
   368    // GFLOG1("CXmppSettingsApi::RemoveParamL - OUT");
       
   369     }
       
   370 
       
   371 
       
   372 // ---------------------------------------------------------------------------
       
   373 // Do-method for all the SetParam-methods.
       
   374 // ---------------------------------------------------------------------------
       
   375 //
       
   376 EXPORT_C void CXmppSettingsApi::GetRepositoryKeyL(
       
   377     TUint32 aSettingsRecordId,
       
   378     const TDesC& aAttrName,
       
   379     TBool aCreateColumnIfNotExist,
       
   380     TUint32& aTheKey )
       
   381     {
       
   382 	 GetColumnKeyL( aSettingsRecordId,
       
   383                   aAttrName,
       
   384                   aCreateColumnIfNotExist , // create column if not found.
       
   385                   aTheKey );
       
   386     }
       
   387     
       
   388 // PRIVATE
       
   389 
       
   390 
       
   391 // ---------------------------------------------------------------------------
       
   392 // Do-method for all the SetParam-methods.
       
   393 // ---------------------------------------------------------------------------
       
   394 //
       
   395 void CXmppSettingsApi::GetColumnKeyL(
       
   396     TUint32 aSettingsRecordId,
       
   397     const TDesC& aAttrName,
       
   398     TBool aCreateColumnIfNotExist,
       
   399     TUint32& aTheKey )
       
   400     {
       
   401 
       
   402     TUint32 settingKey( 0 );
       
   403     // First, check that this setting id does exist.
       
   404     iRecords->FindSettingsRecordKeyL( aSettingsRecordId,
       
   405                                       settingKey );
       
   406 
       
   407     // Try to find Column number from the COLUMN-TABLE.
       
   408     TUint32 col( 0 );
       
   409     iColumns->GetColumnKeyL( aAttrName,
       
   410                             aCreateColumnIfNotExist,
       
   411                             col );
       
   412 
       
   413     // Find that is column already created to this setting id in SETTING-table.
       
   414 
       
   415     aTheKey = KXmppKeyDataTable | settingKey | col;
       
   416 
       
   417    // GFLOG2("CXmppSettingsApi::GetColumnKey - table | settingKey | aTheKey: 0x%08x", aTheKey);
       
   418 
       
   419     }