emailservices/emailserver/cmailhandlerplugin/src/cmailcpshandler.cpp
branchRCL_3
changeset 10 f5907b1a1053
parent 8 e1b6206813b4
child 13 8592a65ad3fb
--- a/emailservices/emailserver/cmailhandlerplugin/src/cmailcpshandler.cpp	Fri Feb 19 22:37:30 2010 +0200
+++ b/emailservices/emailserver/cmailhandlerplugin/src/cmailcpshandler.cpp	Fri Mar 12 15:41:14 2010 +0200
@@ -1,5 +1,5 @@
 /*
-* Copyright (c) 2008 - 2010 Nokia Corporation and/or its subsidiary(-ies). 
+* Copyright (c) 2008 - 2010 Nokia Corporation and/or its subsidiary(-ies).
 * All rights reserved.
 * This component and the accompanying materials are made available
 * under the terms of "Eclipse Public License v1.0"
@@ -22,8 +22,6 @@
 #include <AknUtils.h>
 #include <apgcli.h>
 #include <centralrepository.h>
-#include <starterdomaincrkeys.h>
-#include <startupdomainpskeys.h>
 #include <emailobserverinterface.hrh>
 #include <emailobserverplugin.h>
 #include <memaildata.h>
@@ -50,7 +48,9 @@
 // CMailCpsHandler::CMailCpsHandler
 // ---------------------------------------------------------
 //
-CMailCpsHandler::CMailCpsHandler( MFSNotificationHandlerMgr& aOwner ) : CFSNotificationHandlerBase( aOwner )
+CMailCpsHandler::CMailCpsHandler( MFSNotificationHandlerMgr& aOwner ): 
+    CFSNotificationHandlerBase( aOwner ),
+    iWaitingForNewMailbox(NULL)
     {
     FUNC_LOG;
     }
@@ -78,9 +78,9 @@
     FUNC_LOG;
 
     CFSNotificationHandlerBase::ConstructL();
-    
+
     iEnv = CEikonEnv::Static( );
-    
+
     SetObserving( ETrue );
 
     // Instantiate helper classes
@@ -128,7 +128,7 @@
     // Get current configuration from settings interface,
     // and pass it on to actual publisher interface
     iLiwIf->SetConfiguration( iSettings->Configuration() );
-    
+
     TInt iiMax( iSettings->Mailboxes().Count() );
     for ( TInt ii = 0; ii < iiMax; ii++ )
         {
@@ -152,24 +152,24 @@
             CleanupStack::PushL( mailbox );
             CMailMailboxDetails* mailboxDetails = CreateMailboxDetailsL( *mailbox );
             CleanupStack::PushL( mailboxDetails );
-            
+
             TBuf<KMaxDescLen> cid;
             TInt next(1);
 
             // Check if same mailbox is already in iAccountsArray once or more
             for ( TInt i = 0; i < iAccountsArray.Count(); i++ )
-                {               
+                {
                 TInt id = (TInt)iAccountsArray[i]->iMailboxId.Id();
                 if (id == mailboxId)
                     {
                     next++;
                     }
                 }
-            iSettings->GetContentId(mailboxId, next, cid);          
+            iSettings->GetContentId(mailboxId, next, cid);
             mailboxDetails->SetWidgetInstance(cid);
             iAccountsArray.AppendL( mailboxDetails );
-            CleanupStack::Pop( mailboxDetails );                            
-            CleanupStack::PopAndDestroy( mailbox );            
+            CleanupStack::Pop( mailboxDetails );
+            CleanupStack::PopAndDestroy( mailbox );
             }
         }
     }
@@ -296,7 +296,7 @@
     {
     FUNC_LOG;
     // Create instance to return
-    CMailMailboxDetails* details = 
+    CMailMailboxDetails* details =
         CMailMailboxDetails::NewL( aMailbox.GetId(), aMailbox.GetName() );
     return details;
     }
@@ -314,6 +314,7 @@
     TRAP_IGNORE( InitializeExternalAccountsL() );
     // Update widget contents after settings change
     TRAP_IGNORE( UpdateFullL() );
+    CleanWaitingForNewMailbox();
     }
 
 // ---------------------------------------------------------
@@ -339,31 +340,54 @@
 void CMailCpsHandler::UpdateMailboxesL(TInt aInstance, const TDesC& aContentId)
     {
     FUNC_LOG;
-    TInt row(1); // start from first row
-    TInt mailbox(0);
-    TBool found( EFalse );
-    // try to find mailbox with matching contentId
-    for ( mailbox = 0; mailbox < iAccountsArray.Count(); mailbox++ )
+    // Look up 3rd party plugin that handles this account
+    CMailPluginProxy* plugin = GetExtPluginL( aContentId );
+    if ( plugin ) // handle external 3rd party accounts 
         {
-        if ( !aContentId.Compare( *iAccountsArray[mailbox]->iWidgetInstance ) )
-            {
-            INFO_1("iAccountsArray.Count() == %d", iAccountsArray.Count());
-            found = ETrue;
-            break;
-            }
+        // Publish its data
+        plugin->UpdateAccountL( aContentId );
         }
+    else // handle internal accounts
+        {   
+        TInt row(1); // start from first row
+        TInt mailbox(0);
+        TBool found( EFalse );
+        // try to find mailbox with matching contentId
+        for ( mailbox = 0; mailbox < iAccountsArray.Count(); mailbox++ )
+            {
+            if ( !aContentId.Compare( *iAccountsArray[mailbox]->iWidgetInstance ) )
+                {
+                INFO_1("iAccountsArray.Count() == %d", iAccountsArray.Count());
+                found = ETrue;
+                break;
+                }
+            }
 
-    // if contentId found from array, update the mailbox
-    if ( found )
-        {
-        // Update fields from left to right
-        UpdateMailBoxIconL( mailbox, aInstance, row );
-        UpdateMailboxNameL( mailbox, aInstance, row );
-        UpdateIndicatorIconL( mailbox, aInstance, row );
-        row++;
-        UpdateMessagesL( mailbox, aInstance, 1, row);
-        row++;
-        UpdateMessagesL( mailbox, aInstance, 2, row);
+        // if contentId found from array, update the mailbox
+        if ( found )
+            {
+            // Update fields from left to right
+            UpdateMailBoxIconL( mailbox, aInstance, row );
+            UpdateMailboxNameL( mailbox, aInstance, row );
+            UpdateIndicatorIconL( mailbox, aInstance, row );
+            row++;
+            UpdateMessagesL( mailbox, aInstance, 1, row);
+            row++;
+            UpdateMessagesL( mailbox, aInstance, 2, row);
+            }
+        else
+            {
+            // Clean mailbox name
+            iLiwIf->PublishActiveMailboxNameL( aInstance, 1, KNullDesC );
+            // Clean mailbox icon
+            TFSMailMsgId mailBoxId; // id not essential here        
+            iLiwIf->PublishMailboxIconL( aInstance, 1, KNullIcon, mailBoxId);
+            // Clean indicator icon
+            iLiwIf->PublishIndicatorIconL( aInstance, 1, KNullIcon);
+            // Clean message rows
+            UpdateEmptyMessagesL( aInstance, 2 );
+            UpdateEmptyMessagesL( aInstance, 3 );
+            }
         }
     }
 
@@ -384,7 +408,7 @@
         mailBoxId = iAccountsArray[aMailBoxNumber]->iMailboxId;
         CFSMailBox* mailbox( NULL );
         mailbox = MailClient().GetMailBoxByUidL( mailBoxId );
-        
+
         if(mailbox)
             {
             mailBoxFound = ETrue;
@@ -393,7 +417,7 @@
 
             TBuf<KMaxDescLen> accountName;
             accountName.Append(*iAccountsArray[aMailBoxNumber]->iMailboxName);
-                                    
+
             TInt unreadCount(GetUnreadCountL(iAccountsArray[aMailBoxNumber]->iMailboxId));
             if ( unreadCount > 0 )
                 {
@@ -403,21 +427,19 @@
                 CDesCArrayFlat* strings = new CDesCArrayFlat( 1 );
                 CleanupStack::PushL( strings );
                 strings->AppendL( accountName ); // replace "%0U" with mailbox name
-            
+
                 CArrayFix<TInt>* ints = new(ELeave) CArrayFixFlat<TInt>( 1 );
                 CleanupStack::PushL( ints );
                 ints->AppendL( unreadCount ); // replace "%1N" with number of unread messages
-            
-                HBufC* mailboxAndCount = StringLoader::LoadLC( R_EMAILWIDGET_TEXT_MAILBOX_AND_MAILCOUNT, 
+
+                HBufC* mailboxAndCount = StringLoader::LoadLC( R_EMAILWIDGET_TEXT_MAILBOX_AND_MAILCOUNT,
                                                                *strings,
                                                                *ints );
-            
+
                 iLiwIf->PublishActiveMailboxNameL(
 				        aWidgetInstance,
-                        aRowNumber, *mailboxAndCount,
-                        iAccountsArray.Count(),
-                        mailBoxId, folderId); 
-            
+                        aRowNumber, *mailboxAndCount);
+
                 CleanupStack::PopAndDestroy(mailboxAndCount);
                 CleanupStack::PopAndDestroy(ints);
                 CleanupStack::PopAndDestroy(strings);
@@ -425,12 +447,10 @@
             else
                 {
                 // show only mailbox name
-            
+
                 iLiwIf->PublishActiveMailboxNameL(
 				        aWidgetInstance,
-                        aRowNumber, accountName,
-                        iAccountsArray.Count(),
-                        mailBoxId, folderId);  
+                        aRowNumber, accountName);
                 }
             }
         }
@@ -438,12 +458,9 @@
         {
         // Publishing empty descriptor for rows that should not contain
         // anything on widget UI.
-        iLiwIf->PublishActiveMailboxNameL( aWidgetInstance, 
+        iLiwIf->PublishActiveMailboxNameL( aWidgetInstance,
 		                                   aRowNumber,
-										   KNullDesC,
-										   NULL,
-										   mailBoxId,
-										   folderId );
+										   KNullDesC);
         }
     }
 
@@ -457,7 +474,7 @@
                                        const TInt aMessageNumber,
                                        const TInt aRow )
     {
-    FUNC_LOG;    
+    FUNC_LOG;
 
     if ( aMailBoxNumber < iAccountsArray.Count() )
         {
@@ -479,7 +496,7 @@
             CleanupStack::PushL( folder );
 
             TInt msgCount = folder->GetMessageCount();
-            
+
             if(msgCount<1 || (msgCount == 1 && aRow == 3))
                 {
                 UpdateEmptyMessagesL( aWidgetInstance, aRow );
@@ -496,22 +513,22 @@
             TFSMailDetails details( EFSMsgDataEnvelope );
             RArray<TFSMailSortCriteria> sorting;
             CleanupClosePushL( sorting );
-    
+
             TFSMailSortCriteria sortCriteria;
             sortCriteria.iField = EFSMailSortByDate;
             sortCriteria.iOrder = EFSMailDescending;
             sorting.Append( sortCriteria );
-            // List all or maximum number of messages   
+            // List all or maximum number of messages
             MFSMailIterator* iterator = folder->ListMessagesL( details, sorting );
-            CleanupDeletePushL( iterator ); // standard CleanupStack::PushL does not work with non-C-class pointer  
-        
+            CleanupDeletePushL( iterator ); // standard CleanupStack::PushL does not work with non-C-class pointer
+
             TFSMailMsgId dummy;
             iterator->NextL( dummy, aMessageNumber, folderMessages);
             TInt count (folderMessages.Count());
             if(!count || (count == 1 && aRow == 3))
                 {
                 UpdateEmptyMessagesL( aWidgetInstance, aRow );
-            
+
                 CleanupStack::PopAndDestroy( iterator );
                 CleanupStack::PopAndDestroy( &sorting );
                 CleanupStack::PopAndDestroy( &folderMessages );
@@ -527,6 +544,8 @@
 
             // Get sender information
             TBuf<KMaxDescLen> sender( NULL );
+            TBuf<KMaxDescLen> subject( NULL );
+            subject.Append(msg->GetSubject());
             CFSMailAddress* fromAddress = msg->GetSender();
             if ( fromAddress )
                 {
@@ -539,20 +558,21 @@
                     sender.Append(fromAddress->GetEmailAddress());
                     }
                 }
-            
-            // Get message time and format it correctly 
+
+            // Get message time and format it correctly
             TTime msgTime = msg->GetDate();
             HBufC* timeString( NULL );
             timeString = GetMessageTimeStringL( msgTime );
-            CleanupDeletePushL( timeString );     
-        
-            // Publish message details           
-            PublishMessageL( aWidgetInstance, 
+            CleanupDeletePushL( timeString );
+
+            // Publish message details
+            PublishMessageL( aWidgetInstance,
                               *msg,
                               sender,
+                              subject,
                               *timeString,
                               aRow );
-        
+
             CleanupStack::PopAndDestroy( timeString );
             CleanupStack::PopAndDestroy( msg );
             CleanupStack::PopAndDestroy( iterator );
@@ -575,43 +595,43 @@
     // Get current local time
     TTime currentTime;
     currentTime.HomeTime();
-    
+
     // Get universal time offset and add it to message time
-    TLocale locale;    
+    TLocale locale;
     TTimeIntervalSeconds universalTimeOffset( locale.UniversalTimeOffset() );
-    aMessageTime += universalTimeOffset;      
-    
+    aMessageTime += universalTimeOffset;
+
     // If daylight saving is on, add one hour offset to message time
     if ( locale.QueryHomeHasDaylightSavingOn() )
         {
         TTimeIntervalHours daylightSaving( 1 );
         aMessageTime += daylightSaving;
         }
-    
+
     // Get datetime objects and compare dates
     TDateTime currentDate = currentTime.DateTime();
     TDateTime messageDate = aMessageTime.DateTime();
     HBufC* formatString( NULL );
-    
-    if ( currentDate.Year() == messageDate.Year() 
-        && currentDate.Month() == messageDate.Month() 
+
+    if ( currentDate.Year() == messageDate.Year()
+        && currentDate.Month() == messageDate.Month()
         && currentDate.Day() == messageDate.Day())
         {
         // Message arrived today
-        formatString = StringLoader::LoadLC( R_QTN_TIME_USUAL_WITH_ZERO );       
+        formatString = StringLoader::LoadLC( R_QTN_TIME_USUAL_WITH_ZERO );
         }
     else
         {
         // Message arrived earlier than today
         formatString = StringLoader::LoadLC( R_QTN_DATE_WITHOUT_YEAR_WITH_ZERO );
         }
-    
+
     // Format time string using chosen time or date formatString
     TBuf<KMaxShortDateFormatSpec*2> timeString;
     TRAP_IGNORE( aMessageTime.FormatL( timeString, formatString->Des() ) );
     CleanupStack::PopAndDestroy( formatString );
     // If format fails, result is empty
-    
+
     HBufC* result = timeString.Alloc();
     TPtr ptr = result->Des();
     AknTextUtils::DisplayTextLanguageSpecificNumberConversion( ptr );
@@ -625,7 +645,8 @@
 // ---------------------------------------------------------
 //
 void CMailCpsHandler::PublishMessageL( TInt aWidgetInstance, CFSMailMessage& aMessage,
-                                        const TDesC& aSenderName, const TDesC& aTime, TInt aRow )
+                                        const TDesC& aSenderName, const TDesC& aSubject,
+                                        const TDesC& aTime, TInt aRow )
     {
     FUNC_LOG;
 
@@ -633,6 +654,7 @@
 
     iLiwIf->PublishMailboxIconL( aWidgetInstance, aRow, GetMailIcon( &aMessage ), msgId );
     iLiwIf->PublishMailDetailL( aWidgetInstance, aRow, aSenderName, ESender );
+    iLiwIf->PublishMailDetailL( aWidgetInstance, aRow, aSubject, ESubject );    
     iLiwIf->PublishMailDetailL( aWidgetInstance, aRow, aTime, ETime );
     }
 
@@ -644,16 +666,22 @@
 void CMailCpsHandler::UpdateEmptyMessagesL( const TInt aWidgetInstance,
                                             const TInt aRow )
     {
-    FUNC_LOG;   
-    
+    FUNC_LOG;
+
     TFSMailMsgId mailBoxId; // id not essential here
-    
+
     iLiwIf->PublishMailDetailL(
             aWidgetInstance,
             aRow,
             KNullDes,
             ESender);
 
+    iLiwIf->PublishMailDetailL(
+            aWidgetInstance,
+            aRow,
+            KNullDes,
+            ESubject);    
+    
     iLiwIf->PublishMailboxIconL(
             aWidgetInstance,
             aRow,
@@ -662,66 +690,42 @@
 
     iLiwIf->PublishMailDetailL(
             aWidgetInstance,
-            aRow, 
+            aRow,
             KNullDes,
             ETime);
     }
-/*
-// ---------------------------------------------------------
-// CMailCpsHandler::UpdateConnectStateL
-// ---------------------------------------------------------
-//
-void CMailCpsHandler::UpdateConnectStateL( const TInt aMailBoxNumber, const TInt aRowNumber )
-    {
-    FUNC_LOG;
-    if ( aMailBoxNumber < iAccountsArray.Count() )
-        {
-        TFSMailMsgId mailBoxId;
-        mailBoxId = iAccountsArray[aMailBoxNumber]->iMailboxId;                
-        CFSMailBox* mailbox( NULL );
-        mailbox = MailClient().GetMailBoxByUidL( mailBoxId );
-        if(mailbox)
-            {
-            iLiwIf->PublishConnectIconL(mailbox->GetMailBoxStatus(), aRowNumber);
-            }
-        }
-    else
-        {
-        iLiwIf->PublishConnectIconL( EFSMailBoxOffline, aRowNumber );
-        }
-    }
-*/
+
 // ---------------------------------------------------------
 // CMailCpsHandler::UpdateMailBoxIconL
 // ---------------------------------------------------------
 //
