utilities/downloadmanager/src/clientdownload.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 "clientdownload.h"
       
    18 #include "downloadbackend.h"
       
    19 #include "downloadfactory.h"
       
    20 #include "downloadcoremanager.h"
       
    21 #include "downloadcore.h"
       
    22 #include "downloadmanager.h"
       
    23 #include "downloadevent.h"
       
    24 #include "downloadinfo.h"
       
    25 #include "sequentialdownloadmanager.h"
       
    26 #include <QNetworkReply>
       
    27 #include <QFileInfo>
       
    28 #include <QCoreApplication>
       
    29 
       
    30 #define PROGRESS_MINKB 5120 //Minimum KB to send progress events
       
    31 
       
    32 // private implementation
       
    33 class ClientDownloadPrivate
       
    34 {
       
    35     DM_DECLARE_PUBLIC(ClientDownload);
       
    36 public:
       
    37     ClientDownloadPrivate();
       
    38     ~ClientDownloadPrivate();
       
    39     int m_downloadId;
       
    40     QString m_url;  // url
       
    41     DownloadManager *m_downloadManager; // not owned, only reference
       
    42     DownloadBackend *m_downloadBackend;
       
    43     DownloadCore *m_downloadCore; // not owned
       
    44     EventReceiverList m_eventReceiverList; // list of event listeners
       
    45     DownloadInfo *m_dlInfo;         // not owned  
       
    46     bool m_createdByDlInfo; // flag to indicate persistant ClientDownload
       
    47     int m_parentId; // to set parent id
       
    48     QMap<DownloadAttribute, QVariant> m_downloadAttrMap; // the map of attributes can be set by client
       
    49 };
       
    50 
       
    51 ClientDownloadPrivate::ClientDownloadPrivate():m_downloadId(-1)
       
    52                                               ,m_url("")
       
    53                                               ,m_downloadManager(0)
       
    54                                               ,m_downloadBackend(0)
       
    55                                               ,m_downloadCore(0)
       
    56                                               ,m_dlInfo(0)
       
    57                                               ,m_createdByDlInfo(false)
       
    58                                               ,m_parentId(INVALID_DL_ID)
       
    59 { }
       
    60 
       
    61 ClientDownloadPrivate::~ClientDownloadPrivate()
       
    62 {
       
    63     if(m_downloadBackend)
       
    64     {
       
    65          delete m_downloadBackend;
       
    66          m_downloadBackend = 0;
       
    67     }
       
    68 }
       
    69 
       
    70 /*!
       
    71  * \class ClientDownload
       
    72  *
       
    73  * \brief The public APIs for managing a ClientDownload
       
    74  *
       
    75  * This class has the public APIs for managing a single ClientDownload
       
    76  */
       
    77 
       
    78 // note that parentdlId is id of the parent download which is applicable in the case
       
    79 // where parent download is oma download and child is media download
       
    80 ClientDownload::ClientDownload(DownloadManager *mgr, const QString& url
       
    81                                                    , int dlId
       
    82                                                    , DownloadType type/*=Parallel*/
       
    83                                                    , int parentdlId /*=INVALID_DL_ID*/)
       
    84 {
       
    85     DM_INITIALIZE(ClientDownload);
       
    86     priv->m_downloadId = dlId;
       
    87     priv->m_downloadManager = mgr;
       
    88     priv->m_url = url;
       
    89     priv->m_dlInfo = priv->m_downloadManager->downloadInfo();
       
    90     if (parentdlId > INVALID_DL_ID)
       
    91         setParentId(parentdlId);
       
    92 
       
    93     // set the default destination path
       
    94     priv->m_downloadAttrMap.insert(DlDestPath, mgr->getAttribute(DlMgrDestPath));
       
    95     // set the default filename from url
       
    96     QUrl urlToDownload(url);
       
    97     QFileInfo fileUrl(urlToDownload.path());
       
    98     QString fileName = fileUrl.fileName();
       
    99     priv->m_downloadAttrMap.insert(DlFileName, fileName);
       
   100     priv->m_dlInfo->setValue(priv->m_downloadId, DownloadInfo::EFileName, fileName, parentdlId);
       
   101     // set 5Kb as minimum size to send the progress event
       
   102     priv->m_downloadAttrMap.insert(DlProgressInterval, PROGRESS_MINKB);
       
   103     // set the download type
       
   104     priv->m_downloadAttrMap.insert(DlDownloadType, type);
       
   105     priv->m_dlInfo->setValue(priv->m_downloadId, DownloadInfo::EType, (long)type, parentdlId);
       
   106     // set the download scope
       
   107     priv->m_downloadAttrMap.insert(DlDownloadScope, Normal);
       
   108     priv->m_dlInfo->setValue(priv->m_downloadId, DownloadInfo::EScope, (long)Normal, parentdlId);
       
   109 
       
   110     // create ClientDownload core for network transactions
       
   111     priv->m_downloadCore = priv->m_downloadManager->downloadCoreManager()->createDownloadCore(url);
       
   112     // set the proxy
       
   113     priv->m_downloadCore->setProxy(mgr->proxy());
       
   114     connect(priv->m_downloadCore, SIGNAL(metaDataChanged()), this, SLOT(createDownloadImplementation()));
       
   115 }
       
   116 
       
   117 // note that parentdlId is id of the parent download which is applicable in the case
       
   118 // where parent download is oma download and child is media download
       
   119 ClientDownload::ClientDownload(DownloadManager *mgr, QNetworkReply *reply
       
   120                                                    , int dlId
       
   121                                                    , int parentdlId /*=INVALID_DL_ID*/)
       
   122 {
       
   123     DM_INITIALIZE(ClientDownload);
       
   124     priv->m_downloadManager = mgr;
       
   125     priv->m_downloadId = dlId;
       
   126     priv->m_dlInfo = priv->m_downloadManager->downloadInfo();
       
   127     if (parentdlId > INVALID_DL_ID)
       
   128         setParentId(parentdlId);
       
   129 
       
   130     // set the default destination path
       
   131     priv->m_downloadAttrMap.insert(DlDestPath, mgr->getAttribute(DlMgrDestPath));
       
   132     if (reply) {
       
   133         // set the default filename from url
       
   134         QUrl urlToDownload(reply->url());
       
   135         QFileInfo fileUrl(urlToDownload.path());
       
   136         QString fileName = fileUrl.fileName();
       
   137         priv->m_downloadAttrMap.insert(DlFileName, fileName);
       
   138         priv->m_dlInfo->setValue(priv->m_downloadId, DownloadInfo::EFileName, fileName, parentdlId);
       
   139     }
       
   140     // set 5Kb as minimum size to send the progress event
       
   141     priv->m_downloadAttrMap.insert(DlProgressInterval, PROGRESS_MINKB);
       
   142     // set the download type as parallel
       
   143     priv->m_downloadAttrMap.insert(DlDownloadType, Parallel);
       
   144     priv->m_dlInfo->setValue(priv->m_downloadId, DownloadInfo::EType, (long)Parallel, parentdlId);
       
   145         // set the download scope
       
   146     priv->m_downloadAttrMap.insert(DlDownloadScope, Normal);
       
   147     priv->m_dlInfo->setValue(priv->m_downloadId, DownloadInfo::EScope, (long)Normal, parentdlId);
       
   148     // create ClientDownload core for network transactions
       
   149     priv->m_downloadCore = priv->m_downloadManager->downloadCoreManager()->createDownloadCore(reply);
       
   150     // create actual implementation class based on content type of the ClientDownload
       
   151     priv->m_downloadBackend = DownloadAbstractFactory::createDownloadImplementation(priv->m_downloadCore, this);
       
   152     priv->m_downloadBackend->setStartTime();
       
   153  }
       
   154 
       
   155 // this is the case for reading the persistant  ClientDownload information of last session and 
       
   156 // creating ClientDownload object
       
   157 ClientDownload::ClientDownload(DownloadManager *mgr, int dlId, int parentdlId)
       
   158 {
       
   159     DM_INITIALIZE(ClientDownload);
       
   160     priv->m_downloadManager = mgr;
       
   161     priv->m_downloadId = dlId;
       
   162     priv->m_createdByDlInfo =  true;
       
   163     priv->m_dlInfo = priv->m_downloadManager->downloadInfo();
       
   164     if (parentdlId > INVALID_DL_ID)
       
   165          setParentId(parentdlId);
       
   166 
       
   167     // read the path from persistant
       
   168     QString path;
       
   169     priv->m_dlInfo->getValue(priv->m_downloadId, DownloadInfo::EFinalPath, path, parentdlId);
       
   170     priv->m_downloadAttrMap.insert(DlDestPath, path);
       
   171 
       
   172     // set 5Kb as minimum size to send the progress event
       
   173     priv->m_downloadAttrMap.insert(DlProgressInterval, PROGRESS_MINKB);
       
   174     
       
   175     // set the download type parallel/sequential
       
   176     long type;
       
   177     priv->m_dlInfo->getValue(priv->m_downloadId, DownloadInfo::EType, type, parentdlId);
       
   178     priv->m_downloadAttrMap.insert(DlDownloadType, (DownloadType)type);
       
   179 
       
   180     // set the priority
       
   181     long priority;
       
   182     priv->m_dlInfo->getValue(priv->m_downloadId, DownloadInfo::EPriority, priority, parentdlId);
       
   183     priv->m_downloadAttrMap.insert(DlPriority, (DownloadPriority)priority);
       
   184 
       
   185     // fetching url
       
   186     QString url;
       
   187     priv->m_dlInfo->getValue(priv->m_downloadId, DownloadInfo::EUrl, url, parentdlId);
       
   188     // create ClientDownload core for network transactions
       
   189     priv->m_downloadCore = priv->m_downloadManager->downloadCoreManager()->createDownloadCore(url);
       
   190     // set the proxy
       
   191     QNetworkProxy *proxy = priv->m_downloadCore->proxy();
       
   192     if(!proxy && priv->m_downloadCore && priv->m_downloadManager) {
       
   193          //set ClientDownload core's proxy as ClientDownload manager's proxy, if its not already set
       
   194          priv->m_downloadCore->setProxy(priv->m_downloadManager->proxy());
       
   195     }
       
   196     // fetch the content type
       
   197     QString contentType;
       
   198     priv->m_dlInfo->getValue(priv->m_downloadId, DownloadInfo::EContentType, contentType, parentdlId);
       
   199     priv->m_downloadCore->setContentType(contentType);
       
   200 
       
   201     // fetch ETag header value
       
   202     QString entityTag;
       
   203     priv->m_dlInfo->getValue(priv->m_downloadId, DownloadInfo::EETag, entityTag, parentdlId);
       
   204     priv->m_downloadCore->setEntityTag(entityTag);
       
   205 
       
   206     // create actual implementation class based on content type of the ClientDownload
       
   207     priv->m_downloadBackend = DownloadAbstractFactory::createDownloadImplementation(priv->m_downloadCore, this);
       
   208     priv->m_downloadBackend->init();
       
   209 
       
   210     long size = 0;
       
   211     priv->m_dlInfo->getValue(priv->m_downloadId, DownloadInfo::ETotalSize, size, parentdlId);
       
   212     priv->m_downloadBackend->setTotalSize(size);
       
   213 
       
   214     long state = 0;
       
   215     priv->m_dlInfo->getValue(priv->m_downloadId, DownloadInfo::EDlState, state, parentdlId);
       
   216     if (state == DlCompleted) 
       
   217         priv->m_downloadBackend->setDownloadedDataSize(size);
       
   218     else {
       
   219         // fetch the already downloaded data size
       
   220         qint64 currentSize = priv->m_downloadBackend->storedDataSize();    
       
   221         priv->m_downloadBackend->setDownloadedDataSize(currentSize);
       
   222     }
       
   223     
       
   224     if (state  == DlInprogress)
       
   225         priv->m_downloadBackend->setDownloadState(DlPaused);
       
   226     else
       
   227         priv->m_downloadBackend->setDownloadState((DownloadState)state);       
       
   228 }
       
   229  
       
   230 ClientDownload::~ClientDownload()
       
   231 { 
       
   232     DM_UNINITIALIZE(ClientDownload);
       
   233 }
       
   234 
       
   235 /*!
       
   236   returns id of the ClientDownload
       
   237 */
       
   238 int ClientDownload::id()
       
   239 {
       
   240     DM_PRIVATE(ClientDownload);
       
   241     return priv->m_downloadId;
       
   242 }
       
   243 
       
   244 /*!
       
   245   starts the ClientDownload, returns the success status
       
   246 */
       
   247 int ClientDownload::start()
       
   248 {
       
   249     DM_PRIVATE(ClientDownload);
       
   250     DownloadType type = (DownloadType)((priv->m_downloadAttrMap.value(DlDownloadType)).toInt());
       
   251     if(type == Sequential)
       
   252         priv->m_downloadManager->sequentialManager()->process(priv->m_downloadId);
       
   253     else
       
   254         startDownload(); // starts the download parallely
       
   255     return 0;
       
   256 }
       
   257 
       
   258 /*!
       
   259   sets the attribute for the ClientDownload
       
   260   \a attr indicates attribute
       
   261   \a value indicates value for the ClientDownload
       
   262 */
       
   263 int ClientDownload::setAttribute(DownloadAttribute attr, const QVariant& value)
       
   264 {
       
   265     //sets the attribute to ClientDownload backend
       
   266     DM_PRIVATE(ClientDownload);
       
   267     switch(attr)
       
   268     {
       
   269         case DlFileName:
       
   270         case DlDestPath:
       
   271         {            
       
   272             // download is just created but not started
       
   273             // filename, destination path can be set only before the download has started
       
   274             if(!priv->m_downloadBackend)
       
   275             {
       
   276                 QString strValue = value.toString();       
       
   277                 if(strValue.length() != 0)
       
   278                     priv->m_downloadAttrMap.insert(attr, value);
       
   279                 return 0;
       
   280             }
       
   281             else
       
   282                 return -1;
       
   283         }
       
   284         case DlPriority:
       
   285             {
       
   286                 // cannot change the priority once the download has started
       
   287                 if(!priv->m_downloadBackend)
       
   288                 {
       
   289                     priv->m_downloadAttrMap.insert(attr, value);
       
   290                     priv->m_dlInfo->setValue(priv->m_downloadId, DownloadInfo::EPriority, (long)(value.toInt()), parentId());
       
   291                     // reshuffle the download queue based on the priority
       
   292                     if((DownloadType)(priv->m_downloadAttrMap.value(DlDownloadType).toInt()) == Sequential)
       
   293                         priv->m_downloadManager->sequentialManager()->addToSequentialDownload(this);
       
   294                     return 0;
       
   295                 }
       
   296             }
       
   297         case DlProgressInterval:
       
   298         {
       
   299             qlonglong val = value.toLongLong() * 1024;
       
   300             if (val >= PROGRESS_MINKB) {
       
   301                 priv->m_downloadAttrMap.insert(attr, val);
       
   302                 return 0;
       
   303             }
       
   304         }
       
   305                 
       
   306         default:
       
   307             if(priv->m_downloadBackend)
       
   308                 return priv->m_downloadBackend->setAttribute(attr, value);
       
   309     }
       
   310     return 0;
       
   311 }
       
   312 
       
   313 /*!
       
   314   fetches the attribute of the ClientDownload
       
   315   \a attr indicates ClientDownload attribute
       
   316 */
       
   317 QVariant ClientDownload::getAttribute(DownloadAttribute attr)
       
   318 {
       
   319     //gets attribute
       
   320     DM_PRIVATE(ClientDownload);
       
   321     switch(attr)
       
   322     {
       
   323         case DlFileName:
       
   324         {
       
   325             if(priv->m_downloadBackend) // download is already started
       
   326                 return priv->m_downloadBackend->getAttribute(attr);
       
   327             else
       
   328                 return priv->m_downloadAttrMap.value(DlFileName);
       
   329         }
       
   330         case DlDestPath:
       
   331         {
       
   332             return priv->m_downloadAttrMap.value(DlDestPath);
       
   333         }
       
   334         case DlPriority:
       
   335         {
       
   336             return priv->m_downloadAttrMap.value(DlPriority);
       
   337         }
       
   338         case DlDownloadType:
       
   339         {
       
   340             return priv->m_downloadAttrMap.value(DlDownloadType);
       
   341         }
       
   342         case DlProgressInterval:
       
   343         {
       
   344             qlonglong val = priv->m_downloadAttrMap.value(DlProgressInterval).toLongLong() / 1024;
       
   345             return val;
       
   346         }
       
   347         default:
       
   348         {
       
   349             if(priv->m_downloadBackend)
       
   350                 return priv->m_downloadBackend->getAttribute(attr);
       
   351         }
       
   352     }  
       
   353     return QVariant();
       
   354 }
       
   355 
       
   356 /*!
       
   357   pauses the ClientDownload
       
   358 */
       
   359 int ClientDownload::pause()
       
   360 {
       
   361     DM_PRIVATE(ClientDownload);
       
   362     DownloadType type = (DownloadType)((priv->m_downloadAttrMap.value(DlDownloadType)).toInt());
       
   363     if(type == Sequential)
       
   364         priv->m_downloadManager->sequentialManager()->pauseDownload(priv->m_downloadId);
       
   365     else
       
   366         pauseDownload(); // pauses the download parallely
       
   367     return 0;
       
   368 }
       
   369 
       
   370 /*!
       
   371   resumes the ClientDownload
       
   372 */
       
   373 int ClientDownload::resume()
       
   374 {
       
   375     DM_PRIVATE(ClientDownload);
       
   376     DownloadType type = (DownloadType)((priv->m_downloadAttrMap.value(DlDownloadType)).toInt());
       
   377     if(type == Sequential)
       
   378         priv->m_downloadManager->sequentialManager()->resumeDownload(priv->m_downloadId);
       
   379     else
       
   380         resumeDownload(); // resumes the download parallely
       
   381     return 0;
       
   382 }
       
   383 
       
   384 /*!
       
   385   cancels the ClientDownload
       
   386 */
       
   387 int ClientDownload::cancel()
       
   388 {
       
   389     DM_PRIVATE(ClientDownload);
       
   390     DownloadType type = (DownloadType)((priv->m_downloadAttrMap.value(DlDownloadType)).toInt());
       
   391     if(type == Sequential)
       
   392         priv->m_downloadManager->sequentialManager()->cancelDownload(priv->m_downloadId);
       
   393     else
       
   394         cancelDownload(); // cancels the download parallely
       
   395     return 0;
       
   396 }
       
   397 
       
   398 /*!
       
   399   registers receiver for the ClientDownload events
       
   400   \a reciever indicates reciever which listen to ClientDownload events
       
   401 */
       
   402 void ClientDownload::registerEventReceiver(QObject *receiver)
       
   403 {
       
   404     DM_PRIVATE(ClientDownload);
       
   405     if(receiver)
       
   406         if (!priv->m_eventReceiverList.contains(receiver))
       
   407             priv->m_eventReceiverList.append(receiver);
       
   408 }
       
   409 
       
   410 /*!
       
   411   unregisters the event listener
       
   412   \a receiver indicates listener which will be unregistered
       
   413 */
       
   414 void ClientDownload::unregisterEventReceiver(QObject *receiver)
       
   415 {
       
   416     DM_PRIVATE(ClientDownload);
       
   417     priv->m_eventReceiverList.removeOne(receiver);
       
   418 }
       
   419 
       
   420 EventReceiverList& ClientDownload::eventReceivers()
       
   421 {
       
   422     DM_PRIVATE(ClientDownload);
       
   423     return priv->m_eventReceiverList;
       
   424 }
       
   425 
       
   426 /*!
       
   427   returns ClientDownload manager
       
   428 */
       
   429 DownloadManager* ClientDownload::downloadManager()
       
   430 {
       
   431     DM_PRIVATE(ClientDownload);
       
   432     return priv->m_downloadManager;
       
   433 }
       
   434 
       
   435 /*!
       
   436   returns the child downloads i.e if ClientDownload has any media objects
       
   437   \a list indicates list of child downloads
       
   438 */
       
   439 void ClientDownload::getChildren(QList<Download*>& list)
       
   440 {
       
   441     DM_PRIVATE(ClientDownload);
       
   442     if(priv->m_downloadBackend)
       
   443        priv->m_downloadBackend->getChildren(list);
       
   444 }
       
   445 
       
   446 DownloadInfo* ClientDownload::downloadInfo()
       
   447 {
       
   448     DM_PRIVATE(ClientDownload);
       
   449     return priv->m_dlInfo;
       
   450 }
       
   451 
       
   452 void ClientDownload::createDownloadImplementation()
       
   453 {
       
   454     DM_PRIVATE(ClientDownload);
       
   455     //ClientDownload backend is created based on the content type
       
   456     if(!priv->m_downloadBackend) {
       
   457         priv->m_downloadBackend = DownloadAbstractFactory::createDownloadImplementation(priv->m_downloadCore, this);
       
   458         priv->m_downloadBackend->setStartTime();
       
   459         postEvent(HeaderReceived, NULL);
       
   460     }
       
   461 }
       
   462 
       
   463 void ClientDownload::postEvent(DEventType type, DlEventAttributeMap* attrMap)
       
   464 {
       
   465     DM_PRIVATE(ClientDownload);
       
   466     EventReceiverList list = eventReceivers();
       
   467     for(int i=0; i<list.size(); i++) {
       
   468         if(list[i]) {
       
   469             DownloadEvent *event = new DownloadEvent(type, attrMap, priv->m_downloadId);
       
   470             QCoreApplication::postEvent(list[i], event);
       
   471         }
       
   472     }
       
   473 }
       
   474 
       
   475 void ClientDownload::setError(const QString& errorStr)
       
   476 {
       
   477     DM_PRIVATE(ClientDownload);
       
   478     priv->m_downloadCore->setLastErrorString(errorStr);
       
   479     priv->m_downloadCore->setLastError(QNetworkReply::UnknownContentError);
       
   480 }
       
   481 
       
   482 void ClientDownload::setDownloadState(DownloadState state)
       
   483 {
       
   484     DM_PRIVATE(ClientDownload);
       
   485     priv->m_downloadBackend->setDownloadState(state);
       
   486 }
       
   487 
       
   488 bool ClientDownload::isCreatedByDlInfo(void)
       
   489 {
       
   490     DM_PRIVATE(ClientDownload);
       
   491     return priv->m_createdByDlInfo;
       
   492 }
       
   493 
       
   494 void ClientDownload::setParentId(int parentId)
       
   495 {
       
   496     DM_PRIVATE(ClientDownload);
       
   497     priv->m_parentId = parentId;
       
   498 }
       
   499 
       
   500 int ClientDownload::parentId()
       
   501 {
       
   502     DM_PRIVATE(ClientDownload);
       
   503     return priv->m_parentId;
       
   504 }
       
   505 
       
   506 QMap<DownloadAttribute, QVariant>& ClientDownload::attributes(void)
       
   507 {
       
   508     DM_PRIVATE(ClientDownload);
       
   509     return priv->m_downloadAttrMap;
       
   510 }
       
   511 
       
   512 // starts the ClientDownload, returns the success status
       
   513 int ClientDownload::startDownload()
       
   514 {
       
   515     DM_PRIVATE(ClientDownload);
       
   516     if (!(priv->m_downloadCore))
       
   517         return -1;  
       
   518     QNetworkProxy *proxy = priv->m_downloadCore->proxy();
       
   519     if(!proxy && priv->m_downloadManager) {
       
   520          //set ClientDownload core's proxy as ClientDownload manager's proxy, if its not already set
       
   521          priv->m_downloadCore->setProxy(priv->m_downloadManager->proxy());
       
   522     }
       
   523     priv->m_downloadCore->doDownload();
       
   524 
       
   525     return 0;
       
   526 }
       
   527 
       
   528 int ClientDownload::pauseDownload()
       
   529 {
       
   530     DM_PRIVATE(ClientDownload);
       
   531     if( priv->m_downloadBackend ) {
       
   532         DownloadState state = (DownloadState)priv->m_downloadBackend->getAttribute(DlDownloadState).toInt();
       
   533         if( state != DlInprogress)
       
   534             return 0;
       
   535         return priv->m_downloadBackend->pause();
       
   536     }
       
   537     else
       
   538         priv->m_downloadCore->abort();
       
   539     return 0;
       
   540 }
       
   541 
       
   542 int ClientDownload::resumeDownload()
       
   543 {
       
   544     DM_PRIVATE(ClientDownload);
       
   545     QNetworkProxy *proxy = priv->m_downloadCore->proxy();
       
   546     if(!proxy && priv->m_downloadCore && priv->m_downloadManager) {
       
   547          //set ClientDownload core's proxy as ClientDownload manager's proxy, if its not already set
       
   548          priv->m_downloadCore->setProxy(priv->m_downloadManager->proxy());
       
   549     }
       
   550     if(priv->m_downloadBackend) {
       
   551         DownloadState state = (DownloadState)priv->m_downloadBackend->getAttribute(DlDownloadState).toInt();
       
   552         if((state != DlPaused) && (state != DlCancelled))
       
   553             return 0;
       
   554         return priv->m_downloadBackend->resume();
       
   555     }
       
   556     else
       
   557         startDownload(); // This means, ClientDownload has just been created but never started
       
   558     return 0;
       
   559 }
       
   560 
       
   561 int ClientDownload::cancelDownload()
       
   562 {
       
   563     // cancels the ClientDownload
       
   564     DM_PRIVATE(ClientDownload);
       
   565     if( priv->m_downloadBackend ) {
       
   566         DownloadState state = (DownloadState)priv->m_downloadBackend->getAttribute(DlDownloadState).toInt();
       
   567         if((state == DlCompleted) || (state == DlFailed) || (state == DlCancelled))
       
   568             return 0; 
       
   569         return priv->m_downloadBackend->cancel();
       
   570     }
       
   571     return 0;
       
   572 }
       
   573