cmmanager/cmmgr/cmmserver/src/cmmdestinationstruct.cpp
branchRCL_3
changeset 58 83ca720e2b9a
parent 57 05bc53fe583b
child 62 bb1f80fb7db2
equal deleted inserted replaced
57:05bc53fe583b 58:83ca720e2b9a
     1 /*
       
     2 * Copyright (c) 2009-2010 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:
       
    15 * Cache side object representing a destination.
       
    16 *
       
    17 */
       
    18 
       
    19 
       
    20 #include <datamobilitycommsdattypes.h>
       
    21 #include <in_sock.h> // KAfInet
       
    22 
       
    23 #include "cmmdestinationstruct.h"
       
    24 #include "cmmtransactionhandler.h"
       
    25 #include "cmmdestinationinstance.h"
       
    26 #include "cmmcache.h"
       
    27 
       
    28 #include "OstTraceDefinitions.h"
       
    29 #ifdef OST_TRACE_COMPILER_IN_USE
       
    30 #include "cmmdestinationstructTraces.h"
       
    31 #endif
       
    32 
       
    33 const TUint KDefaultTierManagerTagId = KAfInet;
       
    34 
       
    35 
       
    36 // ---------------------------------------------------------------------------
       
    37 // Two phased construction.
       
    38 // Opens a destination from database based on ID.
       
    39 // ---------------------------------------------------------------------------
       
    40 //
       
    41 CCmmDestinationStruct* CCmmDestinationStruct::NewL(
       
    42         CCmmCache* aCache,
       
    43         CCmmTransactionHandler* aTransactionHandler,
       
    44         const TUint32 aDestinationId )
       
    45     {
       
    46     OstTraceFunctionEntry0( CCMMDESTINATIONSTRUCT_NEWL_ENTRY );
       
    47 
       
    48     CCmmDestinationStruct* self = CCmmDestinationStruct::NewLC(
       
    49             aCache,
       
    50             aTransactionHandler,
       
    51             aDestinationId );
       
    52     CleanupStack::Pop( self );
       
    53 
       
    54     OstTraceFunctionExit0( CCMMDESTINATIONSTRUCT_NEWL_EXIT );
       
    55     return self;
       
    56     }
       
    57 
       
    58 // ---------------------------------------------------------------------------
       
    59 // Two phased construction.
       
    60 // Creates a new destination with name and ID (optional).
       
    61 // If ID is 0, a free ID will be used when destination is saved to database.
       
    62 // ---------------------------------------------------------------------------
       
    63 //
       
    64 CCmmDestinationStruct* CCmmDestinationStruct::NewL(
       
    65         CCmmCache* aCache,
       
    66         CCmmTransactionHandler* aTransactionHandler,
       
    67         const TDesC& aName,
       
    68         const TUint32 aDestinationId )
       
    69     {
       
    70     OstTraceFunctionEntry0( DUP1_CCMMDESTINATIONSTRUCT_NEWL_ENTRY );
       
    71 
       
    72     CCmmDestinationStruct* self = CCmmDestinationStruct::NewLC(
       
    73             aCache,
       
    74             aTransactionHandler,
       
    75             aName,
       
    76             aDestinationId );
       
    77     CleanupStack::Pop( self );
       
    78 
       
    79     OstTraceFunctionExit0( DUP1_CCMMDESTINATIONSTRUCT_NEWL_EXIT );
       
    80     return self;
       
    81     }
       
    82 
       
    83 // ---------------------------------------------------------------------------
       
    84 // Two phased construction.
       
    85 // Opens a destination from database based on ID.
       
    86 // ---------------------------------------------------------------------------
       
    87 //
       
    88 CCmmDestinationStruct* CCmmDestinationStruct::NewLC(
       
    89         CCmmCache* aCache,
       
    90         CCmmTransactionHandler* aTransactionHandler,
       
    91         const TUint32 aDestinationId )
       
    92     {
       
    93     OstTraceFunctionEntry0( CCMMDESTINATIONSTRUCT_NEWLC_ENTRY );
       
    94 
       
    95     CCmmDestinationStruct* self = new( ELeave ) CCmmDestinationStruct(
       
    96             aCache,
       
    97             aTransactionHandler );
       
    98     CleanupStack::PushL( self );
       
    99     self->ConstructL( aDestinationId );
       
   100 
       
   101     OstTraceFunctionExit0( CCMMDESTINATIONSTRUCT_NEWLC_EXIT );
       
   102     return self;
       
   103     }
       
   104 
       
   105 // ---------------------------------------------------------------------------
       
   106 // Two phased construction.
       
   107 // Creates a new destination with name and ID (optional).
       
   108 // If ID is 0, a free ID will be used when destination is saved to database.
       
   109 // ---------------------------------------------------------------------------
       
   110 //
       
   111 CCmmDestinationStruct* CCmmDestinationStruct::NewLC(
       
   112         CCmmCache* aCache,
       
   113         CCmmTransactionHandler* aTransactionHandler,
       
   114         const TDesC& aName,
       
   115         const TUint32 aDestinationId )
       
   116     {
       
   117     OstTraceFunctionEntry0( DUP1_CCMMDESTINATIONSTRUCT_NEWLC_ENTRY );
       
   118 
       
   119     CCmmDestinationStruct* self = new( ELeave ) CCmmDestinationStruct(
       
   120             aCache,
       
   121             aTransactionHandler );
       
   122     CleanupStack::PushL( self );
       
   123     self->ConstructL( aName, aDestinationId );
       
   124 
       
   125     OstTraceFunctionExit0( DUP1_CCMMDESTINATIONSTRUCT_NEWLC_EXIT );
       
   126     return self;
       
   127     }
       
   128 
       
   129 // ---------------------------------------------------------------------------
       
   130 // Destructor.
       
   131 // Cancels any active requests.
       
   132 // ---------------------------------------------------------------------------
       
   133 //
       
   134 CCmmDestinationStruct::~CCmmDestinationStruct()
       
   135     {
       
   136     OstTraceFunctionEntry0( CCMMDESTINATIONSTRUCT_CCMMDESTINATIONSTRUCT_ENTRY );
       
   137 
       
   138     if ( iReferenceCounter != 0 )
       
   139         {
       
   140         ASSERT(0); // Error.
       
   141         }
       
   142     delete iNetworkRecord;
       
   143     delete iDestApRecord;
       
   144     delete iMetadataRecord;
       
   145     delete iName;
       
   146 
       
   147     OstTraceFunctionExit0( CCMMDESTINATIONSTRUCT_CCMMDESTINATIONSTRUCT_EXIT );
       
   148     }
       
   149 
       
   150 // ---------------------------------------------------------------------------
       
   151 // Constructor.
       
   152 // ---------------------------------------------------------------------------
       
   153 //
       
   154 CCmmDestinationStruct::CCmmDestinationStruct(
       
   155         CCmmCache* aCache,
       
   156         CCmmTransactionHandler* aTransactionHandler )
       
   157         :
       
   158         iCache( aCache ),
       
   159         iTransactionHandler( aTransactionHandler )
       
   160     {
       
   161     OstTraceFunctionEntry0( DUP1_CCMMDESTINATIONSTRUCT_CCMMDESTINATIONSTRUCT_ENTRY );
       
   162 
       
   163     iDestApRecord = NULL;
       
   164     iNetworkRecord = NULL;
       
   165     iMetadataRecord = NULL;
       
   166     iDestApRecordStatus = ECmmRecordStatusBlank;
       
   167     iNetworkRecordStatus = ECmmRecordStatusBlank;
       
   168     iMetadataRecordStatus = ECmmRecordStatusBlank;
       
   169     iReferenceCounter = 0;
       
   170     iStatus = ECmmDestinationStatusNotSaved;
       
   171     iId = 0;
       
   172     iName = NULL;
       
   173 
       
   174     OstTraceFunctionExit0( DUP1_CCMMDESTINATIONSTRUCT_CCMMDESTINATIONSTRUCT_EXIT );
       
   175     }
       
   176 
       
   177 // ---------------------------------------------------------------------------
       
   178 // Second phase constructor.
       
   179 // Opens an existing destination based on ID.
       
   180 // ---------------------------------------------------------------------------
       
   181 //
       
   182 void CCmmDestinationStruct::ConstructL( const TUint32 aDestinationId )
       
   183     {
       
   184     OstTraceFunctionEntry0( CCMMDESTINATIONSTRUCT_CONSTRUCTL_ENTRY );
       
   185 
       
   186     // Check ID is in valid range.
       
   187     if ( aDestinationId <= KCmDefaultDestinationAPTagId ||
       
   188             aDestinationId >= KCmMaxDestinationAPTagId )
       
   189         {
       
   190         User::Leave( KErrArgument );
       
   191         }
       
   192     iId = aDestinationId;
       
   193 
       
   194     iDestApRecord = static_cast<CommsDat::CCDAccessPointRecord*>(
       
   195             CommsDat::CCDRecordBase::RecordFactoryL( CommsDat::KCDTIdAccessPointRecord ) );
       
   196     iNetworkRecord = static_cast<CommsDat::CCDNetworkRecord*>(
       
   197             CommsDat::CCDRecordBase::RecordFactoryL( CommsDat::KCDTIdNetworkRecord ) );
       
   198     iMetadataRecord = new( ELeave ) CCDSNAPMetadataRecord(
       
   199             iCache->TableId( ECmmDestMetadataRecord ) );
       
   200 
       
   201     // Load access point record.
       
   202     iDestApRecord->iRecordTag = iId;
       
   203     if ( iDestApRecord->FindL( iTransactionHandler->Session() ) )
       
   204         {
       
   205         iDestApRecord->LoadL( iTransactionHandler->Session() );
       
   206         }
       
   207     else
       
   208         {
       
   209         User::Leave( KErrNotFound );
       
   210         }
       
   211     iDestApRecordStatus = ECmmRecordStatusLoaded;
       
   212 
       
   213     // Load network record.
       
   214     iNetworkRecord->SetRecordId( iDestApRecord->iCustomSelectionPolicy );
       
   215     iNetworkRecord->LoadL( iTransactionHandler->Session() );
       
   216     iNetworkRecordStatus = ECmmRecordStatusLoaded;
       
   217 
       
   218     // Load metadata record.
       
   219     iMetadataRecord->iSNAP = iId;
       
   220     if ( !iMetadataRecord->FindL( iTransactionHandler->Session() ) )
       
   221         {
       
   222         // Not found -> fill in with default values.
       
   223         iMetadataRecord->iSNAP = iId;
       
   224         iMetadataRecord->iMetadata = 0;
       
   225         iMetadataRecord->SetRecordId( KCDNewRecordRequest );
       
   226         iMetadataRecordStatus = ECmmRecordStatusUnsaved;
       
   227         }
       
   228     else
       
   229         {
       
   230         iMetadataRecord->LoadL( iTransactionHandler->Session() );
       
   231         iMetadataRecordStatus = ECmmRecordStatusLoaded;
       
   232         }
       
   233 
       
   234     SetStatus( ECmmDestinationStatusValid );
       
   235 
       
   236     OstTraceFunctionExit0( CCMMDESTINATIONSTRUCT_CONSTRUCTL_EXIT );
       
   237     }
       
   238 
       
   239 // ---------------------------------------------------------------------------
       
   240 // Second phase constructor.
       
   241 // Creates a new destination. With name and optionally also with ID.
       
   242 // ---------------------------------------------------------------------------
       
   243 //
       
   244 void CCmmDestinationStruct::ConstructL( const TDesC& aName, const TUint32 aDestinationId )
       
   245     {
       
   246     OstTraceFunctionEntry0( DUP1_CCMMDESTINATIONSTRUCT_CONSTRUCTL_ENTRY );
       
   247 
       
   248     SetStatus( ECmmDestinationStatusNotSaved );
       
   249     iId = aDestinationId;
       
   250     iName = aName.AllocL();
       
   251 
       
   252     iDestApRecord = static_cast<CommsDat::CCDAccessPointRecord*>(
       
   253             CommsDat::CCDRecordBase::RecordFactoryL( CommsDat::KCDTIdAccessPointRecord ) );
       
   254     iNetworkRecord = static_cast<CommsDat::CCDNetworkRecord*>(
       
   255             CommsDat::CCDRecordBase::RecordFactoryL( CommsDat::KCDTIdNetworkRecord ) );
       
   256     iMetadataRecord = new( ELeave ) CCDSNAPMetadataRecord(
       
   257             iCache->TableId( ECmmDestMetadataRecord ) );
       
   258 
       
   259     if ( aDestinationId >= KTemporaryIdCounterStart )
       
   260         {
       
   261         // Get a real ID from database during UpdateL().
       
   262         iNetworkRecord->SetRecordId( KCDNewRecordRequest );
       
   263         iDestApRecord->iRecordTag = 0;
       
   264         iDestApRecord->iCustomSelectionPolicy = 0;
       
   265         // Access Point record tag ID and custom selection policy value need to
       
   266         // be set during UpdateL(), when network record's record ID is known.
       
   267         // Also metadata record SNAP ID needs to be set.
       
   268         iMetadataRecord->iSNAP = 0;
       
   269         }
       
   270     else
       
   271         {
       
   272         // Client provided an actual ID.
       
   273         iNetworkRecord->SetRecordId( aDestinationId - KCmDefaultDestinationAPTagId );
       
   274         iDestApRecord->iRecordTag = aDestinationId;
       
   275         iDestApRecord->iCustomSelectionPolicy = ( aDestinationId - KCmDefaultDestinationAPTagId );
       
   276         iMetadataRecord->iSNAP = aDestinationId;
       
   277         }
       
   278     iDestApRecord->SetRecordId( KCDNewRecordRequest );
       
   279 
       
   280     iMetadataRecord->iMetadata = 0;
       
   281     iMetadataRecord->SetRecordId( KCDNewRecordRequest );
       
   282 
       
   283     iNetworkRecord->iRecordName.SetL( aName );
       
   284     iDestApRecord->iRecordName.SetL( aName );
       
   285 
       
   286     InitializeDestApRecordL();
       
   287 
       
   288     iDestApRecordStatus = ECmmRecordStatusUnsaved;
       
   289     iNetworkRecordStatus = ECmmRecordStatusUnsaved;
       
   290     iMetadataRecordStatus = ECmmRecordStatusUnsaved;
       
   291 
       
   292     OstTraceFunctionExit0( DUP1_CCMMDESTINATIONSTRUCT_CONSTRUCTL_EXIT );
       
   293     }
       
   294 
       
   295 // ---------------------------------------------------------------------------
       
   296 // Set the destination ID.
       
   297 // ---------------------------------------------------------------------------
       
   298 //
       
   299 void CCmmDestinationStruct::SetId( const TUint32 aId )
       
   300     {
       
   301     iId = aId;
       
   302     }
       
   303 
       
   304 // ---------------------------------------------------------------------------
       
   305 // Get the destination ID.
       
   306 // ---------------------------------------------------------------------------
       
   307 //
       
   308 TUint32 CCmmDestinationStruct::GetId() const
       
   309     {
       
   310     return iId;
       
   311     }
       
   312 
       
   313 // ---------------------------------------------------------------------------
       
   314 // Get the destination ID from network record. To get a valid ID, this must be
       
   315 // called only during or after a call to UpdateL().
       
   316 // ---------------------------------------------------------------------------
       
   317 //
       
   318 TUint32 CCmmDestinationStruct::GetRealId() const
       
   319     {
       
   320     TUint32 id( iId );
       
   321     if ( iNetworkRecord )
       
   322         {
       
   323         id = ( TUint32 )iNetworkRecord->RecordId() + KCmDefaultDestinationAPTagId;
       
   324         }
       
   325     return id;
       
   326     }
       
   327 
       
   328 // ---------------------------------------------------------------------------
       
   329 // Get the destination name.
       
   330 // ---------------------------------------------------------------------------
       
   331 //
       
   332 TDesC& CCmmDestinationStruct::GetDestinationNameL()
       
   333     {
       
   334     OstTraceFunctionEntry0( CCMMDESTINATIONSTRUCT_GETDESTINATIONNAMEL_ENTRY );
       
   335 
       
   336     // No network record exists if destination has not been saved yet.
       
   337     if ( iStatus == ECmmDestinationStatusNotSaved && iName )
       
   338         {
       
   339         OstTraceFunctionExit0( CCMMDESTINATIONSTRUCT_GETDESTINATIONNAMEL_EXIT );
       
   340         return *iName;
       
   341         }
       
   342 
       
   343     if ( iNetworkRecord )
       
   344         {
       
   345         return iNetworkRecord->iRecordName.GetL();
       
   346         }
       
   347 
       
   348     User::Leave( KErrCorrupt );
       
   349 
       
   350     OstTraceFunctionExit0( DUP1_CCMMDESTINATIONSTRUCT_GETDESTINATIONNAMEL_EXIT );
       
   351     return *iName;
       
   352     }
       
   353 
       
   354 // ---------------------------------------------------------------------------
       
   355 // Gets the current status of this destination struct.
       
   356 // ---------------------------------------------------------------------------
       
   357 //
       
   358 TCmmDestinationStatus CCmmDestinationStruct::GetStatus() const
       
   359     {
       
   360     return iStatus;
       
   361     }
       
   362 
       
   363 // ---------------------------------------------------------------------------
       
   364 // Get the current reference count.
       
   365 // ---------------------------------------------------------------------------
       
   366 //
       
   367 TInt CCmmDestinationStruct::GetReferenceCount() const
       
   368     {
       
   369     return iReferenceCounter;
       
   370     }
       
   371 
       
   372 // ---------------------------------------------------------------------------
       
   373 // Set the current status of this destination.
       
   374 // ECmmDestinationStatusValid: Destination exists in database.
       
   375 // ECmmDestinationStatusNotSaved: Destination does NOT exist in database yet.
       
   376 // ---------------------------------------------------------------------------
       
   377 //
       
   378 void CCmmDestinationStruct::SetStatus( const TCmmDestinationStatus& aStatus )
       
   379     {
       
   380     OstTraceFunctionEntry0( CCMMDESTINATIONSTRUCT_SETSTATUS_ENTRY );
       
   381 
       
   382     switch ( aStatus )
       
   383         {
       
   384         case ECmmDestinationStatusValid:
       
   385             {
       
   386             if ( iStatus == ECmmDestinationStatusNotSaved )
       
   387                 {
       
   388                 delete iName;
       
   389                 iName = NULL;
       
   390                 }
       
   391             }
       
   392             // Fallthrough intended.
       
   393         case ECmmDestinationStatusNotSaved:
       
   394         case ECmmDestinationStatusToBeDeleted:
       
   395             {
       
   396             // Set the status.
       
   397             iStatus = aStatus;
       
   398             }
       
   399             break;
       
   400         default:
       
   401             ASSERT( 0 ); // Error, invalid status.
       
   402             break;
       
   403         }
       
   404 
       
   405     OstTraceFunctionExit0( CCMMDESTINATIONSTRUCT_SETSTATUS_EXIT );
       
   406     }
       
   407 
       
   408 // ---------------------------------------------------------------------------
       
   409 // Notify about a possible change in database on specified record table.
       
   410 // ---------------------------------------------------------------------------
       
   411 //
       
   412 void CCmmDestinationStruct::NotifyRecordChange( const TCmmDbRecords aRecordType )
       
   413     {
       
   414     OstTraceFunctionEntry0( CCMMDESTINATIONSTRUCT_NOTIFYRECORDCHANGE_ENTRY );
       
   415 
       
   416     // Only change status if it is currently ECmmRecordStatusLoaded.
       
   417     switch ( aRecordType )
       
   418         {
       
   419         case ECmmDestNetworkRecord:
       
   420             {
       
   421             if ( iNetworkRecordStatus == ECmmRecordStatusLoaded )
       
   422                 {
       
   423                 iNetworkRecordStatus = ECmmRecordStatusExpired;
       
   424                 }
       
   425             }
       
   426             break;
       
   427         case ECmmDestApRecord:
       
   428             {
       
   429             if ( iDestApRecordStatus == ECmmRecordStatusLoaded )
       
   430                 {
       
   431                 iDestApRecordStatus = ECmmRecordStatusExpired;
       
   432                 }
       
   433             }
       
   434             break;
       
   435         case ECmmDestMetadataRecord:
       
   436             {
       
   437             if ( iMetadataRecordStatus == ECmmRecordStatusLoaded )
       
   438                 {
       
   439                 iMetadataRecordStatus = ECmmRecordStatusExpired;
       
   440                 }
       
   441             }
       
   442             break;
       
   443         case ECmmDbSnapRecord:
       
   444         case ECmmDbBearerPriorityRecord:
       
   445         case ECmmDbDefConnRecord:
       
   446         // Fallthrough intended.
       
   447         default:
       
   448             // The record table doesn't affect destinations, just ignore it.
       
   449             break;
       
   450         }
       
   451 
       
   452     OstTraceFunctionExit0( CCMMDESTINATIONSTRUCT_NOTIFYRECORDCHANGE_EXIT );
       
   453     }
       
   454 
       
   455 // ---------------------------------------------------------------------------
       
   456 // Set the record status for all records.
       
   457 // ---------------------------------------------------------------------------
       
   458 //
       
   459 void CCmmDestinationStruct::SetStatusForAllRecords( const TCmmRecordStatus aStatus )
       
   460     {
       
   461     OstTraceFunctionEntry0( CCMMDESTINATIONSTRUCT_SETSTATUSFORALLRECORDS_ENTRY );
       
   462 
       
   463     iDestApRecordStatus = aStatus;
       
   464     iNetworkRecordStatus = aStatus;
       
   465     iMetadataRecordStatus = aStatus;
       
   466 
       
   467     OstTraceFunctionExit0( CCMMDESTINATIONSTRUCT_SETSTATUSFORALLRECORDS_EXIT );
       
   468     }
       
   469 
       
   470 // ---------------------------------------------------------------------------
       
   471 // Copies the data for this destination to a session instance and increments
       
   472 // the reference counter by one.
       
   473 // ---------------------------------------------------------------------------
       
   474 //
       
   475 void CCmmDestinationStruct::CreateDestinationInstanceL(
       
   476         CCmmDestinationInstance& aDestinationInstance )
       
   477     {
       
   478     OstTraceFunctionEntry0( CCMMDESTINATIONSTRUCT_CREATESESSIONINSTANCEL_ENTRY );
       
   479 
       
   480     // This should work according to status. If status is valid, this
       
   481     // destination exists in commsdat, and other sessions can also open a handle
       
   482     // to it. Make copies of the records to give to session instance.
       
   483     //
       
   484     // If the status is unsaved, then this is a new destination that hasn't
       
   485     // been saved to commsdat yet. Thus no other session can open a handle to it
       
   486     // until it is saved with a call to UpdateL().
       
   487     // For a non-valid destination, to save memory, transfer the ownership of
       
   488     // record pointers straight to the session and mark the cache pointers as
       
   489     // NULL. When update is called, copy the records into the cache also.
       
   490     //
       
   491 
       
   492     switch ( iStatus )
       
   493         {
       
   494         case ECmmDestinationStatusNotSaved:
       
   495             {
       
   496             // Transfer ownership of table records to session. Mark local pointers to NULL.
       
   497 
       
   498             aDestinationInstance.SetRecordPointer( ECmmDestApRecord, iDestApRecord ); // iDestApRecord ownership is transfered.
       
   499             aDestinationInstance.SetRecordPointer( ECmmDestNetworkRecord, iNetworkRecord ); // iNetworkRecord ownership is transfered.
       
   500             aDestinationInstance.SetRecordPointer( ECmmDestMetadataRecord, iMetadataRecord ); // iMetadataRecord ownership is transfered.
       
   501             iDestApRecord = NULL;
       
   502             iNetworkRecord = NULL;
       
   503             iMetadataRecord = NULL;
       
   504             iDestApRecordStatus = ECmmRecordStatusBlank;
       
   505             iNetworkRecordStatus = ECmmRecordStatusBlank;
       
   506             iMetadataRecordStatus = ECmmRecordStatusBlank;
       
   507 
       
   508             aDestinationInstance.SetId( iId );
       
   509             aDestinationInstance.SetStatus( ECmmDestinationStatusChanged );
       
   510             }
       
   511             break;
       
   512         case ECmmDestinationStatusValid:
       
   513             {
       
   514             // Reload table records if needed, make copies and transfer those to session.
       
   515             LoadRecordL( aDestinationInstance, ECmmDestApRecord );
       
   516             LoadRecordL( aDestinationInstance, ECmmDestNetworkRecord );
       
   517             LoadRecordL( aDestinationInstance, ECmmDestMetadataRecord );
       
   518 
       
   519             aDestinationInstance.SetId( iId );
       
   520             aDestinationInstance.SetStatus( ECmmDestinationStatusValid );
       
   521             }
       
   522             break;
       
   523         case ECmmDestinationStatusChanged:
       
   524         case ECmmDestinationStatusToBeDeleted:
       
   525         default:
       
   526             User::Leave( KErrCorrupt ); // Invalid status.
       
   527             break;
       
   528         }
       
   529 
       
   530     // A session handle has been made, increment reference counter.
       
   531     iReferenceCounter++;
       
   532 
       
   533     OstTraceFunctionExit0( CCMMDESTINATIONSTRUCT_CREATESESSIONINSTANCEL_EXIT );
       
   534     }
       
   535 
       
   536 // ---------------------------------------------------------------------------
       
   537 // Refresh the data contained in aDestinationInstance. This means reloading
       
   538 // the data from database if necessary. After this call the contents of
       
   539 // aDestinationInstance will reflect the current state in the database. 
       
   540 // ---------------------------------------------------------------------------
       
   541 //
       
   542 void CCmmDestinationStruct::RefreshDestinationInstanceL(
       
   543         CCmmDestinationInstance& aDestinationInstance )
       
   544     {
       
   545     OstTraceFunctionEntry0( CCMMDESTINATIONSTRUCT_REFRESHSESSIONINSTANCEL_ENTRY );
       
   546 
       
   547     switch ( iStatus )
       
   548         {
       
   549         case ECmmDestinationStatusValid:
       
   550         case ECmmDestinationStatusToBeDeleted:
       
   551             {
       
   552             // Reload table records if needed, make copies and transfer those to session.
       
   553             LoadRecordL( aDestinationInstance, ECmmDestApRecord );
       
   554             LoadRecordL( aDestinationInstance, ECmmDestNetworkRecord );
       
   555             LoadRecordL( aDestinationInstance, ECmmDestMetadataRecord );
       
   556             }
       
   557             break;
       
   558         case ECmmDestinationStatusNotSaved: // This was checked before.
       
   559         case ECmmDestinationStatusChanged:
       
   560         default:
       
   561             User::Leave( KErrCorrupt ); // Invalid status.
       
   562             break;
       
   563         }
       
   564 
       
   565     // Add list of currently contained connection methods.
       
   566     iCache->GetConnMethodsFromDestinationL(
       
   567             aDestinationInstance.GetId(),
       
   568             aDestinationInstance.iConnMethodItemArray );
       
   569 
       
   570     // Internal state need to be set to the same state as after a successfull update.
       
   571     aDestinationInstance.UpdateSuccessful();
       
   572 
       
   573     OstTraceFunctionExit0( CCMMDESTINATIONSTRUCT_REFRESHSESSIONINSTANCEL_EXIT );
       
   574     }
       
   575 
       
   576 // ---------------------------------------------------------------------------
       
   577 // Reloads a destination record if needed and copies the latest version to
       
   578 // the session instance given as parameter.
       
   579 // ---------------------------------------------------------------------------
       
   580 //
       
   581 void CCmmDestinationStruct::LoadRecordL(
       
   582         CCmmDestinationInstance& aDestinationInstance,
       
   583         TCmmDbRecords aRecordType )
       
   584     {
       
   585     OstTraceFunctionEntry0( CCMMDESTINATIONSTRUCT_LOADRECORDL_ENTRY );
       
   586 
       
   587     if ( iStatus != ECmmDestinationStatusValid && iStatus != ECmmDestinationStatusToBeDeleted )
       
   588         {
       
   589         // Destination must exist in database to be able to load any records.
       
   590         User::Leave( KErrCorrupt );
       
   591         }
       
   592 
       
   593     // Record status:            Action:
       
   594     //
       
   595     // ECmmRecordStatusBlank     Error
       
   596     // ECmmRecordStatusLoaded    Skip load, already loaded
       
   597     // ECmmRecordStatusExpired   Load record from database
       
   598     // ECmmRecordStatusModified  Error, session side only status
       
   599     // ECmmRecordStatusUnsaved   Skip load, not in database
       
   600 
       
   601     CommsDat::CCDRecordBase* aRecordPointer;
       
   602 
       
   603     switch ( aRecordType )
       
   604         {
       
   605         case ECmmDestNetworkRecord:
       
   606             {
       
   607             if ( iNetworkRecordStatus == ECmmRecordStatusExpired )
       
   608                 {
       
   609                 iNetworkRecord->LoadL( iTransactionHandler->Session() );
       
   610                 iNetworkRecordStatus = ECmmRecordStatusLoaded;
       
   611                 }
       
   612             else if ( iNetworkRecordStatus == ECmmRecordStatusBlank ||
       
   613                     iNetworkRecordStatus == ECmmRecordStatusModified )
       
   614                 {
       
   615                 User::Leave( KErrCorrupt );
       
   616                 }
       
   617             aRecordPointer = CopyRecordL(
       
   618                     ECmmDestNetworkRecord,
       
   619                     static_cast<CommsDat::CCDRecordBase*>( iNetworkRecord ) );
       
   620             aDestinationInstance.SetRecordPointer(
       
   621                     ECmmDestNetworkRecord,
       
   622                     aRecordPointer ); // Ownership is transfered.
       
   623             }
       
   624             break;
       
   625         case ECmmDestApRecord:
       
   626             {
       
   627             if ( iDestApRecordStatus == ECmmRecordStatusExpired )
       
   628                 {
       
   629                 iDestApRecord->LoadL( iTransactionHandler->Session() );
       
   630                 iDestApRecordStatus = ECmmRecordStatusLoaded;
       
   631                 }
       
   632             aRecordPointer = CopyRecordL(
       
   633                     ECmmDestApRecord,
       
   634                     static_cast<CommsDat::CCDRecordBase*>( iDestApRecord ) );
       
   635             aDestinationInstance.SetRecordPointer(
       
   636                     ECmmDestApRecord,
       
   637                     aRecordPointer ); // Ownership is transfered.
       
   638             }
       
   639             break;
       
   640         case ECmmDestMetadataRecord:
       
   641             {
       
   642             if ( iMetadataRecordStatus == ECmmRecordStatusExpired )
       
   643                 {
       
   644                 iMetadataRecord->LoadL( iTransactionHandler->Session() );
       
   645                 iMetadataRecordStatus = ECmmRecordStatusLoaded;
       
   646                 }
       
   647             aRecordPointer = CopyRecordL(
       
   648                     ECmmDestMetadataRecord,
       
   649                     static_cast<CommsDat::CCDRecordBase*>( iMetadataRecord ) );
       
   650             aDestinationInstance.SetRecordPointer(
       
   651                     ECmmDestMetadataRecord,
       
   652                     aRecordPointer ); // Ownership is transfered.
       
   653             }
       
   654             break;
       
   655         default:
       
   656             {
       
   657             User::Leave( KErrArgument );
       
   658             }
       
   659             break;
       
   660         }
       
   661     aRecordPointer = NULL;
       
   662 
       
   663     OstTraceFunctionExit0( CCMMDESTINATIONSTRUCT_LOADRECORDL_EXIT );
       
   664     }
       
   665 
       
   666 // ---------------------------------------------------------------------------
       
   667 // Save the contents of a session side destination handle into database.
       
   668 // ---------------------------------------------------------------------------
       
   669 //
       
   670 void CCmmDestinationStruct::UpdateL(
       
   671         CCmmDestinationInstance& aDestinationInstance,
       
   672         CCmmCache* aCache )
       
   673     {
       
   674     OstTraceFunctionEntry0( CCMMDESTINATIONSTRUCT_UPDATEL_ENTRY );
       
   675 
       
   676     TBool setProtection( EFalse );
       
   677     CMManager::TProtectionLevel protLevel( CMManager::EProtLevel0 );
       
   678 
       
   679     // Structure:
       
   680     // - Use ModifyL() or StoreL() according to own status (new destination or
       
   681     //   changes to existing one).
       
   682     //
       
   683     // - Update (store/modify) records to database using client copies.
       
   684     // - Free local record tables.
       
   685     // - Copy record tables from client instance into cache.
       
   686     // - Update DataMobilitySelectionPolicy-records.
       
   687     //
       
   688 
       
   689     TInt id( iId );
       
   690     switch ( aDestinationInstance.GetStatus() )
       
   691         {
       
   692         case ECmmDestinationStatusValid:
       
   693             {
       
   694             if ( iStatus != ECmmDestinationStatusValid )
       
   695                 {
       
   696                 // Error, a 'valid'-status on session side implies a 'valid'-status on cache side.
       
   697                 User::Leave( KErrCorrupt );
       
   698                 }
       
   699             }
       
   700             // Fallthrough intended.
       
   701         case ECmmDestinationStatusChanged:
       
   702             {
       
   703             CommsDat::CCDRecordBase* recordPointer;
       
   704 
       
   705             // Get pointers to destination records in client instance.
       
   706             aDestinationInstance.GetRecordPointer( ECmmDestApRecord, recordPointer );
       
   707             CommsDat::CCDAccessPointRecord* instanceDestApRecord =
       
   708                     static_cast<CommsDat::CCDAccessPointRecord*>( recordPointer );
       
   709             aDestinationInstance.GetRecordPointer( ECmmDestNetworkRecord, recordPointer );
       
   710             CommsDat::CCDNetworkRecord* instanceNetworkRecord =
       
   711                     static_cast<CommsDat::CCDNetworkRecord*>( recordPointer );
       
   712             aDestinationInstance.GetRecordPointer( ECmmDestMetadataRecord, recordPointer );
       
   713             CCDSNAPMetadataRecord* instanceMetadataRecord =
       
   714                     static_cast<CCDSNAPMetadataRecord*>( recordPointer );
       
   715 
       
   716             recordPointer = NULL;
       
   717 
       
   718             // Set protections on network record as needed.
       
   719             protLevel = aDestinationInstance.CurrentProtectionLevelL();
       
   720             if ( protLevel == CMManager::EProtLevel1 ||
       
   721                     protLevel == CMManager::EProtLevel2 )
       
   722                 {
       
   723                 setProtection = ETrue;
       
   724                 }
       
   725             SetAttribute( instanceNetworkRecord, CommsDat::ECDProtectedWrite, setProtection );
       
   726 
       
   727             switch ( iStatus )
       
   728                 {
       
   729                 case ECmmDestinationStatusNotSaved:
       
   730                     {
       
   731                     // This is a new destination that doesn't exist in database yet.
       
   732 
       
   733                     // Store network record first, the record ID is needed for
       
   734                     // destination access point record.
       
   735                     // Record ID for network record is set during construction.
       
   736                     instanceNetworkRecord->StoreL( iTransactionHandler->Session() );
       
   737 
       
   738                     // If ID is out of valid range, it means this destination was created
       
   739                     // without a specific ID. It is a temporary ID and can now be replaced
       
   740                     // with the real database ID. This also enables this destination to be
       
   741                     // opened by other clients.
       
   742                     // Record ID from network record must be used as real ID.
       
   743                     if ( iId >= KTemporaryIdCounterStart )
       
   744                         {
       
   745                         id = instanceNetworkRecord->RecordId() + KCmDefaultDestinationAPTagId;
       
   746                         instanceDestApRecord->iRecordTag = id;
       
   747                         instanceDestApRecord->iCustomSelectionPolicy =
       
   748                                 ( id - KCmDefaultDestinationAPTagId );
       
   749                         instanceMetadataRecord->iSNAP = id;
       
   750                         }
       
   751                     instanceDestApRecord->StoreL( iTransactionHandler->Session() );
       
   752                     instanceMetadataRecord->StoreL( iTransactionHandler->Session() );
       
   753                     }
       
   754                     break;
       
   755 
       
   756                 case ECmmDestinationStatusValid:
       
   757                 case ECmmDestinationStatusToBeDeleted:
       
   758                     {
       
   759                     // This destination already exists in database and is beeing modified.
       
   760 
       
   761                     instanceNetworkRecord->ModifyL( iTransactionHandler->Session() );
       
   762                     instanceDestApRecord->ModifyL( iTransactionHandler->Session() );
       
   763                     if ( iMetadataRecordStatus == ECmmRecordStatusUnsaved )
       
   764                         {
       
   765                         instanceMetadataRecord->StoreL( iTransactionHandler->Session() );
       
   766                         }
       
   767                     else
       
   768                         {
       
   769                         instanceMetadataRecord->ModifyL( iTransactionHandler->Session() );
       
   770                         }
       
   771                     }
       
   772                     break;
       
   773                 case ECmmDestinationStatusChanged:
       
   774                 default:
       
   775                     User::Leave( KErrCorrupt ); // Error, invalid status.
       
   776                     break;
       
   777                 }
       
   778 
       
   779             // Copy destination records from client instance into cache.
       
   780             delete iDestApRecord;
       
   781             delete iNetworkRecord;
       
   782             delete iMetadataRecord;
       
   783             iDestApRecord = NULL;
       
   784             iNetworkRecord = NULL;
       
   785             iMetadataRecord = NULL;
       
   786             iDestApRecordStatus = ECmmRecordStatusBlank;
       
   787             iNetworkRecordStatus = ECmmRecordStatusBlank;
       
   788             iMetadataRecordStatus = ECmmRecordStatusBlank;
       
   789             iDestApRecord = static_cast<CommsDat::CCDAccessPointRecord*>(
       
   790                     CopyRecordL( ECmmDestApRecord, instanceDestApRecord ) );
       
   791             iNetworkRecord = static_cast<CommsDat::CCDNetworkRecord*>(
       
   792                     CopyRecordL( ECmmDestNetworkRecord, instanceNetworkRecord ) );
       
   793             iMetadataRecord = static_cast<CCDSNAPMetadataRecord*>(
       
   794                     CopyRecordL( ECmmDestMetadataRecord, instanceMetadataRecord ) );
       
   795 
       
   796             instanceDestApRecord = NULL;
       
   797             instanceNetworkRecord = NULL;
       
   798             instanceMetadataRecord = NULL;
       
   799 
       
   800 
       
   801 
       
   802             // Write DataMobilitySelectionPolicy-records into database.
       
   803             TInt neededRecordCount( aDestinationInstance.iConnMethodItemArray.Count() ); // +1 for destination itself.
       
   804             TInt existingRecordCount( 0 );
       
   805 
       
   806             // Create DataMobilitySelectionPolicy-record set.
       
   807             CommsDat::CMDBRecordSet<CCDDataMobilitySelectionPolicyRecord>* snapRecordSet =
       
   808                     new( ELeave ) CommsDat::CMDBRecordSet<CCDDataMobilitySelectionPolicyRecord>(
       
   809                             iCache->TableId( ECmmDbSnapRecord ) );
       
   810             CleanupStack::PushL( snapRecordSet );
       
   811 
       
   812             // Create a DataMobilitySelectionPolicy-record to prime the set.
       
   813             CCDDataMobilitySelectionPolicyRecord* snapRecordPrime =
       
   814                     new( ELeave ) CCDDataMobilitySelectionPolicyRecord(
       
   815                             iCache->TableId( ECmmDbSnapRecord ) );
       
   816             CleanupStack::PushL( snapRecordPrime );
       
   817 
       
   818             snapRecordPrime->iSNAP = id;
       
   819             snapRecordSet->iRecords.AppendL( snapRecordPrime );
       
   820             CleanupStack::Pop( snapRecordPrime ); // Ownership moved.
       
   821 
       
   822             // Find matching DataMobilitySelectionPolicy-records.
       
   823             if ( snapRecordSet->FindL( iTransactionHandler->Session() ) )
       
   824                 {
       
   825                 existingRecordCount = snapRecordSet->iRecords.Count();
       
   826                 }
       
   827 
       
   828             CCDDataMobilitySelectionPolicyRecord* snapRecord1 =
       
   829                     new( ELeave ) CCDDataMobilitySelectionPolicyRecord(
       
   830                             iCache->TableId( ECmmDbSnapRecord ) );
       
   831             CleanupStack::PushL( snapRecord1 );
       
   832 
       
   833             // Current index in snapRecordSet.
       
   834             TInt index( 0 );
       
   835 
       
   836             // Initial record for destination itself.
       
   837             snapRecord1->iSNAP = id;
       
   838             snapRecord1->iRecordName.SetL( iNetworkRecord->iRecordName.GetL() );
       
   839 
       
   840             // Set protections on snap record.
       
   841             SetAttribute( snapRecord1, CommsDat::ECDProtectedWrite, setProtection );
       
   842 
       
   843             if ( existingRecordCount > index )
       
   844                 {
       
   845                 snapRecord1->SetElementId( snapRecordSet->iRecords[index]->ElementId() );
       
   846                 snapRecord1->iEmbeddedSNAP = 0;
       
   847                 snapRecord1->iIAP = 0;
       
   848                 snapRecord1->iPriority = 0;
       
   849                 snapRecord1->ModifyL( iTransactionHandler->Session() );
       
   850                 index++;
       
   851                 }
       
   852             else
       
   853                 {
       
   854                 snapRecord1->SetRecordId( KCDNewRecordRequest );
       
   855                 snapRecord1->StoreL( iTransactionHandler->Session() );
       
   856                 }
       
   857             CleanupStack::PopAndDestroy( snapRecord1 );
       
   858 
       
   859             // Records for destination contents.
       
   860             for ( TInt i = 0; i < neededRecordCount; i++ )
       
   861                 {
       
   862                 // Weird stuff starts to happen in commsdat side if we try to
       
   863                 // reuse a snap-record, so create new record for each entry.
       
   864                 CCDDataMobilitySelectionPolicyRecord* snapRecord2 =
       
   865                         new( ELeave ) CCDDataMobilitySelectionPolicyRecord(
       
   866                                 iCache->TableId( ECmmDbSnapRecord ) );
       
   867                 CleanupStack::PushL( snapRecord2 );
       
   868 
       
   869                 snapRecord2->iSNAP = id;
       
   870                 snapRecord2->iRecordName.SetL( iNetworkRecord->iRecordName.GetL() );
       
   871 
       
   872                 // If the connection method was added to destination while
       
   873                 // still having a temporary ID, update the ID to real ID.
       
   874                 TUint32 connMethodRealId( aDestinationInstance.iConnMethodItemArray[i].iId );
       
   875                 if ( connMethodRealId >= KTemporaryIdCounterStart )
       
   876                     {
       
   877                     aCache->TranslateTemporaryId(
       
   878                             aDestinationInstance.iConnMethodItemArray[i].iId,
       
   879                             connMethodRealId );
       
   880                     }
       
   881 
       
   882                 // Set protections on destination contents as needed.
       
   883                 setProtection = EFalse;
       
   884                 protLevel = aDestinationInstance.CurrentProtectionLevelL();
       
   885                 if ( protLevel == CMManager::EProtLevel1 )
       
   886                     {
       
   887                     setProtection = ETrue;
       
   888                     }
       
   889                 SetAttribute( snapRecord2, CommsDat::ECDProtectedWrite, setProtection );
       
   890 
       
   891                 if ( aDestinationInstance.iConnMethodItemArray[i].IsEmbedded() )
       
   892                     {
       
   893                     // Embedded destination.
       
   894                     snapRecord2->iEmbeddedSNAP = ( TInt )connMethodRealId;
       
   895                     snapRecord2->iIAP = 0;
       
   896                     snapRecord2->iPriority =
       
   897                             CMManager::KDataMobilitySelectionPolicyPriorityWildCard;
       
   898                     aDestinationInstance.iConnMethodItemArray[i].iPriority =
       
   899                             CMManager::KDataMobilitySelectionPolicyPriorityWildCard;
       
   900                     }
       
   901                 else
       
   902                     {
       
   903                     // Normal connection method.
       
   904                     snapRecord2->iEmbeddedSNAP = 0;
       
   905                     snapRecord2->iIAP = CommsDat::KCDTIdIAPRecord | ( connMethodRealId << 8 );
       
   906                     // If the connection method is a virtual IAP that doesn't
       
   907                     // link to an IAP, set priority to wildcard.
       
   908                     if ( aDestinationInstance.iConnMethodItemArray[i].IsVirtual() &&
       
   909                             aDestinationInstance.iConnMethodItemArray[i].LinkedIapId() == 0 )
       
   910                         {
       
   911                         snapRecord2->iPriority =
       
   912                                 CMManager::KDataMobilitySelectionPolicyPriorityWildCard;
       
   913                         aDestinationInstance.iConnMethodItemArray[i].iPriority =
       
   914                                 CMManager::KDataMobilitySelectionPolicyPriorityWildCard;
       
   915                         }
       
   916                     else
       
   917                         {
       
   918                         snapRecord2->iPriority = i + 1; // Priority values start from 1.
       
   919                         aDestinationInstance.iConnMethodItemArray[i].iPriority = i + 1;
       
   920                         }
       
   921                     }
       
   922 
       
   923                 if ( existingRecordCount > index )
       
   924                     {
       
   925                     // An old record can be reused.
       
   926                     snapRecord2->SetElementId( snapRecordSet->iRecords[index]->ElementId() );
       
   927                     snapRecord2->ModifyL( iTransactionHandler->Session() );
       
   928                     index++;
       
   929                     }
       
   930                 else
       
   931                     {
       
   932                     // A new record must be created.
       
   933                     snapRecord2->SetRecordId( KCDNewRecordRequest );
       
   934                     snapRecord2->StoreL( iTransactionHandler->Session() );
       
   935                     }
       
   936                 CleanupStack::PopAndDestroy( snapRecord2 );
       
   937                 }
       
   938 
       
   939             // Delete any extra records.
       
   940             while ( existingRecordCount > index )
       
   941                 {
       
   942                 CCDDataMobilitySelectionPolicyRecord* snapRecord3 =
       
   943                         new( ELeave ) CCDDataMobilitySelectionPolicyRecord(
       
   944                                 iCache->TableId( ECmmDbSnapRecord ) );
       
   945                 CleanupStack::PushL( snapRecord3 );
       
   946                 snapRecord3->SetElementId( snapRecordSet->iRecords[index]->ElementId() );
       
   947                 snapRecord3->DeleteL( iTransactionHandler->Session() );
       
   948                 CleanupStack::PopAndDestroy( snapRecord3 );
       
   949                 index++;
       
   950                 }
       
   951 
       
   952 
       
   953             CleanupStack::PopAndDestroy( snapRecordSet );
       
   954             }
       
   955             break;
       
   956         case ECmmDestinationStatusNotSaved:
       
   957         case ECmmDestinationStatusToBeDeleted:
       
   958         default:
       
   959             User::Leave( KErrCorrupt ); // Error, invalid status.
       
   960             break;
       
   961         }
       
   962 
       
   963     // Do not update destination status or record status values yet. Cache
       
   964     // will set them after successfully commiting transaction to database.
       
   965 
       
   966     OstTraceFunctionExit0( CCMMDESTINATIONSTRUCT_UPDATEL_EXIT );
       
   967     }
       
   968 
       
   969 // ---------------------------------------------------------------------------
       
   970 // Delete all database records of this destination. Also removes any records
       
   971 // making this destination an embedded destination in another destination.
       
   972 // ---------------------------------------------------------------------------
       
   973 //
       
   974 void CCmmDestinationStruct::DeleteL()
       
   975     {
       
   976     OstTraceFunctionEntry0( CCMMDESTINATIONSTRUCT_DELETEL_ENTRY );
       
   977 
       
   978     // Check status for records.
       
   979     switch ( iNetworkRecordStatus )
       
   980         {
       
   981         case ECmmRecordStatusLoaded:
       
   982         case ECmmRecordStatusExpired:
       
   983             {
       
   984             iNetworkRecord->DeleteL( iTransactionHandler->Session() );
       
   985             }
       
   986             break;
       
   987         case ECmmRecordStatusUnsaved:
       
   988             // Skip.
       
   989             break;
       
   990         case ECmmRecordStatusBlank:
       
   991         case ECmmRecordStatusModified:
       
   992         default:
       
   993             ASSERT( 0 ); // Error, invalid status.
       
   994             User::Leave( KErrCorrupt );
       
   995             break;
       
   996         }
       
   997     switch ( iDestApRecordStatus )
       
   998         {
       
   999         case ECmmRecordStatusLoaded:
       
  1000         case ECmmRecordStatusExpired:
       
  1001             {
       
  1002             iDestApRecord->DeleteL( iTransactionHandler->Session() );
       
  1003             }
       
  1004             break;
       
  1005         case ECmmRecordStatusUnsaved:
       
  1006             // Skip.
       
  1007             break;
       
  1008         case ECmmRecordStatusBlank:
       
  1009         case ECmmRecordStatusModified:
       
  1010         default:
       
  1011             ASSERT( 0 ); // Error, invalid status.
       
  1012             User::Leave( KErrCorrupt );
       
  1013             break;
       
  1014         }
       
  1015     switch ( iMetadataRecordStatus )
       
  1016         {
       
  1017         case ECmmRecordStatusLoaded:
       
  1018         case ECmmRecordStatusExpired:
       
  1019             {
       
  1020             iMetadataRecord->DeleteL( iTransactionHandler->Session() );
       
  1021             }
       
  1022             break;
       
  1023         case ECmmRecordStatusUnsaved:
       
  1024             // Skip.
       
  1025             break;
       
  1026         case ECmmRecordStatusBlank:
       
  1027         case ECmmRecordStatusModified:
       
  1028         default:
       
  1029             ASSERT( 0 ); // Error, invalid status.
       
  1030             User::Leave( KErrCorrupt );
       
  1031             break;
       
  1032         }
       
  1033 
       
  1034     // Delete all datamobility selection policy records related to this destination.
       
  1035 
       
  1036     // Create DataMobilitySelectionPolicy-record set.
       
  1037     CommsDat::CMDBRecordSet<CCDDataMobilitySelectionPolicyRecord>* snapRecordSet =
       
  1038             new( ELeave ) CommsDat::CMDBRecordSet<CCDDataMobilitySelectionPolicyRecord>(
       
  1039                     iCache->TableId( ECmmDbSnapRecord ) );
       
  1040     CleanupStack::PushL( snapRecordSet );
       
  1041 
       
  1042     // Create a DataMobilitySelectionPolicy-record.
       
  1043     CCDDataMobilitySelectionPolicyRecord* snapRecord =
       
  1044             new( ELeave ) CCDDataMobilitySelectionPolicyRecord(
       
  1045                     iCache->TableId( ECmmDbSnapRecord ) );
       
  1046     CleanupStack::PushL( snapRecord );
       
  1047 
       
  1048     TRAP_IGNORE( snapRecordSet->LoadL( iTransactionHandler->Session() ) );
       
  1049 
       
  1050     // Loop through all datamobility selection policy records.
       
  1051     TInt snapRecordCount( snapRecordSet->iRecords.Count() );
       
  1052     for ( TInt i = 0; i < snapRecordCount; i++ )
       
  1053         {
       
  1054         snapRecord->SetElementId( snapRecordSet->iRecords[i]->ElementId() );
       
  1055         snapRecord->LoadL( iTransactionHandler->Session() );
       
  1056 
       
  1057         TInt snap = snapRecord->iSNAP;
       
  1058         TInt embeddedSnap = snapRecord->iEmbeddedSNAP;
       
  1059         if ( snap == iId || embeddedSnap == iId )
       
  1060             {
       
  1061             snapRecord->DeleteL( iTransactionHandler->Session() );
       
  1062             }
       
  1063         }
       
  1064     CleanupStack::PopAndDestroy( snapRecord );
       
  1065     CleanupStack::PopAndDestroy( snapRecordSet );
       
  1066 
       
  1067     OstTraceFunctionExit0( DUP1_CCMMDESTINATIONSTRUCT_DELETEL_EXIT );
       
  1068     }
       
  1069 
       
  1070 // ---------------------------------------------------------------------------
       
  1071 // This should be called when a client session closes a destination handle.
       
  1072 // Reference counter is decremented by one and the remaining number of
       
  1073 // references is returned.
       
  1074 // ---------------------------------------------------------------------------
       
  1075 //
       
  1076 TInt CCmmDestinationStruct::DestinationInstanceClosed()
       
  1077     {
       
  1078     OstTraceFunctionEntry0( CCMMDESTINATIONSTRUCT_SESSIONINSTANCECLOSED_ENTRY );
       
  1079 
       
  1080     iReferenceCounter--;
       
  1081     if ( iReferenceCounter < 0 )
       
  1082         {
       
  1083         // Error, reference counter shouldn't be able to go negative.
       
  1084         ASSERT( 0 );
       
  1085         iReferenceCounter = 0;
       
  1086         }
       
  1087 
       
  1088     OstTraceFunctionExit0( CCMMDESTINATIONSTRUCT_SESSIONINSTANCECLOSED_EXIT );
       
  1089     return iReferenceCounter;
       
  1090     }
       
  1091 
       
  1092 // -----------------------------------------------------------------------------
       
  1093 // Called after this destination has been updated and database transaction has
       
  1094 // completed successfully. Sets the internal state of this destination
       
  1095 // structure to reflect the new valid state.
       
  1096 // -----------------------------------------------------------------------------
       
  1097 //
       
  1098 void CCmmDestinationStruct::UpdateSuccessful()
       
  1099     {
       
  1100     OstTraceFunctionEntry0( CCMMDESTINATIONSTRUCT_UPDATESUCCESSFUL_ENTRY );
       
  1101 
       
  1102     SetStatus( ECmmDestinationStatusValid );
       
  1103     SetStatusForAllRecords( ECmmRecordStatusLoaded );
       
  1104 
       
  1105     OstTraceFunctionExit0( CCMMDESTINATIONSTRUCT_UPDATESUCCESSFUL_EXIT );
       
  1106     }
       
  1107 
       
  1108 // -----------------------------------------------------------------------------
       
  1109 // CCmmDestinationStruct::InitializeDestApRecordL
       
  1110 // Set default values to a new destination access point record.
       
  1111 // -----------------------------------------------------------------------------
       
  1112 //
       
  1113 void CCmmDestinationStruct::InitializeDestApRecordL()
       
  1114     {
       
  1115     OstTraceFunctionEntry0( CCMMDESTINATIONSTRUCT_INITIALIZEDESTAPRECORDL_ENTRY );
       
  1116 
       
  1117     SetDefaultTierManagerL();
       
  1118     SetDefaultMCprL();
       
  1119     SetDefaultCprL();
       
  1120     SetDefaultSCprL();
       
  1121     SetDefaultProtocolL();
       
  1122 
       
  1123     OstTraceFunctionExit0( CCMMDESTINATIONSTRUCT_INITIALIZEDESTAPRECORDL_EXIT );
       
  1124     }
       
  1125 
       
  1126 // -----------------------------------------------------------------------------
       
  1127 // CCmmDestinationStruct::SetDefaultTierManagerL
       
  1128 // Sets the default tier manager to destination access point record.
       
  1129 // -----------------------------------------------------------------------------
       
  1130 void CCmmDestinationStruct::SetDefaultTierManagerL()
       
  1131     {
       
  1132     OstTraceFunctionEntry0( CCMMDESTINATIONSTRUCT_SETDEFAULTTIERMANAGERL_ENTRY );
       
  1133 
       
  1134     CommsDat::CCDTierRecord* tierRecord =
       
  1135             static_cast<CommsDat::CCDTierRecord*>(
       
  1136                     CommsDat::CCDRecordBase::RecordFactoryL( CommsDat::KCDTIdTierRecord ) );
       
  1137     CleanupStack::PushL( tierRecord );
       
  1138 
       
  1139     tierRecord->iRecordTag = KDefaultTierManagerTagId;
       
  1140 
       
  1141     if ( tierRecord->FindL( iTransactionHandler->Session() ) )
       
  1142         {
       
  1143         iDestApRecord->iTier = tierRecord->ElementId();
       
  1144         }
       
  1145     else
       
  1146         {
       
  1147         User::Leave( KErrNotFound );
       
  1148         }
       
  1149     CleanupStack::PopAndDestroy( tierRecord );
       
  1150 
       
  1151     OstTraceFunctionExit0( CCMMDESTINATIONSTRUCT_SETDEFAULTTIERMANAGERL_EXIT );
       
  1152     }
       
  1153 
       
  1154 // -----------------------------------------------------------------------------
       
  1155 // CCmmDestinationStruct::SetDefaultMCprL
       
  1156 // Sets the default MCpr to destination access point record.
       
  1157 // -----------------------------------------------------------------------------
       
  1158 void CCmmDestinationStruct::SetDefaultMCprL()
       
  1159     {
       
  1160     OstTraceFunctionEntry0( CCMMDESTINATIONSTRUCT_SETDEFAULTMCPRL_ENTRY );
       
  1161 
       
  1162     CommsDat::CCDMCprRecord* mcprRecord =
       
  1163             static_cast<CommsDat::CCDMCprRecord*>(
       
  1164                     CommsDat::CCDRecordBase::RecordFactoryL( CommsDat::KCDTIdMCprRecord ) );
       
  1165     CleanupStack::PushL( mcprRecord );
       
  1166 
       
  1167     mcprRecord->SetRecordId( 1 );
       
  1168 
       
  1169     mcprRecord->LoadL( iTransactionHandler->Session() );
       
  1170     iDestApRecord->iMCpr = mcprRecord->ElementId();
       
  1171     CleanupStack::PopAndDestroy( mcprRecord );
       
  1172 
       
  1173     OstTraceFunctionExit0( CCMMDESTINATIONSTRUCT_SETDEFAULTMCPRL_EXIT );
       
  1174     }
       
  1175 
       
  1176 // -----------------------------------------------------------------------------
       
  1177 // CCmmDestinationStruct::SetDefaultCprL
       
  1178 // Sets the default Cpr to destination access point record.
       
  1179 // -----------------------------------------------------------------------------
       
  1180 void CCmmDestinationStruct::SetDefaultCprL()
       
  1181     {
       
  1182     OstTraceFunctionEntry0( CCMMDESTINATIONSTRUCT_SETDEFAULTCPRL_ENTRY );
       
  1183 
       
  1184     CommsDat::CCDCprRecord* cprRecord =
       
  1185             static_cast<CommsDat::CCDCprRecord*>(
       
  1186                     CommsDat::CCDRecordBase::RecordFactoryL( CommsDat::KCDTIdCprRecord ) );
       
  1187     CleanupStack::PushL( cprRecord );
       
  1188 
       
  1189     cprRecord->SetRecordId( 1 );
       
  1190 
       
  1191     cprRecord->LoadL( iTransactionHandler->Session() );
       
  1192     iDestApRecord->iCpr = cprRecord->ElementId();
       
  1193     CleanupStack::PopAndDestroy( cprRecord );
       
  1194 
       
  1195     OstTraceFunctionExit0( CCMMDESTINATIONSTRUCT_SETDEFAULTCPRL_EXIT );
       
  1196     }
       
  1197 
       
  1198 // -----------------------------------------------------------------------------
       
  1199 // CCmmDestinationStruct::SetDefaultSCprL
       
  1200 // Sets the default SCpr to destination access point record.
       
  1201 // -----------------------------------------------------------------------------
       
  1202 void CCmmDestinationStruct::SetDefaultSCprL()
       
  1203     {
       
  1204     OstTraceFunctionEntry0( CCMMDESTINATIONSTRUCT_SETDEFAULTSCPRL_ENTRY );
       
  1205 
       
  1206     CommsDat::CCDSCprRecord* scprRecord =
       
  1207             static_cast<CommsDat::CCDSCprRecord*>(
       
  1208                     CommsDat::CCDRecordBase::RecordFactoryL( CommsDat::KCDTIdSCprRecord ) );
       
  1209     CleanupStack::PushL( scprRecord );
       
  1210 
       
  1211     scprRecord->SetRecordId( 1 );
       
  1212 
       
  1213     scprRecord->LoadL( iTransactionHandler->Session() );
       
  1214     iDestApRecord->iSCpr = scprRecord->ElementId();
       
  1215     CleanupStack::PopAndDestroy( scprRecord );
       
  1216 
       
  1217     OstTraceFunctionExit0( CCMMDESTINATIONSTRUCT_SETDEFAULTSCPRL_EXIT );
       
  1218     }
       
  1219 
       
  1220 // -----------------------------------------------------------------------------
       
  1221 // CCmmDestinationStruct::SetDefaultProtocolL
       
  1222 // Sets the default protocol to destination access point record.
       
  1223 // -----------------------------------------------------------------------------
       
  1224 void CCmmDestinationStruct::SetDefaultProtocolL()
       
  1225     {
       
  1226     OstTraceFunctionEntry0( CCMMDESTINATIONSTRUCT_SETDEFAULTPROTOCOLL_ENTRY );
       
  1227 
       
  1228     CommsDat::CCDProtocolRecord* protocolRecord =
       
  1229             static_cast<CommsDat::CCDProtocolRecord*>(
       
  1230                     CommsDat::CCDRecordBase::RecordFactoryL( CommsDat::KCDTIdProtocolRecord ) );
       
  1231     CleanupStack::PushL( protocolRecord );
       
  1232 
       
  1233     protocolRecord->SetRecordId( 1 );
       
  1234 
       
  1235     protocolRecord->LoadL( iTransactionHandler->Session() );
       
  1236     iDestApRecord->iProtocol = protocolRecord->ElementId();
       
  1237     CleanupStack::PopAndDestroy( protocolRecord );
       
  1238 
       
  1239     OstTraceFunctionExit0( CCMMDESTINATIONSTRUCT_SETDEFAULTPROTOCOLL_EXIT );
       
  1240     }
       
  1241 
       
  1242 // -----------------------------------------------------------------------------
       
  1243 // Set attribute flag on the given record.
       
  1244 // -----------------------------------------------------------------------------
       
  1245 void CCmmDestinationStruct::SetAttribute(
       
  1246         CommsDat::CCDRecordBase* aRecord,
       
  1247         TUint32 aAttribute,
       
  1248         TBool aSet )
       
  1249     {
       
  1250     OstTraceFunctionEntry0( CCMMDESTINATIONSTRUCT_SETATTRIBUTE_ENTRY );
       
  1251 
       
  1252     if ( aSet )
       
  1253         {
       
  1254         aRecord->SetAttributes( aAttribute );
       
  1255         }
       
  1256     else
       
  1257         {
       
  1258         aRecord->ClearAttributes( aAttribute );
       
  1259         }
       
  1260 
       
  1261     OstTraceFunctionExit0( CCMMDESTINATIONSTRUCT_SETATTRIBUTE_EXIT );
       
  1262     }
       
  1263 
       
  1264 // -----------------------------------------------------------------------------
       
  1265 // Creates a copy of a record of the given type.
       
  1266 // -----------------------------------------------------------------------------
       
  1267 CommsDat::CCDRecordBase* CCmmDestinationStruct::CopyRecordL(
       
  1268         TCmmDbRecords aRecordType,
       
  1269         CommsDat::CCDRecordBase* aSource )
       
  1270     {
       
  1271     OstTraceFunctionEntry0( CCMMDESTINATIONSTRUCT_COPYRECORDL_ENTRY );
       
  1272 
       
  1273     CommsDat::CCDRecordBase* target = NULL;
       
  1274 
       
  1275     switch ( aRecordType )
       
  1276         {
       
  1277         case ECmmDestNetworkRecord:
       
  1278             {
       
  1279             // Network record.
       
  1280 
       
  1281             CommsDat::CCDNetworkRecord* source =
       
  1282                     static_cast<CommsDat::CCDNetworkRecord*>( aSource );
       
  1283             CommsDat::CCDNetworkRecord* networkRecord =
       
  1284                     static_cast<CommsDat::CCDNetworkRecord*>(
       
  1285                             CommsDat::CCDRecordBase::RecordFactoryL(
       
  1286                                     CommsDat::KCDTIdNetworkRecord ) );
       
  1287             CleanupStack::PushL( networkRecord );
       
  1288 
       
  1289             if ( !source->iRecordTag.IsNull() )
       
  1290                 {
       
  1291                 networkRecord->iRecordTag.SetL( source->iRecordTag );
       
  1292                 }
       
  1293             if ( !source->iRecordName.IsNull() )
       
  1294                 {
       
  1295                 networkRecord->iRecordName.SetL( source->iRecordName );
       
  1296                 }
       
  1297             if ( !source->iHostName.IsNull() )
       
  1298                 {
       
  1299                 networkRecord->iHostName.SetL( source->iHostName );
       
  1300                 }
       
  1301             networkRecord->SetElementId( source->ElementId() );
       
  1302 
       
  1303             CleanupStack::Pop( networkRecord );
       
  1304             target = static_cast<CommsDat::CCDRecordBase*>( networkRecord );
       
  1305             }
       
  1306             break;
       
  1307         case ECmmDestApRecord:
       
  1308             {
       
  1309             // Access point record.
       
  1310 
       
  1311             CommsDat::CCDAccessPointRecord* source =
       
  1312                     static_cast<CommsDat::CCDAccessPointRecord*>( aSource );
       
  1313             CommsDat::CCDAccessPointRecord* destApRecord =
       
  1314                     static_cast<CommsDat::CCDAccessPointRecord*>(
       
  1315                             CommsDat::CCDRecordBase::RecordFactoryL(
       
  1316                                     CommsDat::KCDTIdAccessPointRecord ) );
       
  1317             CleanupStack::PushL( destApRecord );
       
  1318 
       
  1319             if ( !source->iRecordTag.IsNull() )
       
  1320                 {
       
  1321                 destApRecord->iRecordTag.SetL( source->iRecordTag );
       
  1322                 }
       
  1323             if ( !source->iRecordName.IsNull() )
       
  1324                 {
       
  1325                 destApRecord->iRecordName.SetL( source->iRecordName );
       
  1326                 }
       
  1327             if ( !source->iAccessPointGID.IsNull() )
       
  1328                 {
       
  1329                 destApRecord->iAccessPointGID.SetL( source->iAccessPointGID );
       
  1330                 }
       
  1331             if ( !source->iSelectionPolicy.IsNull() )
       
  1332                 {
       
  1333                 destApRecord->iSelectionPolicy.SetL( source->iSelectionPolicy );
       
  1334                 }
       
  1335 
       
  1336             if ( !source->iTier.IsNull() )
       
  1337                 {
       
  1338                 destApRecord->iTier.SetL( source->iTier );
       
  1339                 }
       
  1340             if ( !source->iMCpr.IsNull() )
       
  1341                 {
       
  1342                 destApRecord->iMCpr.SetL( source->iMCpr );
       
  1343                 }
       
  1344             if ( !source->iCpr.IsNull() )
       
  1345                 {
       
  1346                 destApRecord->iCpr.SetL( source->iCpr );
       
  1347                 }
       
  1348             if ( !source->iSCpr.IsNull() )
       
  1349                 {
       
  1350                 destApRecord->iSCpr.SetL( source->iSCpr );
       
  1351                 }
       
  1352             if ( !source->iProtocol.IsNull() )
       
  1353                 {
       
  1354                 destApRecord->iProtocol.SetL( source->iProtocol );
       
  1355                 }
       
  1356             if ( !source->iCprConfig.IsNull() )
       
  1357                 {
       
  1358                 destApRecord->iCprConfig.SetL( source->iCprConfig );
       
  1359                 }
       
  1360             if ( !source->iAppSID.IsNull() )
       
  1361                 {
       
  1362                 destApRecord->iAppSID.SetL( source->iAppSID );
       
  1363                 }
       
  1364             if ( !source->iConfigAPIdList.IsNull() )
       
  1365                 {
       
  1366                 destApRecord->iConfigAPIdList.SetL( source->iConfigAPIdList );
       
  1367                 }
       
  1368             if ( !source->iCustomSelectionPolicy.IsNull() )
       
  1369                 {
       
  1370                 destApRecord->iCustomSelectionPolicy.SetL( source->iCustomSelectionPolicy );
       
  1371                 }
       
  1372             if ( !source->iPriority.IsNull() )
       
  1373                 {
       
  1374                 destApRecord->iPriority.SetL( source->iPriority );
       
  1375                 }
       
  1376 
       
  1377             destApRecord->SetElementId( aSource->ElementId() );
       
  1378 
       
  1379             CleanupStack::Pop( destApRecord );
       
  1380             target = static_cast<CommsDat::CCDRecordBase*>( destApRecord );
       
  1381             }
       
  1382             break;
       
  1383         case ECmmDestMetadataRecord:
       
  1384             {
       
  1385             // Metadata record.
       
  1386             CCDSNAPMetadataRecord* source =
       
  1387                     static_cast<CCDSNAPMetadataRecord*>( aSource );
       
  1388             CCDSNAPMetadataRecord* metadataRecord =
       
  1389                     new( ELeave ) CCDSNAPMetadataRecord(
       
  1390                             iCache->TableId( ECmmDestMetadataRecord ) );
       
  1391             CleanupStack::PushL( metadataRecord );
       
  1392 
       
  1393             if ( !source->iRecordTag.IsNull() )
       
  1394                 {
       
  1395                 metadataRecord->iRecordTag.SetL( source->iRecordTag );
       
  1396                 }
       
  1397             if ( !source->iRecordName.IsNull() )
       
  1398                 {
       
  1399                 metadataRecord->iRecordName.SetL( source->iRecordName );
       
  1400                 }
       
  1401             if ( !source->iSNAP.IsNull() )
       
  1402                 {
       
  1403                 metadataRecord->iSNAP.SetL( source->iSNAP );
       
  1404                 }
       
  1405             if ( !source->iMetadata.IsNull() )
       
  1406                 {
       
  1407                 metadataRecord->iMetadata.SetL( source->iMetadata );
       
  1408                 }
       
  1409             if ( !source->iIcon.IsNull() )
       
  1410                 {
       
  1411                 metadataRecord->iIcon.SetL( source->iIcon );
       
  1412                 }
       
  1413             if ( !source->iIconFileName.IsNull() )
       
  1414                 {
       
  1415                 metadataRecord->iIconFileName.SetL( source->iIconFileName );
       
  1416                 }
       
  1417             metadataRecord->SetElementId( source->ElementId() );
       
  1418 
       
  1419             CleanupStack::Pop( metadataRecord );
       
  1420             target = static_cast<CommsDat::CCDRecordBase*>( metadataRecord );
       
  1421             }
       
  1422             break;
       
  1423         default:
       
  1424             {
       
  1425             User::Leave( KErrArgument );
       
  1426             }
       
  1427             break;
       
  1428         }
       
  1429 
       
  1430     OstTraceFunctionExit0( CCMMDESTINATIONSTRUCT_COPYRECORDL_EXIT );
       
  1431     return target;
       
  1432     }
       
  1433 
       
  1434 // End of file