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