wvsettings20/IMPSSrc/CIMPSSAPCenRep.cpp
changeset 0 094583676ce7
equal deleted inserted replaced
-1:000000000000 0:094583676ce7
       
     1 /*
       
     2 * Copyright (c) 2005 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:  IMPS SAP Settings Store implementation.
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 // INCLUDE FILES
       
    20 
       
    21 #include 	<e32std.h>
       
    22 #include    <centralrepository.h>
       
    23 //#ifdef SYMBIAN_ENABLE_SPLIT_HEADERS
       
    24 //#include <centralrepositoryinternal.h>
       
    25 //#endif
       
    26 #include    "CIMPSSAPCenRep.h"
       
    27 #include	"cimpssapsettings.h"
       
    28 #include	"CIMPSSAPSettingsBody.h"
       
    29 #include	"cimpssapsettingslist.h"
       
    30 #include	"CIMPSSAPKeyValuePairs.h"
       
    31 #include	"CIMPSSAPKeyValuePair.h"
       
    32 #include	"CIMPSSAPNotifier.h"
       
    33 
       
    34 #include	"IMPSSAPSettingsStoreCenRepUids.h"
       
    35 #include    "IMPSSAPSettingsStoreDebugPrint.h"
       
    36 #include    "IMPSSAPSettingsStorePanics.h"
       
    37 
       
    38 #include	"RIMPSReleaseArray.h"
       
    39 
       
    40 #include 	"TIMPSSAPSettingsListItemNameKey.h"
       
    41 #include	"TIMPSCenRepOperations.h"
       
    42 
       
    43 // This implementation uses the following way of dividing the
       
    44 // central repository id range for storing
       
    45 // SAP Settings data:
       
    46 //
       
    47 // - 6 bits for base id(defines the SAP access group)
       
    48 // - 10 bits for sap uid(unique within all groups)
       
    49 // - 16 bits for settings, which is further divided into 256
       
    50 // subsets each containing 256 items. Currently 2 subsets are in use.
       
    51 // First of the subsets contains settings that have fixed identifiers
       
    52 // and the second one key-value pairs.
       
    53 
       
    54 
       
    55 /*
       
    56 Example:
       
    57 ------------------------------
       
    58 
       
    59 cenrep entry: 3221356546 int 8081 0
       
    60 
       
    61 =
       
    62 
       
    63 110000 		| 0000000010 	| 0000000000000010
       
    64 ------ 		  ----------   	  ----------------
       
    65 SAP			| SAP Uid    	| Setting Id
       
    66 Base
       
    67 
       
    68 6 bits, 	  10 bits	  	  16 bits
       
    69 110000		 ..010 = 	 	 (10 = 2 dec = ESAPPort in TFixedSAPSetting)
       
    70 (0x30)		   2 dec
       
    71 PEC base
       
    72 id
       
    73 
       
    74 */
       
    75 
       
    76 // ============================ MEMBER FUNCTIONS ===============================
       
    77 
       
    78 // -----------------------------------------------------------------------------
       
    79 // CIMPSSAPCenRep::CIMPSSAPCenRep
       
    80 // C++ default constructor can NOT contain any code, that
       
    81 // might leave.
       
    82 // -----------------------------------------------------------------------------
       
    83 //
       
    84 CIMPSSAPCenRep::CIMPSSAPCenRep()
       
    85     {
       
    86     }
       
    87 
       
    88 // -----------------------------------------------------------------------------
       
    89 // CIMPSSAPCenRep::ConstructL
       
    90 // Symbian 2nd phase constructor can leave.
       
    91 // -----------------------------------------------------------------------------
       
    92 //
       
    93 void CIMPSSAPCenRep::ConstructL( TInt aPriority )
       
    94     {
       
    95     iRepository = CRepository::NewL( KCRUidIMPSStore );
       
    96     iNotifier = CIMPSSAPNotifier::NewL( aPriority, KCRUidIMPSStore );
       
    97     iCommsDatabase = CCommsDatabase::NewL( EDatabaseTypeIAP );
       
    98     SetUpDefaultsL();
       
    99     }
       
   100 
       
   101 // -----------------------------------------------------------------------------
       
   102 // CIMPSSAPCenRep::NewL
       
   103 // Two-phased constructor.
       
   104 // -----------------------------------------------------------------------------
       
   105 //
       
   106 CIMPSSAPCenRep* CIMPSSAPCenRep::NewL( TInt aPriority )
       
   107     {
       
   108     CIMPSSAPCenRep* self = CIMPSSAPCenRep::NewLC( aPriority );
       
   109     CleanupStack::Pop();
       
   110 
       
   111     return self;
       
   112     }
       
   113 
       
   114 // -----------------------------------------------------------------------------
       
   115 // CIMPSSAPCenRep::NewLC
       
   116 // Two-phased constructor.
       
   117 // -----------------------------------------------------------------------------
       
   118 //
       
   119 CIMPSSAPCenRep* CIMPSSAPCenRep::NewLC( TInt aPriority )
       
   120     {
       
   121     CIMPSSAPCenRep* self = new( ELeave ) CIMPSSAPCenRep();
       
   122 
       
   123     CleanupStack::PushL( self );
       
   124     self->ConstructL( aPriority );
       
   125 
       
   126     return self;
       
   127     }
       
   128 
       
   129 // -----------------------------------------------------------------------------
       
   130 // CIMPSSAPCenRep::~CIMPSSAPCenRep
       
   131 // Destructor
       
   132 // -----------------------------------------------------------------------------
       
   133 //
       
   134 CIMPSSAPCenRep::~CIMPSSAPCenRep()
       
   135     {
       
   136     delete iRepository;
       
   137     delete iNotifier;
       
   138     delete iCommsDatabase;
       
   139     }
       
   140 
       
   141 // HELPER FUNCTIONS
       
   142 
       
   143 // -----------------------------------------------------------------------------
       
   144 // CIMPSSAPCenRep::MaskedId()
       
   145 // Calculates masked central repository id for setting
       
   146 // (other items were commented in a header).
       
   147 // -----------------------------------------------------------------------------
       
   148 //
       
   149 TUint32 CIMPSSAPCenRep::MaskedId( TInt aSetting, TUint32 aUid,
       
   150                                   TIMPSAccessGroup aGroup )
       
   151     {
       
   152     __ASSERT_ALWAYS( ( ( aGroup & EIMPSAccessFilterAll ) == EIMPSIMAccessGroup ) ||
       
   153                      ( ( aGroup & EIMPSAccessFilterAll ) == EIMPSPECAccessGroup ),
       
   154                      Panic( EIMPSInvalidAccessGroup ) );
       
   155 
       
   156     TUint32 maskBase( 0 );
       
   157     if ( aGroup & EIMPSIMAccessGroup )
       
   158         {
       
   159         maskBase += KSAPSettingsIMBaseId << KBaseOffset;
       
   160         }
       
   161     else
       
   162         {
       
   163         maskBase += KSAPSettingsPECBaseId << KBaseOffset;
       
   164         }
       
   165     maskBase += aUid << KSAPOffset;
       
   166     maskBase += aSetting;
       
   167 
       
   168     return maskBase;
       
   169     }
       
   170 
       
   171 // -----------------------------------------------------------------------------
       
   172 // CIMPSSAPCenRep::SAPNameExistsL()
       
   173 // Checks if SAP with given name already exists in given access group.
       
   174 // (other items were commented in a header).
       
   175 // -----------------------------------------------------------------------------
       
   176 //
       
   177 TBool CIMPSSAPCenRep::SAPNameExistsL( const TDesC& aSAPName, TUint32 aUid,
       
   178                                       TIMPSAccessGroup aGroup )
       
   179     {
       
   180     __ASSERT_ALWAYS( ( ( aGroup & EIMPSAccessFilterAll ) == EIMPSIMAccessGroup ) ||
       
   181                      ( ( aGroup & EIMPSAccessFilterAll ) == EIMPSPECAccessGroup ),
       
   182                      Panic( EIMPSInvalidAccessGroup ) );
       
   183 
       
   184     CIMPSSAPSettingsList* list = CIMPSSAPSettingsList::NewLC();
       
   185 
       
   186     PopulateSAPSettingsListL( *list, aGroup );
       
   187 
       
   188     TInt count( list->Count() );
       
   189     TBool alreadyExists( EFalse );
       
   190     TInt index( 0 );
       
   191     if ( list->FindNameL( aSAPName, index ) == 0 )
       
   192         {
       
   193         if ( list->UidForIndex( index ) != aUid )
       
   194             {
       
   195             alreadyExists = ETrue;
       
   196             }
       
   197         }
       
   198 
       
   199 
       
   200     CleanupStack::PopAndDestroy( list );
       
   201     return alreadyExists;
       
   202     }
       
   203 
       
   204 // -----------------------------------------------------------------------------
       
   205 // CIMPSSAPCenRep::SAPProtectionL()
       
   206 // Checks SAP protection level.
       
   207 // (other items were commented in a header).
       
   208 // -----------------------------------------------------------------------------
       
   209 //
       
   210 
       
   211 TIMPSSAPProtection CIMPSSAPCenRep::SAPProtectionL( TUint32 aUid,
       
   212                                                    TIMPSAccessGroup aGroup )
       
   213     {
       
   214     __ASSERT_ALWAYS( ( ( aGroup & EIMPSAccessFilterAll ) == EIMPSIMAccessGroup ) ||
       
   215                      ( ( aGroup & EIMPSAccessFilterAll ) == EIMPSPECAccessGroup ),
       
   216                      Panic( EIMPSInvalidAccessGroup ) );
       
   217 
       
   218     TInt ret( ESAPNoProtection );
       
   219     User::LeaveIfError( iRepository->Get( EProtection + aUid, ret ) );
       
   220     return static_cast< TIMPSSAPProtection >( ret );
       
   221     }
       
   222 
       
   223 // -----------------------------------------------------------------------------
       
   224 // CIMPSSAPCenRep::NewUidL()
       
   225 // Gets new Uid for new SAP
       
   226 // (other items were commented in a header).
       
   227 // -----------------------------------------------------------------------------
       
   228 //
       
   229 
       
   230 TUint32 CIMPSSAPCenRep::NewUidL()
       
   231     {
       
   232     TInt newUid;
       
   233     TInt ret;
       
   234     ret = iRepository->Get( KSAPHighestUid, newUid );
       
   235     if ( ret == KErrNotFound ) // Highest uid not found, create it
       
   236         {
       
   237         // Check if we have existing SAPs despite not having the highest uid.
       
   238         // This may happen if we have older settings.
       
   239         if ( SAPCountL( EIMPSAccessFilterAll ) > 0 ) // existing SAPs
       
   240             {
       
   241             RArray<TUint32> foundSAPs;
       
   242             CleanupClosePushL( foundSAPs );
       
   243             // FindL return value is currently undocumented,
       
   244             // thus silently consumed for now
       
   245             iRepository->FindL( ESAPName, KSAPPopulateAllMask, foundSAPs );
       
   246             TInt count( foundSAPs.Count() );
       
   247             for ( TInt i( 0 ); i < count; ++i )
       
   248                 {
       
   249                 //transform found cenrep uids to SAP uids
       
   250                 foundSAPs[i] &= KSAPUidMask;
       
   251                 foundSAPs[i] = foundSAPs[i] >> KSAPOffset;
       
   252                 }
       
   253             foundSAPs.SortUnsigned();
       
   254             // Find the highest existing uid
       
   255             for ( TUint32 i( KLastSAPId ); i >= KFirstSAPId;i-- )
       
   256                 {
       
   257                 if ( foundSAPs.Find( i ) == KErrNone )
       
   258                     {
       
   259                     if ( i < KLastSAPId )
       
   260                         {
       
   261                         newUid = i + 1;
       
   262                         }
       
   263                     else
       
   264                         {
       
   265                         newUid = KFirstSAPId;
       
   266                         }
       
   267                     break;
       
   268                     }
       
   269                 }
       
   270             CleanupStack::PopAndDestroy(); // foundSAPs
       
   271             }
       
   272         else // No existing SAPs
       
   273             {
       
   274             newUid = KFirstSAPId;
       
   275             }
       
   276         User::LeaveIfError( iRepository->Create( KSAPHighestUid, newUid ) );
       
   277         }
       
   278 
       
   279     else // Otherwise if it's an error situation
       
   280         {
       
   281         User::LeaveIfError( ret );
       
   282         }
       
   283 
       
   284     if ( newUid < KFirstSAPId )
       
   285         {
       
   286         // Something is wrong if we get here
       
   287         User::Leave( KErrGeneral );
       
   288         }
       
   289     else if ( newUid >= KLastSAPId ) // If at the end of the range
       
   290         {
       
   291         // start from the beginning
       
   292         newUid = KFirstSAPId;
       
   293         }
       
   294     // Check if there are ANY free uids
       
   295     if ( ( KLastSAPId - KFirstSAPId ) <= SAPCountL( EIMPSAccessFilterAll ) )
       
   296         {
       
   297         User::Leave( KErrInUse ); // All UIDs in use
       
   298         }
       
   299 
       
   300     // Check if there is the next uid is unused.
       
   301     TBool okId( EFalse );
       
   302     TInt err( KErrNone );
       
   303     while ( !okId )
       
   304         {
       
   305         newUid++;
       
   306         TUint32 maskedUid = newUid << KSAPOffset;
       
   307         RArray<TUint32> foundSAPs;
       
   308         CleanupClosePushL( foundSAPs );
       
   309         err = iRepository->FindL( maskedUid, KSAPExistsMask, foundSAPs );
       
   310         if ( err == KErrNotFound ) // The uid is free
       
   311             {
       
   312             okId = ETrue;
       
   313             }
       
   314         else if ( err == KErrNone ) // There was a SAP with that uid
       
   315             {
       
   316             // Check range
       
   317             if ( newUid >= KLastSAPId )
       
   318                 {
       
   319                 newUid = KFirstSAPId; // Start from the beginning.
       
   320                 }
       
   321             }
       
   322         else
       
   323             {
       
   324             User::Leave( err );
       
   325             }
       
   326         CleanupStack::PopAndDestroy(); //foundSAPs
       
   327         }
       
   328     // Save the new highest
       
   329     User::LeaveIfError( iRepository->Set( KSAPHighestUid, newUid ) );
       
   330     return newUid; // return the new uid
       
   331     }
       
   332 
       
   333 // -----------------------------------------------------------------------------
       
   334 // CIMPSSAPCenRep::SAPTypeL()
       
   335 // Gets SAP type
       
   336 // (other items were commented in a header).
       
   337 // -----------------------------------------------------------------------------
       
   338 //
       
   339 
       
   340 TIMPSAccessGroup CIMPSSAPCenRep::SAPTypeL( TUint32 aUid )
       
   341     {
       
   342     TIMPSAccessGroup type( EIMPSNoAccessGroup );
       
   343     aUid &= KSAPBaseMask;
       
   344     aUid = aUid >> KBaseOffset;
       
   345     if ( aUid == KSAPSettingsPECBaseId )
       
   346         {
       
   347         type = EIMPSPECAccessGroup;
       
   348         }
       
   349     else if ( aUid == KSAPSettingsIMBaseId )
       
   350         {
       
   351         type = EIMPSIMAccessGroup;
       
   352         }
       
   353     else
       
   354         {
       
   355         // aUid was found in unknown access group. Should not happen.
       
   356         User::Leave( KErrGeneral );
       
   357         }
       
   358     return type;
       
   359     }
       
   360 
       
   361 // -----------------------------------------------------------------------------
       
   362 // CIMPSSAPCenRep::SAPExistsL()
       
   363 // Checks if SAP exists in central repository
       
   364 // (other items were commented in a header).
       
   365 // -----------------------------------------------------------------------------
       
   366 //
       
   367 
       
   368 void CIMPSSAPCenRep::SAPExistsL( TUint32 aUid )
       
   369     {
       
   370     RArray<TUint32> foundSAPs;
       
   371     CleanupClosePushL( foundSAPs );
       
   372     // FindL return value is currently undocumented,
       
   373     // thus silently consumed for now
       
   374     iRepository->FindL( aUid, KSAPExistsMask, foundSAPs );
       
   375     if ( foundSAPs.Count() != 1 )
       
   376         {
       
   377         User::Leave( KErrNotFound );
       
   378         }
       
   379     CleanupStack::PopAndDestroy(); //foundSAPs
       
   380     }
       
   381 
       
   382 // -----------------------------------------------------------------------------
       
   383 // CIMPSSAPCenRep::SetUpDefaultsL()
       
   384 // Sets up default SAP ids if they do not exist in the repository
       
   385 // (other items were commented in a header).
       
   386 // -----------------------------------------------------------------------------
       
   387 //
       
   388 
       
   389 void CIMPSSAPCenRep::SetUpDefaultsL()
       
   390     {
       
   391     TInt uid( 0 );
       
   392     TInt err ( iRepository->Get( KDefaultIMSAPId, uid ) );
       
   393     if ( err == KErrNotFound )
       
   394         {
       
   395         User::LeaveIfError( iRepository->Create( KDefaultIMSAPId,
       
   396                                                  KIMPSSettingsNullUid ) );
       
   397         }
       
   398     else
       
   399         {
       
   400         User::LeaveIfError( err );
       
   401         }
       
   402 
       
   403     err = iRepository->Get( KDefaultPECSAPId, uid );
       
   404     if ( err == KErrNotFound )
       
   405         {
       
   406         User::LeaveIfError( iRepository->Create( KDefaultPECSAPId,
       
   407                                                  KIMPSSettingsNullUid ) );
       
   408         }
       
   409     else
       
   410         {
       
   411         User::LeaveIfError( err );
       
   412         }
       
   413     }
       
   414 
       
   415 // -----------------------------------------------------------------------------
       
   416 // CIMPSSAPCenRep::FindSAPsFromAccessGroupL()
       
   417 // Finds and populates SAP ids to an array from desired access group
       
   418 // (other items were commented in a header).
       
   419 // -----------------------------------------------------------------------------
       
   420 //
       
   421 
       
   422 void CIMPSSAPCenRep::FindSAPsFromAccessGroupL( TIMPSAccessGroup aGroup,
       
   423                                                RArray<TUint32>& aFoundSAPs )
       
   424     {
       
   425     if ( aGroup == EIMPSAccessFilterAll )
       
   426         {
       
   427         iRepository->FindL( ESAPAddress, KSAPPopulateAllMask, aFoundSAPs );
       
   428         }
       
   429     else if ( aGroup == EIMPSIMAccessGroup )
       
   430         {
       
   431         iRepository->FindL( MaskedId( ESAPAddress, KIMPSSettingsNullUid,
       
   432                                       EIMPSIMAccessGroup ), KSAPPopulateGroupMask, aFoundSAPs );
       
   433         }
       
   434     else if ( aGroup == EIMPSPECAccessGroup )
       
   435         {
       
   436         iRepository->FindL( MaskedId( ESAPAddress, KIMPSSettingsNullUid,
       
   437                                       EIMPSPECAccessGroup ), KSAPPopulateGroupMask, aFoundSAPs );
       
   438         }
       
   439     }
       
   440 
       
   441 
       
   442 // -----------------------------------------------------------------------------
       
   443 // CIMPSSAPCenRep::StartOwnTransaction()
       
   444 // Starts new transaction if there is no ongoing transaction
       
   445 // (other items were commented in a header).
       
   446 // -----------------------------------------------------------------------------
       
   447 //
       
   448 TBool CIMPSSAPCenRep::StartOwnTransactionL( TInt aMode )
       
   449     {
       
   450 //    TInt transaction( iRepository->TransactionState() );
       
   451 
       
   452  	if( !iInTransaction )
       
   453  		{
       
   454  		iInTransaction = ETrue;
       
   455  		CleanupStack::PushL( TCleanupItem( ReSetTransactionState, this ));
       
   456  		iRepository->StartTransaction( static_cast<CRepository::TTransactionMode>( aMode ) );
       
   457  		return ETrue;
       
   458  		}
       
   459 	return EFalse;
       
   460     }
       
   461 
       
   462 // OBSERVER SUPPORT
       
   463 
       
   464 // -----------------------------------------------------------------------------
       
   465 // CIMPSSAPCenRep::AddObserverL()
       
   466 // Adds SAP observer
       
   467 // (other items were commented in a header).
       
   468 // -----------------------------------------------------------------------------
       
   469 //
       
   470 void CIMPSSAPCenRep::AddObserverL( MIMPSSAPObserver* aObserver,
       
   471                                    TIMPSAccessGroup aGroup )
       
   472     {
       
   473     SSS_DP_TXT( "CIMPSSAPCenRep::AddObserverL()" );
       
   474     iNotifier->AddObserverL( aObserver, aGroup );
       
   475     }
       
   476 
       
   477 // -----------------------------------------------------------------------------
       
   478 // CIMPSSAPCenRep::RemoveObserverL()
       
   479 // Removes SAP observer
       
   480 // (other items were commented in a header).
       
   481 // -----------------------------------------------------------------------------
       
   482 //
       
   483 void CIMPSSAPCenRep::RemoveObserver( MIMPSSAPObserver* aObserver )
       
   484     {
       
   485     SSS_DP_TXT( "CIMPSSAPCenRep::RemoveObserver()" );
       
   486     iNotifier->RemoveObserver( aObserver );
       
   487     }
       
   488 
       
   489 // SAP SETTINGS
       
   490 
       
   491 // -----------------------------------------------------------------------------
       
   492 // CIMPSSAPCenRep::StoreNewSAPL()
       
   493 // Stores new SAP in central repository
       
   494 // (other items were commented in a header).
       
   495 // -----------------------------------------------------------------------------
       
   496 //
       
   497 TUint32 CIMPSSAPCenRep::StoreNewSAPL( CIMPSSAPSettings* aSAPSettings,
       
   498                                       TIMPSAccessGroup aGroup )
       
   499     {
       
   500     SSS_DP_TXT( "CIMPSSAPCenRep::StoreNewSAPL()" );
       
   501 
       
   502     __ASSERT_ALWAYS( ( ( aGroup & EIMPSAccessFilterAll ) == EIMPSIMAccessGroup ) ||
       
   503                      ( ( aGroup & EIMPSAccessFilterAll ) == EIMPSPECAccessGroup ),
       
   504                      Panic( EIMPSInvalidAccessGroup ) );
       
   505 
       
   506     // SAP name must be defined
       
   507     if ( aSAPSettings->SAPName().Length() == 0 )
       
   508         {
       
   509         User::Leave( KErrArgument );
       
   510         }
       
   511 
       
   512     // no documentation for StartTransaction() return values, leave if any error
       
   513 	iInTransaction = ETrue;
       
   514 	CleanupStack::PushL(TCleanupItem( ReSetTransactionState, this ));
       
   515     User::LeaveIfError( iRepository->StartTransaction( CRepository::EReadWriteTransaction ) );
       
   516 
       
   517     iRepository->CleanupCancelTransactionPushL();
       
   518 
       
   519     //Check if SAP with given name already exists
       
   520 
       
   521     // KIMPSSettingsNullUid cannot exist so this checks for all duplicates
       
   522     if ( SAPNameExistsL( aSAPSettings->SAPName(), KIMPSSettingsNullUid,
       
   523                          aGroup ) )
       
   524         {
       
   525         User::Leave( KErrAlreadyExists );
       
   526         }
       
   527 
       
   528     RIMPSReleaseArray tempArray;
       
   529     CleanupClosePushL( tempArray );
       
   530 
       
   531     // Assign Uid for the new entry, leave if all Uids are in use
       
   532     TUint32 newUid( NewUidL() );
       
   533 
       
   534     //Encrypt passwords before storing has been removed
       
   535     //401-1808 - Ease of Instant Messaging SERVER customization
       
   536     HBufC* tmpPasswd = aSAPSettings->SAPUserPassword().AllocLC();
       
   537     HBufC* tmpHTTPPasswd = aSAPSettings->HTTPProxyUserPassword().AllocLC();
       
   538     TPtr passwdPtr( tmpPasswd->Des() );
       
   539     TPtr httpPasswdPtr( tmpHTTPPasswd->Des() );
       
   540 
       
   541     //get the first stored setting ID as the returnable ID for the client
       
   542     TUint32 returnId( MaskedId( ESAPAddress, newUid, aGroup ) );
       
   543 
       
   544     // If this is the first SAP to be stored in either access group,
       
   545     // set it to default
       
   546     if ( aGroup == EIMPSIMAccessGroup )
       
   547         {
       
   548         if ( SAPCountL( EIMPSIMAccessGroup ) == 0 )
       
   549             {
       
   550             User::LeaveIfError( iRepository->Set( KDefaultIMSAPId,
       
   551                                                   static_cast< TInt >( returnId ) ) );
       
   552             }
       
   553         }
       
   554     else
       
   555         {
       
   556         if ( SAPCountL( EIMPSPECAccessGroup ) == 0 )
       
   557             {
       
   558             User::LeaveIfError( iRepository->Set( KDefaultPECSAPId,
       
   559                                                   static_cast< TInt >( returnId ) ) );
       
   560             }
       
   561         }
       
   562 
       
   563     TIMPSCenRepOperations op( returnId, *iRepository );
       
   564 
       
   565     op.CreateL( ESAPAddress, aSAPSettings->SAPAddress() );
       
   566     op.CreateL( ESAPName, aSAPSettings->SAPName() );
       
   567     op.CreateL( ESAPPort, static_cast< TInt >( aSAPSettings->SAPPort() ) );
       
   568     op.CreateL( ESAPUserId, aSAPSettings->SAPUserId() );
       
   569     op.CreateL( ESAPUserPassword, passwdPtr );
       
   570     op.CreateL( EHTTPProxyUserId, aSAPSettings->HTTPProxyUserId() );
       
   571     op.CreateL( EHTTPProxyUserPassword, httpPasswdPtr );
       
   572     op.CreateL( EHTTPProxyAddress, aSAPSettings->HTTPProxyAddress() );
       
   573     op.CreateL( EHTTPProxyPort, static_cast< TInt >( aSAPSettings->HTTPProxyPort() ) );
       
   574 
       
   575     HBufC* apName = DoGetAccessPointsNameL(
       
   576                         static_cast< TInt >( aSAPSettings->AccessPoint() ) );
       
   577     CleanupStack::PushL( apName );
       
   578     op.CreateL( EAccessPoint, *apName );
       
   579     CleanupStack::PopAndDestroy( apName );
       
   580 
       
   581     op.CreateL( EClientId, aSAPSettings->ClientId() );
       
   582     op.CreateL( EServerAuthenticationName, aSAPSettings->ServerAuthenticationName() );
       
   583     op.CreateL( EServerAuthenticationPassword, aSAPSettings->ServerAuthenticationPassword() );
       
   584     op.CreateL( EServerAcceptedContentType, aSAPSettings->ServerAcceptedContentType() );
       
   585     op.CreateL( EHighLevelServices, static_cast< TInt >( aSAPSettings->HighLevelServices() ) );
       
   586     op.CreateL( EAuthorizationMode, static_cast< TInt >( aSAPSettings->AuthorizationMode() ) );
       
   587     op.CreateL( EProtection, static_cast< TInt >( aSAPSettings->Protection() ) );
       
   588 
       
   589     const RPointerArray< CIMPSSAPKeyValuePair >& pairs( aSAPSettings->KeyValuePairs().Pairs() );
       
   590 
       
   591     TInt count( pairs.Count() );
       
   592 
       
   593     for ( TInt i( 0 );i < count; ++i )
       
   594         {
       
   595         //HBufC ownership transfers here
       
   596         HBufC* valuePairFlat = pairs[i]->KeyValuePairFlatLC();
       
   597         User::LeaveIfError( iRepository->Create( EKeyValuePairBase +
       
   598                                                  returnId + i,
       
   599                                                  *valuePairFlat ) );
       
   600         // CRepository->Create() is not copying the pointed data during transaction
       
   601         // so these must be stored until the transaction has completed
       
   602         tempArray.AppendL( valuePairFlat );
       
   603         CleanupStack::Pop( valuePairFlat );
       
   604         }
       
   605 
       
   606     TUint32 err( KErrNone );
       
   607     //passed err will be silently consumed because the value is of no use to client
       
   608     User::LeaveIfError( iRepository->CommitTransaction( err ) );
       
   609 	iInTransaction = EFalse;
       
   610 	CleanupStack::PopAndDestroy( 5 ); //cleanupitem, Transaction, tempArray, tmpPasswd, tmpHTTPPasswd
       
   611 
       
   612     aSAPSettings->Body().DoSetUid( returnId );
       
   613     aSAPSettings->Body().DoSetAccessGroup( aGroup );
       
   614     return returnId;
       
   615     }
       
   616 
       
   617 // -----------------------------------------------------------------------------
       
   618 // CIMPSSAPCenRep::GetSAPL()
       
   619 // Gets SAP from central repository
       
   620 // (other items were commented in a header).
       
   621 // -----------------------------------------------------------------------------
       
   622 
       
   623 void CIMPSSAPCenRep::GetSAPL( TUint32 aUid, CIMPSSAPSettings* aSAPSettings )
       
   624     {
       
   625     TRAPD( err, DoGetSAPL( aUid, aSAPSettings ) );
       
   626     if ( err == KErrArgument )
       
   627         {
       
   628         aSAPSettings->Reset();
       
   629         }
       
   630     else if ( err )
       
   631         {
       
   632         User::Leave( err );
       
   633         }
       
   634     }
       
   635 
       
   636 // -----------------------------------------------------------------------------
       
   637 // CIMPSSAPCenRep::DoGetSAPL()
       
   638 // Does the actual get operation
       
   639 // (other items were commented in a header).
       
   640 // -----------------------------------------------------------------------------
       
   641 
       
   642 void CIMPSSAPCenRep::DoGetSAPL( TUint32 aUid, CIMPSSAPSettings* aSAPSettings )
       
   643     {
       
   644     SSS_DP_TXT( "CIMPSSAPCenRep::GetSAPL()" );
       
   645 
       
   646 	TBool transaction( StartOwnTransactionL( CRepository::EReadTransaction ) );
       
   647     if ( transaction )
       
   648         {
       
   649         iRepository->CleanupCancelTransactionPushL();
       
   650         }
       
   651 
       
   652     HBufC* tmpBuffer = HBufC::NewLC( NCentralRepositoryConstants::KMaxUnicodeStringLength );
       
   653     TPtr tmpPtr( tmpBuffer->Des() );
       
   654 
       
   655     TInt tmpInteger( 0 );
       
   656 
       
   657     SAPExistsL( aUid );
       
   658 
       
   659     TIMPSAccessGroup type( SAPTypeL( aUid ) );
       
   660 
       
   661     TIMPSCenRepOperations op( aUid, *iRepository );
       
   662 
       
   663     op.GetL( ESAPAddress, tmpPtr );
       
   664     aSAPSettings->SetSAPAddressL( tmpPtr );
       
   665 
       
   666     op.GetL( ESAPName, tmpPtr );
       
   667     aSAPSettings->SetSAPNameL( tmpPtr );
       
   668 
       
   669     op.GetL( ESAPPort, tmpInteger );
       
   670     aSAPSettings->SetSAPPort( tmpInteger );
       
   671 
       
   672     op.GetL( ESAPUserId, tmpPtr );
       
   673     aSAPSettings->SetSAPUserIdL( tmpPtr );
       
   674 
       
   675     op.GetL( ESAPUserPassword, tmpPtr );
       
   676     //Decrypting of passwrd has been removed
       
   677     //401-1808 - Ease of Instant Messaging SERVER customization
       
   678     aSAPSettings->SetSAPUserPasswordL( tmpPtr );
       
   679 
       
   680     op.GetL( EHTTPProxyUserId, tmpPtr );
       
   681     aSAPSettings->SetHTTPProxyUserIdL( tmpPtr );
       
   682 
       
   683     //Decrypting of passwrd has been removed
       
   684     //401-1808 - Ease of Instant Messaging SERVER customization
       
   685     op.GetL( EHTTPProxyUserPassword, tmpPtr );
       
   686     aSAPSettings->SetHTTPProxyUserPasswordL( tmpPtr );
       
   687 
       
   688     op.GetL( EHTTPProxyAddress, tmpPtr );
       
   689     aSAPSettings->SetHTTPProxyAddressL( tmpPtr );
       
   690 
       
   691     op.GetL( EHTTPProxyPort, tmpInteger );
       
   692     aSAPSettings->SetHTTPProxyPort( tmpInteger );
       
   693 
       
   694     //op.GetL( EAccessPoint, tmpInteger );
       
   695     op.GetL( EAccessPoint, tmpPtr );
       
   696     tmpInteger = static_cast< TInt >( DoGetAccessPointsL( tmpPtr ) );
       
   697     aSAPSettings->SetAccessPoint( tmpInteger );
       
   698 
       
   699     op.GetL( EClientId, tmpPtr );
       
   700     aSAPSettings->SetClientIdL( tmpPtr );
       
   701 
       
   702     op.GetL( EServerAuthenticationName, tmpPtr );
       
   703     aSAPSettings->SetServerAuthenticationNameL( tmpPtr );
       
   704 
       
   705     op.GetL( EServerAuthenticationPassword, tmpPtr );
       
   706     aSAPSettings->SetServerAuthenticationPasswordL( tmpPtr );
       
   707 
       
   708     op.GetL( EServerAcceptedContentType, tmpPtr );
       
   709     aSAPSettings->SetServerAcceptedContentTypeL( tmpPtr );
       
   710 
       
   711     op.GetL( EHighLevelServices, tmpInteger );
       
   712     aSAPSettings->SetHighLevelServices( tmpInteger );
       
   713 
       
   714     op.GetL( EAuthorizationMode, tmpInteger );
       
   715     aSAPSettings->SetAuthorizationMode( tmpInteger );
       
   716 
       
   717     op.GetL( EProtection, tmpInteger );
       
   718     aSAPSettings->ProtectL( static_cast<TIMPSSAPProtection>( tmpInteger ) );
       
   719 
       
   720     RPointerArray< CIMPSSAPKeyValuePair >& pairs( aSAPSettings->KeyValuePairs().Pairs() );
       
   721     pairs.ResetAndDestroy();
       
   722 
       
   723     RArray<TUint32> foundPairs;
       
   724     CleanupClosePushL( foundPairs );
       
   725     iRepository->FindL( EKeyValuePairBase + aUid, KSAPPairsMask, foundPairs );
       
   726     TInt count( foundPairs.Count() );
       
   727 
       
   728     for ( TInt i( 0 );i < count; ++i )
       
   729         {
       
   730         TPtr valuePairFlatPtr( tmpBuffer->Des() );
       
   731 
       
   732         User::LeaveIfError(
       
   733             iRepository->Get( foundPairs[ i ],
       
   734                               valuePairFlatPtr ) );
       
   735 
       
   736         // parse the key-value pair descriptor
       
   737 
       
   738         CIMPSSAPKeyValuePair* pair = CIMPSSAPKeyValuePair::NewLC( valuePairFlatPtr );
       
   739 
       
   740         pairs.AppendL( pair );
       
   741 
       
   742         CleanupStack::Pop( pair );
       
   743         }
       
   744 
       
   745     if ( transaction )
       
   746         {
       
   747         TUint32 err( KErrNone );
       
   748         User::LeaveIfError( iRepository->CommitTransaction( err ) );
       
   749 		iInTransaction = EFalse;
       
   750 		CleanupStack::PopAndDestroy(2); // cleanupitem, transaction
       
   751         }
       
   752     CleanupStack::PopAndDestroy( 2 ); // foundPairs, tmpBuffer
       
   753     aSAPSettings->Body().DoSetAccessGroup( type );
       
   754     aSAPSettings->Body().DoSetUid( aUid );
       
   755     }
       
   756 
       
   757 // -----------------------------------------------------------------------------
       
   758 // CIMPSSAPCenRep::SAPCountL()
       
   759 // Counts the number of SAPs
       
   760 // (other items were commented in a header).
       
   761 // -----------------------------------------------------------------------------
       
   762 //
       
   763 TInt CIMPSSAPCenRep::SAPCountL( TIMPSAccessGroup aGroup )
       
   764     {
       
   765     SSS_DP_TXT( "CIMPSSAPCenRep::SAPCountL()" );
       
   766 
       
   767     __ASSERT_ALWAYS(    aGroup & EIMPSAccessFilterAll,
       
   768                         Panic( EIMPSInvalidAccessGroup ) );
       
   769 
       
   770     RArray<TUint32> foundSAPs;
       
   771     CleanupClosePushL( foundSAPs );
       
   772     FindSAPsFromAccessGroupL( aGroup, foundSAPs );
       
   773     TInt count( foundSAPs.Count() );
       
   774     CleanupStack::PopAndDestroy(); //foundSAPs
       
   775     return count;
       
   776     }
       
   777 
       
   778 
       
   779 // -----------------------------------------------------------------------------
       
   780 // CIMPSSAPCenRep::DeleteSAPL()
       
   781 // Deletes SAP from central repository
       
   782 // (other items were commented in a header).
       
   783 // -----------------------------------------------------------------------------
       
   784 //
       
   785 void CIMPSSAPCenRep::DeleteSAPL( TUint32 aUid )
       
   786     {
       
   787     SSS_DP_TXT( "CIMPSSAPCenRep::DeleteSAPL()" );
       
   788 
       
   789     iInTransaction = ETrue;
       
   790     CleanupStack::PushL( TCleanupItem( ReSetTransactionState, this ));
       
   791     User::LeaveIfError( iRepository->StartTransaction( CRepository::EReadWriteTransaction ) );
       
   792     iRepository->CleanupCancelTransactionPushL();
       
   793 
       
   794     SAPExistsL( aUid );
       
   795     TIMPSAccessGroup type( SAPTypeL( aUid ) );
       
   796 
       
   797     RArray<TUint32> foundIds;
       
   798     CleanupClosePushL( foundIds );
       
   799     iRepository->FindL( aUid, KSAPUidMask, foundIds );
       
   800     TInt count( foundIds.Count() );
       
   801     for ( TInt i( 0 ); i < count; ++i )
       
   802         {
       
   803         User::LeaveIfError( iRepository->Delete( foundIds[ i ] ) );
       
   804         }
       
   805 
       
   806     CleanupStack::PopAndDestroy(); //foundIds
       
   807 
       
   808     TInt sapCount( SAPCountL( type ) );
       
   809 
       
   810     if ( sapCount > 1 )	// deletion not commited yet, but nowadays the API
       
   811         // seems to give up-to-date counts regardless
       
   812         {
       
   813         TUint32 defaultUid;
       
   814         GetDefaultL( defaultUid, type );
       
   815         // default SAP id is set to KIMPSSettingsNullUid if the
       
   816         // default SAP was deleted
       
   817         if ( type == EIMPSIMAccessGroup )
       
   818             {
       
   819             if ( aUid == defaultUid )
       
   820                 {
       
   821                 User::LeaveIfError( iRepository->Set( KDefaultIMSAPId,
       
   822                                                       KIMPSSettingsNullUid ) );
       
   823                 }
       
   824             }
       
   825         else
       
   826             {
       
   827             if ( aUid == defaultUid )
       
   828                 {
       
   829                 User::LeaveIfError( iRepository->Set( KDefaultPECSAPId,
       
   830                                                       KIMPSSettingsNullUid ) );
       
   831                 }
       
   832             }
       
   833         }
       
   834     // If there is only one SAP left after the deletion, set it to default.
       
   835     if ( sapCount == 1 )
       
   836         {
       
   837         CIMPSSAPSettingsList* list = CIMPSSAPSettingsList::NewLC();
       
   838         PopulateSAPSettingsListL( *list, type );
       
   839         TUint32 newDefault( list->UidForIndex( 0 ) );
       
   840         SetToDefaultL( newDefault, type );
       
   841         CleanupStack::PopAndDestroy( list );
       
   842         }
       
   843     // If there are no SAPs left after the deletion, default SAP id is
       
   844     // set to KIMPSSettingsNullUid
       
   845     if ( sapCount == 0 )
       
   846         {
       
   847         if ( type == EIMPSIMAccessGroup )
       
   848             {
       
   849             User::LeaveIfError( iRepository->Set( KDefaultIMSAPId,
       
   850                                                   KIMPSSettingsNullUid ) );
       
   851             }
       
   852         else
       
   853             {
       
   854             User::LeaveIfError( iRepository->Set( KDefaultPECSAPId,
       
   855                                                   KIMPSSettingsNullUid ) );
       
   856             }
       
   857         }
       
   858     TUint32 err;
       
   859     User::LeaveIfError( iRepository->CommitTransaction( err ) );
       
   860 	iInTransaction = EFalse;
       
   861   	CleanupStack::PopAndDestroy(2); //cleanupitem, Transaction
       
   862     }
       
   863 
       
   864 // -----------------------------------------------------------------------------
       
   865 // CIMPSSAPCenRep::UpdateOldSAPL()
       
   866 // Updates existing SAP
       
   867 // (other items were commented in a header).
       
   868 // -----------------------------------------------------------------------------
       
   869 //
       
   870 void CIMPSSAPCenRep::UpdateOldSAPL( CIMPSSAPSettings* aSAPSettings, TUint32 aUid )
       
   871     {
       
   872     SSS_DP_TXT( "CIMPSSAPCenRep::UpdateOldSAPL()" );
       
   873 
       
   874     iInTransaction = ETrue;
       
   875     CleanupStack::PushL( TCleanupItem( ReSetTransactionState, this ));
       
   876     User::LeaveIfError( iRepository->StartTransaction( CRepository::EReadWriteTransaction ) );
       
   877     iRepository->CleanupCancelTransactionPushL();
       
   878 
       
   879     SAPExistsL( aUid );
       
   880     TIMPSAccessGroup type( SAPTypeL( aUid ) );
       
   881     TIMPSSAPProtection protection( SAPProtectionL( aUid, type ) );
       
   882 
       
   883     if ( protection == ESAPBrandProtection )
       
   884         {
       
   885         CIMPSSAPSettings* settings = CIMPSSAPSettings::NewLC();
       
   886         GetSAPL( aUid, settings );
       
   887         if ( ( settings->SAPName().Compare( aSAPSettings->SAPName() ) != 0 ) )
       
   888             {
       
   889             // User is trying to modify protected values, leave
       
   890             User::Leave( KErrAccessDenied );
       
   891             }
       
   892         CleanupStack::PopAndDestroy( settings );
       
   893         }
       
   894 
       
   895     //Check for duplicate SAP names. SAPName in SAP with aUid is permitted to be
       
   896     //same as it is the one we are updating.
       
   897     if ( SAPNameExistsL( aSAPSettings->SAPName(), aUid, type ) )
       
   898         {
       
   899         User::Leave( KErrAlreadyExists );
       
   900         }
       
   901 
       
   902     RIMPSReleaseArray tempArray;
       
   903     CleanupClosePushL( tempArray );
       
   904 
       
   905     //Encrypt password before storing has been removed
       
   906     //401-1808 - Ease of Instant Messaging SERVER customization
       
   907     HBufC* tmpPasswd = aSAPSettings->SAPUserPassword().AllocLC();
       
   908     HBufC* tmpHTTPPasswd = aSAPSettings->HTTPProxyUserPassword().AllocLC();
       
   909     TPtr passwdPtr( tmpPasswd->Des() );
       
   910     TPtr httpPasswdPtr( tmpHTTPPasswd->Des() );
       
   911 
       
   912     TIMPSCenRepOperations op( aUid, *iRepository );
       
   913 
       
   914     op.SetL( ESAPAddress, aSAPSettings->SAPAddress() );
       
   915     op.SetL( ESAPName, aSAPSettings->SAPName() );
       
   916     op.SetL( ESAPPort, static_cast< TInt >( aSAPSettings->SAPPort() ) );
       
   917     op.SetL( ESAPUserId, aSAPSettings->SAPUserId() );
       
   918     op.SetL( ESAPUserPassword, passwdPtr );
       
   919     op.SetL( EHTTPProxyUserId, aSAPSettings->HTTPProxyUserId() );
       
   920     op.SetL( EHTTPProxyUserPassword, httpPasswdPtr );
       
   921     op.SetL( EHTTPProxyAddress, aSAPSettings->HTTPProxyAddress() );
       
   922     op.SetL( EHTTPProxyPort, static_cast< TInt >( aSAPSettings->HTTPProxyPort() ) );
       
   923 
       
   924     HBufC* apName = DoGetAccessPointsNameL(
       
   925                         static_cast< TInt >( aSAPSettings->AccessPoint() ) );
       
   926     CleanupStack::PushL( apName );
       
   927     op.SetL( EAccessPoint, *apName );
       
   928     CleanupStack::PopAndDestroy( apName );
       
   929 
       
   930     op.SetL( EClientId, aSAPSettings->ClientId() );
       
   931     op.SetL( EServerAuthenticationName, aSAPSettings->ServerAuthenticationName() );
       
   932     op.SetL( EServerAuthenticationPassword, aSAPSettings->ServerAuthenticationPassword() );
       
   933     op.SetL( EServerAcceptedContentType, aSAPSettings->ServerAcceptedContentType() );
       
   934     op.SetL( EHighLevelServices, static_cast< TInt >( aSAPSettings->HighLevelServices() ) );
       
   935     op.SetL( EAuthorizationMode, static_cast< TInt >( aSAPSettings->AuthorizationMode() ) );
       
   936     op.SetL( EProtection, static_cast< TInt >( aSAPSettings->Protection() ) );
       
   937 
       
   938     //First delete old key-value pairs
       
   939     RArray<TUint32> foundPairs;
       
   940     CleanupClosePushL( foundPairs );
       
   941     iRepository->FindL( EKeyValuePairBase + aUid, KSAPPairsMask, foundPairs );
       
   942     TInt oldCount( foundPairs.Count() );
       
   943 
       
   944     for ( TInt i( 0 ); i < oldCount; ++i )
       
   945         {
       
   946         User::LeaveIfError( iRepository->Delete( foundPairs[i] ) );
       
   947         }
       
   948 
       
   949     //Create new pairs
       
   950     const RPointerArray< CIMPSSAPKeyValuePair >& pairs( aSAPSettings->KeyValuePairs().Pairs() );
       
   951 
       
   952     TInt count( pairs.Count() );
       
   953 
       
   954     for ( TInt i( 0 );i < count; ++i )
       
   955         {
       
   956         //this creates a new hbufc and transfers ownership here
       
   957         HBufC* valuePairFlat = pairs[i]->KeyValuePairFlatLC();
       
   958         TPtr valuePairFlatPtr( valuePairFlat->Des() );
       
   959 
       
   960         User::LeaveIfError( iRepository->Create( EKeyValuePairBase + i + aUid,
       
   961                                                  valuePairFlatPtr ) );
       
   962 
       
   963         // CRepository->Set() is not copying the pointed data during transaction
       
   964         // so these must be stored until the transaction has completed
       
   965         tempArray.AppendL( valuePairFlat );
       
   966         CleanupStack::Pop( valuePairFlat );
       
   967         }
       
   968 
       
   969     TUint32 err( KErrNone );
       
   970     User::LeaveIfError( iRepository->CommitTransaction( err ) );
       
   971     iInTransaction = EFalse;
       
   972 	CleanupStack::PopAndDestroy( 6 ); // cleanupitem, Transaction, tmpPasswd, tmpHTTPPasswd, tempArray, foundPairs
       
   973 
       
   974     aSAPSettings->Body().DoSetUid( aUid );
       
   975     aSAPSettings->Body().DoSetAccessGroup( type );
       
   976     }
       
   977 
       
   978 // -----------------------------------------------------------------------------
       
   979 // CIMPSSAPCenRep::PopulateSAPSettingsListL()
       
   980 // Populates SAP settings list
       
   981 // (other items were commented in a header).
       
   982 // -----------------------------------------------------------------------------
       
   983 //
       
   984 
       
   985 void CIMPSSAPCenRep::PopulateSAPSettingsListL( CIMPSSAPSettingsList& aList,
       
   986                                                TIMPSAccessGroup aGroup, TBool aAscending )
       
   987     {
       
   988     SSS_DP_TXT( "CIMPSSAPCenRep::PopulateSAPSettingsListL()" );
       
   989 
       
   990     __ASSERT_ALWAYS(    aGroup & EIMPSAccessFilterAll,
       
   991                         Panic( EIMPSInvalidAccessGroup ) );
       
   992 
       
   993 	TBool transaction( StartOwnTransactionL( CRepository::EReadTransaction ) );
       
   994     if ( transaction )
       
   995         {
       
   996         iRepository->CleanupCancelTransactionPushL();
       
   997         }
       
   998 
       
   999     CIMPSSAPSettingsListItem* listItem = NULL;
       
  1000     TIMPSSAPSettingsListItemNameKey key( aAscending );
       
  1001 
       
  1002     RArray<TUint32> foundSAPs;
       
  1003     CleanupClosePushL( foundSAPs );
       
  1004 
       
  1005     FindSAPsFromAccessGroupL( aGroup, foundSAPs );
       
  1006 
       
  1007     TInt count( foundSAPs.Count() );
       
  1008 
       
  1009     for ( TInt i( 0 ); i < count; ++i )
       
  1010         {
       
  1011         HBufC16* name = HBufC::NewLC( NCentralRepositoryConstants::KMaxUnicodeStringLength );
       
  1012         TPtr namePtr( name->Des() );
       
  1013 
       
  1014         TIMPSAccessGroup type( SAPTypeL( foundSAPs[i] ) );
       
  1015 
       
  1016         TInt err( iRepository->Get( foundSAPs[i] + ESAPName, namePtr ) );
       
  1017         if ( err && ( err != KErrArgument ) )
       
  1018             {
       
  1019             User::Leave( err );
       
  1020             }
       
  1021 
       
  1022         TInt protectionInt( ESAPNoProtection );
       
  1023         err = iRepository->Get( foundSAPs[i] + EProtection, protectionInt );
       
  1024 
       
  1025         if ( err && ( err != KErrArgument ) )
       
  1026             {
       
  1027             User::Leave( err );
       
  1028             }
       
  1029 
       
  1030         listItem = CIMPSSAPSettingsListItem::NewLC( *name, foundSAPs[ i ], type,
       
  1031                                                     static_cast<TIMPSSAPProtection>( protectionInt ) );
       
  1032         aList.InsertIsqAllowDuplicatesL( listItem, key ); //takes ownership of listitem
       
  1033         CleanupStack::Pop( listItem );
       
  1034         CleanupStack::PopAndDestroy( name );
       
  1035         }
       
  1036     CleanupStack::PopAndDestroy(); // foundSAPs
       
  1037     if ( transaction )
       
  1038         {
       
  1039         TUint32 err( KErrNone );
       
  1040         User::LeaveIfError( iRepository->CommitTransaction( err ) );
       
  1041 		iInTransaction = EFalse;
       
  1042 		CleanupStack::PopAndDestroy(2); // cleanupitem, transaction
       
  1043         }
       
  1044     }
       
  1045 
       
  1046 // -----------------------------------------------------------------------------
       
  1047 // CIMPSSAPCenRep::GetDefaultL()
       
  1048 // Gets Uid of default SAP
       
  1049 // (other items were commented in a header).
       
  1050 // -----------------------------------------------------------------------------
       
  1051 //
       
  1052 
       
  1053 void CIMPSSAPCenRep::GetDefaultL( TUint32& aUid, TIMPSAccessGroup aGroup )
       
  1054     {
       
  1055     SSS_DP_TXT( "CIMPSSAPCenRep::GetDefaultL()" );
       
  1056 
       
  1057     __ASSERT_ALWAYS( ( ( aGroup & EIMPSAccessFilterAll ) == EIMPSIMAccessGroup ) ||
       
  1058                      ( ( aGroup & EIMPSAccessFilterAll ) == EIMPSPECAccessGroup ),
       
  1059                      Panic( EIMPSInvalidAccessGroup ) );
       
  1060 	TBool transaction( StartOwnTransactionL( CRepository::EReadTransaction ) );
       
  1061     if ( transaction )
       
  1062         {
       
  1063         iRepository->CleanupCancelTransactionPushL();
       
  1064         }
       
  1065 
       
  1066     TInt uid( 0 );
       
  1067     if ( aGroup & EIMPSIMAccessGroup )
       
  1068         {
       
  1069         User::LeaveIfError( iRepository->Get( KDefaultIMSAPId, uid ) );
       
  1070         }
       
  1071     else
       
  1072         {
       
  1073         User::LeaveIfError( iRepository->Get( KDefaultPECSAPId, uid ) );
       
  1074         }
       
  1075     if ( transaction )
       
  1076         {
       
  1077         TUint32 err( KErrNone );
       
  1078         User::LeaveIfError( iRepository->CommitTransaction( err ) );
       
  1079 		iInTransaction = EFalse;
       
  1080 		CleanupStack::PopAndDestroy(2); // cleanupitem, transaction
       
  1081         }
       
  1082     aUid = uid;
       
  1083     }
       
  1084 
       
  1085 // -----------------------------------------------------------------------------
       
  1086 // CIMPSSAPCenRep::GetDefaultL()
       
  1087 // Gets default SAP to settings container
       
  1088 // (other items were commented in a header).
       
  1089 // -----------------------------------------------------------------------------
       
  1090 //
       
  1091 
       
  1092 void CIMPSSAPCenRep::GetDefaultL( CIMPSSAPSettings* aSAPSettings,
       
  1093                                   TIMPSAccessGroup aGroup )
       
  1094     {
       
  1095     SSS_DP_TXT( "CIMPSSAPCenRep::GetDefaultL() #2" );
       
  1096 
       
  1097     __ASSERT_ALWAYS( ( ( aGroup & EIMPSAccessFilterAll ) == EIMPSIMAccessGroup ) ||
       
  1098                      ( ( aGroup & EIMPSAccessFilterAll ) == EIMPSPECAccessGroup ),
       
  1099                      Panic( EIMPSInvalidAccessGroup ) );
       
  1100 
       
  1101     TUint32 uid;
       
  1102     GetDefaultL( uid, aGroup );
       
  1103     GetSAPL( uid, aSAPSettings );
       
  1104     }
       
  1105 
       
  1106 // -----------------------------------------------------------------------------
       
  1107 // CIMPSSAPCenRep::SetToDefaultL()
       
  1108 // Sets given SAP to default SAP
       
  1109 // (other items were commented in a header).
       
  1110 // -----------------------------------------------------------------------------
       
  1111 //
       
  1112 
       
  1113 void CIMPSSAPCenRep::SetToDefaultL( TUint32 aUid, TIMPSAccessGroup aGroup )
       
  1114     {
       
  1115     SSS_DP_TXT( "CIMPSSAPCenRep::SetToDefaultL()" );
       
  1116 
       
  1117     __ASSERT_ALWAYS( ( ( aGroup & EIMPSAccessFilterAll ) == EIMPSIMAccessGroup ) ||
       
  1118                      ( ( aGroup & EIMPSAccessFilterAll ) == EIMPSPECAccessGroup ),
       
  1119                      Panic( EIMPSInvalidAccessGroup ) );
       
  1120 
       
  1121 	TBool transaction( StartOwnTransactionL( CRepository::EReadWriteTransaction ) );
       
  1122     if ( transaction )
       
  1123         {
       
  1124         iRepository->CleanupCancelTransactionPushL();
       
  1125         }
       
  1126 
       
  1127     // check that aUid exists
       
  1128     SAPExistsL( aUid );
       
  1129 
       
  1130     //and it belongs to the given group
       
  1131     if ( SAPTypeL( aUid ) & aGroup )
       
  1132         {
       
  1133         if ( aGroup & EIMPSIMAccessGroup )
       
  1134             {
       
  1135             User::LeaveIfError( iRepository->Set( KDefaultIMSAPId,
       
  1136                                                   static_cast<TInt>( aUid ) ) );
       
  1137             }
       
  1138         else
       
  1139             {
       
  1140             User::LeaveIfError( iRepository->Set( KDefaultPECSAPId,
       
  1141                                                   static_cast<TInt>( aUid ) ) );
       
  1142             }
       
  1143         }
       
  1144     else
       
  1145         {
       
  1146         User::Leave( KErrNotFound );
       
  1147         }
       
  1148 
       
  1149     if ( transaction )
       
  1150         {
       
  1151         TUint32 err( KErrNone );
       
  1152         User::LeaveIfError( iRepository->CommitTransaction( err ) );
       
  1153 		iInTransaction = EFalse;
       
  1154 		CleanupStack::PopAndDestroy(2); // cleanupitem, transaction
       
  1155         }
       
  1156     }
       
  1157 
       
  1158 //401-1808 - Ease of Instant Messaging SERVER customization
       
  1159 
       
  1160 // ---------------------------------------------------------
       
  1161 // CIMPSSAPCenRep::DoGetAccessPointsL
       
  1162 // ---------------------------------------------------------
       
  1163 //
       
  1164 TUint32 CIMPSSAPCenRep::DoGetAccessPointsL( const TDesC& aAPName )
       
  1165     {
       
  1166     TUint32 ap = 0;
       
  1167     TInt err( KErrNone );
       
  1168 
       
  1169     // Iterate the IAP table from CommsDat
       
  1170     CCommsDbTableView* table = NULL;
       
  1171 
       
  1172     table = iCommsDatabase->OpenViewMatchingTextLC( TPtrC( IAP ), TPtrC( COMMDB_NAME ), aAPName );
       
  1173 
       
  1174     err = table->GotoFirstRecord();
       
  1175 
       
  1176     if ( !err )
       
  1177         {
       
  1178         // Read IAP's ID
       
  1179         //
       
  1180         table->ReadUintL( TPtrC( COMMDB_ID ), ap );
       
  1181         }
       
  1182 
       
  1183     CleanupStack::PopAndDestroy( table );
       
  1184 
       
  1185     return ap;
       
  1186     }
       
  1187 
       
  1188 
       
  1189 // ---------------------------------------------------------
       
  1190 // CIMPSSAPCenRep::DoGetAccessPointsNameL
       
  1191 // ---------------------------------------------------------
       
  1192 //
       
  1193 HBufC* CIMPSSAPCenRep::DoGetAccessPointsNameL( const TUint32 aAP )
       
  1194     {
       
  1195     TInt err( KErrNone );
       
  1196     HBufC* buf1 = NULL;
       
  1197 
       
  1198     // Iterate the IAP table from CommsDat
       
  1199     CCommsDbTableView* table = NULL;
       
  1200     table = iCommsDatabase->OpenViewMatchingUintLC( TPtrC( IAP ), TPtrC( COMMDB_ID ), aAP );
       
  1201     err = table->GotoFirstRecord();
       
  1202 
       
  1203     if ( !err )
       
  1204         {
       
  1205         TInt    length1( 0 );
       
  1206 
       
  1207         // Read IAP's connection name
       
  1208         //
       
  1209         table->ReadColumnLengthL( TPtrC( COMMDB_NAME ), length1 );
       
  1210         buf1 = HBufC::NewLC( length1 );
       
  1211         TPtr ptr1( buf1->Des() );
       
  1212         table->ReadTextL( TPtrC( COMMDB_NAME ), ptr1 );
       
  1213         }
       
  1214     else
       
  1215         {
       
  1216         buf1 = KNullDesC().AllocLC();
       
  1217         }
       
  1218 
       
  1219     CleanupStack::Pop( buf1 );
       
  1220     CleanupStack::PopAndDestroy( table );
       
  1221 
       
  1222     return buf1;
       
  1223     }
       
  1224 
       
  1225 // ---------------------------------------------------------
       
  1226 // CIMPSSAPCenRep::ReSetTransactionState
       
  1227 // ---------------------------------------------------------
       
  1228 //
       
  1229 void CIMPSSAPCenRep::ReSetTransactionState(TAny* ptr)
       
  1230 	{
       
  1231 	reinterpret_cast<CIMPSSAPCenRep*>( ptr )->ReSetTransactionFlag();
       
  1232 	}
       
  1233 
       
  1234 // ---------------------------------------------------------
       
  1235 // CIMPSSAPCenRep::ReSetTransactionFlag
       
  1236 // ---------------------------------------------------------
       
  1237 //
       
  1238 void CIMPSSAPCenRep::ReSetTransactionFlag()
       
  1239 	{
       
  1240 	iInTransaction = EFalse;
       
  1241 	}
       
  1242 //End 401-1808 - Ease of Instant Messaging SERVER customization
       
  1243 
       
  1244 //  End of File