ipsservices/ipssosplugin/src/ipsplgeventhandler.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 CIPSAccountInfo.
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include "emailtrace.h"
       
    20 #include "ipsplgheaders.h"
       
    21 
       
    22 const TInt KEventGranularity = 2;
       
    23 //<cmail>
       
    24 const TInt KMaxDriveUnitValue = 25;
       
    25 //</cmail>
       
    26 
       
    27 #ifdef _DEBUG
       
    28 _LIT( KIpsPlgEventHandlerPanic, "ArrError");
       
    29 #endif
       
    30 
       
    31 // ----------------------------------------------------------------------------
       
    32 // class CIPSAccountInfo
       
    33 // ----------------------------------------------------------------------------
       
    34 
       
    35 // ----------------------------------------------------------------------------
       
    36 // ----------------------------------------------------------------------------
       
    37 CIPSAccountInfo* CIPSAccountInfo::NewL(
       
    38     const TDesC& aName, const TFSMailMsgId& aMBoxId )
       
    39     {
       
    40     FUNC_LOG;
       
    41     CIPSAccountInfo* self = new(ELeave)CIPSAccountInfo( aMBoxId );
       
    42     CleanupStack::PushL( self );
       
    43     self->ConstructL( aName );
       
    44     CleanupStack::Pop( self );
       
    45     return self;
       
    46     }
       
    47 
       
    48 // ----------------------------------------------------------------------------
       
    49 // ----------------------------------------------------------------------------
       
    50 void CIPSAccountInfo::ConstructL( const TDesC& aName )
       
    51     {
       
    52     FUNC_LOG;
       
    53     iName = aName.AllocL();
       
    54     }
       
    55 
       
    56 // ----------------------------------------------------------------------------
       
    57 // ----------------------------------------------------------------------------
       
    58 CIPSAccountInfo::CIPSAccountInfo( const TFSMailMsgId& aMBoxId ) :
       
    59     iMBoxId( aMBoxId )
       
    60     {
       
    61     FUNC_LOG;
       
    62     }
       
    63 
       
    64 // ----------------------------------------------------------------------------
       
    65 // ----------------------------------------------------------------------------
       
    66 CIPSAccountInfo::~CIPSAccountInfo()
       
    67     {
       
    68     FUNC_LOG;
       
    69     delete iName;
       
    70     }
       
    71 
       
    72 // ----------------------------------------------------------------------------
       
    73 // class CIpsPlgEventHandler
       
    74 // ----------------------------------------------------------------------------
       
    75 
       
    76 // ---------------------------------------------------------------------------
       
    77 // ---------------------------------------------------------------------------
       
    78 CIpsPlgEventHandler* CIpsPlgEventHandler::NewL(
       
    79     CIpsPlgSosBasePlugin& aBasePlugin )
       
    80     {
       
    81     FUNC_LOG;
       
    82     CIpsPlgEventHandler* self = new( ELeave ) CIpsPlgEventHandler(
       
    83         aBasePlugin );
       
    84     CleanupStack::PushL( self );
       
    85     self->ConstructL( );
       
    86     CleanupStack::Pop( self );
       
    87     return self;
       
    88     }
       
    89 
       
    90 // ---------------------------------------------------------------------------
       
    91 // ---------------------------------------------------------------------------
       
    92 CIpsPlgEventHandler::~CIpsPlgEventHandler()
       
    93     {
       
    94     FUNC_LOG;
       
    95     UnRegisterPropertyObserver( this );
       
    96     iMBoxObservers.ResetAndDestroy();
       
    97     iMBoxObservers.Close();
       
    98     iIPSAccounts.ResetAndDestroy();
       
    99     iIPSAccounts.Close();
       
   100     if ( iCenRep )
       
   101         {
       
   102         iCenRep->NotifyCancelAll();
       
   103         }
       
   104     delete iCenRep;
       
   105     iIPSSettingsObservers.ResetAndDestroy();
       
   106     iIPSSettingsObservers.Close();
       
   107     iPropertyObservers.Close();
       
   108     delete iSettingsApi;
       
   109     iImapFolderIds.Close();
       
   110     }
       
   111 
       
   112 // ---------------------------------------------------------------------------
       
   113 // ---------------------------------------------------------------------------
       
   114 CIpsPlgEventHandler::CIpsPlgEventHandler(
       
   115     CIpsPlgSosBasePlugin& aBasePlugin )
       
   116     :iBasePlugin( aBasePlugin ),
       
   117     iMBoxObservers( KEventGranularity ),
       
   118     iIPSAccounts( KEventGranularity ),
       
   119     iIPSSettingsObservers( KEventGranularity ),
       
   120     iIsConnected( EFalse ),
       
   121     iPropertyObservers( KEventGranularity )
       
   122     {
       
   123     FUNC_LOG;
       
   124     }
       
   125 
       
   126 // ---------------------------------------------------------------------------
       
   127 // ---------------------------------------------------------------------------
       
   128 void CIpsPlgEventHandler::ConstructL( )
       
   129     {
       
   130     FUNC_LOG;
       
   131     iCenRep = CRepository::NewL( KCRUidExtendedSettingsUid );
       
   132     iPluginId = iBasePlugin.PluginId();
       
   133     RegisterPropertyObserverL( this );
       
   134     }
       
   135 
       
   136 // ----------------------------------------------------------------------------
       
   137 // ----------------------------------------------------------------------------
       
   138 void CIpsPlgEventHandler::CompleteConstructL( CMsvSession* aSession )
       
   139     {
       
   140     FUNC_LOG;
       
   141     iSession = aSession;
       
   142     iSettingsApi = CIpsSetDataApi::NewL( *iSession );
       
   143 
       
   144     // this collects all folderids to array from mailboxes that
       
   145     // have registered observers, this is need to do here because
       
   146     // observers might be registered before calling complete construct
       
   147     RArray<TMsvId> mboxes( KEventGranularity );
       
   148     for ( TInt i = 0; i < iMBoxObservers.Count(); i++ )
       
   149         {
       
   150         TMsvId mboxId = iMBoxObservers[i]->iMBoxId.Id();
       
   151         if ( mboxes.Find(mboxId) == KErrNotFound )
       
   152             {
       
   153             mboxes.Append( mboxId );
       
   154             }
       
   155         }
       
   156 
       
   157     for ( TInt i = 0; i < mboxes.Count(); i++ )
       
   158         {
       
   159         CollectSubscribedFoldersL( mboxes[i] );
       
   160         }
       
   161     mboxes.Close();
       
   162 
       
   163     }
       
   164 
       
   165 // ----------------------------------------------------------------------------
       
   166 // ----------------------------------------------------------------------------
       
   167 TInt CIpsPlgEventHandler::SetNewPropertyEvent(
       
   168         TInt aMailbox, TInt aEvent, TInt aError )
       
   169     {
       
   170     FUNC_LOG;
       
   171     TIpsPlgPropertyEvent pEvent(  aEvent, aMailbox ,
       
   172             iBasePlugin.PluginId() ,aError );
       
   173     TInt error = SetProperty( pEvent );
       
   174     return error;
       
   175     }
       
   176 
       
   177 // ----------------------------------------------------------------------------
       
   178 // ----------------------------------------------------------------------------
       
   179 TInt CIpsPlgEventHandler::SetProperty( TIpsPlgPropertyEvent aEvent )
       
   180     {
       
   181     FUNC_LOG;
       
   182     TInt error = RProperty::Define(
       
   183             KIpsPlgPropertyCatUid,
       
   184         KIPSSosEventPropertyKey, RProperty::EByteArray,
       
   185         KAllowAllPolicy,
       
   186         KAllowWriteDeviceDataPolicy );
       
   187 
       
   188     iPropertyBuf = aEvent;
       
   189 
       
   190     // if definition was success
       
   191     if ( error == KErrNone ||
       
   192          error == KErrAlreadyExists ||
       
   193          error == KErrPermissionDenied )
       
   194         {
       
   195         error = RProperty::Set(
       
   196                 KIpsPlgPropertyCatUid,
       
   197                 KIPSSosEventPropertyKey, iPropertyBuf );
       
   198 
       
   199         }
       
   200     return error;
       
   201     }
       
   202 
       
   203 // ----------------------------------------------------------------------------
       
   204 // ----------------------------------------------------------------------------
       
   205 TInt CIpsPlgEventHandler::RegisterPropertyObserverL(
       
   206         MIpsPlgPropertyObserver* aObserver )
       
   207     {
       
   208     FUNC_LOG;
       
   209     if ( !aObserver )
       
   210         {
       
   211         User::Leave( KErrArgument );
       
   212         }
       
   213 
       
   214     if ( !iPropertyWatcher )
       
   215         {
       
   216         iPropertyWatcher = CIpsPlgPropertyWatcher::NewL(
       
   217                 CActive::EPriorityIdle, *this );
       
   218         }
       
   219 
       
   220     return iPropertyObservers.Append( aObserver );
       
   221     }
       
   222 
       
   223 // ----------------------------------------------------------------------------
       
   224 // ----------------------------------------------------------------------------
       
   225 void CIpsPlgEventHandler::UnRegisterPropertyObserver(
       
   226         MIpsPlgPropertyObserver* aObserver )
       
   227     {
       
   228     FUNC_LOG;
       
   229     TInt index = iPropertyObservers.Find( aObserver );
       
   230 
       
   231     if ( index != KErrNotFound )
       
   232         {
       
   233         iPropertyObservers.Remove( index );
       
   234         }
       
   235 
       
   236     if ( iPropertyObservers.Count() == 0 )
       
   237         {
       
   238         delete iPropertyWatcher;
       
   239         iPropertyWatcher = NULL;
       
   240         }
       
   241 
       
   242     }
       
   243 
       
   244 // ----------------------------------------------------------------------------
       
   245 // ----------------------------------------------------------------------------
       
   246 void CIpsPlgEventHandler::NotifyPropertyEventL(
       
   247         TIpsPlgPropertyEvent aEvent )
       
   248     {
       
   249     FUNC_LOG;
       
   250     TBool doNotify = ETrue;
       
   251     switch ( aEvent.iEvent )
       
   252         {
       
   253         case KIpsSosEmailSyncStarted:
       
   254             SignalStartSyncL( TFSMailMsgId( aEvent.iPluginId,
       
   255                     aEvent.iMailbox ) );
       
   256             break;
       
   257         case KIpsSosEmailSyncCompleted:
       
   258             {
       
   259             SignalSyncCompletedL( TFSMailMsgId( aEvent.iPluginId,
       
   260                     aEvent.iMailbox ), aEvent.iError );
       
   261                 }
       
   262             break;
       
   263         default:
       
   264             break;
       
   265         }
       
   266 
       
   267     for ( TInt i = 0; doNotify && i < iPropertyObservers.Count(); i++ )
       
   268         {
       
   269         TRAP_IGNORE( iPropertyObservers[i]->HandlePropertyEventL(
       
   270                 aEvent.iEvent,  aEvent.iMailbox,
       
   271                 aEvent.iPluginId, aEvent.iError ) );
       
   272         }
       
   273     }
       
   274 
       
   275 // ----------------------------------------------------------------------------
       
   276 // ----------------------------------------------------------------------------
       
   277 void CIpsPlgEventHandler::HandleSessionEventL(
       
   278     MMsvSessionObserver::TMsvSessionEvent aEvent,
       
   279     TAny* aArg1,
       
   280     TAny* aArg2,
       
   281     TAny* aArg3)
       
   282     {
       
   283     FUNC_LOG;
       
   284     if ( aEvent == MMsvSessionObserver::EMsvServerReady )
       
   285         {
       
   286         iBasePlugin.CompleteConstructL();
       
   287         IPSAccountsL();
       
   288         return;
       
   289         }
       
   290     if ( !iPluginObserver && iMBoxObservers.Count()==0 )
       
   291         {
       
   292         //if no observer we can't relay these events.
       
   293         return;
       
   294         }
       
   295     //<cmail>
       
   296     if ( iMBoxObservers.Count() == 0 && aArg2 &&
       
   297         (*(TMsvId*) (aArg2)) != KMsvRootIndexEntryIdValue &&
       
   298         aEvent != EMsvMediaChanged )
       
   299         {
       
   300         // event is related to messages / folders, can return
       
   301         // because no mailbox observers found
       
   302         return;
       
   303         }
       
   304     //</cmail>
       
   305     
       
   306     switch( aEvent )
       
   307         {
       
   308         case EMsvEntriesCreated:
       
   309             HandleEntriesCreatedL( aArg1, aArg2, aArg3 );
       
   310             break;
       
   311         case EMsvEntriesDeleted:
       
   312             HandleEntriesDeletedL( aArg1, aArg2, aArg3 );
       
   313             break;
       
   314         case EMsvEntriesChanged:
       
   315             HandleEntriesChangedL( aArg1, aArg2, aArg3 );
       
   316             break;
       
   317         case EMsvEntriesMoved:
       
   318             HandleEntriesMovedL( aArg1, aArg2, aArg3 );
       
   319             break;
       
   320         case EMsvServerTerminated:
       
   321             iBasePlugin.SessionTerminated();
       
   322             iSession = NULL;
       
   323             break;
       
   324         case EMsvCloseSession:
       
   325             break;
       
   326         case EMsvGeneralError:
       
   327             break;
       
   328         //<cmail>    
       
   329         case EMsvMediaChanged:
       
   330             HandleMediaChangedL( aArg1, aArg2, aArg3 );
       
   331             break;
       
   332         //</cmail>
       
   333         default:
       
   334             //we don't care about unhandled events
       
   335             break;
       
   336         };
       
   337     }
       
   338 
       
   339 // ----------------------------------------------------------------------------
       
   340 // ----------------------------------------------------------------------------
       
   341 void CIpsPlgEventHandler::AddPluginObserverL(
       
   342     MFSMailEventObserver* aObserver )
       
   343     {
       
   344     FUNC_LOG;
       
   345     // there can be several plugin observers??
       
   346     iPluginObserver = aObserver;
       
   347     }
       
   348 // ----------------------------------------------------------------------------
       
   349 // ----------------------------------------------------------------------------
       
   350 void CIpsPlgEventHandler::RemovePluginObserver(
       
   351     MFSMailEventObserver* /*aObserver*/ )
       
   352     {
       
   353     FUNC_LOG;
       
   354     iPluginObserver = NULL;
       
   355     }
       
   356 
       
   357 // ----------------------------------------------------------------------------
       
   358 // ----------------------------------------------------------------------------
       
   359 void CIpsPlgEventHandler::SubscribeMailboxEventsL(
       
   360     const TFSMailMsgId& aMailboxId,
       
   361     MFSMailEventObserver& aObserver,
       
   362     TUint32 aKey )
       
   363     {
       
   364     FUNC_LOG;
       
   365     CMBoxEventObserver* observer = new(ELeave) CMBoxEventObserver( aObserver, aMailboxId );
       
   366     CleanupStack::PushL( observer );
       
   367     iMBoxObservers.AppendL( observer );
       
   368     CleanupStack::Pop( observer );
       
   369     AppendSettingsObserverL( aMailboxId, aKey );
       
   370     //at the moment we won't support syncstate events for IMAP alwaysonline.
       
   371     //because of imap idle. we can't extract usefull syncstate changes when it is in use.
       
   372    /* if ( iBasePlugin.MtmId().iUid != KSenduiMtmImap4UidValue )
       
   373         {
       
   374         AppendSyncStateObserverL( aMailboxId );
       
   375         }*/
       
   376     if( aMailboxId.PluginId() == KIpsPlgImap4PluginUid )
       
   377         {
       
   378         CollectSubscribedFoldersL( aMailboxId.Id() );
       
   379         }
       
   380     }
       
   381 
       
   382 // ----------------------------------------------------------------------------
       
   383 // ----------------------------------------------------------------------------
       
   384 void CIpsPlgEventHandler::UnsubscribeMailboxEvents(
       
   385     const TFSMailMsgId& aMailboxId,
       
   386     MFSMailEventObserver& aObserver )
       
   387     {
       
   388     FUNC_LOG;
       
   389     TInt count = iMBoxObservers.Count();
       
   390     for( TInt i=0; i<count; i++ )
       
   391         {
       
   392         if( &iMBoxObservers[i]->iObserver == &aObserver )
       
   393             {
       
   394             RemoveSettingsObserver( aMailboxId );
       
   395             delete iMBoxObservers[i];
       
   396             iMBoxObservers.Remove(i);
       
   397             break;
       
   398             }
       
   399         }
       
   400     }
       
   401 
       
   402 // ---------------------------------------------------------------------------
       
   403 // ---------------------------------------------------------------------------
       
   404 TBool CIpsPlgEventHandler::IsEventFromIpsSourceL( TAny* aArg1,
       
   405                                                   TUid& aSourceType,
       
   406                                                   TMsvEntry& aEntry )
       
   407     {
       
   408     FUNC_LOG;
       
   409     const CMsvEntrySelection* selection =
       
   410         static_cast<CMsvEntrySelection*>( aArg1 );
       
   411 
       
   412     TMsvId dummy( KMsvNullIndexEntryIdValue );
       
   413     TMsvId entryId( KMsvNullIndexEntryIdValue );
       
   414     if ( selection->Count() == 0)
       
   415         {
       
   416         User::Leave( KErrArgument );
       
   417         }
       
   418 
       
   419     if( !iSession )
       
   420         {
       
   421         User::Leave( KErrNotReady );
       
   422         }
       
   423 
       
   424     entryId = selection->At(0);
       
   425     User::LeaveIfError( iSession->GetEntry( entryId, dummy, aEntry ) );
       
   426 
       
   427     //we set the source type regardless is it IPS ( imap/pop )
       
   428     aSourceType = aEntry.iType;
       
   429 
       
   430 
       
   431     TBool ret = ETrue;
       
   432     //we're interested only in events from own mtm type
       
   433     //or ny messages in smtp folder
       
   434     if ( aEntry.iMtm.iUid != iBasePlugin.MtmId().iUid )
       
   435         {
       
   436         //when creating a mailbox, we come here only once,
       
   437         //so this is reliable way to see mailbox creation.
       
   438         ret = EFalse;
       
   439         }
       
   440     else if ( aEntry.iType.iUid < KUidMsvRootEntryValue ||
       
   441              aEntry.iType.iUid > KUidMsvAttachmentEntryValue )
       
   442          {
       
   443          ret = EFalse;
       
   444          }
       
   445 
       
   446     if ( !ret && aEntry.iMtm.iUid == KSenduiMtmSmtpUidValue )
       
   447         {
       
   448         ret = ETrue;
       
   449         }
       
   450 
       
   451 
       
   452     return ret;
       
   453     }
       
   454 
       
   455 // ---------------------------------------------------------------------------
       
   456 // ---------------------------------------------------------------------------
       
   457 inline TFSMailMsgId CIpsPlgEventHandler::SymId2FsId(
       
   458     TMsvId aId, TUint aMtmUid )
       
   459     {
       
   460     FUNC_LOG;
       
   461     TFSMailMsgId id;
       
   462     id.SetId( aId );
       
   463     if ( aMtmUid == KSenduiMtmImap4UidValue )
       
   464         {
       
   465         id.SetPluginId( KIpsPlgImap4PluginUid );
       
   466         }
       
   467     else if ( aMtmUid == KSenduiMtmPop3UidValue )
       
   468         {
       
   469         id.SetPluginId( KIpsPlgPop3PluginUid );
       
   470         }
       
   471     else if ( aMtmUid == KSenduiMtmSmtpUidValue )
       
   472         {
       
   473         // set plugin id to this plugin
       
   474         id.SetPluginId( TUid::Uid(iPluginId) );
       
   475         }
       
   476     return id;
       
   477     }
       
   478 
       
   479 // ---------------------------------------------------------------------------
       
   480 // ---------------------------------------------------------------------------
       
   481 inline TFSMailMsgId CIpsPlgEventHandler::SymId2FsId(
       
   482     const TMsvEntry& aEntry )
       
   483     {
       
   484     FUNC_LOG;
       
   485     return SymId2FsId( aEntry.Id(), aEntry.iMtm.iUid );
       
   486     }
       
   487 
       
   488 // ---------------------------------------------------------------------------
       
   489 // ---------------------------------------------------------------------------
       
   490 inline void CIpsPlgEventHandler::FillFSMessageArray(
       
   491     RArray<TFSMailMsgId>& aFSArray,
       
   492     const CMsvEntrySelection* aSelection,
       
   493     TUint aMtmUid )
       
   494     {
       
   495     FUNC_LOG;
       
   496     // function should used only with empty (new) arrays
       
   497     __ASSERT_DEBUG( ( aFSArray.Count() == 0 ),
       
   498         User::Panic( KIpsPlgEventHandlerPanic, KErrGeneral ) );
       
   499 
       
   500     for ( TInt i = 0; i < aSelection->Count(); i++ )
       
   501         {
       
   502         aFSArray.Append( SymId2FsId(aSelection->At(i), aMtmUid) );
       
   503         }
       
   504     }
       
   505 
       
   506 // ----------------------------------------------------------------------------
       
   507 // ----------------------------------------------------------------------------
       
   508 void CIpsPlgEventHandler::SendDelayedEventL(
       
   509     TFSMailEvent aEvent,
       
   510     TFSMailMsgId aMailbox,
       
   511     TAny* aArg1,
       
   512     TAny* aArg2,
       
   513     TAny* aArg3 )
       
   514     {
       
   515     FUNC_LOG;
       
   516     TIpsPlgEvent event;
       
   517     event.iAccountId = aMailbox;
       
   518     event.iEvent = aEvent;
       
   519     event.iArg1 = aArg1;
       
   520     event.iArg2 = aArg2;
       
   521     event.iArg3 = aArg3;
       
   522 
       
   523     RPointerArray<MFSMailEventObserver> observers;
       
   524     CleanupClosePushL( observers );
       
   525 
       
   526     if( aEvent == TFSEventNewMailbox ||
       
   527         aEvent == TFSEventMailboxRenamed ||
       
   528         aEvent == TFSEventMailboxDeleted )
       
   529         {
       
   530         //<cmail>
       
   531         if( iPluginObserver )
       
   532             {
       
   533             // Could been deleted within observer itself
       
   534             observers.AppendL( iPluginObserver );
       
   535             }
       
   536         //</cmail>
       
   537         }
       
   538     else
       
   539         {
       
   540         //other events go to mailbox specific observers
       
   541         MailboxObserversL( aMailbox, observers );
       
   542         }
       
   543     for( TInt i = 0; i < observers.Count(); ++i )
       
   544         {
       
   545         // ignore leave so that other observers get event even if
       
   546         // one leaves
       
   547         TRAP_IGNORE(observers[i]->EventL(
       
   548             event.iEvent,
       
   549             event.iAccountId,
       
   550             event.iArg1,
       
   551             event.iArg2,
       
   552             event.iArg3 ));
       
   553         }
       
   554     CleanupStack::PopAndDestroy( &observers );
       
   555     }
       
   556 
       
   557 // ----------------------------------------------------------------------------
       
   558 // ----------------------------------------------------------------------------
       
   559 void CIpsPlgEventHandler::IPSAccountsL()
       
   560     {
       
   561     FUNC_LOG;
       
   562     if( !iSession )
       
   563         {
       
   564         User::Leave( KErrNotReady );
       
   565         }
       
   566     CMsvEntry* root = iSession->GetEntryL( KMsvRootIndexEntryIdValue );
       
   567     CleanupStack::PushL( root );
       
   568 
       
   569     CMsvEntrySelection* pop = root->ChildrenWithMtmL( KSenduiMtmPop3Uid );
       
   570     CleanupStack::PushL( pop );
       
   571 
       
   572     CMsvEntrySelection* imap = root->ChildrenWithMtmL( KSenduiMtmImap4Uid );
       
   573     CleanupStack::PushL( imap );
       
   574 
       
   575     TInt count = pop->Count();
       
   576     TMsvEntry tEntry;
       
   577     TMsvId service;
       
   578     for ( TInt i=0; i<count; i++ )
       
   579         {
       
   580         iSession->GetEntry( pop->At(i), service, tEntry );
       
   581         //<cmail>
       
   582         if( !AccountExistsL( tEntry ) )
       
   583             {
       
   584             iIPSAccounts.AppendL( CIPSAccountInfo::NewL( 
       
   585                 tEntry.iDetails, 
       
   586                 SymId2FsId( tEntry ) ) );    
       
   587             }
       
   588         //</cmail>
       
   589         }
       
   590         
       
   591     count = imap->Count();
       
   592     for ( TInt i=0; i<count; i++ )
       
   593         {
       
   594         iSession->GetEntry( imap->At(i), service, tEntry );
       
   595         //<cmail>
       
   596         if( !AccountExistsL( tEntry ) )
       
   597             {  
       
   598             iIPSAccounts.AppendL( CIPSAccountInfo::NewL( 
       
   599                 tEntry.iDetails, 
       
   600                 SymId2FsId( tEntry ) ) );
       
   601             }
       
   602         //</cmail>
       
   603         }        
       
   604     
       
   605     CleanupStack::PopAndDestroy( 3, root );
       
   606     }
       
   607 
       
   608 // ----------------------------------------------------------------------------
       
   609 // ----------------------------------------------------------------------------
       
   610 void CIpsPlgEventHandler::HandleEntriesCreatedL(
       
   611     TAny* aArg1, TAny* aArg2, TAny* /*aArg3*/ )
       
   612     {
       
   613     FUNC_LOG;
       
   614     TUid type;
       
   615     TMsvEntry tNew;
       
   616 
       
   617     if( IsEventFromIpsSourceL( aArg1, type, tNew ) )
       
   618         {
       
   619         TAny* arg1=NULL;
       
   620         TAny* arg2=NULL;
       
   621         TAny* arg3=NULL;
       
   622 
       
   623         TFSMailEvent event;
       
   624         TFSMailMsgId mbox;
       
   625 
       
   626         if ( type == KUidMsvServiceEntry )
       
   627             {
       
   628             event = TFSEventNewMailbox;
       
   629 
       
   630             mbox = SymId2FsId( tNew );
       
   631 
       
   632             if( AccountExistsL( tNew ) )
       
   633                 {
       
   634                 //this is a duplicate event. Ignoring...
       
   635                 return;
       
   636                 }
       
   637 
       
   638             iIPSAccounts.AppendL( CIPSAccountInfo::NewL(
       
   639                 tNew.iDetails,
       
   640                 mbox ) );
       
   641             SendDelayedEventL( event, mbox,
       
   642                 arg1, arg2 , arg3 );
       
   643             }
       
   644         
       
   645         // discard any new mail events if message is marked as deleted in imap
       
   646         const TMsvEmailEntry& emlEntry(tNew);
       
   647         TBool isMarkedAsDeleted = EFalse;
       
   648         if ( mbox.PluginId() == KIpsPlgImap4PluginUid
       
   649                 && (EDisconnectedDeleteOperation == emlEntry.DisconnectedOperation()
       
   650                 || emlEntry.DeletedIMAP4Flag()) )
       
   651             {
       
   652             isMarkedAsDeleted = ETrue;
       
   653             }
       
   654         
       
   655         
       
   656         if ( type == KUidMsvMessageEntry && !isMarkedAsDeleted )
       
   657             {
       
   658             TMsvId* parent = static_cast<TMsvId*>(aArg2);
       
   659             event = TFSEventNewMail;
       
   660             TFSMailMsgId mbox;
       
   661 
       
   662             if ( tNew.iServiceId == KMsvLocalServiceIndexEntryIdValue
       
   663                 || tNew.Parent() == KMsvDraftEntryIdValue )
       
   664                 {
       
   665                 TMsvEntry srvEntry;
       
   666                 TMsvId dummy;
       
   667                 if( !iSession )
       
   668                     {
       
   669                     User::Leave( KErrNotReady );
       
   670                     }
       
   671                 iSession->GetEntry( tNew.iServiceId, dummy, srvEntry );
       
   672                 iSession->GetEntry( srvEntry.iRelatedId, dummy, srvEntry );
       
   673                 mbox = SymId2FsId( srvEntry );
       
   674                 }
       
   675             else
       
   676                 {
       
   677                 mbox = SymId2FsId( tNew.iServiceId , tNew.iMtm.iUid );
       
   678                 // start automatic sync
       
   679                 if ( iBasePlugin.IsUnderUiProcess() &&
       
   680                         iBasePlugin.PluginId() == KIpsPlgImap4PluginUidValue )
       
   681                     {
       
   682                     iBasePlugin.PopulateNewMailL(
       
   683                             mbox, tNew.Id(), tNew.Parent() );
       
   684                     }
       
   685                 }
       
   686 
       
   687             // set entries array pointer
       
   688             RArray<TFSMailMsgId> array(KEventGranularity);
       
   689             CleanupClosePushL( array );
       
   690 
       
   691             FillFSMessageArray(
       
   692                 array,
       
   693                 static_cast<const CMsvEntrySelection*>(aArg1),
       
   694                 tNew.iMtm.iUid );
       
   695             arg1 = static_cast<TAny*>(&array);
       
   696 
       
   697             // set parent pointer
       
   698             parent = static_cast<TMsvId*>(aArg2);
       
   699             TFSMailMsgId tfsid( iPluginId, *parent);
       
   700             arg2 = static_cast<TAny*>(&tfsid);
       
   701 
       
   702             SendDelayedEventL( event, mbox,
       
   703                 arg1, arg2 , arg3 );
       
   704             CleanupStack::PopAndDestroy( &array );
       
   705             }
       
   706         else if ( type == KUidMsvFolderEntry )
       
   707             {
       
   708             event = TFSEventNewFolder;
       
   709             mbox = SymId2FsId( tNew.iServiceId, tNew.iMtm.iUid );
       
   710 
       
   711             // set entries array pointer
       
   712             RArray<TFSMailMsgId> array(1);
       
   713             CleanupClosePushL( array );
       
   714             array.Append( SymId2FsId( tNew ) );
       
   715             arg1 = &array;
       
   716 
       
   717             // set parent pointer
       
   718             TMsvId* parent = static_cast<TMsvId*>(aArg2);
       
   719             TFSMailMsgId tfsid( iPluginId, *parent);
       
   720             arg2 = static_cast<TAny*>(&tfsid);
       
   721 
       
   722             SendDelayedEventL( event, mbox,
       
   723                 arg1, arg2 , arg3 );
       
   724 
       
   725             CleanupStack::PopAndDestroy( &array );
       
   726 
       
   727             if( tNew.iMtm.iUid == KSenduiMtmImap4UidValue )
       
   728                 {
       
   729                 SetFolderIdToArrayL( tNew.Id() );
       
   730                 }
       
   731             }
       
   732         else//attachment
       
   733             {
       
   734             return;
       
   735             //do we need to handle this? maybe....
       
   736             }
       
   737 
       
   738         // set null to prevent later usage in framework side
       
   739         // causes kern exec 3 panic...
       
   740         arg1 = arg2 = arg3 = NULL;
       
   741         }
       
   742     }
       
   743 
       
   744 // ----------------------------------------------------------------------------
       
   745 // ----------------------------------------------------------------------------
       
   746 void CIpsPlgEventHandler::HandleEntriesMovedL(
       
   747     TAny* aArg1, TAny* aArg2, TAny* aArg3 )
       
   748     {
       
   749     FUNC_LOG;
       
   750     TUid type;
       
   751     TMsvEntry tMoved;
       
   752     if( IsEventFromIpsSourceL( aArg1, type, tMoved ) )
       
   753         {
       
   754         //sent to draft can cause problems
       
   755         TAny* arg1=NULL;
       
   756         TAny* arg2=NULL;
       
   757         TAny* arg3=NULL;
       
   758 
       
   759         TFSMailEvent event( TFSMailboxUnavailable );
       
   760 
       
   761         RArray<TFSMailMsgId> array(KEventGranularity);
       
   762         CleanupClosePushL( array );
       
   763         FillFSMessageArray(
       
   764             array,
       
   765             static_cast<const CMsvEntrySelection*>(aArg1),
       
   766             tMoved.iMtm.iUid );
       
   767 
       
   768         arg1 = &array;
       
   769         TFSMailMsgId tempId1 = TFSMailMsgId( iPluginId, *(static_cast<TMsvId*>(aArg2) ) ); //target
       
   770         arg2 = &tempId1;
       
   771         TFSMailMsgId tempId2 = TFSMailMsgId( iPluginId, *(static_cast<TMsvId*>(aArg3)) ); //source
       
   772         arg3 = &tempId2;
       
   773 
       
   774         if( tMoved.iType == KUidMsvMessageEntry )
       
   775             {
       
   776             event = TFSEventMailMoved;
       
   777             }
       
   778         else if( tMoved.iType == KUidMsvFolderEntry )
       
   779             {
       
   780             event = TFSEventFoldersMoved;
       
   781             }
       
   782 
       
   783         TFSMailMsgId mbox;
       
   784         // solve mailbox.
       
   785         if ( tMoved.iMtm.iUid == KSenduiMtmSmtpUidValue )
       
   786             {
       
   787             TMsvEntry serv;
       
   788             TMsvId service;
       
   789             iSession->GetEntry( tMoved.iServiceId, service, serv );
       
   790             //get correct imap/pop service to tEntry
       
   791             iSession->GetEntry( serv.iRelatedId, service, serv );
       
   792             mbox = SymId2FsId( serv.iServiceId, serv.iMtm.iUid );
       
   793             }
       
   794         else
       
   795             {
       
   796             mbox = SymId2FsId( tMoved.iServiceId, tMoved.iMtm.iUid );
       
   797             }
       
   798 
       
   799         SendDelayedEventL(
       
   800             event,
       
   801             mbox,
       
   802             arg1,
       
   803             arg2,
       
   804             arg3 );
       
   805 
       
   806         CleanupStack::PopAndDestroy( &array );
       
   807         // set null to prevent later usage in framework side
       
   808         // causes kern exec 3 panic...
       
   809         arg1 = arg2 = arg3 = NULL;
       
   810         }
       
   811     }
       
   812 
       
   813 // ----------------------------------------------------------------------------
       
   814 // ----------------------------------------------------------------------------
       
   815 void CIpsPlgEventHandler::HandleEntriesDeletedL(
       
   816     TAny* aArg1, TAny* aArg2, TAny* /*aArg3*/ )
       
   817     {
       
   818     FUNC_LOG;
       
   819 
       
   820     if ( !aArg1 || !aArg2 )
       
   821         {
       
   822         // sometimes we get null pointers from symbian api
       
   823         return;
       
   824         }
       
   825 
       
   826     TAny* arg1=NULL;
       
   827     TAny* arg2=NULL;
       
   828     TAny* arg3=NULL;
       
   829 
       
   830     TFSMailEvent event( TFSMailboxUnavailable );
       
   831 
       
   832     //parent must be root for it to be mailbox
       
   833     if( (*(TMsvId*) (aArg2)) == KMsvRootIndexEntryIdValue )
       
   834         {
       
   835         CMsvEntrySelection* sel = static_cast<CMsvEntrySelection*>(aArg1);
       
   836         TFSMailMsgId id;
       
   837         TBool found = EFalse;
       
   838         TInt count = iIPSAccounts.Count();
       
   839         TInt selCount = sel->Count();
       
   840 
       
   841         for(TInt i=count-1; i>=0; i--)
       
   842             {
       
   843             TMsvId mboxId = iIPSAccounts[i]->iMBoxId.Id();
       
   844             for (TInt j = 0; j < selCount; j++ )
       
   845                 {
       
   846                 TMsvId deletedId = sel->At(j);
       
   847                 if ( deletedId == mboxId)
       
   848                     {
       
   849                     iIPSAccounts[i];
       
   850                     id.SetId( deletedId );
       
   851                     id.SetPluginId( iIPSAccounts[i]->iMBoxId.PluginId() );
       
   852                     delete iIPSAccounts[i];
       
   853                     iIPSAccounts.Remove(i);
       
   854                     found = ETrue;
       
   855                     break;
       
   856                     }
       
   857                 }
       
   858             }
       
   859 
       
   860         if(!found)
       
   861             {
       
   862             //was some other service, ignore.
       
   863             return;
       
   864             }
       
   865 
       
   866         //check is the id of the deleted entry in our account list.
       
   867 
       
   868         event = TFSEventMailboxDeleted;
       
   869         SendDelayedEventL(
       
   870             event,
       
   871             id,
       
   872             arg1,
       
   873             arg2,
       
   874             arg3 );
       
   875         }
       
   876     else
       
   877         {
       
   878         //aArg2 is a service OR a folder
       
   879         TMsvEntry tEntry;
       
   880         TMsvId service;
       
   881         TBool isFolderId( EFalse );
       
   882         if( !iSession )
       
   883             {
       
   884             User::Leave( KErrNotReady );
       
   885             }
       
   886         iSession->GetEntry( (*(TMsvId*) (aArg2)), service, tEntry );
       
   887 
       
   888         if( tEntry.iMtm.iUid == iBasePlugin.MtmId().iUid )
       
   889             {
       
   890             CMsvEntrySelection* sel = static_cast<CMsvEntrySelection*>(aArg1);
       
   891 	        TMsvId deletedId( KMsvNullIndexEntryIdValue );
       
   892 	        TInt count = sel->Count();
       
   893 
       
   894     		if( count )
       
   895 	            {
       
   896 	            // In case we get several id's, ignore others than
       
   897 	            // last one which is the actual message/root folder id.
       
   898 	            deletedId = sel->At(count-1);
       
   899 	            }
       
   900 
       
   901             // message in remote folder (inbox)
       
   902             TFSMailMsgId mbox(iPluginId, tEntry.iServiceId );
       
   903             RArray<TFSMailMsgId> array( KEventGranularity );
       
   904             CleanupClosePushL( array );
       
   905             TFSMailMsgId parentId( iPluginId, tEntry.Id() );
       
   906 
       
   907             if ( tEntry.iMtm.iUid == KSenduiMtmImap4UidValue )
       
   908                 {
       
   909                 isFolderId = MatchFolderIdFound( deletedId );
       
   910                 }
       
   911             if( !isFolderId )
       
   912                 {
       
   913                 FindCorrectDeletedEntryAndParentL(
       
   914                     tEntry, *sel, parentId, array );
       
   915 
       
   916                 if ( array.Count() > 0 )
       
   917                     {
       
   918                     arg1 = &array;
       
   919                     arg2 = &parentId;
       
   920 
       
   921                     event = TFSEventMailDeleted;
       
   922                     SendDelayedEventL(
       
   923                         event,
       
   924                         mbox,
       
   925                         arg1,
       
   926                         arg2,
       
   927                         arg3 );
       
   928                     }
       
   929                 }
       
   930             else
       
   931                 {
       
   932                 array.Append( SymId2FsId(deletedId, tEntry.iMtm.iUid) );
       
   933                 arg1 = &array;
       
   934                 arg2 = &parentId;
       
   935 
       
   936                 event = TFSEventFoldersDeleted;
       
   937                 SendDelayedEventL(
       
   938                     event,
       
   939                     mbox,
       
   940                     arg1,
       
   941                     arg2,
       
   942                     arg3 );
       
   943                 }
       
   944 
       
   945             CleanupStack::PopAndDestroy( &array );
       
   946             }
       
   947         else if ( tEntry.iMtm.iUid == KSenduiMtmSmtpUidValue )
       
   948             {
       
   949             // seems that case when deleding from draft, symbian
       
   950             // sends event that contains some child part's id of
       
   951             // correct deleted mail. So append parents id to array
       
   952             TFSMailMsgId msg;
       
   953             msg.SetId( tEntry.Id() );
       
   954             TFSMailMsgId parent;
       
   955             parent.SetId( tEntry.Parent() );
       
   956             if( !iSession )
       
   957                 {
       
   958                 User::Leave( KErrNotReady );
       
   959                 }
       
   960             //re-load smtp service to tEntry
       
   961             iSession->GetEntry( tEntry.iServiceId, service, tEntry );
       
   962             //get correct imap/pop service to tEntry
       
   963             iSession->GetEntry( tEntry.iRelatedId, service, tEntry );
       
   964 
       
   965             TFSMailMsgId mbox;
       
   966             mbox.SetId( tEntry.Id() );
       
   967 
       
   968             // set plugin id to msg, parent and mbox
       
   969             if ( tEntry.iMtm.iUid == KSenduiMtmImap4UidValue )
       
   970         		{
       
   971         		parent.SetPluginId( KIpsPlgImap4PluginUid );
       
   972         		msg.SetPluginId( KIpsPlgImap4PluginUid );
       
   973         		mbox.SetPluginId( KIpsPlgImap4PluginUid );
       
   974         		}
       
   975     		else if ( tEntry.iMtm.iUid == KSenduiMtmPop3UidValue )
       
   976         		{
       
   977         		parent.SetPluginId( KIpsPlgPop3PluginUid );
       
   978         		msg.SetPluginId( KIpsPlgPop3PluginUid );
       
   979         		mbox.SetPluginId( KIpsPlgPop3PluginUid );
       
   980         		}
       
   981             else
       
   982                 {
       
   983                 // Should be panic at this point?
       
   984                 return;
       
   985                 }
       
   986 
       
   987             RArray<TFSMailMsgId> array(1);
       
   988             CleanupClosePushL( array );
       
   989             array.Append( msg );
       
   990             arg1 = &array;
       
   991             arg2 = &parent;
       
   992             event = TFSEventMailDeleted;
       
   993             SendDelayedEventL(
       
   994                 event,
       
   995                 mbox,
       
   996                 arg1,
       
   997                 arg2,
       
   998                 arg3 );
       
   999             CleanupStack::PopAndDestroy( &array );
       
  1000             }
       
  1001         else
       
  1002             {
       
  1003             //deletion happened under some other service that IPS.
       
  1004             return;
       
  1005             }
       
  1006         }
       
  1007     // set null to prevent later usage in framework side
       
  1008     // causes kern exec 3 panic...
       
  1009     arg1 = arg2 = arg3 = NULL;
       
  1010     }
       
  1011 
       
  1012 // ----------------------------------------------------------------------------
       
  1013 // ----------------------------------------------------------------------------
       
  1014 void CIpsPlgEventHandler::HandleEntriesChangedL(
       
  1015     TAny* aArg1, TAny* aArg2, TAny* /*aArg3*/ )
       
  1016     {
       
  1017     FUNC_LOG;
       
  1018     TAny* arg1=NULL;
       
  1019     TAny* arg2=NULL;
       
  1020     TAny* arg3=NULL;
       
  1021 
       
  1022     TUid uId;
       
  1023     TMsvEntry tChanged;
       
  1024     if ( !IsEventFromIpsSourceL( aArg1, uId, tChanged ) )
       
  1025         {
       
  1026         return;
       
  1027         }
       
  1028     else if ( tChanged.iMtm.iUid == KSenduiMtmSmtpUidValue )
       
  1029         {
       
  1030         // do not send entry changed events from draft messages
       
  1031         // mess up draft email
       
  1032         return;
       
  1033         }
       
  1034 
       
  1035     TFSMailEvent event = static_cast<TFSMailEvent>( KErrNotFound );
       
  1036 
       
  1037     RArray<TFSMailMsgId> array(1);
       
  1038     CleanupClosePushL( array );
       
  1039 
       
  1040     if ( tChanged.iType == KUidMsvMessageEntry )
       
  1041         {
       
  1042         TMsvEmailEntry emailEntry(tChanged);
       
  1043         if( emailEntry.DeletedIMAP4Flag() /*|| tChanged.Deleted()*/ )
       
  1044             {
       
  1045             event = TFSEventMailDeleted;
       
  1046             }
       
  1047         else
       
  1048             {
       
  1049             event = TFSEventMailChanged;
       
  1050             }
       
  1051 
       
  1052         // message entry
       
  1053         array.Append( SymId2FsId( tChanged )  );
       
  1054         arg1 = &array;
       
  1055 
       
  1056         // parent entry
       
  1057         TFSMailMsgId id = SymId2FsId( *(static_cast<TMsvId*>(aArg2)), tChanged.iMtm.iUid );
       
  1058         arg2 = &id;
       
  1059 
       
  1060         }
       
  1061     else if ( tChanged.iType == KUidMsvFolderEntry )
       
  1062         {
       
  1063         event = TFSEventFolderChanged;
       
  1064 
       
  1065         array.Append( SymId2FsId( tChanged ) );
       
  1066         arg1 = &array;
       
  1067 
       
  1068         TFSMailMsgId id = SymId2FsId( *(static_cast<TMsvId*>(aArg2)), tChanged.iMtm.iUid );
       
  1069         arg2 = &id;
       
  1070 
       
  1071         if ( tChanged.iMtm.iUid == KSenduiMtmImap4UidValue )
       
  1072             {
       
  1073             TMsvEmailEntry eml( tChanged );
       
  1074             TInt index = iImapFolderIds.Find(tChanged.Id());
       
  1075             if ( eml.LocalSubscription() && index == KErrNotFound )
       
  1076                 {
       
  1077                 iImapFolderIds.Append( tChanged.Id() );
       
  1078                 }
       
  1079             else if ( !eml.LocalSubscription() && index != KErrNotFound )
       
  1080                 {
       
  1081                 iImapFolderIds.Remove( index );
       
  1082                 }
       
  1083             }
       
  1084 
       
  1085         }
       
  1086     else if( IsConnectionChangeL( aArg1, aArg2 ) )
       
  1087         {
       
  1088         if ( iIsConnected )
       
  1089             {
       
  1090             event = TFSEventMailboxOnline;
       
  1091             }
       
  1092         else
       
  1093             {
       
  1094             event = TFSEventMailboxOffline;
       
  1095             }
       
  1096         }
       
  1097     else if( IsAccountNameChangeL( aArg1, aArg2 ) )
       
  1098         {
       
  1099         event = TFSEventMailboxRenamed;
       
  1100         }
       
  1101 
       
  1102     // send event only if we found correct one
       
  1103     if ( event != KErrNotFound )
       
  1104         {
       
  1105         TFSMailMsgId mbox;
       
  1106         // solve mailbox.
       
  1107         if ( tChanged.iMtm.iUid == KSenduiMtmSmtpUidValue )
       
  1108             {
       
  1109             TMsvEntry serv;
       
  1110             TMsvId service;
       
  1111             if( !iSession )
       
  1112                 {
       
  1113                 User::Leave( KErrNotReady );
       
  1114                 }
       
  1115             iSession->GetEntry( tChanged.iServiceId, service, serv );
       
  1116             //get correct imap/pop service to tEntry
       
  1117             iSession->GetEntry( serv.iRelatedId, service, serv );
       
  1118             mbox = SymId2FsId( serv.iServiceId, serv.iMtm.iUid );
       
  1119             }
       
  1120         else
       
  1121             {
       
  1122             mbox = SymId2FsId( tChanged.iServiceId, tChanged.iMtm.iUid );
       
  1123             }
       
  1124 
       
  1125         SendDelayedEventL( event,
       
  1126             mbox, arg1, arg2, arg3 );
       
  1127         }
       
  1128     CleanupStack::PopAndDestroy( &array );
       
  1129     // set null to prevent later usage in framework side
       
  1130     // causes kern exec 3 panic...
       
  1131     arg1 = arg2 = arg3 = NULL;
       
  1132     }
       
  1133 //<cmail>
       
  1134 // ----------------------------------------------------------------------------
       
  1135 // ----------------------------------------------------------------------------
       
  1136 void CIpsPlgEventHandler::HandleMediaChangedL( 
       
  1137     TAny* aArg1, TAny* aArg2, TAny* /*aArg3*/ )
       
  1138     {
       
  1139     TAny* arg1=NULL;
       
  1140     TAny* arg2=NULL;
       
  1141     TAny* arg3=NULL;    
       
  1142     TMsvId service;
       
  1143     TMsvEntry tChanged;
       
  1144     TFSMailMsgId mbox;
       
  1145     TFSMailEvent event( TFSEventMailboxDeleted );
       
  1146     TInt startPoint(iIPSAccounts.Count()-1);
       
  1147     TInt from(*(TInt*) (aArg1));
       
  1148     TInt to(*(TInt*) (aArg2));
       
  1149 
       
  1150     if ( !aArg1 || !aArg2 || 
       
  1151          from>KMaxDriveUnitValue || to>KMaxDriveUnitValue )
       
  1152         {
       
  1153         // Possibly duplicate event 
       
  1154         // or some other disinformation
       
  1155         return; 
       
  1156         }
       
  1157     if( !iSession )
       
  1158         {
       
  1159         User::Leave( KErrNotReady );
       
  1160         }
       
  1161 
       
  1162     CMsvEntry* cEntry = iSession->GetEntryL( KMsvRootIndexEntryId );
       
  1163     CleanupStack::PushL( cEntry );
       
  1164     
       
  1165     CMsvEntrySelection* children = cEntry->ChildrenWithMtmL( MtmId() );
       
  1166     CleanupStack::PushL( children );
       
  1167         
       
  1168     for( TInt accIndex(startPoint); accIndex>=0; accIndex-- )
       
  1169         {
       
  1170         // Remove all mailboxes, also the ones possibly added to 
       
  1171         // array from current store to avoid duplicate boxes
       
  1172         mbox.SetId( iIPSAccounts[accIndex]->iMBoxId.Id() );
       
  1173         mbox.SetPluginId( iIPSAccounts[accIndex]->iMBoxId.PluginId() );
       
  1174         iIPSAccounts.Remove(accIndex);
       
  1175         SendDelayedEventL( event, mbox, arg1, arg2, arg3 );
       
  1176         }
       
  1177     for( TInt chdIndex(0); chdIndex<children->Count(); chdIndex++ )
       
  1178         {
       
  1179         // With clear table send mailboxes in current store
       
  1180         // to observers to complete full update
       
  1181         iSession->GetEntry( children->At(chdIndex), service, tChanged );
       
  1182         mbox = SymId2FsId( tChanged );
       
  1183         event = TFSEventNewMailbox;
       
  1184         SendDelayedEventL( event, mbox, arg1, arg2, arg3 );
       
  1185         }
       
  1186 
       
  1187     CleanupStack::PopAndDestroy( 2 );
       
  1188     // set null to prevent later usage in framework side
       
  1189     // causes kern exec 3 panic...
       
  1190     arg1 = arg2 = arg3 = NULL;
       
  1191     }
       
  1192 
       
  1193 // ---------------------------------------------------------------------------
       
  1194 // ---------------------------------------------------------------------------
       
  1195 TUid CIpsPlgEventHandler::MtmId() const
       
  1196     {
       
  1197     TUid ret = KSenduiMtmImap4Uid;
       
  1198     if ( iBasePlugin.MtmId().iUid == KIpsPlgPop3PluginUidValue )
       
  1199         {
       
  1200         ret = KSenduiMtmPop3Uid;
       
  1201         }
       
  1202     return ret;
       
  1203     }
       
  1204 //</cmail>
       
  1205 // ----------------------------------------------------------------------------
       
  1206 // ----------------------------------------------------------------------------    
       
  1207 TBool CIpsPlgEventHandler::IsConnectionChangeL( TAny* aArg1, TAny* aArg2 )
       
  1208     {
       
  1209     FUNC_LOG;
       
  1210     TMsvId parent = (*(TMsvId*) (aArg2));
       
  1211 
       
  1212     //we check that parent is the root. if not, it cannot be a
       
  1213     //event from service, thus can't be from connection change..
       
  1214     if ( parent == KMsvRootIndexEntryId )
       
  1215         {
       
  1216         const CMsvEntrySelection* selection =
       
  1217             static_cast<CMsvEntrySelection*>( aArg1 );
       
  1218 
       
  1219         TMsvEntry tEntry;
       
  1220         TMsvId service;
       
  1221         if( !iSession )
       
  1222             {
       
  1223             User::Leave( KErrNotReady );
       
  1224             }
       
  1225         if ( selection->Count() )
       
  1226             {
       
  1227             iSession->GetEntry( selection->At(0), service, tEntry );
       
  1228             }
       
  1229 
       
  1230         if ( tEntry.Connected() == iIsConnected )
       
  1231             {
       
  1232             return EFalse;
       
  1233             }
       
  1234         else
       
  1235             {
       
  1236             //state has changed, update iIsConnected accordingly.
       
  1237             iIsConnected = tEntry.Connected();
       
  1238             return ETrue;
       
  1239             }
       
  1240         }
       
  1241 
       
  1242     return EFalse;
       
  1243     }
       
  1244 
       
  1245 // ----------------------------------------------------------------------------
       
  1246 // ----------------------------------------------------------------------------
       
  1247 void CIpsPlgEventHandler::MailboxObserversL(
       
  1248     TFSMailMsgId aMailbox,
       
  1249     RPointerArray<MFSMailEventObserver>& aObservers )
       
  1250     {
       
  1251     FUNC_LOG;
       
  1252     TInt count = iMBoxObservers.Count();
       
  1253     for( TInt i=0; i<count; i++ )
       
  1254         {
       
  1255           CMBoxEventObserver* observer = iMBoxObservers[i];
       
  1256           if ( observer && observer->iMBoxId == aMailbox )
       
  1257             {
       
  1258             aObservers.AppendL( &iMBoxObservers[i]->iObserver );
       
  1259             }
       
  1260         }
       
  1261     }
       
  1262 
       
  1263 // ----------------------------------------------------------------------------
       
  1264 // ----------------------------------------------------------------------------
       
  1265 TBool CIpsPlgEventHandler::IsAccountNameChangeL( TAny* aArg1, TAny* aArg2 )
       
  1266     {
       
  1267     FUNC_LOG;
       
  1268     TMsvId parent = (*(TMsvId*) (aArg2));
       
  1269 
       
  1270     TBool ret = EFalse;
       
  1271 
       
  1272     //we check that parent is the root. if not, it cannot be a
       
  1273     //event from service, thus can't be from account name change
       
  1274     if ( parent == KMsvRootIndexEntryId )
       
  1275         {
       
  1276         const CMsvEntrySelection* selection =
       
  1277             static_cast<CMsvEntrySelection*>( aArg1 );
       
  1278 
       
  1279         TMsvEntry tEntry;
       
  1280         TMsvId service;
       
  1281         if( !iSession )
       
  1282             {
       
  1283             User::Leave( KErrNotReady );
       
  1284             }
       
  1285         if ( selection->Count() )
       
  1286             {
       
  1287             iSession->GetEntry( selection->At(0), service, tEntry );
       
  1288             }
       
  1289 
       
  1290         TInt count = iIPSAccounts.Count();
       
  1291         for( TInt i=0; i<count; i++ )
       
  1292             {
       
  1293             if( iIPSAccounts[i]->iMBoxId.Id() == tEntry.Id() )
       
  1294                 {
       
  1295                 if( iIPSAccounts[i]->iName->CompareC( tEntry.iDetails ) != 0 )
       
  1296                     {
       
  1297                     //account has been renamed
       
  1298                     delete iIPSAccounts[i]->iName;
       
  1299                     iIPSAccounts[i]->iName=NULL;
       
  1300                     iIPSAccounts[i]->iName = tEntry.iDetails.AllocL();
       
  1301                     ret = ETrue;
       
  1302                     break;
       
  1303                     }
       
  1304                 }
       
  1305             }
       
  1306         }
       
  1307 
       
  1308     return ret;
       
  1309     }
       
  1310 
       
  1311 // ----------------------------------------------------------------------------
       
  1312 // ----------------------------------------------------------------------------
       
  1313 void CIpsPlgEventHandler::AppendSettingsObserverL(
       
  1314     TFSMailMsgId aAccount,
       
  1315     TUint32 aSettingKey )
       
  1316     {
       
  1317     FUNC_LOG;
       
  1318     // recycle setting observers
       
  1319     TInt find = KErrNotFound;
       
  1320     for ( TInt i = 0; i < iIPSSettingsObservers.Count(); i++ )
       
  1321         {
       
  1322         if ( iIPSSettingsObservers[i]->MailboxId() == aAccount )
       
  1323             {
       
  1324             find = i;
       
  1325             break;
       
  1326             }
       
  1327         }
       
  1328 
       
  1329     RPointerArray<MFSMailEventObserver> observers;
       
  1330     MailboxObserversL( aAccount, observers );
       
  1331 
       
  1332     if ( find != KErrNotFound )
       
  1333         {
       
  1334         iIPSSettingsObservers[find]->SetKeyAndActivateL( aSettingKey, observers );
       
  1335         }
       
  1336     else
       
  1337         {
       
  1338         CIpsPlgSettingsObserver* obs =
       
  1339             CIpsPlgSettingsObserver::NewL( aAccount, *iCenRep, *this );
       
  1340         obs->SetKeyAndActivateL( aSettingKey, observers );
       
  1341         iIPSSettingsObservers.AppendL( obs );
       
  1342         }
       
  1343     observers.Close();
       
  1344     }
       
  1345 
       
  1346 // ----------------------------------------------------------------------------
       
  1347 // ----------------------------------------------------------------------------
       
  1348 void CIpsPlgEventHandler::RemoveSettingsObserver( TFSMailMsgId aMailboxId )
       
  1349     {
       
  1350     FUNC_LOG;
       
  1351     TInt count = iIPSSettingsObservers.Count();
       
  1352     for( TInt i=0; i < count; i++ )
       
  1353         {
       
  1354         if( iIPSSettingsObservers[i]->MailboxId() == aMailboxId )
       
  1355             {
       
  1356             iIPSSettingsObservers[i]->UnActivate();
       
  1357             break;
       
  1358             }
       
  1359         }
       
  1360     }
       
  1361 
       
  1362 // ----------------------------------------------------------------------------
       
  1363 // ----------------------------------------------------------------------------
       
  1364 MFSMailEventObserver* CIpsPlgEventHandler::MailboxObserverL(
       
  1365     TFSMailMsgId aMailbox )
       
  1366     {
       
  1367     FUNC_LOG;
       
  1368     TInt count = iMBoxObservers.Count();
       
  1369     for( TInt i=0; i<count; i++ )
       
  1370         {
       
  1371         if ( iMBoxObservers[i]->iMBoxId == aMailbox )
       
  1372             {
       
  1373             return &iMBoxObservers[i]->iObserver;
       
  1374             }
       
  1375         }
       
  1376     return NULL;
       
  1377     }
       
  1378 
       
  1379 // ----------------------------------------------------------------------------
       
  1380 // ----------------------------------------------------------------------------
       
  1381 TBool CIpsPlgEventHandler::AccountExistsL( const TMsvEntry& aEntry )
       
  1382     {
       
  1383     FUNC_LOG;
       
  1384     if ( !(aEntry.iMtm.iUid == KSenduiMtmImap4UidValue
       
  1385         || aEntry.iMtm.iUid == KSenduiMtmPop3UidValue) )
       
  1386         {
       
  1387         // check only imap and pop
       
  1388         return ETrue;
       
  1389         }
       
  1390 
       
  1391     TInt count = iIPSAccounts.Count();
       
  1392 
       
  1393     for( TInt i=0; i<count; i++ )
       
  1394         {
       
  1395         TInt a = aEntry.Id();
       
  1396         TInt b = iIPSAccounts[i]->iMBoxId.Id();
       
  1397 
       
  1398         if( a == b )
       
  1399             {
       
  1400             return ETrue;
       
  1401             }
       
  1402         }
       
  1403     return EFalse;
       
  1404     }
       
  1405 
       
  1406 // ----------------------------------------------------------------------------
       
  1407 // ----------------------------------------------------------------------------
       
  1408 void CIpsPlgEventHandler::FindCorrectDeletedEntryAndParentL(
       
  1409     const TMsvEmailEntry& aParent,
       
  1410     const CMsvEntrySelection& aDeletedIds,
       
  1411     TFSMailMsgId& aFSParent,
       
  1412     RArray<TFSMailMsgId>& aFSDeletedArray )
       
  1413     {
       
  1414     FUNC_LOG;
       
  1415     if ( aParent.iType.iUid == KUidMsvRootEntryValue )
       
  1416         {
       
  1417         // stop recursion
       
  1418         return;
       
  1419         }
       
  1420 
       
  1421     // only pop or imap types are alloved beyond this point
       
  1422     __ASSERT_DEBUG( ( aParent.iMtm.iUid == KSenduiMtmImap4UidValue ||
       
  1423             aParent.iMtm.iUid == KSenduiMtmPop3UidValue ),
       
  1424         User::Panic( KIpsPlgEventHandlerPanic, KErrGeneral ) );
       
  1425 
       
  1426     TBool doRecursion = EFalse;
       
  1427 
       
  1428     if ( aParent.iMtm.iUid == KSenduiMtmImap4UidValue )
       
  1429         {
       
  1430         if ( aParent.iType.iUid == KUidMsvServiceEntryValue )
       
  1431             {
       
  1432             // return in case of service / root entries to avoid
       
  1433             // forever recursion
       
  1434             return;
       
  1435             }
       
  1436         else if ( aParent.iType.iUid != KUidMsvFolderEntryValue
       
  1437             || !aParent.LocalSubscription() )
       
  1438             {
       
  1439             // some message part is deleted and main message still appears
       
  1440             // on msg store, this case happens when messages are deleted
       
  1441             // in offline state
       
  1442             doRecursion = ETrue;
       
  1443             }
       
  1444         }
       
  1445     else
       
  1446         {
       
  1447         // POP
       
  1448         if ( aParent.iType.iUid != KUidMsvServiceEntryValue )
       
  1449             {
       
  1450             if ( iBasePlugin.GetSyncStateHandler().GetMailboxIpsState(
       
  1451                     aParent.iServiceId ) != KIpsSosEmailSyncStarted )
       
  1452                 {
       
  1453                 // message entries parent must be service in case of POP
       
  1454                 doRecursion = ETrue;
       
  1455                 }
       
  1456             }
       
  1457         }
       
  1458 
       
  1459 
       
  1460     if ( !doRecursion )
       
  1461         {
       
  1462         aFSParent.SetPluginId( TUid::Uid( iPluginId ) );
       
  1463         aFSParent.SetId( aParent.Id() );
       
  1464         for ( TInt i = 0; i < aDeletedIds.Count(); i++ )
       
  1465             {
       
  1466             aFSDeletedArray.Append(
       
  1467                     TFSMailMsgId( iPluginId, aDeletedIds.At(i) ) );
       
  1468             }
       
  1469         }
       
  1470     else
       
  1471         {
       
  1472         TMsvEmailEntry eml;
       
  1473         TMsvId dummy;
       
  1474         if ( iSession && iSession->GetEntry( aParent.Parent(), dummy, eml )
       
  1475                 == KErrNone )
       
  1476             {
       
  1477             CMsvEntrySelection* sel = new ( ELeave ) CMsvEntrySelection();
       
  1478             CleanupStack::PushL( sel );
       
  1479             sel->AppendL( aParent.Id() );
       
  1480             // recursive call
       
  1481             FindCorrectDeletedEntryAndParentL(
       
  1482                 eml, *sel, aFSParent, aFSDeletedArray );
       
  1483             CleanupStack::PopAndDestroy( sel );
       
  1484             }
       
  1485         }
       
  1486     }
       
  1487 
       
  1488 // ----------------------------------------------------------------------------
       
  1489 // ----------------------------------------------------------------------------
       
  1490 void CIpsPlgEventHandler::SignalStartSyncL( const TFSMailMsgId& aAccount )
       
  1491     {
       
  1492     FUNC_LOG;
       
  1493     TFSMailEvent event = TFSEventMailboxSyncStateChanged;
       
  1494     TSSMailSyncState state = StartingSync;
       
  1495 
       
  1496     SaveSyncStatusL( aAccount.Id(), TInt( state ) );
       
  1497 
       
  1498     SendDelayedEventL(
       
  1499         event,
       
  1500         aAccount,
       
  1501         &state,
       
  1502         NULL,
       
  1503         NULL );
       
  1504     }
       
  1505 
       
  1506 // ----------------------------------------------------------------------------
       
  1507 // ----------------------------------------------------------------------------
       
  1508 void CIpsPlgEventHandler::SignalSyncCompletedL(
       
  1509     const TFSMailMsgId& aAccount,
       
  1510     TInt aError )
       
  1511     {
       
  1512     FUNC_LOG;
       
  1513     TFSMailEvent event = TFSEventMailboxSyncStateChanged;
       
  1514 
       
  1515     TSSMailSyncState state = FinishedSuccessfully;
       
  1516 
       
  1517     if ( aError == KErrCancel )
       
  1518         {
       
  1519         state = SyncCancelled;
       
  1520         }
       
  1521     else if ( aError != KErrNone && aError != KErrCancel )
       
  1522         {
       
  1523         state = SyncError;
       
  1524         }
       
  1525 
       
  1526     SaveSyncStatusL( aAccount.Id(), TInt( state ) );
       
  1527 
       
  1528     SendDelayedEventL(
       
  1529         event,
       
  1530         aAccount,
       
  1531         &state,
       
  1532         NULL,
       
  1533         NULL );
       
  1534     }
       
  1535 
       
  1536 // ----------------------------------------------------------------------------
       
  1537 // ----------------------------------------------------------------------------
       
  1538 void CIpsPlgEventHandler::SignalMailboxOfflineStateL(
       
  1539     const TFSMailMsgId& aAccount )
       
  1540     {
       
  1541     FUNC_LOG;
       
  1542     SendDelayedEventL(
       
  1543         TFSEventMailboxOffline,
       
  1544         aAccount,
       
  1545         NULL,
       
  1546         NULL,
       
  1547         NULL );
       
  1548     }
       
  1549 
       
  1550 // ----------------------------------------------------------------------------
       
  1551 // ----------------------------------------------------------------------------
       
  1552 void CIpsPlgEventHandler::SettingsChangedL( TFSMailMsgId /*aAccount*/ )
       
  1553     {
       
  1554     FUNC_LOG;
       
  1555     }
       
  1556 
       
  1557 // ----------------------------------------------------------------------------
       
  1558 // ----------------------------------------------------------------------------
       
  1559 void CIpsPlgEventHandler::SaveSyncStatusL( TMsvId aMailboxId, TInt aState )
       
  1560     {
       
  1561     FUNC_LOG;
       
  1562     TMsvEntry tEntry;
       
  1563     TMsvId service;
       
  1564     if( !iSession )
       
  1565         {
       
  1566         User::Leave( KErrNotReady );
       
  1567         }
       
  1568     TInt err = iSession->GetEntry( aMailboxId, service, tEntry );
       
  1569 
       
  1570     if( err == KErrNone )
       
  1571         {
       
  1572         iSettingsApi->SaveSyncStatusL( tEntry, aState );
       
  1573         }
       
  1574     }
       
  1575 
       
  1576 // ----------------------------------------------------------------------------
       
  1577 // ----------------------------------------------------------------------------
       
  1578 void CIpsPlgEventHandler::HandlePropertyEventL(
       
  1579         TInt aEvent,
       
  1580         TInt aMailbox,
       
  1581         TInt /*aPluginId*/,
       
  1582         TInt aError )
       
  1583     {
       
  1584     FUNC_LOG;
       
  1585     RProcess process;
       
  1586     if ( aEvent == EIPSSosPswErr && process.SecureId() == FREESTYLE_FSSERVER_SID )
       
  1587         {
       
  1588         TFSMailMsgId mbox = SymId2FsId( aMailbox,
       
  1589                         iBasePlugin.MtmId().iUid );
       
  1590         TFSMailEvent event = TFSEventException;
       
  1591         TFsEmailNotifierSystemMessageType msg = EFsEmailNotifErrLoginUnsuccesfull;
       
  1592         SendDelayedEventL( event, mbox, &msg, NULL , (MFSMailExceptionEventCallback*)this );
       
  1593         }
       
  1594     else if ( aEvent == EIPSSosCredientialsSet || aEvent == EIPSSosCredientialsCancelled )
       
  1595         {
       
  1596         if ( iConnOpCallback )
       
  1597             {
       
  1598             iConnOpCallback->CredientialsSetL( aEvent );
       
  1599 
       
  1600             //Set to null after we have used this.
       
  1601             //don't delete, we don't own this.
       
  1602             iConnOpCallback=NULL;
       
  1603             }
       
  1604         //if password was changed, we need to send settings changed event also.
       
  1605         if( aEvent == EIPSSosCredientialsSet )
       
  1606             {
       
  1607             TFSMailMsgId mbox = SymId2FsId( aMailbox,
       
  1608                     iBasePlugin.MtmId().iUid );
       
  1609             TFSMailEvent event = TFSEventMailboxSettingsChanged;
       
  1610             SendDelayedEventL( event, mbox, NULL, NULL , NULL );
       
  1611             }
       
  1612         }
       
  1613     else if ( aEvent == EIPSSosSettingsChanged )
       
  1614         {
       
  1615         TFSMailMsgId mbox = SymId2FsId( aMailbox,
       
  1616                 iBasePlugin.MtmId().iUid );
       
  1617         TFSMailEvent event = TFSEventMailboxSettingsChanged;
       
  1618         SendDelayedEventL( event, mbox, NULL, NULL , NULL );
       
  1619         }
       
  1620     else if ( aEvent == KIpsSosEmailSyncCompleted &&
       
  1621             aError == KErrImapBadLogon )
       
  1622         {
       
  1623         TFSMailMsgId mbox = SymId2FsId( aMailbox,
       
  1624                         iBasePlugin.MtmId().iUid );
       
  1625         TFSMailEvent event = TFSEventException;
       
  1626         TFsEmailNotifierSystemMessageType msg = EFsEmailNotifErrLoginUnsuccesfull;
       
  1627         SendDelayedEventL( event, mbox, &msg, NULL , this );
       
  1628         }
       
  1629     }
       
  1630 
       
  1631 // ----------------------------------------------------------------------------
       
  1632 // ----------------------------------------------------------------------------
       
  1633 void CIpsPlgEventHandler::QueryUsrPassL( TMsvId aMbox, MIpsPlgConnectOpCallback* aCallback )
       
  1634     {
       
  1635     FUNC_LOG;
       
  1636     iConnOpCallback = aCallback;//can be null, doesn't matter.
       
  1637     SetNewPropertyEvent( aMbox, EIPSSosPswErr, 0 );
       
  1638     }
       
  1639 
       
  1640 // ----------------------------------------------------------------------------
       
  1641 // ----------------------------------------------------------------------------
       
  1642 void CIpsPlgEventHandler::SignalCredientialsSetL( TInt aMailboxId, TBool aCancelled )
       
  1643     {
       
  1644     FUNC_LOG;
       
  1645     TInt event = EIPSSosCredientialsSet;
       
  1646     if ( aCancelled )
       
  1647         {
       
  1648         event = EIPSSosCredientialsCancelled;
       
  1649         }
       
  1650 
       
  1651     SetNewPropertyEvent( aMailboxId, event, 0 );
       
  1652     }
       
  1653 
       
  1654 // ----------------------------------------------------------------------------
       
  1655 // ----------------------------------------------------------------------------
       
  1656 void CIpsPlgEventHandler::SetFolderIdToArrayL( TMsvId aNewId )
       
  1657     {
       
  1658     FUNC_LOG;
       
  1659     TBool idInArray( EFalse );
       
  1660 
       
  1661     for( TInt i=0; i<iImapFolderIds.Count(); i++ )
       
  1662         {
       
  1663         if( iImapFolderIds[i] == aNewId )
       
  1664             {
       
  1665             idInArray = ETrue;
       
  1666             }
       
  1667         }
       
  1668     if( !idInArray )
       
  1669         {
       
  1670         iImapFolderIds.AppendL( aNewId );
       
  1671         }
       
  1672     }
       
  1673 // ----------------------------------------------------------------------------
       
  1674 // ----------------------------------------------------------------------------
       
  1675 TBool CIpsPlgEventHandler::MatchFolderIdFound( TMsvId aDeletedId )
       
  1676     {
       
  1677     FUNC_LOG;
       
  1678     TBool match( EFalse );
       
  1679 
       
  1680     for ( TInt i(iImapFolderIds.Count()-1); i >= 0; i-- )
       
  1681         {
       
  1682         if( aDeletedId == iImapFolderIds[i] )
       
  1683             {
       
  1684             iImapFolderIds.Remove( i );
       
  1685             match = ETrue;
       
  1686             }
       
  1687         }
       
  1688     return match;
       
  1689     }
       
  1690 
       
  1691 // ----------------------------------------------------------------------------
       
  1692 // ----------------------------------------------------------------------------
       
  1693 void CIpsPlgEventHandler::CollectSubscribedFoldersL( TMsvId aMailboxId )
       
  1694     {
       
  1695     FUNC_LOG;
       
  1696     if ( iSession )
       
  1697         {
       
  1698         iSettingsApi->GetSubscribedImapFoldersL( aMailboxId, iImapFolderIds );
       
  1699         }
       
  1700     }
       
  1701 
       
  1702 // ----------------------------------------------------------------------------
       
  1703 // ----------------------------------------------------------------------------
       
  1704 void CIpsPlgEventHandler::ExceptionEventCallbackL(
       
  1705         TFSMailMsgId aMailboxId,
       
  1706         TInt /*aEventType*/,
       
  1707         TBool /*aResponse*/ )
       
  1708     {
       
  1709     FUNC_LOG;
       
  1710     TFSMailEvent event = TFSEventMailboxSyncStateChanged;
       
  1711     TSSMailSyncState state = PasswordNotVerified;
       
  1712 
       
  1713     SendDelayedEventL( event, aMailboxId, &state, NULL , NULL );
       
  1714     }
       
  1715 
       
  1716 // End of File
       
  1717