iaupdate/IAD/ui/src/iaupdateengine.cpp
changeset 29 26b6f0522fd8
child 33 8110bf1194d1
equal deleted inserted replaced
25:98b66e4fb0be 29:26b6f0522fd8
       
     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:   This module contains the implementation of IAUpdateEngine
       
    15 *                class member functions.
       
    16 *
       
    17 */
       
    18 
       
    19 #include <qapplication.h>
       
    20 #include <hbmessagebox.h>
       
    21 #include <hbaction.h>
       
    22 #include <eikenv.h>
       
    23 #include <centralrepository.h>
       
    24 #include <cmmanagerext.h>
       
    25 #include <cmdestinationext.h>
       
    26 #include <rconnmon.h>
       
    27 #include <apgwgnam.h>
       
    28 
       
    29 #include "iaupdateengine.h"
       
    30 #include "iaupdateserviceprovider.h"
       
    31 #include "iaupdateuicontroller.h"
       
    32 #include "iaupdategloballockhandler.h"
       
    33 #include "iaupdatenodefilter.h"
       
    34 #include "iaupdateresult.h"
       
    35 #include "iaupdateprivatecrkeys.h"
       
    36 #include "iaupdateuiconfigdata.h"
       
    37 #include "iaupdatequeryhistory.h"
       
    38 #include "iaupdateparameters.h"
       
    39 #include "iaupdatedebug.h"
       
    40 
       
    41 IAUpdateEngine::IAUpdateEngine(QObject *parent)
       
    42     : QObject(parent),
       
    43       iController(NULL),
       
    44       iGlobalLockHandler(NULL),
       
    45       iIdle(NULL),
       
    46       iUpdateNow(EFalse),
       
    47       iRequestIssued(EFalse),
       
    48       iStartedFromApplication(EFalse),
       
    49       iUpdatequeryUid(0)
       
    50 {
       
    51     IAUPDATE_TRACE("[IAUPDATE] IAUpdateEngine::IAUpdateEngine() begin");
       
    52     iEikEnv = CEikonEnv::Static();
       
    53     mServiceProvider = new IAUpdateServiceProvider( *this );
       
    54     connect(mServiceProvider, SIGNAL(clientDisconnected()), this, SLOT(handleAllClientsClosed()));
       
    55     TRAP_IGNORE( iController = CIAUpdateUiController::NewL( *this ));
       
    56     IAUPDATE_TRACE("[IAUPDATE] IAUpdateEngine::IAUpdateEngine() end")
       
    57 }
       
    58 
       
    59 
       
    60 IAUpdateEngine::~IAUpdateEngine()
       
    61 {
       
    62     IAUPDATE_TRACE("[IAUPDATE] IAUpdateEngine::~IAUpdateEngine() begin");
       
    63     InformRequestObserver( KErrCancel );
       
    64     delete iGlobalLockHandler;
       
    65     delete iController;
       
    66     delete mServiceProvider;    
       
    67     IAUPDATE_TRACE("[IAUPDATE] IAUpdateEngine::~IAUpdateEngine() end");
       
    68 }
       
    69 
       
    70 // -----------------------------------------------------------------------------
       
    71 // IAUpdateEngine::StartedByLauncherL
       
    72 // 
       
    73 // -----------------------------------------------------------------------------
       
    74 //
       
    75 void IAUpdateEngine::StartedByLauncherL( TBool aRefreshFromNetworkDenied )
       
    76     {
       
    77     IAUPDATE_TRACE("[IAUPDATE] IAUpdateEngine::StartedByLauncherL() begin");
       
    78     iRequestIssued = ETrue;
       
    79     iRequestType = IAUpdateUiDefines::ENoRequest;
       
    80     iController->SetRequestType( iRequestType );
       
    81     SetVisibleL( ETrue );
       
    82     CIAUpdateParameters* params = iController->ParamsReadAndRemoveFileL();
       
    83     iController->CheckUpdatesDeferredL( params, aRefreshFromNetworkDenied );
       
    84     IAUPDATE_TRACE("[IAUPDATE] IAUpdateEngine::StartedByLauncherL() end");
       
    85     }
       
    86 
       
    87 // -----------------------------------------------------------------------------
       
    88 // IAUpdateEngine::CheckUpdatesRequestL
       
    89 // 
       
    90 // -----------------------------------------------------------------------------
       
    91 //
       
    92 void IAUpdateEngine::CheckUpdatesRequestL( int wgid, CIAUpdateParameters* aFilterParams )
       
    93                                            
       
    94     {
       
    95     IAUPDATE_TRACE("[IAUPDATE] IAUpdateEngine::CheckUpdatesRequestL() begin");
       
    96     SetClientWgId( wgid );
       
    97     iRequestIssued = ETrue;
       
    98     iStartedFromApplication  = ETrue;
       
    99     CleanupStack::PushL( aFilterParams );
       
   100     if ( wgid > 0 )
       
   101         {
       
   102         HideApplicationInFSWL( ETrue );
       
   103         }
       
   104     CleanupStack::Pop( aFilterParams );
       
   105     
       
   106     if ( !aFilterParams->ShowProgress() )
       
   107         {
       
   108         iEikEnv->RootWin().SetOrdinalPosition( -1, ECoeWinPriorityNeverAtFront ); 
       
   109         }
       
   110            
       
   111     iRequestType = IAUpdateUiDefines::ECheckUpdates; 
       
   112     iController->SetRequestType( iRequestType );
       
   113     
       
   114     iController->CheckUpdatesDeferredL( aFilterParams, EFalse ); 
       
   115     
       
   116     IAUPDATE_TRACE("[IAUPDATE] IAUpdateEngine::CheckUpdatesRequestL() end");
       
   117     }
       
   118 
       
   119 // -----------------------------------------------------------------------------
       
   120 // IAUpdateEngine::ShowUpdatesRequestL
       
   121 // 
       
   122 // -----------------------------------------------------------------------------
       
   123 // 
       
   124 void IAUpdateEngine::ShowUpdatesRequestL( int wgid, CIAUpdateParameters* aFilterParams )
       
   125     {
       
   126     IAUPDATE_TRACE("[IAUPDATE] IAUpdateEngine::ShowUpdatesRequestL() begin"); 
       
   127     SetClientWgId( wgid );
       
   128     iRequestIssued = ETrue;
       
   129 //    delete iBackgroundTimer;
       
   130 //    iBackgroundTimer = NULL;
       
   131     iStartedFromApplication  = ETrue;
       
   132     CleanupStack::PushL( aFilterParams );
       
   133     if ( wgid > 0 )
       
   134         {
       
   135         HideApplicationInFSWL( ETrue );
       
   136         }
       
   137    
       
   138     //StatusPane()->MakeVisible( ETrue );
       
   139     iEikEnv->RootWin().SetOrdinalPosition( 0, ECoeWinPriorityNormal );
       
   140     
       
   141     //iRequestObserver = &aObserver;
       
   142     iRequestType = IAUpdateUiDefines::EShowUpdates;
       
   143     iController->SetRequestType( iRequestType );
       
   144      
       
   145 
       
   146     //if ( !iMainView )
       
   147     //    {
       
   148     //    iMainView  = CIAUpdateMainView::NewL( ClientRect() ); 
       
   149     //    AddViewL( iMainView );
       
   150     //    }
       
   151     
       
   152       
       
   153     // by pushing object to cleanup stack its destructor is called if leave happens
       
   154     // so global lock issued by this instance can be released in destructor of CIAUpdateGlobalLockHandler
       
   155     CIAUpdateGlobalLockHandler* globalLockHandler = CIAUpdateGlobalLockHandler::NewLC();
       
   156     if ( !globalLockHandler->InUseByAnotherInstanceL() )
       
   157         {
       
   158         globalLockHandler->SetToInUseForAnotherInstancesL( ETrue );
       
   159         CleanupStack::Pop( globalLockHandler );
       
   160         CleanupStack::Pop( aFilterParams );
       
   161         CleanupStack::PushL( globalLockHandler );
       
   162         iController->CheckUpdatesDeferredL( aFilterParams, EFalse );
       
   163         CleanupStack::Pop( globalLockHandler ); 
       
   164         delete iGlobalLockHandler;
       
   165         iGlobalLockHandler = globalLockHandler;
       
   166         //now possible deletion of iGlobalLockHandler in leave situation is handled
       
   167         //in HandleLeaveErrorL() and HandleLeaveErrorWithoutLeave methods. 
       
   168         }
       
   169     else
       
   170         {
       
   171         CleanupStack::PopAndDestroy( globalLockHandler );
       
   172         CleanupStack::PopAndDestroy( aFilterParams );
       
   173         // locked by another IAD instance, nothing else to do than just complete client's request.  
       
   174         InformRequestObserver( KErrNone );
       
   175         }  
       
   176 
       
   177     IAUPDATE_TRACE("[IAUPDATE] IAUpdateEngine::ShowUpdatesRequestL() end"); 
       
   178     }
       
   179     
       
   180 
       
   181 // -----------------------------------------------------------------------------
       
   182 // IAUpdateEngine::ShowUpdateQueryRequestL
       
   183 // 
       
   184 // -----------------------------------------------------------------------------
       
   185 //     
       
   186 void IAUpdateEngine::ShowUpdateQueryRequestL( int wgid, TUint aUid )
       
   187     {
       
   188     IAUPDATE_TRACE("[IAUPDATE] IAUpdateEngine::ShowUpdateQueryRequestL begin");
       
   189     SetClientWgId( wgid );
       
   190     iRequestIssued = ETrue;
       
   191     iStartedFromApplication  = ETrue;
       
   192     iUpdatequeryUid = aUid;
       
   193     iUpdateNow = EFalse;
       
   194     if ( wgid > 0 )
       
   195         {
       
   196         HideApplicationInFSWL( ETrue );
       
   197         }
       
   198     
       
   199     //iRequestObserver = &aObserver;
       
   200     iRequestType = IAUpdateUiDefines::EUpdateQuery;
       
   201     
       
   202     
       
   203     delete iIdle;
       
   204     iIdle = NULL;
       
   205     iIdle = CIdle::NewL( CActive::EPriorityIdle ); 
       
   206     iIdle->Start( TCallBack( UpdateQueryCallbackL, this ) ); 
       
   207     IAUPDATE_TRACE("[IAUPDATE] IAUpdateEngine::ShowUpdateQueryRequestL end")
       
   208     }
       
   209 
       
   210 // -----------------------------------------------------------------------------
       
   211 // IAUpdateEngine::SetVisibleL
       
   212 // 
       
   213 // -----------------------------------------------------------------------------
       
   214 //  
       
   215 void IAUpdateEngine::SetVisibleL( TBool aVisible )
       
   216     {
       
   217     IAUPDATE_TRACE("[IAUPDATE] IAUpdateEngine::SetVisibleL() begin");
       
   218     IAUPDATE_TRACE_1("[IAUPDATE] visible: %d", aVisible );
       
   219     /*if ( aVisible )
       
   220         {
       
   221         if ( iEikonEnv->RootWin().OrdinalPosition() != 0 || iEikonEnv->RootWin().OrdinalPriority() != ECoeWinPriorityNormal )
       
   222             {
       
   223             iEikonEnv->RootWin().SetOrdinalPosition( 0, ECoeWinPriorityNormal );
       
   224             }
       
   225         
       
   226         StatusPane()->MakeVisible( ETrue );
       
   227         iMainView  = CIAUpdateMainView::NewL( ClientRect() ); 
       
   228         AddViewL( iMainView );
       
   229         ActivateLocalViewL( TUid::Uid( EIAUpdateMainViewId ) );
       
   230         }
       
   231     else
       
   232         {
       
   233         iEikonEnv->RootWin().SetOrdinalPosition( -1, ECoeWinPriorityNeverAtFront );
       
   234         StatusPane()->MakeVisible( EFalse );
       
   235         }*/
       
   236     IAUPDATE_TRACE("[IAUPDATE] IAUpdateEngine::SetVisibleL() end");
       
   237     }
       
   238 
       
   239 // -----------------------------------------------------------------------------
       
   240 // IAUpdateEngine::SetClientWgId
       
   241 // 
       
   242 // -----------------------------------------------------------------------------
       
   243 // 
       
   244 void IAUpdateEngine::SetClientWgId( TInt aWgId )
       
   245     {
       
   246     IAUPDATE_TRACE_1("[IAUPDATE] IAUpdateEngine::SetClientWgId() wgId %d", aWgId );
       
   247     iWgId = aWgId;
       
   248     }
       
   249 
       
   250 // -----------------------------------------------------------------------------
       
   251 // IAUpdateEngine::ClientInBackgroundL
       
   252 // 
       
   253 // -----------------------------------------------------------------------------
       
   254 // 
       
   255 TInt IAUpdateEngine::ClientInBackgroundL() const
       
   256     {
       
   257     IAUPDATE_TRACE("[IAUPDATE] IAUpdateEngine::ClientInBackgroundL() begin");
       
   258     TBool inBackground = EFalse;
       
   259     if ( iWgId > 0 )
       
   260         {
       
   261         CArrayFixFlat<TInt>*  wgArray = new( ELeave ) CArrayFixFlat<TInt>(10);  
       
   262         CleanupStack::PushL( wgArray );
       
   263         User::LeaveIfError( iEikEnv->WsSession().WindowGroupList( 0, wgArray ) );  
       
   264         TInt ownWgId = iEikEnv->RootWin().Identifier();
       
   265         if ( ( wgArray->At( 0 ) != ownWgId ) && ( wgArray->At( 0 ) != iWgId  ) )
       
   266             {
       
   267             inBackground = ETrue;
       
   268             }
       
   269         CleanupStack::PopAndDestroy( wgArray );  
       
   270         }
       
   271     IAUPDATE_TRACE_1("[IAUPDATE] IAUpdateEngine::ClientInBackgroundL() inBackground: %d", inBackground );
       
   272     return inBackground;  
       
   273     }
       
   274 
       
   275 
       
   276 
       
   277 // -----------------------------------------------------------------------------
       
   278 // IAUpdateEngine::refresh
       
   279 // 
       
   280 // -----------------------------------------------------------------------------
       
   281 //  
       
   282 /*void IAUpdateEngine::refresh(int error) 
       
   283     {
       
   284     IAUPDATE_TRACE("[IAUPDATE] IAUpdateEngine::refresh() begin");
       
   285     IAUPDATE_TRACE_1("[IAUPDATE] Error code: %d", error );
       
   286     //iMainView->RefreshL( iController->Nodes(), iController->FwNodes(), aError );                    
       
   287     IAUPDATE_TRACE("[IAUPDATE] IAUpdateEngine::refresh() end");
       
   288     }*/
       
   289 
       
   290 
       
   291 void IAUpdateEngine::handleAllClientsClosed()
       
   292 {
       
   293     qApp->quit(); 
       
   294 }
       
   295 
       
   296 
       
   297 // -----------------------------------------------------------------------------
       
   298 // IAUpdateEngine::StartupComplete
       
   299 // 
       
   300 // -----------------------------------------------------------------------------
       
   301 //    
       
   302 void IAUpdateEngine::StartupComplete( TInt aError )
       
   303     {
       
   304     IAUPDATE_TRACE("[IAUPDATE] IAUpdateEngine::StartupComplete() begin"); 
       
   305     IAUPDATE_TRACE_1("[IAUPDATE] error code: %d", aError);
       
   306     
       
   307     if( aError != KErrNone ) 
       
   308         {  
       
   309         HandleLeaveErrorWithoutLeave( aError );
       
   310         }
       
   311     else
       
   312         {
       
   313         TRAPD( err, StartupCompleteL() );
       
   314         if( err != KErrNone ) 
       
   315            {  
       
   316            HandleLeaveErrorWithoutLeave( err );
       
   317            }
       
   318         }
       
   319  
       
   320     IAUPDATE_TRACE("[IAUPDATE] IAUpdateEngine::StartupComplete() end");    
       
   321     }
       
   322     
       
   323 // -----------------------------------------------------------------------------
       
   324 // IAUpdateEngine::StartupCompleteL
       
   325 // 
       
   326 // -----------------------------------------------------------------------------
       
   327 //    
       
   328 void IAUpdateEngine::StartupCompleteL()
       
   329     {
       
   330     IAUPDATE_TRACE("[IAUPDATE] IAUpdateEngine::StartupCompleteL() begin"); 
       
   331     
       
   332     delete iGlobalLockHandler;
       
   333     iGlobalLockHandler = NULL;
       
   334     iGlobalLockHandler = CIAUpdateGlobalLockHandler::NewL();
       
   335     if ( !iGlobalLockHandler->InUseByAnotherInstanceL() )
       
   336         {
       
   337         TBool totalSilent( EFalse );
       
   338         if ( iRequestType == IAUpdateUiDefines::ECheckUpdates )
       
   339             {
       
   340             if ( iController->Filter() )
       
   341                 {
       
   342                 if ( iController->Filter()->FilterParams() )
       
   343                     {
       
   344                     if ( iController->Filter()->FilterParams()->Refresh() )
       
   345                         {
       
   346                         //from bgchecker, make it silent
       
   347                         totalSilent = ETrue;
       
   348                         }
       
   349                     }
       
   350                 }
       
   351             }
       
   352         SetDefaultConnectionMethodL( totalSilent );
       
   353         iGlobalLockHandler->SetToInUseForAnotherInstancesL( ETrue );
       
   354         iController->StartRefreshL();  
       
   355         }
       
   356     else
       
   357         {
       
   358         RefreshCompleteL( ETrue, KErrServerBusy );
       
   359         }    
       
   360  
       
   361        
       
   362     
       
   363     IAUPDATE_TRACE("[IAUPDATE] IAUpdateEngine::StartupCompleteL() end");    
       
   364     }
       
   365     
       
   366  
       
   367 // -----------------------------------------------------------------------------
       
   368 // IAUpdateEngine::HandleLeaveErrorL
       
   369 // 
       
   370 // -----------------------------------------------------------------------------
       
   371 //        
       
   372 void IAUpdateEngine::HandleLeaveErrorL( TInt aError )
       
   373     {
       
   374     IAUPDATE_TRACE("[IAUPDATE] IAUpdateEngine::HandleLeaveErrorL() begin");
       
   375     //removal of iGlobalLockHandler releases possible global operation lock to other IAD instances
       
   376     delete iGlobalLockHandler;
       
   377     iGlobalLockHandler = NULL;
       
   378     // client request is completed before leave in case of leave error
       
   379     if ( aError != KErrNone ) 
       
   380         {   
       
   381         InformRequestObserver( aError );
       
   382         User::Leave( aError );
       
   383         }
       
   384     
       
   385     IAUPDATE_TRACE("[IAUPDATE] IAUpdateEngine::HandleLeaveErrorL() end");
       
   386     }
       
   387 
       
   388 
       
   389 // -----------------------------------------------------------------------------
       
   390 // IAUpdateEngine::HandleLeaveErrorWithoutLeave
       
   391 // 
       
   392 // -----------------------------------------------------------------------------
       
   393 //  
       
   394 void IAUpdateEngine::HandleLeaveErrorWithoutLeave( TInt aError )
       
   395     {
       
   396     IAUPDATE_TRACE("[IAUPDATE] IAUpdateEngine::HandleLeaveErrorWithoutLeave() begin");
       
   397     //removal of iGlobalLockHandler releases possible global operation lock to other IAD instances
       
   398     delete iGlobalLockHandler;
       
   399     iGlobalLockHandler = NULL;
       
   400     if ( aError != KErrNone ) 
       
   401         {
       
   402         InformRequestObserver( aError );
       
   403         }
       
   404     if ( aError == KErrDiskFull )
       
   405         {
       
   406         //TRAP_IGNORE( ShowGlobalErrorNoteL( aError ) );
       
   407         }
       
   408     IAUPDATE_TRACE("[IAUPDATE] IAUpdateEngine::HandleLeaveErrorWithoutLeave end");
       
   409     }
       
   410 
       
   411 
       
   412 // -----------------------------------------------------------------------------
       
   413 // IAUpdateEngine::RefreshCompleteL
       
   414 // 
       
   415 // -----------------------------------------------------------------------------
       
   416 //      
       
   417 void IAUpdateEngine::RefreshCompleteL( TBool /*aWithViewActivation*/, TInt aError )
       
   418     {
       
   419     IAUPDATE_TRACE("[IAUPDATE] IAUpdateEngine::RefreshCompleteL() begin");
       
   420     IAUPDATE_TRACE_1("[IAUPDATE] error code: %d", aError );
       
   421     //removal of iGlobalLockHandler releases possible global operation lock to other IAD instances
       
   422     delete iGlobalLockHandler;
       
   423     iGlobalLockHandler = NULL;   
       
   424    
       
   425     if ( iRequestType == IAUpdateUiDefines::ECheckUpdates )
       
   426         {
       
   427         InformRequestObserver( aError );
       
   428         }
       
   429     else 
       
   430         {    
       
   431         emit refresh( iController->Nodes(), iController->FwNodes(), aError );   
       
   432         //if ( aWithViewActivation)
       
   433           //  {
       
   434           //  ActivateLocalViewL( TUid::Uid( EIAUpdateMainViewId ) );
       
   435           //  }
       
   436  
       
   437         // By calling CIdle possible waiting dialog can be closed before
       
   438         // automatic check where a new dialog may be launched
       
   439         //delete iIdleAutCheck;
       
   440         //iIdleAutCheck = NULL;
       
   441         //iIdleAutCheck = CIdle::NewL( CActive::EPriorityIdle ); 
       
   442         //iIdleAutCheck->Start( TCallBack( AutomaticCheckCallbackL, this ) );*/
       
   443         } 
       
   444  
       
   445   
       
   446     IAUPDATE_TRACE("[IAUPDATE] IAUpdateEngine::RefreshCompleteL() end");        
       
   447     }
       
   448 
       
   449 // -----------------------------------------------------------------------------
       
   450 // IAUpdateEngine::UpdateCompleteL
       
   451 // 
       
   452 // -----------------------------------------------------------------------------
       
   453 //     
       
   454 void IAUpdateEngine::UpdateCompleteL( TInt aError )    
       
   455     {
       
   456     IAUPDATE_TRACE("[IAUPDATE] IAUpdateEngine::UpdateCompleteL begin");
       
   457     IAUPDATE_TRACE_1("[IAUPDATE] error code: %d", aError );
       
   458     //removal of iGlobalLockHandler releases possible global operation lock to other IAD instances
       
   459     delete iGlobalLockHandler;
       
   460     iGlobalLockHandler = NULL;  
       
   461     if ( iRequestType != IAUpdateUiDefines::ENoRequest )
       
   462         {
       
   463         InformRequestObserver( aError );
       
   464         }
       
   465     
       
   466     //RefreshL( KErrNone ); 
       
   467       
       
   468     //ShowStatusDialogDeferredL();
       
   469                 
       
   470     IAUPDATE_TRACE("[IAUPDATE] IAUpdateEngine::UpdateCompleteL end");
       
   471     }
       
   472 
       
   473 
       
   474 
       
   475 // -----------------------------------------------------------------------------
       
   476 // IAUpdateEngine::InformRequestObserver
       
   477 // 
       
   478 // -----------------------------------------------------------------------------
       
   479 //      
       
   480 void IAUpdateEngine::InformRequestObserver( TInt aError )
       
   481     {
       
   482     IAUPDATE_TRACE("[IAUPDATE] IAUpdateEngine::InformRequestObserver() begin");
       
   483         
       
   484     //if ( iRequestObserver )
       
   485     if ( iRequestIssued )    
       
   486         {
       
   487         if ( iController->ClosingAllowedByClient() )
       
   488             {
       
   489             if ( iRequestType != IAUpdateUiDefines::ENoRequest )
       
   490                 {
       
   491 //                if ( iRequestType == IAUpdateUiDefines::EUpdateQuery && iUpdateNow )
       
   492 //                    {
       
   493 //                  if ( !iBackgroundTimer )
       
   494 //                        {
       
   495 //                        iBackgroundTimer = CIAUpdateUITimer::NewL( *this, CIAUpdateUITimer::EBackgroundDelay );
       
   496 //                        }
       
   497 //                 if ( !iBackgroundTimer->IsActive() )
       
   498 //                        {
       
   499 //                        iBackgroundTimer->After( 500000 );
       
   500 //                        }
       
   501 //                    }
       
   502 //                 else
       
   503 //                    {
       
   504                       iEikEnv->RootWin().SetOrdinalPosition( -1, ECoeWinPriorityNeverAtFront );
       
   505 //                    }
       
   506                 }
       
   507             }
       
   508         
       
   509         switch ( iRequestType )
       
   510             {
       
   511             case IAUpdateUiDefines::ENoRequest:
       
   512                 {
       
   513                 mServiceProvider->completeLauncherLaunch( aError );
       
   514                 break;
       
   515                 }
       
   516             case IAUpdateUiDefines::ECheckUpdates:
       
   517                 {
       
   518                 mServiceProvider->completeCheckUpdates( iController->CountOfAvailableUpdates(), aError );
       
   519                 break;
       
   520                 }
       
   521             case IAUpdateUiDefines::EShowUpdates:
       
   522                 {
       
   523                 CIAUpdateResult* result( NULL );
       
   524                 TRAPD( error, result = CIAUpdateResult::NewL() )
       
   525                 if ( result )
       
   526                     {
       
   527                     TIAUpdateResultsInfo resultsInfo( iController->ResultsInfo() );
       
   528                     IAUPDATE_TRACE_3("[IAUPDATE] IAUpdateEngine::InformRequestObserver succeed: %d failed: %d  cancelled: %d", 
       
   529                                                   resultsInfo.iCountSuccessfull, 
       
   530                                                   resultsInfo.iCountFailed, 
       
   531                                                   resultsInfo.iCountCancelled );
       
   532                     result->SetSuccessCount( resultsInfo.iCountSuccessfull );
       
   533                     result->SetFailCount( resultsInfo.iCountFailed );
       
   534                     result->SetCancelCount( resultsInfo.iCountCancelled );    
       
   535                     mServiceProvider->completeShowUpdates( result, aError );
       
   536                     // Ownership of result is transferred here.
       
   537                     }
       
   538                 else
       
   539                     {
       
   540                     mServiceProvider->completeShowUpdates( NULL, error );
       
   541                     }
       
   542                 break;
       
   543                 }
       
   544             case IAUpdateUiDefines::EUpdateQuery:
       
   545                 {
       
   546                 mServiceProvider->completeUpdateQuery( iUpdateNow, aError );
       
   547                 break;
       
   548                 }
       
   549             default:
       
   550                 {
       
   551                 break;
       
   552                 }
       
   553             }
       
   554 
       
   555         //iRequestObserver = NULL; 
       
   556         iRequestIssued = EFalse;
       
   557         }
       
   558         
       
   559     IAUPDATE_TRACE("[IAUPDATE] IAUpdateEngine::InformRequestObserver() end");
       
   560     }
       
   561 
       
   562 
       
   563 // ---------------------------------------------------------------------------
       
   564 // IAUpdateEngine::SetDefaultConnectionMethodL
       
   565 // Sets the connection method for the update network connection.
       
   566 // ---------------------------------------------------------------------------
       
   567 //
       
   568 void IAUpdateEngine::SetDefaultConnectionMethodL( TBool aTotalSilent )
       
   569     {
       
   570     if ( aTotalSilent )
       
   571         {
       
   572         // from back ground checker, choose the IAP to make the internet access silent
       
   573         IAUPDATE_TRACE("[IAUPDATE] IAUpdateEngine::SetDefaultConnectionMethodL() begin");
       
   574 
       
   575            TUint32 connectionMethodId( 0 );
       
   576            TInt connMethodId( 0 );
       
   577 
       
   578            // Let's first check whether cenrep contains SNAP id other than zero
       
   579            CRepository* cenrep( CRepository::NewLC( KCRUidIAUpdateSettings ) );
       
   580            User::LeaveIfError(  
       
   581                cenrep->Get( KIAUpdateAccessPoint, connMethodId ) );
       
   582            CleanupStack::PopAndDestroy( cenrep ); 
       
   583            cenrep = NULL;
       
   584 
       
   585            RCmManagerExt cmManagerExt;
       
   586            cmManagerExt.OpenL();
       
   587            CleanupClosePushL( cmManagerExt );
       
   588            
       
   589            if ( connMethodId == -1 )
       
   590                {
       
   591                IAUPDATE_TRACE("[IAUPDATE] user chooses default connection, use IAP logic");
       
   592                
       
   593                //check what is the default connection by users     
       
   594                
       
   595                TCmDefConnValue DCSetting;
       
   596                cmManagerExt.ReadDefConnL( DCSetting );
       
   597               
       
   598                
       
   599                switch ( DCSetting.iType )
       
   600                    {
       
   601                    case ECmDefConnAlwaysAsk:
       
   602                    case ECmDefConnAskOnce:
       
   603                        {
       
   604                        //go with the best IAP under internet snap
       
   605                        connectionMethodId = GetBestIAPInAllSNAPsL( cmManagerExt );
       
   606                        break;
       
   607                        }
       
   608                    case ECmDefConnDestination:
       
   609                        {
       
   610                        //go with the best IAP under this snap
       
   611                        connectionMethodId = GetBestIAPInThisSNAPL( cmManagerExt, DCSetting.iId );
       
   612                        break;
       
   613                        }
       
   614                    case ECmDefConnConnectionMethod:
       
   615                        {
       
   616                        //go with the best IAP under this snap
       
   617                        connectionMethodId = DCSetting.iId;
       
   618                        break;
       
   619                        }
       
   620                    }
       
   621                }
       
   622            else if ( connMethodId == 0 )
       
   623                {
       
   624                //no choice from user, we go with the best IAP under Internent SNAP
       
   625                connectionMethodId = GetBestIAPInAllSNAPsL( cmManagerExt );
       
   626                }
       
   627            else
       
   628                {
       
   629                IAUPDATE_TRACE("[IAUPDATE] use chooses a snap");
       
   630                // It was some SNAP value
       
   631                connectionMethodId = GetBestIAPInThisSNAPL( cmManagerExt, connMethodId );
       
   632                }
       
   633 
       
   634            CleanupStack::PopAndDestroy( &cmManagerExt ); 
       
   635            
       
   636            if ( connectionMethodId != 0 )
       
   637                {
       
   638                TIAUpdateConnectionMethod connectionMethod( 
       
   639                    connectionMethodId, 
       
   640                    TIAUpdateConnectionMethod::EConnectionMethodTypeAccessPoint );
       
   641 
       
   642                iController->SetDefaultConnectionMethodL( connectionMethod );
       
   643                }
       
   644            else
       
   645                {
       
   646                //In the totally silent case, if no usable IAP, we complete the check update with 0 updates.
       
   647                //the bgchecker will try again later after 1 month. 
       
   648                //The LEAVE will be catched up later and complete the request from background checker.
       
   649                User::LeaveIfError( KErrNotFound );
       
   650                
       
   651                //the following code will pop up dialog to ask from user, just for proto
       
   652               /* connectionMethodId = 0;               
       
   653                TIAUpdateConnectionMethod connectionMethod( 
       
   654                    connectionMethodId, TIAUpdateConnectionMethod::EConnectionMethodTypeDefault );
       
   655 
       
   656                iController->SetDefaultConnectionMethodL( connectionMethod );*/
       
   657                }
       
   658            
       
   659 
       
   660 
       
   661            IAUPDATE_TRACE("[IAUPDATE] IAUpdateEngine::SetDefaultConnectionMethodL() end");
       
   662         }
       
   663     else
       
   664         {
       
   665         // from grid, use the old logic
       
   666         IAUPDATE_TRACE("[IAUPDATE] IAUpdateEngine::SetDefaultConnectionMethodL() begin");
       
   667                   TUint32 connectionMethodId( 0 );
       
   668                   TInt connMethodId( 0 );
       
   669 
       
   670                   // Set initial value to always ask
       
   671                   TInt connectionMethodType( TIAUpdateConnectionMethod::EConnectionMethodTypeAlwaysAsk );
       
   672                   TBool needSaving(EFalse);
       
   673 
       
   674                   // Let's first check whether cenrep contains SNAP id other than zero
       
   675                   CRepository* cenrep( CRepository::NewLC( KCRUidIAUpdateSettings ) );
       
   676                   User::LeaveIfError(  
       
   677                       cenrep->Get( KIAUpdateAccessPoint, connMethodId ) );
       
   678                   CleanupStack::PopAndDestroy( cenrep ); 
       
   679                   cenrep = NULL;
       
   680                     
       
   681                   if ( connMethodId == -1 )
       
   682                       {
       
   683                       IAUPDATE_TRACE("[IAUPDATE] user chooses default connection, use IAP logic");
       
   684                                             
       
   685                       connectionMethodId = 0;
       
   686                       connectionMethodType = TIAUpdateConnectionMethod::EConnectionMethodTypeDefault;
       
   687                       }
       
   688                   else if ( connMethodId == 0 )
       
   689                       {
       
   690                       
       
   691                       IAUPDATE_TRACE("[IAUPDATE] use chooses nothing, use internal IAP logic");
       
   692                       //if nothing is set by user, use our new logic
       
   693                       //SetDefaultConnectionMethod2L();
       
   694                       //return;
       
   695                       // CenRep didn't contain any SNAP id. Let's try Internet SNAP then.
       
   696                           
       
   697                       RCmManagerExt cmManagerExt;
       
   698                       cmManagerExt.OpenL();
       
   699                       CleanupClosePushL( cmManagerExt );
       
   700                       iDestIdArray.Reset();
       
   701                       cmManagerExt.AllDestinationsL( iDestIdArray );
       
   702 
       
   703                       for ( TInt i = 0; i< iDestIdArray.Count(); i++ )
       
   704                           {
       
   705                           RCmDestinationExt dest = cmManagerExt.DestinationL( iDestIdArray[i] );
       
   706                           CleanupClosePushL( dest );
       
   707                            
       
   708                           if ( dest.MetadataL( CMManager::ESnapMetadataInternet ) )
       
   709                               {
       
   710                               // Check whether Internet SNAP contains any IAP.
       
   711                               if ( dest.ConnectionMethodCount() > 0 )
       
   712                                   {
       
   713                                   connectionMethodId = iDestIdArray[i];
       
   714                                   needSaving = ETrue;
       
   715                                   IAUPDATE_TRACE_1("[IAUPDATE] connectionMethodId: %d", connectionMethodId );
       
   716                                   }
       
   717                               CleanupStack::PopAndDestroy( &dest ); 
       
   718                               break;
       
   719                               }
       
   720                                
       
   721                           CleanupStack::PopAndDestroy( &dest ); 
       
   722                           }
       
   723                       iDestIdArray.Reset();
       
   724                       CleanupStack::PopAndDestroy( &cmManagerExt ); 
       
   725                       }
       
   726                   else
       
   727                       {
       
   728                       IAUPDATE_TRACE("[IAUPDATE] use chooses a snap");
       
   729                       // It was some SNAP value
       
   730                       connectionMethodId = connMethodId;
       
   731                       }
       
   732                   
       
   733                   if ( connectionMethodId > 0)
       
   734                       {
       
   735                       // We have now some valid SNAP id, either from CenRep or Internet SNAP
       
   736                       connectionMethodType = TIAUpdateConnectionMethod::EConnectionMethodTypeDestination;
       
   737                       // Save to cenrep if needed
       
   738                       if ( needSaving )
       
   739                           {
       
   740                           cenrep = CRepository::NewLC( KCRUidIAUpdateSettings );
       
   741                           TInt err = cenrep->StartTransaction( CRepository::EReadWriteTransaction );
       
   742                           User::LeaveIfError( err );
       
   743                           cenrep->CleanupCancelTransactionPushL();
       
   744                           
       
   745                           connMethodId = connectionMethodId;
       
   746                           err = cenrep->Set( KIAUpdateAccessPoint, connMethodId );
       
   747                           User::LeaveIfError( err );
       
   748                           TUint32 ignore = KErrNone;
       
   749                           User::LeaveIfError( cenrep->CommitTransaction( ignore ) );
       
   750                           CleanupStack::PopAndDestroy(); // CleanupCancelTransactionPushL()
       
   751                           CleanupStack::PopAndDestroy( cenrep );            
       
   752                           }
       
   753                       }
       
   754 
       
   755                   TIAUpdateConnectionMethod connectionMethod( 
       
   756                       connectionMethodId, 
       
   757                       static_cast< TIAUpdateConnectionMethod::TConnectionMethodType >( connectionMethodType ) );
       
   758 
       
   759                   iController->SetDefaultConnectionMethodL( connectionMethod );
       
   760 
       
   761                   IAUPDATE_TRACE("[IAUPDATE] IAUpdateEngine::SetDefaultConnectionMethodL() end");
       
   762         }
       
   763 
       
   764     }
       
   765 
       
   766 
       
   767 // ---------------------------------------------------------------------------
       
   768 // IAUpdateEngine::GetBestIAPInAllSNAPsL
       
   769 // Sets the best IAP from all snaps
       
   770 // ---------------------------------------------------------------------------
       
   771 //
       
   772 TUint32 IAUpdateEngine::GetBestIAPInAllSNAPsL( RCmManagerExt& aCmManagerExt  )
       
   773     { 
       
   774     //go with internet SNAP first.
       
   775     TUint32 IAPID = 0;
       
   776     IAPID = GetBestIAPInInternetSNAPL( aCmManagerExt );
       
   777     
       
   778     if ( IAPID )
       
   779         {
       
   780         return IAPID;
       
   781         }
       
   782     
       
   783     //select IAP from rest of the SNAPs
       
   784     iDestIdArray.Reset();
       
   785     aCmManagerExt.AllDestinationsL( iDestIdArray );
       
   786     
       
   787     for ( TInt i = 0; i< iDestIdArray.Count(); i++ )
       
   788         {
       
   789         TUint32 SNAPID = iDestIdArray[i];                   
       
   790         IAPID = GetBestIAPInThisSNAPL( aCmManagerExt, SNAPID );
       
   791         if ( IAPID )
       
   792             {
       
   793             break;
       
   794             }
       
   795          }
       
   796     iDestIdArray.Reset();
       
   797     return IAPID;
       
   798     }
       
   799 
       
   800 
       
   801 
       
   802 // ---------------------------------------------------------------------------
       
   803 // IAUpdateEngine::GetBestIAPInInternetSNAPL
       
   804 // Sets the best IAP from internet snap
       
   805 // ---------------------------------------------------------------------------
       
   806 //
       
   807 TUint32 IAUpdateEngine::GetBestIAPInInternetSNAPL( RCmManagerExt& aCmManagerExt  )
       
   808     {
       
   809     //select IAP from Internet SNAP
       
   810     iDestIdArray.Reset();
       
   811     aCmManagerExt.AllDestinationsL( iDestIdArray );
       
   812     TUint32 InternetSNAPID = 0;
       
   813     for ( TInt i = 0; i< iDestIdArray.Count(); i++ )
       
   814         {
       
   815         RCmDestinationExt dest = aCmManagerExt.DestinationL( iDestIdArray[i] );
       
   816         CleanupClosePushL( dest );
       
   817                                      
       
   818         if ( dest.MetadataL( CMManager::ESnapMetadataInternet ) )
       
   819             {
       
   820             InternetSNAPID = iDestIdArray[i];
       
   821             CleanupStack::PopAndDestroy( &dest ); 
       
   822             break;
       
   823             }                     
       
   824          CleanupStack::PopAndDestroy( &dest ); 
       
   825          }
       
   826     iDestIdArray.Reset();
       
   827     
       
   828     return GetBestIAPInThisSNAPL( aCmManagerExt, InternetSNAPID );
       
   829     }
       
   830 
       
   831 
       
   832 
       
   833 // ---------------------------------------------------------------------------
       
   834 // IAUpdateEngine::GetBestIAPInThisSNAPL
       
   835 // Sets the best IAP from the given snap
       
   836 // ---------------------------------------------------------------------------
       
   837 //
       
   838 TUint32 IAUpdateEngine::GetBestIAPInThisSNAPL( RCmManagerExt& aCmManagerExt, TUint32 aSNAPID  )
       
   839     {
       
   840     //get all usable IAPs
       
   841     TConnMonIapInfoBuf iapInfo;
       
   842     TRequestStatus status;
       
   843                        
       
   844     RConnectionMonitor connMon;
       
   845     connMon.ConnectL();
       
   846     CleanupClosePushL( connMon );
       
   847     
       
   848     connMon.GetPckgAttribute( EBearerIdAll, 0, KIapAvailability,iapInfo, status );
       
   849     User::WaitForRequest( status );
       
   850     User::LeaveIfError( status.Int() );
       
   851     
       
   852     CleanupStack::PopAndDestroy( &connMon ); 
       
   853     
       
   854     RCmDestinationExt dest = aCmManagerExt.DestinationL( aSNAPID );
       
   855     CleanupClosePushL( dest );
       
   856     
       
   857     // Check whether the SNAP contains any IAP.
       
   858     for  (TInt i = 0; i < dest.ConnectionMethodCount(); i++ )
       
   859         {
       
   860         RCmConnectionMethodExt cm =  dest.ConnectionMethodL( i );
       
   861         CleanupClosePushL( cm );
       
   862         
       
   863         TUint32 iapid= cm.GetIntAttributeL( CMManager::ECmIapId );
       
   864         
       
   865         for ( TInt i = 0; i < iapInfo().iCount; i++ )
       
   866             {
       
   867             if ( iapInfo().iIap[i].iIapId == iapid )
       
   868                 {
       
   869                 CleanupStack::PopAndDestroy( 2 ); //cm & dest;
       
   870                 return iapid;
       
   871                 }
       
   872             }    
       
   873                                                                                  
       
   874         CleanupStack::PopAndDestroy( &cm );
       
   875         }
       
   876     
       
   877     CleanupStack::PopAndDestroy( &dest ); 
       
   878     return 0;
       
   879     }
       
   880 
       
   881 // -----------------------------------------------------------------------------
       
   882 // IAUpdateEngine::ShowUpdateQueryL
       
   883 // 
       
   884 // -----------------------------------------------------------------------------
       
   885 //    
       
   886 void IAUpdateEngine::ShowUpdateQueryL()
       
   887     {
       
   888     IAUPDATE_TRACE("[IAUPDATE] IAUpdateEngine::ShowUpdateQueryL() begin");
       
   889     CIAUpdateQueryHistory* updateQueryHistory = CIAUpdateQueryHistory::NewL();
       
   890     // Get the delay information from the controller that has read it from
       
   891     // the config file.
       
   892     updateQueryHistory->SetDelay( iController->ConfigData().QueryHistoryDelayHours() );
       
   893     CleanupStack::PushL( updateQueryHistory );
       
   894     if ( !updateQueryHistory->IsDelayedL( iUpdatequeryUid ) )
       
   895         {
       
   896         if ( ClientInBackgroundL() )
       
   897             {
       
   898             iEikEnv->RootWin().SetOrdinalPosition( -1, ECoeWinPriorityNormal );
       
   899             }
       
   900         else
       
   901             {
       
   902             iEikEnv->RootWin().SetOrdinalPosition( 0, ECoeWinPriorityNormal );    
       
   903             }
       
   904  
       
   905         HbMessageBox messageBox(HbMessageBox::MessageTypeQuestion); 
       
   906         messageBox.setText(QString("Application update is available from Nokia. Update?"));
       
   907         HbAction nowAction("Now");
       
   908         HbAction laterAction("Later");
       
   909         messageBox.setPrimaryAction(&nowAction);
       
   910         messageBox.setSecondaryAction(&laterAction);
       
   911         messageBox.setTimeout(HbPopup::NoTimeout);
       
   912         messageBox.show();
       
   913         HbAction *selectedAction = messageBox.exec();
       
   914         
       
   915         if (selectedAction == messageBox.primaryAction())
       
   916             {
       
   917             IAUPDATE_TRACE("[IAUPDATE] IAUpdateEngine::ShowUpdateQueryL() Now");
       
   918             iUpdateNow = ETrue;
       
   919             }
       
   920         else if (selectedAction == messageBox.secondaryAction())
       
   921             {
       
   922             IAUPDATE_TRACE("[IAUPDATE] IAUpdateEngine::ShowUpdateQueryL() Later");
       
   923             updateQueryHistory->SetTimeL( iUpdatequeryUid );
       
   924             }
       
   925         }
       
   926     CleanupStack::PopAndDestroy( updateQueryHistory );
       
   927     InformRequestObserver( KErrNone );  
       
   928     IAUPDATE_TRACE("[IAUPDATE] IAUpdateEngine::ShowUpdateQueryL() end");
       
   929     }
       
   930 
       
   931 
       
   932 
       
   933 // -----------------------------------------------------------------------------
       
   934 // IAUpdateEngine::HideApplicationInFSWL
       
   935 // 
       
   936 // -----------------------------------------------------------------------------
       
   937 //  
       
   938 void IAUpdateEngine::HideApplicationInFSWL( TBool aHide ) const
       
   939     {
       
   940     IAUPDATE_TRACE("[IAUPDATE] IAUpdateEngine::HideApplicationInFSW() begin");
       
   941     IAUPDATE_TRACE_1("[IAUPDATE] hide: %d", aHide );
       
   942     TInt id = iEikEnv->RootWin().Identifier();
       
   943 
       
   944     CApaWindowGroupName* wgName = CApaWindowGroupName::NewLC( 
       
   945             iEikEnv->WsSession(), id );
       
   946     
       
   947     wgName->SetHidden( aHide );
       
   948     wgName->SetWindowGroupName( iEikEnv->RootWin() );    
       
   949     CleanupStack::PopAndDestroy( wgName ); 
       
   950     IAUPDATE_TRACE("[IAUPDATE] IAUpdateEngine::HideApplicationInFSW() end");
       
   951     }
       
   952 
       
   953 // ---------------------------------------------------------------------------
       
   954 // IAUpdateEngine::UpdateQueryCallbackL
       
   955 // ---------------------------------------------------------------------------
       
   956 //
       
   957 TInt IAUpdateEngine::UpdateQueryCallbackL( TAny* aPtr )
       
   958     {
       
   959     IAUPDATE_TRACE("[IAUPDATE] IAUpdateEngine::UpdateQueryCallbackL() begin");
       
   960     IAUpdateEngine* engine = static_cast<IAUpdateEngine*>( aPtr ); 
       
   961     //TRAPD( err, engine->ShowUpdateQueryL() );
       
   962     TRAP_IGNORE( engine->ShowUpdateQueryL() );
       
   963     //if ( err != KErrNone )
       
   964     //    {
       
   965     //    appUI->HandleLeaveErrorL( err );
       
   966     //    }
       
   967     IAUPDATE_TRACE("[IAUPDATE] IAUpdateEngine::UpdateQueryCallbackL() end");
       
   968     return KErrNone;
       
   969     }    
       
   970 
       
   971 
       
   972 
       
   973 
       
   974