presetserver/clientsrc/Pspresetinterface.cpp
changeset 0 09774dfdd46b
child 12 608f67c22514
equal deleted inserted replaced
-1:000000000000 0:09774dfdd46b
       
     1 /*
       
     2 * Copyright (c) 2006-2006 Nokia Corporation and/or its subsidiary(-ies). 
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:  Preset interface implementation
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include <ecom.h>
       
    20 #include <pscommon.h>
       
    21 #include <pspresetinterface.h>
       
    22 #include <pspresetnotifier.h>
       
    23 #include <pstransaction.h>
       
    24 #include <s32mem.h>
       
    25 
       
    26 #include "psdebug.h"
       
    27 #include "psservsession.h"
       
    28 
       
    29 const TInt KPSExternalizeBufGranularity = 32; // Granularity of the preset data externalisation buffer.
       
    30 
       
    31 /**
       
    32  * Preset interface initialization parameters.
       
    33  */
       
    34 typedef struct
       
    35     {
       
    36     /** Session with the preset server. */
       
    37     RPSServ* iServ;
       
    38     /** Data handler of the preset. */
       
    39     TUid iDataHandler;
       
    40     /** Id of the preset. */
       
    41     TInt iId;
       
    42     } TPSPresetInterfaceParams;
       
    43 
       
    44 // ---------------------------------------------------------------------------
       
    45 // Panics the process.
       
    46 // ---------------------------------------------------------------------------
       
    47 //
       
    48 void Panic( TInt aReason )
       
    49     {
       
    50     _LIT( category, "CPSPresetInterface" );
       
    51     User::Panic( category, aReason );
       
    52     }
       
    53 
       
    54 // ---------------------------------------------------------------------------
       
    55 // Two-phased constructor.
       
    56 // ---------------------------------------------------------------------------
       
    57 //
       
    58 CPSPresetInterface* CPSPresetInterface::CreateL( RPSServ& aServ, TInt aId, TUid aDataHandler )
       
    59     {
       
    60     TPSPresetInterfaceParams params = { &aServ, aDataHandler, aId };
       
    61     CPSPresetInterface* self = static_cast<CPSPresetInterface*>( REComSession::CreateImplementationL( aDataHandler, _FOFF( CPSPresetInterface, iDtor ), &params ) );
       
    62     CleanupStack::PushL( self );
       
    63     self->UpdatePresetL();
       
    64     CleanupStack::Pop( self );
       
    65     return self;
       
    66     }
       
    67 
       
    68 // ---------------------------------------------------------------------------
       
    69 // Destructor.
       
    70 // ---------------------------------------------------------------------------
       
    71 //
       
    72 EXPORT_C CPSPresetInterface::~CPSPresetInterface()
       
    73     {
       
    74     delete iName;
       
    75     delete iNotifier;
       
    76     
       
    77     if ( iSession )
       
    78         {
       
    79         iSession->Close();
       
    80         }
       
    81     delete iSession;
       
    82     
       
    83     REComSession::DestroyedImplementation( iDtor );
       
    84     }
       
    85 
       
    86 // ---------------------------------------------------------------------------
       
    87 // Creates a transaction.
       
    88 // Transfers ownership.
       
    89 // ---------------------------------------------------------------------------
       
    90 //
       
    91 EXPORT_C CPSTransaction* CPSPresetInterface::CreateTransactionL()
       
    92     {
       
    93     CPSTransaction* transaction = CreateTransactionLC();
       
    94     CleanupStack::Pop( transaction );
       
    95     return transaction;
       
    96     }
       
    97 
       
    98 // ---------------------------------------------------------------------------
       
    99 // Creates a transaction and leaves it on the cleanup stack.
       
   100 // Transfers ownership.
       
   101 // ---------------------------------------------------------------------------
       
   102 //
       
   103 EXPORT_C CPSTransaction* CPSPresetInterface::CreateTransactionLC()
       
   104     {
       
   105     CPSTransaction* transaction = CPSTransaction::NewL( *this );
       
   106     CleanupStack::PushL( transaction );
       
   107     return transaction;
       
   108     }
       
   109 
       
   110 // ---------------------------------------------------------------------------
       
   111 // Sets the name of the preset.
       
   112 // ---------------------------------------------------------------------------
       
   113 //
       
   114 EXPORT_C void CPSPresetInterface::SetNameL( const TDesC& aName )
       
   115     {
       
   116     ValidateDataChange();
       
   117 
       
   118     delete iName;
       
   119     iName = NULL;
       
   120     iName = aName.AllocL();
       
   121     }
       
   122 
       
   123 // ---------------------------------------------------------------------------
       
   124 // Returns the name of the preset.
       
   125 // ---------------------------------------------------------------------------
       
   126 //
       
   127 EXPORT_C const TDesC& CPSPresetInterface::Name() const
       
   128     {
       
   129     return *iName;
       
   130     }
       
   131 
       
   132 // ---------------------------------------------------------------------------
       
   133 // Sets the index of the preset.
       
   134 // ---------------------------------------------------------------------------
       
   135 //
       
   136 EXPORT_C void CPSPresetInterface::SetIndex( TInt aIndex )
       
   137     {    
       
   138     ValidateDataChange();
       
   139 
       
   140     iIndex = aIndex;
       
   141     }
       
   142 
       
   143 // ---------------------------------------------------------------------------
       
   144 // Returns the index of the preset.
       
   145 // ---------------------------------------------------------------------------
       
   146 //
       
   147 EXPORT_C TInt CPSPresetInterface::Index() const
       
   148     {
       
   149     return iIndex; 
       
   150     }
       
   151 
       
   152 // ---------------------------------------------------------------------------
       
   153 // Returns the id of the preset.
       
   154 // ---------------------------------------------------------------------------
       
   155 //
       
   156 EXPORT_C TInt CPSPresetInterface::Id() const
       
   157     {
       
   158     return iId;
       
   159     }
       
   160 
       
   161 // ---------------------------------------------------------------------------
       
   162 // Returns the data handler of the preset.
       
   163 // ---------------------------------------------------------------------------
       
   164 //
       
   165 EXPORT_C TUid CPSPresetInterface::DataHandler() const
       
   166     {
       
   167     return iDataHandler;
       
   168     }
       
   169 
       
   170 // ---------------------------------------------------------------------------
       
   171 // Performs base construction on the preset.
       
   172 // ---------------------------------------------------------------------------
       
   173 //
       
   174 EXPORT_C void CPSPresetInterface::BaseConstructL( TAny* aParams )
       
   175     {
       
   176     const TPSPresetInterfaceParams& params = *static_cast<TPSPresetInterfaceParams*>( aParams );
       
   177     
       
   178     iDataHandler = params.iDataHandler;
       
   179     iId = params.iId;
       
   180 
       
   181     iSession = new ( ELeave ) RPSServSession( *params.iServ, iId );
       
   182     iNotifier = CPSPresetNotifier::NewL( *params.iServ, *this, iId );
       
   183     }
       
   184 
       
   185 // ---------------------------------------------------------------------------
       
   186 // Validates the preset for data change.
       
   187 // ---------------------------------------------------------------------------
       
   188 //  
       
   189 EXPORT_C void CPSPresetInterface::ValidateDataChange()
       
   190     {
       
   191     __ASSERT_ALWAYS( iTransaction, Panic( KErrNotReady ) );
       
   192     }
       
   193 
       
   194 // ---------------------------------------------------------------------------
       
   195 // Handles changes in the preset.
       
   196 // ---------------------------------------------------------------------------
       
   197 //  
       
   198 EXPORT_C void CPSPresetInterface::HandlePresetChangedL( TInt PSDEBUGVAR( aId ), TUid /*aDataHandler*/, MPSPresetObserver::TPSReason aReason )
       
   199     {
       
   200     __ASSERT_DEBUG( iId == aId, Panic( KErrCorrupt ) );
       
   201 
       
   202     switch ( aReason )
       
   203         {
       
   204         case MPSPresetObserver::EPSModified:
       
   205             UpdatePresetL();
       
   206             break;            
       
   207         case MPSPresetObserver::EPSDeleted:
       
   208             // Preset deleted from the database, close the session.
       
   209             iSession->Close();
       
   210             delete iSession;
       
   211             iSession = NULL;                            
       
   212             break;
       
   213         case MPSPresetObserver::EPSCreated: // Falls through.                
       
   214         default:
       
   215             break;
       
   216         }
       
   217     }
       
   218 
       
   219 // ---------------------------------------------------------------------------
       
   220 // Begins the transaction.
       
   221 // ---------------------------------------------------------------------------
       
   222 //
       
   223 EXPORT_C void CPSPresetInterface::BeginTransactionL()
       
   224     {
       
   225     if( iSession )
       
   226         {
       
   227         iSession->BeginTransactionL();
       
   228         iTransaction = ETrue;
       
   229         }
       
   230     }
       
   231 
       
   232 // ---------------------------------------------------------------------------
       
   233 // Commits the transaction.
       
   234 // ---------------------------------------------------------------------------
       
   235 //
       
   236 EXPORT_C void CPSPresetInterface::CommitL()
       
   237     {
       
   238     if( iSession )
       
   239         {
       
   240         iSession->SetL( EPSOpSetPresetName, *iName );
       
   241         iSession->SetL( EPSOpSetPresetIndex, iIndex );    
       
   242     
       
   243         CBufFlat* buf = CBufFlat::NewL( KPSExternalizeBufGranularity );
       
   244         CleanupStack::PushL( buf );
       
   245         RBufWriteStream stream( *buf );
       
   246         CleanupClosePushL( stream );
       
   247         ExternalizeL( stream );
       
   248         stream.CommitL();
       
   249         CleanupStack::PopAndDestroy( &stream );
       
   250         iSession->SetL( EPSOpSetPresetData, buf->Ptr( 0 ) );
       
   251         CleanupStack::PopAndDestroy( buf );
       
   252 
       
   253         iSession->CommitL();
       
   254     
       
   255         iTransaction = EFalse;
       
   256         }        
       
   257     }
       
   258     
       
   259 // ---------------------------------------------------------------------------
       
   260 // Reverts all changes done during the transaction.
       
   261 // ---------------------------------------------------------------------------
       
   262 //
       
   263 EXPORT_C void CPSPresetInterface::Rollback()
       
   264     {
       
   265     if( iSession )
       
   266         {
       
   267         iSession->Rollback();
       
   268         iTransaction = EFalse;
       
   269         }
       
   270     }
       
   271 
       
   272 // ---------------------------------------------------------------------------
       
   273 // Updates the preset.
       
   274 // ---------------------------------------------------------------------------
       
   275 //  
       
   276 void CPSPresetInterface::UpdatePresetL()
       
   277     {
       
   278     if( iSession )
       
   279         {
       
   280         iSession->GetL( EPSOpGetPresetIndex, iIndex );
       
   281     
       
   282         delete iName;
       
   283         iName = NULL;
       
   284     
       
   285         iSession->GetL( EPSOpGetPresetName, EPSOpGetPresetNameLength, iName );
       
   286 
       
   287         HBufC8* data;
       
   288         iSession->GetL( EPSOpGetPresetData, EPSOpGetPresetDataLength, data );
       
   289         CleanupStack::PushL( data );
       
   290     
       
   291         if ( data->Length() )
       
   292             {
       
   293             // There was packed data in the database, implementing plugin can unpack it now.
       
   294             RDesReadStream stream( *data );
       
   295             CleanupClosePushL( stream );
       
   296             InternalizeL( stream );
       
   297             CleanupStack::PopAndDestroy( &stream );
       
   298             }
       
   299 
       
   300         CleanupStack::PopAndDestroy( data );
       
   301         }
       
   302     }
       
   303