phonebookui/Phonebook/View/src/CPbkWaitNoteWrapperBase.cpp
changeset 0 e686773b3f54
equal deleted inserted replaced
-1:000000000000 0:e686773b3f54
       
     1 /*
       
     2 * Copyright (c) 2002 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: 
       
    15 *       Provides methods for a wait note wrapper.
       
    16 *
       
    17 */
       
    18 
       
    19 
       
    20 // INCLUDE FILES
       
    21 #include <CPbkWaitNoteWrapperBase.h>
       
    22 #include <eikprogi.h>
       
    23 #include <AknWaitDialog.h>
       
    24 #include <MPbkBackgroundProcess.h>
       
    25 #include <PbkDebug.h>
       
    26 
       
    27 #ifdef __WINS__
       
    28 // Disable warning from CPbkWaitNoteWrapperBase constructor
       
    29 #pragma warning( disable : 4355 )
       
    30 #endif
       
    31 
       
    32 namespace {
       
    33 
       
    34 #ifdef _DEBUG
       
    35 // LOCAL CONSTANTS
       
    36 enum TPanicCode
       
    37     {
       
    38     EPanicPreCond_ExecuteL_MPbkBackgroundProcess = 1
       
    39     };
       
    40 
       
    41 // ================= LOCAL FUNCTIONS =======================
       
    42 void Panic(TPanicCode aReason)
       
    43     {
       
    44     _LIT(KPanicText,"CPbkWaitNoteWrapperBase");
       
    45     User::Panic(KPanicText,aReason);
       
    46     }
       
    47 #endif // _DEBUG
       
    48 }
       
    49 
       
    50 
       
    51 // ================= MEMBER FUNCTIONS =======================
       
    52 
       
    53 // CPbkWaitNoteWrapperBase::CIdleCallback
       
    54 inline CPbkWaitNoteWrapperBase::CIdleCallback::CIdleCallback
       
    55         (CPbkWaitNoteWrapperBase& aWaitNoteWrapper) :
       
    56     CActive(EPriorityIdle),
       
    57     iWaitNoteWrapper(aWaitNoteWrapper)
       
    58     {
       
    59     CActiveScheduler::Add(this);
       
    60     }
       
    61 
       
    62 CPbkWaitNoteWrapperBase::CIdleCallback::~CIdleCallback()
       
    63     {
       
    64     Cancel();
       
    65     }
       
    66 
       
    67 void CPbkWaitNoteWrapperBase::CIdleCallback::IssueRequest()
       
    68     {
       
    69     TRequestStatus* sp = &iStatus;
       
    70     User::RequestComplete(sp, KErrNone);
       
    71     SetActive();
       
    72     }
       
    73 
       
    74 void CPbkWaitNoteWrapperBase::CIdleCallback::DoCancel()
       
    75     {
       
    76     }
       
    77 
       
    78 void CPbkWaitNoteWrapperBase::CIdleCallback::RunL()
       
    79     {
       
    80     iWaitNoteWrapper.IdleRunL();
       
    81     }
       
    82 
       
    83 TInt CPbkWaitNoteWrapperBase::CIdleCallback::RunError(TInt aError)
       
    84     {
       
    85     return iWaitNoteWrapper.IdleRunError(aError);
       
    86     }
       
    87 
       
    88 
       
    89 // CPbkWaitNoteWrapperBase::TNoteParams
       
    90 EXPORT_C CPbkWaitNoteWrapperBase::TNoteParams::TNoteParams() :
       
    91     iVisibilityDelayOff(EFalse),
       
    92     iTone(CAknNoteDialog::ENoTone),
       
    93     iText(NULL),
       
    94     iObserver(NULL)
       
    95     {
       
    96 	}
       
    97 
       
    98 // CPbkWaitNoteWrapperBase
       
    99 CPbkWaitNoteWrapperBase::CPbkWaitNoteWrapperBase() :
       
   100     iIdleCallback(*this), iNoteOpen(EFalse)
       
   101     {
       
   102     PBK_DEBUG_PRINT(PBK_DEBUG_STRING
       
   103 		("CPbkWaitNoteWrapperBase::CPbkWaitNoteWrapperBase(0x%x)"),this);
       
   104     }
       
   105 
       
   106 CPbkWaitNoteWrapperBase::~CPbkWaitNoteWrapperBase()
       
   107     {
       
   108     PBK_DEBUG_PRINT(PBK_DEBUG_STRING
       
   109 		("CPbkWaitNoteWrapperBase::~CPbkWaitNoteWrapperBase(0x%x)"),this);
       
   110 
       
   111 	// Wait dialog gets deleted automatically, but delete it by force
       
   112 	// if it somehow still exists
       
   113 	if (iWaitDialog)
       
   114 		{
       
   115 		delete iWaitDialog;
       
   116 		iWaitDialog=NULL;
       
   117 		}
       
   118     Cancel();
       
   119     }
       
   120 
       
   121 EXPORT_C void CPbkWaitNoteWrapperBase::DisplayL
       
   122         (TInt aResId, 
       
   123         const TNoteParams& aNoteParams /*=TNoteParams()*/)
       
   124     {
       
   125     PBK_DEBUG_PRINT
       
   126         (PBK_DEBUG_STRING("CPbkWaitNoteWrapperBase::DisplayL(0x%x,%d)"),
       
   127         this, aResId);
       
   128 
       
   129     Cancel();
       
   130     iObserver = aNoteParams.iObserver;
       
   131     iWaitDialog = CreateDialogL
       
   132         (reinterpret_cast<CEikDialog**>(&iWaitDialog),
       
   133 		aNoteParams.iVisibilityDelayOff);
       
   134     iWaitDialog->SetCallback(this);
       
   135     iWaitDialog->SetTone(aNoteParams.iTone);
       
   136     iWaitDialog->PrepareLC(aResId);
       
   137     if (aNoteParams.iText)
       
   138         {
       
   139         iWaitDialog->SetTextL(*aNoteParams.iText);
       
   140         }
       
   141 	iNoteOpen = ETrue;
       
   142     iWaitDialog->RunLD();
       
   143     }
       
   144 
       
   145 EXPORT_C void CPbkWaitNoteWrapperBase::Execute
       
   146         (MPbkBackgroundProcess& aProcess)
       
   147     {
       
   148     PBK_DEBUG_PRINT
       
   149         (PBK_DEBUG_STRING("CPbkWaitNoteWrapperBase::Execute(0x%x,0x%x)"),
       
   150         this, &aProcess);
       
   151     __ASSERT_DEBUG(iWaitDialog && !iBackgroundProcess,
       
   152         Panic(EPanicPreCond_ExecuteL_MPbkBackgroundProcess));
       
   153 
       
   154     iBackgroundProcess = &aProcess;
       
   155     // Request IdleRunL call
       
   156     iIdleCallback.IssueRequest();
       
   157     }
       
   158 
       
   159 EXPORT_C void CPbkWaitNoteWrapperBase::ExecuteL
       
   160         (MPbkBackgroundProcess& aProcess,
       
   161         TInt aResId, 
       
   162         const TNoteParams& aNoteParams /*=TNoteParams()*/)
       
   163     {
       
   164     DisplayL(aResId,aNoteParams);
       
   165     Execute(aProcess);
       
   166     }
       
   167 
       
   168 EXPORT_C TBool CPbkWaitNoteWrapperBase::IsNoteClosed() const
       
   169     {
       
   170     return (!iNoteOpen);
       
   171     }
       
   172 
       
   173 EXPORT_C void CPbkWaitNoteWrapperBase::SetProgressDialogCallback
       
   174         (MProgressDialogCallback& aCallback)
       
   175     {
       
   176     iDialogCallbackObserver = &aCallback;
       
   177     }
       
   178 
       
   179 void CPbkWaitNoteWrapperBase::Cancel()
       
   180     {
       
   181     iIdleCallback.Cancel();
       
   182     ProcessCanceled();
       
   183     }
       
   184 
       
   185 void CPbkWaitNoteWrapperBase::IdleRunL()
       
   186     {
       
   187     if (!iBackgroundProcess)
       
   188         {
       
   189         return;
       
   190         }
       
   191 
       
   192     if (ShowProgressInfo() && !iProgressInfoControl)
       
   193         {
       
   194 		if (iNoteOpen)
       
   195 			{
       
   196 			iProgressInfoControl = iWaitDialog->GetProgressInfoL();
       
   197 			}
       
   198         }
       
   199     if (iProgressInfoControl)
       
   200         {
       
   201         const TInt numberOfSteps = iBackgroundProcess->TotalNumberOfSteps();
       
   202         if (numberOfSteps > 0 && 
       
   203             iProgressInfoControl->Info().iFinalValue != numberOfSteps)
       
   204             {
       
   205             iProgressInfoControl->SetFinalValue(numberOfSteps);
       
   206             }
       
   207         }
       
   208 
       
   209     if (iBackgroundProcess->IsProcessDone())
       
   210         {
       
   211         ProcessFinished();
       
   212         }
       
   213     else if (!iNoteOpen)
       
   214         {
       
   215         ProcessCanceled();
       
   216         }
       
   217     else
       
   218         {
       
   219         // Do one step of processing
       
   220         iBackgroundProcess->StepL();
       
   221         // Update progress info
       
   222         if (iProgressInfoControl)
       
   223             {
       
   224             iProgressInfoControl->IncrementAndDraw(1);
       
   225             }
       
   226         // Request next cycle
       
   227         iIdleCallback.IssueRequest();
       
   228         }
       
   229     }
       
   230 
       
   231 TInt CPbkWaitNoteWrapperBase::IdleRunError
       
   232         (TInt aError)
       
   233     {
       
   234     PBK_DEBUG_PRINT(PBK_DEBUG_STRING
       
   235 		("CPbkWaitNoteWrapperBase::IdleRunError(0x%x,%d)"),this,aError);
       
   236 
       
   237     TInt translatedError = aError;
       
   238     if (iBackgroundProcess)
       
   239         {
       
   240         translatedError = iBackgroundProcess->HandleStepError(aError);
       
   241         }
       
   242 
       
   243     if (translatedError == KErrNone)
       
   244         {
       
   245         // Continue processing
       
   246         iIdleCallback.IssueRequest();
       
   247         }
       
   248     else
       
   249         {
       
   250         if (iBackgroundProcess)
       
   251             {
       
   252             MPbkBackgroundProcess& process = *iBackgroundProcess;
       
   253             iBackgroundProcess = NULL;
       
   254             KillNote();
       
   255             NotifyObserver(process);
       
   256             }
       
   257         }
       
   258  
       
   259     return translatedError;
       
   260     }
       
   261 
       
   262 void CPbkWaitNoteWrapperBase::DialogDismissedL
       
   263         (TInt aButtonId)
       
   264     {
       
   265     PBK_DEBUG_PRINT(PBK_DEBUG_STRING
       
   266 		("CPbkWaitNoteWrapperBase::DialogDismissedL(0x%x)"),this);
       
   267     
       
   268     if (iDialogCallbackObserver)
       
   269         {
       
   270         iDialogCallbackObserver->DialogDismissedL(aButtonId);
       
   271         }
       
   272 
       
   273 	iNoteOpen = EFalse;
       
   274     ProcessCanceled();
       
   275     }
       
   276 
       
   277 /**
       
   278  * Closes wait note gracefully.
       
   279  */
       
   280 void CPbkWaitNoteWrapperBase::CloseNote()
       
   281     {
       
   282     PBK_DEBUG_PRINT(PBK_DEBUG_STRING
       
   283 		("CPbkWaitNoteWrapperBase::CloseNote(0x%x)"),this);
       
   284 
       
   285     if (iWaitDialog)
       
   286         {
       
   287         iWaitDialog->SetCallback(NULL);
       
   288         TRAPD(err,iWaitDialog->ProcessFinishedL());
       
   289         if (err != KErrNone)
       
   290             {
       
   291             // Graceful close was unsuccesful -> stop
       
   292 			// being nice and kill the dialog
       
   293             KillNote();
       
   294             }
       
   295 		iNoteOpen = EFalse;
       
   296         }
       
   297 	}
       
   298 
       
   299 /**
       
   300  * Closes wait note immediately.
       
   301  */
       
   302 void CPbkWaitNoteWrapperBase::KillNote()
       
   303     {
       
   304     PBK_DEBUG_PRINT(PBK_DEBUG_STRING
       
   305 		("CPbkWaitNoteWrapperBase::KillNote(0x%x)"),this);
       
   306 
       
   307     if (iWaitDialog)
       
   308         {
       
   309         iWaitDialog->SetCallback(NULL);
       
   310 		delete iWaitDialog;
       
   311 		iWaitDialog=NULL;
       
   312         }
       
   313 
       
   314 	iNoteOpen = EFalse;
       
   315     }
       
   316 
       
   317 void CPbkWaitNoteWrapperBase::ProcessFinished()
       
   318     {
       
   319     PBK_DEBUG_PRINT(PBK_DEBUG_STRING
       
   320 		("CPbkWaitNoteWrapperBase::ProcessFinished(0x%x)"),this);
       
   321 
       
   322     if (iBackgroundProcess)
       
   323         {
       
   324         MPbkBackgroundProcess& process = *iBackgroundProcess;
       
   325         iBackgroundProcess = NULL;
       
   326         process.ProcessFinished();
       
   327         CloseNote();
       
   328         NotifyObserver(process);
       
   329 		}
       
   330     }
       
   331 
       
   332 void CPbkWaitNoteWrapperBase::ProcessCanceled()
       
   333     {
       
   334     PBK_DEBUG_PRINT(PBK_DEBUG_STRING
       
   335 		("CPbkWaitNoteWrapperBase::ProcessCanceled(0x%x)"),this);
       
   336 
       
   337     if (iBackgroundProcess)
       
   338         {
       
   339         MPbkBackgroundProcess& process = *iBackgroundProcess;
       
   340         iBackgroundProcess = NULL;
       
   341         process.ProcessCanceled();
       
   342 		// Do not kill the note, if it already has been done
       
   343 		if (iNoteOpen)
       
   344 			{
       
   345 			KillNote();
       
   346 			}
       
   347         NotifyObserver(process);
       
   348         }
       
   349     }
       
   350 
       
   351 /**
       
   352  * Notifies observer about process completion.
       
   353  */
       
   354 void CPbkWaitNoteWrapperBase::NotifyObserver(MPbkBackgroundProcess& aProcess)
       
   355     {
       
   356     if (iObserver)
       
   357         {
       
   358         MPbkProcessObserver& observer = *iObserver;
       
   359         iObserver = NULL;
       
   360         observer.ProcessFinished(aProcess);
       
   361         }
       
   362     }
       
   363 
       
   364 //  End of File