examples/SFExamples/symbian_os_communications_programming_book_v2/chapter8/receivingmessages/CMessageSummaryEngine.cpp

00001 // 
00002 // Copyright (c) 2002-2009 Nokia Corporation and/or its subsidiary(-ies).
00003 // All rights reserved.
00004 // This component and the accompanying materials are made available
00005 // under the terms of the License "Eclipse Public License v1.0"
00006 // which accompanies this distribution, and is available
00007 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
00008 // 
00009 // Initial Contributors:
00010 // Nokia Corporation - initial contribution.
00011 // 
00012 // Contributors:
00013 // 
00014 // Description:
00015 // 
00016 
00017 #include "CMessageSummaryEngine.h"
00018 #include <miutset.h>
00019 #include "summaryscreenlog.h"
00020 
00021 const CMessageSummaryGenerator& CMessageSummaryEngine::SmsSummary()
00022         {
00023         return *iSmsInboxSummary;
00024         }
00025         
00026 const CMessageSummaryGenerator* CMessageSummaryEngine::EmailSummary()
00027         {
00028         return iEmailInboxSummary;
00029         }
00030 
00031 
00032 CMessageSummaryEngine* CMessageSummaryEngine::NewL(MSummaryEngineObserver& aObserver)
00033         {
00034         CMessageSummaryEngine* self = new (ELeave) CMessageSummaryEngine(aObserver);
00035         CleanupStack::PushL(self);
00036         self->ConstructL();
00037         CleanupStack::Pop(self);
00038         CActiveScheduler::Add(self);
00039         return self;
00040         }
00041 
00042 // Start the summary operation
00043 void CMessageSummaryEngine::StartL()
00044         {
00045         LOG(">> CMessageSummaryEngine::StartL()");
00046         // Connect to the Message Server asynchronously
00047         iState = EConnectingToMessageServer;
00048         iMessagingSession = CMsvSession::OpenAsyncL(*this);
00049         
00050         // The message server will call HandleSessionEventL when the session is connected.
00051         LOG("<< CMessageSummaryEngine::StartL()");
00052         }
00053 
00054 // CActive functions
00055 void CMessageSummaryEngine::DoCancel()
00056         {
00057         // Nothing to cancel here
00058         }
00059 
00060 
00061 TBool CMessageSummaryEngine::UpdateNextSummary()
00062         {
00063         LOG(">> CMessageSummaryEngine::UpdateNextSummary()");
00064         TBool moreSummaries = ETrue;
00065 
00066         switch (iState)
00067                 {
00068                 case EConnectingToMessageServer:
00069                         iState = ERefreshing;
00070                         iStatus = KRequestPending;
00071                         // Move on to the next state, scan for messages
00072                 case ERefreshing:
00073                         // If the SMS list needs to be refreshed then refresh it
00074                         if (iRefreshSms)
00075                                 {
00076                                 iSmsInboxSummary->StartL(iStatus);
00077                                 iRefreshSms = EFalse;
00078                                 }
00079                         else if (iRefreshEmail)
00080                                 {
00081                                 // If the Email list needs to be refreshed then refresh it                              
00082                                 if (iEmailInboxSummary != NULL)
00083                                         // Get the email summaries if there is an email account.
00084                                         {
00085                                         iEmailInboxSummary->StartL(iStatus);                    
00086                                         }
00087                                 else
00088                                         {
00089                                         moreSummaries = EFalse;
00090                                         }
00091                                         
00092                                 iRefreshEmail = EFalse;
00093                                 }
00094                         else
00095                                 {
00096                                 moreSummaries = EFalse;
00097                                 }
00098                         break;
00099                         
00100                 default:
00101                         break;
00102                         }
00103         
00104         if (moreSummaries)
00105                 SetActive();
00106 
00107         LOG("<< CMessageSummaryEngine::UpdateNextSummary()");
00108 
00109         return moreSummaries;
00110         }
00111 
00112 
00113 void CMessageSummaryEngine::RunL()
00114         {
00115         LOG(">> CMessageSummaryEngine::RunL()");
00116 
00117         // One of the message summary generators has completed.
00118         if (!UpdateNextSummary())
00119                 {
00120                 // If UpdateNextSummary returns EFalse then there are no more message
00121                 // types to check. So refresh the UI.
00122                 iObserver.Refresh();            
00123                 }
00124                 
00125         LOG("<< CMessageSummaryEngine::RunL()");
00126         }
00127 
00128         
00129 CMessageSummaryEngine::CMessageSummaryEngine(MSummaryEngineObserver& aObserver) : CActive(EPriorityStandard),
00130                                                                                                 iObserver(aObserver),
00131                                                                                                 iRefreshSms(ETrue),
00132                                                                                                 iRefreshEmail(ETrue)
00133         {       
00134         }
00135 
00136 void CMessageSummaryEngine::ConstructL()
00137         {
00138         }
00139 
00140 void CMessageSummaryEngine::CreateSummaryGeneratorsL()
00141         {
00142         LOG(">> CMessageSummaryEngine::CreateSummaryGeneratorsL()");
00143         // Select the ordering we wish to use when enumerating through messages.
00144         // The message summary application is only interested in the most recent
00145         // messages so EMsvSortByDateReverse is approprate.
00146         TMsvSelectionOrdering ordering(KMsvNoGrouping, EMsvSortByDateReverse);
00147         
00148         // Create the CMsvEntry object that will be shared by all summary generators
00149         // Note that it is only possible to create the CMsvEntry once the message session
00150         // is connected.
00151         iMessagingEntry = CMsvEntry::NewL(*iMessagingSession, KMsvGlobalInBoxIndexEntryId, ordering);
00152         iSmsInboxSummary = new (ELeave) CMessageSummaryGenerator(KMsvGlobalInBoxIndexEntryId, KUidMsgTypeSMS, *iMessagingEntry);
00153         iEmailInboxSummary = NULL;
00154         
00155         // Find the first POP3 mailbox
00156         TMsvId pop3MailboxId;
00157         if (FindPop3ServiceEntryL(pop3MailboxId))
00158                 {
00159                 iEmailInboxSummary = new (ELeave) CMessageSummaryGenerator(pop3MailboxId, KUidMsgTypePOP3, *iMessagingEntry);
00160                 }
00161 
00162         LOG("<< CMessageSummaryEngine::CreateSummaryGeneratorsL()");
00163         }
00164 
00165 TBool CMessageSummaryEngine::FindPop3ServiceEntryL(TMsvId& aPop3MailboxId)
00166 {
00167         // Search for a POP3 mailbox entry
00168         TBool pop3MailboxFound = EFalse;
00169 
00170         //      Any entry under the root where iMtm == KUidMsgTypePOP3 is a POP3 service        
00171         iMessagingEntry->SetEntryL(KMsvRootIndexEntryIdValue);
00172         CMsvEntrySelection* serviceEntryList = iMessagingEntry->ChildrenWithMtmL(KUidMsgTypePOP3);
00173         CleanupStack::PushL(serviceEntryList);
00174         
00175         // Return the ID of the first entry in the list, if there is one
00176         if (serviceEntryList->Count() > 0)
00177                 {
00178                 aPop3MailboxId = (*serviceEntryList)[0];
00179                 pop3MailboxFound = ETrue;
00180                 }
00181         
00182         CleanupStack::PopAndDestroy(serviceEntryList);
00183         
00184         return pop3MailboxFound;
00185 }
00186 
00187 void CMessageSummaryEngine::HandleSessionEventL(TMsvSessionEvent aEvent, TAny *aArg1, TAny *aArg2, TAny* /* aArg3 */)
00188 // This function is called by the message server for every message server event,
00189 // e.g. when a new message is recieved or created. */
00190         {
00191         LOG(">> CMessageSummaryEngine::HandleSessionEventL");
00192         
00193         switch (aEvent)
00194                 {
00195         case MMsvSessionObserver::EMsvServerReady:
00196                 // The session is now connected to the message server
00197                 LOG(">> MMsvSessionObserver::EMsvServerReady:");
00198                 if (iState == EConnectingToMessageServer)
00199                         {
00200                         // Create the summary generators for each message type
00201                         CreateSummaryGeneratorsL();
00202                         
00203                         // If we were waiting for connection to the message server
00204                         // then move on to the next state, update the message summaries.
00205                         UpdateNextSummary();
00206                         }
00207                 LOG("<< MMsvSessionObserver::EMsvServerReady:");
00208                 break;
00209                 
00210         case MMsvSessionObserver::EMsvEntriesChanged:
00211                 LOG(">> MMsvSessionObserver::EMsvEntriesChanged:");
00212                 // Get the list of the changed messages. Note that there may be more than one.
00213                 CMsvEntrySelection* newMessages = static_cast<CMsvEntrySelection*> (aArg1);
00214 
00215                 // Get the ID of the parent entry
00216                 TMsvId* parentMsvId = static_cast<TMsvId*> (aArg2);
00217                 
00218                 // Set the CMsvEntry to point to the parent entry
00219                 iMessagingEntry->SetEntryL(*parentMsvId);
00220                 
00221                 // Add each new message to the appropriate message summary
00222                 TInt index = newMessages->Count() - 1;
00223                 while (index >= 0)
00224                 {
00225                         // Get the entry details for each new message
00226                         TMsvEntry tempMessageEntry = iMessagingEntry->ChildDataL(newMessages->At(index));
00227                         
00228                         if (tempMessageEntry.Complete())                         // Is the message complete? Do not add incomplete entries.
00229                                 {
00230                                 if (iSmsInboxSummary->MessageOfCorrectType(tempMessageEntry))
00231                                         {
00232                                         iRefreshSms = ETrue;
00233                                         }
00234                                 else if ((iEmailInboxSummary != NULL)
00235                                                 && (iEmailInboxSummary->MessageOfCorrectType(tempMessageEntry)))
00236                                         {
00237                                         // If we have a new Email message in the email inbox then update the Email summaries
00238                                         iRefreshEmail = ETrue;          
00239                                         }               
00240                                 }
00241                                 
00242                         index--;
00243                 }
00244 
00245                 if ((iRefreshSms) || (iRefreshEmail))
00246                         // Refresh the SMS and / or email list if there have been any changes
00247                         // to SMS and / or email messages
00248                         {
00249                         iState = ERefreshing;
00250                         
00251                         // If this object is not active then update the next summary
00252                         // If this object is active then the the appropriate summary list will
00253                         // be refreshed when it reaches the RunL.
00254                         if (!IsActive())
00255                                 UpdateNextSummary();
00256                         }
00257 
00258                 LOG("<< MMsvSessionObserver::EMsvEntriesChanged:");
00259 
00260                 break;
00261                 
00262         default:
00263                 // Do nothing in the default case.
00264                 // Assume that this event notification is not needed.
00265                 LOG("default:");
00266                 break;  
00267                 }
00268 
00269         LOG("<< CMessageSummaryEngine::HandleSessionEventL");
00270 
00271         }
00272 
00273 
00274 CMessageSummaryEngine::~CMessageSummaryEngine()
00275         {
00276         delete iEmailInboxSummary;
00277         delete iSmsInboxSummary;
00278         delete iMessagingEntry; 
00279         delete iMessagingSession; // This closes the session.
00280         }

Generated by  doxygen 1.6.2