emailuis/emailui/sendasmtm/fsmtms/remove/src/fsremovemtm.cpp
changeset 0 8466d47a6819
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:  UnInstall freestyle MTM account. This exe is executed when 
       
    15 *  Nokia-Email SIS package is uninstalled.
       
    16 *
       
    17 */
       
    18 
       
    19 
       
    20 #include "emailtrace.h"
       
    21 #include <msvapi.h>
       
    22 #include <msvids.h>
       
    23 #include <msvuids.h>
       
    24 #include <apgtask.h>
       
    25 #include <data_caging_path_literals.hrh> 
       
    26 #include "FSEmailBuildFlags.h"
       
    27 #include <featurecontrol.h>                // RFeatureControl
       
    28 #include <centralrepository.h>
       
    29 #include "fsmtmsconstants.h"
       
    30 
       
    31 LOCAL_C TFileName UnInstallationFilenameL();
       
    32 LOCAL_C void KillProcess( const TDesC& aName );
       
    33 LOCAL_C void RemoveAccountsL();
       
    34 LOCAL_C void KillAppsL();
       
    35 LOCAL_C void DoRemoveL(); 
       
    36 LOCAL_C void RemoveL();
       
    37 LOCAL_C void UnDefineFWflag();
       
    38 
       
    39 
       
    40 #define ARRAY_SIZE(a) sizeof(a)/sizeof(a[0])
       
    41 
       
    42 
       
    43 class TDummyObserver : public MMsvSessionObserver
       
    44     {
       
    45 public:
       
    46     void HandleSessionEventL(TMsvSessionEvent /*aEvent*/, TAny* /*aArg1*/, 
       
    47         TAny* /*aArg2*/, TAny* /*aArg3*/) {};
       
    48     };
       
    49 
       
    50 // ---------------------------------------------------------------------------
       
    51 // UnInstallationFilenameL
       
    52 // Finds mtm resource file name path
       
    53 // ---------------------------------------------------------------------------
       
    54 //  
       
    55 LOCAL_C TFileName UnInstallationFilenameL()
       
    56     {
       
    57     FUNC_LOG;
       
    58     //Construct the path information for the Installation file.
       
    59     TFileName fileName;
       
    60     RFs fs;
       
    61     User::LeaveIfError( fs.Connect() );
       
    62     CleanupClosePushL( fs );
       
    63     TFindFile finder( fs );
       
    64     User::LeaveIfError(
       
    65         finder.FindByDir( KFsMtmDirAndFile, KDC_MTM_INFO_FILE_DIR ) );
       
    66     fileName = finder.File();
       
    67     CleanupStack::PopAndDestroy();
       
    68     return fileName;
       
    69     }  
       
    70     
       
    71 // ---------------------------------------------------------------------------
       
    72 // KillProcess
       
    73 // Kill Messaging server if present
       
    74 // ---------------------------------------------------------------------------
       
    75 //    
       
    76 LOCAL_C void KillProcess( const TDesC& aName )
       
    77     {
       
    78     FUNC_LOG;
       
    79     TFullName name;
       
    80     TFindProcess find( aName );
       
    81     RProcess process;
       
    82 
       
    83     while ( find.Next( name ) == KErrNone )
       
    84         {
       
    85         if ( process.Open( find ) == KErrNone )
       
    86             {
       
    87             process.Kill( KErrGeneral );
       
    88             process.Close();
       
    89             }
       
    90         } // end while
       
    91         
       
    92     }
       
    93     
       
    94 // ---------------------------------------------------------------------------
       
    95 // RemoveAccountL
       
    96 // Removes the Nokia-Email accounts from MCE and also from MCE default settings
       
    97 // ---------------------------------------------------------------------------
       
    98 //  
       
    99 LOCAL_C void RemoveAccountsL()
       
   100     {
       
   101     FUNC_LOG;
       
   102 
       
   103     // Open a session to the message server.
       
   104     TDummyObserver ob;
       
   105     CMsvSession* session = CMsvSession::OpenSyncL(ob);    
       
   106     CleanupStack::PushL( session );    
       
   107     
       
   108     CMsvEntry* msvEntry = CMsvEntry::NewL( *session, KMsvRootIndexEntryId, 
       
   109     TMsvSelectionOrdering( KMsvNoGrouping, EMsvSortByNone, ETrue ) );                                       
       
   110     CleanupStack::PushL( msvEntry );
       
   111 
       
   112     msvEntry->SetEntryL( KMsvRootIndexEntryId );
       
   113     // Get Freestyle services.
       
   114     CMsvEntrySelection* childSelection = msvEntry->ChildrenWithMtmL( 
       
   115             KUidMsgValTypeFsMtmVal );
       
   116     CleanupStack::PushL( childSelection );
       
   117     // No Need to do any thing if there are no accounts.
       
   118     if(childSelection->Count() > 0)
       
   119     	{
       
   120 		TMsvId accountId = NULL;
       
   121         // Remove accounts in a loop
       
   122 		for( TInt i = 0; i < childSelection->Count(); i++ )
       
   123 	        {
       
   124 	        msvEntry->SetEntryL( childSelection->At( i ) );
       
   125 	        if ( msvEntry->Entry().iMtm == KUidMsgValTypeFsMtmVal )
       
   126 	            {
       
   127 	            accountId = msvEntry->Entry().Id();
       
   128 	            msvEntry->SetEntryL( KMsvRootIndexEntryId );
       
   129 	            msvEntry->DeleteL( accountId );
       
   130 	            accountId = NULL;
       
   131 	            }
       
   132 	        }
       
   133     	}
       
   134     else
       
   135     	{
       
   136     	}
       
   137 	    CleanupStack::PopAndDestroy( childSelection ); 
       
   138 	    CleanupStack::PopAndDestroy( msvEntry );
       
   139 	    CleanupStack::PopAndDestroy( session ); 
       
   140 }
       
   141 
       
   142 // ---------------------------------------------------------------------------
       
   143 // KillAppsL
       
   144 // ---------------------------------------------------------------------------
       
   145 //  
       
   146 LOCAL_C void KillAppsL()
       
   147     {
       
   148     FUNC_LOG;
       
   149 
       
   150     RWsSession session;
       
   151     User::LeaveIfError( session.Connect() );
       
   152     CleanupClosePushL( session );
       
   153 
       
   154     TInt appCount = ARRAY_SIZE( KAppsToKill );
       
   155 
       
   156     TApaTaskList taskList( session );
       
   157 
       
   158     for ( int i = 0 ; i < appCount ; i++ ) 
       
   159         {
       
   160         TApaTask task = taskList.FindApp( KAppsToKill[i] );
       
   161         if ( task.Exists() )
       
   162             {
       
   163             task.EndTask();
       
   164             }
       
   165         }
       
   166     CleanupStack::PopAndDestroy( &session );
       
   167 
       
   168     }
       
   169 
       
   170 // ---------------------------------------------------------------------------
       
   171 // RemoveL
       
   172 // Remove accounts and then Deinstall Nokia-Email
       
   173 // ---------------------------------------------------------------------------
       
   174 //  
       
   175 LOCAL_C void RemoveL()
       
   176     {       
       
   177     FUNC_LOG; 
       
   178 
       
   179     TDummyObserver ob;
       
   180     CMsvSession* session = CMsvSession::OpenSyncL( ob );
       
   181     CleanupStack::PushL( session );
       
   182     // Remove the accounts.
       
   183     RemoveAccountsL();
       
   184     
       
   185     //Construct the path information for the Installation file.            
       
   186     TFileName resourceFileName = UnInstallationFilenameL();
       
   187  
       
   188     // Kill the message server process
       
   189     KillProcess( _L( "MSEXE*" ) );
       
   190     User::After( KOneSecond );    
       
   191     // Uninstall the MTMs.
       
   192     TInt retryCount = 0;
       
   193     TInt err = 0;
       
   194     do 
       
   195         {
       
   196         err = session->DeInstallMtmGroup( resourceFileName );
       
   197         if ( err == KErrNone )
       
   198             {
       
   199             }
       
   200         else if ( err == KErrInUse )
       
   201             {
       
   202             TRAP_IGNORE( KillAppsL() );
       
   203             //wait one second
       
   204             User::After( KOneSecond );
       
   205             }
       
   206         else
       
   207         {
       
   208         }
       
   209     } while ( err == KErrInUse && retryCount++ < KMAxRetry );
       
   210 
       
   211     CleanupStack::PopAndDestroy( session ); 
       
   212     // Remove all Nokia-Email accounts after uninstalling MTM
       
   213     //RemoveAccountsL();
       
   214     
       
   215     } 
       
   216 
       
   217 // ---------------------------------------------------------------------------
       
   218 // UnDefineFWflag
       
   219 // Framework flag will be undefined.
       
   220 // ---------------------------------------------------------------------------
       
   221 //  
       
   222 LOCAL_C void UnDefineFWflag() 
       
   223     { 
       
   224     FUNC_LOG; 
       
   225     RFeatureControl featureControl; 
       
   226     TInt err = featureControl.Connect(); 
       
   227     if ( err == KErrNone )
       
   228         { 
       
   229         featureControl.Close(); 
       
   230         } 
       
   231     }
       
   232 
       
   233 LOCAL_C void UnSetCenrepParams()
       
   234 	{
       
   235 	}
       
   236 
       
   237 // ---------------------------------------------------------------------------
       
   238 // DoRemoveL
       
   239 // ---------------------------------------------------------------------------
       
   240 //  
       
   241 LOCAL_C void DoRemoveL() 
       
   242     {    
       
   243     FUNC_LOG;
       
   244     
       
   245     // Undfine FW flag
       
   246     UnDefineFWflag();
       
   247     UnSetCenrepParams();
       
   248     // Construct and install the active scheduler
       
   249     CActiveScheduler* scheduler = new (ELeave) CActiveScheduler;
       
   250     CleanupStack::PushL( scheduler );
       
   251     CActiveScheduler::Install( scheduler );
       
   252 
       
   253     TRAP_IGNORE(RemoveL());
       
   254     
       
   255     CleanupStack::PopAndDestroy( scheduler );
       
   256     
       
   257     } 
       
   258 
       
   259 // ---------------------------------------------------------------------------
       
   260 // E32Main
       
   261 // ---------------------------------------------------------------------------
       
   262 //  
       
   263 GLDEF_C TInt E32Main()
       
   264     {
       
   265     FUNC_LOG;
       
   266     __UHEAP_MARK;
       
   267     
       
   268     CTrapCleanup* cleanup=CTrapCleanup::New(); 
       
   269     
       
   270     TRAP_IGNORE( DoRemoveL() );
       
   271     
       
   272     delete cleanup; 
       
   273     
       
   274     __UHEAP_MARKEND;
       
   275     return 0; 
       
   276     } 
       
   277 
       
   278 // End of file