emailservices/emailserver/cmailhandlerplugin/src/cmailcpshandler.cpp
branchRCL_3
changeset 29 6b8f3b30d0ec
parent 26 67369d1b217f
child 52 efd4f1afd43e
--- a/emailservices/emailserver/cmailhandlerplugin/src/cmailcpshandler.cpp	Tue May 25 12:23:16 2010 +0300
+++ b/emailservices/emailserver/cmailhandlerplugin/src/cmailcpshandler.cpp	Wed Jun 09 09:22:57 2010 +0300
@@ -54,7 +54,8 @@
 CMailCpsHandler::CMailCpsHandler( MFSNotificationHandlerMgr& aOwner ): 
     CFSNotificationHandlerBase( aOwner ),
     iWaitingForNewMailbox( NULL ),
-    iWaitingForNewWidget()
+    iWaitingForNewWidget(),
+    iUpdateHelper( NULL )
     {
     FUNC_LOG;
     iWaitingForNewWidget.SetPluginId( KNullUid );
@@ -108,6 +109,7 @@
 CMailCpsHandler::~CMailCpsHandler()
     {
     FUNC_LOG;
+    delete iUpdateHelper;
     delete iLiwIf;
     delete iSettings;
     iAccountsArray.ResetAndDestroy();
@@ -350,6 +352,20 @@
 void CMailCpsHandler::UpdateFullL()
     {
     FUNC_LOG;
+    if ( !iUpdateHelper )
+        {
+        iUpdateHelper = CMailCpsUpdateHelper::NewL( this );
+        }
+    iUpdateHelper->UpdateL();
+    }
+
+// ---------------------------------------------------------
+// CMailCpsHandler::DoUpdateFullL
+// ---------------------------------------------------------
+//
+void CMailCpsHandler::DoUpdateFullL()
+    {
+    FUNC_LOG;
     for (TInt instance = 0; instance < iLiwIf->GetWidgetInstanceCount(); instance++)
         {
         if ( iLiwIf->AllowedToPublish(instance) )
@@ -981,13 +997,6 @@
         User::Leave( KErrArgument );
         }
 
-    // Find mailbox instance from array
-    CMailMailboxDetails* mailbox = FindMailboxDetails( aMailbox );
-    if ( !mailbox )
-        {
-        return;
-        }
-
     // typecast param2
     TFSMailMsgId* parentFolder = static_cast<TFSMailMsgId*>( aParam2 );
 
@@ -1018,6 +1027,7 @@
     // Loop through message array
     TFSMailMsgId msgId;
     CFSMailMessage* msg( NULL );
+    CMailMailboxDetails* mailbox;
     for ( TInt ii = 0; ii < iiMax; ii++ )
         {
         msgId = (*newEntries)[ii];
@@ -1038,6 +1048,15 @@
             continue;
             }
 
+        // Find mailbox instance from array
+        mailbox = FindMailboxDetails( aMailbox );
+        if ( !mailbox )
+            {
+            CleanupStack::PopAndDestroy( msg );
+            CleanupStack::PopAndDestroy( newEntries );
+            return;
+            }
+
         // Check if message is duplicate
         if ( IsDuplicate( *mailbox, msgId ) )
             {
@@ -1992,3 +2011,103 @@
         }
     return backupOrRestore;
     }
+
+// ----------------------------------------------------------------------------
+// class CMailCpsUpdateHelper : public CTimer
+// Used to limit the rate of updates to Homescreen widget
+// ----------------------------------------------------------------------------
+//
+
+CMailCpsUpdateHelper::CMailCpsUpdateHelper(
+    CMailCpsHandler *aHandler )
+    : CTimer( EPriorityStandard )  // Could be EPriorityLow
+    , iCpsHandler( aHandler )
+    , iPending( EFalse )
+    {
+    FUNC_LOG;
+    }
+
+CMailCpsUpdateHelper* CMailCpsUpdateHelper::NewLC( CMailCpsHandler* aHandler )
+    {
+    FUNC_LOG;
+    CMailCpsUpdateHelper* self = new ( ELeave ) CMailCpsUpdateHelper( aHandler );
+    CleanupStack::PushL( self );
+    self->ConstructL();
+    return self;
+    }
+
+CMailCpsUpdateHelper* CMailCpsUpdateHelper::NewL( CMailCpsHandler *aHandler )
+    {
+    FUNC_LOG;
+    CMailCpsUpdateHelper* self = CMailCpsUpdateHelper::NewLC( aHandler );
+    CleanupStack::Pop( self );
+    return self;
+    }
+
+void CMailCpsUpdateHelper::ConstructL()
+    {
+    FUNC_LOG;
+    CTimer::ConstructL();
+    CActiveScheduler::Add( this );
+    }
+
+CMailCpsUpdateHelper::~CMailCpsUpdateHelper()
+    {
+    FUNC_LOG;
+    iCpsHandler = NULL; // Not owned
+    Cancel();   // Stop any pending request
+    Deque();    // Remove from CActiveScheduler
+    }
+
+// Notification that Homescreen widget(s) should be updated
+void CMailCpsUpdateHelper::UpdateL()
+    {
+    FUNC_LOG;
+    if ( IsActive() )
+        {
+        // Timer is running, so just flag that an update should be sent when
+        // the timer expires.
+        iPending = ETrue;
+        }
+    else
+        {
+        // Timer is not running, so perform an update and set the timer
+        // running.
+        DoUpdateL();
+        }
+    }
+
+// Actually perform an update of the Homescreen widget.  Also sets the timer
+// running and clears the pending flag.
+void CMailCpsUpdateHelper::DoUpdateL()
+    {
+    FUNC_LOG;
+    // Set the timer running.
+    After( KMailCpsHandlerUpdateDelay );
+    // Clear the pending flag.
+    iPending = EFalse;
+    // Do the update.
+    iCpsHandler->DoUpdateFullL();
+    }
+
+void CMailCpsUpdateHelper::RunL()
+    {
+    FUNC_LOG;
+    if ( iPending )
+        {
+        // There was an update request since the last update, so do another
+        // update (and set the timer running again, etc.).
+        DoUpdateL();
+        }
+    }
+
+TInt CMailCpsUpdateHelper::RunError( TInt aError )
+    {
+    FUNC_LOG;
+    if ( KErrNone != aError )
+        {
+        Cancel();   // Stop any pending request
+        }
+    return KErrNone;    // Don't panic the thread
+    }
+