-void CMailCpsHandler::UpdateMailBoxIconL( const TInt aMailBoxNumber, 
+void CMailCpsHandler::UpdateMailBoxIconL( const TInt aMailBoxNumber,
                                           const TInt aWidgetInstance,
                                           const TInt aRowNumber )
     {
     FUNC_LOG;
-    
+
     TFSMailMsgId mailBoxId; // id required only for getting branded mailbox icon
-    
+
     if ( aMailBoxNumber < iAccountsArray.Count() )
         {
         mailBoxId = iAccountsArray[aMailBoxNumber]->iMailboxId;
-        iLiwIf->PublishMailboxIconL( aWidgetInstance, 
+        iLiwIf->PublishMailboxIconL( aWidgetInstance,
 		                             aRowNumber,
 									 EMbmCmailhandlerpluginQgn_indi_cmail_drop_email_account,
 									 mailBoxId);
         }
     else
-        {    
+        {
         iLiwIf->PublishMailboxIconL( aWidgetInstance, aRowNumber, KNullIcon, mailBoxId);
         }
     }
-    
+
 // ---------------------------------------------------------
 // CMailCpsHandler::UpdateIndicatorIconL
 // ---------------------------------------------------------
 //
-void CMailCpsHandler::UpdateIndicatorIconL( const TInt aMailBoxNumber, 
+void CMailCpsHandler::UpdateIndicatorIconL( const TInt aMailBoxNumber,
                                             const TInt aWidgetInstance,
                                             const TInt aRowNumber )
 
@@ -731,51 +735,35 @@
     if ( aMailBoxNumber < iAccountsArray.Count() )
         {
         TFSMailMsgId mailBoxId;
-        mailBoxId = iAccountsArray[aMailBoxNumber]->iMailboxId;    
-    
+        mailBoxId = iAccountsArray[aMailBoxNumber]->iMailboxId;
+
         if ( iSettings->GetNewMailState( mailBoxId ) )
             {
             iLiwIf->PublishIndicatorIconL( aWidgetInstance,
                                            aRowNumber,
                                            EMbmCmailhandlerpluginQgn_stat_message_mail_uni );
             }
-    
+
         else if( !IsOutboxEmptyL(mailBoxId) )
             {
             iLiwIf->PublishIndicatorIconL( aWidgetInstance,
-                                           aRowNumber,                
+                                           aRowNumber,
                                            EMbmCmailhandlerpluginQgn_indi_cmail_outbox_msg);
             }
         else
-            {    
+            {
             iLiwIf->PublishIndicatorIconL( aWidgetInstance,
                                            aRowNumber,
                                            KNullIcon);
             }
         }
     else
-        {    
+        {
         iLiwIf->PublishIndicatorIconL( aWidgetInstance,
                                        aRowNumber,
                                        KNullIcon);
-        }        
+        }
     }
-    
-/*
-// ---------------------------------------------------------
-// CMailCpsHandler::ClearMessageTimeL
-// ---------------------------------------------------------
-//
-void CMailCpsHandler::ClearMessageTimeL( const TInt aRowNumber )
-    {
-    FUNC_LOG;
-
-    iLiwIf->PublishMailDetailL(
-            aRowNumber, 
-            KNullDes,
-            ETime);
-    }
-*/
 
 // ---------------------------------------------------------
 // CMailCpsHandler::CapabilitiesToContinueL
@@ -830,7 +818,7 @@
         {
         case TFSEventNewMailbox:
             {
-//            HandleNewMailboxEventL( aMailbox );
+            HandleNewMailboxEventL( aMailbox );
             break;
             }
         case TFSEventMailboxRenamed:
@@ -845,12 +833,6 @@
             UpdateFullL();
             break;
             }
-        case TFSEventMailboxSettingsChanged:
-            {
-//            HandleMailboxDeletedEventL( aMailbox );
-//            HandleNewMailboxEventL( aMailbox );
-            break;
-            }            
         case TFSEventNewMail:
             {
             HandleNewMailEventL( aMailbox, aParam1, aParam2 );
@@ -863,20 +845,10 @@
             UpdateFullL();
             break;
             }
-        case TFSEventMailChanged:
-            {
-            HandleMailChangedEventL( aMailbox, aParam1, aParam2 );
-            break;
-            }
-        case TFSEventNewFolder:
-            {
-            HandleNewFolderEventL( aMailbox, aParam1, aParam2 );
-            break;
-            }
         default:
             {
             break;
-            }            
+            }
         }
     }
 
@@ -887,40 +859,10 @@
 void CMailCpsHandler::HandleNewMailboxEventL( const TFSMailMsgId aMailbox )
     {
     FUNC_LOG;
-
-    // Check that max mailbox count is not already reached
-    const TInt iiMax( iAccountsArray.Count() );
-    if( iiMax >= iSettings->MaxMailboxCount() )
+    if (iWaitingForNewMailbox)
         {
-        return;
-        }
-
-    // Check that mailbox is not already in widget
-    for ( TInt ii = 0; ii < iiMax; ii++ )
-        {
-        if( iAccountsArray[ii]->iMailboxId.Id() == aMailbox.Id() )
-            {
-            return;
-            }
+        iSettings->AssociateWidgetToSetting( iWaitingForNewMailbox->Des(), aMailbox );
         }
-    
-    CFSMailBox* mailbox( NULL );
-    mailbox = MailClient().GetMailBoxByUidL( aMailbox );
-    if ( !mailbox )
-        {
-        return; // nothing that could be done
-        }
-    else
-        {
-        CleanupStack::PushL( mailbox );
-        CMailMailboxDetails* mailboxDetails = CreateMailboxDetailsL( *mailbox );
-        CleanupStack::PushL( mailboxDetails );
-        iAccountsArray.AppendL( mailboxDetails );
-        CleanupStack::Pop( mailboxDetails );
-        CleanupStack::PopAndDestroy( mailbox ); 
-        }
-    
-    iSettings->AddMailboxL( aMailbox );
     }
 
 // ---------------------------------------------------------
@@ -980,15 +922,15 @@
     TFSMailMsgId aMailbox, TAny* aParam1, TAny* aParam2 )
     {
     FUNC_LOG;
-    
+
     iSettings->ToggleWidgetNewMailIconL( ETrue, aMailbox );
-    
+
     // Basic assertions
     if ( !aParam1 || !aParam2 )
         {
         User::Leave( KErrArgument );
         }
-    
+
     // Find mailbox instance from array
     CMailMailboxDetails* mailbox = FindMailboxDetails( aMailbox );
     if ( !mailbox )
@@ -1030,7 +972,7 @@
         {
         msgId = (*newEntries)[ii];
 
-        msg = MailClient().GetMessageByUidL( 
+        msg = MailClient().GetMessageByUidL(
             aMailbox, *parentFolder, (*newEntries)[ii], EFSMsgDataEnvelope );
         if ( !msg )
             {
@@ -1053,7 +995,7 @@
             CleanupStack::PopAndDestroy( msg );
             continue;
             }
-        
+
         CMailMessageDetails* messageDetails = CMailMessageDetails::NewL(
             msg->GetMessageId(),
             mailbox,
@@ -1091,17 +1033,17 @@
         {
         User::Leave( KErrArgument );
         }
-    
+
     // Find correct mailbox
     CMailMailboxDetails* mailbox = FindMailboxDetails( aMailbox );
     if ( !mailbox )
         {
-        //<Cmail>  
+        //<Cmail>
         CleanupStack::PopAndDestroy( entries );
         //<Cmail>
         return;
         }
-    
+
     const TInt iiMax( entries->Count() );
     const TInt jjMax( mailbox->iMessageDetailsArray.Count() );
     // Loop through entries in the array
@@ -1129,26 +1071,6 @@
     }
 
 // ---------------------------------------------------------
-// CMailCpsHandler::HandleMailChangedEventL
-// ---------------------------------------------------------
-//
-void CMailCpsHandler::HandleMailChangedEventL(
-    TFSMailMsgId /*aMailbox*/, TAny* /*aParam1*/, TAny* /*aParam2*/ )
-    {
-    FUNC_LOG;
-    }
-
-// ---------------------------------------------------------
-// CMailCpsHandler::HandleNewFolderEventL
-// ---------------------------------------------------------
-//
-void CMailCpsHandler::HandleNewFolderEventL(
-    TFSMailMsgId /*aMailbox*/, TAny* /*aParam1*/, TAny* /*aParam2*/ )
-    {
-    FUNC_LOG;
-    }
-    
-// ---------------------------------------------------------
 // CMailCpsHandler::GetUnreadCountL
 // ---------------------------------------------------------
 //
@@ -1159,7 +1081,7 @@
     TInt unread (0);
     mailbox = MailClient().GetMailBoxByUidL( aMailbox );
     if(mailbox)
-        { 
+        {
         TFSMailMsgId folderId( mailbox->GetStandardFolderId( EFSInbox ) );
         // Check that folder is correct
         CFSMailFolder* folder = MailClient().GetFolderByUidL( aMailbox, folderId );
@@ -1168,11 +1090,11 @@
             return KErrNotFound;
             }
         CleanupStack::PushL( folder );
-    
+
         unread = folder->GetUnreadCount();
 
         CleanupStack::PopAndDestroy( folder );
-    
+
         if (unread > KMaxUnreadCount)
             {
             unread = KMaxUnreadCount;
@@ -1193,7 +1115,7 @@
     TInt unseen (0);
     mailbox = MailClient().GetMailBoxByUidL( aMailbox );
     if(mailbox)
-        { 
+        {
         TFSMailMsgId folderId( mailbox->GetStandardFolderId( EFSInbox ) );
         // Check that folder is correct
         CFSMailFolder* folder = MailClient().GetFolderByUidL( aMailbox, folderId );
@@ -1202,11 +1124,11 @@
             return KErrNotFound;
             }
         CleanupStack::PushL( folder );
-    
+
         unseen = folder->GetUnseenCount();
 
         CleanupStack::PopAndDestroy( folder );
-    
+
         if (unseen > KMaxUnreadCount)
             {
             unseen = KMaxUnreadCount;
@@ -1228,7 +1150,7 @@
     TInt msgCount(0);
     mailbox = MailClient().GetMailBoxByUidL( aMailbox );
     if(mailbox)
-        { 
+        {
         TFSMailMsgId folderId( mailbox->GetStandardFolderId( EFSOutbox ) );
         // Check that folder is correct
         CFSMailFolder* folder = MailClient().GetFolderByUidL( aMailbox, folderId );
@@ -1237,7 +1159,7 @@
             return KErrNotFound;
             }
         CleanupStack::PushL( folder );
-    
+
         msgCount = folder->GetMessageCount();
 
         if ( msgCount<1 )
@@ -1245,37 +1167,40 @@
             CleanupStack::PopAndDestroy( folder );
             return ret;
             }
-        
+
         TFSMailDetails details( EFSMsgDataEnvelope );
         RArray<TFSMailSortCriteria> sorting;
         CleanupClosePushL( sorting );
         TFSMailSortCriteria sortCriteria;
         sortCriteria.iField = EFSMailDontCare;
         sortCriteria.iOrder = EFSMailDescending;
-        sorting.Append( sortCriteria );       
+        sorting.Append( sortCriteria );
         // List all or maximum number of messages
         MFSMailIterator* iterator = folder->ListMessagesL( details, sorting );
-        CleanupStack::PopAndDestroy( &sorting );        
+        CleanupStack::PopAndDestroy( &sorting );
         CleanupDeletePushL( iterator );
-        
+
         RPointerArray<CFSMailMessage> messages;
         CleanupClosePushL( messages );
-        TInt amount( msgCount );
-        iterator->NextL( TFSMailMsgId(), amount, messages );
-        
-        for (TInt i = 0; i < msgCount; i++)
+        iterator->NextL( TFSMailMsgId(), msgCount, messages );
+
+        TInt arrayCount(messages.Count());
+        for (TInt i = 0; i < arrayCount; i++)
             {
-            TFSMailMsgId msgId = messages[0]->GetMessageId();
+            TFSMailMsgId msgId = messages[i]->GetMessageId();
             CFSMailMessage* msg( NULL );
             msg = MailClient().GetMessageByUidL( aMailbox, folderId, msgId, EFSMsgDataEnvelope );
-            CleanupDeletePushL( msg );
-            TFSMailMsgId mailboxId = msg->GetMailBoxId();
-            CleanupStack::PopAndDestroy( msg );
-            
-            if (mailboxId == aMailbox)
+			if (msg)
                 {
-                ret = EFalse;
-                break;
+                CleanupDeletePushL( msg );
+                TFSMailMsgId mailboxId = msg->GetMailBoxId();
+                CleanupStack::PopAndDestroy( msg );
+
+                if (mailboxId == aMailbox)
+                    {
+                    ret = EFalse;
+                    break;
+                    }
                 }
             }
 
@@ -1330,14 +1255,14 @@
 // -----------------------------------------------------------------------------
 TInt CMailCpsHandler::GetUnreadMsgIcon( CFSMailMessage* aMsg )
     {
-    FUNC_LOG;    
+    FUNC_LOG;
     TInt icon(EMbmCmailhandlerpluginQgn_indi_cmail_unread);
 
     // Unread calendar invitation
     if ( aMsg->IsFlagSet( EFSMsgFlag_CalendarMsg ))
         {
         icon = GetUnreadCalMsgIcon( aMsg );
-        }   
+        }
     else    // Normal message icons
         {
         // Check whether msg has attachment or not
@@ -1371,10 +1296,10 @@
                 else
                     {
                     icon = EMbmCmailhandlerpluginQgn_indi_cmail_unread_attach_low_prio;
-                    }  
+                    }
                 }
             else // Normal priority, has attachments
-                {       
+                {
                 if ( aMsg->IsFlagSet( EFSMsgFlag_Answered ) )
                     {
                     icon = EMbmCmailhandlerpluginQgn_indi_cmail_unread_replied_attach;
@@ -1389,7 +1314,7 @@
                     }
                 }
             }
-    
+
         else // No attachments
             {
             if ( aMsg->IsFlagSet( EFSMsgFlag_Important )) // High priority, no attachments
@@ -1405,7 +1330,7 @@
                 else
                     {
                     icon = EMbmCmailhandlerpluginQgn_indi_cmail_unread_high_prio;
-                    }                                                                                                                                           
+                    }
                 }
             else if ( aMsg->IsFlagSet( EFSMsgFlag_Low ) ) // Low priority, no attachments
                 {
@@ -1420,10 +1345,10 @@
                 else
                     {
                     icon = EMbmCmailhandlerpluginQgn_indi_cmail_unread_low_prio;
-                    }  
+                    }
                 }
             else // Normal priority, no attachments
-                {       
+                {
                 if ( aMsg->IsFlagSet( EFSMsgFlag_Answered ) )
                     {
                     icon = EMbmCmailhandlerpluginQgn_indi_cmail_unread_replied;
@@ -1433,7 +1358,7 @@
                     icon = EMbmCmailhandlerpluginQgn_indi_cmail_unread_forwarded;
                     }
                 }
-            }               
+            }
         }
     return icon;
     }
@@ -1443,7 +1368,7 @@
 // -----------------------------------------------------------------------------
 TInt CMailCpsHandler::GetReadMsgIcon( CFSMailMessage* aMsg )
     {
-    FUNC_LOG;    
+    FUNC_LOG;
     TInt icon(EMbmCmailhandlerpluginQgn_indi_cmail_read);
 
     if ( aMsg->IsFlagSet( EFSMsgFlag_CalendarMsg ))
@@ -1468,7 +1393,7 @@
                 else
                     {
                     icon = EMbmCmailhandlerpluginQgn_indi_cmail_read_attach_high_prio;
-                    }                                                                                                                                           
+                    }
                 }
             else if ( aMsg->IsFlagSet( EFSMsgFlag_Low ) ) // Low priority, has attachments
                 {
@@ -1483,10 +1408,10 @@
                 else
                     {
                     icon = EMbmCmailhandlerpluginQgn_indi_cmail_read_attach_low_prio;
-                    }  
+                    }
                 }
             else // Normal priority, has attachments
