clientprovisioning/cpqtsp/src/CWPSaver.cpp
changeset 45 0f9fc722d255
child 42 aa33c2cb9a50
equal deleted inserted replaced
44:137912d1a556 45:0f9fc722d255
       
     1 /*
       
     2 * ============================================================================
       
     3 *  Name        : CWPSaver.cpp
       
     4 *  Part of     : Provisioning / ProvisioningBC
       
     5 *  Description : Helper class for saving Provisioning settings. Provides a progress note.
       
     6 *  Version     : %version: 1 % << Don't touch! Updated by Synergy at check-out.
       
     7 *
       
     8 *  Copyright © 2002-2006 Nokia.  All rights reserved.
       
     9 *  This material, including documentation and any related computer
       
    10 *  programs, is protected by copyright controlled by Nokia.  All
       
    11 *  rights are reserved.  Copying, including reproducing, storing,
       
    12 *  adapting or translating, any or all of this material requires the
       
    13 *  prior written consent of Nokia.  This material also contains
       
    14 *  confidential information which may not be disclosed to others
       
    15 *  without the prior written consent of Nokia.
       
    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 #include <HbProgressDialog>
       
    29 #include <HbAction>
       
    30 
       
    31 // CONSTANTS
       
    32 const TInt KMaxWaitTime = 2000000;
       
    33 const TInt KRetryCount = 5;
       
    34 
       
    35 // CLASS DECLARATION
       
    36 
       
    37 // ========================== MEMBER FUNCTIONS ===============================
       
    38 
       
    39 // ----------------------------------------------------------------------------
       
    40 // C++ default constructor.
       
    41 // ----------------------------------------------------------------------------
       
    42 CWPSaver::CWPSaver( CWPEngine& aEngine, TBool aSetAsDefault )
       
    43                   : CActive( EPriorityStandard ),
       
    44                     iEngine( aEngine ), 
       
    45                     iSetAsDefault( aSetAsDefault ),
       
    46                     iCurrentItem( 0 ),
       
    47                     iResult( KErrNone )
       
    48     {
       
    49     }
       
    50 
       
    51 // ----------------------------------------------------------------------------
       
    52 // CWPSaver::PrepareLC
       
    53 // ----------------------------------------------------------------------------
       
    54 //
       
    55 void CWPSaver::PrepareLC()
       
    56     {
       
    57     // Assume ownership of this.
       
    58     CleanupStack::PushL( this );
       
    59 
       
    60     iApDbNotifier = CActiveApDb::NewL( EDatabaseTypeIAP );
       
    61     iApDbNotifier->AddObserverL( this );
       
    62     
       
    63     User::LeaveIfError( iSession.Connect() );
       
    64     User::LeaveIfError( iBookmarkDb.Open( iSession, KBrowserBookmarks ) );
       
    65     iFavouritesNotifier = 
       
    66                 new(ELeave) CActiveFavouritesDbNotifier( iBookmarkDb, *this );  
       
    67 
       
    68     iFavouritesNotifier->Start();
       
    69 
       
    70     iRetryTimer = CPeriodic::NewL( EPriorityStandard );
       
    71     iProgress = new HbProgressDialog(HbProgressDialog::WaitDialog);
       
    72     iProgress->setText(hbTrId("txt_device_update_setlabel_saving_settings"));
       
    73     //iProgress->setTextAlignment(Qt::AlignCenter);
       
    74     iProgress->clearActions();   
       
    75     //iProgress->setPrimaryAction(new HbAction(""));
       
    76     //iProgress->setSecondaryAction(new HbAction(""));
       
    77     iProgress->show();
       
    78 
       
    79  }
       
    80 
       
    81 // ----------------------------------------------------------------------------
       
    82 // CWPSaver::ExecuteLD
       
    83 // ----------------------------------------------------------------------------
       
    84 //
       
    85 TInt CWPSaver::ExecuteLD( TInt& aNumSaved, TBufC<256>& Value )
       
    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     Value = iValue;
       
    99     CleanupStack::PopAndDestroy(); // this
       
   100 
       
   101     return result;
       
   102     }
       
   103 
       
   104 // ----------------------------------------------------------------------------
       
   105 // Destructor
       
   106 // ----------------------------------------------------------------------------
       
   107 CWPSaver::~CWPSaver()
       
   108     {
       
   109     Cancel();
       
   110 
       
   111     delete iApDbNotifier;
       
   112 
       
   113     if( iFavouritesNotifier )
       
   114         {
       
   115         iFavouritesNotifier->Cancel();
       
   116         delete iFavouritesNotifier;
       
   117         }
       
   118 
       
   119     iBookmarkDb.Close();
       
   120     iSession.Close();
       
   121     delete iRetryTimer;
       
   122     }
       
   123 
       
   124 // ----------------------------------------------------------------------------
       
   125 // CWPSaver::DoCancel
       
   126 // ----------------------------------------------------------------------------
       
   127 //
       
   128 void CWPSaver::DoCancel()
       
   129     {
       
   130     }
       
   131 
       
   132 // ----------------------------------------------------------------------------
       
   133 // CWPSaver::RunL
       
   134 // ----------------------------------------------------------------------------
       
   135 //
       
   136 void CWPSaver::RunL()
       
   137     {
       
   138     // Choose whether to save or set as default
       
   139     TInt err( KErrNone );
       
   140     if( iSetAsDefault )
       
   141         {
       
   142         TRAP( err, 
       
   143             if( iEngine.CanSetAsDefault( iCurrentItem ) )
       
   144                 {
       
   145                 iEngine.SetAsDefaultL( iCurrentItem );
       
   146                 } );
       
   147         }
       
   148     else
       
   149         {
       
   150         TRAP(err, QT_TRYCATCH_LEAVING( iEngine.SaveL( iCurrentItem ) )); 
       
   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 )
       
   166         {
       
   167         // For all other errors, pass them through.
       
   168         iValue = iEngine.SummaryText(iCurrentItem);
       
   169         delete iProgress;
       
   170         iProgress = NULL;
       
   171         User::LeaveIfError( err );
       
   172         }
       
   173     // Succesful save, so reset retry count
       
   174     iRetryCount = 0;
       
   175 
       
   176     // Normal progress
       
   177     if( iCurrentItem == iEngine.ItemCount()-1 )
       
   178         {
       
   179         //iProgress->cancel();
       
   180         //iProgress->close();
       
   181         delete iProgress;
       
   182         iProgress = NULL;
       
   183         iWait.AsyncStop();       
       
   184         }
       
   185     else
       
   186         {
       
   187         //CEikProgressInfo* progressInfo = iDialog->GetProgressInfoL();
       
   188         iCurrentItem++;
       
   189         //progressInfo->SetAndDraw(iCurrentItem);
       
   190         CompleteRequest();
       
   191         }
       
   192     }
       
   193 
       
   194 // ----------------------------------------------------------------------------
       
   195 // CWPSaver::RunError
       
   196 // ----------------------------------------------------------------------------
       
   197 //
       
   198 TInt CWPSaver::RunError( TInt aError )
       
   199     {
       
   200     // There was a leave in RunL(). Store the error and
       
   201     // stop the dialog.
       
   202     iResult = aError;
       
   203     iWait.AsyncStop();
       
   204     return KErrNone;
       
   205     }
       
   206 
       
   207 // ----------------------------------------------------------------------------
       
   208 // CWPSaver::DialogDismissedL
       
   209 // ----------------------------------------------------------------------------
       
   210 //
       
   211 void CWPSaver::DialogDismissedL( TInt aButtonId )
       
   212     {
       
   213     if( aButtonId < 0 )
       
   214         {
       
   215         iResult = KErrCancel;
       
   216         }
       
   217 
       
   218     iWait.AsyncStop();
       
   219     }
       
   220 
       
   221 // ----------------------------------------------------------------------------
       
   222 // CWPSaver::CompleteRequest
       
   223 // ----------------------------------------------------------------------------
       
   224 //
       
   225 void CWPSaver::CompleteRequest()
       
   226     {
       
   227     // Schedule an immediate complete. Make sure that there
       
   228     // is no timer alive first
       
   229     Cancel();
       
   230     iRetryTimer->Cancel();
       
   231 
       
   232     SetActive();
       
   233     TRequestStatus* sp = &iStatus;
       
   234     User::RequestComplete( sp, KErrNone );
       
   235     }
       
   236 
       
   237 // ----------------------------------------------------------------------------
       
   238 // CWPSaver::DelayedCompleteRequestL
       
   239 // ----------------------------------------------------------------------------
       
   240 //
       
   241 void CWPSaver::DelayedCompleteRequestL()
       
   242     {
       
   243     if( iRetryCount < KRetryCount )
       
   244         {
       
   245         // Schedule a delayed complete. Cancel first in case
       
   246         // an immediate request was scheduled.
       
   247         iRetryTimer->Cancel();
       
   248         iRetryTimer->Start( KMaxWaitTime, KMaxTInt32, TCallBack( Timeout, this ) );
       
   249         iRetryCount++;
       
   250         }
       
   251     else
       
   252         {
       
   253         User::Leave( KErrTimedOut );
       
   254         }
       
   255     }
       
   256 
       
   257 // ----------------------------------------------------------------------------
       
   258 // CWPSaver::Retry
       
   259 // ----------------------------------------------------------------------------
       
   260 //
       
   261 void CWPSaver::Retry()
       
   262     {
       
   263     // Immediate retry. Mark that we're not waiting
       
   264     // for an event and complete request.
       
   265     iWaitCommsDb = EFalse;
       
   266     iWaitFavourites = EFalse;
       
   267     CompleteRequest();
       
   268     }
       
   269 
       
   270 // ----------------------------------------------------------------------------
       
   271 // CWPSaver::Timeout
       
   272 // ----------------------------------------------------------------------------
       
   273 //
       
   274 TInt CWPSaver::Timeout(TAny* aSelf)
       
   275     {
       
   276     // There was a time-out. Retry saving even though we
       
   277     // didn't get a notify from database.
       
   278     CWPSaver* self = static_cast<CWPSaver*>( aSelf );
       
   279     self->Retry();
       
   280 
       
   281     return KErrNone;
       
   282     }
       
   283 
       
   284 // ----------------------------------------------------------------------------
       
   285 // CWPSaver::HandleApDbEventL
       
   286 // ----------------------------------------------------------------------------
       
   287 //
       
   288 void CWPSaver::HandleApDbEventL( TEvent aEvent )
       
   289     {
       
   290     // We received an event from CommsDB. Retry if we're
       
   291     // waiting for it.
       
   292     if( iWaitCommsDb && aEvent == EDbAvailable )
       
   293         {
       
   294         Retry();
       
   295         }
       
   296     }
       
   297 
       
   298 // ----------------------------------------------------------------------------
       
   299 // CWPSaver::HandleFavouritesDbEventL
       
   300 // ----------------------------------------------------------------------------
       
   301 //
       
   302 void CWPSaver::HandleFavouritesDbEventL( RDbNotifier::TEvent /*aEvent*/ )
       
   303     {
       
   304     // We received an event from BookmarkDB. Retry if we're
       
   305     // waiting for it.
       
   306     if( iWaitFavourites )
       
   307         {
       
   308         Retry();
       
   309         }
       
   310     }
       
   311 
       
   312 //  End of File