omaprovisioning/provisioning/ProvisioningBC/Src/CWPSaver.cpp
changeset 0 b497e44ab2fc
child 2 5594fba90824
equal deleted inserted replaced
-1:000000000000 0:b497e44ab2fc
       
     1 /*
       
     2 * Copyright (c) 2002-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:  Helper class for saving Provisioning settings. Provides a progress note.
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 // INCLUDE FILES
       
    20 #include <AknQueryDialog.h>
       
    21 #include <eikprogi.h>
       
    22 #include <ProvisioningBC.rsg>
       
    23 #include <commdb.h>
       
    24 #include <CWPEngine.h>
       
    25 #include <CWPAdapter.h>
       
    26 #include <ActiveFavouritesDbNotifier.h>
       
    27 #include "CWPSaver.h"
       
    28 
       
    29 // CONSTANTS
       
    30 const TInt KMaxWaitTime = 2000000;
       
    31 const TInt KRetryCount = 5;
       
    32 
       
    33 // CLASS DECLARATION
       
    34 
       
    35 // ========================== MEMBER FUNCTIONS ===============================
       
    36 
       
    37 // ----------------------------------------------------------------------------
       
    38 // C++ default constructor.
       
    39 // ----------------------------------------------------------------------------
       
    40 CWPSaver::CWPSaver( CWPEngine& aEngine, TBool aSetAsDefault )
       
    41                   : CActive( EPriorityStandard ),
       
    42                     iEngine( aEngine ), 
       
    43                     iSetAsDefault( aSetAsDefault ),
       
    44                     iCurrentItem( 0 ),
       
    45                     iResult( KErrNone ),
       
    46                     iNumAccessDenied(0)
       
    47     {
       
    48     }
       
    49 
       
    50 // ----------------------------------------------------------------------------
       
    51 // CWPSaver::PrepareLC
       
    52 // ----------------------------------------------------------------------------
       
    53 //
       
    54 void CWPSaver::PrepareLC()
       
    55     {
       
    56     // Assume ownership of this.
       
    57     CleanupStack::PushL( this );
       
    58 
       
    59     iApDbNotifier = CActiveApDb::NewL( EDatabaseTypeIAP );
       
    60     iApDbNotifier->AddObserverL( this );
       
    61     
       
    62     User::LeaveIfError( iSession.Connect() );
       
    63     User::LeaveIfError( iBookmarkDb.Open( iSession, KBrowserBookmarks ) );
       
    64     iFavouritesNotifier = 
       
    65                 new(ELeave) CActiveFavouritesDbNotifier( iBookmarkDb, *this );  
       
    66 
       
    67     iFavouritesNotifier->Start();
       
    68 
       
    69     iRetryTimer = CPeriodic::NewL( EPriorityStandard );
       
    70 
       
    71     // Set up the dialog and callback mechanism.
       
    72     iDialog = new(ELeave)CAknProgressDialog(
       
    73         reinterpret_cast<CEikDialog**>(&iDialog), EFalse );
       
    74     iDialog->SetCallback( this );
       
    75     iDialog->ExecuteLD(R_WAITNOTE_SAVE);
       
    76     CEikProgressInfo* progressInfo = iDialog->GetProgressInfoL();
       
    77     progressInfo->SetAndDraw(iCurrentItem);
       
    78     progressInfo->SetFinalValue(iEngine.ItemCount());
       
    79     }
       
    80 
       
    81 // ----------------------------------------------------------------------------
       
    82 // CWPSaver::ExecuteLD
       
    83 // ----------------------------------------------------------------------------
       
    84 //
       
    85 TInt CWPSaver::ExecuteLD( TInt& aNumSaved )
       
    86     {
       
    87     PrepareLC();
       
    88 
       
    89     // Add us to active scheduler and make sure RunL() gets called.
       
    90     CActiveScheduler::Add( this );
       
    91     CompleteRequest();
       
    92     iWait.Start();
       
    93 
       
    94     // Progress note has been finished/cancelled. Cache the result
       
    95     // and delete this.
       
    96     TInt result( iResult );
       
    97     aNumSaved = iCurrentItem;
       
    98     CleanupStack::PopAndDestroy(); // this
       
    99 
       
   100     return result;
       
   101     }
       
   102 
       
   103 // ----------------------------------------------------------------------------
       
   104 // Destructor
       
   105 // ----------------------------------------------------------------------------
       
   106 CWPSaver::~CWPSaver()
       
   107     {
       
   108     Cancel();
       
   109 
       
   110     delete iApDbNotifier;
       
   111 
       
   112     if( iFavouritesNotifier )
       
   113         {
       
   114         iFavouritesNotifier->Cancel();
       
   115         delete iFavouritesNotifier;
       
   116         }
       
   117 
       
   118     iBookmarkDb.Close();
       
   119     iSession.Close();
       
   120     delete iRetryTimer;
       
   121     }
       
   122 
       
   123 // ----------------------------------------------------------------------------
       
   124 // CWPSaver::DoCancel
       
   125 // ----------------------------------------------------------------------------
       
   126 //
       
   127 void CWPSaver::DoCancel()
       
   128     {
       
   129     }
       
   130 
       
   131 // ----------------------------------------------------------------------------
       
   132 // CWPSaver::RunL
       
   133 // ----------------------------------------------------------------------------
       
   134 //
       
   135 void CWPSaver::RunL()
       
   136     {
       
   137     // Choose whether to save or set as default
       
   138     TInt err( KErrNone );
       
   139     if( iSetAsDefault )
       
   140         {
       
   141         TRAP( err, 
       
   142             if( iEngine.CanSetAsDefault( iCurrentItem ) )
       
   143                 {
       
   144                 iEngine.SetAsDefaultL( iCurrentItem );
       
   145                 } );
       
   146         }
       
   147     else
       
   148         {
       
   149         TRAP( err, iEngine.SaveL( iCurrentItem ) );
       
   150         }
       
   151 
       
   152     // If CommsDB or BookmarkDB are locked, schedule a retry
       
   153     if( err == EWPCommsDBLocked || err == KErrLocked)
       
   154         {
       
   155         iWaitCommsDb = ETrue;
       
   156         DelayedCompleteRequestL();
       
   157         return;
       
   158         }
       
   159     else if( err == EWPBookmarksLocked )
       
   160         {
       
   161         iWaitFavourites = ETrue;
       
   162         DelayedCompleteRequestL();
       
   163         return;
       
   164         }
       
   165     else if( err == KErrNone || err == KErrAccessDenied)
       
   166         {
       
   167         if( err == 	KErrAccessDenied)
       
   168 	          {
       
   169 	          TInt 	aNumAccessDenied = GetNumAccessDenied();
       
   170 	          aNumAccessDenied++;
       
   171 	          SetNumAccessDenied(aNumAccessDenied);
       
   172 	          iEngine.SetAccessDenied(ETrue);
       
   173 	          }
       
   174     
       
   175         // Succesful save, so reset retry count
       
   176         iRetryCount = 0;
       
   177 
       
   178         // Normal progress
       
   179         if( iCurrentItem == iEngine.ItemCount()-1 )
       
   180             {
       
   181             iDialog->ProcessFinishedL();
       
   182             }
       
   183         else
       
   184             {
       
   185             CEikProgressInfo* progressInfo = iDialog->GetProgressInfoL();
       
   186             iCurrentItem++;
       
   187             progressInfo->SetAndDraw(iCurrentItem);
       
   188             CompleteRequest();
       
   189             }
       
   190         }
       
   191     else
       
   192         {    
       
   193         // For all other errors, pass them through.
       
   194         User::LeaveIfError( err );
       
   195         }
       
   196     }
       
   197 
       
   198 // ----------------------------------------------------------------------------
       
   199 // CWPSaver::RunError
       
   200 // ----------------------------------------------------------------------------
       
   201 //
       
   202 TInt CWPSaver::RunError( TInt aError )
       
   203     {
       
   204     // There was a leave in RunL(). Store the error and
       
   205     // stop the dialog.
       
   206     iResult = aError;
       
   207     iWait.AsyncStop();
       
   208     delete iDialog;
       
   209     iDialog = NULL;
       
   210 
       
   211     return KErrNone;
       
   212     }
       
   213 
       
   214 // ----------------------------------------------------------------------------
       
   215 // CWPSaver::DialogDismissedL
       
   216 // ----------------------------------------------------------------------------
       
   217 //
       
   218 void CWPSaver::DialogDismissedL( TInt aButtonId )
       
   219     {
       
   220     if( aButtonId < 0 )
       
   221         {
       
   222         iResult = KErrCancel;
       
   223         }
       
   224 
       
   225     iWait.AsyncStop();
       
   226     }
       
   227 
       
   228 // ----------------------------------------------------------------------------
       
   229 // CWPSaver::CompleteRequest
       
   230 // ----------------------------------------------------------------------------
       
   231 //
       
   232 void CWPSaver::CompleteRequest()
       
   233     {
       
   234     // Schedule an immediate complete. Make sure that there
       
   235     // is no timer alive first
       
   236     Cancel();
       
   237     iRetryTimer->Cancel();
       
   238 
       
   239     SetActive();
       
   240     TRequestStatus* sp = &iStatus;
       
   241     User::RequestComplete( sp, KErrNone );
       
   242     }
       
   243 
       
   244 // ----------------------------------------------------------------------------
       
   245 // CWPSaver::DelayedCompleteRequestL
       
   246 // ----------------------------------------------------------------------------
       
   247 //
       
   248 void CWPSaver::DelayedCompleteRequestL()
       
   249     {
       
   250     if( iRetryCount < KRetryCount )
       
   251         {
       
   252         // Schedule a delayed complete. Cancel first in case
       
   253         // an immediate request was scheduled.
       
   254         iRetryTimer->Cancel();
       
   255         iRetryTimer->Start( KMaxWaitTime, KMaxTInt32, TCallBack( Timeout, this ) );
       
   256         iRetryCount++;
       
   257         }
       
   258     else
       
   259         {
       
   260         User::Leave( KErrTimedOut );
       
   261         }
       
   262     }
       
   263 
       
   264 // ----------------------------------------------------------------------------
       
   265 // CWPSaver::Retry
       
   266 // ----------------------------------------------------------------------------
       
   267 //
       
   268 void CWPSaver::Retry()
       
   269     {
       
   270     // Immediate retry. Mark that we're not waiting
       
   271     // for an event and complete request.
       
   272     iWaitCommsDb = EFalse;
       
   273     iWaitFavourites = EFalse;
       
   274     CompleteRequest();
       
   275     }
       
   276 
       
   277 // ----------------------------------------------------------------------------
       
   278 // CWPSaver::Timeout
       
   279 // ----------------------------------------------------------------------------
       
   280 //
       
   281 TInt CWPSaver::Timeout(TAny* aSelf)
       
   282     {
       
   283     // There was a time-out. Retry saving even though we
       
   284     // didn't get a notify from database.
       
   285     CWPSaver* self = static_cast<CWPSaver*>( aSelf );
       
   286     self->Retry();
       
   287 
       
   288     return KErrNone;
       
   289     }
       
   290 
       
   291 // ----------------------------------------------------------------------------
       
   292 // CWPSaver::HandleApDbEventL
       
   293 // ----------------------------------------------------------------------------
       
   294 //
       
   295 void CWPSaver::HandleApDbEventL( TEvent aEvent )
       
   296     {
       
   297     // We received an event from CommsDB. Retry if we're
       
   298     // waiting for it.
       
   299     if( iWaitCommsDb && aEvent == EDbAvailable )
       
   300         {
       
   301         Retry();
       
   302         }
       
   303     }
       
   304 
       
   305 // ----------------------------------------------------------------------------
       
   306 // CWPSaver::HandleFavouritesDbEventL
       
   307 // ----------------------------------------------------------------------------
       
   308 //
       
   309 void CWPSaver::HandleFavouritesDbEventL( RDbNotifier::TEvent /*aEvent*/ )
       
   310     {
       
   311     // We received an event from BookmarkDB. Retry if we're
       
   312     // waiting for it.
       
   313     if( iWaitFavourites )
       
   314         {
       
   315         Retry();
       
   316         }
       
   317     }
       
   318 
       
   319 // ----------------------------------------------------------------------------
       
   320 // CWPSaver::GetNumAccessDenied
       
   321 // ----------------------------------------------------------------------------
       
   322 //
       
   323     
       
   324 TInt CWPSaver::GetNumAccessDenied()
       
   325     {
       
   326     return iNumAccessDenied;
       
   327     }	
       
   328 
       
   329 // ----------------------------------------------------------------------------
       
   330 // CWPSaver::SetNumAccessDenied
       
   331 // ----------------------------------------------------------------------------
       
   332 //
       
   333         
       
   334 void CWPSaver::SetNumAccessDenied(TInt aNumAccessDenied )
       
   335     {
       
   336     iNumAccessDenied = aNumAccessDenied;
       
   337     }	
       
   338 //  End of File