-                {       
+                {
                 if ( aMsg->IsFlagSet( EFSMsgFlag_Answered ) )
                     {
                     icon = EMbmCmailhandlerpluginQgn_indi_cmail_read_replied_attach;
@@ -1498,8 +1423,8 @@
                 else
                     {
                     icon = EMbmCmailhandlerpluginQgn_indi_cmail_read_attach;
-                    }                                                                                       
-                }           
+                    }
+                }
             }
         else // No attachments
             {
@@ -1516,7 +1441,7 @@
                 else
                     {
                     icon = EMbmCmailhandlerpluginQgn_indi_cmail_read_high_prio;
-                    }                                                                                                                                           
+                    }
                 }
             else if ( aMsg->IsFlagSet( EFSMsgFlag_Low ) ) // Low priority, no attachments
                 {
@@ -1531,10 +1456,10 @@
                 else
                     {
                     icon = EMbmCmailhandlerpluginQgn_indi_cmail_read_low_prio;
-                    }  
+                    }
                 }
             else // Normal priority, no attachments
-                {       
+                {
                 if ( aMsg->IsFlagSet( EFSMsgFlag_Answered ) )
                     {
                     icon = EMbmCmailhandlerpluginQgn_indi_cmail_read_replied;
@@ -1554,42 +1479,42 @@
 // -----------------------------------------------------------------------------
 TInt CMailCpsHandler::GetUnreadCalMsgIcon( CFSMailMessage* aMsg )
     {
-    FUNC_LOG;    
+    FUNC_LOG;
     TInt icon(0);
 
     if ( aMsg->IsFlagSet( EFSMsgFlag_Important ) )
         {
         if ( aMsg->IsFlagSet( EFSMsgFlag_Attachments ) )
             {
-            icon = EMbmCmailhandlerpluginQgn_indi_cmail_calendar_event_unread_attachments_high_prio ;                                               
+            icon = EMbmCmailhandlerpluginQgn_indi_cmail_calendar_event_unread_attachments_high_prio ;
             }
-        else 
+        else
             {
-            icon = EMbmCmailhandlerpluginQgn_indi_cmail_calendar_event_unread_high_prio;                                
-            }           
+            icon = EMbmCmailhandlerpluginQgn_indi_cmail_calendar_event_unread_high_prio;
+            }
         }
     else if ( aMsg->IsFlagSet( EFSMsgFlag_Low ) )
         {
         if ( aMsg->IsFlagSet( EFSMsgFlag_Attachments ) )
             {
-            icon = EMbmCmailhandlerpluginQgn_indi_cmail_calendar_event_unread_attachments_low_prio;                                             
+            icon = EMbmCmailhandlerpluginQgn_indi_cmail_calendar_event_unread_attachments_low_prio;
             }
         else
             {
-            icon = EMbmCmailhandlerpluginQgn_indi_cmail_calendar_event_unread_low_prio;                             
-            }                           
+            icon = EMbmCmailhandlerpluginQgn_indi_cmail_calendar_event_unread_low_prio;
+            }
         }
     else
         {
         if ( aMsg->IsFlagSet( EFSMsgFlag_Attachments ) )
             {
-            icon = EMbmCmailhandlerpluginQgn_indi_cmail_calendar_event_unread_attachments;                                              
+            icon = EMbmCmailhandlerpluginQgn_indi_cmail_calendar_event_unread_attachments;
             }
         else
             {
-            icon = EMbmCmailhandlerpluginQgn_indi_cmail_calendar_event_unread;                              
-            }                       
-        }       
+            icon = EMbmCmailhandlerpluginQgn_indi_cmail_calendar_event_unread;
+            }
+        }
 
     return icon;
     }
