iaupdate/IAD/api/client/src/iaupdateclient.cpp
branchRCL_3
changeset 25 7333d7932ef7
parent 18 f9033e605ee2
child 26 8b7f4e561641
equal deleted inserted replaced
24:5cc91383ab1e 25:7333d7932ef7
     9 * Initial Contributors:
     9 * Initial Contributors:
    10 * Nokia Corporation - initial contribution.
    10 * Nokia Corporation - initial contribution.
    11 *
    11 *
    12 * Contributors:
    12 * Contributors:
    13 *
    13 *
    14 * Description:   This module contains the implementation of RIAUpdateClient
    14 * Description:   This module contains the implementation of IAUpdateClient
    15 *                class member functions.
    15 *                class member functions.
    16 *
    16 *
    17 */
    17 */
    18 
    18 
    19 
    19 
    20 
    20 
    21 //INCLUDES
    21 //INCLUDES
    22 #include <eikenv.h>
    22 #include <eikenv.h>
    23 #include <apgcli.h>
    23 #include <apgcli.h>
    24 #include <e32math.h>
    24 #include <e32math.h>
       
    25 #include <iaupdateparameters.h>
       
    26 #include <iaupdateresult.h>
       
    27 #include <xqservicerequest.h>
       
    28 #include <xqserviceutil.h>
       
    29 #include <xqrequestinfo.h>
       
    30 
    25 #include "iaupdateclient.h"
    31 #include "iaupdateclient.h"
    26 #include "iaupdateclientdefines.h"
    32 #include "iaupdateclientdefines.h"
    27 #include "iaupdatetools.h"
       
    28 #include "iaupdatedebug.h"
    33 #include "iaupdatedebug.h"
    29 
    34 
    30 
    35 
    31 // -----------------------------------------------------------------------------
    36 // -----------------------------------------------------------------------------
    32 // RIAUpdateClient::RIAUpdateClient
    37 // IAUpdateClient::IAUpdateClient
    33 // 
    38 // 
    34 // -----------------------------------------------------------------------------
    39 // -----------------------------------------------------------------------------
    35 // 
    40 // 
    36 RIAUpdateClient::RIAUpdateClient() 
    41 IAUpdateClient::IAUpdateClient(MIAUpdateObserver& observer):
    37 : iPtr1( NULL, 0 ),
    42     mObserver(observer)
    38   iPtr2( NULL, 0 ),
    43     {
    39   iPtr3( NULL, 0 )
    44     mServiceRequest = NULL;
    40     {
    45     mCurrentRequestType = NoOperation;
    41     }
    46     }
    42 
    47 
    43 
    48 // -----------------------------------------------------------------------------
    44 // -----------------------------------------------------------------------------
    49 // IAUpdateClient::~IAUpdateClient
    45 // RIAUpdateClient::Open
    50 // 
    46 // 
    51 // -----------------------------------------------------------------------------
    47 // -----------------------------------------------------------------------------
    52 // 
    48 // 
    53 IAUpdateClient::~IAUpdateClient() 
    49 TInt RIAUpdateClient::Open()
    54     {
    50     {
    55     if ( mServiceRequest)
    51     IAUPDATE_TRACE("[IAUPDATE] RIAUpdateClient::Open() begin");
    56         {
    52     TInt error( KErrNone );
    57         delete mServiceRequest;
       
    58         }
       
    59     }
       
    60 
       
    61 
       
    62 // -----------------------------------------------------------------------------
       
    63 // IAUpdateClient::initRequest
       
    64 // 
       
    65 // -----------------------------------------------------------------------------
       
    66 // 
       
    67 int IAUpdateClient::initRequest(const CIAUpdateParameters* updateParameters, const QString& message, bool toBackground)
       
    68 {
       
    69     IAUPDATE_TRACE("[IAUPDATE] IAUpdateClient::initRequest() begin");
       
    70     int error(KErrNone);
       
    71     if (mCurrentRequestType != NoOperation) 
       
    72         {
       
    73         error = KErrServerBusy;
       
    74         }
       
    75     else if (!mServiceRequest)
       
    76     {
       
    77         mServiceRequest = new XQServiceRequest("com.nokia.services.swupdate.swupdate_interface", message, false);
       
    78         if (mServiceRequest)
       
    79         {    
       
    80             connect(mServiceRequest, SIGNAL(requestCompleted(QVariant)), this, SLOT(requestCompleted(QVariant)));
       
    81             connect(mServiceRequest, SIGNAL(requestError(int)), this, SLOT(requestError(int)));
       
    82         }
       
    83         else
       
    84         {
       
    85             error = KErrNoMemory;
       
    86         }
       
    87     }
       
    88     else
       
    89     {
       
    90         mServiceRequest->setMessage(message);
       
    91     }
    53     
    92     
    54     if ( !iConnected )
       
    55         {
       
    56         TRAP( error, ConnectNewAppL( ServiceUid() ) );
       
    57         if ( error == KErrNone ) 
       
    58             {
       
    59             iConnected = ETrue;
       
    60             CEikonEnv* eikEnv = CEikonEnv::Static();
       
    61             if ( eikEnv )
       
    62                 {
       
    63             	RWindowGroup owngroup;
       
    64 		        iOwnWgId = eikEnv->RootWin().Identifier(); 
       
    65 		        
       
    66 	            TPckg<TInt> wgId( iOwnWgId );
       
    67 	            delete iData;
       
    68 	            iData = NULL;
       
    69 	             TRAP_IGNORE( iData = wgId.AllocL() );
       
    70 	            
       
    71                 TIpcArgs args;
       
    72                 args.Set( 0, iData );
       
    73                 SendReceive( IAUpdateClientDefines::EIAUpdateServerSendWgId, args );    
       
    74 	            } 
       
    75             }
       
    76         }
       
    77     IAUPDATE_TRACE_1("[IAUPDATE] RIAUpdateClient::Open() end error code: %d", error );
       
    78     return error;
       
    79     }
       
    80 
       
    81 // -----------------------------------------------------------------------------
       
    82 // RIAUpdateClient::OpenToBackroundAsync
       
    83 // 
       
    84 // -----------------------------------------------------------------------------
       
    85 //
       
    86 TInt RIAUpdateClient::OpenToBackroundAsync( TRequestStatus& aStatus )
       
    87     {
       
    88     IAUPDATE_TRACE("[IAUPDATE] RIAUpdateClient::OpenToBackroundAsync() begin");
       
    89     TInt error( KErrNone );
       
    90     if ( !iConnected )
       
    91         {
       
    92         TRAP( error, StartNewAppToBackgroundL( ServiceUid(), aStatus ) );
       
    93         }
       
    94     IAUPDATE_TRACE_1("[IAUPDATE] RIAUpdateClient::OpenToBackroundAsync() end error code: %d", error );
       
    95     return error;
       
    96     }
       
    97 
       
    98 // -----------------------------------------------------------------------------
       
    99 // RIAUpdateClient::ConnectToApp
       
   100 // 
       
   101 // -----------------------------------------------------------------------------
       
   102 //
       
   103 TInt RIAUpdateClient::ConnectToApp()
       
   104     {
       
   105     IAUPDATE_TRACE("[IAUPDATE] RIAUpdateClient::ConnectToApp() begin");
       
   106     TInt error( KErrNone );
       
   107     TName serverName;
       
   108     ServerName(serverName, ServiceUid(), iDifferentiator);
       
   109     TRAP( error,ConnectExistingByNameL( serverName ) );
       
   110     
    93     
   111     if ( error == KErrNone ) 
    94     if (error == KErrNone)
   112         {
    95     {    
   113         iConnected = ETrue;
    96         XQRequestInfo requestInfo;
       
    97         requestInfo.setBackground(toBackground);
       
    98         mServiceRequest->setInfo(requestInfo); 
       
    99         int wgId = 0;
   114         CEikonEnv* eikEnv = CEikonEnv::Static();
   100         CEikonEnv* eikEnv = CEikonEnv::Static();
   115         if ( eikEnv )
   101         if ( eikEnv )
       
   102         {
       
   103             RWindowGroup owngroup;
       
   104             wgId = eikEnv->RootWin().Identifier();
       
   105         }
       
   106         IAUPDATE_TRACE_1("IAUpdateClient::initRequest() wgId: %d", wgId);
       
   107         QString stringWgid;
       
   108         stringWgid.setNum(wgId);
       
   109         *mServiceRequest << stringWgid;  
       
   110         if (updateParameters)
       
   111         {    
       
   112             IAUPDATE_TRACE_1("[IAUPDATE] IAUpdateClient::initRequest() UID: %d", updateParameters->Uid().iUid);
       
   113             QString stringUid; 
       
   114             stringUid.setNum(updateParameters->Uid().iUid);
       
   115             *mServiceRequest << stringUid;
       
   116                 
       
   117             IAUPDATE_TRACE_1("[IAUPDATE] IAUpdateClient::initRequest() searchcriteria: %S", &updateParameters->SearchCriteria());
       
   118             *mServiceRequest << qStringFromTDesC(updateParameters->SearchCriteria());
       
   119                 
       
   120             IAUPDATE_TRACE_1("[IAUPDATE] IAUpdateClient::initRequest() executable: %S", &updateParameters->CommandLineExecutable());
       
   121             *mServiceRequest << qStringFromTDesC(updateParameters->CommandLineExecutable());
       
   122                 
       
   123             IAUPDATE_TRACE_1("[IAUPDATE] IAUpdateClient::initRequest() arguments: %S8", &updateParameters->CommandLineArguments());
       
   124             *mServiceRequest << qStringFromTDesC8(updateParameters->CommandLineArguments());
       
   125                 
       
   126             IAUPDATE_TRACE_1("[IAUPDATE] IAUpdateClient::initRequest() show progress: %d", updateParameters->ShowProgress());
       
   127             QString stringShowProgress;
       
   128             stringShowProgress.setNum(updateParameters->ShowProgress());
       
   129             *mServiceRequest << stringShowProgress;
       
   130                 
       
   131             IAUPDATE_TRACE_1("[IAUPDATE] IAUpdateClient::initRequest() importance: %d", updateParameters->Importance());
       
   132             QString stringImportance;
       
   133             stringImportance.setNum(updateParameters->Importance());
       
   134             *mServiceRequest << stringImportance;
       
   135                 
       
   136             IAUPDATE_TRACE_1("[IAUPDATE] IAUpdateClient::initRequest() type: %d", updateParameters->Type());
       
   137             QString stringType;
       
   138             stringType.setNum(updateParameters->Type());
       
   139             *mServiceRequest << stringType;
       
   140                 
       
   141             IAUPDATE_TRACE_1("[IAUPDATE] IAUpdateClient::initRequest() refresh: %d", updateParameters->Refresh());
       
   142             QString stringRefresh;
       
   143             stringRefresh.setNum(updateParameters->Refresh());
       
   144             *mServiceRequest << stringRefresh;
       
   145         }
       
   146     }                 
       
   147      
       
   148     
       
   149     IAUPDATE_TRACE_1("[IAUPDATE] IAUpdateClient::initRequest() error code: %d", error );
       
   150     IAUPDATE_TRACE("[IAUPDATE] IAUpdateClient::initRequest() end");
       
   151     return error;
       
   152 }
       
   153 
       
   154 // -----------------------------------------------------------------------------
       
   155 // IAUpdateClient::checkUpdates
       
   156 // 
       
   157 // -----------------------------------------------------------------------------
       
   158 //
       
   159 void IAUpdateClient::checkUpdates(const CIAUpdateParameters& updateParameters)
       
   160     {
       
   161     IAUPDATE_TRACE("[IAUPDATE] IAUpdateClient::checkUpdates()");
       
   162     QString message("checkUpdates(QString,QString,QString,QString,QString,QString,QString,QString,QString)");
       
   163     //QString message("checkUpdates(int,int)");
       
   164     int ret = initRequest(&updateParameters,message,!updateParameters.ShowProgress());
       
   165     if (ret == KErrNone)
       
   166         {
       
   167         if (mServiceRequest->send()) 
   116             {
   168             {
   117             RWindowGroup owngroup;
   169             mCurrentRequestType = CheckUpdates;
   118             iOwnWgId = eikEnv->RootWin().Identifier(); 
   170             }
       
   171         else
       
   172             {
       
   173             mObserver.CheckUpdatesComplete(ret,0);        
       
   174             }
       
   175         }
       
   176     }
       
   177 
       
   178 // -----------------------------------------------------------------------------
       
   179 // IAUpdateClient::showUpdates
       
   180 // 
       
   181 // -----------------------------------------------------------------------------
       
   182 //
       
   183 void IAUpdateClient::showUpdates(const CIAUpdateParameters& updateParameters)
       
   184     {
       
   185     IAUPDATE_TRACE("[IAUPDATE] IAUpdateClient::showUpdates()");
       
   186     QString message("showUpdates(QString,QString,QString,QString,QString,QString,QString,QString,QString)");
       
   187     int ret = initRequest(&updateParameters, message, false);
       
   188     if (ret == KErrNone)
       
   189         {
       
   190         if (mServiceRequest->send())
       
   191             {
       
   192             mCurrentRequestType = ShowUpdates;
       
   193             }
       
   194         else
       
   195             {
       
   196             mObserver.UpdateComplete(ret,NULL);
       
   197             }
       
   198         }
       
   199     }
       
   200     
       
   201 
       
   202 // -----------------------------------------------------------------------------
       
   203 // IAUpdateClient::updateQuery
       
   204 // 
       
   205 // -----------------------------------------------------------------------------
       
   206 //    
       
   207 void IAUpdateClient::updateQuery()
       
   208 {
       
   209     IAUPDATE_TRACE("[IAUPDATE] IAUpdateClient::updateQuery() begin");
       
   210     QString message("updateQuery(QString)");
       
   211     CIAUpdateParameters* nullParameters = NULL;
       
   212     int ret = initRequest(nullParameters, message, false);
       
   213     if (ret == KErrNone)
       
   214     {
       
   215         if (mServiceRequest->send()) 
       
   216         {
       
   217             mCurrentRequestType = UpdateQuery;
       
   218         }
       
   219         else
       
   220         {
       
   221             mObserver.UpdateQueryComplete(ret,false);
       
   222         }
       
   223     }
       
   224 	IAUPDATE_TRACE("[IAUPDATE] IAUpdateClient::updateQuery() end");    
       
   225 }
       
   226 
       
   227 // -----------------------------------------------------------------------------
       
   228 // IAUpdateClient::update
       
   229 // 
       
   230 // -----------------------------------------------------------------------------
       
   231 // 
       
   232 void IAUpdateClient::update()
       
   233 {
       
   234     mObserver.UpdateComplete(KErrNotSupported,NULL);
       
   235 }
       
   236 
       
   237 // -----------------------------------------------------------------------------
       
   238 // IAUpdateClient::broughtToForeground
       
   239 // 
       
   240 // -----------------------------------------------------------------------------
       
   241 //
       
   242 void IAUpdateClient::broughtToForeground()
       
   243 {
       
   244     IAUPDATE_TRACE("[IAUPDATE] IAUpdateClient::broughtToForeground() begin");
       
   245     if (mServiceRequest)
       
   246     {
       
   247         QString message("broughtToForeground(int)");
       
   248         CIAUpdateParameters* nullParameters = NULL;
       
   249         int ret = initRequest(nullParameters, message, false);
       
   250         if (ret == KErrNone)
       
   251         {
       
   252             if (mServiceRequest->send())
       
   253             {
       
   254                 mCurrentRequestType = BroughtToForeground;
       
   255             }
       
   256         }
       
   257     } 
       
   258     IAUPDATE_TRACE("[IAUPDATE] IAUpdateClient::broughtToForeground() end");
       
   259 }
       
   260 
       
   261 // -----------------------------------------------------------------------------
       
   262 // IAUpdateClient::requestCompleted
       
   263 // 
       
   264 // -----------------------------------------------------------------------------
       
   265 //
       
   266 void IAUpdateClient::requestCompleted(const QVariant& value)
       
   267 {
       
   268     IAUPDATE_TRACE("[IAUPDATE] IAUpdateClient::requestCompleted() begin");
       
   269     RequestType requestType = mCurrentRequestType;
       
   270     IAUPDATE_TRACE_1("[IAUPDATE] IAUpdateClient::requestCompleted()request type: %d", requestType );
       
   271     QList<QVariant> resultlist = value.toList();
       
   272     int errorCode = resultlist.at(0).toInt();
       
   273     CIAUpdateResult* updateResult(NULL);
       
   274             
       
   275     if ( requestType == ShowUpdates )
       
   276     {
       
   277         // Update result object is required.
       
   278         // Notice that the ownership is transferred later.
       
   279         // So, this function does not need to delete updateResult object.
       
   280         TRAPD( trapError, updateResult = CIAUpdateResult::NewL() );
       
   281         if ( updateResult )
       
   282         {
       
   283             updateResult->SetSuccessCount(resultlist.at(1).toInt());
       
   284             updateResult->SetFailCount(resultlist.at(2).toInt());
       
   285             updateResult->SetCancelCount(resultlist.at(3).toInt());
       
   286         }
       
   287         else
       
   288         {
       
   289             // Something went wrong when creating update result object.
       
   290             // Update the error code accordingly.
       
   291             errorCode = trapError;
       
   292         }
       
   293             // Let's assume that connection is not needed anymore
       
   294         if (mServiceRequest)
       
   295         {
       
   296             delete mServiceRequest;
       
   297             mServiceRequest= NULL;
       
   298         }
       
   299     }
       
   300         
       
   301     // Inform that no operation is going on anymore.
       
   302     // This is required for busy check.
       
   303     mCurrentRequestType = NoOperation;
       
   304         
       
   305     // Use the request type of the ongoing operation to check what callback
       
   306     // function to call.
       
   307     int countOfUpdates = 0;
       
   308     bool updateNow = false;
       
   309     switch (requestType)
       
   310     {
       
   311         case CheckUpdates:
       
   312         countOfUpdates = resultlist.at(1).toInt();    
       
   313         if (countOfUpdates == 0)
       
   314         {
       
   315             // Let's assume that connection is not needed anymore
       
   316             delete mServiceRequest;  
       
   317             mServiceRequest= NULL;
       
   318         }
       
   319         IAUPDATE_TRACE_1("[IAUPDATE] IAUpdateClient::requestCompleted() count of updates: %d", countOfUpdates );
       
   320         mObserver.CheckUpdatesComplete(errorCode, countOfUpdates);
       
   321         break;
       
   322 
       
   323         case ShowUpdates:
       
   324         // Notice that ownership of result object is transferred here.
       
   325         IAUPDATE_TRACE_3("[IAUPDATE] IAUpdateClient::requestCompleted() success count: %d failed count: %d cancelled count: %d", updateResult->SuccessCount(), updateResult->FailCount(), updateResult->CancelCount() );
       
   326         mObserver.UpdateComplete(errorCode, updateResult);
       
   327         break;
       
   328                 
       
   329         case UpdateQuery:
       
   330         updateNow = resultlist.at(1).toBool();      
       
   331         if ( !updateNow )
       
   332         {
       
   333             // Let's assume that connection is not needed anymore
       
   334             delete mServiceRequest;
       
   335             mServiceRequest= NULL;
       
   336         }    
       
   337         IAUPDATE_TRACE_1("[IAUPDATE] IAUpdateClient::requestCompleted() update now: %d", updateNow );
       
   338         mObserver.UpdateQueryComplete(errorCode, updateNow);
       
   339         break;
       
   340              
       
   341         default:
       
   342         // Should not ever come here.
       
   343         break;
       
   344     }
       
   345             
       
   346     // Do not anything else than return after callback function is called because 
       
   347     // this instance can be deleted by a client in a callback function
       
   348     // 
       
   349     IAUPDATE_TRACE("[IAUPDATE] IAUpdateClient::requestCompleted() end");
       
   350 }
       
   351 
       
   352 // -----------------------------------------------------------------------------
       
   353 // IAUpdateClient::requestError
       
   354 // 
       
   355 // -----------------------------------------------------------------------------
       
   356 //
       
   357 void IAUpdateClient::requestError(int /*err*/)
       
   358 {
       
   359     IAUPDATE_TRACE("[IAUPDATE] IAUpdateClient::requestError() begin");
       
   360     if ( mServiceRequest)
       
   361         {
       
   362             delete mServiceRequest;
       
   363             mServiceRequest= NULL;
       
   364         }
       
   365     RequestType requestType = mCurrentRequestType;
       
   366     mCurrentRequestType = NoOperation;
       
   367     // because this method is called also when iaupdate is closed normally, error code is not passed to a client   
       
   368     CIAUpdateResult* updateResult(NULL);
       
   369     switch (requestType)
       
   370     {
       
   371         case CheckUpdates:
       
   372         mObserver.CheckUpdatesComplete(0, 0);
       
   373         break;
       
   374 
       
   375         case ShowUpdates:
       
   376         // Notice that ownership of result object is transferred here.
       
   377         TRAP_IGNORE( updateResult = CIAUpdateResult::NewL() );    
       
   378         mObserver.UpdateComplete(0, updateResult);
       
   379         break;
   119                     
   380                     
   120             TPckg<TInt> wgId( iOwnWgId );
   381         case UpdateQuery:
   121             delete iData;
   382         mObserver.UpdateQueryComplete(0, false);
   122             iData = NULL;
   383         break;
   123             TRAP_IGNORE( iData = wgId.AllocL() );
   384                  
   124                     
   385         default:
   125             TIpcArgs args;
   386         // Should not ever come here.
   126             args.Set( 0, iData );
   387         break;
   127             SendReceive( IAUpdateClientDefines::EIAUpdateServerSendWgId, args );    
   388     }
   128             } 
   389     IAUPDATE_TRACE("[IAUPDATE] IAUpdateClient::requestError() end");
   129         }
   390 }     
   130     IAUPDATE_TRACE_1("[IAUPDATE] RIAUpdateClient::ConnectToApp() end error code: %d", error );
   391 
   131     return error; 
   392 // -----------------------------------------------------------------------------
   132     }
   393 // IAUpdateClient::qStringFromTDesC16
   133 
   394 // 
   134 
   395 // -----------------------------------------------------------------------------
   135 
   396 //
   136 
   397 QString IAUpdateClient::qStringFromTDesC16( const TDesC16& aDes16 )             
   137 // -----------------------------------------------------------------------------
   398 {
   138 // RIAUpdateClient::Close
   399     return QString::fromUtf16( aDes16.Ptr(), aDes16.Length() );
   139 // 
   400 }
   140 // -----------------------------------------------------------------------------
   401 
   141 //
   402 // -----------------------------------------------------------------------------
   142 void RIAUpdateClient::Close()
   403 // IAUpdateClient::qStringFromTDesC8
   143     {
   404 // 
   144     IAUPDATE_TRACE("[IAUPDATE] RIAUpdateClient::Close() begin");
   405 // -----------------------------------------------------------------------------
   145     // Let the parent handle closing.
   406 //
   146     RAknAppServiceBase::Close();
   407 QString IAUpdateClient::qStringFromTDesC8( const TDesC8& aDes8 )                
   147     iConnected = EFalse;
   408 {
   148     delete iData;
   409     return QString::fromUtf8( reinterpret_cast<const char*>( aDes8.Ptr() ), aDes8.Length() );
   149     iData = NULL;
   410 }
   150     IAUPDATE_TRACE("[IAUPDATE] RIAUpdateClient::Close() end");
   411 
   151     }
   412 // -----------------------------------------------------------------------------
   152 
   413 // IAUpdateClient::qStringFromTDesC
   153 // -----------------------------------------------------------------------------
   414 // 
   154 // RIAUpdateClient::CheckUpdates
   415 // -----------------------------------------------------------------------------
   155 // 
   416 //
   156 // -----------------------------------------------------------------------------
   417 QString IAUpdateClient::qStringFromTDesC( const TDesC& aDes )                    
   157 //
   418 {
   158 TInt RIAUpdateClient::CheckUpdates( const CIAUpdateParameters& aUpdateParameters,
   419 #if defined(_UNICODE)
   159                                     TInt& aAvailableUpdates, 
   420     return qStringFromTDesC16( aDes );
   160                                     TRequestStatus& aStatus )
   421 #else
   161     {
   422     return qStringFromTDesC8( aDes );
   162     IAUPDATE_TRACE("[IAUPDATE] RIAUpdateClient::CheckUpdates()");
   423 #endif
   163     // Inform the caller about the success of the request initializing.
   424 }
   164     return SendCheckUpdatesRequest( IAUpdateClientDefines::EIAUpdateServerCheckUpdates,
   425 
   165                                     aUpdateParameters,
   426 
   166                                     aAvailableUpdates,
   427 
   167                                     aStatus );
       
   168     }
       
   169 
       
   170 // -----------------------------------------------------------------------------
       
   171 // RIAUpdateClient::ShowUpdates
       
   172 // 
       
   173 // -----------------------------------------------------------------------------
       
   174 //
       
   175 TInt RIAUpdateClient::ShowUpdates( const CIAUpdateParameters& aUpdateParameters,
       
   176                                    TInt& aNumberOfSuccessfullUpdates,
       
   177                                    TInt& aNumberOfFailedUpdates,
       
   178                                    TInt& aNumberOfCancelledUpdates,
       
   179                                    TRequestStatus& aStatus )
       
   180     {
       
   181     IAUPDATE_TRACE("[IAUPDATE] RIAUpdateClient::ShowUpdates()");
       
   182     // Inform the caller about the success of the request initializing.
       
   183     return SendUpdateRequest( IAUpdateClientDefines::EIAUpdateServerShowUpdates,
       
   184                               aUpdateParameters,
       
   185                               aNumberOfSuccessfullUpdates,
       
   186                               aNumberOfFailedUpdates,
       
   187                               aNumberOfCancelledUpdates,
       
   188                               aStatus );
       
   189     }
       
   190     
       
   191 
       
   192 // -----------------------------------------------------------------------------
       
   193 // RIAUpdateClient::UpdateQuery
       
   194 // 
       
   195 // -----------------------------------------------------------------------------
       
   196 //    
       
   197 TInt RIAUpdateClient::UpdateQuery( TBool& aUpdateNow, TRequestStatus& aStatus )
       
   198     {
       
   199     IAUPDATE_TRACE("[IAUPDATE] RIAUpdateClient::UpdateQuery() begin");
       
   200 	TPckg<TBool> updateNow( aUpdateNow );
       
   201 	iPtr1.Set( updateNow );
       
   202 	            
       
   203     TIpcArgs args;
       
   204     args.Set( 1, &iPtr1 );
       
   205     
       
   206 	SendReceive( IAUpdateClientDefines::EIAUpdateServerShowUpdateQuery, 
       
   207 	             args, 
       
   208 	             aStatus );    
       
   209 	IAUPDATE_TRACE("[IAUPDATE] RIAUpdateClient::UpdateQuery() begin");    
       
   210     return KErrNone;
       
   211     }
       
   212 
       
   213 // -----------------------------------------------------------------------------
       
   214 // RIAUpdateClient::BroughtToForeground
       
   215 // 
       
   216 // -----------------------------------------------------------------------------
       
   217 //
       
   218 void RIAUpdateClient::BroughtToForeground()
       
   219     {
       
   220     IAUPDATE_TRACE("[IAUPDATE] RIAUpdateClient::BroughtToForeground() begin");
       
   221     if ( iConnected )
       
   222         {
       
   223     	SendReceive( IAUpdateClientDefines::EIAUpdateServerToForeground );
       
   224         }
       
   225     IAUPDATE_TRACE("[IAUPDATE] RIAUpdateClient::BroughtToForeground() end");
       
   226     }
       
   227 
       
   228 
       
   229 // -----------------------------------------------------------------------------
       
   230 // RIAUpdateClient::CancelAsyncRequest
       
   231 // 
       
   232 // -----------------------------------------------------------------------------
       
   233 //
       
   234 void RIAUpdateClient::CancelAsyncRequest()
       
   235     {
       
   236     IAUPDATE_TRACE("[IAUPDATE] RIAUpdateClient::CancelAsyncRequest() begin");
       
   237     // We suppose that an active object will
       
   238     // wait for the cancellation to complete.
       
   239     // So, let the server know that operation is cancelled.
       
   240     // The server should set the correct status and complete
       
   241     // the request of the active object. So, the cancellation 
       
   242     // can proceed to the end.
       
   243     if ( iConnected )
       
   244         {
       
   245     	SendReceive( IAUpdateClientDefines::EIAUpdateServerCancel ); 
       
   246         }
       
   247     IAUPDATE_TRACE("[IAUPDATE] RIAUpdateClient::CancelAsyncRequest() end");
       
   248     }
       
   249 
       
   250 
       
   251 
       
   252 // -----------------------------------------------------------------------------
       
   253 // RIAUpdateClient::ServiceUid()
       
   254 // 
       
   255 // -----------------------------------------------------------------------------
       
   256 //
       
   257 TUid RIAUpdateClient::ServiceUid() const
       
   258     {
       
   259     IAUPDATE_TRACE("[IAUPDATE] RIAUpdateClient::ServiceUid()");
       
   260     return IAUpdateClientDefines::KIAUpdateServiceUid;    
       
   261     }
       
   262 
       
   263 // -----------------------------------------------------------------------------
       
   264 // RIAUpdateClient::SendCheckUpdatesRequest
       
   265 // 
       
   266 // -----------------------------------------------------------------------------
       
   267 //
       
   268 TInt RIAUpdateClient::SendCheckUpdatesRequest( TInt aUpdateFunction,
       
   269                                                const CIAUpdateParameters& aUpdateParameters,
       
   270                                                TInt& aCount,
       
   271                                                TRequestStatus& aStatus )
       
   272     {
       
   273     IAUPDATE_TRACE("[IAUPDATE] RIAUpdateClient::SendCheckUpdatesRequest() begin");
       
   274     
       
   275     aStatus = KRequestPending;
       
   276 
       
   277     delete iData;
       
   278     iData = NULL;   
       
   279     
       
   280     TInt error( KErrNone );
       
   281     TRAP( error, 
       
   282           IAUpdateTools::ExternalizeParametersL( iData, 
       
   283                                                  aUpdateParameters ) );
       
   284     IAUPDATE_TRACE_1("[IAUPDATE] error code: %d", error );
       
   285 
       
   286     // Because this function does not leave,
       
   287     // use error value to check if request can be done.
       
   288     if ( error == KErrNone )
       
   289         {
       
   290         TPckg<TInt> count( aCount );
       
   291 	    iPtr1.Set( count );
       
   292 	            
       
   293         TIpcArgs args;
       
   294         args.Set( 0, iData );
       
   295         args.Set( 1, &iPtr1 );
       
   296               
       
   297         // Start the asynchronous operation in the server side.
       
   298         SendReceive( aUpdateFunction, args, aStatus );        
       
   299         }
       
   300 
       
   301     // Inform the caller about the success of the request initializing.
       
   302     IAUPDATE_TRACE("[IAUPDATE] RIAUpdateClient::SendCheckUpdatesRequest() begin");
       
   303     return error;
       
   304     }
       
   305 
       
   306 // -----------------------------------------------------------------------------
       
   307 // RIAUpdateClient::SendUpdateRequest
       
   308 // 
       
   309 // -----------------------------------------------------------------------------
       
   310 //
       
   311 TInt RIAUpdateClient::SendUpdateRequest( TInt aUpdateFunction,
       
   312                                          const CIAUpdateParameters& aUpdateParameters,
       
   313                                          TInt& aNumberOfSuccessfullUpdates,
       
   314                                          TInt& aNumberOfFailedUpdates,
       
   315                                          TInt& aNumberOfCancelledUpdates,
       
   316                                          TRequestStatus& aStatus )
       
   317     {
       
   318     IAUPDATE_TRACE("[IAUPDATE] RIAUpdateClient::SendUpdateRequest() begin");
       
   319     aStatus = KRequestPending;
       
   320 
       
   321     delete iData;
       
   322     iData = NULL;   
       
   323     
       
   324     TInt error( KErrNone );
       
   325     TRAP( error, 
       
   326           IAUpdateTools::ExternalizeParametersL( iData, 
       
   327                                                  aUpdateParameters ) );
       
   328     IAUPDATE_TRACE_1("[IAUPDATE] error code: %d", error );
       
   329     // Because this function does not leave,
       
   330     // use error value to check if request can be done.
       
   331     if ( error == KErrNone )
       
   332         {
       
   333         TPckg<TInt> successCount( aNumberOfSuccessfullUpdates );
       
   334 	    iPtr1.Set( successCount );
       
   335 
       
   336         TPckg<TInt> failCount( aNumberOfFailedUpdates );
       
   337 	    iPtr2.Set( failCount );
       
   338 
       
   339         TPckg<TInt> cancelCount( aNumberOfCancelledUpdates );
       
   340 	    iPtr3.Set( cancelCount );
       
   341 	    	            
       
   342         TIpcArgs args;
       
   343         args.Set( 0, iData );
       
   344         args.Set( 1, &iPtr1 );
       
   345         args.Set( 2, &iPtr2 );
       
   346         args.Set( 3, &iPtr3 );
       
   347       
       
   348         // Start the asynchronous operation in the server side.
       
   349         SendReceive( aUpdateFunction, args, aStatus );        
       
   350         }
       
   351 
       
   352     // Inform the caller about the success of the request initializing.
       
   353     IAUPDATE_TRACE("[IAUPDATE] RIAUpdateClient::SendUpdateRequest() end");
       
   354     return error;
       
   355     }    
       
   356 
       
   357 // -----------------------------------------------------------------------------
       
   358 // RIAUpdateClient::StartNewAppToBackgroundL
       
   359 // 
       
   360 // -----------------------------------------------------------------------------
       
   361 //
       
   362 void RIAUpdateClient::StartNewAppToBackgroundL( TUid aAppUid, TRequestStatus& aStatus )
       
   363     {
       
   364     IAUPDATE_TRACE("[IAUPDATE] RIAUpdateClient::StartNewAppToBackgroundL() begin");
       
   365     TName notUsed;
       
   366     iDifferentiator = GenerateServerDifferentiatorAndName(notUsed, aAppUid);
       
   367        
       
   368     RApaLsSession apa;
       
   369     User::LeaveIfError(apa.Connect());
       
   370     CleanupClosePushL(apa);
       
   371     
       
   372     TApaAppInfo info;
       
   373     User::LeaveIfError(apa.GetAppInfo(info, aAppUid));
       
   374     
       
   375     CApaCommandLine* cmdLine = CApaCommandLine::NewLC();
       
   376     cmdLine->SetExecutableNameL(info.iFullName);
       
   377     cmdLine->SetServerRequiredL( iDifferentiator );
       
   378     cmdLine->SetCommandL(EApaCommandBackground);
       
   379          
       
   380     TThreadId notUsedId;
       
   381     User::LeaveIfError(apa.StartApp(*cmdLine, notUsedId, &aStatus));
       
   382           
       
   383     CleanupStack::PopAndDestroy(2, &apa);   // cmdLine and apa
       
   384     IAUPDATE_TRACE("[IAUPDATE] RIAUpdateClient::StartNewAppToBackgroundL() end");
       
   385     }
       
   386 
       
   387 
       
   388 
       
   389 void RIAUpdateClient::ServerName(TName& aServerName, TUid aAppServerUid, TUint aServerDifferentiator)
       
   390     {
       
   391     _LIT(KServerNameFormat, "%08x_%08x_AppServer");
       
   392     aServerName.Format(KServerNameFormat, aServerDifferentiator, aAppServerUid);
       
   393     }
       
   394     
       
   395 TUint RIAUpdateClient::GenerateServerDifferentiatorAndName(TName& aServerName, TUid aAppServerUid)
       
   396     {
       
   397     TUint r;
       
   398     FOREVER
       
   399         {
       
   400         r = Math::Random();
       
   401         if (r==0)
       
   402             continue;
       
   403         ServerName(aServerName, aAppServerUid, r);
       
   404         TFindServer find(aServerName);
       
   405         TFullName fullName;
       
   406         if (find.Next(fullName) == KErrNone)
       
   407             continue;
       
   408         break;
       
   409         }       
       
   410     return r;
       
   411     }