htiui/HtiServicePlugins/HtiAppServicePlugin/src/HtiAppControl.cpp
branchRCL_3
changeset 20 48060abbbeaf
parent 19 d40e813b23c0
child 21 b3cee849fa46
equal deleted inserted replaced
19:d40e813b23c0 20:48060abbbeaf
     1 /*
       
     2 * Copyright (c) 2009 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:  HtiAppControl implementation
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 // INCLUDE FILES
       
    20 #include <apacmdln.h>
       
    21 #include <apgtask.h>
       
    22 #include <apgwgnam.h>
       
    23 #include <apmstd.h>
       
    24 
       
    25 #include <badesca.h>
       
    26 #include <f32file.h>
       
    27 
       
    28 #include "HtiAppControl.h"
       
    29 #include <HtiDispatcherInterface.h>
       
    30 #include <HtiLogging.h>
       
    31 
       
    32 #if ( SYMBIAN_VERSION_SUPPORT < SYMBIAN_4 )
       
    33 #include <SWInstApi.h>
       
    34 #include <swi/sisregistrysession.h>
       
    35 #include <swi/sisregistrypackage.h>
       
    36 #else
       
    37 #include <usif/sif/sif.h>
       
    38 #include <usif/scr/scr.h>    //RSoftwareComponentRegistry
       
    39 #endif
       
    40 #include <javaregistryincludes.h>
       
    41 #include <WidgetRegistryClient.h>
       
    42 
       
    43 using namespace Java;
       
    44 #if ( SYMBIAN_VERSION_SUPPORT >= SYMBIAN_4 )
       
    45 _LIT8( KSisxMimeType, "x-epoc/x-sisx-app" );
       
    46 _LIT8( KSisMimeType, "application/vnd.symbian.install" );
       
    47 _LIT8( KPipMimeType, "application/x-pip" );
       
    48 _LIT8( KJadMIMEType, "text/vnd.sun.j2me.app-descriptor" );
       
    49 _LIT8( KJarMIMEType, "application/java-archive" );
       
    50 _LIT8( KJavaMIMEType, "application/java");
       
    51 _LIT8( KJarxMIMEType, "application/x-java-archive");
       
    52 #endif
       
    53 
       
    54 // CONSTANTS
       
    55 const static TUid KAppServiceUid = { 0x1020DEC7 }; //This is Uid of AppServiceOS
       
    56 
       
    57 //error descriptions
       
    58 _LIT8( KErrDescrNoMemory, "No memory" );
       
    59 _LIT8( KErrDescrInvalidCmd, "Invalid command" );
       
    60 _LIT8( KErrDescrInvalidArguments, "Invalid arguments" );
       
    61 _LIT8( KErrDescrFailedCreateProcess, "Failed create process" );
       
    62 _LIT8( KErrDescrNotSupported, "Command not supported" );
       
    63 
       
    64 _LIT8( KErrDescrFailedStartApp, "Failed start app" );
       
    65 _LIT8( KErrDescrInvalidUid, "Invalid application uid" );
       
    66 _LIT8( KErrDescrFailedFindApp, "Failed find app" );
       
    67 
       
    68 _LIT8( KErrDescrFailedConnectSilentInstaller, "Failed to connect to silent installer" );
       
    69 _LIT8( KErrDescrFailedInstall, "Failed to install" );
       
    70 _LIT8( KErrDescrFailedUnInstall, "Failed to uninstall" );
       
    71 _LIT8( KErrDescrFailedFindPackage, "Failed to find the package" );
       
    72 #if ( SYMBIAN_VERSION_SUPPORT >= SYMBIAN_4 )
       
    73 _LIT8( KErrDescrBadComponentId, "Bad component id");
       
    74 #endif
       
    75 _LIT8( KErrDescrFailedListInstApps, "Failed to list installed apps" );
       
    76 
       
    77 const static TUint8 KUnicodeMask = 0x01;
       
    78 const static TInt KTerminateReason = 0;
       
    79 const static TInt KTUintLength = sizeof(TUint);
       
    80 const static TInt KCloseTaskDelay = 250000; // microseconds
       
    81 
       
    82 // MACROS
       
    83 
       
    84 // LOCAL CONSTANTS AND MACROS
       
    85 
       
    86 // MODULE DATA STRUCTURES
       
    87 
       
    88 // LOCAL FUNCTION PROTOTYPES
       
    89 
       
    90 // FORWARD DECLARATIONS
       
    91 
       
    92 // ============================ MEMBER FUNCTIONS ===============================
       
    93 CHtiAppControl* CHtiAppControl::NewL()
       
    94     {
       
    95     CHtiAppControl* self = new ( ELeave ) CHtiAppControl;
       
    96     CleanupStack::PushL( self );
       
    97     self->ConstructL();
       
    98     CleanupStack::Pop();
       
    99     return self;
       
   100     }
       
   101 
       
   102 // Constructor
       
   103 CHtiAppControl::CHtiAppControl():iIsAppCmdsSupported( EFalse )
       
   104     {
       
   105     }
       
   106 
       
   107 EXPORT_C CHtiAppControl* NewHtiAppControl()
       
   108     {
       
   109     return CHtiAppControl::NewL();
       
   110     }
       
   111 
       
   112 CHtiAppControl::~CHtiAppControl()
       
   113     {
       
   114     HTI_LOG_FUNC_IN( "~CHtiAppControl" );
       
   115 
       
   116     for ( TInt i = 0; i < iProcessHandleArray.Count(); i++ )
       
   117         {
       
   118         iProcessHandleArray[i].Close();
       
   119         }
       
   120     iProcessHandleArray.Close();
       
   121 
       
   122     iAppServer.Close();
       
   123     iWs.Close();
       
   124 
       
   125     if ( iMimeTypes )
       
   126         {
       
   127         iMimeTypes->Reset();
       
   128         delete iMimeTypes;
       
   129         }
       
   130 
       
   131 #if ( SYMBIAN_VERSION_SUPPORT < SYMBIAN_4 )
       
   132     iAugmentations.ResetAndDestroy();
       
   133 #endif
       
   134 
       
   135     HTI_LOG_FUNC_OUT( "~CHtiAppControl" );
       
   136     }
       
   137 
       
   138 // Second phase construction.
       
   139 void CHtiAppControl::ConstructL()
       
   140     {
       
   141     HTI_LOG_FUNC_IN( "CHtiAppControl::ConstructL" );
       
   142     iMimeTypes = new (ELeave) CDesC8ArrayFlat( 8 );
       
   143 #if ( SYMBIAN_VERSION_SUPPORT < SYMBIAN_4 )
       
   144     iMimeTypes->AppendL( SwiUI::KSisxMimeType() );
       
   145     iMimeTypes->AppendL( SwiUI::KSisMimeType() );
       
   146     iMimeTypes->AppendL( SwiUI::KPipMimeType() );
       
   147     iMimeTypes->AppendL( SwiUI::KJadMIMEType() );
       
   148     iMimeTypes->AppendL( SwiUI::KJarMIMEType() );
       
   149     iMimeTypes->AppendL( SwiUI::KJavaMIMEType() );
       
   150     iMimeTypes->AppendL( SwiUI::KJarxMIMEType() );
       
   151 #else
       
   152     iMimeTypes->AppendL( KSisxMimeType() );
       
   153     iMimeTypes->AppendL( KSisMimeType() );
       
   154     iMimeTypes->AppendL( KPipMimeType() );
       
   155     iMimeTypes->AppendL( KJadMIMEType() );
       
   156     iMimeTypes->AppendL( KJarMIMEType() );
       
   157     iMimeTypes->AppendL( KJavaMIMEType() );
       
   158     iMimeTypes->AppendL( KJarxMIMEType() );
       
   159 #endif
       
   160     iMimeTypes->AppendL( KWidgetMime() ); // from widgetregistryconstants.h
       
   161 #if ( SYMBIAN_VERSION_SUPPORT < SYMBIAN_4 )
       
   162     iAugmentationIndex = 0;
       
   163 #endif
       
   164 
       
   165     TInt err = iAppServer.Connect();
       
   166     if ( err == KErrNone )
       
   167         {
       
   168         err = iWs.Connect();
       
   169         }
       
   170     if ( err == KErrNone )
       
   171         {
       
   172         iIsAppCmdsSupported = ETrue;
       
   173         }
       
   174     else
       
   175         {
       
   176         iAppServer.Close();
       
   177         }
       
   178     HTI_LOG_FUNC_OUT( "CHtiAppControl::ConstructL" );
       
   179     }
       
   180 
       
   181 TInt CHtiAppControl::ParseString( const TDesC8& aRequest,
       
   182                                         TInt anOffset,
       
   183                                         TBool aUnicode,
       
   184                                         TDes& aResult )
       
   185     {
       
   186     HTI_LOG_FUNC_IN( "CHtiAppControl::ParseString" );
       
   187     //validate parameters
       
   188     //if offset outside the string return empty string
       
   189     if ( anOffset >= aRequest.Size() )
       
   190         {
       
   191         return anOffset;
       
   192         }
       
   193 
       
   194     TInt len = aRequest[anOffset];
       
   195     HTI_LOG_FORMAT( "length %d", len );
       
   196 
       
   197     if ( len > aResult.MaxLength() )
       
   198         {
       
   199         return KErrBadDescriptor;
       
   200         }
       
   201 
       
   202     TInt nextOffset = ( aUnicode ? len * 2 : len ) + anOffset + 1;
       
   203     HTI_LOG_FORMAT( "nextOffset %d", nextOffset );
       
   204     HTI_LOG_FORMAT( "reqSize %d", aRequest.Size() );
       
   205     if ( nextOffset > aRequest.Size() )
       
   206         {
       
   207         return KErrArgument;
       
   208         }
       
   209 
       
   210     if ( aUnicode )
       
   211         {
       
   212         const TPtrC8 aFrom( aRequest.Mid( anOffset + 1, len * 2 ) );
       
   213         aResult.SetLength( len );
       
   214         for ( TInt i = 0; i < len; ++i )
       
   215             {
       
   216             aResult[i] = ( TUint16 ) aFrom[i << 1] +
       
   217                 ( ( ( TUint16 ) aFrom[( i << 1 ) + 1] ) << 8 );
       
   218             }
       
   219         }
       
   220     else
       
   221         {
       
   222         aResult.Copy( aRequest.Mid( anOffset + 1, len ) );
       
   223         }
       
   224 
       
   225     HTI_LOG_FUNC_OUT( "CHtiAppControl::ParseString" );
       
   226     return nextOffset;
       
   227     }
       
   228 
       
   229 void CHtiAppControl::ProcessMessageL( const TDesC8& aMessage,
       
   230                 THtiMessagePriority /*aPriority*/ )
       
   231     {
       
   232     HTI_LOG_FUNC_IN( "CHtiAppControl::ProcessMessage" );
       
   233     if ( aMessage.Length() < 1 )
       
   234         {
       
   235         // no command
       
   236         SendErrorMsg( KErrArgument, KErrDescrInvalidCmd );
       
   237         return;
       
   238         }
       
   239 
       
   240     if ( aMessage.Length() < 2 &&
       
   241          aMessage[0] !=  EListInstalledApps &&
       
   242          aMessage[0] !=  EListInstalledApps_u)
       
   243         {
       
   244          // parameter is required with all commands except
       
   245          // listing processes or installed applications
       
   246         SendErrorMsg( KErrArgument, KErrDescrInvalidCmd );
       
   247         return;
       
   248         }
       
   249 
       
   250     HTI_LOG_FORMAT( "cmd %d", aMessage[0] );
       
   251 
       
   252     if ( aMessage[0] > EProcessLastCommand &&
       
   253               aMessage[0] < EAppLastCommand)
       
   254         {
       
   255         if ( iIsAppCmdsSupported )
       
   256             {
       
   257             HandleAppControlL( aMessage );
       
   258             }
       
   259         else
       
   260             {
       
   261             SendErrorMsg( KErrNotSupported, KErrDescrNotSupported );
       
   262             }
       
   263         }
       
   264     else if ( aMessage[0] > EAppLastCommand &&
       
   265               aMessage[0] < ESisLastCommand )
       
   266         {
       
   267         HandleInstallerControlL( aMessage );
       
   268         }
       
   269     else
       
   270         {
       
   271         SendErrorMsg( KErrArgument, KErrDescrInvalidCmd );
       
   272         }
       
   273     HTI_LOG_FUNC_OUT( "CHtiAppControl::ProcessMessage" );
       
   274     }
       
   275 
       
   276 void CHtiAppControl::HandleAppControlL( const TDesC8& aMessage )
       
   277     {
       
   278     HTI_LOG_FUNC_IN( "CHtiAppControl::HandleAppControl" );
       
   279 
       
   280     TBool unicode = aMessage[0] & KUnicodeMask;
       
   281 
       
   282     HTI_LOG_FORMAT( "unicode %d", unicode );
       
   283 
       
   284     TApaAppInfo appInfo;
       
   285     TFileName appName;
       
   286     TFileName docName;
       
   287 
       
   288     switch ( aMessage[0] )
       
   289         {
       
   290         case EStartApp_uid:
       
   291         case EStartApp_uid_u:
       
   292             {
       
   293             TPtrC8 parameters = aMessage.Mid( 1 );
       
   294             if ( parameters.Length() >= 4 )
       
   295                 {
       
   296                 TInt32 uid = Parse32<TInt32>( parameters );
       
   297 
       
   298                 TInt offset = ParseString( parameters, 4, unicode, docName );
       
   299                 if ( offset >= 0)
       
   300                     {
       
   301                     if ( FindAppL( appInfo, uid ) )
       
   302                         {
       
   303                         HandleStartAppL( appInfo, docName );
       
   304                         }
       
   305                     }
       
   306                 else
       
   307                     {
       
   308                     SendErrorMsg( offset , KErrDescrInvalidArguments );
       
   309                     }
       
   310                 }
       
   311             else
       
   312                 {
       
   313                 SendErrorMsg( KErrArgument , KErrDescrInvalidUid );
       
   314                 }
       
   315             }
       
   316             break;
       
   317         case EStartApp:
       
   318         case EStartApp_u:
       
   319             {
       
   320             TInt offset = ParseString( aMessage, 1, unicode, appName );
       
   321             if ( offset >= 0 )
       
   322                 {
       
   323                 offset = ParseString( aMessage, offset, unicode, docName );
       
   324                 if ( offset >= 0 )
       
   325                     {
       
   326                     //find appInfo by app full name
       
   327                     if ( FindAppL( appInfo, appName ) )
       
   328                         {
       
   329                         HTI_LOG_TEXT( "call HandleStartAppL" );
       
   330                         HandleStartAppL( appInfo, docName );
       
   331                         }
       
   332                     }
       
   333                 }
       
   334 
       
   335             if ( offset < 0 )
       
   336                 {
       
   337                 SendErrorMsg( offset , KErrDescrInvalidArguments );
       
   338                 }
       
   339             }
       
   340             break;
       
   341         case EStartDoc:
       
   342         case EStartDoc_u:
       
   343             {
       
   344             TInt offset = ParseString( aMessage, 1, unicode, docName );
       
   345             if ( offset >= 0 )
       
   346                 {
       
   347                 HandleStartAppL( appInfo, docName ); //appInfo is empty
       
   348                 }
       
   349             else
       
   350                 {
       
   351                 SendErrorMsg( offset , KErrDescrInvalidArguments );
       
   352                 }
       
   353             }
       
   354             break;
       
   355         case EStatusApp_uid:
       
   356             {
       
   357             TPtrC8 appUid8 = aMessage.Mid( 1 );
       
   358             if ( appUid8.Length() == 4 )
       
   359                 {
       
   360                 TApaTaskList tl( iWs );
       
   361                 TInt32 appUid = Parse32<TInt32>( appUid8 );
       
   362                 TApaTask task = tl.FindApp( TUid::Uid( appUid ) );
       
   363                 if ( task.Exists() )
       
   364                     {
       
   365                     SendMessageL( ERunning );
       
   366                     }
       
   367                 else
       
   368                     {
       
   369                     SendMessageL( ENotFound );
       
   370                     }
       
   371                 }
       
   372             else
       
   373                 {
       
   374                 SendErrorMsg( KErrArgument, KErrDescrInvalidUid );
       
   375                 }
       
   376             }
       
   377             break;
       
   378         case EStatusApp:
       
   379         case EStatusApp_u:
       
   380             {
       
   381             TInt offset = ParseString( aMessage, 1, unicode, appName );
       
   382             if ( offset >= 0 )
       
   383                 {
       
   384                 TApaTaskList tl( iWs );
       
   385                 TApaTask task = tl.FindApp( appName );
       
   386                 if ( task.Exists() )
       
   387                     {
       
   388                     SendMessageL( ERunning );
       
   389                     break; // we are done
       
   390                     }
       
   391 
       
   392                 // Maybe the user gave path or filename instead of caption
       
   393                 if ( FindAppL( appInfo, appName ) )
       
   394                     {
       
   395                     TApaTask task2 = tl.FindApp( appInfo.iUid );
       
   396                     if ( task2.Exists() )
       
   397                         {
       
   398                         SendMessageL( ERunning );
       
   399                         }
       
   400                     else
       
   401                         {
       
   402                         SendMessageL( ENotFound );
       
   403                         }
       
   404                     }
       
   405                 // If app was not found by FindAppL method, a response message
       
   406                 // has already been sent there - no need to send anything here.
       
   407                 }
       
   408             else
       
   409                 {
       
   410                 SendErrorMsg( offset , KErrDescrInvalidArguments );
       
   411                 }
       
   412             }
       
   413             break;
       
   414         case EStatusDoc:
       
   415         case EStatusDoc_u:
       
   416             {
       
   417             TInt offset = ParseString( aMessage, 1, unicode, docName );
       
   418             if ( offset >= 0 )
       
   419                 {
       
   420                 TApaTaskList tl( iWs );
       
   421                 TApaTask task = tl.FindDoc( docName );
       
   422                 if ( task.Exists() )
       
   423                     {
       
   424                     SendMessageL( ERunning );
       
   425                     }
       
   426                 else
       
   427                     {
       
   428                     SendMessageL( ENotFound );
       
   429                     }
       
   430                 }
       
   431             else
       
   432                 {
       
   433                 SendErrorMsg( offset , KErrDescrInvalidArguments );
       
   434                 }
       
   435             }
       
   436             break;
       
   437         case EStopApp:
       
   438         case EStopApp_u:
       
   439             {
       
   440             TInt offset = ParseString( aMessage, 1, unicode, appName );
       
   441             if ( offset >= 0 )
       
   442                 {
       
   443                 TApaTaskList tl( iWs );
       
   444                 TApaTask task = tl.FindApp( appName );
       
   445                 if ( task.Exists() )
       
   446                     {
       
   447                     task.EndTask();
       
   448                     User::After( KCloseTaskDelay );
       
   449                     TApaTask task = tl.FindApp( appName );
       
   450                     if ( task.Exists() )
       
   451                         {
       
   452                         task.KillTask();
       
   453                         }
       
   454                     SendMessageL( EOk );
       
   455                     break; // we are done
       
   456                     }
       
   457 
       
   458                 // Maybe the user gave path or filename instead of caption
       
   459                 if ( FindAppL( appInfo, appName ) )
       
   460                     {
       
   461                     TApaTask task2 = tl.FindApp( appInfo.iUid );
       
   462                     if ( task2.Exists() )
       
   463                         {
       
   464                         task2.EndTask();
       
   465                         User::After( KCloseTaskDelay );
       
   466                         TApaTask task2 = tl.FindApp( appInfo.iUid );
       
   467                         if ( task2.Exists() )
       
   468                             {
       
   469                             task2.KillTask();
       
   470                             }
       
   471                         SendMessageL( EOk );
       
   472                         }
       
   473                     else
       
   474                         {
       
   475                         SendMessageL( ENotFound );
       
   476                         }
       
   477                     }
       
   478                 // If app was not found by FindAppL method, a response message
       
   479                 // has already been sent there - no need to send anything here.
       
   480                 }
       
   481             else
       
   482                 {
       
   483                 SendErrorMsg( offset , KErrDescrInvalidArguments );
       
   484                 }
       
   485             }
       
   486             break;
       
   487         case EStopDoc:
       
   488         case EStopDoc_u:
       
   489             {
       
   490             TInt offset = ParseString( aMessage, 1, unicode, docName );
       
   491             if ( offset >= 0 )
       
   492                 {
       
   493                 TApaTaskList tl( iWs );
       
   494                 TApaTask task = tl.FindDoc( docName );
       
   495                 if ( task.Exists() )
       
   496                     {
       
   497                     task.EndTask();
       
   498                     User::After( KCloseTaskDelay );
       
   499                     TApaTask task = tl.FindDoc( docName );
       
   500                     if ( task.Exists() )
       
   501                         {
       
   502                         task.KillTask();
       
   503                         }
       
   504                     SendMessageL( EOk );
       
   505                     }
       
   506                 else
       
   507                     {
       
   508                     SendMessageL( ENotFound );
       
   509                     }
       
   510                 }
       
   511             else
       
   512                 {
       
   513                 SendErrorMsg( offset , KErrDescrInvalidArguments );
       
   514                 }
       
   515             }
       
   516             break;
       
   517         case EStopApp_uid:
       
   518             {
       
   519             TPtrC8 appUid8 = aMessage.Mid( 1 );
       
   520             if ( appUid8.Length() == 4 )
       
   521                 {
       
   522                 TApaTaskList tl( iWs );
       
   523                 TInt32 appUid = Parse32<TInt32>( appUid8 );
       
   524                 TApaTask task = tl.FindApp( TUid::Uid( appUid ) );
       
   525                 if ( task.Exists() )
       
   526                     {
       
   527                     task.EndTask();
       
   528                     User::After(KCloseTaskDelay);
       
   529                     TApaTask task = tl.FindApp( TUid::Uid( appUid ) );
       
   530                     if ( task.Exists() )
       
   531                         {
       
   532                         task.KillTask();
       
   533                         }
       
   534                     SendMessageL( EOk );
       
   535                     }
       
   536                 else
       
   537                     {
       
   538                     SendMessageL( ENotFound );
       
   539                     }
       
   540                 }
       
   541             else
       
   542                 {
       
   543                 SendErrorMsg( KErrArgument , KErrDescrInvalidUid );
       
   544                 }
       
   545             }
       
   546             break;
       
   547         case EListApps:
       
   548         case EListApps_u:
       
   549             {
       
   550             if ( aMessage.Length() != 3 )
       
   551                 {
       
   552                 SendErrorMsg( KErrArgument, KErrDescrInvalidArguments );
       
   553                 }
       
   554             else
       
   555                 {
       
   556                 HandleListAppsL( aMessage[1], aMessage[2], unicode );
       
   557                 }
       
   558             }
       
   559             break;
       
   560         case EListInstalledApps:
       
   561         case EListInstalledApps_u:
       
   562             {
       
   563             if ( aMessage.Length() != 1 )
       
   564                 {
       
   565                 SendErrorMsg( KErrArgument, KErrDescrInvalidArguments );
       
   566                 }
       
   567             else
       
   568                 {
       
   569                 HandleListInstalledAppsL( unicode );
       
   570                 }
       
   571             }
       
   572             break;
       
   573         default:
       
   574             {
       
   575             SendErrorMsg( KErrArgument, KErrDescrInvalidCmd );
       
   576             }
       
   577         }
       
   578 
       
   579     HTI_LOG_FUNC_OUT("CHtiAppControl::HandleAppControl");
       
   580     }
       
   581 
       
   582 void CHtiAppControl::HandleInstallerControlL( const TDesC8& aMessage )
       
   583     {
       
   584     HTI_LOG_FUNC_IN( "CHtiAppControl::HandleInstallerControlL" );
       
   585 
       
   586     if ( aMessage.Length() < 2 )
       
   587         {
       
   588         HTI_LOG_TEXT( "Command parameters missing" );
       
   589         SendErrorMsg( KErrArgument , KErrDescrInvalidCmd );
       
   590         return;
       
   591         }
       
   592 
       
   593     TBool unicode = aMessage[0] & KUnicodeMask;
       
   594     HTI_LOG_FORMAT( "Unicode flag: %d", unicode );
       
   595 
       
   596     TFileName path;
       
   597 #if ( SYMBIAN_VERSION_SUPPORT < SYMBIAN_4 )
       
   598     TBuf<SwiUI::KSWInstMaxPasswordLength> login;
       
   599     TBuf<SwiUI::KSWInstMaxPasswordLength> password;
       
   600 #endif
       
   601 
       
   602     switch ( aMessage[0] )
       
   603         {
       
   604         case EInstall:
       
   605         case EInstall_u:
       
   606             {
       
   607             TPtrC8 parameters = aMessage.Mid( 1 );
       
   608             if ( ValidateInstallParams( parameters, unicode ) )
       
   609                 {
       
   610                 TInt offset = ParseString( parameters, 0, unicode, path );
       
   611 
       
   612                 HTI_LOG_TEXT( "Parsed path to install package:" );
       
   613                 HTI_LOG_DES( path )
       
   614 
       
   615                 if ( offset >= 0)
       
   616                     {
       
   617 #if ( SYMBIAN_VERSION_SUPPORT < SYMBIAN_4 )
       
   618                     iInstOpts = SwiUI::TInstallOptions();
       
   619 
       
   620                     iInstOpts.iUpgrade =            ConvertToPolicy( parameters[offset] ); offset++;
       
   621                     iInstOpts.iOptionalItems =      ConvertToPolicy( parameters[offset] ); offset++;
       
   622                     iInstOpts.iOCSP =               ConvertToPolicy( parameters[offset] ); offset++;
       
   623                     iInstOpts.iIgnoreOCSPWarnings = ConvertToPolicy( parameters[offset] ); offset++;
       
   624                     iInstOpts.iUntrusted =          ConvertToPolicy( parameters[offset] ); offset++;
       
   625                     iInstOpts.iPackageInfo =        ConvertToPolicy( parameters[offset] ); offset++;
       
   626                     iInstOpts.iCapabilities =       ConvertToPolicy( parameters[offset] ); offset++;
       
   627                     iInstOpts.iKillApp =            ConvertToPolicy( parameters[offset] ); offset++;
       
   628                     iInstOpts.iOverwrite =          ConvertToPolicy( parameters[offset] ); offset++;
       
   629                     iInstOpts.iDownload =           ConvertToPolicy( parameters[offset] ); offset++;
       
   630 
       
   631                     HTI_LOG_FORMAT( "iUpgrade: %d",            iInstOpts.iUpgrade );
       
   632                     HTI_LOG_FORMAT( "iOptionalItems: %d",      iInstOpts.iOptionalItems );
       
   633                     HTI_LOG_FORMAT( "iOCSP: %d",               iInstOpts.iOCSP );
       
   634                     HTI_LOG_FORMAT( "iIgnoreOCSPWarnings: %d", iInstOpts.iIgnoreOCSPWarnings );
       
   635                     HTI_LOG_FORMAT( "iUntrusted: %d",          iInstOpts.iUntrusted );
       
   636                     HTI_LOG_FORMAT( "iPackageInfo: %d",        iInstOpts.iPackageInfo );
       
   637                     HTI_LOG_FORMAT( "iCapabilities: %d",       iInstOpts.iCapabilities );
       
   638                     HTI_LOG_FORMAT( "iKillApp: %d",            iInstOpts.iKillApp );
       
   639                     HTI_LOG_FORMAT( "iOverwrite: %d",          iInstOpts.iOverwrite );
       
   640                     HTI_LOG_FORMAT( "iDownload: %d",           iInstOpts.iDownload );
       
   641 
       
   642                     offset = ParseString( parameters, offset, unicode, login );
       
   643                     iInstOpts.iLogin.Copy( login );
       
   644                     HTI_LOG_TEXT( "Parsed login:" );
       
   645                     HTI_LOG_DES( login )
       
   646 
       
   647                     offset = ParseString( parameters, offset, unicode, password );
       
   648                     iInstOpts.iPassword.Copy( password );
       
   649                     HTI_LOG_TEXT( "Parsed password:" );
       
   650                     HTI_LOG_DES( password )
       
   651 
       
   652                     iInstOpts.iDrive =        (TChar) parameters[offset]; offset++;
       
   653                     iInstOpts.iLang =         (TLanguage) parameters[offset]; offset++;
       
   654                     iInstOpts.iUsePhoneLang = (TBool) parameters[offset]; offset++;
       
   655                     iInstOpts.iUpgradeData =  ConvertToPolicy( parameters[offset] );
       
   656                     offset++;
       
   657                     HTI_LOG_FORMAT( "parameters length: %d", parameters.Length());
       
   658                     HTI_LOG_FORMAT( "next offset: %d", offset);
       
   659 
       
   660                     if(parameters.Length() > offset)
       
   661                         {
       
   662                         TInt language = iInstOpts.iLang;
       
   663                         if (parameters.Length() == offset+2)
       
   664                             {
       
   665                             language = parameters[offset] + (parameters[offset+1]<<8);
       
   666                             }
       
   667                         if (parameters.Length() == offset+1)
       
   668                             {
       
   669                             language = parameters[offset];
       
   670                             }
       
   671                         if ((language > ELangTest) && (language < ELangMaximum))
       
   672                             {
       
   673                             iInstOpts.iLang =         (TLanguage) language;
       
   674                             }
       
   675                         }
       
   676                     HTI_LOG_FORMAT( "iDrive: %c",        iInstOpts.iDrive.GetLowerCase() );
       
   677                     HTI_LOG_FORMAT( "iLang: %d",         iInstOpts.iLang );
       
   678                     HTI_LOG_FORMAT( "iUsePhoneLang: %d", iInstOpts.iUsePhoneLang );
       
   679                     HTI_LOG_FORMAT( "iUpgradeData: %d",  iInstOpts.iUpgradeData );
       
   680 
       
   681                     iInstOptsPckg = iInstOpts;
       
   682 
       
   683                     // Connect to silent installer
       
   684                     SwiUI::RSWInstSilentLauncher launcher;
       
   685                     TInt err = launcher.Connect();
       
   686                     if ( err )
       
   687                         {
       
   688                         HTI_LOG_FORMAT( "Error connecting to silent installer, err: %d", err );
       
   689                         SendErrorMsg( err , KErrDescrFailedConnectSilentInstaller );
       
   690                         break;
       
   691                         }
       
   692 
       
   693                     err = launcher.SilentInstall( path, iInstOptsPckg );
       
   694                     if ( err )
       
   695                         {
       
   696                         HTI_LOG_FORMAT( "Error installing software, err: %d", err );
       
   697                         launcher.Close();
       
   698                         SendErrorMsg( err , KErrDescrFailedInstall );
       
   699                         break;
       
   700                         }
       
   701 
       
   702                     launcher.Close();
       
   703                     SendMessageL( EOk );
       
   704 #else
       
   705                     HTI_LOG_TEXT( "============RSoftwareInstall::Install=============" );
       
   706                     Usif::RSoftwareInstall installer;
       
   707                     TInt err = installer.Connect();
       
   708                     if(err)
       
   709                         {
       
   710                         SendErrorMsg( err , KErrDescrFailedConnectSilentInstaller );
       
   711                         HTI_LOG_FORMAT("cannot connect to SIF server, err %d", err);
       
   712                         User::Leave(err);
       
   713                         }
       
   714                     CleanupClosePushL(installer);
       
   715                     TRequestStatus status;
       
   716                     Usif::COpaqueNamedParams *arguments = Usif::COpaqueNamedParams::NewL();
       
   717                     CleanupStack::PushL(arguments);
       
   718                     Usif::COpaqueNamedParams *results = Usif::COpaqueNamedParams::NewL();
       
   719                     CleanupStack::PushL(results);
       
   720                     
       
   721                     arguments->AddIntL(Usif::KSifInParam_InstallSilently, ETrue);
       
   722                     
       
   723                     TInt intValue = ConvertToSifPolicy(parameters[offset]); offset++;
       
   724                     if(intValue != Usif::EAllowed)
       
   725                         arguments->AddIntL(Usif::KSifInParam_AllowUpgrade, intValue);
       
   726                     HTI_LOG_FORMAT( "iUpgrade: %d", intValue );
       
   727                     intValue = ConvertToSifPolicy(parameters[offset]); offset++; 
       
   728                     if(intValue != Usif::EAllowed)
       
   729                         arguments->AddIntL(Usif::KSifInParam_InstallOptionalItems, intValue);
       
   730                     HTI_LOG_FORMAT( "iOptionalItems: %d", intValue );
       
   731                     intValue = ConvertToSifPolicy(parameters[offset]); offset++;
       
   732                     if(intValue != Usif::EAllowed)
       
   733                         arguments->AddIntL(Usif::KSifInParam_PerformOCSP, intValue);
       
   734                     HTI_LOG_FORMAT( "iOCSP: %d", intValue );
       
   735                     intValue = ConvertToSifPolicy(parameters[offset]); offset++;
       
   736                     if(intValue != Usif::EAllowed)
       
   737                         arguments->AddIntL(Usif::KSifInParam_IgnoreOCSPWarnings, intValue);
       
   738                     HTI_LOG_FORMAT( "iIgnoreOCSPWarnings: %d", intValue );
       
   739                     intValue = ConvertToSifPolicy(parameters[offset]); offset++;
       
   740                     if(intValue != Usif::ENotAllowed)
       
   741                         arguments->AddIntL(Usif::KSifInParam_AllowUntrusted, intValue);
       
   742                     HTI_LOG_FORMAT( "iUntrusted: %d", intValue );
       
   743                     intValue = ConvertToSifPolicy(parameters[offset]); offset++;
       
   744                     if(intValue != Usif::EAllowed)
       
   745 						arguments->AddIntL(Usif::KSifInParam_PackageInfo, intValue);
       
   746                     HTI_LOG_FORMAT( "iPackageInfo: %d", intValue );
       
   747                     intValue = ConvertToSifPolicy(parameters[offset]); offset++;
       
   748                     if(intValue != Usif::EAllowed)
       
   749                         arguments->AddIntL(Usif::KSifInParam_GrantCapabilities,intValue);
       
   750                     HTI_LOG_FORMAT( "iCapabilities: %d", intValue );
       
   751                     intValue = ConvertToSifPolicy(parameters[offset]); offset++;
       
   752                     if(intValue != Usif::EAllowed)
       
   753                         arguments->AddIntL(Usif::KSifInParam_AllowAppShutdown,intValue);
       
   754                     HTI_LOG_FORMAT( "iKillApp: %d", intValue );
       
   755                     intValue = ConvertToSifPolicy(parameters[offset]); offset++;
       
   756                     if(intValue != Usif::EAllowed)
       
   757                         arguments->AddIntL(Usif::KSifInParam_AllowOverwrite,intValue);
       
   758                     HTI_LOG_FORMAT( "iOverwrite: %d", intValue );
       
   759                     intValue = ConvertToSifPolicy(parameters[offset]); offset++;
       
   760                     if(intValue != Usif::EAllowed)
       
   761                         arguments->AddIntL(Usif::KSifInParam_AllowDownload, intValue);
       
   762                     HTI_LOG_FORMAT( "iDownload: %d", intValue );
       
   763 
       
   764                     TBuf<32> login;
       
   765                     offset = ParseString( parameters, offset, unicode, login );
       
   766                     HTI_LOG_FORMAT( "login length: %d", login.Length() );
       
   767                     if(login != KNullDesC)
       
   768                         {
       
   769                         arguments->AddStringL(Usif::KSifInParam_UserName, login);
       
   770                         }
       
   771                     HTI_LOG_TEXT( "Parsed login:" );
       
   772                     HTI_LOG_DES( login )
       
   773 
       
   774                     TBuf<32> password;
       
   775                     offset = ParseString( parameters, offset, unicode, password );
       
   776                     HTI_LOG_FORMAT( "password length: %d", password.Length() );
       
   777                     if(password != KNullDesC)
       
   778                         {
       
   779                         arguments->AddStringL(Usif::KSifInParam_Password, password );
       
   780                         }
       
   781                     HTI_LOG_TEXT( "Parsed password:" );
       
   782                     HTI_LOG_DES( password )
       
   783 
       
   784                     TChar driver = (TChar) parameters[offset]; offset++;
       
   785                     if(driver >= 'A' && driver <= 'Z')
       
   786                         {
       
   787                         intValue = driver - (TChar)'A';
       
   788                         arguments->AddIntL(Usif::KSifInParam_Drive, intValue);
       
   789                         HTI_LOG_FORMAT( "iDrive: %c", intValue + 'a' );
       
   790                         }
       
   791                     else if(driver >= 'a' && driver <= 'z')
       
   792                         {
       
   793                         intValue = driver - (TChar)'a';
       
   794                         arguments->AddIntL(Usif::KSifInParam_Drive, intValue);
       
   795                         HTI_LOG_FORMAT( "iDrive: %c", intValue + 'a' );
       
   796                         }
       
   797 
       
   798                     TLanguage oldFormatLanuage = (TLanguage)parameters[offset]; offset++;
       
   799 
       
   800                     TBool usePhoneLang = (TBool)parameters[offset]; offset++;
       
   801                     HTI_LOG_FORMAT( "iUsePhoneLang: %d", usePhoneLang );
       
   802 
       
   803                     intValue =  ConvertToSifPolicy( parameters[offset] );offset++;
       
   804                     /*if(intValue != Usif::EAllowed)
       
   805                         arguments->AddIntL(Usif::KSifInParam_AllowUpgrade, intValue);*/
       
   806                     HTI_LOG_FORMAT( "iUpgradeData: %d",  intValue ); 
       
   807                     
       
   808                     HTI_LOG_FORMAT( "parameters length: %d", parameters.Length());
       
   809                     HTI_LOG_FORMAT( "next offset: %d", offset);
       
   810 
       
   811                     if(usePhoneLang == EFalse)
       
   812                         {
       
   813                         if(parameters.Length() > offset)
       
   814                             {
       
   815                             TLanguage language = ELangTest;
       
   816                             if (parameters.Length() == offset+2)
       
   817                                 {
       
   818                                 language = (TLanguage)(parameters[offset] + (parameters[offset+1]<<8));
       
   819                                 }
       
   820                             else if (parameters.Length() == offset+1)
       
   821                                 {
       
   822                                 language = (TLanguage)(parameters[offset]);
       
   823                                 }
       
   824                             if ((language > ELangTest) && (language < ELangMaximum))
       
   825                                 {
       
   826                                 arguments->AddIntL(Usif::KSifInParam_Languages, language);
       
   827                                 HTI_LOG_FORMAT( "iLang: %d", language );
       
   828                                 }
       
   829                             }
       
   830                         else
       
   831                             {
       
   832                             arguments->AddIntL(Usif::KSifInParam_Languages, oldFormatLanuage);
       
   833                             HTI_LOG_FORMAT( "iLang0: %d", oldFormatLanuage );
       
   834                             }
       
   835                         }
       
   836 
       
   837                     installer.Install(path, *arguments, *results, status);
       
   838                     User::WaitForRequest(status);
       
   839                     HTI_LOG_FORMAT("install status: %d", status.Int());
       
   840                     TInt componentId = 0;
       
   841                     TBool idExisted = results->GetIntByNameL(Usif::KSifOutParam_ComponentId, componentId);
       
   842                     if ( idExisted )
       
   843                         {
       
   844                         HTI_LOG_FORMAT( "componentId:%d", componentId);
       
   845                         }
       
   846                     CleanupStack::PopAndDestroy(3);
       
   847                     if (status != KErrNone)
       
   848                         {
       
   849                         SendErrorMsg( status.Int(), KErrDescrFailedInstall );
       
   850                         break;
       
   851                         }
       
   852                     SendMessageL( EOk );
       
   853 #endif
       
   854                     }
       
   855                 else
       
   856                     {
       
   857                     HTI_LOG_TEXT( "Error parsing path" );
       
   858                     SendErrorMsg( offset , KErrDescrInvalidArguments );
       
   859                     }
       
   860                 }
       
   861             else
       
   862                 {
       
   863                 SendErrorMsg( KErrArgument, KErrDescrInvalidCmd );
       
   864                 }
       
   865             }
       
   866             break;
       
   867 
       
   868         case EUnInstall:
       
   869         case EUnInstallName:
       
   870         case EUnInstallName_u:
       
   871             {
       
   872             TPtrC8 parameters = aMessage.Mid( 1 );
       
   873             if ( ( aMessage[0] == EUnInstall && parameters.Length() != 7 ) ||
       
   874                  ( aMessage[0] == EUnInstallName &&
       
   875                    parameters.Length() != parameters[0] + 4 ) ||
       
   876                  ( aMessage[0] == EUnInstallName_u &&
       
   877                    parameters.Length() != parameters[0] * 2 + 4 ) )
       
   878                 {
       
   879                 HTI_LOG_FORMAT( "Invalid command length: %d",
       
   880                     parameters.Length() );
       
   881                 SendErrorMsg( KErrArgument, KErrDescrInvalidCmd );
       
   882                 break;
       
   883                 }
       
   884 
       
   885             TInt offset = 0;
       
   886             TInt32 uid = KErrNotFound;
       
   887             HBufC* packageName = NULL;
       
   888 #if ( SYMBIAN_VERSION_SUPPORT >= SYMBIAN_4 )
       
   889             Usif::TComponentId cid = KErrNotFound; 
       
   890             HTI_LOG_TEXT( "============RSoftwareInstall::Uninstall=============" );
       
   891             Usif::RSoftwareInstall installer;
       
   892             TInt err = installer.Connect();
       
   893             if(err)
       
   894                 {
       
   895                 SendErrorMsg( err , KErrDescrFailedConnectSilentInstaller );
       
   896                 HTI_LOG_FORMAT("cannot connect to SIF server, err %d", err);
       
   897                 User::Leave(err);
       
   898                 }
       
   899             CleanupClosePushL(installer);
       
   900             TRequestStatus status;
       
   901             Usif::COpaqueNamedParams *arguments = Usif::COpaqueNamedParams::NewL();
       
   902             CleanupStack::PushL(arguments);
       
   903             Usif::COpaqueNamedParams *results = Usif::COpaqueNamedParams::NewL();
       
   904             CleanupStack::PushL(results);
       
   905 #endif
       
   906             if ( aMessage[0] == EUnInstall )
       
   907                 {
       
   908                 uid = Parse32<TInt32>( parameters );
       
   909                 offset += 4;
       
   910                 HTI_LOG_FORMAT( "Uninstall by uid: %d", uid );
       
   911 #if ( SYMBIAN_VERSION_SUPPORT >= SYMBIAN_4 )
       
   912                 cid = GetComponentIdFromUid(uid);
       
   913                 if ( cid == KErrNotFound )
       
   914                     {
       
   915                     HTI_LOG_FORMAT( "cid: %d", cid );
       
   916                     SendErrorMsg( KErrNotFound, KErrDescrBadComponentId );
       
   917                     CleanupStack::PopAndDestroy(3); //results, arguments, installer
       
   918                     break;
       
   919                     }
       
   920 #endif
       
   921                 }
       
   922             else
       
   923                 {
       
   924                 packageName = HBufC::NewLC( parameters[offset] );
       
   925                 TPtr namePtr = packageName->Des();
       
   926                 offset = ParseString( parameters, offset, unicode, namePtr );
       
   927                 HTI_LOG_FORMAT( "Uninstall by name: %S", packageName );
       
   928 #if ( SYMBIAN_VERSION_SUPPORT >= SYMBIAN_4 )
       
   929                 cid = GetComponentIdFromPackageName(*packageName);
       
   930                 CleanupStack::PopAndDestroy(); // packageName
       
   931                 if ( cid == KErrNotFound )
       
   932                     {
       
   933                     HTI_LOG_FORMAT( "cid: %d", cid );
       
   934                     SendErrorMsg( KErrNotFound, KErrDescrFailedFindPackage );
       
   935                     CleanupStack::PopAndDestroy(3); //results, arguments, installer
       
   936                     break;
       
   937                     }
       
   938 #endif
       
   939                 }
       
   940 #if ( SYMBIAN_VERSION_SUPPORT < SYMBIAN_4 )
       
   941             iUnInstOpts = SwiUI::TUninstallOptions();
       
   942             iUnInstOpts.iKillApp = ConvertToPolicy( parameters[offset] );
       
   943             offset++;
       
   944             iUnInstOpts.iBreakDependency = ConvertToPolicy( parameters[offset] );
       
   945             offset++;
       
   946             HTI_LOG_FORMAT( "iKillApp: %d",         iUnInstOpts.iKillApp );
       
   947             HTI_LOG_FORMAT( "iBreakDependency: %d", iUnInstOpts.iBreakDependency );
       
   948 
       
   949             TInt mimeIndex = parameters[offset];
       
   950             if ( mimeIndex > iMimeTypes->Count() - 1 )
       
   951                 {
       
   952                 HTI_LOG_FORMAT( "Invalid mime type: %d", mimeIndex );
       
   953                 SendErrorMsg( KErrArgument, KErrDescrInvalidCmd );
       
   954                 break;
       
   955                 }
       
   956 
       
   957             HTI_LOG_TEXT( "Uninstall mime type:" );
       
   958             HTI_LOG_DES( (*iMimeTypes)[mimeIndex] );
       
   959 
       
   960             TInt err = KErrNone;
       
   961             iAugmentationIndex = 0;
       
   962             if ( packageName )
       
   963                 {
       
   964                 TRAP( err, uid = GetPackageUidL( *packageName, mimeIndex ) );
       
   965                 CleanupStack::PopAndDestroy(); // packageName
       
   966                 iAugmentations.ResetAndDestroy();
       
   967                 if ( err != KErrNone )
       
   968                     {
       
   969                     SendErrorMsg( err, KErrDescrFailedFindPackage );
       
   970                     break;
       
   971                     }
       
   972                 }
       
   973             HTI_LOG_FORMAT( "UID = %d", uid );
       
   974 
       
   975             iUnInstOptsPckg = iUnInstOpts;
       
   976 
       
   977             // Connect to silent installer
       
   978             SwiUI::RSWInstSilentLauncher launcher;
       
   979             err = launcher.Connect();
       
   980             if ( err )
       
   981                 {
       
   982                 HTI_LOG_FORMAT( "Error connecting to silent installer, err: %d", err );
       
   983                 SendErrorMsg( err , KErrDescrFailedConnectSilentInstaller );
       
   984                 break;
       
   985                 }
       
   986 
       
   987             if ( iAugmentationIndex > 0 )
       
   988                 {
       
   989                 SwiUI::TOpUninstallIndexParam params;
       
   990                 params.iUid = TUid::Uid( uid );
       
   991                 params.iIndex = iAugmentationIndex;
       
   992                 SwiUI::TOpUninstallIndexParamPckg paramPckg( params );
       
   993                 SwiUI::TOperation operation( SwiUI::EOperationUninstallIndex );
       
   994                 err = launcher.SilentCustomUninstall( operation, iUnInstOptsPckg,
       
   995                     paramPckg, (*iMimeTypes)[mimeIndex] );
       
   996                 }
       
   997             else
       
   998                 {
       
   999                 err = launcher.SilentUninstall( TUid::Uid( uid ), iUnInstOptsPckg,
       
  1000                         (*iMimeTypes)[mimeIndex] );
       
  1001                 }
       
  1002 
       
  1003             if ( err )
       
  1004                 {
       
  1005                 HTI_LOG_FORMAT( "Error uninstalling software, err: %d", err );
       
  1006                 launcher.Close();
       
  1007                 SendErrorMsg( err , KErrDescrFailedUnInstall );
       
  1008                 break;
       
  1009                 }
       
  1010 
       
  1011             launcher.Close();
       
  1012 #else
       
  1013             
       
  1014             TInt intValue = ConvertToSifPolicy( parameters[offset] );
       
  1015             offset++;
       
  1016             arguments->AddIntL(Usif::KSifInParam_AllowAppShutdown, intValue);
       
  1017             HTI_LOG_FORMAT( "iKillApp: %d", intValue );
       
  1018             intValue = ConvertToSifPolicy( parameters[offset] );
       
  1019             offset++;
       
  1020             arguments->AddIntL(Usif::KSifInParam_AllowAppBreakDependency, intValue);
       
  1021             HTI_LOG_FORMAT( "iBreakDependency: %d", intValue );
       
  1022 
       
  1023             TInt mimeIndex = parameters[offset];
       
  1024             if ( mimeIndex > iMimeTypes->Count() - 1 )
       
  1025                 {
       
  1026                 HTI_LOG_FORMAT( "Invalid mime type: %d", mimeIndex );
       
  1027                 SendErrorMsg( KErrArgument, KErrDescrInvalidCmd );
       
  1028                 break;
       
  1029                 }
       
  1030 
       
  1031             HTI_LOG_TEXT( "Uninstall mime type:" );
       
  1032             HTI_LOG_DES( (*iMimeTypes)[mimeIndex] );
       
  1033 
       
  1034             HBufC* buf = HBufC::NewLC((*iMimeTypes)[mimeIndex].Length());
       
  1035             TPtr ptr = buf->Des();
       
  1036             ptr.Copy((*iMimeTypes)[mimeIndex]);
       
  1037             arguments->AddStringL(Usif::KSifInParam_MimeType, *buf);
       
  1038             CleanupStack::PopAndDestroy(); // buf
       
  1039       
       
  1040             HTI_LOG_FORMAT( "Component ID = %d", cid );
       
  1041             
       
  1042             arguments->AddIntL(Usif::KSifInParam_InstallSilently, ETrue);
       
  1043             
       
  1044             installer.Uninstall(cid, *arguments, *results, status, EFalse);
       
  1045             User::WaitForRequest(status);
       
  1046             HTI_LOG_FORMAT("uninstall status:%d", status.Int());
       
  1047             CleanupStack::PopAndDestroy(3); //results, arguments, installer
       
  1048             if (status != KErrNone)
       
  1049                 {
       
  1050                 SendErrorMsg(status.Int(), KErrDescrFailedUnInstall);
       
  1051                 break;
       
  1052                 }            
       
  1053 #endif
       
  1054             SendMessageL( EOk );
       
  1055             }
       
  1056             break;
       
  1057 
       
  1058         default:
       
  1059             {
       
  1060             SendErrorMsg( KErrArgument, KErrDescrInvalidCmd );
       
  1061             }
       
  1062         }
       
  1063 
       
  1064     HTI_LOG_FUNC_OUT( "CHtiAppControl::HandleInstallerControlL" );
       
  1065     }
       
  1066 
       
  1067 
       
  1068 void CHtiAppControl::HandleStartProcessL( const TDesC& aProgramName,
       
  1069                                                 const TDesC& aCmdLine,
       
  1070                                                 TBool aStoreProcessHandle )
       
  1071     {
       
  1072     HTI_LOG_FUNC_IN( "CHtiAppControl::HandleStartProcessL" );
       
  1073     HTI_LOG_FORMAT( "progr name %d", aProgramName.Length() );
       
  1074     HTI_LOG_DES( aProgramName );
       
  1075 
       
  1076     RProcess process;
       
  1077     TInt err = process.Create( aProgramName, aCmdLine ); // command parameters
       
  1078 
       
  1079     if ( err == KErrNone )
       
  1080         {
       
  1081         CleanupClosePushL( process );
       
  1082 
       
  1083         //convert process id to binary des
       
  1084         TUint processId = process.Id();
       
  1085         HTI_LOG_FORMAT( "process id %d", processId );
       
  1086 
       
  1087         TBuf8<KTUintLength> processIdDes;
       
  1088         processIdDes.Append(
       
  1089                 ( TUint8* )( &processId ), KTUintLength );
       
  1090 
       
  1091         SendMessageL( EOk, processIdDes );
       
  1092 
       
  1093         process.Resume();
       
  1094 
       
  1095         if ( aStoreProcessHandle )
       
  1096             {
       
  1097             HTI_LOG_TEXT( "Storing the process handle" );
       
  1098             iProcessHandleArray.Append( process );
       
  1099             CleanupStack::Pop();
       
  1100             }
       
  1101         else
       
  1102             {
       
  1103             CleanupStack::PopAndDestroy();
       
  1104             }
       
  1105         }
       
  1106     else if ( err == KErrNotFound )
       
  1107         {
       
  1108         SendMessageL( ENotFound );
       
  1109         }
       
  1110     else
       
  1111         {
       
  1112         SendErrorMsg( err ,KErrDescrFailedCreateProcess );
       
  1113         }
       
  1114 
       
  1115     HTI_LOG_FUNC_OUT( "CHtiAppControl::HandleStartProcessL" );
       
  1116     }
       
  1117 
       
  1118 void CHtiAppControl::HandleStopProcessL( RProcess& aProcess )
       
  1119     {
       
  1120     if ( aProcess.ExitType() == EExitPending )
       
  1121         {
       
  1122         aProcess.Kill( KTerminateReason );
       
  1123         SendMessageL( EOk );
       
  1124         }
       
  1125     else
       
  1126         {
       
  1127         SendMessageL( EAlreadyStopped );
       
  1128         }
       
  1129     }
       
  1130 
       
  1131 void CHtiAppControl::HandleStatusProcessL( RProcess& aProcess )
       
  1132     {
       
  1133     TExitType exT = aProcess.ExitType();
       
  1134 
       
  1135     switch ( exT )
       
  1136         {
       
  1137         case EExitPending:
       
  1138             {
       
  1139             SendMessageL( ERunning );
       
  1140             }
       
  1141             break;
       
  1142         case EExitKill:
       
  1143         case EExitTerminate:
       
  1144             {
       
  1145             SendMessageL( EKilled );
       
  1146             }
       
  1147             break;
       
  1148         case EExitPanic:
       
  1149             {
       
  1150             SendMessageL( EPanic );
       
  1151             }
       
  1152             break;
       
  1153         };
       
  1154     }
       
  1155 
       
  1156 
       
  1157 void CHtiAppControl::HandleListProcessesL( const TDesC& aMatch )
       
  1158     {
       
  1159     HTI_LOG_FUNC_IN( "CHtiAppControl::HandleListProcessesL" );
       
  1160 
       
  1161     RProcess process;
       
  1162     TFullName processName;
       
  1163     TUint processId;
       
  1164     TBuf8<KTUintLength> processIdDes;
       
  1165     TExitType exitType;
       
  1166 
       
  1167     TBuf8<128> buf;
       
  1168     CBufFlat* processListBuf = NULL;
       
  1169     TRAPD( err, processListBuf = CBufFlat::NewL( 128 ) );
       
  1170     if ( err )
       
  1171         {
       
  1172         SendErrorMsg( err ,KErrDescrNoMemory );
       
  1173         return;
       
  1174         }
       
  1175 
       
  1176     CleanupStack::PushL( processListBuf );
       
  1177 
       
  1178     // The default match pattern is the single character *
       
  1179     TFindProcess finder;
       
  1180 
       
  1181     // if some real match pattern is defined, use it
       
  1182     if ( aMatch.Length() > 0 )
       
  1183         {
       
  1184         HTI_LOG_TEXT( "Match pattern was given:" );
       
  1185         HTI_LOG_DES( aMatch );
       
  1186         finder.Find( aMatch );
       
  1187         }
       
  1188 
       
  1189     TInt pos = 0;
       
  1190     TUint numberOfEntries = 0;
       
  1191 
       
  1192     while ( finder.Next( processName ) == KErrNone )
       
  1193         {
       
  1194         err = process.Open( finder );
       
  1195 
       
  1196         //convert process id to binary des
       
  1197         processId = process.Id();
       
  1198         processIdDes.Append( ( TUint8* ) ( &processId ), KTUintLength );
       
  1199         buf.Append( processIdDes );
       
  1200 
       
  1201         // status
       
  1202         exitType = process.ExitType();
       
  1203         switch ( exitType )
       
  1204             {
       
  1205             case EExitPending:
       
  1206                 {
       
  1207                 buf.Append( ERunning );
       
  1208                 }
       
  1209                 break;
       
  1210             case EExitKill:
       
  1211             case EExitTerminate:
       
  1212                 {
       
  1213                 buf.Append( EKilled );
       
  1214                 }
       
  1215                 break;
       
  1216             case EExitPanic:
       
  1217                 {
       
  1218                 buf.Append( EPanic );
       
  1219                 }
       
  1220                 break;
       
  1221             };
       
  1222 
       
  1223         // name length
       
  1224         buf.Append( processName.Length() );
       
  1225 
       
  1226         // name
       
  1227         buf.Append( processName );
       
  1228 
       
  1229         process.Close();
       
  1230 
       
  1231         TRAP( err, processListBuf->ExpandL( pos, buf.Length() ) );
       
  1232         if ( err )
       
  1233             {
       
  1234             SendErrorMsg( err , KErrDescrNoMemory );
       
  1235             delete processListBuf;
       
  1236             return;
       
  1237             }
       
  1238         processListBuf->Write( pos, buf, buf.Length() );
       
  1239 
       
  1240         pos += buf.Length();
       
  1241         buf.Zero();
       
  1242         processIdDes.Zero();
       
  1243         numberOfEntries++;
       
  1244         }
       
  1245 
       
  1246     // insert the number of entries in the beginning
       
  1247     TBuf8<2> entries;
       
  1248     entries.Append( ( TUint8* ) ( &numberOfEntries ), 2 );
       
  1249     processListBuf->ExpandL( 0, 2 );
       
  1250     processListBuf->Write( 0, entries, 2 );
       
  1251 
       
  1252     SendMessageL( EOk, processListBuf->Ptr( 0 ) );
       
  1253 
       
  1254     CleanupStack::PopAndDestroy( processListBuf );
       
  1255 
       
  1256     HTI_LOG_FUNC_OUT( "CHtiAppControl::HandleListProcessesL" );
       
  1257     }
       
  1258 
       
  1259 
       
  1260 void CHtiAppControl::HandleStartAppL( TApaAppInfo &aAppInfo,
       
  1261                                             const TDesC& aDocName )
       
  1262     {
       
  1263     HTI_LOG_FUNC_IN( "CHtiAppControl::HandleStartAppL" );
       
  1264 
       
  1265     // Check if app is running as a root app (not embedded)
       
  1266     TApaTask task = FindRunningRootApp( aAppInfo.iUid );
       
  1267     if ( task.Exists() )
       
  1268         {
       
  1269         User::ResetInactivityTime();
       
  1270         task.BringToForeground();
       
  1271         SendMessageL( EAlreadyRunning );
       
  1272         }
       
  1273     else
       
  1274         {
       
  1275         TThreadId threadId;
       
  1276         TInt err = KErrNone;
       
  1277         if ( aAppInfo.iUid != TUid::Null() )
       
  1278             {
       
  1279             TApaAppCapabilityBuf capBuf;
       
  1280             err = iAppServer.GetAppCapability( capBuf, aAppInfo.iUid );
       
  1281             TApaAppCapability& caps = capBuf();
       
  1282             CApaCommandLine* cmdLine = CApaCommandLine::NewLC();
       
  1283             cmdLine->SetExecutableNameL( aAppInfo.iFullName );
       
  1284             if ( caps.iLaunchInBackground )
       
  1285                 {
       
  1286                 cmdLine->SetCommandL( EApaCommandBackground );
       
  1287                 HTI_LOG_TEXT( "Launching in background" );
       
  1288                 }
       
  1289             else
       
  1290                 {
       
  1291                 if ( aDocName != KNullDesC )
       
  1292                     {
       
  1293                     cmdLine->SetCommandL( EApaCommandOpen );
       
  1294                     cmdLine->SetDocumentNameL( aDocName );
       
  1295                     HTI_LOG_TEXT( "Launching with document" );
       
  1296                     }
       
  1297                 else
       
  1298                     {
       
  1299                     cmdLine->SetCommandL( EApaCommandRun );
       
  1300                     HTI_LOG_TEXT( "Launching without document" );
       
  1301                     }
       
  1302                 }
       
  1303             err = iAppServer.StartApp( *cmdLine, threadId );
       
  1304             CleanupStack::PopAndDestroy( cmdLine );
       
  1305             }
       
  1306         else if ( aDocName != KNullDesC )
       
  1307             {
       
  1308             HTI_LOG_TEXT( "Launching a document" );
       
  1309             /*
       
  1310              * If the app that should handle the doc is already running, we
       
  1311              * try to switch the open file and bring it to foreground. Whether
       
  1312              * switching the file works depends on the application: It must
       
  1313              * implement CEikAppUi::OpenFileL() or CAknAppUi::OpenFileL() in S60.
       
  1314              */
       
  1315             TUid appUid;
       
  1316             TDataType dataType;
       
  1317             err = iAppServer.AppForDocument( aDocName, appUid, dataType );
       
  1318             if ( err == KErrNone )
       
  1319                 {
       
  1320                 TApaTask task = FindRunningRootApp( appUid );
       
  1321                 if ( task.Exists() )
       
  1322                     {
       
  1323                     HTI_LOG_TEXT( "App for doc already running, switch file" );
       
  1324                     err = task.SwitchOpenFile( aDocName );
       
  1325                     task.BringToForeground();
       
  1326                     }
       
  1327                 else
       
  1328                     {
       
  1329                     err = iAppServer.StartDocument( aDocName, threadId );
       
  1330                     }
       
  1331                 }
       
  1332             }
       
  1333         else
       
  1334             {
       
  1335             User::Leave( KErrArgument );
       
  1336             }
       
  1337 
       
  1338         if ( err == KErrNone )
       
  1339             {
       
  1340             User::ResetInactivityTime();
       
  1341             //convert thread id to string
       
  1342             TUint threadIdUint = threadId; //cast to UInt
       
  1343             TBuf8<KTUintLength> threadIdDes;
       
  1344             threadIdDes.Append( ( TUint8* )( &threadIdUint ), KTUintLength );
       
  1345             SendMessageL( EOk, threadIdDes );
       
  1346             }
       
  1347         else if ( err == KErrNotFound )
       
  1348             {
       
  1349             SendMessageL( ENotFound );
       
  1350             }
       
  1351         else
       
  1352             {
       
  1353             SendErrorMsg( err, KErrDescrFailedStartApp );
       
  1354             }
       
  1355         }
       
  1356     HTI_LOG_FUNC_OUT( "CHtiAppControl::HandleStartAppL" );
       
  1357     }
       
  1358 
       
  1359 void CHtiAppControl::HandleListInstalledAppsL( TBool aUnicode )
       
  1360     {
       
  1361     HTI_LOG_FUNC_IN( "CHtiAppControl::HandleListInstalledAppsL" );
       
  1362 
       
  1363     // max file name + max caption + max short caption + 7 constant bytes
       
  1364     TInt bufSize = KMaxFileName + 2 * KApaMaxAppCaption + 7;
       
  1365     if ( aUnicode )
       
  1366         {
       
  1367         bufSize *= 2;
       
  1368         }
       
  1369 
       
  1370     CBufFlat* appListBuf = CBufFlat::NewL( bufSize );
       
  1371     CleanupStack::PushL( appListBuf );
       
  1372 
       
  1373     HBufC8* appsArray = HBufC8::NewLC( bufSize );
       
  1374     TPtr8 appsArrayPtr = appsArray->Des();
       
  1375 
       
  1376     TInt err = KErrNone;
       
  1377     err = iAppServer.GetAllApps();
       
  1378     if ( err != KErrNone )
       
  1379         {
       
  1380         SendErrorMsg( err, KErrDescrFailedListInstApps );
       
  1381         }
       
  1382     else
       
  1383         {
       
  1384         TApaAppInfo appInfo;
       
  1385         TInt pos( 0 );
       
  1386 
       
  1387         // Add application count
       
  1388         TInt numOfEntries = 0;
       
  1389         iAppServer.AppCount( numOfEntries );
       
  1390 
       
  1391         appsArrayPtr.Append( ( TUint8*) ( &numOfEntries ), 2 );
       
  1392 
       
  1393         while ( iAppServer.GetNextApp( appInfo ) == KErrNone )
       
  1394             {
       
  1395             TUint appUidUint = appInfo.iUid.iUid;
       
  1396             appsArrayPtr.Append( ( TUint8* ) &appUidUint, KTUintLength );
       
  1397 
       
  1398             // Add length of full name
       
  1399             appsArrayPtr.Append( appInfo.iFullName.Length() );
       
  1400             // Add full name if there is one
       
  1401             if ( appInfo.iFullName.Length() > 0 )
       
  1402                 {
       
  1403                 if ( aUnicode )
       
  1404                     {
       
  1405                     appsArrayPtr.Append( ( TUint8* ) appInfo.iFullName.Ptr(),
       
  1406                         appInfo.iFullName.Length() * 2 );
       
  1407                     }
       
  1408                 else
       
  1409                     {
       
  1410                     appsArrayPtr.Append( appInfo.iFullName );
       
  1411                     }
       
  1412                 }
       
  1413 
       
  1414             // Add length of caption
       
  1415             appsArrayPtr.Append( appInfo.iCaption.Length() );
       
  1416             // Add caption if there is one
       
  1417             if ( appInfo.iCaption.Length() > 0 )
       
  1418                 {
       
  1419                 if ( aUnicode )
       
  1420                     {
       
  1421                     appsArrayPtr.Append( ( TUint8* ) appInfo.iCaption.Ptr(),
       
  1422                         appInfo.iCaption.Length() * 2 );
       
  1423                     }
       
  1424                 else
       
  1425                     {
       
  1426                     appsArrayPtr.Append( appInfo.iCaption );
       
  1427                     }
       
  1428                 }
       
  1429 
       
  1430             // Add length of short caption
       
  1431             appsArrayPtr.Append( appInfo.iShortCaption.Length() );
       
  1432             // Add short caption if there is one
       
  1433             if ( appInfo.iShortCaption.Length() > 0 )
       
  1434                 {
       
  1435                 if ( aUnicode )
       
  1436                     {
       
  1437                     appsArrayPtr.Append( ( TUint8* ) appInfo.iShortCaption.Ptr(),
       
  1438                         appInfo.iCaption.Length() * 2 );
       
  1439                     }
       
  1440                 else
       
  1441                     {
       
  1442                     appsArrayPtr.Append( appInfo.iShortCaption );
       
  1443                     }
       
  1444                 }
       
  1445 
       
  1446             // Add app info to response buffer
       
  1447             appListBuf->ExpandL( pos, appsArray->Length() );
       
  1448             appListBuf->Write( pos, *appsArray, appsArray->Length() );
       
  1449 
       
  1450             pos += appsArray->Length();
       
  1451             appsArrayPtr.Zero();
       
  1452             }
       
  1453 
       
  1454         SendMessageL( EOk, appListBuf->Ptr( 0 ) );
       
  1455         }
       
  1456 
       
  1457     CleanupStack::PopAndDestroy( 2 ); //appListBuf, appsArray
       
  1458 
       
  1459     HTI_LOG_FUNC_IN( "CHtiAppControl::HandleListInstalledAppsL" );
       
  1460     }
       
  1461 
       
  1462 void CHtiAppControl::HandleListAppsL( TBool aIncludeHidden,
       
  1463                                             TBool aIncludeSystem,
       
  1464                                             TBool aUnicode )
       
  1465     {
       
  1466     HTI_LOG_FUNC_IN( "CHtiAppControl::HandleListAppsL" );
       
  1467 
       
  1468     RArray<RWsSession::TWindowGroupChainInfo>* wgChain = new ( ELeave )
       
  1469                             RArray<RWsSession::TWindowGroupChainInfo>( 12 );
       
  1470     CleanupDeletePushL( wgChain );
       
  1471     CleanupClosePushL( *wgChain );
       
  1472     User::LeaveIfError( iWs.WindowGroupList( 0, wgChain ) );
       
  1473     TInt wgCount = wgChain->Count();
       
  1474 
       
  1475     HTI_LOG_FORMAT( "%d Window Groups in the chain", wgCount );
       
  1476 
       
  1477     TInt bufSize = 2 * KMaxFileName + 11; // caption & document + 11 constant bytes
       
  1478     if ( aUnicode )
       
  1479         {
       
  1480         bufSize *= 2;
       
  1481         }
       
  1482     CBufFlat* appListBuf = CBufFlat::NewL( bufSize );
       
  1483     CleanupStack::PushL( appListBuf );
       
  1484     HBufC8* buf = HBufC8::NewLC( bufSize );
       
  1485     TPtr8 bufPtr = buf->Des();
       
  1486 
       
  1487     TInt pos = 0;
       
  1488     TInt numOfEntries = 0;
       
  1489 
       
  1490     for ( TInt i = 0; i < wgCount; i++ )
       
  1491         {
       
  1492         const RWsSession::TWindowGroupChainInfo& info = ( *wgChain )[i];
       
  1493         CApaWindowGroupName* wgName = CApaWindowGroupName::NewLC( iWs, info.iId );
       
  1494 
       
  1495         HTI_LOG_FORMAT( "WG ID: %d", info.iId );
       
  1496         HTI_LOG_DES( wgName->WindowGroupName() );
       
  1497 
       
  1498         // Info is returned only from root tasks and system and hidden tasks
       
  1499         // are included only if requested.
       
  1500         if ( info.iParentId <= 0 &&
       
  1501              ( !wgName->IsSystem() || aIncludeSystem ) &&
       
  1502              ( !wgName->Hidden() || aIncludeHidden ) )
       
  1503             {
       
  1504             // Add application UID
       
  1505             TUint appUidUint = wgName->AppUid().iUid;
       
  1506             bufPtr.Append( ( TUint8* ) &appUidUint, KTUintLength );
       
  1507 
       
  1508             // Add caption
       
  1509             TPtrC caption = wgName->Caption();
       
  1510             bufPtr.Append( caption.Length() );
       
  1511             if ( caption.Length() > 0 )
       
  1512                 {
       
  1513                 if ( aUnicode )
       
  1514                     {
       
  1515                     bufPtr.Append(
       
  1516                         ( TUint8* ) caption.Ptr(), caption.Length() * 2 );
       
  1517                     }
       
  1518                 else
       
  1519                     {
       
  1520                     bufPtr.Append( caption );
       
  1521                     }
       
  1522                 }
       
  1523 
       
  1524             // Add document name
       
  1525             TPtrC document = wgName->DocName();
       
  1526             bufPtr.Append( document.Length() );
       
  1527             if ( document.Length() > 0 )
       
  1528                 {
       
  1529                 if ( aUnicode )
       
  1530                     {
       
  1531                     bufPtr.Append(
       
  1532                         ( TUint8* ) document.Ptr(), document.Length() * 2 );
       
  1533                     }
       
  1534                 else
       
  1535                     {
       
  1536                     bufPtr.Append( document );
       
  1537                     }
       
  1538                 }
       
  1539 
       
  1540             // Add Hidden flag
       
  1541             if ( wgName->Hidden() )
       
  1542                 bufPtr.Append( 1 );
       
  1543             else
       
  1544                 bufPtr.Append( 0 );
       
  1545 
       
  1546             // Add System flag
       
  1547             if ( wgName->IsSystem() )
       
  1548                 bufPtr.Append( 1 );
       
  1549             else
       
  1550                 bufPtr.Append( 0 );
       
  1551 
       
  1552             // Add Ready flag
       
  1553             if ( wgName->IsAppReady() )
       
  1554                 bufPtr.Append( 1 );
       
  1555             else
       
  1556                 bufPtr.Append( 0 );
       
  1557 
       
  1558             // Add Busy flag
       
  1559             if ( wgName->IsBusy() )
       
  1560                 bufPtr.Append( 1 );
       
  1561             else
       
  1562                 bufPtr.Append( 0 );
       
  1563 
       
  1564             // Add Shutdown response flag
       
  1565             if ( wgName->RespondsToShutdownEvent() )
       
  1566                 bufPtr.Append( 1 );
       
  1567             else
       
  1568                 bufPtr.Append( 0 );
       
  1569 
       
  1570             // Add this task's info to response buffer
       
  1571             appListBuf->ExpandL( pos, buf->Length() );
       
  1572             appListBuf->Write( pos, *buf, buf->Length() );
       
  1573 
       
  1574             pos += buf->Length();
       
  1575             bufPtr.Zero();
       
  1576             numOfEntries++;
       
  1577             } // if
       
  1578         CleanupStack::PopAndDestroy(); // wgName
       
  1579         } // for
       
  1580 
       
  1581     CleanupStack::PopAndDestroy(); // buf
       
  1582 
       
  1583     // Add number of entries to the beginning of the response
       
  1584     TBuf8<2> entries;
       
  1585     entries.Append( ( TUint8* ) ( &numOfEntries ), 2 );
       
  1586     appListBuf->ExpandL( 0, 2 );
       
  1587     appListBuf->Write( 0, entries, 2 );
       
  1588 
       
  1589     SendMessageL( EOk, appListBuf->Ptr( 0 ) );
       
  1590 
       
  1591     CleanupStack::PopAndDestroy( 3 ); // appListBuf, wgChain Close, wgChain delete
       
  1592 
       
  1593     HTI_LOG_FUNC_OUT( "CHtiAppControl::HandleListAppsL" );
       
  1594     }
       
  1595 
       
  1596 TBool CHtiAppControl::FindAppL( TApaAppInfo& aAppInfo,
       
  1597                                     const TDesC& aAppFullName )
       
  1598     {
       
  1599     HTI_LOG_FUNC_IN( "CHtiAppControl::FindAppL name" );
       
  1600     TInt err = KErrNone;
       
  1601     err = iAppServer.GetAllApps();
       
  1602     if ( err != KErrNone )
       
  1603         {
       
  1604         SendErrorMsg( err, KErrDescrFailedFindApp );
       
  1605         return EFalse;
       
  1606         }
       
  1607 
       
  1608     TParse toFind;
       
  1609     err = toFind.SetNoWild( aAppFullName, NULL, NULL );
       
  1610     if ( err != KErrNone )
       
  1611         {
       
  1612         SendErrorMsg( err, KErrDescrFailedFindApp );
       
  1613         return EFalse;
       
  1614         }
       
  1615 
       
  1616     TBool isFullName = ( toFind.DrivePresent() && toFind.PathPresent() );
       
  1617 
       
  1618     while ( ( err = iAppServer.GetNextApp( aAppInfo) ) == KErrNone )
       
  1619         {
       
  1620         if ( isFullName )  // assume that full path has been given
       
  1621             {
       
  1622             if ( !aAppFullName.CompareF( aAppInfo.iFullName )  )
       
  1623                 {
       
  1624                 return ETrue;
       
  1625                 }
       
  1626             }
       
  1627         else // assume that only filename has been given
       
  1628             {
       
  1629             TParsePtrC current( aAppInfo.iFullName );
       
  1630 
       
  1631             // does the given filename contain extension
       
  1632             if ( toFind.ExtPresent() )
       
  1633                 {
       
  1634                 // compare with filename + extension
       
  1635                 if ( !toFind.NameAndExt().CompareF( current.NameAndExt() ) )
       
  1636                     {
       
  1637                     return ETrue;
       
  1638                     }
       
  1639                 }
       
  1640             else
       
  1641                 {
       
  1642                 // compare with filename only
       
  1643                 if ( !toFind.Name().CompareF( current.Name() ) )
       
  1644                     {
       
  1645                     return ETrue;
       
  1646                     }
       
  1647                 // Try to match the caption.
       
  1648                 // For Java MIDlets the full name is like C:\270194328.fakeapp
       
  1649                 // so we have to use caption to find MIDlets.
       
  1650                 if ( !toFind.Name().CompareF( aAppInfo.iCaption ) )
       
  1651                     {
       
  1652                     return ETrue;
       
  1653                     }
       
  1654                 }
       
  1655             }
       
  1656         }
       
  1657 
       
  1658     if ( err == RApaLsSession::ENoMoreAppsInList )
       
  1659         {
       
  1660         SendMessageL( ENotFound );
       
  1661         }
       
  1662     else if ( err != KErrNone )
       
  1663         {
       
  1664         SendErrorMsg( err,KErrDescrFailedFindApp );
       
  1665         }
       
  1666     HTI_LOG_FUNC_OUT( "CHtiAppControl::FindAppL name" );
       
  1667     return EFalse;
       
  1668     }
       
  1669 
       
  1670 TBool CHtiAppControl::FindAppL( TApaAppInfo &aAppInfo,
       
  1671                                      const TInt32 aUid )
       
  1672     {
       
  1673     HTI_LOG_FUNC_IN( "CHtiAppControl::FindAppL uid" );
       
  1674     TInt err = iAppServer.GetAppInfo( aAppInfo, TUid::Uid( aUid ) );
       
  1675     if ( err == KErrNone )
       
  1676         {
       
  1677         return ETrue;
       
  1678         }
       
  1679     else if ( err == KErrNotFound )
       
  1680         {
       
  1681         SendMessageL( ENotFound );
       
  1682         }
       
  1683     else
       
  1684         {
       
  1685         SendErrorMsg( err , KErrDescrFailedFindApp );
       
  1686         }
       
  1687     HTI_LOG_FUNC_OUT( "CHtiAppControl::FindAppL uid" );
       
  1688     return EFalse;
       
  1689     }
       
  1690 
       
  1691 TApaTask CHtiAppControl::FindRunningRootApp( TUid aAppUid )
       
  1692     {
       
  1693     HTI_LOG_FUNC_IN( "CHtiAppControl::FindRunningRootApp" );
       
  1694     TApaTask task( iWs );
       
  1695     task.SetWgId( 0 ); // initialize to empty task
       
  1696 
       
  1697     TInt wgId = 0; // on first call to FindByAppUid wgId must be zero
       
  1698     CApaWindowGroupName::FindByAppUid( aAppUid, iWs, wgId );
       
  1699     HTI_LOG_FORMAT( "FindByAppUid returned WG ID: %d", wgId );
       
  1700 
       
  1701     RArray<RWsSession::TWindowGroupChainInfo> wgs;
       
  1702     TInt err = KErrNone;
       
  1703     TInt wgCount = 0;
       
  1704     if ( wgId != KErrNotFound )
       
  1705         {
       
  1706         // Get a list of Window Group Chain Infos
       
  1707         err = iWs.WindowGroupList( 0, &wgs ); // get only priority 0 WGs
       
  1708         wgCount = wgs.Count();
       
  1709         HTI_LOG_FORMAT( "WindowGroupList returned %d WGs", wgCount );
       
  1710         }
       
  1711     while ( wgId != KErrNotFound && task.WgId() == 0 && err == KErrNone )
       
  1712         {
       
  1713         // App was found - check if it is root by looping through the
       
  1714         // WG chain infos to find the one with out wgId
       
  1715         TInt i = 0;
       
  1716         while ( task.WgId() == 0 && i < wgCount )
       
  1717             {
       
  1718             const RWsSession::TWindowGroupChainInfo& info = wgs[i];
       
  1719             if ( info.iId == wgId && info.iParentId <= 0 )
       
  1720                 {
       
  1721                 // This is the one and it is root (does not have parent)
       
  1722                 task.SetWgId( wgId );
       
  1723                 }
       
  1724             i++;
       
  1725             }
       
  1726         if ( task.WgId() == 0 )
       
  1727             {
       
  1728             // This was not root - check if there's more instances of the app
       
  1729             CApaWindowGroupName::FindByAppUid( aAppUid, iWs, wgId );
       
  1730             HTI_LOG_FORMAT( "FindByAppUid returned WG ID: %d", wgId );
       
  1731             }
       
  1732         }
       
  1733 
       
  1734     wgs.Close();
       
  1735     HTI_LOG_FORMAT( "Returning task with WG ID %d", task.WgId() );
       
  1736     HTI_LOG_FUNC_OUT( "CHtiAppControl::FindRunningRootApp" );
       
  1737     return task;
       
  1738     }
       
  1739 
       
  1740 TInt CHtiAppControl::OpenProcessL( RProcess& aProcess,
       
  1741                                          const TDesC& aMatch )
       
  1742     {
       
  1743     HTI_LOG_FUNC_IN( "CHtiAppControl::OpenProcessL" );
       
  1744     TFullName processName;
       
  1745     TInt err = KErrNone;
       
  1746     TFindProcess finder( aMatch );
       
  1747 
       
  1748     err = finder.Next( processName );
       
  1749     if ( err == KErrNone )
       
  1750         {
       
  1751         err = aProcess.Open( finder );
       
  1752         }
       
  1753 
       
  1754     HTI_LOG_FUNC_OUT( "CHtiAppControl::OpenProcessL" );
       
  1755     return err;
       
  1756     }
       
  1757 
       
  1758 template<class T> T CHtiAppControl::Parse32(
       
  1759                         const TDesC8& a32int )
       
  1760     {
       
  1761     //manually construct TUint or TInt32
       
  1762     return T( a32int[0] + ( a32int[1] << 8 ) +
       
  1763                       ( a32int[2] << 16) + ( a32int[3] << 24) );
       
  1764     }
       
  1765 
       
  1766 void CHtiAppControl::SendMessageL( TAppCommand aResponseCode,
       
  1767                                          const TDesC8& aMsg )
       
  1768     {
       
  1769     HTI_LOG_FORMAT( "SendMessage %d", aResponseCode );
       
  1770     HTI_LOG_FORMAT( "Message len %d", aMsg.Length() );
       
  1771     HBufC8* sendMsg = HBufC8::NewL( 1 + aMsg.Length() );
       
  1772     CleanupStack::PushL( sendMsg );
       
  1773     sendMsg->Des().Append( aResponseCode );
       
  1774     sendMsg->Des().Append( aMsg );
       
  1775 
       
  1776     User::LeaveIfError( iDispatcher->DispatchOutgoingMessage(
       
  1777                             sendMsg,
       
  1778                             KAppServiceUid ) );
       
  1779     CleanupStack::Pop();
       
  1780     }
       
  1781 
       
  1782 inline TInt CHtiAppControl::SendErrorMsg( TInt anError,
       
  1783                                                 const TDesC8& aMsg )
       
  1784     {
       
  1785     return iDispatcher->DispatchOutgoingErrorMessage( anError,
       
  1786                                                aMsg,
       
  1787                                                KAppServiceUid );
       
  1788     }
       
  1789 
       
  1790 #if ( SYMBIAN_VERSION_SUPPORT < SYMBIAN_4 )
       
  1791 SwiUI::TPolicy CHtiAppControl::ConvertToPolicy( const TInt8 aValue )
       
  1792     {
       
  1793     if ( aValue == 0 ) return SwiUI::EPolicyNotAllowed;
       
  1794 
       
  1795     return  SwiUI::EPolicyAllowed;
       
  1796     }
       
  1797 #else
       
  1798 Usif::TSifPolicy CHtiAppControl::ConvertToSifPolicy( const TInt8 aValue )
       
  1799     {
       
  1800     if ( aValue == 0 ) return Usif::ENotAllowed;
       
  1801 
       
  1802     return  Usif::EAllowed;    
       
  1803     }
       
  1804 #endif
       
  1805 
       
  1806 TBool CHtiAppControl::ValidateInstallParams( const TDesC8& aParams, TBool aIsUnicode )
       
  1807     {
       
  1808     HTI_LOG_FORMAT( "ValidateInstallParams => length: %d", aParams.Length() );
       
  1809     if ( aParams.Length() > 0 )
       
  1810         {
       
  1811         TInt offset = 0;
       
  1812         TInt length = aParams[offset]; // inst package path length;
       
  1813         if ( aIsUnicode ) length *= 2;
       
  1814         offset++;
       
  1815         if ( aParams.Length() < offset + length )
       
  1816             {
       
  1817             HTI_LOG_TEXT( "ValidateInstallParams: Failed, data missing in inst pkg path" );
       
  1818             return EFalse;
       
  1819             }
       
  1820 
       
  1821         offset += length; // skip over inst package path
       
  1822         offset += 10;     // skip over the following one byte params
       
  1823 
       
  1824         if ( aParams.Length() < offset )
       
  1825             {
       
  1826             HTI_LOG_TEXT( "ValidateInstallParams: Failed, data missing in one byte params" );
       
  1827             return EFalse;
       
  1828             }
       
  1829 
       
  1830         length = aParams[offset]; // login username length;
       
  1831         if ( aIsUnicode ) length *= 2;
       
  1832         offset++;
       
  1833         if ( aParams.Length() < offset + length )
       
  1834             {
       
  1835             HTI_LOG_TEXT( "ValidateInstallParams: Failed, data missing in username" );
       
  1836             return EFalse;
       
  1837             }
       
  1838 
       
  1839         offset += length; // skip over login username
       
  1840 
       
  1841         length = aParams[offset]; // password length;
       
  1842         if ( aIsUnicode ) length *= 2;
       
  1843         offset++;
       
  1844         if ( aParams.Length() < offset + length )
       
  1845             {
       
  1846             HTI_LOG_TEXT( "ValidateInstallParams: Failed, data missing in password" );
       
  1847             return EFalse;
       
  1848             }
       
  1849 
       
  1850         offset += length; // skip over password
       
  1851         offset += 4;      // the last one byte params
       
  1852 
       
  1853         if ( aParams.Length() < offset || aParams.Length() > offset + 2)
       
  1854             {
       
  1855             HTI_LOG_TEXT( "ValidateInstallParams: Failed, final length incorrect" );
       
  1856             return EFalse;
       
  1857             }
       
  1858 
       
  1859         return ETrue;
       
  1860         }
       
  1861 
       
  1862     return EFalse;
       
  1863     }
       
  1864 
       
  1865 #if ( SYMBIAN_VERSION_SUPPORT < SYMBIAN_4 )
       
  1866 TInt CHtiAppControl::GetPackageUidL( const TDesC& aPackageName,
       
  1867                                           TInt aMimeIndex )
       
  1868     {
       
  1869     HTI_LOG_FUNC_IN( "CHtiAppControl::GetPackageUidL" );
       
  1870 
       
  1871     if ( aMimeIndex >= 0 && aMimeIndex < 2 ) // SIS
       
  1872         {
       
  1873         Swi::RSisRegistrySession regSession;
       
  1874         User::LeaveIfError( regSession.Connect() );
       
  1875         CleanupClosePushL( regSession );
       
  1876 
       
  1877         RArray<TUid> uids;
       
  1878         CleanupClosePushL( uids );
       
  1879         regSession.InstalledUidsL( uids );
       
  1880         TInt count = uids.Count();
       
  1881         HTI_LOG_FORMAT( "Found %d SISx installations", count );
       
  1882         for ( TInt i = 0; i < count; i++ )
       
  1883             {
       
  1884             Swi::RSisRegistryEntry entry;
       
  1885             CleanupClosePushL( entry );
       
  1886             User::LeaveIfError( entry.Open( regSession, uids[i] ) );
       
  1887             if ( !entry.IsInRomL() && entry.IsPresentL() )
       
  1888                 {
       
  1889                 if ( aPackageName.Compare( *( entry.PackageNameL() ) ) == 0 )
       
  1890                     {
       
  1891                     HTI_LOG_TEXT( "Matching SIS package found" );
       
  1892                     TInt uid = entry.UidL().iUid;
       
  1893                     CleanupStack::PopAndDestroy( 3 ); // entry, uids, regSession
       
  1894                     return uid;
       
  1895                     }
       
  1896                 }
       
  1897             // Check augmentations of this entry
       
  1898             entry.AugmentationsL( iAugmentations );
       
  1899             TInt augCount = iAugmentations.Count();
       
  1900             for ( TInt j = 0; j < augCount; j++ )
       
  1901                 {
       
  1902                 Swi::RSisRegistryEntry augmentation;
       
  1903                 CleanupClosePushL( augmentation );
       
  1904                 augmentation.OpenL( regSession, *iAugmentations[j] );
       
  1905                 if ( aPackageName.Compare(
       
  1906                         *( augmentation.PackageNameL() ) ) == 0 )
       
  1907                     {
       
  1908                     if ( !augmentation.IsInRomL() && augmentation.IsPresentL() )
       
  1909                         {
       
  1910                         HTI_LOG_TEXT( "Matching SIS augmentation found" );
       
  1911                         TInt uid = augmentation.UidL().iUid;
       
  1912                         Swi::CSisRegistryPackage* pkg = augmentation.PackageL();
       
  1913                         iAugmentationIndex = pkg->Index();
       
  1914                         delete pkg;
       
  1915                         HTI_LOG_FORMAT( "Aug. index %d", iAugmentationIndex );
       
  1916                         CleanupStack::PopAndDestroy( 4 ); // augmentation, entry, uids, regSession
       
  1917                         return uid;
       
  1918                         }
       
  1919                     }
       
  1920                 CleanupStack::PopAndDestroy(); // augmentation
       
  1921                 } // for j
       
  1922             iAugmentations.ResetAndDestroy();
       
  1923             CleanupStack::PopAndDestroy(); // entry
       
  1924             } // for i
       
  1925         User::Leave( KErrNotFound );
       
  1926         }
       
  1927 
       
  1928     else if ( aMimeIndex > 2 && aMimeIndex < 7 ) // Java
       
  1929         {
       
  1930             RArray<TUid> uids;
       
  1931             CleanupClosePushL( uids );
       
  1932 
       
  1933             CJavaRegistry* javaRegistry = CJavaRegistry::NewLC();
       
  1934             javaRegistry->GetRegistryEntryUidsL( uids );
       
  1935 
       
  1936             TInt uid = KErrNotFound;
       
  1937             TInt count = uids.Count();
       
  1938             HTI_LOG_FORMAT( "Found %d Java installations", count );
       
  1939             for ( TInt i = 0; i < count; i++ )
       
  1940                 {
       
  1941                 CJavaRegistryEntry* entry = javaRegistry->RegistryEntryL( uids[i] );
       
  1942                 if ( entry )
       
  1943                     {
       
  1944                     CleanupStack::PushL( entry );
       
  1945                     if ( entry->Type() >= EGeneralPackage &&
       
  1946                             entry->Type() < EGeneralApplication )
       
  1947                         {
       
  1948                         // entry was a package (MIDlet suite)
       
  1949                         CJavaRegistryPackageEntry* packageEntry =
       
  1950                             ( CJavaRegistryPackageEntry* ) entry;
       
  1951                         if ( aPackageName.Compare( packageEntry->Name() ) == 0 )
       
  1952                             {
       
  1953                             HTI_LOG_TEXT( "Matching Java installation found" );
       
  1954                             uid = packageEntry->Uid().iUid;
       
  1955                             CleanupStack::PopAndDestroy( entry );
       
  1956                             break;
       
  1957                             }
       
  1958                         }
       
  1959                     CleanupStack::PopAndDestroy( entry );
       
  1960                     }
       
  1961                 }
       
  1962             CleanupStack::PopAndDestroy( javaRegistry );
       
  1963             CleanupStack::PopAndDestroy( &uids );
       
  1964             User::LeaveIfError( uid );
       
  1965             return uid;
       
  1966         }
       
  1967 
       
  1968     else if ( aMimeIndex == 7 ) // Widget
       
  1969         {
       
  1970         RWidgetRegistryClientSession widgetRegistry;
       
  1971         User::LeaveIfError( widgetRegistry.Connect() );
       
  1972 
       
  1973         RWidgetInfoArray widgets;
       
  1974         TRAPD( err, widgetRegistry.InstalledWidgetsL( widgets ) );
       
  1975         if ( err != KErrNone )
       
  1976             {
       
  1977             HTI_LOG_FORMAT( "Failed to get installed widgets %d", err );
       
  1978             widgets.ResetAndDestroy();
       
  1979             widgetRegistry.Disconnect();
       
  1980             User::Leave( err );
       
  1981             }
       
  1982 
       
  1983         TInt uid = KErrNotFound;
       
  1984         TInt count = widgets.Count();
       
  1985         HTI_LOG_FORMAT( "Found %d Widget installations", count );
       
  1986         for ( TInt i = 0; i < count; i++ )
       
  1987             {
       
  1988             CWidgetInfo* widgetInfo = widgets[i];
       
  1989             HTI_LOG_DES( *( widgetInfo->iBundleName ) );
       
  1990             if ( aPackageName.Compare( *( widgetInfo->iBundleName ) ) == 0 )
       
  1991                 {
       
  1992                 HTI_LOG_TEXT( "Matching Widget installation found" );
       
  1993                 uid = widgetInfo->iUid.iUid;
       
  1994                 break;
       
  1995                 }
       
  1996             }
       
  1997         widgets.ResetAndDestroy();
       
  1998         widgetRegistry.Disconnect();
       
  1999         User::LeaveIfError( uid );
       
  2000         return uid;
       
  2001         }
       
  2002 
       
  2003     else  // invalid mime index
       
  2004         {
       
  2005         User::Leave( KErrArgument );
       
  2006         }
       
  2007 
       
  2008     return KErrNone; // never returns from here
       
  2009     }
       
  2010 #else
       
  2011 TInt CHtiAppControl::GetComponentIdFromUid(const TInt32 aUid)
       
  2012     {
       
  2013     TInt cid = KErrNotFound;
       
  2014     Usif::RSoftwareComponentRegistry registry;
       
  2015     User::LeaveIfError(registry.Connect());
       
  2016     CleanupClosePushL(registry);
       
  2017     RArray<TUid> uidList;
       
  2018     CleanupClosePushL(uidList);
       
  2019     RArray<Usif::TComponentId> componentIdList;
       
  2020     CleanupClosePushL(componentIdList);
       
  2021     registry.GetComponentIdsL(componentIdList);
       
  2022     for(TInt i = 0; i < componentIdList.Count(); i++)
       
  2023         {
       
  2024         Usif::TComponentId compId = componentIdList[i];
       
  2025         Usif::CComponentEntry *compEntry = Usif::CComponentEntry::NewLC();
       
  2026         if(registry.GetComponentL(compId, *compEntry))
       
  2027             {
       
  2028             /*if(compEntry->IsRemovable() && 
       
  2029                  compEntry->SoftwareType() == Usif::KSoftwareTypeNative)*/
       
  2030             if(compEntry->IsRemovable())
       
  2031                 {
       
  2032                 _LIT(KCompUid, "CompUid");
       
  2033                 Usif::CPropertyEntry *property = 
       
  2034                     registry.GetComponentPropertyL(compId, KCompUid);
       
  2035                 CleanupStack::PushL(property);
       
  2036                 Usif::CIntPropertyEntry* intProperty = 
       
  2037                     dynamic_cast<Usif::CIntPropertyEntry*>(property);
       
  2038                 uidList.AppendL(TUid::Uid(intProperty->IntValue()));
       
  2039                 CleanupStack::PopAndDestroy(property);
       
  2040                 }
       
  2041             else
       
  2042                 {
       
  2043                 uidList.AppendL(KNullUid);
       
  2044                 }
       
  2045             }
       
  2046         CleanupStack::PopAndDestroy( compEntry );
       
  2047         }
       
  2048     TUid tuid(TUid::Uid(aUid));
       
  2049     if(tuid != KNullUid)
       
  2050         {
       
  2051         TInt index = uidList.Find(tuid);
       
  2052         if(index >= 0 && index < componentIdList.Count())
       
  2053             {
       
  2054             cid = componentIdList[index];
       
  2055             }
       
  2056         }
       
  2057         CleanupStack::PopAndDestroy( 3, &registry );// componentIdList, uidList, registry
       
  2058         return cid;
       
  2059     }
       
  2060 
       
  2061 TInt CHtiAppControl::GetComponentIdFromPackageName(const TDesC& aPackageName)
       
  2062     {
       
  2063     TInt cid = KErrNotFound;
       
  2064     Usif::RSoftwareComponentRegistry registry;
       
  2065     User::LeaveIfError(registry.Connect());
       
  2066     CleanupClosePushL(registry);
       
  2067     RArray<Usif::TComponentId> componentIdList;
       
  2068     CleanupClosePushL(componentIdList);
       
  2069     registry.GetComponentIdsL(componentIdList);
       
  2070     TInt count = componentIdList.Count();    
       
  2071     for(TInt i = 0; i < count; i++)
       
  2072         {
       
  2073         Usif::TComponentId compId = componentIdList[i];
       
  2074         Usif::CComponentEntry *compEntry = Usif::CComponentEntry::NewLC();
       
  2075         if(registry.GetComponentL(compId, *compEntry))
       
  2076             {
       
  2077             if(compEntry->IsRemovable())
       
  2078                 {
       
  2079                 if ( aPackageName.Compare( compEntry->Name() ) == 0 )
       
  2080                     {
       
  2081                     cid = compId;
       
  2082                     CleanupStack::PopAndDestroy( compEntry );
       
  2083                     break;
       
  2084                     }
       
  2085                 }
       
  2086             }
       
  2087         CleanupStack::PopAndDestroy( compEntry );
       
  2088         }
       
  2089         CleanupStack::PopAndDestroy( 2, &registry );// componentIdList, registry
       
  2090         return cid;
       
  2091     }
       
  2092 
       
  2093 #endif
       
  2094 // End of File