@@ -1599,42 +1524,42 @@
 // -----------------------------------------------------------------------------
 TInt CMailCpsHandler::GetReadCalMsgIcon( CFSMailMessage* aMsg )
     {
-    FUNC_LOG;    
+    FUNC_LOG;
     TInt icon(0);
 
     if ( aMsg->IsFlagSet( EFSMsgFlag_Important ) )
         {
         if ( aMsg->IsFlagSet( EFSMsgFlag_Attachments ) )
             {
-            icon = EMbmCmailhandlerpluginQgn_indi_cmail_calendar_event_read_attachments_high_prio ;                                             
+            icon = EMbmCmailhandlerpluginQgn_indi_cmail_calendar_event_read_attachments_high_prio ;
             }
-        else 
+        else
             {
-            icon = EMbmCmailhandlerpluginQgn_indi_cmail_calendar_event_read_high_prio;                              
-            }           
+            icon = EMbmCmailhandlerpluginQgn_indi_cmail_calendar_event_read_high_prio;
+            }
         }
     else if ( aMsg->IsFlagSet( EFSMsgFlag_Low ) )
         {
         if ( aMsg->IsFlagSet( EFSMsgFlag_Attachments ) )
             {
-            icon = EMbmCmailhandlerpluginQgn_indi_cmail_calendar_event_read_attachments_low_prio;                                               
+            icon = EMbmCmailhandlerpluginQgn_indi_cmail_calendar_event_read_attachments_low_prio;
             }
         else
             {
-            icon = EMbmCmailhandlerpluginQgn_indi_cmail_calendar_event_read_low_prio;                               
-            }                           
+            icon = EMbmCmailhandlerpluginQgn_indi_cmail_calendar_event_read_low_prio;
+            }
         }
     else
         {
         if ( aMsg->IsFlagSet( EFSMsgFlag_Attachments ) )
             {
-            icon = EMbmCmailhandlerpluginQgn_indi_cmail_calendar_event_read_attachments;                                                
+            icon = EMbmCmailhandlerpluginQgn_indi_cmail_calendar_event_read_attachments;
             }
         else
             {
-            icon = EMbmCmailhandlerpluginQgn_indi_cmail_calendar_event_read;                                
-            }                       
-        }       
+            icon = EMbmCmailhandlerpluginQgn_indi_cmail_calendar_event_read;
+            }
+        }
 
     return icon;
     }
