applicationmanagement/server/src/AMDownloader.cpp
changeset 0 3ce708148e4d
child 4 75a71fdb4c92
equal deleted inserted replaced
-1:000000000000 0:3ce708148e4d
       
     1 /*
       
     2  * Copyright (c) 2002-2004 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:  Implementation of applicationmanagement components
       
    15  *
       
    16  */
       
    17 
       
    18 
       
    19 // INCLUDE FILES
       
    20 //#include <BrowserUiInternalCRKeys.h>
       
    21 #include <DownloadMgrClient.h>
       
    22 
       
    23 #include "AMDownloader.h"
       
    24 //#include "aminstalloptions.h"
       
    25 #include "debug.h"
       
    26 #include <e32property.h>
       
    27 #include <aknnotewrappers.h> 
       
    28 #include <AknNotify.h>
       
    29 #include <AknNotifyStd.h>
       
    30 #include <AknGlobalNote.h>
       
    31 #include "ampskeys.h"
       
    32 #include "ApplicationManagementConst.h"
       
    33 #include <applicationmanagement.rsg>
       
    34 #include <StringLoader.h>
       
    35 #include <etelpckt.h>
       
    36 #include <exterror.h>
       
    37 #include <wlanerrorcodes.h>
       
    38 #include <CoreApplicationUIsSDKCRKeys.h>// for KCRUidCoreApplicationUIs
       
    39 
       
    40 using namespace NApplicationManagement;
       
    41 
       
    42 // ============================ MEMBER FUNCTIONS ===============================
       
    43 
       
    44 // -----------------------------------------------------------------------------
       
    45 // CAMDownloader::CAMDownloader
       
    46 // C++ default constructor can NOT contain any code, that
       
    47 // might leave.
       
    48 // -----------------------------------------------------------------------------
       
    49 //
       
    50 CAMDownloader::CAMDownloader(RHttpDownload& aDownload,
       
    51         MAMDownloaderObserver& aObserver) :
       
    52         iDownload(aDownload), iObserver(aObserver)
       
    53         {
       
    54         }
       
    55 
       
    56 // -----------------------------------------------------------------------------
       
    57 // CAMDownloader::ConstructL
       
    58 // Symbian 2nd phase constructor can leave.
       
    59 // -----------------------------------------------------------------------------
       
    60 //
       
    61 void CAMDownloader::ConstructL()
       
    62     {
       
    63     iProgStarted=0;
       
    64     iSetFinalValue =0;
       
    65     iProgressCancelled = EFalse;
       
    66     iCurrentDownloadProgState = EHttpProgNone;
       
    67     iProgressNote= new(ELeave)CAppMgmtProgDialog(this);
       
    68     }
       
    69 
       
    70 // -----------------------------------------------------------------------------
       
    71 // CAMDownloader::NewL
       
    72 // Two-phased constructor.
       
    73 // -----------------------------------------------------------------------------
       
    74 //
       
    75 CAMDownloader* CAMDownloader::NewL(RHttpDownload& aDownload,
       
    76         MAMDownloaderObserver& aObserver)
       
    77     {
       
    78     CAMDownloader* self = new( ELeave ) CAMDownloader( aDownload, aObserver );
       
    79 
       
    80     CleanupStack::PushL(self);
       
    81     self->ConstructL();
       
    82     CleanupStack::Pop();
       
    83 
       
    84     return self;
       
    85     }
       
    86 
       
    87 // ---------------------------------------------------------------------------
       
    88 // Destructor
       
    89 // ---------------------------------------------------------------------------
       
    90 CAMDownloader::~CAMDownloader()
       
    91     {
       
    92     RDEBUG_3( "CAMDownloader::~CAMDownloader 0x%X - 0x%X", reinterpret_cast<TUint>( this ),
       
    93             reinterpret_cast<TUint>(this)+sizeof( CAMDownloader ) );
       
    94 
       
    95     if (iProgressNote)
       
    96         delete iProgressNote;
       
    97 
       
    98     if (iFileName)
       
    99         {
       
   100         delete iFileName;
       
   101         iFileName = NULL;
       
   102         }
       
   103 
       
   104     if (iContentType)
       
   105         {
       
   106         delete iContentType;
       
   107         iContentType = NULL;
       
   108         }
       
   109 
       
   110     delete iURI;
       
   111     iURI = NULL;
       
   112     }
       
   113 
       
   114 // ---------------------------------------------------------------------------
       
   115 // StartDownload()
       
   116 // ---------------------------------------------------------------------------
       
   117 TInt CAMDownloader::StartDownload()
       
   118     {
       
   119     RDEBUG("CAMDownloader::StartDownload");
       
   120     TInt err(iDownload.SetIntAttribute(EDlAttrAction, EMove) );
       
   121     RDEBUG_2("CAMDownload::StartDownload: response to EDlAttrAction set to true: (%d)", err );
       
   122 
       
   123     err = iDownload.Start();
       
   124     if (err == KErrNone)
       
   125         {
       
   126         RDEBUG( "CAMDownload::StartDownloadL: Started download successfully!!" );
       
   127         }
       
   128     else
       
   129         {
       
   130         RDEBUG_2( "CAMDownload::StartDownloadL: ERROR Failed to start downlad '%d'", err );
       
   131         }
       
   132     return err;
       
   133     }
       
   134 
       
   135 // ---------------------------------------------------------------------------
       
   136 // StopDownload()
       
   137 // ---------------------------------------------------------------------------
       
   138 TInt CAMDownloader::StopDownload()
       
   139     {
       
   140     RDEBUG("CAMDownloader::StopDownload");
       
   141     TInt result(KErrNone);
       
   142     if (!iProgressCancelled)
       
   143         {
       
   144         TRAP_IGNORE(iProgressNote->ProgressCompletedL());
       
   145         }
       
   146     result = iDownload.Delete();
       
   147     RDEBUG_2("CAMDownloader::StopDownload: result (%d)", result);
       
   148     return result;
       
   149     }
       
   150 
       
   151 // ---------------------------------------------------------------------------
       
   152 // ContinueDownload()
       
   153 // ---------------------------------------------------------------------------
       
   154 TInt CAMDownloader::ContinueDownload()
       
   155     {
       
   156     RDEBUG("CAMDownloader::ContinueDownload");
       
   157     TInt result(KErrNone);
       
   158     result = iDownload.Start();
       
   159     RDEBUG_2("CAMDownloader::ContinueDownload: result (%d)", result);
       
   160     return result;
       
   161     }
       
   162 
       
   163 void CAMDownloader::CreateAndUpdateProgressDialogL(TInt32 aDownloadedSize,
       
   164         TInt32 aContentSize, THttpProgressState aState)
       
   165     {
       
   166 
       
   167     if (iProgStarted && aState == EHttpProgConnectionSuspended)    
       
   168     {
       
   169      if (!iProgressCancelled)
       
   170         {
       
   171         TRAP_IGNORE(iProgressNote->ProgressCompletedL());
       
   172         }
       
   173      iProgressCancelled = ETrue;
       
   174     
       
   175     TInt UIdisabled=0;
       
   176     RProperty::Set(KUidPSApplicationManagementKeys, KAMServerUIEnabled,
       
   177                 UIdisabled);
       
   178 
       
   179      TApaTaskList taskList(CEikonEnv::Static()->WsSession() );
       
   180             TApaTask task = taskList.FindApp(TUid::Uid(KAppMgmtServerUid));
       
   181 
       
   182             task.SetWgId(CEikonEnv::Static()->RootWin().Identifier());
       
   183 
       
   184             if (task.Exists())
       
   185                 task.SendToBackground();
       
   186 
       
   187 
       
   188 
       
   189     iCurrentDownloadProgState = EHttpProgConnectionSuspended;          
       
   190 
       
   191      RDEBUG( "CreateAndUpdateProgressDialogL : CAMDownloader Connection Failed" );  
       
   192         HBufC *netlossnote = StringLoader::LoadLC(R_DOWNLOADFAILED_CONNECTION);
       
   193 
       
   194         CAknGlobalNote* globalNote = CAknGlobalNote::NewL();
       
   195         CleanupStack::PushL(globalNote);
       
   196         
       
   197         globalNote->ShowNoteL(EAknGlobalErrorNote, *netlossnote);
       
   198 
       
   199         CleanupStack::PopAndDestroy( 2);// globalNote, dialognote
       
   200 
       
   201     
       
   202     }
       
   203 
       
   204     if (!iProgStarted)
       
   205         {
       
   206 
       
   207         RDEBUG( "CAMDownloadManager::HandleAlreadyRunningL - StartProgressNoteL" );
       
   208         iProgressNote->StartProgressNoteL();
       
   209 
       
   210         // UI in progress whenever switch btween application occur AM server will be moved 
       
   211         // to foreground
       
   212 
       
   213         //Define P&Skey
       
   214 
       
   215 
       
   216         TInt UIenabled = 1;
       
   217         TInt err = RProperty::Set(KUidPSApplicationManagementKeys,
       
   218                 KAMServerUIEnabled, UIenabled);
       
   219         User::LeaveIfError(err); // invalid
       
   220 
       
   221 
       
   222         //Check if operation is silent or Non Silent
       
   223 
       
   224         TInt operNonSilent = -1;
       
   225 
       
   226         RProperty::Get(KUidPSApplicationManagementKeys,
       
   227                 KAMOperationNonSilent, operNonSilent);
       
   228 
       
   229         if (operNonSilent==1 || operNonSilent==KErrNotFound)
       
   230             {
       
   231             TApaTaskList taskList(CEikonEnv::Static()->WsSession() );
       
   232             TApaTask task = taskList.FindApp(TUid::Uid(KAppMgmtServerUid));
       
   233 
       
   234             task.SetWgId(CEikonEnv::Static()->RootWin().Identifier());
       
   235 
       
   236             if (task.Exists())
       
   237                 task.BringToForeground();
       
   238 
       
   239             }
       
   240 
       
   241         iProgStarted = 1;
       
   242         }
       
   243 
       
   244     if (!iSetFinalValue && aContentSize>=0)
       
   245         {
       
   246         RDEBUG( "CAMDownloadManager::HandleAlreadyRunningL - SetFinalValue" );
       
   247         if (!iProgressCancelled)
       
   248         {
       
   249         iProgressNote->SetFinalValueL(aContentSize);
       
   250         iSetFinalValue = 1;
       
   251         }
       
   252         }
       
   253     else
       
   254         if (aContentSize>=0)
       
   255          {
       
   256             RDEBUG( "CAMDownloadManager::HandleAlreadyRunningL - UpdateProcessL" );
       
   257             if (!iProgressCancelled)
       
   258             {
       
   259              iProgressNote->UpdateProcessL(aDownloadedSize);
       
   260              }
       
   261           }
       
   262 
       
   263     RDEBUG( "CAMDownloadManager::HandleAlreadyRunningL - END" );
       
   264 
       
   265     }
       
   266 
       
   267 // ---------------------------------------------------------------------------
       
   268 // CAMDownloader::FileNameFromDownloadL
       
   269 // ---------------------------------------------------------------------------
       
   270 void CAMDownloader::FileNameFromDownloadL()
       
   271     {
       
   272     RFile f;
       
   273     TInt err(iDownload.GetFileHandleAttribute(f));
       
   274     if (err == KErrNone)
       
   275         {
       
   276         TFileName fn;
       
   277         err = f.Name(fn);
       
   278         if (err == KErrNone)
       
   279             {
       
   280             if (iFileName)
       
   281                 {
       
   282                 delete iFileName;
       
   283                 iFileName = NULL;
       
   284                 }
       
   285             iFileName = fn.AllocL();
       
   286 
       
   287             }
       
   288         else
       
   289             {
       
   290 
       
   291             }
       
   292         }
       
   293     else
       
   294         {
       
   295         RDEBUG_2( "CAMDownloader::GetFileNameL: WARNING Failed to get download file handle '%d'", err );
       
   296         RDEBUG("CAMDownloader::GetFileNameL: Using filename instead");
       
   297         TBuf<KMaxPath> file;
       
   298         err = iDownload.GetStringAttribute(EDlAttrDestFilename, file);
       
   299         if (err == KErrNone)
       
   300             {
       
   301             if (iFileName)
       
   302                 {
       
   303                 delete iFileName;
       
   304                 iFileName = NULL;
       
   305                 }
       
   306             iFileName = file.AllocL();
       
   307 
       
   308             }
       
   309         else
       
   310             {
       
   311 
       
   312             }
       
   313         }
       
   314     }
       
   315 
       
   316 // ---------------------------------------------------------------------------
       
   317 // CAMDownloader::ContentTypeFromDownloadL
       
   318 // ---------------------------------------------------------------------------
       
   319 void CAMDownloader::ContentTypeFromDownloadL()
       
   320     {
       
   321     TBuf8<KMaxContentTypeLength> contype;
       
   322     TInt err(iDownload.GetStringAttribute(EDlAttrContentType, contype) );
       
   323     if (err != KErrNone)
       
   324         {
       
   325         RDEBUG_2( "CAMDownloader::ContentTypeFromDownloadL: Failed to get content type: %d", err );
       
   326         }
       
   327     else
       
   328         {
       
   329         if (iContentType)
       
   330             {
       
   331             delete iContentType;
       
   332             iContentType = NULL;
       
   333             }
       
   334         iContentType = contype.AllocL();
       
   335         RDEBUG( "CAMDownloader::ContentTypeFromDownloadL: ");RDEBUG_HEX8( iContentType->Ptr(), iContentType->Size() );
       
   336         }
       
   337     }
       
   338 
       
   339 // ---------------------------------------------------------------------------
       
   340 // CAMDownloader::DownloadURIFromDownloadL
       
   341 // ---------------------------------------------------------------------------
       
   342 void CAMDownloader::DownloadURIFromDownloadL()
       
   343     {
       
   344     TBuf8<KMaxUrlLength> uri;
       
   345     TInt err(iDownload.GetStringAttribute(EDlAttrReqUrl, uri) );
       
   346     if (err != KErrNone)
       
   347         {
       
   348         RDEBUG_2( "CAMDownloader::DownloadURIFromDownloadL: Failed to get uri: %d", err );
       
   349         }
       
   350     else
       
   351         {
       
   352         if (iURI)
       
   353             {
       
   354             delete iURI;
       
   355             iURI = NULL;
       
   356             }
       
   357         iURI = uri.AllocL();
       
   358         RDEBUG( "CAMDownloader::DownloadURIFromDownloadL: ");RDEBUG_HEX8( iURI->Ptr(), iURI->Size() );
       
   359         }
       
   360     }
       
   361 
       
   362 // ---------------------------------------------------------------------------
       
   363 // CAMDownloader::FileName
       
   364 // ---------------------------------------------------------------------------
       
   365 const TDesC& CAMDownloader::FileName()
       
   366     {
       
   367     if (iFileName)
       
   368         {
       
   369         return *iFileName;
       
   370         }
       
   371     else
       
   372         {
       
   373         TRAPD( err, FileNameFromDownloadL() )
       
   374         ;
       
   375         if (err == KErrNone)
       
   376             {
       
   377             return *iFileName;
       
   378             }
       
   379         }
       
   380 
       
   381     return KNullDesC();
       
   382     }
       
   383 
       
   384 // ---------------------------------------------------------------------------
       
   385 // CAMDownloader::MimeType
       
   386 // ---------------------------------------------------------------------------
       
   387 const TDesC8& CAMDownloader::MimeType()
       
   388     {
       
   389     if (iContentType)
       
   390         {
       
   391         return *iContentType;
       
   392         }
       
   393     else
       
   394         {
       
   395         TRAPD( err, ContentTypeFromDownloadL() )
       
   396         ;
       
   397         if (err == KErrNone)
       
   398             {
       
   399             return *iContentType;
       
   400             }
       
   401         }
       
   402     return KNullDesC8();
       
   403     }
       
   404 
       
   405 // ---------------------------------------------------------------------------
       
   406 // CAMDownloader::DownloadURI
       
   407 // ---------------------------------------------------------------------------
       
   408 const TDesC8& CAMDownloader::DownloadURI()
       
   409     {
       
   410     if (iURI)
       
   411         {
       
   412         return *iURI;
       
   413         }
       
   414     else
       
   415         {
       
   416         TRAPD( err, DownloadURIFromDownloadL() )
       
   417         ;
       
   418         if (err == KErrNone)
       
   419             {
       
   420             return *iURI;
       
   421             }
       
   422         }
       
   423     return KNullDesC8();
       
   424     }
       
   425 
       
   426 // ---------------------------------------------------------------------------
       
   427 // CompareDownloadURI
       
   428 // ---------------------------------------------------------------------------
       
   429 TBool CAMDownloader::CompareDownloadURI(const TDesC8& aURI)
       
   430     {
       
   431     TBool match(EFalse);
       
   432     if (DownloadURI() == aURI)
       
   433         {
       
   434         match = ETrue;
       
   435         }
       
   436     return match;
       
   437     }
       
   438 
       
   439 // ---------------------------------------------------------------------------
       
   440 // DownloadStatusCode
       
   441 // ---------------------------------------------------------------------------
       
   442 TInt32 CAMDownloader::DownloadStatusCodeL(THttpDownloadState aDownloadState)
       
   443     {
       
   444     RDEBUG_2( "CAMDownloader::DownloadStatusCodeL: State '%d'", aDownloadState );	
       
   445     
       
   446     TInt32 code;
       
   447     User::LeaveIfError(iDownload.GetIntAttribute(EDlAttrStatusCode, code) );
       
   448     RDEBUG_2( "CAMDownloader::DownloadStatusCodeL: ERROR download status code '%d'", code );
       
   449 
       
   450     THttpDownloadMgrError errx;
       
   451     User::LeaveIfError(iDownload.GetIntAttribute(EDlAttrErrorId,
       
   452             (TInt32&)errx ) );
       
   453     RDEBUG_2( "CAMDownloader::DownloadStatusCodeL: ERROR download error id '%d'", errx );
       
   454 
       
   455     TInt32 gerr;
       
   456     User::LeaveIfError(iDownload.GetIntAttribute(EDlAttrGlobalErrorId, gerr) );
       
   457     RDEBUG_2( "CAMDownloader::DownloadStatusCodeL: ERROR global error id '%d'", gerr );
       
   458 
       
   459     TInt32 res(KErrNone);
       
   460     if (code == 0 || code == 200)
       
   461         {
       
   462         res = errx;
       
   463         
       
   464         }
       
   465     else
       
   466         {
       
   467         res = code;
       
   468         }
       
   469 
       
   470     if (errx == EHttpUnhandled || errx == EGeneral || errx
       
   471             == EConnectionFailed || errx == ETransactionFailed)
       
   472         {
       
   473         res = gerr;
       
   474         }
       
   475     if (errx == EDiskFull)
       
   476         res = KStatusDowloadFailedOOM;
       
   477 
       
   478     if(errx == ETransactionFailed && gerr == -7376)
       
   479         res = KStatusAltDowldUnavailable;
       
   480 
       
   481     if (errx == EHttpAuthenticationFailed)
       
   482         res = KStatusAlternateDownldAuthFail;
       
   483 
       
   484     RDEBUG( "Result code visited" ); 
       
   485 
       
   486     if(iCurrentDownloadProgState == EHttpProgConnectionSuspended && aDownloadState == EHttpDlPaused)
       
   487     {
       
   488       RDEBUG( "Connection lost and  suspended" ); 
       
   489       res = KStatusDownloadFailed;
       
   490       
       
   491     }
       
   492 
       
   493     // Display note in case of download failure
       
   494     // if download fails because of network loss then display note specific to Networkloss
       
   495     // else display note for general
       
   496     if(errx != ENoError )
       
   497     TRAP_IGNORE(DisplayDownloadFailedNoteL(errx,gerr));
       
   498     
       
   499     if (!iProgressCancelled)
       
   500             {
       
   501             RDEBUG( "iProgressNote->ProgressCompletedL() Start" ); 
       
   502             TRAP_IGNORE(iProgressNote->ProgressCompletedL());
       
   503             RDEBUG( "iProgressNote->ProgressCompletedL() End" ); 
       
   504             }
       
   505     
       
   506     iProgressCancelled = ETrue;
       
   507     
       
   508     RDEBUG_2( "CAMDownloader::DownloadStatusCodeL: Status code (%d)", res );
       
   509     return res;
       
   510     }
       
   511 
       
   512 // ---------------------------------------------------------------------------
       
   513 // DisplayDownloadFailedNoteL
       
   514 // ---------------------------------------------------------------------------
       
   515 
       
   516 void CAMDownloader::DisplayDownloadFailedNoteL(THttpDownloadMgrError aError, TInt32 aGlobalErr)
       
   517     {
       
   518 
       
   519     TBool iOffline = EFalse;
       
   520     TInt allowed(0);
       
   521     CRepository* coreAppUisCr = CRepository::NewLC( KCRUidCoreApplicationUIs );
       
   522     coreAppUisCr->Get( KCoreAppUIsNetworkConnectionAllowed, allowed ) ;
       
   523     CleanupStack::PopAndDestroy( coreAppUisCr );
       
   524     if( allowed != ECoreAppUIsNetworkConnectionAllowed )
       
   525         {
       
   526         iOffline = ETrue;
       
   527         }
       
   528 
       
   529     // current phone in offline mode or connection failure display note to user that connection is lost
       
   530 
       
   531     if(aError == EConnectionFailed || iOffline)
       
   532         {
       
   533          RDEBUG( "DisplayDownloadFailedNoteL : CAMDownloader Connection Failed" );  
       
   534         HBufC *netlossnote = StringLoader::LoadLC(R_DOWNLOADFAILED_CONNECTION);
       
   535 
       
   536         CAknGlobalNote* globalNote = CAknGlobalNote::NewL();
       
   537         CleanupStack::PushL(globalNote);
       
   538         
       
   539         globalNote->ShowNoteL(EAknGlobalErrorNote, *netlossnote);
       
   540 
       
   541         CleanupStack::PopAndDestroy( 2);// globalNote, dialognote
       
   542         }
       
   543     else
       
   544         {
       
   545         RDEBUG( "DisplayDownloadFailedNoteL : CAMDownloader" );       
       
   546 
       
   547         HBufC *generrornote = StringLoader::LoadLC(R_DOWNLOADFAILED_GENERAL);
       
   548         TRequestStatus status;              
       
   549 
       
   550         CAknGlobalNote* dialog = CAknGlobalNote::NewLC();
       
   551         dialog->SetSoftkeys(R_AVKON_SOFTKEYS_OK_EMPTY);
       
   552         
       
   553         dialog->ShowNoteL(status, EAknGlobalErrorNote, *generrornote);
       
   554         User::WaitForRequest(status);
       
   555 
       
   556         // ignore the softkey status..
       
   557 
       
   558         CleanupStack::PopAndDestroy(2);         // pop dialog, generrornote
       
   559         }    
       
   560 
       
   561     }
       
   562 
       
   563 // ---------------------------------------------------------------------------
       
   564 // HandleDLProgressDialogExitL
       
   565 // ---------------------------------------------------------------------------
       
   566 
       
   567 void CAMDownloader::HandleDLProgressDialogExitL(TInt aButtonId)
       
   568     {
       
   569 
       
   570     if (aButtonId == EAknSoftkeyCancel)
       
   571         {
       
   572         iProgressCancelled = ETrue;
       
   573 
       
   574         HBufC* note = StringLoader::LoadLC( R_DOWNLOAD_CANCELLED);
       
   575 
       
   576         CAknGlobalNote* globalNote = CAknGlobalNote::NewL();
       
   577         CleanupStack::PushL(globalNote);
       
   578         globalNote->ShowNoteL(EAknGlobalInformationNote, *note);
       
   579 
       
   580         CleanupStack::PopAndDestroy( 2);// globalNote, note
       
   581 
       
   582 
       
   583         TInt UIdisabled=0;
       
   584         RProperty::Set(KUidPSApplicationManagementKeys, KAMServerUIEnabled,
       
   585                 UIdisabled);
       
   586 
       
   587         iObserver.SetStatus(KStatusUserCancelled);
       
   588         }
       
   589 
       
   590     }
       
   591 //  End of File