iaupdate/IAD/api/client/src/iaupdatemanager.cpp
changeset 0 ba25891c3a9e
child 18 f9033e605ee2
equal deleted inserted replaced
-1:000000000000 0:ba25891c3a9e
       
     1 /*
       
     2 * Copyright (c) 2007-2008 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:   This module contains the implementation of CIAUpdateManager
       
    15 *                class member functions.
       
    16 *
       
    17 */
       
    18 
       
    19 
       
    20 
       
    21 // INCLUDES
       
    22 #include <eikenv.h>
       
    23 #include <iaupdateobserver.h>
       
    24 #include <iaupdateresult.h>
       
    25 
       
    26 #include "iaupdatemanager.h"
       
    27 #include "iaupdateclient.h"
       
    28 #include "iaupdatedebug.h"
       
    29 
       
    30 
       
    31 // -----------------------------------------------------------------------------
       
    32 // CIAUpdateManager::NewL
       
    33 // 
       
    34 // -----------------------------------------------------------------------------
       
    35 // 
       
    36 CIAUpdateManager* CIAUpdateManager::NewL( MIAUpdateObserver& aObserver )
       
    37     {
       
    38     IAUPDATE_TRACE("[IAUPDATE] CIAUpdateManager::NewL() begin");
       
    39     CIAUpdateManager* self = CIAUpdateManager::NewLC( aObserver );
       
    40     CleanupStack::Pop( self );
       
    41     IAUPDATE_TRACE("[IAUPDATE] CIAUpdateManager::NewL() end");
       
    42     return self;
       
    43     }
       
    44 
       
    45 // -----------------------------------------------------------------------------
       
    46 // CIAUpdateManager::NewLC
       
    47 // 
       
    48 // -----------------------------------------------------------------------------
       
    49 //    
       
    50 CIAUpdateManager* CIAUpdateManager::NewLC( MIAUpdateObserver& aObserver )
       
    51     {
       
    52     IAUPDATE_TRACE("[IAUPDATE] CIAUpdateManager::NewLC() begin");
       
    53     CIAUpdateManager* self = new( ELeave ) CIAUpdateManager( aObserver );
       
    54     CleanupStack::PushL( self );
       
    55     self->ConstructL();
       
    56     IAUPDATE_TRACE("[IAUPDATE] CIAUpdateManager::NewLC() end");
       
    57     return self;
       
    58     }
       
    59 
       
    60 // -----------------------------------------------------------------------------
       
    61 // CIAUpdateManager::CIAUpdateManager
       
    62 // 
       
    63 // -----------------------------------------------------------------------------
       
    64 //    
       
    65 CIAUpdateManager::CIAUpdateManager( MIAUpdateObserver& aObserver )
       
    66 : CActive( CActive::EPriorityStandard ),
       
    67   iObserver( aObserver ),
       
    68   iUpdateType( EIAUpdateIdle )
       
    69     {
       
    70     IAUPDATE_TRACE("[IAUPDATE] CIAUpdateManager::CIAUpdateManager()");
       
    71     iEikEnv = CEikonEnv::Static();
       
    72     }
       
    73 
       
    74 // -----------------------------------------------------------------------------
       
    75 // CIAUpdateManager::ConstructL()
       
    76 // 
       
    77 // -----------------------------------------------------------------------------
       
    78 //    
       
    79 void CIAUpdateManager::ConstructL()
       
    80     {
       
    81     IAUPDATE_TRACE("[IAUPDATE] CIAUpdateManager::ConstructL() begin");
       
    82     CActiveScheduler::Add( this );
       
    83     if ( iEikEnv )
       
    84         {
       
    85     	iEikEnv->AddForegroundObserverL(*this);
       
    86         }
       
    87     IAUPDATE_TRACE("[IAUPDATE] CIAUpdateManager::ConstructL() end");
       
    88     }
       
    89 
       
    90 // -----------------------------------------------------------------------------
       
    91 // CIAUpdateManager::~CIAUpdateManager
       
    92 // 
       
    93 // -----------------------------------------------------------------------------
       
    94 //
       
    95 CIAUpdateManager::~CIAUpdateManager()
       
    96     {
       
    97     IAUPDATE_TRACE("[IAUPDATE] CIAUpdateManager::~CIAUpdateManager() begin");
       
    98     // If an operation is still active, 
       
    99     // then DoCancel will cancel ongoing request
       
   100     Cancel();
       
   101     UpdateClient().Close();
       
   102     if ( iEikEnv )
       
   103         {
       
   104     	iEikEnv->RemoveForegroundObserver(*this);
       
   105         }
       
   106     IAUPDATE_TRACE("[IAUPDATE] CIAUpdateManager::~CIAUpdateManager() end");
       
   107     }
       
   108 
       
   109 // -----------------------------------------------------------------------------
       
   110 // CIAUpdateManager::CheckUpdates
       
   111 // 
       
   112 // -----------------------------------------------------------------------------
       
   113 //
       
   114 void CIAUpdateManager::CheckUpdates( const CIAUpdateParameters& aUpdateParameters )
       
   115     {
       
   116     IAUPDATE_TRACE("[IAUPDATE] CIAUpdateManager::CheckUpdates() begin");
       
   117     if ( BusyCheck() )
       
   118         {
       
   119         // An operation is already going on.
       
   120         // Do not continue with this new operation.
       
   121         // Only, inform the observer with the error code.
       
   122         // Notice that the callback is called synchronously here 
       
   123         // because there is no better way to do it. Asynchronous way 
       
   124         // would be really cumbersome because this object is already active. 
       
   125         // This error is users fault. Therefore we, could also panic here.
       
   126         Observer().CheckUpdatesComplete( KErrServerBusy, 0 );
       
   127         return;
       
   128         }
       
   129 
       
   130     // Reset result values because we are starting a new operation.
       
   131     ResetResults();
       
   132 
       
   133     // Set the update type. 
       
   134     // So, we know later in RunL what operation was requested.
       
   135     SetUpdateType( EIAUpdateCheck );
       
   136     
       
   137     TInt error( UpdateClient().Open( ETrue) );
       
   138     if ( error == KErrNone )
       
   139         {    
       
   140         error =
       
   141             UpdateClient().CheckUpdates( aUpdateParameters, 
       
   142                                          iSuccessCount, 
       
   143                                          iStatus );
       
   144         }        
       
   145 
       
   146     if ( error == KErrNone )
       
   147         {
       
   148         // Set this object active.
       
   149         // Because everything went ok, 
       
   150         // the operation will be handled asynchronously
       
   151         // and the service provider will inform us when the operation
       
   152         // is finished.
       
   153         SetActive();        
       
   154         }
       
   155     else
       
   156         {
       
   157         // Because we are going to activate this active object,
       
   158         // set the status pending.
       
   159         iStatus = KRequestPending;
       
   160         
       
   161         // An error occurred above. 
       
   162         // Therefore, the operation did not proceed any further.
       
   163         // Set this object active for asynchronous error handling.
       
   164         SetActive();
       
   165                 
       
   166         // Now, that everything is ready, just inform the active scheduler
       
   167         // that operation is finished. Pass the error code for the observer.
       
   168         TRequestStatus* status( &iStatus );
       
   169         User::RequestComplete( status, error );                
       
   170         }
       
   171     IAUPDATE_TRACE("[IAUPDATE] CIAUpdateManager::CheckUpdates() end");
       
   172     }
       
   173 
       
   174 // -----------------------------------------------------------------------------
       
   175 // CIAUpdateManager::ShowUpdates
       
   176 // 
       
   177 // -----------------------------------------------------------------------------
       
   178 // 
       
   179 void CIAUpdateManager::ShowUpdates( const CIAUpdateParameters& aUpdateParameters )
       
   180     {
       
   181     IAUPDATE_TRACE("[IAUPDATE] CIAUpdateManager::ShowUpdates() begin");
       
   182     if ( BusyCheck() )
       
   183          {
       
   184          // An update operation is already going on.
       
   185          // Do not continue with this new operation.
       
   186          // Only, inform the observer with the error code.
       
   187          // Notice that the callback is called synchronously here 
       
   188          // because there is no better way to do it. Asynchronous way 
       
   189          // would be really cumbersome because this object is already active. 
       
   190          // This error is users fault. Therefore we, could also panic here.
       
   191          Observer().UpdateComplete( KErrServerBusy, NULL );
       
   192          return;
       
   193          }
       
   194 
       
   195     // Reset result values because we are starting a new operation.
       
   196     ResetResults();
       
   197 
       
   198     // Set the update type. 
       
   199     // So, we know later in RunL what operation was requested.
       
   200     SetUpdateType( EIAUpdateUpdate );
       
   201     
       
   202     TInt error( UpdateClient().Open( EFalse ) );
       
   203     if ( error == KErrNone )
       
   204         {    
       
   205         error =
       
   206             UpdateClient().ShowUpdates( aUpdateParameters, 
       
   207                                         iSuccessCount,
       
   208                                         iFailCount,
       
   209                                         iCancelCount,  
       
   210                                         iStatus );
       
   211         }        
       
   212 
       
   213     if ( error == KErrNone )
       
   214         {
       
   215         // Set this object active.
       
   216         // Because everything went ok, 
       
   217         // the operation will be handled asynchronously
       
   218         // and the service provider will inform us when the operation
       
   219         // is finished.
       
   220         SetActive();        
       
   221         }
       
   222     else
       
   223         {
       
   224         // Because we are going to activate this active object,
       
   225         // set the status pending.
       
   226         iStatus = KRequestPending;
       
   227         
       
   228         // An error occurred above. 
       
   229         // Therefore, the operation did not proceed any further.
       
   230         // Set this object active for asynchronous error handling.
       
   231         SetActive();
       
   232                 
       
   233         // Now, that everything is ready, just inform the active scheduler
       
   234         // that operation is finished. Pass the error code for the observer.
       
   235         TRequestStatus* status( &iStatus );
       
   236         User::RequestComplete( status, error );                
       
   237         }
       
   238     IAUPDATE_TRACE("[IAUPDATE] CIAUpdateManager::ShowUpdates() end");
       
   239     }
       
   240     
       
   241 // -----------------------------------------------------------------------------
       
   242 // CIAUpdateManager::Update
       
   243 // 
       
   244 // -----------------------------------------------------------------------------
       
   245 // 
       
   246 void CIAUpdateManager::Update( const CIAUpdateParameters& /*aUpdateParameters*/ )
       
   247     {
       
   248     IAUPDATE_TRACE("[IAUPDATE] CIAUpdateManager::Update() begin");
       
   249     Observer().UpdateComplete( KErrNotSupported, NULL );
       
   250     
       
   251     
       
   252     IAUPDATE_TRACE("[IAUPDATE] CIAUpdateManager::Update() begin");
       
   253     }
       
   254 
       
   255 // -----------------------------------------------------------------------------
       
   256 // CIAUpdateManager::UpdateQuery
       
   257 // 
       
   258 // -----------------------------------------------------------------------------
       
   259 //   
       
   260 void CIAUpdateManager::UpdateQuery()
       
   261     {
       
   262     IAUPDATE_TRACE("[IAUPDATE] CIAUpdateManager::UpdateQuery() begin");
       
   263     if ( BusyCheck() )
       
   264         {
       
   265         // An update operation is already going on.
       
   266         // Do not continue with this new operation.
       
   267         // Only, inform the observer with the error code.
       
   268         // Notice that the callback is called synchronously here 
       
   269         // because there is no better way to do it. Asynchronous way 
       
   270         // would be really cumbersome because this object is already active. 
       
   271         // This error is users fault. Therefore we, could also panic here.
       
   272         Observer().UpdateQueryComplete( KErrServerBusy, EFalse );
       
   273         return;
       
   274     }
       
   275 
       
   276     // Reset result values because we are starting a new operation.
       
   277     ResetResults();
       
   278 
       
   279     // Set the update type. 
       
   280     // So, we know later in RunL what operation was requested.
       
   281     SetUpdateType( EIAUpdateQuery );
       
   282     
       
   283     TInt error( UpdateClient().Open( EFalse ) );
       
   284     if ( error == KErrNone )
       
   285         {    
       
   286         error =
       
   287             UpdateClient().UpdateQuery( iUpdateNow, iStatus );
       
   288         }        
       
   289 
       
   290     if ( error == KErrNone )
       
   291         {
       
   292         // Set this object active.
       
   293         // Because everything went ok, 
       
   294         // the operation will be handled asynchronously
       
   295         // and the service provider will inform us when the operation
       
   296         // is finished.
       
   297         SetActive();        
       
   298         }
       
   299     else
       
   300         {
       
   301         // Because we are going to activate this active object,
       
   302         // set the status pending.
       
   303         iStatus = KRequestPending;
       
   304         
       
   305         // An error occurred above. 
       
   306         // Therefore, the operation did not proceed any further.
       
   307         // Set this object active for asynchronous error handling.
       
   308         SetActive();
       
   309                 
       
   310         // Now, that everything is ready, just inform the active scheduler
       
   311         // that operation is finished. Pass the error code for the observer.
       
   312         TRequestStatus* status( &iStatus );
       
   313         User::RequestComplete( status, error );                
       
   314         }
       
   315     IAUPDATE_TRACE("[IAUPDATE] CIAUpdateManager::UpdateQuery() end");
       
   316     }
       
   317 
       
   318 // -----------------------------------------------------------------------------
       
   319 // CIAUpdateManager::DoCancel
       
   320 // 
       
   321 // -----------------------------------------------------------------------------
       
   322 //
       
   323 void CIAUpdateManager::DoCancel()
       
   324     {
       
   325     IAUPDATE_TRACE("[IAUPDATE] CIAUpdateManager::DoCancel() begin");
       
   326     UpdateClient().CancelAsyncRequest();
       
   327 
       
   328     // Reset result values because we are starting a new operation.
       
   329     ResetResults();
       
   330     IAUPDATE_TRACE("[IAUPDATE] CIAUpdateManager::DoCancel() end");
       
   331     }
       
   332 
       
   333 // -----------------------------------------------------------------------------
       
   334 // CIAUpdateManager::RunL
       
   335 // 
       
   336 // -----------------------------------------------------------------------------
       
   337 //
       
   338 void CIAUpdateManager::RunL()
       
   339     {
       
   340     IAUPDATE_TRACE("[IAUPDATE] CIAUpdateManager::RunL() begin");
       
   341     // Update operation error code
       
   342     TInt errorCode( iStatus.Int() );
       
   343     IAUPDATE_TRACE_1("[IAUPDATE] error code: %d", errorCode );
       
   344     if ( errorCode == KErrServerTerminated )
       
   345         {
       
   346     	// Handle is released if server is terminated
       
   347         UpdateClient().Close();
       
   348         }
       
   349 
       
   350     // Variables for operation and result information.
       
   351     TIAUpdateType updateType( UpdateType() );
       
   352     IAUPDATE_TRACE_1("[IAUPDATE] update type: %d", updateType );
       
   353     CIAUpdateResult* updateResult( NULL );
       
   354         
       
   355     if ( updateType == EIAUpdateUpdate )
       
   356         {
       
   357         // Update result object is required.
       
   358         // Notice that the ownership is transferred later.
       
   359         // So, this function does not need to delete updateResult object.
       
   360         TRAPD( trapError, updateResult = CIAUpdateResult::NewL() );
       
   361         if ( updateResult )
       
   362             {
       
   363             updateResult->SetSuccessCount( iSuccessCount );
       
   364             updateResult->SetFailCount( iFailCount );
       
   365             updateResult->SetCancelCount( iCancelCount );
       
   366             }
       
   367         else
       
   368             {
       
   369             // Something went wrong when creating update result object.
       
   370             // Update the error code accordingly.
       
   371             errorCode = trapError;
       
   372             }
       
   373         // Let's assume that connection is not needed anymore
       
   374         UpdateClient().Close();
       
   375         }
       
   376 
       
   377     
       
   378     // Inform that no operation is going on anymore.
       
   379     // This is required for busy check.
       
   380     SetUpdateType( EIAUpdateIdle );    
       
   381     
       
   382     // Use the update type of the ongoing operation to check what callback
       
   383     // function to call.
       
   384     switch ( updateType )
       
   385         {
       
   386         case EIAUpdateCheck:
       
   387             if ( iSuccessCount == 0 )
       
   388                 {
       
   389             	// Let's assume that connection is not needed anymore
       
   390                 UpdateClient().Close();
       
   391                 }
       
   392             IAUPDATE_TRACE_1("[IAUPDATE] success count: %d", iSuccessCount );
       
   393             Observer().CheckUpdatesComplete( errorCode, iSuccessCount );
       
   394             break;
       
   395 
       
   396         case EIAUpdateUpdate:
       
   397             // Notice that ownership of result object is transferred here.
       
   398             IAUPDATE_TRACE_3("[IAUPDATE] success count: %d failed count: %d cancelled count: %d", iSuccessCount, iFailCount, iCancelCount );
       
   399             Observer().UpdateComplete( errorCode, updateResult );
       
   400             break;
       
   401             
       
   402         case EIAUpdateQuery:
       
   403             if ( !iUpdateNow )
       
   404                 {
       
   405             	// Let's assume that connection is not needed anymore
       
   406                 UpdateClient().Close();
       
   407                 } 
       
   408             IAUPDATE_TRACE_1("[IAUPDATE] update now: %d", iUpdateNow );
       
   409             Observer().UpdateQueryComplete( errorCode, iUpdateNow );
       
   410             break;
       
   411          
       
   412         default:
       
   413             // Should not ever come here.
       
   414             break;
       
   415         }
       
   416         
       
   417     // Do not anything else than return after callback function is called because 
       
   418     // this instance can be deleted by a client in a callback function
       
   419     // 
       
   420     IAUPDATE_TRACE("[IAUPDATE] CIAUpdateManager::RunL() end");
       
   421     }
       
   422 
       
   423 // -----------------------------------------------------------------------------
       
   424 // CIAUpdateManager::ResetResults
       
   425 // 
       
   426 // -----------------------------------------------------------------------------
       
   427 //
       
   428 void CIAUpdateManager::ResetResults()
       
   429     {
       
   430     IAUPDATE_TRACE("[IAUPDATE] CIAUpdateManager::ResetResults() begin");
       
   431     // Set the member variables to their default values.
       
   432     iSuccessCount = 0;
       
   433     iFailCount = 0;
       
   434     iCancelCount = 0;
       
   435     iUpdateNow = EFalse;
       
   436     IAUPDATE_TRACE("[IAUPDATE] CIAUpdateManager::ResetResults() end");
       
   437     }
       
   438 
       
   439 // -----------------------------------------------------------------------------
       
   440 // CIAUpdateManager::BusyCheck
       
   441 // 
       
   442 // -----------------------------------------------------------------------------
       
   443 //
       
   444 TBool CIAUpdateManager::BusyCheck() const
       
   445     {
       
   446     IAUPDATE_TRACE("[IAUPDATE] CIAUpdateManager::BusyCheck()");
       
   447     if ( UpdateType() == EIAUpdateIdle )
       
   448         {
       
   449         return EFalse;
       
   450         }
       
   451     else
       
   452         {
       
   453         return ETrue; 
       
   454         }
       
   455     }
       
   456     
       
   457 // -----------------------------------------------------------------------------
       
   458 // CIAUpdateManager::UpdateClient 
       
   459 // 
       
   460 // -----------------------------------------------------------------------------
       
   461 //
       
   462 RIAUpdateClient& CIAUpdateManager::UpdateClient()
       
   463     {
       
   464     IAUPDATE_TRACE("[IAUPDATE] CIAUpdateManager::UpdateClient()");
       
   465     return iUpdateClient;
       
   466     }
       
   467 
       
   468 // -----------------------------------------------------------------------------
       
   469 // CIAUpdateManager::Observer
       
   470 // 
       
   471 // -----------------------------------------------------------------------------
       
   472 //
       
   473 MIAUpdateObserver& CIAUpdateManager::Observer() const
       
   474     {
       
   475     IAUPDATE_TRACE("[IAUPDATE] CIAUpdateManager::Observer()");
       
   476     return iObserver;
       
   477     }
       
   478 
       
   479 // -----------------------------------------------------------------------------
       
   480 // CIAUpdateManager::UpdateType
       
   481 // 
       
   482 // -----------------------------------------------------------------------------
       
   483 //
       
   484 CIAUpdateManager::TIAUpdateType CIAUpdateManager::UpdateType() const
       
   485     {
       
   486     IAUPDATE_TRACE("[IAUPDATE] CIAUpdateManager::UpdateType()");
       
   487     return iUpdateType;
       
   488     }
       
   489 
       
   490 
       
   491 // -----------------------------------------------------------------------------
       
   492 // CIAUpdateManager::SetUpdateType
       
   493 // 
       
   494 // -----------------------------------------------------------------------------
       
   495 // 
       
   496 void CIAUpdateManager::SetUpdateType( CIAUpdateManager::TIAUpdateType aUpdateType )
       
   497     {
       
   498     IAUPDATE_TRACE("[IAUPDATE] CIAUpdateManager::SetUpdateType() begin");
       
   499     IAUPDATE_TRACE_1("[IAUPDATE] update type: %d", aUpdateType );
       
   500     iUpdateType = aUpdateType;
       
   501     IAUPDATE_TRACE("[IAUPDATE] CIAUpdateManager::SetUpdateType() end");
       
   502     }
       
   503  
       
   504 // -----------------------------------------------------------------------------
       
   505 // CIAUpdateManager::HandleGainingForeground
       
   506 //  iaupdate.exe is brought to foreground if visible request is ongoing
       
   507 // -----------------------------------------------------------------------------
       
   508 //     
       
   509 void CIAUpdateManager::HandleGainingForeground()
       
   510     {
       
   511     IAUPDATE_TRACE("[IAUPDATE] CIAUpdateManager::HandleGainingForeground() begin");
       
   512 	iUpdateClient.BroughtToForeground();   
       
   513 	IAUPDATE_TRACE("[IAUPDATE] CIAUpdateManager::HandleGainingForeground() end");  
       
   514     }
       
   515 
       
   516 // -----------------------------------------------------------------------------
       
   517 // CIAUpdateManager::HandleLosingForeground
       
   518 // 
       
   519 // -----------------------------------------------------------------------------
       
   520 // 
       
   521 void CIAUpdateManager::HandleLosingForeground()
       
   522     {
       
   523     }
       
   524 
       
   525 
       
   526 
       
   527 
       
   528 
       
   529