@@ -1660,7 +1585,7 @@
         {
         return EFalse;
         }
-    
+
     return ETrue;
     }
 
@@ -1688,7 +1613,7 @@
 //
 void CMailCpsHandler::LaunchWidgetSettingsL( const TDesC& aContentId )
     {
-    FUNC_LOG;    
+    FUNC_LOG;
 
     RApaLsSession appArcSession;
     CleanupClosePushL( appArcSession );
@@ -1713,11 +1638,12 @@
     commandLine->SetCommandL( EApaCommandRun );
     commandLine->SetProcessEnvironmentL(process);
     CleanupStack::PopAndDestroy(commandLine);
-    
+
     process.Resume();
-    process.Close();    
-    
-    CleanupStack::PopAndDestroy(&appArcSession);        
+    process.Close();
+
+    CleanupStack::PopAndDestroy(&appArcSession);
+    SetWaitingForNewMailbox( aContentId );
     }
 
 // -----------------------------------------------------------------------------
@@ -1734,7 +1660,7 @@
         {
         TUid mailBoxUid;
         mailBoxUid.iUid = nativeMailboxId;
-        
+
         TFSMailMsgId mailBoxId;
         mailBoxId.SetId(mailBoxUid.iUid);
         TUid pluginUid;
@@ -1745,14 +1671,14 @@
         if ( mailBox )
             {
             TFSMailMsgId inboxFolderId = mailBox->GetStandardFolderId( EFSInbox );
-    
+
             TMailListActivationData tmp;
             tmp.iFolderId = inboxFolderId;
             tmp.iMailBoxId = mailBoxId;
             const TPckgBuf<TMailListActivationData> pkgOut( tmp );
-            iEnv->EikAppUi()->ActivateViewL( TVwsViewId(KUidEmailUi, KMailListId),
-                                             KStartListWithFolderId,
-                                             pkgOut);
+            iEnv->EikAppUi()->ActivateViewL(
+                TVwsViewId(KUidEmailUi, KMailListId),
+                KStartListWithFolderIdFromHomeScreen, pkgOut);
             }
         CleanupStack::PopAndDestroy( mailBox );
         }
