widgets/widgetlauncher/src/WidgetLauncher.cpp
changeset 0 dd21522fd290
child 1 7c90e6132015
equal deleted inserted replaced
-1:000000000000 0:dd21522fd290
       
     1 /*
       
     2 * Copyright (c) 2006, 2008 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of the License "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: 
       
    15 *     Lauches/inform widget stub ui to display widget
       
    16 *
       
    17 *
       
    18 */
       
    19 
       
    20 
       
    21 // INCLUDE FILES
       
    22 
       
    23 #include "widgetappdefs.rh"
       
    24 #include <APACMDLN.H>
       
    25 #include <s32mem.h>
       
    26 #include <e32std.h>
       
    27 #include <e32base.h>
       
    28 #include <f32file.h>
       
    29 #include <apgcli.h>
       
    30 #include <W32STD.H>
       
    31 #include <APGTASK.H>
       
    32 
       
    33 // CONSTANTS
       
    34 
       
    35 // LOCAL FUNCTION DEFINITIONS
       
    36 // RDesWriteStream
       
    37 /** 
       
    38 * Parse command line in order to launch the widget.
       
    39 *
       
    40 * The function checks the command line arguments and parses 
       
    41 * relevant information from it. Parameters are all out-params.
       
    42 * @param aUid UID of the widget.
       
    43 * @param aOperation Operation to perform.
       
    44 */
       
    45 static void ParseCmdLineL( TUid& aUid, TUint32& aOperation );
       
    46 
       
    47 /** 
       
    48 * Launch or bring foreground the asked widget.
       
    49 *
       
    50 * Launch widget.
       
    51 * @param aUid UID of the widget.
       
    52 * @param aOperation Operation to perform.
       
    53 */
       
    54 static void LaunchWidgetL( const TUid& aUid, TUint32 aOperation );
       
    55 
       
    56 /** 
       
    57 * Launch new widget.
       
    58 *
       
    59 * Launch new widget.
       
    60 * @param aUid UID of the widget.
       
    61 * @param aOperation Operation to perform.
       
    62 */
       
    63 static void LaunchWidgetUIL( 
       
    64     const TUid& aUid, 
       
    65     const TDesC8& aMessage, 
       
    66     TUint32 aOperation );
       
    67 
       
    68 //===========================================================================
       
    69 // Start the stuff.
       
    70 //===========================================================================
       
    71 void StartTheApplicationL()
       
    72     {
       
    73     TUid uid( TUid::Null() );
       
    74     TUint32 operation( 0 );
       
    75     
       
    76     ParseCmdLineL( uid, operation );
       
    77     LaunchWidgetL( uid, operation );
       
    78     }
       
    79     
       
    80 //===========================================================================
       
    81 // Parse command line.
       
    82 // The function checks the command line arguments to launch the widget.
       
    83 // 
       
    84 // Too bad that we can't have version number information in the beginning 
       
    85 // of the command line. Now we have to try/catch the miniview size.
       
    86 
       
    87 // The format is originally:
       
    88 // <UID.iUid> <url lenght> <url>
       
    89 // 
       
    90 // For miniview, we append miniview size as in 
       
    91 // <TUint32>, meaning
       
    92 //      - 0 - launch to full screen
       
    93 //      - 1 - launch to mini view
       
    94 //      - 2 - deactivate    
       
    95 //
       
    96 //===========================================================================
       
    97 void ParseCmdLineL( TUid& aUid, TUint32& aOperation )
       
    98     {
       
    99     CApaCommandLine* commandLine( NULL );
       
   100     
       
   101     // KApaMaxCommandLine says this can be only 0x100, but we can't break the 
       
   102     // data format.
       
   103     TUint32 mainHTMLLen( 0 ); 
       
   104     HBufC* mainHTML( NULL );
       
   105     TPtr mainHTMLPtr( NULL, 0, 0 );
       
   106     TPtrC8 data( NULL, 0 );
       
   107     
       
   108     User::LeaveIfError( CApaCommandLine::GetCommandLineFromProcessEnvironment( commandLine) );
       
   109     
       
   110     data.Set( commandLine->OpaqueData() );
       
   111     RDesReadStream readStream( data );
       
   112     
       
   113     // Read widget uid from opaque data
       
   114     aUid.iUid = readStream.ReadUint32L();
       
   115     
       
   116     // Read widget main html path from opaque data
       
   117     mainHTMLLen = readStream.ReadUint32L();
       
   118     
       
   119     if ( !mainHTMLLen )
       
   120         {
       
   121         User::Leave( KErrArgument );
       
   122         }
       
   123             
       
   124     mainHTML = HBufC::NewLC(mainHTMLLen);
       
   125 
       
   126     mainHTMLPtr.Set( mainHTML->Des() );
       
   127     readStream.ReadL(mainHTMLPtr, mainHTMLLen);
       
   128     
       
   129     // Dump the URL, we don't need it anymore.    
       
   130     CleanupStack::PopAndDestroy( mainHTML );
       
   131 
       
   132     // Try to get the size.
       
   133     TRAP_IGNORE( aOperation = readStream.ReadInt32L() );
       
   134     }
       
   135     
       
   136 //===========================================================================
       
   137 // Launch widget.
       
   138 //===========================================================================
       
   139 //
       
   140 void LaunchWidgetL( const TUid& aUid, TUint32 aOperation )
       
   141     {
       
   142     __UHEAP_MARK;
       
   143     
       
   144     TUid widgetAppUid( TUid::Uid( KWidgetAppUid ) );
       
   145     
       
   146     RWsSession wsSession;
       
   147     TApaTaskList taskList( wsSession );
       
   148     HBufC8* message( HBufC8::NewLC( KWidgetUiMaxMessageLength ) );
       
   149     TPtr8 des( message->Des() );
       
   150     RDesWriteStream stream( des );
       
   151     
       
   152     CleanupClosePushL( stream );
       
   153     
       
   154     // Make the message to be sent.
       
   155     stream.WriteUint32L( 1 );
       
   156     stream.WriteUint32L( aUid.iUid );
       
   157     stream.WriteInt32L( aOperation );
       
   158         
       
   159     CleanupStack::PopAndDestroy( &stream );
       
   160     
       
   161     // Create Window server session
       
   162     User::LeaveIfError( wsSession.Connect() );
       
   163     CleanupClosePushL( wsSession );
       
   164 
       
   165     // Get the task list
       
   166     // Try to find out if stub ui is already running
       
   167     TApaTask task = taskList.FindApp( widgetAppUid );
       
   168 
       
   169     if ( task.Exists() )
       
   170         {
       
   171         // TODO make something here, or not...
       
   172         widgetAppUid = TUid::Uid( 1 );
       
   173         if ( aOperation == LaunchFullscreen ||
       
   174              aOperation == WidgetSelect )
       
   175             {
       
   176             task.BringToForeground();
       
   177             }
       
   178         task.SendMessage( widgetAppUid, des );
       
   179         }
       
   180     else
       
   181         {
       
   182         // TODO CONST
       
   183         if ( aOperation == LaunchFullscreen ||
       
   184              aOperation == LaunchMiniview ||
       
   185              aOperation == WidgetSelect ) //WidgetUI has died -> re-launch
       
   186             {
       
   187             LaunchWidgetUIL( widgetAppUid, *message, aOperation );
       
   188             }
       
   189         }
       
   190         
       
   191     CleanupStack::PopAndDestroy( 2, message );
       
   192     
       
   193     __UHEAP_MARKEND;
       
   194     }
       
   195 
       
   196 //===========================================================================
       
   197 // Launch Widget UI.
       
   198 //===========================================================================
       
   199 void LaunchWidgetUIL( 
       
   200     const TUid& aUid, 
       
   201     const TDesC8& aMessage, 
       
   202     TUint32 aOperation )
       
   203     {
       
   204     HBufC* document( NULL );
       
   205     CApaCommandLine* line( CApaCommandLine::NewLC() );
       
   206     TApaAppInfo info;
       
   207     RApaLsSession session;
       
   208     
       
   209     User::LeaveIfError( session.Connect() );
       
   210     CleanupClosePushL( session );
       
   211     
       
   212     User::LeaveIfError( session.GetAppInfo( info, aUid ) );
       
   213         
       
   214     document = HBufC::NewMaxLC( TReal( TReal( aMessage.Length() )  / 2.0 ) + 0.5 );
       
   215 
       
   216     Mem::Copy( 
       
   217         reinterpret_cast< TUint8* >( const_cast< TUint16* >( document->Ptr() ) ),
       
   218         aMessage.Ptr(),
       
   219         KWidgetUiMaxMessageLength );
       
   220         
       
   221     line->SetDocumentNameL( *document );
       
   222     line->SetExecutableNameL( info.iFullName );
       
   223         
       
   224     // TODO make const definitions.
       
   225     if ( aOperation == 1 )
       
   226         {
       
   227         line->SetCommandL( EApaCommandBackground );
       
   228         }
       
   229         
       
   230     session.StartApp( *line );
       
   231 
       
   232     CleanupStack::PopAndDestroy( 3, line );
       
   233     }
       
   234 
       
   235 /*
       
   236 * E32Main: called by operating system to start the program
       
   237 */
       
   238 TInt E32Main(void)
       
   239     {
       
   240     CTrapCleanup* cleanupStack = CTrapCleanup::New();
       
   241     if (cleanupStack == NULL)
       
   242         {
       
   243         return -1;
       
   244         }
       
   245     TRAP_IGNORE(StartTheApplicationL());
       
   246 
       
   247     delete cleanupStack;
       
   248     return KErrNone;
       
   249     }
       
   250 
       
   251 
       
   252 
       
   253 
       
   254