mmfenh/enhancedmediaclient/Client/src/Components/ClientProgDLSource/DownloadGateway.cpp
changeset 16 43d09473c595
parent 14 80975da52420
child 22 128eb6a32b84
equal deleted inserted replaced
14:80975da52420 16:43d09473c595
     1 /*
       
     2 * Copyright (c) 2006 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 DownloadGateway class.
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include "DownloadGateway.h"
       
    20 #include <e32std.h>
       
    21 
       
    22 #define RETURN_IF_ERROR(x) if(x != KErrNone) return x
       
    23 
       
    24 using namespace multimedia;
       
    25 
       
    26 // CONSTANTS
       
    27 CDownloadGateway::CDownloadGateway()
       
    28     {
       
    29     iDMgrConnected = EFalse;
       
    30     iDownload = NULL;
       
    31     iDownloadId = 0;
       
    32     }
       
    33 
       
    34 CDownloadGateway::~CDownloadGateway()
       
    35     {
       
    36     if ( iDMgrConnected )
       
    37         {
       
    38         iDownloadMgr.Close();
       
    39         }
       
    40     }
       
    41 
       
    42 TInt CDownloadGateway::Initialize(TUid aClientUid)
       
    43     {
       
    44     TInt status(KErrNone);
       
    45     if ( !iDMgrConnected )
       
    46         {
       
    47         TRAP(status,iDownloadMgr.ConnectL( aClientUid, *this, EFalse ));
       
    48         if(!status)
       
    49             {
       
    50             iDMgrConnected = ETrue;
       
    51             }
       
    52         }
       
    53     return status;
       
    54     }
       
    55 
       
    56 TInt CDownloadGateway::MoveFile( TInt aId, const TDesC& aDestFileName )
       
    57     {
       
    58     TInt status(KErrNotFound);
       
    59     // Check here if the download is complete and the Source is in Stopped State
       
    60     if(iDownloadId == aId && iDownload)
       
    61         {
       
    62         status = iDownload->SetStringAttribute( EDlAttrDestFilename, aDestFileName );
       
    63         if(!status)
       
    64             {
       
    65             iDownload->Move();
       
    66             }
       
    67         }
       
    68     return status;
       
    69     }
       
    70 
       
    71 TInt CDownloadGateway::ConnectToDownload(TInt aId, MDownloadGatewayObserver& aObserver, TUid /*aAppUid*/)
       
    72     {
       
    73     TInt status(KErrNotFound);
       
    74     TRAP(status, iDownload = &(iDownloadMgr.AttachToDownloadL( aId )));
       
    75     if(status == KErrNone)
       
    76     	{
       
    77 	    iObserver = &aObserver;
       
    78 	    iDownloadId = aId;
       
    79     	}
       
    80 	else
       
    81 		{
       
    82 		iDownload = NULL;	
       
    83 		}    	
       
    84     return status;
       
    85     }
       
    86 
       
    87 TInt CDownloadGateway::DisconnectFromDownload(TInt aId)
       
    88     {
       
    89     TInt status(KErrNotFound);
       
    90     if(iDownloadId == aId && iDownload)
       
    91         {
       
    92         iDownload = NULL;
       
    93         iDownloadId = 0;
       
    94         status = KErrNone;
       
    95         }
       
    96     return status;
       
    97     }
       
    98 
       
    99 TInt CDownloadGateway::GetMimeType(TInt aId, TDes8& aMimeType)
       
   100     {
       
   101     TInt status(KErrNotFound);
       
   102     if(iDownloadId == aId && iDownload)
       
   103         {
       
   104         status = iDownload->GetStringAttribute( EDlAttrContentType, aMimeType  );
       
   105         }
       
   106     return status;
       
   107     }
       
   108 
       
   109 TInt CDownloadGateway::GetCurrentSize(  TInt aId, TUint& aSize )
       
   110     {
       
   111     TInt status(KErrNotFound);
       
   112     TInt32 value(0);
       
   113     if(iDownloadId == aId && iDownload)
       
   114         {
       
   115          status = iDownload->GetIntAttribute( EDlAttrDownloadedSize, value );
       
   116          if(!status)
       
   117             {
       
   118             aSize = value;
       
   119             }
       
   120         }
       
   121     return status;
       
   122     }
       
   123 
       
   124 TInt CDownloadGateway::GetDownloadSize( TInt aId, TUint& aSize )
       
   125     {
       
   126     TInt status(KErrNotFound);
       
   127     TInt32 value(0);
       
   128     if(iDownloadId == aId && iDownload)
       
   129         {
       
   130         status = iDownload->GetIntAttribute( EDlAttrLength, value );
       
   131         if(status == KErrNone)
       
   132             {
       
   133             aSize = value;
       
   134             }
       
   135         }
       
   136     return status;
       
   137     }
       
   138 
       
   139 TInt CDownloadGateway::GetDownloadStatus(TInt aId, MProgDLSource::TDownloadStatus& aStatus)
       
   140     {
       
   141     TInt status(KErrNotFound);
       
   142     if(iDownloadId == aId && iDownload)
       
   143         {
       
   144         aStatus = iDownloadStatus;
       
   145         }
       
   146     return status;
       
   147     }
       
   148 
       
   149 TInt CDownloadGateway::DeleteDownload(TInt aId)
       
   150     {
       
   151     TInt status(KErrNotFound);
       
   152     if(iDownloadId == aId && iDownload)
       
   153         {
       
   154         status = iDownload->Delete();
       
   155         }
       
   156     return status;
       
   157     }
       
   158 
       
   159 TInt CDownloadGateway::ResumeDownload(TInt aId)
       
   160     {
       
   161     TInt status(KErrNotFound);
       
   162     if(iDownloadId == aId && iDownload)
       
   163         {
       
   164         status = iDownload->Start();
       
   165         }
       
   166     return status;
       
   167     }
       
   168 
       
   169 TInt CDownloadGateway::SetProgressiveMode(TInt aId, TBool aMode)
       
   170     {
       
   171     TInt status(KErrNotFound);
       
   172     if(iDownloadId == aId && iDownloadStatus != MProgDLSource::EDeleted && iDownload)
       
   173         {
       
   174         status = iDownload->SetBoolAttribute( EDlAttrProgressive, aMode );
       
   175         }
       
   176     return status;
       
   177     }
       
   178 
       
   179 TInt CDownloadGateway::SetLocalFileName( TInt aId, TDesC& aFilName )
       
   180     {
       
   181     TInt status(KErrNotFound);
       
   182     if( iDownloadId == aId  && iDownload)
       
   183         {
       
   184         status = iDownload->SetStringAttribute( EDlAttrLocalFileName, aFilName );
       
   185         }
       
   186     return status;
       
   187     }
       
   188 
       
   189 TInt CDownloadGateway::GetLocalFileName( TInt aId, TDes& aFileName )
       
   190     {
       
   191     TInt status(KErrNotFound);
       
   192     if( iDownloadId == aId  && iDownload)
       
   193         {
       
   194         status = iDownload->GetStringAttribute( EDlAttrDestFilename, aFileName );
       
   195         }
       
   196     return status;
       
   197     }
       
   198 
       
   199 void CDownloadGateway::HandleDMgrEventL( RHttpDownload& /*aDownload*/, THttpDownloadEvent aEvent )
       
   200     {
       
   201 #ifdef _DEBUG
       
   202         RDebug::Print(_L("CDownloadGateway::HandleDMgrEventL DownloadState[%d] ProgressState[%d]"),aEvent.iDownloadState,aEvent.iProgressState);
       
   203 #endif        
       
   204         MProgDLSource::TDownloadStatus status;
       
   205 
       
   206         // When the new evnet is different in one of the states 
       
   207         // then the current event 
       
   208         if(iPrevEvent.iDownloadState != aEvent.iDownloadState)
       
   209             {
       
   210                 switch(aEvent.iDownloadState)
       
   211                 {
       
   212                     case EHttpDlCreated:
       
   213                             status = MProgDLSource::EConnecting;
       
   214                             break;
       
   215                     case EHttpDlInprogress:
       
   216                             status = MProgDLSource::EStarted;
       
   217                             break;
       
   218                     case EHttpDlPaused:
       
   219                             status = MProgDLSource::EPaused;
       
   220                             break;
       
   221                     case EHttpDlCompleted:
       
   222                             status = MProgDLSource::ECompleted;
       
   223                             break;
       
   224                     case EHttpDlFailed:
       
   225                             status = MProgDLSource::EFailed;
       
   226                             break;
       
   227                     case EHttpDlDeleting:
       
   228                             status = MProgDLSource::EDeleted;
       
   229                             break;                        
       
   230                     default:
       
   231                             status = MProgDLSource::EUnknown;
       
   232                             break;
       
   233                 };
       
   234                 
       
   235                 iDownloadStatus = status;
       
   236                 iObserver->Event(status);
       
   237             }
       
   238         
       
   239         if(iPrevEvent.iProgressState != aEvent.iProgressState)
       
   240             {
       
   241                 switch(aEvent.iProgressState)
       
   242                 {
       
   243                     
       
   244                     case EHttpProgContentFileMoved:
       
   245                             {
       
   246                             TInt32 value(0);
       
   247                             TInt err = iDownload->GetIntAttribute( EDlAttrErrorId, value );
       
   248 #ifdef _DEBUG
       
   249                             RDebug::Print(_L("Delete File Code 2[%d][%d]"),err,value);
       
   250 #endif                            
       
   251                             if(!err)
       
   252                                 {
       
   253                                 iObserver->MoveFileComplete(value);
       
   254                                 }
       
   255                             }
       
   256                             break;
       
   257                    case EHttpProgCodLoadEnd:
       
   258                             // Case for COD download complete
       
   259                             iDownloadStatus = MProgDLSource::ECompleted;
       
   260                             iObserver->Event(MProgDLSource::ECompleted);
       
   261                             break;
       
   262                     case EHttpProgContentFileMovedAndDestFNChanged:
       
   263                             {
       
   264                             iObserver->MoveFileComplete(KErrAlreadyExists);
       
   265                             break;
       
   266                             }
       
   267                 };
       
   268             }
       
   269         
       
   270         iPrevEvent = aEvent;
       
   271     }
       
   272 
       
   273 // End of file