@@ -1763,16 +1689,17 @@
     }
 
 // -----------------------------------------------------------------------------
-//  CMailCpsHandler::LaunchEmailUIL()
+//  CMailCpsHandler::LaunchEmailWizardL()
 // -----------------------------------------------------------------------------
 //
-void CMailCpsHandler::LaunchEmailWizardL()
+void CMailCpsHandler::LaunchEmailWizardL( const TDesC& aContentId )
     {
     FUNC_LOG;
     TUid viewUid(KNullUid);
-    iEnv->EikAppUi()->ActivateViewL( TVwsViewId(KUidWizardApp, KUidEmailWizardView), 
-                                     viewUid, 
+    iEnv->EikAppUi()->ActivateViewL( TVwsViewId(KUidWizardApp, KUidEmailWizardView),
+                                     viewUid,
                                      KNullDesC8);
+    SetWaitingForNewMailbox( aContentId );
     }
 
 // -----------------------------------------------------------------------------
@@ -1807,32 +1734,6 @@
     return plugin;
     }
 
-// -----------------------------------------------------------------------------
-//  CMailCpsHandler::UpdateExtAccountL
-// -----------------------------------------------------------------------------
-//
-void CMailCpsHandler::UpdateExtAccountL( const TDesC& aContentId )
-    {
-    FUNC_LOG;
-    // Look up plugin that handles this account
-    CMailPluginProxy* plugin = GetExtPluginL( aContentId );
-    if ( plugin )
-        {
-        // Publish its data
-        plugin->UpdateAccountL( aContentId );
-        }
-    }
-
-// ---------------------------------------------------------------------------
-// CMailCpsHandler::AssociateWidget
-// ---------------------------------------------------------------------------
-//
-TBool CMailCpsHandler::AssociateWidgetToSetting( const TDesC& aContentId )
-    {
-    FUNC_LOG;
-    return iSettings->AssociateWidgetToSetting( aContentId );
-    }
-
 // ---------------------------------------------------------------------------
 // CMailCpsHandler::DissociateWidget
 // ---------------------------------------------------------------------------
