utilities/downloadmanager/src/wmdrmdownloadbackend.cpp
changeset 16 3c88a81ff781
equal deleted inserted replaced
14:6aeb7a756187 16:3c88a81ff781
       
     1 /**
       
     2    This file is part of CWRT package **
       
     3 
       
     4    Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). **
       
     5 
       
     6    This program is free software: you can redistribute it and/or modify
       
     7    it under the terms of the GNU (Lesser) General Public License as 
       
     8    published by the Free Software Foundation, version 2.1 of the License. 
       
     9    This program is distributed in the hope that it will be useful, but
       
    10    WITHOUT ANY WARRANTY; without even the implied warranty of 
       
    11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 
       
    12    (Lesser) General Public License for more details. You should have 
       
    13    received a copy of the GNU (Lesser) General Public License along 
       
    14    with this program. If not, see <http://www.gnu.org/licenses/>.
       
    15 */
       
    16 
       
    17 #include "downloadmanager.h"
       
    18 #include "clientdownload.h"
       
    19 #include "downloadcore.h"
       
    20 #include "downloadevent.h"
       
    21 #include "wmdrmdownloadbackend.h"
       
    22 #include <cameseutility.h>
       
    23 #include <utf.h>
       
    24 #include <QFileInfo>
       
    25 #include <QUrl>
       
    26 
       
    27 WMDRMDownloadBackend::WMDRMDownloadBackend(DownloadCore *dlCore, ClientDownload *dl)
       
    28     :DownloadBackend(dlCore, dl), CActive( EPriorityStandard )
       
    29 {    
       
    30     m_downloadCore = dlCore;
       
    31     m_download = dl;
       
    32     m_contentDownload = NULL;
       
    33     m_utility = NULL;
       
    34     m_contentUrl = NULL;
       
    35      
       
    36     CActiveScheduler::Add( this );
       
    37 } 
       
    38 
       
    39 WMDRMDownloadBackend::~WMDRMDownloadBackend()
       
    40 {
       
    41     if (m_utility) {
       
    42         delete m_utility;
       
    43         m_utility = NULL;
       
    44     }
       
    45     if (m_contentUrl) {
       
    46         delete m_contentUrl;
       
    47         m_contentUrl = NULL;
       
    48     }
       
    49     if (m_contentDownload) {
       
    50         m_contentDownload->unregisterEventReceiver(this);
       
    51         delete m_contentDownload;
       
    52         m_contentDownload = NULL;
       
    53     }  
       
    54 }
       
    55 
       
    56 int WMDRMDownloadBackend::pause()
       
    57 {
       
    58     if (m_contentDownload) {
       
    59         m_contentDownload->pause();
       
    60         setDownloadState(DlPaused);
       
    61     }
       
    62     return 0;
       
    63 }
       
    64 
       
    65 int WMDRMDownloadBackend::resume()
       
    66 {
       
    67     if (m_contentDownload)
       
    68         return (m_contentDownload->resume());
       
    69     return 0;
       
    70 }
       
    71 
       
    72 int WMDRMDownloadBackend::cancel()
       
    73 {
       
    74     if (m_contentDownload) 
       
    75         return (m_contentDownload->cancel());
       
    76     
       
    77     return 0;
       
    78 }
       
    79 
       
    80 QVariant WMDRMDownloadBackend::getAttribute(DownloadAttribute attr)
       
    81 {
       
    82     switch(attr) {
       
    83     case DlFileName:
       
    84     {
       
    85         if(m_contentDownload)
       
    86             return m_contentDownload->getAttribute(DlFileName);
       
    87     }
       
    88     case DlContentType:
       
    89     {
       
    90         if(m_contentDownload) {
       
    91             return m_contentDownload->getAttribute(DlContentType);
       
    92         }
       
    93     }
       
    94     default:
       
    95         return DownloadBackend::getAttribute(attr);
       
    96     }    
       
    97     return QVariant();
       
    98 }
       
    99 
       
   100 int WMDRMDownloadBackend::setAttribute(DownloadAttribute attr, const QVariant& value)
       
   101 {
       
   102     return DownloadBackend::setAttribute(attr, value);
       
   103 }
       
   104 
       
   105 // stores the data in storage
       
   106 void WMDRMDownloadBackend::store(QByteArray /*data*/, bool /*lastChunk=false*/)
       
   107 {
       
   108     return;
       
   109 }
       
   110 
       
   111 // deletes the storage
       
   112 void WMDRMDownloadBackend::deleteStore()
       
   113 {
       
   114     return;
       
   115 }
       
   116 
       
   117 // returns the size of stored data
       
   118 qint64 WMDRMDownloadBackend::storedDataSize()
       
   119 {
       
   120     return 0;
       
   121 }
       
   122 
       
   123 void WMDRMDownloadBackend::bytesRecieved(qint64 /*bytesRecieved*/, qint64 /*bytesTotal*/)
       
   124 {
       
   125      //Do nothing. This is here to avoid this signal to reach to base class' slot.
       
   126      return;
       
   127 }
       
   128 
       
   129 void WMDRMDownloadBackend::handleFinished()
       
   130 {
       
   131     QByteArray data = m_downloadCore->reply()->readAll();
       
   132     TPtrC8 drmHeader((TUint8 *)(data.constData()));
       
   133     // delete the persistant information if saved any
       
   134     deleteInfo(); 
       
   135     
       
   136     TRAPD(err, m_utility = CCameseUtility::NewL());
       
   137     if (err != KErrNone) {
       
   138         markDownloadFailed(tr("General Error"));
       
   139         return;
       
   140     }
       
   141     // Use the Camese Interface, passing in
       
   142     // the DRM header url. Camese uses it for
       
   143     // error reporting.
       
   144     ASSERT( !m_contentUrl );
       
   145     QByteArray url = m_downloadCore->reply()->url().toEncoded();
       
   146     TPtrC8 s60Url((TUint8 *)(url.constData()));
       
   147 
       
   148     m_contentUrl = s60Url.Alloc();
       
   149     if (!m_contentUrl) {
       
   150         markDownloadFailed(tr("General Error"));
       
   151         return;
       
   152     }
       
   153     // post the event
       
   154     postEvent(WMDRMLicenseAcquiring, NULL);
       
   155     m_requestResult = KErrNone;
       
   156     m_utility->AcquireLicense( drmHeader, m_contentUrl, iStatus );
       
   157     SetActive();
       
   158     // Wait for completion of the license aquisition    
       
   159     m_wait.Start();     
       
   160     
       
   161     // Check request results
       
   162     if ( !m_requestResult ) {
       
   163         if (!m_contentUrl || (m_contentUrl->Length() == 0)) {
       
   164             markDownloadFailed(tr("File is corrupt"));
       
   165             return;
       
   166         } 
       
   167     }
       
   168     else if (m_requestResult == KErrCancel) {
       
   169         // Check if the content URL was correctly set.                
       
   170         markDownloadFailed(tr("Cancelled"));
       
   171         return;
       
   172     }
       
   173     else {
       
   174         // Check if the content URL was correctly set.                
       
   175         markDownloadFailed(tr("General Error"));
       
   176         return;
       
   177     }
       
   178     
       
   179     if ( m_contentUrl && m_contentUrl->CompareF( s60Url ) ) {   
       
   180         HBufC* s60str;  
       
   181         TRAPD(error, s60str = CnvUtfConverter::ConvertToUnicodeFromUtf8L(m_contentUrl->Des()));
       
   182         if (error != KErrNone) {
       
   183             markDownloadFailed(tr("General Error"));
       
   184             return;      
       
   185         }
       
   186         QString contentUrl = QString::fromUtf16(s60str->Ptr(),s60str->Length());
       
   187         delete s60str;
       
   188  
       
   189         QUrl qtUrl(contentUrl);
       
   190         if (qtUrl.isValid()) {
       
   191             deleteInfo(); 
       
   192             m_contentDownload = new ClientDownload(m_download->downloadManager(), contentUrl, m_download->id()); 
       
   193             if (!m_contentDownload) {                
       
   194                 markDownloadFailed(tr("General Error"));
       
   195                 return;               
       
   196             }
       
   197             // set the file name
       
   198             m_download->attributes().insert(DlFileName, m_contentDownload->getAttribute(DlFileName));
       
   199             m_contentDownload->registerEventReceiver(this);
       
   200             m_contentDownload->start();            
       
   201         } else {
       
   202             markDownloadFailed(tr("Malformed Url"));
       
   203             return; 
       
   204         }
       
   205     } else {
       
   206         markDownloadFailed(tr("No valid redirection Url"));
       
   207         return;  
       
   208     }
       
   209     
       
   210     // reset
       
   211     delete m_contentUrl;
       
   212     m_contentUrl = NULL;
       
   213     
       
   214     return;     
       
   215 }          
       
   216 
       
   217 bool WMDRMDownloadBackend::event(QEvent *event)
       
   218 {
       
   219     DEventType type = (DEventType)event->type();
       
   220     switch(type) {
       
   221     case Started:
       
   222     case HeaderReceived:
       
   223         break;
       
   224     case Progress:
       
   225     {
       
   226         QVariant tSize = m_contentDownload->getAttribute(DlTotalSize);
       
   227         setTotalSize(tSize.toInt());
       
   228         QVariant curDlsize = m_contentDownload->getAttribute(DlDownloadedSize);
       
   229         setDownloadedDataSize(curDlsize.toInt());
       
   230         setDownloadState(DlInprogress);
       
   231         postEvent(Progress, NULL);
       
   232         break;
       
   233     }
       
   234     case NetworkLoss:
       
   235     {
       
   236         postEvent(NetworkLoss, NULL);
       
   237         break;
       
   238     }
       
   239     case Cancelled:
       
   240     { 
       
   241         setDownloadState(DlCancelled);
       
   242         postEvent(Cancelled, NULL);
       
   243         break;
       
   244     }
       
   245     case Completed:
       
   246     {
       
   247         setDownloadState(DlCompleted);
       
   248         postEvent(Completed, NULL); 
       
   249         break;
       
   250     }
       
   251     case Error:
       
   252     {
       
   253         setDownloadState(DlFailed);
       
   254         postEvent(Error, NULL);
       
   255         break;
       
   256     } 
       
   257     default:
       
   258         break;
       
   259     }
       
   260     return true;
       
   261 }
       
   262 
       
   263 void WMDRMDownloadBackend::markDownloadFailed(const QString& error)
       
   264 {
       
   265     m_download->setError(error);
       
   266     setDownloadState(DlFailed);
       
   267     postEvent(Error, NULL);
       
   268     return;
       
   269 }
       
   270 
       
   271 void WMDRMDownloadBackend::RunL()
       
   272 {
       
   273     TInt result( iStatus.Int() );   
       
   274     // Handle request completion
       
   275     m_requestResult = result;
       
   276     // Stop Wait loop                            
       
   277     m_wait.AsyncStop();            
       
   278 }
       
   279 
       
   280 void WMDRMDownloadBackend::DoCancel()
       
   281 {   
       
   282     m_utility->Cancel();    
       
   283     // Cleanup
       
   284     m_requestResult = KErrCancel;
       
   285     // Stop Wait loop           
       
   286     m_wait.AsyncStop();
       
   287 }
       
   288