emailservices/emailserver/cmailhandlerplugin/src/fsmailmtmhandler.cpp
branchRCL_3
changeset 25 3533d4323edc
equal deleted inserted replaced
24:d189ee25cf9d 25:3533d4323edc
       
     1 /*
       
     2 * Copyright (c) 2007-2008 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:  Class to handle mail led turning on.
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 //<cmail>
       
    20 #include "emailtrace.h"
       
    21 #include "cfsmailclient.h"
       
    22 //</cmail>
       
    23 #include <e32base.h> // needed because of the following hrh
       
    24 #include <data_caging_path_literals.hrh> // KDC_MTM_INFO_FILE_DIR
       
    25 #include <mtclreg.h>
       
    26 #include <w32std.h>
       
    27 #include <apgtask.h>
       
    28 //<cmail>
       
    29 #include "cfsclientmtm.h"
       
    30 #include "fsmtmsconstants.h"
       
    31 //</cmail>
       
    32 #include <coemain.h>
       
    33 
       
    34 #include "fsmailmtmhandler.h"
       
    35 
       
    36 
       
    37 // FREESTYLE EMAIL FRAMEWORK INCLUDES
       
    38 //<cmail>
       
    39 #include "cfsmailclient.h"
       
    40 #include "mfsmailbrandmanager.h"
       
    41 //</cmail>
       
    42 
       
    43 
       
    44 #define ARRAY_SIZE(a) sizeof(a)/sizeof(a[0])
       
    45 
       
    46 _LIT( KMessageServerProcessIdentifier, "MSEXE*" );
       
    47 
       
    48 
       
    49 CFSMailMtmHandler* CFSMailMtmHandler::NewL(
       
    50     MFSNotificationHandlerMgr& aOwner )
       
    51     {
       
    52     FUNC_LOG;
       
    53     CFSMailMtmHandler* self =
       
    54         new (ELeave) CFSMailMtmHandler( aOwner );
       
    55     CleanupStack::PushL( self );
       
    56     self->ConstructL();
       
    57     CleanupStack::Pop( self );
       
    58     return self;
       
    59     }
       
    60 
       
    61 CFSMailMtmHandler::CFSMailMtmHandler(
       
    62     MFSNotificationHandlerMgr& aOwner ) :
       
    63     CFSNotificationHandlerBase( aOwner ),
       
    64     iStartingUp( ETrue )
       
    65     {
       
    66     FUNC_LOG;
       
    67     }
       
    68 
       
    69 void CFSMailMtmHandler::ConstructL()
       
    70     {
       
    71     FUNC_LOG;
       
    72     CFSNotificationHandlerBase::ConstructL();
       
    73     
       
    74     TRAPD( installError, InstallMtmL() );
       
    75     if ( installError != KErrNone )
       
    76         {
       
    77         User::Leave( installError );
       
    78         }
       
    79     
       
    80     CFSMailClient* mail = CFSMailClient::NewL();
       
    81 
       
    82     RPointerArray<CFSMailBox> mailBoxes;
       
    83     mailBoxes.Reset();
       
    84 
       
    85     TFSMailMsgId plugin;
       
    86     mail->ListMailBoxes(plugin,mailBoxes);
       
    87     
       
    88     RArray<TFSMailMsgId> mailBoxIds;
       
    89     
       
    90     for(int i=0; i<mailBoxes.Count(); i++)
       
    91     	{
       
    92     	TDesC& name( mailBoxes[i]->GetName() );
       
    93     	TDesC& ownAddress( mailBoxes[i]->OwnMailAddress().GetEmailAddress() );
       
    94     	TRAPD(createError, iMtmClient->CreateAccountL( name, ownAddress, mailBoxes[i]->GetId() ));
       
    95     	if(createError != KErrNone)
       
    96     		{
       
    97     		// Error
       
    98     		} 	
       
    99     	mailBoxIds.Append(mailBoxes[i]->GetId());
       
   100     	}
       
   101     // Remove rendundant accounts
       
   102     TRAPD(removeRedundantError, iMtmClient->RemoveRedundantAccountsL( mailBoxIds ));
       
   103     if(removeRedundantError != KErrNone)
       
   104         {
       
   105         }
       
   106     
       
   107     
       
   108     mailBoxIds.Close();
       
   109     mailBoxes.ResetAndDestroy();
       
   110     mailBoxes.Close();
       
   111 
       
   112     mail->Close();
       
   113     
       
   114     }
       
   115 
       
   116 CFSMailMtmHandler::~CFSMailMtmHandler()
       
   117     {
       
   118     FUNC_LOG;
       
   119     UninstallMtm();
       
   120     }
       
   121 
       
   122 // ---------------------------------------------------------------------------
       
   123 // This is here just to absorb callbacks.
       
   124 // ---------------------------------------------------------------------------
       
   125 //
       
   126 void CFSMailMtmHandler::HandleSessionEventL(
       
   127     TMsvSessionEvent aEvent,
       
   128     TAny* /*aArg1*/,
       
   129     TAny* aArg2,
       
   130     TAny* /*aArg3*/)
       
   131     {
       
   132     FUNC_LOG;
       
   133     
       
   134     // Just being really really sure that the mtm client is created only
       
   135     // when we are in the start up phase.
       
   136     if ( iStartingUp )
       
   137         {
       
   138         // Normally this would have been done after the registry creation.
       
   139         // If NewMtmL is called immediately after registry creation,
       
   140         // it seems to cause panic when active scheduler is started.
       
   141         // This seems to be because the previous DeInstallMtmGroup and
       
   142         // InstallMtmGroup get done only after active scheduler is started.
       
   143         // If they are done at that point, the CFsMtmClient is already
       
   144         // created and the DeInstallMtmGroup seem to cause panic.
       
   145         // The panic is avoided by first waiting for that the deinstall and
       
   146         // install are done before creating the CFsMtmClient.
       
   147         if ( aEvent == EMsvMtmGroupInstalled &&
       
   148              KUidMsgValTypeFsMtmVal == *static_cast<TUid*>(aArg2) )
       
   149             {
       
   150             }
       
   151         }
       
   152     }
       
   153 
       
   154 TBool CFSMailMtmHandler::CapabilitiesToContinueL(
       
   155     TFSMailEvent /*aEvent*/,
       
   156     TFSMailMsgId aMailbox,
       
   157     TAny* /*aParam1*/,
       
   158     TAny* /*aParam2*/,
       
   159     TAny* /*aParam3*/ ) const
       
   160     {
       
   161     FUNC_LOG;
       
   162     CFSMailBox* mailBox( MailClient().GetMailBoxByUidL( aMailbox ) );
       
   163     if ( mailBox == NULL )
       
   164         {
       
   165         User::Leave( KErrArgument );
       
   166         }
       
   167 
       
   168     if ( mailBox->HasCapability( EFSMBoxCapaSymbianMsgIntegration ) )
       
   169         {
       
   170         delete mailBox;
       
   171         return EFalse;
       
   172         }
       
   173     else
       
   174         {
       
   175         delete mailBox;
       
   176         return ETrue;
       
   177         }
       
   178     }
       
   179 
       
   180 void CFSMailMtmHandler::HandleEventL(
       
   181     TFSMailEvent aEvent,
       
   182     TFSMailMsgId aMailbox,
       
   183     TAny* /*aParam1*/,
       
   184     TAny* /*aParam2*/,
       
   185     TAny* /*aParam3*/ )
       
   186     {
       
   187     FUNC_LOG;
       
   188     switch ( aEvent )
       
   189         {
       
   190         case TFSEventMailboxSettingsChanged:
       
   191       	    {
       
   192             // At the moment this event is only for handling the case when
       
   193             // Email address of the mailbox has been changed
       
   194             CFSMailBox* mailBox( MailClient().GetMailBoxByUidL( aMailbox ) );
       
   195             User::LeaveIfNull( mailBox );
       
   196             CleanupStack::PushL( mailBox );
       
   197                     TDesC& ownAddress( mailBox->OwnMailAddress().GetEmailAddress() );
       
   198                     User::LeaveIfError(
       
   199                 iMtmClient->RenameAccountL( KNullDesC, ownAddress, aMailbox ) );
       
   200             CleanupStack::PopAndDestroy( mailBox );
       
   201             break;
       
   202             }
       
   203         case TFSEventNewMailbox:
       
   204             {
       
   205             CFSMailBox* mailBox(
       
   206                 MailClient().GetMailBoxByUidL( aMailbox ) );
       
   207             User::LeaveIfNull( mailBox );
       
   208             CleanupStack::PushL( mailBox );
       
   209             
       
   210             // Branded mailbox name is nowadays set in new mailbox event
       
   211             // handling, so we don't need to use brand manager here anymore.
       
   212             
       
   213             TDesC& name( mailBox->GetName() );
       
   214             TDesC& ownAddress( mailBox->OwnMailAddress().GetEmailAddress() );
       
   215             
       
   216             User::LeaveIfError(
       
   217                 iMtmClient->CreateAccountL( name, ownAddress, aMailbox ) );
       
   218             
       
   219             CleanupStack::PopAndDestroy( mailBox );
       
   220             break;
       
   221             }
       
   222         case TFSEventMailboxDeleted:
       
   223             {
       
   224 
       
   225             // Entry details cannot necessarily be fetched anymore as the
       
   226             // entry might be deleted from the Message Server.
       
   227             User::LeaveIfError(
       
   228                 iMtmClient->DeleteAccountL( KNullDesC, aMailbox ) );
       
   229             
       
   230             break;
       
   231             }
       
   232         case TFSEventMailboxRenamed:
       
   233             {
       
   234             CFSMailBox* mailBox(
       
   235                 MailClient().GetMailBoxByUidL( aMailbox ) );
       
   236             User::LeaveIfNull( mailBox );
       
   237             CleanupStack::PushL( mailBox );
       
   238             TDesC& name( mailBox->GetName() );
       
   239             TDesC& ownAddress( mailBox->OwnMailAddress().GetEmailAddress() );
       
   240 
       
   241             User::LeaveIfError(
       
   242                 iMtmClient->RenameAccountL( name, ownAddress, aMailbox ) );
       
   243             
       
   244             CleanupStack::PopAndDestroy( mailBox );
       
   245             break;
       
   246             }
       
   247         case TFSMailboxAvailable: // Flow through
       
   248         case TFSMailboxUnavailable: // Flow through
       
   249             {
       
   250             break;
       
   251             }
       
   252         default:
       
   253             {
       
   254             break;
       
   255             }
       
   256         }
       
   257     }
       
   258 
       
   259 void CFSMailMtmHandler::InstallMtmL()
       
   260     {
       
   261     FUNC_LOG;
       
   262     iMsvSession = CMsvSession::OpenSyncL( *this );
       
   263 
       
   264     //Construct the path information for the Installation file.    
       
   265     TFileName resourceFileName = InstallationFilenameL();
       
   266 
       
   267 
       
   268     //Install MTMs.
       
   269     TInt err = iMsvSession->InstallMtmGroup( resourceFileName  );
       
   270     if( err!=KErrNone && err!=KErrAlreadyExists  )
       
   271         {
       
   272         User::LeaveIfError( err );
       
   273         }
       
   274     
       
   275     
       
   276     // Create client side mtm account.
       
   277     iMtmRegistry = CClientMtmRegistry::NewL( *iMsvSession );
       
   278 
       
   279 
       
   280     //Jagan: 19th Oct - Added this by looking at TxtMTM Example
       
   281     if( !iMtmRegistry->IsPresent( KUidMsgValTypeFsMtmVal ) )
       
   282         {
       
   283         //Freestyle not present in Client MTM Registry
       
   284         User::Leave( KErrNotFound );
       
   285         }
       
   286 
       
   287     TRAPD( creationError, iMtmClient =
       
   288         (CFsMtmClient*) iMtmRegistry->NewMtmL(
       
   289         KUidMsgValTypeFsMtmVal ); )
       
   290 	if ( creationError != KErrNone )
       
   291     	{
       
   292     	User::Leave( creationError );
       
   293     	}
       
   294 	iStartingUp = EFalse;
       
   295 	// We are now ready to observe mail events
       
   296 	SetObserving( ETrue );
       
   297     }
       
   298     
       
   299 void CFSMailMtmHandler::UninstallMtm()
       
   300     {
       
   301     FUNC_LOG;
       
   302     delete iMtmClient;
       
   303     iMtmClient = NULL;
       
   304     
       
   305     delete iMtmRegistry;
       
   306     iMtmRegistry = NULL;
       
   307     
       
   308     // KILL THE MESSAGE SERVER PROCESS
       
   309     KillProcess( KMessageServerProcessIdentifier );
       
   310     User::After( KOneSecond );
       
   311     
       
   312     // UNINSTALL THE MTM
       
   313 
       
   314     TInt err;
       
   315 
       
   316     //Construct the path information for the Installation file.
       
   317     TFileName resourceFileName;    
       
   318     TRAP( err, resourceFileName = InstallationFilenameL() );
       
   319 
       
   320     if ( err == KErrNone && iMsvSession )
       
   321         {
       
   322         // Uninstall the MTMs.
       
   323         TInt retryCount = 0;
       
   324         do 
       
   325             {
       
   326             err = iMsvSession->DeInstallMtmGroup( resourceFileName  );
       
   327                 if ( err == KErrNone )
       
   328                 {
       
   329                 // success
       
   330                 }
       
   331             else if ( err == KErrInUse )
       
   332                 {
       
   333                 // MTMs in use, killing apps and retry
       
   334                 TRAP_IGNORE( KillAppsL() );
       
   335                 //wait one second
       
   336                 User::After( KOneSecond );
       
   337                 }
       
   338             else
       
   339                 {
       
   340                 // failure
       
   341                 }
       
   342             } while ( err == KErrInUse && retryCount++ < KMAxRetry );
       
   343         }
       
   344      else
       
   345         {
       
   346         // Cannot define the filename or session not created.
       
   347         // Just continue with the session deletion.
       
   348         
       
   349         // Notice: Although deinstallation would fail
       
   350         // (and installation was done earlier) it seems that
       
   351         // crash does not occur even if we continue onwards.
       
   352         }
       
   353     
       
   354     delete iMsvSession;
       
   355     iMsvSession = NULL;
       
   356     }
       
   357 
       
   358 TFileName CFSMailMtmHandler::InstallationFilenameL()
       
   359     {
       
   360     FUNC_LOG;
       
   361     //Construct the path information for the Installation file.
       
   362     TFileName fileName;
       
   363 
       
   364     TFindFile finder( CCoeEnv::Static()->FsSession() );
       
   365     User::LeaveIfError(
       
   366         finder.FindByDir( KFsMtmDirAndFile, KDC_MTM_INFO_FILE_DIR ) );
       
   367     fileName = finder.File();
       
   368 
       
   369     return fileName;
       
   370     }
       
   371 
       
   372 void CFSMailMtmHandler::KillProcess( const TDesC& aName )
       
   373     {
       
   374     FUNC_LOG;
       
   375     TFullName name;
       
   376     TFindProcess find( aName );
       
   377     RProcess process;
       
   378 
       
   379     while ( find.Next( name ) == KErrNone )
       
   380         {
       
   381         if ( process.Open( find ) == KErrNone )
       
   382             {
       
   383             process.Kill( KErrGeneral );
       
   384             process.Close();
       
   385             }
       
   386         } // end while
       
   387     }
       
   388 
       
   389 void CFSMailMtmHandler::KillAppsL()
       
   390     {
       
   391     FUNC_LOG;
       
   392     RWsSession session;
       
   393     User::LeaveIfError( session.Connect() );
       
   394     CleanupClosePushL( session );
       
   395 
       
   396     TInt appCount = ARRAY_SIZE( KAppsToKill );
       
   397 
       
   398     TApaTaskList taskList( session );
       
   399 
       
   400     for ( int i = 0 ; i < appCount ; i++ ) 
       
   401         {
       
   402         TApaTask task = taskList.FindApp( KAppsToKill[i] );
       
   403         if ( task.Exists() )
       
   404             {
       
   405             task.EndTask();
       
   406             }
       
   407         }
       
   408     CleanupStack::PopAndDestroy( &session );
       
   409     }
       
   410 
       
   411 void CFSMailMtmHandler::TurnNotificationOn()
       
   412     {
       
   413     FUNC_LOG;
       
   414     // This is not used. Instead CFsMtmClient is used
       
   415     // directly in HandleEventL.
       
   416     }
       
   417 
       
   418 void CFSMailMtmHandler::TurnNotificationOff()
       
   419     {
       
   420     FUNC_LOG;
       
   421     // This is not used. Instead CFsMtmClient is used
       
   422     // directly in HandleEventL.
       
   423     }
       
   424