emailservices/emailserver/cmailhandlerplugin/src/fsnotificationhandlerbaseimpl.cpp
changeset 0 8466d47a6819
child 8 e1b6206813b4
equal deleted inserted replaced
-1:000000000000 0:8466d47a6819
       
     1 /*
       
     2 * Copyright (c) 2007 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: This file implements class CFSNotificationHandlerBase.
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 //<cmail>
       
    20 #include "emailtrace.h"
       
    21 #include "CFSMailClient.h"
       
    22 //</cmail>
       
    23 
       
    24 #include "fsnotificationhandlermgr.h"
       
    25 #include "fsnotificationhandlerbase.h"
       
    26 #include "cmailhandlerpluginpanic.h"
       
    27 
       
    28 
       
    29 // ======== MEMBER FUNCTIONS ========
       
    30 
       
    31 CFSNotificationHandlerBase::CFSNotificationHandlerBase(
       
    32     MFSNotificationHandlerMgr& aOwner ) :
       
    33     iOwner( aOwner ),
       
    34     iObserving( EFalse )
       
    35     {
       
    36     FUNC_LOG;
       
    37     }
       
    38 
       
    39 void CFSNotificationHandlerBase::ConstructL()
       
    40     {
       
    41     FUNC_LOG;
       
    42     }
       
    43 
       
    44 CFSNotificationHandlerBase::~CFSNotificationHandlerBase()
       
    45     {
       
    46     FUNC_LOG;
       
    47     REComSession::DestroyedImplementation( iDestructorKey );
       
    48     }
       
    49 
       
    50 CFSMailClient& CFSNotificationHandlerBase::MailClient() const
       
    51     {
       
    52     FUNC_LOG;
       
    53     return iOwner.MailClient();
       
    54     }
       
    55 void CFSNotificationHandlerBase::EventL( TFSMailEvent aEvent, TFSMailMsgId aMailbox,
       
    56                                          TAny* aParam1, TAny* aParam2, TAny* aParam3 )
       
    57     {
       
    58     FUNC_LOG;
       
    59     if ( !iObserving )
       
    60         {
       
    61         return;
       
    62         }   
       
    63     
       
    64    /* TBool capabilitiesToContinue( CapabilitiesToContinueL( aEvent,
       
    65                                                           aMailbox,
       
    66                                                           aParam1,
       
    67                                                           aParam2,
       
    68                                                           aParam3) );
       
    69     if ( !capabilitiesToContinue )
       
    70         {
       
    71         return;
       
    72         }*/
       
    73     
       
    74     HandleEventL( aEvent, aMailbox, aParam1, aParam2, aParam3 );
       
    75     }
       
    76 
       
    77 
       
    78     
       
    79 void CFSNotificationHandlerBase::SetObserving( TBool aNewValue )
       
    80     {
       
    81     FUNC_LOG;
       
    82     iObserving = aNewValue;
       
    83     }
       
    84 
       
    85 TBool CFSNotificationHandlerBase::MsgIsUnread( CFSMailMessage& aMessage ) const
       
    86     {
       
    87     FUNC_LOG;
       
    88     TBool read( aMessage.IsFlagSet( EFSMsgFlag_Read ) );
       
    89     TBool read_locally( aMessage.IsFlagSet( EFSMsgFlag_Read_Locally ) );
       
    90     
       
    91     if ( !read && !read_locally )
       
    92         {
       
    93         return ETrue;
       
    94         }
       
    95     else
       
    96         {
       
    97         return EFalse;
       
    98         }
       
    99     }
       
   100 
       
   101 TBool CFSNotificationHandlerBase::MessagesCauseNotificationL( TFSMailMsgId aMailboxId,
       
   102                                                               CFSMailFolder& aParentFolder,
       
   103                                                               const RArray<TFSMailMsgId>& aMsgIdList )
       
   104     {
       
   105     FUNC_LOG;
       
   106     CFSMailMessage* newestMsg( NULL );
       
   107     TRAPD( notFoundError,
       
   108            newestMsg =
       
   109                NewestMsgInFolderL( aParentFolder ) );
       
   110     if ( notFoundError == KErrNotFound )
       
   111         {
       
   112         // For some odd reason we are not able to get the newest
       
   113         // message from the folder. This should not be possible
       
   114         // as we just received notification of a new message.
       
   115         // Possibly something has moved/deleted the message?
       
   116         return EFalse;
       
   117         }
       
   118     User::LeaveIfError( notFoundError );
       
   119     
       
   120     TTime dateOfNewest( newestMsg->GetDate() );
       
   121 
       
   122     delete newestMsg;
       
   123     newestMsg = NULL;
       
   124 
       
   125     TFSMailMsgId parentFolderId( aParentFolder.GetFolderId() );
       
   126 
       
   127     TInt index( 0 );
       
   128     const TInt entriesCount( aMsgIdList.Count() );
       
   129     while ( index < entriesCount )
       
   130         {
       
   131         // Let's get the message. We need to check from it that
       
   132         // it is really unread. This info is stored in the
       
   133         // flags. Also check that the message is newest.
       
   134         // EFSMsgDataEnvelope is used as TFSMailDetails 
       
   135         // so that we get enough data.
       
   136         CFSMailMessage*
       
   137             currentMessage( MailClient().GetMessageByUidL(
       
   138                 aMailboxId, 
       
   139                 parentFolderId,
       
   140                 aMsgIdList[index], 
       
   141                 EFSMsgDataEnvelope ) );
       
   142         User::LeaveIfNull( currentMessage );
       
   143         const TTime dateOfCurrentMsg( currentMessage->GetDate() );
       
   144         
       
   145         
       
   146         const TBool msgIsUnread( MsgIsUnread( *currentMessage ) );
       
   147         delete currentMessage;
       
   148         currentMessage = NULL;
       
   149         
       
   150         if ( msgIsUnread &&
       
   151              ( dateOfCurrentMsg >= dateOfNewest ) )
       
   152             {
       
   153             // At least one of the messages is unread and newest.
       
   154             return ETrue;
       
   155             }
       
   156             
       
   157         ++index;
       
   158         }
       
   159     
       
   160     return EFalse;
       
   161     }
       
   162 
       
   163 TBool CFSNotificationHandlerBase::Observing() const
       
   164     {
       
   165     FUNC_LOG;
       
   166     return iObserving;
       
   167     }
       
   168 
       
   169 TBool CFSNotificationHandlerBase::CapabilitiesToContinueL(
       
   170     TFSMailEvent aEvent,
       
   171     TFSMailMsgId aMailbox,
       
   172     TAny* /*aParam1*/,
       
   173     TAny* /*aParam2*/,
       
   174     TAny* /*aParam3*/ ) const
       
   175     {
       
   176     FUNC_LOG;
       
   177     if ( aEvent != TFSEventMailboxDeleted )
       
   178     	{
       
   179     	CFSMailBox* mailBox( MailClient().GetMailBoxByUidL( aMailbox ) );
       
   180     	if ( mailBox == NULL )
       
   181     		{
       
   182     		User::Leave( KErrArgument );
       
   183     		}
       
   184 
       
   185     	if ( mailBox->HasCapability( EFSMBoxCapaNewEmailNotifications ) )
       
   186     		{
       
   187     		delete mailBox;
       
   188     		return EFalse;
       
   189     		}
       
   190     	else
       
   191     		{
       
   192     		delete mailBox;
       
   193     		return ETrue;
       
   194     		}
       
   195     	}
       
   196     else
       
   197     	{
       
   198     	return ETrue;
       
   199     	}
       
   200     }
       
   201 
       
   202 void CFSNotificationHandlerBase::HandleEventL(
       
   203     TFSMailEvent aEvent,
       
   204     TFSMailMsgId aMailbox,
       
   205     TAny* aParam1,
       
   206     TAny* aParam2,
       
   207     TAny* /*aParam3*/ )
       
   208     {
       
   209     FUNC_LOG;
       
   210     // Only event TFSEventNewMail means that the mail is completely new.
       
   211     if ( aEvent == TFSEventNewMail )
       
   212         {
       
   213         // In case of TFSEventNewMail we have parent folder id
       
   214         // in aParam2
       
   215         TFSMailMsgId* parentFolderId( NULL );
       
   216         parentFolderId = static_cast< TFSMailMsgId* >( aParam2 );
       
   217         if ( parentFolderId == NULL )
       
   218             {
       
   219             User::Leave( KErrArgument );
       
   220             }
       
   221         CFSMailFolder* parentFolder(
       
   222             MailClient().GetFolderByUidL( aMailbox, *parentFolderId ) );
       
   223         User::LeaveIfNull( parentFolder );
       
   224         CleanupStack::PushL( parentFolder );
       
   225         
       
   226         // Set the notification on only in cases that the new mail is
       
   227         // in folder of type EFSInbox
       
   228         if ( parentFolder->GetFolderType() == EFSInbox )
       
   229             {
       
   230             
       
   231             RArray<TFSMailMsgId>* newEntries(
       
   232                 static_cast< RArray<TFSMailMsgId>* >( aParam1 ) );
       
   233 
       
   234             if ( MessagesCauseNotificationL(
       
   235                      aMailbox,
       
   236                      *parentFolder,
       
   237                      *newEntries ) )
       
   238                 {
       
   239                 TurnNotificationOn();
       
   240                 }
       
   241             }
       
   242          else
       
   243             {
       
   244             // If messages are in some other folder than in inbox
       
   245             // they have no effect on the notification
       
   246             }
       
   247         CleanupStack::PopAndDestroy( parentFolder );
       
   248         }
       
   249     else
       
   250         {
       
   251         // No other events than new mail are handled. For example
       
   252         // moving of messages and changing message status has no
       
   253         // effect on the notification.
       
   254         }
       
   255     }
       
   256 
       
   257 
       
   258 
       
   259 CFSMailMessage* CFSNotificationHandlerBase::NewestMsgInFolderL(
       
   260     /*const*/ CFSMailFolder& aFolder ) const
       
   261     {
       
   262     FUNC_LOG;
       
   263     // Load info only necessary for sorting by date into the messages.
       
   264     TFSMailDetails details( EFSMsgDataDate );
       
   265 
       
   266     // Want to sort mails so that the newest is in the beginning
       
   267     TFSMailSortCriteria criteriaDate;
       
   268     criteriaDate.iField = EFSMailSortByDate;
       
   269     criteriaDate.iOrder = EFSMailDescending;
       
   270 
       
   271     RArray<TFSMailSortCriteria> sorting;
       
   272     CleanupClosePushL( sorting );
       
   273     // First criteria appended would be the primary criteria
       
   274     // but here we don't have any other criterias
       
   275     sorting.Append( criteriaDate );
       
   276     MFSMailIterator* iterator = aFolder.ListMessagesL( details, sorting );
       
   277     
       
   278     // Resetting array of sort criterias already here because
       
   279     // the iterator does not need it anymore.
       
   280     CleanupStack::PopAndDestroy(); // sorting
       
   281                                     
       
   282     // CleanupStack::PushL doesn't work for M-class
       
   283     CleanupDeletePushL( iterator ); 
       
   284     
       
   285     RPointerArray<CFSMailMessage> messages;
       
   286     CleanupClosePushL( messages );
       
   287     // Let's get only the first and therefore the newest message.
       
   288     TInt amount( 1 );
       
   289     iterator->NextL( TFSMailMsgId(), amount, messages );    
       
   290     if ( messages.Count() < 1 )
       
   291         {
       
   292         messages.ResetAndDestroy();
       
   293         User::Leave( KErrNotFound );
       
   294         }
       
   295             
       
   296     CFSMailMessage* outcome = messages[0];
       
   297     messages.Remove( 0 ); // remove from array to prevent destruction of element
       
   298     messages.ResetAndDestroy();
       
   299     CleanupStack::PopAndDestroy(); // messages
       
   300     CleanupStack::PopAndDestroy( iterator );
       
   301     return outcome;
       
   302     }
       
   303 
       
   304 void Panic( TCmailhandlerPanic aPanic )
       
   305     {
       
   306     _LIT( KPanicText, "emailhandlerplugin" );
       
   307     User::Panic( KPanicText, aPanic );
       
   308     }
       
   309 
       
   310 // End of file
       
   311