emailservices/emailserver/mailserverautostart/src/fsmailserverautostart.cpp
changeset 0 8466d47a6819
equal deleted inserted replaced
-1:000000000000 0:8466d47a6819
       
     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: This file implements class CFSMailServerAutoStart.
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include "emailtrace.h"
       
    20 #include "emailshutdownconst.h"
       
    21 #include "fsmailserverautostart.h"
       
    22 
       
    23 _LIT( KFSMailServerAutoStartName, "FSMailServerAutoStart" );
       
    24 
       
    25 // Format string for process finder to find processes by executable name
       
    26 _LIT( KFormatProcessFinder, "%S*" );
       
    27 
       
    28 // ======== MEMBER FUNCTIONS ========
       
    29 
       
    30 CFSMailServerAutoStart* CFSMailServerAutoStart::NewL()
       
    31     {
       
    32     FUNC_LOG;
       
    33     CFSMailServerAutoStart *self = new(ELeave) CFSMailServerAutoStart();
       
    34     CleanupStack::PushL( self );
       
    35     self->ConstructL();
       
    36     CleanupStack::Pop( self );
       
    37     return self;
       
    38     }
       
    39 
       
    40 
       
    41 CFSMailServerAutoStart::CFSMailServerAutoStart()
       
    42     {
       
    43     FUNC_LOG;
       
    44     }
       
    45 
       
    46 //----------------------------------------------------------------------//
       
    47 // Currently is chosen that no errors are leaked out of this class
       
    48 // if possible.
       
    49 // In case of failure in timer construction this might not be possible,
       
    50 // but that should be improbable.
       
    51 //----------------------------------------------------------------------//
       
    52 void CFSMailServerAutoStart::ConstructL()
       
    53     {
       
    54     FUNC_LOG;
       
    55 
       
    56     // start fs mail server
       
    57     TRAPD( startError, StartOneApplicationL( KEmailShutdownHandlerExe() ) );
       
    58     if ( startError != KErrNone )
       
    59         {
       
    60         ERROR_1( startError, "EmailServerMonitor start failed, error code: %d", startError );
       
    61         }
       
    62     }
       
    63 
       
    64 // -----------------------------------------------------------------------------
       
    65 // Starts one application by the executable name given as parameter
       
    66 // -----------------------------------------------------------------------------
       
    67 //
       
    68 void CFSMailServerAutoStart::StartOneApplicationL( const TDesC& aAppName ) const
       
    69     {
       
    70     FUNC_LOG;
       
    71     
       
    72     // Start the application only if it's not already running
       
    73     if( !IsProcessRunningL( aAppName ) )
       
    74         {
       
    75         RProcess process;
       
    76         TInt err = process.Create( aAppName, KEmailShutdownHandlerArgRestart );
       
    77         
       
    78         if( err == KErrNone )
       
    79             {
       
    80             TRequestStatus stat = KRequestPending;
       
    81             process.Rendezvous( stat );
       
    82             
       
    83             if( stat != KRequestPending )
       
    84                 {            
       
    85                 ERROR_1( stat.Int(), "RProcess::Rendezvous failed, error code: %d", stat.Int() );
       
    86                 process.Kill( KErrNone );
       
    87                 }
       
    88             else
       
    89                 {            
       
    90                 process.Resume();
       
    91     
       
    92                 User::WaitForRequest( stat );
       
    93                 }
       
    94             }
       
    95         else
       
    96             {
       
    97             ERROR_1( err, "RProcess::Create failed, error code: %d", err );
       
    98             }
       
    99             
       
   100         process.Close();
       
   101         }
       
   102     }
       
   103 
       
   104 // -----------------------------------------------------------------------------
       
   105 // Checks if process with the specified name is running
       
   106 // -----------------------------------------------------------------------------
       
   107 TBool CFSMailServerAutoStart::IsProcessRunningL( const TDesC& aAppName ) const
       
   108     {
       
   109     TFullName fullName;
       
   110     
       
   111     HBufC* findBuf = HBufC::NewLC( aAppName.Length() + KFormatProcessFinder().Length() );
       
   112     findBuf->Des().Format( KFormatProcessFinder, &aAppName );
       
   113 
       
   114     TFindProcess findProcess( *findBuf );
       
   115 
       
   116     TBool found = EFalse;
       
   117     // Loop through all the found processes to check is any of those running
       
   118     while( !found && findProcess.Next( fullName ) == KErrNone )
       
   119         {
       
   120         RProcess process;
       
   121         TInt error = process.Open( findProcess );
       
   122         // Check is the found process alive
       
   123         if( error == KErrNone &&
       
   124             process.ExitType() == EExitPending )
       
   125             {
       
   126             found = ETrue;
       
   127             }
       
   128         process.Close();
       
   129         }
       
   130 
       
   131     CleanupStack::PopAndDestroy( findBuf );
       
   132 
       
   133     return found;
       
   134     }
       
   135 
       
   136 
       
   137 //Main Thread Entry point
       
   138 GLDEF_C TInt E32Main()
       
   139     {
       
   140     RThread::RenameMe( KFSMailServerAutoStartName );
       
   141 
       
   142     // Create the cleanup stack
       
   143     CTrapCleanup* cleanup = NULL;
       
   144     cleanup = CTrapCleanup::New();
       
   145     if ( cleanup == NULL )
       
   146         {
       
   147         // No errors leaked outside, return success.
       
   148         return KErrNone;
       
   149         }
       
   150     
       
   151     CFSMailServerAutoStart* autoStart = NULL;
       
   152 
       
   153     TRAPD( mainError, autoStart = CFSMailServerAutoStart::NewL() );
       
   154     if ( mainError != KErrNone )
       
   155         {
       
   156         ERROR_1( mainError, "EmailServerMonitor start failed, error code: %d", mainError );
       
   157         }
       
   158     
       
   159     // Delete all objects created above.	
       
   160     delete autoStart;
       
   161     delete cleanup;
       
   162 
       
   163     return KErrNone; 
       
   164     } // end E32Main