iaupdate/IAD/ui/src/iaupdatestarter.cpp
changeset 0 ba25891c3a9e
child 18 f9033e605ee2
equal deleted inserted replaced
-1:000000000000 0:ba25891c3a9e
       
     1 /*
       
     2 * Copyright (c) 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 module contains the implementation of CIAUpdateStarter class 
       
    15 *                member functions.
       
    16 *
       
    17 */
       
    18 
       
    19 
       
    20 //INCLUDES
       
    21 
       
    22 #include <apacmdln.h>
       
    23 #include <apgtask.h>
       
    24 #include <apgcli.h>
       
    25 #include <apgwgnam.h>
       
    26 #include <eikenv.h>
       
    27 
       
    28 #include "iaupdatestarter.h"
       
    29 #include "iaupdatestarterobserver.h"
       
    30 #include "iaupdateutils.h"
       
    31 #include "iaupdateuitimer.h"
       
    32 
       
    33 // ================= MEMBER FUNCTIONS =======================
       
    34 
       
    35 // ---------------------------------------------------------------------------
       
    36 // CIAUpdateStarter::NewL
       
    37 // Two-phased constructor.
       
    38 // ---------------------------------------------------------------------------
       
    39 //
       
    40 CIAUpdateStarter* CIAUpdateStarter::NewL( const TDesC& aCommandLineExecutable,
       
    41                                           const TDesC8& aCommandLineArguments )
       
    42     {
       
    43     CIAUpdateStarter* self = new (ELeave) CIAUpdateStarter();
       
    44     CleanupStack::PushL( self );
       
    45     self->ConstructL( aCommandLineExecutable, aCommandLineArguments );
       
    46     CleanupStack::Pop( self );
       
    47     return self;
       
    48     }
       
    49 
       
    50 
       
    51 // ---------------------------------------------------------------------------
       
    52 // void CIAUpdateStarter::ConstructL
       
    53 // ---------------------------------------------------------------------------
       
    54 //
       
    55 void CIAUpdateStarter::ConstructL( const TDesC& aCommandLineExecutable,
       
    56                                    const TDesC8& aCommandLineArguments )
       
    57     {
       
    58     iCommandLineExecutable = aCommandLineExecutable.AllocL();
       
    59     iCommandLineArguments = aCommandLineArguments.AllocL();
       
    60     }    
       
    61 
       
    62 // ---------------------------------------------------------------------------
       
    63 // CIAUpdateStarter::CIAUpdateStarter
       
    64 // constructor
       
    65 // ---------------------------------------------------------------------------
       
    66 //
       
    67 CIAUpdateStarter::CIAUpdateStarter()  
       
    68     : CActive( CActive::EPriorityStandard )
       
    69     {
       
    70     CActiveScheduler::Add( this );
       
    71     iEikEnv = CEikonEnv::Static();
       
    72     }
       
    73 
       
    74 // ---------------------------------------------------------------------------
       
    75 // CIAUpdateStarter::~CIAUpdateStarter
       
    76 // Destructor
       
    77 // ---------------------------------------------------------------------------
       
    78 //
       
    79 CIAUpdateStarter::~CIAUpdateStarter()
       
    80     {
       
    81     Cancel();
       
    82     delete iProcessStartTimer;
       
    83     delete iCommandLineExecutable;
       
    84     delete iCommandLineArguments;
       
    85     iThread.Close();
       
    86     iProcess.Close();
       
    87     }
       
    88 
       
    89 // ---------------------------------------------------------------------------
       
    90 // CIAUpdateStarter::StartExecutableL
       
    91 // Starts an executable
       
    92 // ---------------------------------------------------------------------------
       
    93 //
       
    94 void CIAUpdateStarter::StartExecutableL( MIAUpdateStarterObserver& aStarterObserver ) 
       
    95     {
       
    96     iStarterObserver = &aStarterObserver;
       
    97     //Close all instances of executable first
       
    98     
       
    99     if ( !iStartNeeded )
       
   100         {
       
   101     	iStarterObserver->StartExecutableCompletedL( KErrNone );
       
   102     	iStarterObserver = NULL;
       
   103         }
       
   104     else
       
   105         {
       
   106         iStartNeeded = EFalse;
       
   107         iUid = TUid::Null();
       
   108         UidForExecutableL( iUid );
       
   109         if ( iUid.iUid == 0 )
       
   110             {
       
   111             iStarterObserver->StartExecutableCompletedL( KErrNotFound );
       
   112             iStarterObserver = NULL;
       
   113             }
       
   114         else
       
   115             {
       
   116     	    iPrevWgId = 0;  
       
   117     	    CApaWindowGroupName::FindByAppUid( iUid, iEikEnv->WsSession(), iPrevWgId );
       
   118 	
       
   119 	        if ( iPrevWgId == KErrNotFound )
       
   120 	            {
       
   121 	   	        LaunchExeL();
       
   122 	            }
       
   123 	        else
       
   124 	            {
       
   125 	            CloseExecutableL();	
       
   126 	            }
       
   127     	    
       
   128             }
       
   129         }
       
   130     }
       
   131 
       
   132 // ---------------------------------------------------------------------------
       
   133 // CIAUpdateStarter::CheckInstalledPackageL
       
   134 // 
       
   135 // ---------------------------------------------------------------------------
       
   136 // 
       
   137 void CIAUpdateStarter::CheckInstalledPackageL( const TUid& aPUid )
       
   138     {
       
   139     if ( iCommandLineExecutable->Length() > 0 )
       
   140         {
       
   141     	if ( !iStartNeeded )
       
   142 	        {
       
   143 		    if ( IAUpdateUtils::IsInstalledL( aPUid, *iCommandLineExecutable ) )
       
   144 		        {
       
   145 		    	iStartNeeded = ETrue;
       
   146 		        }
       
   147 	        }
       
   148         }
       
   149     }
       
   150 
       
   151 
       
   152 // ---------------------------------------------------------------------------
       
   153 // CIAUpdateStarter::BringToForegroundL
       
   154 // Bring started application to foreground
       
   155 // ---------------------------------------------------------------------------
       
   156 //    
       
   157 void CIAUpdateStarter::BringToForegroundL() const
       
   158     {
       
   159     if ( iExeStarted )	
       
   160         {
       
   161         TUid uid( TUid::Null() );
       
   162         UidForExecutableL( uid );
       
   163         
       
   164         if ( uid.iUid != 0 )
       
   165             {
       
   166         	RWsSession ws;   
       
   167                                              
       
   168             if ( ws.Connect() == KErrNone )
       
   169                 {
       
   170                 CleanupClosePushL( ws );        
       
   171                 TApaTaskList tasklist( ws );   
       
   172                  
       
   173       	        TApaTask task = tasklist.FindApp( uid );
       
   174        	        if ( task.Exists() )
       
   175        	            {
       
   176        	            task.BringToForeground();
       
   177                     }
       
   178                                                                         
       
   179                 CleanupStack::PopAndDestroy( &ws );        
       
   180                 }   
       
   181             }
       
   182         }
       
   183     }
       
   184 
       
   185 
       
   186 // ---------------------------------------------------------------------------
       
   187 // CIAUpdateStarter::DoCancel
       
   188 // 
       
   189 // ---------------------------------------------------------------------------
       
   190 // 
       
   191 void CIAUpdateStarter::DoCancel()
       
   192     {
       
   193 	iProcess.LogonCancel( iStatus );
       
   194 	if ( iProcessStartTimer )
       
   195 	    {
       
   196 	    iProcessStartTimer->Cancel();
       
   197 	    }
       
   198     }
       
   199 
       
   200 // ---------------------------------------------------------------------------
       
   201 // CIAUpdateStarter::RunL
       
   202 // 
       
   203 // ---------------------------------------------------------------------------
       
   204 //
       
   205 void CIAUpdateStarter::RunL()
       
   206     {
       
   207     //Check first if there are still running instances
       
   208     CApaWindowGroupName::FindByAppUid( iUid, iEikEnv->WsSession(), iPrevWgId );
       
   209 	
       
   210 	if ( iPrevWgId == KErrNotFound )
       
   211 	    {
       
   212 	    LaunchExeL();
       
   213 	    }
       
   214 	else
       
   215 	    {
       
   216 	    CloseExecutableL();	
       
   217 	    }
       
   218     
       
   219 	
       
   220     }
       
   221 
       
   222 // ---------------------------------------------------------------------------
       
   223 // CIAUpdateStarter::RunError
       
   224 // 
       
   225 // ---------------------------------------------------------------------------
       
   226 //    
       
   227 TInt CIAUpdateStarter::RunError( TInt aError )
       
   228     {
       
   229 	TRAP_IGNORE( iStarterObserver->StartExecutableCompletedL( aError ) );
       
   230     iStarterObserver = NULL;
       
   231     return KErrNone;
       
   232     }
       
   233 
       
   234 
       
   235 // ---------------------------------------------------------------------------
       
   236 // CIAUpdateStarter::LaunchExeL
       
   237 // 
       
   238 // ---------------------------------------------------------------------------
       
   239 // 
       
   240 void CIAUpdateStarter::LaunchExeL()
       
   241     {
       
   242     // One second delay before launching because sometimes launching does not succeed immediately 
       
   243     if ( !iProcessStartTimer )
       
   244         {
       
   245         iProcessStartTimer = CIAUpdateUITimer::NewL( *this, CIAUpdateUITimer::EProcessStartDelay );
       
   246         }
       
   247     else
       
   248         {
       
   249         iProcessStartTimer->Cancel();
       
   250         }
       
   251     iProcessStartTimer->After( 1000000 );
       
   252     }
       
   253 
       
   254 // ---------------------------------------------------------------------------
       
   255 // CIAUpdateStarter::CloseExecutableL
       
   256 // 
       
   257 // ---------------------------------------------------------------------------
       
   258 // 
       
   259 void CIAUpdateStarter::CloseExecutableL() 
       
   260     {
       
   261 	CApaWindowGroupName* wgName = CApaWindowGroupName::NewL( iEikEnv->WsSession() );	
       
   262 	CleanupStack::PushL( wgName );
       
   263 	wgName->ConstructFromWgIdL( iPrevWgId );
       
   264 	TBool respondsToShutdownEvent = wgName->RespondsToShutdownEvent();
       
   265 	CleanupStack::PopAndDestroy( wgName );
       
   266 	if( respondsToShutdownEvent )
       
   267 	    {
       
   268 	    TApaTask task( iEikEnv->WsSession() );
       
   269 	    task.SetWgId( iPrevWgId );
       
   270 
       
   271 	    iThread.Close();
       
   272 	    User::LeaveIfError(iThread.Open(task.ThreadId()));
       
   273 			
       
   274 	    iProcess.Close();
       
   275 	    User::LeaveIfError(iThread.Process(iProcess));
       
   276 	    iProcess.Logon( iStatus );
       
   277 				
       
   278 	    task.SendSystemEvent( EApaSystemEventShutdown );
       
   279         SetActive(); 	
       
   280         }
       
   281     else  //Just give up 
       
   282 	    {
       
   283 	    iStarterObserver->StartExecutableCompletedL( KErrNone );
       
   284         iStarterObserver = NULL;	
       
   285 	    }
       
   286     } 
       
   287 
       
   288 
       
   289 
       
   290 // ---------------------------------------------------------------------------
       
   291 // CIAUpdateStarter::UidForExecutableL
       
   292 // Find Uid for executable
       
   293 // ---------------------------------------------------------------------------
       
   294 //    
       
   295 void CIAUpdateStarter::UidForExecutableL( TUid& aUid ) const
       
   296     {
       
   297     aUid = TUid::Null();
       
   298     RApaLsSession lsSession;
       
   299     User::LeaveIfError( lsSession.Connect() );
       
   300     CleanupClosePushL( lsSession );
       
   301     lsSession.GetAllApps();
       
   302     TApaAppInfo appInfo;
       
   303     TInt err = KErrNone;
       
   304     while ( err == KErrNone )
       
   305         {
       
   306         err = lsSession.GetNextApp( appInfo );
       
   307         TFileName fullName = appInfo.iFullName;	
       
   308         TParse parse;
       
   309         parse.Set( fullName, NULL, NULL);    
       
   310         if ( parse.NameAndExt().CompareF( *iCommandLineExecutable ) == 0 )
       
   311             {
       
   312             aUid = appInfo.iUid;
       
   313           	err = RApaLsSession::ENoMoreAppsInList;
       
   314             }
       
   315         }
       
   316     CleanupStack::PopAndDestroy( &lsSession ); 
       
   317     }
       
   318  
       
   319 // ---------------------------------------------------------------------------
       
   320 // CIAUpdateStarter::ProcessStartDelayComplete
       
   321 // 
       
   322 // ---------------------------------------------------------------------------
       
   323 //
       
   324 void CIAUpdateStarter::ProcessStartDelayComplete( TInt /*aError*/ )
       
   325     {
       
   326     delete iProcessStartTimer;
       
   327     iProcessStartTimer = NULL;
       
   328     TRAPD( err, StartProcessL() );
       
   329     TRAP_IGNORE( iStarterObserver->StartExecutableCompletedL( err ) );
       
   330     iStarterObserver = NULL;
       
   331     }
       
   332 
       
   333 // ---------------------------------------------------------------------------
       
   334 // CIAUpdateStarter::BackgroundDelayComplete
       
   335 // 
       
   336 // ---------------------------------------------------------------------------
       
   337 //
       
   338 void CIAUpdateStarter::BackgroundDelayComplete( TInt /*aError*/ )
       
   339     {
       
   340     
       
   341     }
       
   342 
       
   343 // ---------------------------------------------------------------------------
       
   344 // CIAUpdateStarter::ForegroundDelayComplete
       
   345 // 
       
   346 // ---------------------------------------------------------------------------
       
   347 //
       
   348 void CIAUpdateStarter::ForegroundDelayComplete( TInt /*aError*/ )
       
   349     {
       
   350     
       
   351     }
       
   352 
       
   353 // ---------------------------------------------------------------------------
       
   354 // CIAUpdateStarter::StartProcessL
       
   355 // 
       
   356 // ---------------------------------------------------------------------------
       
   357 //
       
   358 void CIAUpdateStarter::StartProcessL()
       
   359     {
       
   360     RProcess process;
       
   361     TInt err = process.Create( *iCommandLineExecutable, KNullDesC );
       
   362     if ( err != KErrNone )
       
   363         {
       
   364         return; // process is already running or some error, nothing to do than return 
       
   365         }
       
   366     else
       
   367         {
       
   368         CleanupClosePushL( process );
       
   369         CApaCommandLine* commandLine = CApaCommandLine::NewLC();
       
   370         commandLine->SetDocumentNameL( KNullDesC );
       
   371         commandLine->SetExecutableNameL( *iCommandLineExecutable );
       
   372         commandLine->SetCommandL( EApaCommandBackground );
       
   373         if ( iCommandLineArguments->Length() > 0 )
       
   374             {
       
   375             commandLine->SetTailEndL( *iCommandLineArguments );
       
   376             }
       
   377         commandLine->SetProcessEnvironmentL( process );
       
   378         CleanupStack::PopAndDestroy( commandLine );
       
   379 
       
   380         process.Resume();
       
   381         CleanupStack::PopAndDestroy( &process );
       
   382                         
       
   383         iExeStarted = ETrue;
       
   384         }
       
   385     }
       
   386 
       
   387 
       
   388 
       
   389 // End of File