@@ -1840,13 +1741,7 @@
 void CMailCpsHandler::DissociateWidgetFromSettingL( const TDesC& aContentId )
     {
     FUNC_LOG;
-    //Do not dissociate if device is shutting down
-    TInt status( 0 );
-    RProperty::Get( KPSUidStartup, KPSGlobalSystemState, status );
-    if (status != ESwStateShuttingDown)
-        {
-        iSettings->DissociateWidgetFromSettingL( aContentId );
-        }
+    iSettings->DissociateWidgetFromSettingL( aContentId );
     }
 
 // ---------------------------------------------------------------------------
@@ -1886,78 +1781,50 @@
         {
         TUid implUid = plugins[i]->ImplementationUid();
         INFO_1("Instantiating plugin %d", implUid.iUid);
-        EmailInterface::CEmailObserverPlugin* plugin = 
+        EmailInterface::CEmailObserverPlugin* plugin =
             EmailInterface::CEmailObserverPlugin::NewL( implUid, this );
         MEmailData& data( plugin->EmailDataL() );
         totalMailboxCount += data.MailboxesL().Count();
         }
-    
+
     CleanupStack::PopAndDestroy(); // plugins
     return totalMailboxCount;
     }
 
 // ---------------------------------------------------------------------------
-// CMailCpsHandler::ManualAccountSelectionL
-// ---------------------------------------------------------------------------
-//
-void CMailCpsHandler::ManualAccountSelectionL( const TDesC& aContentId )
-    {
-    FUNC_LOG;
-
-    if ( FirstBootL() )
-        {
-        if (!iSettings->FindFromContentIdListL(aContentId))
-            {
-            iSettings->AddToContentIdListL(aContentId);
-            }
-        }
-    else
-        {
-        if (!iSettings->FindFromContentIdListL(aContentId))
-            {
-            iSettings->AddToContentIdListL(aContentId);
-            if ( TotalMailboxCountL() )
-                {
-                LaunchWidgetSettingsL(aContentId);
-                }
-            else
-                {
-                LaunchEmailWizardL();
-                }
-            }
-        }
-    }
-
-// ---------------------------------------------------------------------------
-// CMailCpsHandler::FirstBootL
-// ---------------------------------------------------------------------------
-TBool CMailCpsHandler::FirstBootL()
-    {
-    FUNC_LOG;
-    TInt value( 0 );
-    TBool ret(EFalse);
-
-    CRepository* repository(NULL);
-
-    TRAPD( err, repository = CRepository::NewL( KCRUidStartup ) );
-    if ( err == KErrNone )
-        {
-        err = repository->Get( KStartupFirstBoot, value );
-        }
-    delete repository;
-    
-    if (!value)
-        {
-        ret = ETrue;
-        }
-
-    return ret;
-    }
-
-// ---------------------------------------------------------------------------
 // CMailCpsHandler::EmailObserverEvent
 // ---------------------------------------------------------------------------
 void CMailCpsHandler::EmailObserverEvent( EmailInterface::MEmailData& /*aData*/ )
     {
+    FUNC_LOG;
     // Nothing to do
     }
+
+// -----------------------------------------------------------------------------
+//  CMailCpsHandler::SetWaitingForNewMailbox()
+// -----------------------------------------------------------------------------
+//
+void CMailCpsHandler::SetWaitingForNewMailbox( const TDesC& aContentId )
+    {
+    FUNC_LOG;
+    HBufC* newCid = aContentId.Alloc();
+    if ( newCid )
+        {
+        delete iWaitingForNewMailbox;
+        iWaitingForNewMailbox = newCid;
+        }
+    }
+
+// -----------------------------------------------------------------------------
+//  CMailCpsHandler::CleanWaitingForNewMailbox()
+// -----------------------------------------------------------------------------
+//
+void CMailCpsHandler::CleanWaitingForNewMailbox()
+    {
+    FUNC_LOG;
+	if (iWaitingForNewMailbox)
+        {
+        delete iWaitingForNewMailbox;
+        iWaitingForNewMailbox = NULL;
+        }
+    }