utilities/downloadmanager/src/downloadbackend.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 "downloadmanager.h"
hgs
parents:
diff changeset
    19
#include "downloadbackend.h"
hgs
parents:
diff changeset
    20
#include "downloadcore.h"
hgs
parents:
diff changeset
    21
#include "downloadfactory.h"
hgs
parents:
diff changeset
    22
#include "downloadstore.h"
hgs
parents:
diff changeset
    23
#include <QCoreApplication>
hgs
parents:
diff changeset
    24
#include <QDateTime>
hgs
parents:
diff changeset
    25
hgs
parents:
diff changeset
    26
//private implementation
hgs
parents:
diff changeset
    27
class DownloadBackendPrivate
hgs
parents:
diff changeset
    28
{
hgs
parents:
diff changeset
    29
    DM_DECLARE_PUBLIC(DownloadBackend);
hgs
parents:
diff changeset
    30
public:
hgs
parents:
diff changeset
    31
    DownloadBackendPrivate();
hgs
parents:
diff changeset
    32
    ~DownloadBackendPrivate();
hgs
parents:
diff changeset
    33
hgs
parents:
diff changeset
    34
    DownloadCore *m_downloadCore; // not owned
hgs
parents:
diff changeset
    35
    ClientDownload *m_download; //// not owned, only reference 
hgs
parents:
diff changeset
    36
    DownloadInfo *m_dlInfo; // not owned
hgs
parents:
diff changeset
    37
    qint64 m_totalSize; // total size of the download
hgs
parents:
diff changeset
    38
    qint64 m_currentDownloadedSize; // current downloaded size
hgs
parents:
diff changeset
    39
    qint64 m_lastPausedSize;
hgs
parents:
diff changeset
    40
    DownloadState m_downloadState; // state of the download
hgs
parents:
diff changeset
    41
    bool m_infoDeleted; // flag to indicate the info deletion    
hgs
parents:
diff changeset
    42
    QDateTime m_startTime; // download start/resumed time
hgs
parents:
diff changeset
    43
    QDateTime m_endTime; // download completed time
hgs
parents:
diff changeset
    44
    int m_progressCounter;
hgs
parents:
diff changeset
    45
};  
hgs
parents:
diff changeset
    46
hgs
parents:
diff changeset
    47
DownloadBackendPrivate::DownloadBackendPrivate()
hgs
parents:
diff changeset
    48
{
hgs
parents:
diff changeset
    49
    m_downloadCore = 0;
hgs
parents:
diff changeset
    50
    m_download = 0;
hgs
parents:
diff changeset
    51
    m_dlInfo = 0;
hgs
parents:
diff changeset
    52
    m_totalSize = 0;
hgs
parents:
diff changeset
    53
    m_currentDownloadedSize = 0;
hgs
parents:
diff changeset
    54
    m_lastPausedSize =0;
hgs
parents:
diff changeset
    55
    m_infoDeleted = false;
hgs
parents:
diff changeset
    56
    m_progressCounter = 1;
hgs
parents:
diff changeset
    57
}
hgs
parents:
diff changeset
    58
hgs
parents:
diff changeset
    59
DownloadBackendPrivate::~DownloadBackendPrivate()
hgs
parents:
diff changeset
    60
{
hgs
parents:
diff changeset
    61
#if 0 //m_downloadCore may be stale.
hgs
parents:
diff changeset
    62
    if(m_downloadCore)
hgs
parents:
diff changeset
    63
    {     
hgs
parents:
diff changeset
    64
        // cancel if there is any transaction
hgs
parents:
diff changeset
    65
        m_downloadCore->abort();
hgs
parents:
diff changeset
    66
    }
hgs
parents:
diff changeset
    67
#endif
hgs
parents:
diff changeset
    68
}
hgs
parents:
diff changeset
    69
hgs
parents:
diff changeset
    70
DownloadBackend::DownloadBackend(DownloadCore *dlCore, ClientDownload* dl)
hgs
parents:
diff changeset
    71
{
hgs
parents:
diff changeset
    72
    DM_INITIALIZE(DownloadBackend);
hgs
parents:
diff changeset
    73
    priv->m_downloadCore = dlCore;
hgs
parents:
diff changeset
    74
    priv->m_download = dl;
hgs
parents:
diff changeset
    75
    priv->m_dlInfo = dl->downloadInfo();
hgs
parents:
diff changeset
    76
    // connect all the signals from network
hgs
parents:
diff changeset
    77
    connect(dlCore, SIGNAL(downloadProgress(qint64 , qint64 )), this, SLOT(bytesRecieved(qint64 , qint64 )));
hgs
parents:
diff changeset
    78
    connect(dlCore, SIGNAL(finished()), this, SLOT(handleFinished()));
hgs
parents:
diff changeset
    79
    connect(dlCore, SIGNAL(metaDataChanged()), this, SLOT(headerReceived()));
hgs
parents:
diff changeset
    80
    connect(dlCore, SIGNAL(error(QNetworkReply::NetworkError)), this, SLOT(error(QNetworkReply::NetworkError)));
hgs
parents:
diff changeset
    81
    connect(dlCore, SIGNAL(uploadProgress(qint64, qint64)), this, SLOT(bytesUploaded(qint64, qint64)));
hgs
parents:
diff changeset
    82
hgs
parents:
diff changeset
    83
    // save the content type and url
hgs
parents:
diff changeset
    84
    setValue(DownloadInfo::EContentType, priv->m_downloadCore->contentType()); 
hgs
parents:
diff changeset
    85
    setValue(DownloadInfo::EUrl, priv->m_downloadCore->url()); 
hgs
parents:
diff changeset
    86
    setValue(DownloadInfo::EETag, priv->m_downloadCore->entityTag());
hgs
parents:
diff changeset
    87
    postEvent(Started, NULL);
hgs
parents:
diff changeset
    88
}
hgs
parents:
diff changeset
    89
hgs
parents:
diff changeset
    90
DownloadBackend::~DownloadBackend()
hgs
parents:
diff changeset
    91
{
hgs
parents:
diff changeset
    92
    DM_UNINITIALIZE(DownloadBackend);
hgs
parents:
diff changeset
    93
}
hgs
parents:
diff changeset
    94
hgs
parents:
diff changeset
    95
int DownloadBackend::pause()
hgs
parents:
diff changeset
    96
{
hgs
parents:
diff changeset
    97
    DM_PRIVATE(DownloadBackend);
hgs
parents:
diff changeset
    98
    setDownloadState(DlPaused);
hgs
parents:
diff changeset
    99
    priv->m_downloadCore->abort();
hgs
parents:
diff changeset
   100
    return 0;
hgs
parents:
diff changeset
   101
}
hgs
parents:
diff changeset
   102
hgs
parents:
diff changeset
   103
int DownloadBackend::resume()
hgs
parents:
diff changeset
   104
{
hgs
parents:
diff changeset
   105
    DM_PRIVATE(DownloadBackend);
hgs
parents:
diff changeset
   106
    setDownloadState(DlInprogress);
hgs
parents:
diff changeset
   107
    // save the content type and url
hgs
parents:
diff changeset
   108
    setValue(DownloadInfo::EUrl, priv->m_downloadCore->url());
hgs
parents:
diff changeset
   109
    setValue(DownloadInfo::EContentType, priv->m_downloadCore->contentType());
hgs
parents:
diff changeset
   110
    priv->m_lastPausedSize = priv->m_currentDownloadedSize;
hgs
parents:
diff changeset
   111
    priv->m_downloadCore->resumeDownload(priv->m_currentDownloadedSize);
hgs
parents:
diff changeset
   112
    priv->m_startTime = QDateTime::currentDateTime();
hgs
parents:
diff changeset
   113
    postEvent(Progress, NULL);
hgs
parents:
diff changeset
   114
    return 0;
hgs
parents:
diff changeset
   115
}
hgs
parents:
diff changeset
   116
hgs
parents:
diff changeset
   117
int DownloadBackend::cancel()
hgs
parents:
diff changeset
   118
{
hgs
parents:
diff changeset
   119
    DM_PRIVATE(DownloadBackend);   
hgs
parents:
diff changeset
   120
    setDownloadState(DlCancelled);
hgs
parents:
diff changeset
   121
    // cancel the transaction
hgs
parents:
diff changeset
   122
    priv->m_downloadCore->abort();
hgs
parents:
diff changeset
   123
    // delete the temporary storage
hgs
parents:
diff changeset
   124
    deleteStore();
hgs
parents:
diff changeset
   125
    // reset the states
hgs
parents:
diff changeset
   126
    priv->m_totalSize = 0;
hgs
parents:
diff changeset
   127
    priv->m_currentDownloadedSize = 0;
hgs
parents:
diff changeset
   128
    priv->m_lastPausedSize = 0;
hgs
parents:
diff changeset
   129
    return 0;
hgs
parents:
diff changeset
   130
}
hgs
parents:
diff changeset
   131
hgs
parents:
diff changeset
   132
QVariant DownloadBackend::getAttribute(DownloadAttribute attr)
hgs
parents:
diff changeset
   133
{
hgs
parents:
diff changeset
   134
    DM_PRIVATE(DownloadBackend); 
hgs
parents:
diff changeset
   135
    switch(attr)
hgs
parents:
diff changeset
   136
    {
hgs
parents:
diff changeset
   137
         case DlDownloadedSize:
hgs
parents:
diff changeset
   138
         {
hgs
parents:
diff changeset
   139
             return QVariant(priv->m_currentDownloadedSize);
hgs
parents:
diff changeset
   140
         }
hgs
parents:
diff changeset
   141
         case DlTotalSize:
hgs
parents:
diff changeset
   142
         {
hgs
parents:
diff changeset
   143
             return QVariant(priv->m_totalSize);
hgs
parents:
diff changeset
   144
         }
hgs
parents:
diff changeset
   145
         case DlDownloadState:
hgs
parents:
diff changeset
   146
         {
hgs
parents:
diff changeset
   147
             return QVariant(priv->m_downloadState);
hgs
parents:
diff changeset
   148
         }
hgs
parents:
diff changeset
   149
         case DlSourceUrl:
hgs
parents:
diff changeset
   150
         {
hgs
parents:
diff changeset
   151
             return QVariant(priv->m_downloadCore->url());
hgs
parents:
diff changeset
   152
         }
hgs
parents:
diff changeset
   153
         case DlContentType:
hgs
parents:
diff changeset
   154
         {
hgs
parents:
diff changeset
   155
             return QVariant(priv->m_downloadCore->contentType());
hgs
parents:
diff changeset
   156
         }
hgs
parents:
diff changeset
   157
         case DlStartTime:
hgs
parents:
diff changeset
   158
         {
hgs
parents:
diff changeset
   159
             return QVariant(priv->m_startTime);
hgs
parents:
diff changeset
   160
         }
hgs
parents:
diff changeset
   161
         case DlEndTime:
hgs
parents:
diff changeset
   162
         {
hgs
parents:
diff changeset
   163
             if (priv->m_downloadState == DlCompleted)
hgs
parents:
diff changeset
   164
                 return priv->m_endTime;
hgs
parents:
diff changeset
   165
             else 
hgs
parents:
diff changeset
   166
                 return QVariant();
hgs
parents:
diff changeset
   167
         }
hgs
parents:
diff changeset
   168
         case DlElapsedTime:
hgs
parents:
diff changeset
   169
         {
hgs
parents:
diff changeset
   170
             if (priv->m_downloadState == DlCompleted)
hgs
parents:
diff changeset
   171
                 return QVariant((priv->m_endTime.toTime_t()-priv->m_startTime.toTime_t()));
hgs
parents:
diff changeset
   172
hgs
parents:
diff changeset
   173
             if (priv->m_downloadState != DlInprogress)
hgs
parents:
diff changeset
   174
                 return QVariant();
hgs
parents:
diff changeset
   175
             QDateTime currentTime = QDateTime::currentDateTime();
hgs
parents:
diff changeset
   176
             uint timeElasped = currentTime.toTime_t() - priv->m_startTime.toTime_t();
hgs
parents:
diff changeset
   177
             return QVariant(timeElasped);
hgs
parents:
diff changeset
   178
         }
hgs
parents:
diff changeset
   179
         case DlRemainingTime:
hgs
parents:
diff changeset
   180
         {
hgs
parents:
diff changeset
   181
             if (priv->m_downloadState != DlInprogress)
hgs
parents:
diff changeset
   182
                 return QVariant();
hgs
parents:
diff changeset
   183
             QDateTime currentTime = QDateTime::currentDateTime();
hgs
parents:
diff changeset
   184
             uint timeElasped = currentTime.toTime_t() - priv->m_startTime.toTime_t();
hgs
parents:
diff changeset
   185
             // total bytes recieved since last start/resume
hgs
parents:
diff changeset
   186
             qint64 totalBytesRecieved = priv->m_currentDownloadedSize - priv->m_lastPausedSize;
hgs
parents:
diff changeset
   187
             qint64 remainingSize = priv->m_totalSize - priv->m_currentDownloadedSize;
hgs
parents:
diff changeset
   188
             if (totalBytesRecieved > 0)
hgs
parents:
diff changeset
   189
                 return QVariant((timeElasped*remainingSize)/totalBytesRecieved);
hgs
parents:
diff changeset
   190
             else
hgs
parents:
diff changeset
   191
                 return QVariant();
hgs
parents:
diff changeset
   192
         }
hgs
parents:
diff changeset
   193
         case DlSpeed:
hgs
parents:
diff changeset
   194
         {
hgs
parents:
diff changeset
   195
             if (priv->m_downloadState != DlInprogress)
hgs
parents:
diff changeset
   196
                 return QVariant();
hgs
parents:
diff changeset
   197
             QDateTime currentTime = QDateTime::currentDateTime();
hgs
parents:
diff changeset
   198
             uint timeElasped = currentTime.toTime_t() - priv->m_startTime.toTime_t();
hgs
parents:
diff changeset
   199
             qint64 totalBytesRecieved = priv->m_currentDownloadedSize - priv->m_lastPausedSize;
hgs
parents:
diff changeset
   200
             if (timeElasped > 0)
hgs
parents:
diff changeset
   201
                 return QVariant(totalBytesRecieved/timeElasped);
hgs
parents:
diff changeset
   202
             else
hgs
parents:
diff changeset
   203
                 return QVariant();
hgs
parents:
diff changeset
   204
         }
hgs
parents:
diff changeset
   205
         case DlPercentage:
hgs
parents:
diff changeset
   206
         {
hgs
parents:
diff changeset
   207
             if (priv->m_totalSize > 0)
hgs
parents:
diff changeset
   208
                 return QVariant((priv->m_currentDownloadedSize*100)/priv->m_totalSize);
hgs
parents:
diff changeset
   209
             else
hgs
parents:
diff changeset
   210
                 return QVariant();
hgs
parents:
diff changeset
   211
         }
hgs
parents:
diff changeset
   212
         case DlLastError:
hgs
parents:
diff changeset
   213
         {
hgs
parents:
diff changeset
   214
             return QVariant(priv->m_downloadCore->lastError());
hgs
parents:
diff changeset
   215
         }
hgs
parents:
diff changeset
   216
         case DlLastErrorString:
hgs
parents:
diff changeset
   217
         {
hgs
parents:
diff changeset
   218
             return QVariant(priv->m_downloadCore->lastErrorString());
hgs
parents:
diff changeset
   219
         }
hgs
parents:
diff changeset
   220
         default:
hgs
parents:
diff changeset
   221
         {
hgs
parents:
diff changeset
   222
             break;
hgs
parents:
diff changeset
   223
         }
hgs
parents:
diff changeset
   224
     }
hgs
parents:
diff changeset
   225
    return QVariant();
hgs
parents:
diff changeset
   226
     
hgs
parents:
diff changeset
   227
}
hgs
parents:
diff changeset
   228
hgs
parents:
diff changeset
   229
int DownloadBackend::setAttribute(DownloadAttribute /*attr*/, const QVariant& /*value*/)
hgs
parents:
diff changeset
   230
{
hgs
parents:
diff changeset
   231
    return -1;
hgs
parents:
diff changeset
   232
}
hgs
parents:
diff changeset
   233
hgs
parents:
diff changeset
   234
void DownloadBackend::bytesRecieved(qint64 bytesRecieved, qint64 bytesTotal)
hgs
parents:
diff changeset
   235
{
hgs
parents:
diff changeset
   236
    DM_PRIVATE(DownloadBackend);
hgs
parents:
diff changeset
   237
    if((priv->m_downloadState == DlFailed) || (bytesRecieved == 0))
hgs
parents:
diff changeset
   238
    {
hgs
parents:
diff changeset
   239
        return;
hgs
parents:
diff changeset
   240
    }
hgs
parents:
diff changeset
   241
 
hgs
parents:
diff changeset
   242
    // in case of resumed downloads, we recieve total size remained to download
hgs
parents:
diff changeset
   243
    setTotalSize(priv->m_lastPausedSize + bytesTotal);
hgs
parents:
diff changeset
   244
    priv->m_currentDownloadedSize = priv->m_lastPausedSize + bytesRecieved;
hgs
parents:
diff changeset
   245
    setDownloadState(DlInprogress);
hgs
parents:
diff changeset
   246
    // store the recieved chunk
hgs
parents:
diff changeset
   247
    store(priv->m_downloadCore->reply()->readAll(), false); 
hgs
parents:
diff changeset
   248
    postEvent(Progress, NULL);
hgs
parents:
diff changeset
   249
}
hgs
parents:
diff changeset
   250
hgs
parents:
diff changeset
   251
void DownloadBackend::handleFinished()
hgs
parents:
diff changeset
   252
{
hgs
parents:
diff changeset
   253
    DM_PRIVATE(DownloadBackend);
hgs
parents:
diff changeset
   254
hgs
parents:
diff changeset
   255
    DownloadState state = priv->m_downloadState;
hgs
parents:
diff changeset
   256
    if( state == DlFailed )
hgs
parents:
diff changeset
   257
    {
hgs
parents:
diff changeset
   258
        postEvent(Failed, NULL);  
hgs
parents:
diff changeset
   259
        return;
hgs
parents:
diff changeset
   260
    }
hgs
parents:
diff changeset
   261
    if( state == DlPaused )
hgs
parents:
diff changeset
   262
    {
hgs
parents:
diff changeset
   263
        postEvent(Paused, NULL);  
hgs
parents:
diff changeset
   264
        return;
hgs
parents:
diff changeset
   265
    }
hgs
parents:
diff changeset
   266
    if( state == DlCancelled )
hgs
parents:
diff changeset
   267
    {
hgs
parents:
diff changeset
   268
        postEvent(Cancelled, NULL);  
hgs
parents:
diff changeset
   269
        return;
hgs
parents:
diff changeset
   270
    }
hgs
parents:
diff changeset
   271
    if(priv->m_currentDownloadedSize < priv->m_totalSize)
hgs
parents:
diff changeset
   272
    {
hgs
parents:
diff changeset
   273
        // all packets are not recieved, so it is not last chunk 
hgs
parents:
diff changeset
   274
        // should be some network problem
hgs
parents:
diff changeset
   275
        store(priv->m_downloadCore->reply()->readAll(), false); 
hgs
parents:
diff changeset
   276
        postEvent(NetworkLoss, NULL);
hgs
parents:
diff changeset
   277
    }
hgs
parents:
diff changeset
   278
    else
hgs
parents:
diff changeset
   279
    {
hgs
parents:
diff changeset
   280
        //finish is successful
hgs
parents:
diff changeset
   281
        store(priv->m_downloadCore->reply()->readAll(), true);
hgs
parents:
diff changeset
   282
        //finish is successful
hgs
parents:
diff changeset
   283
        setDownloadState(DlCompleted);
hgs
parents:
diff changeset
   284
        priv->m_endTime = QDateTime::currentDateTime();
hgs
parents:
diff changeset
   285
        postEvent(Completed, NULL);
hgs
parents:
diff changeset
   286
    } 
hgs
parents:
diff changeset
   287
}
hgs
parents:
diff changeset
   288
hgs
parents:
diff changeset
   289
void DownloadBackend::error(QNetworkReply::NetworkError code)
hgs
parents:
diff changeset
   290
{
hgs
parents:
diff changeset
   291
    DM_PRIVATE(DownloadBackend);
hgs
parents:
diff changeset
   292
    if((code == QNetworkReply::OperationCanceledError) && (priv->m_downloadState == DlCancelled))
hgs
parents:
diff changeset
   293
    { 
hgs
parents:
diff changeset
   294
        return;
hgs
parents:
diff changeset
   295
    }   
hgs
parents:
diff changeset
   296
    if(code == QNetworkReply::OperationCanceledError) 
hgs
parents:
diff changeset
   297
    {
hgs
parents:
diff changeset
   298
        // this means user has paused the download
hgs
parents:
diff changeset
   299
        setDownloadState(DlPaused);
hgs
parents:
diff changeset
   300
    }   
hgs
parents:
diff changeset
   301
    else if(code != QNetworkReply::NoError)
hgs
parents:
diff changeset
   302
    {
hgs
parents:
diff changeset
   303
        priv->m_downloadCore->setLastError(code);
hgs
parents:
diff changeset
   304
        if(priv->m_downloadCore->reply())
hgs
parents:
diff changeset
   305
        {
hgs
parents:
diff changeset
   306
            priv->m_downloadCore->setLastErrorString(priv->m_downloadCore->reply()->errorString());
hgs
parents:
diff changeset
   307
        }
hgs
parents:
diff changeset
   308
        setDownloadState(DlFailed);
hgs
parents:
diff changeset
   309
        postEvent(Error, NULL);
hgs
parents:
diff changeset
   310
    }
hgs
parents:
diff changeset
   311
}
hgs
parents:
diff changeset
   312
hgs
parents:
diff changeset
   313
void DownloadBackend::postEvent(DEventType type, DlEventAttributeMap* attrMap)
hgs
parents:
diff changeset
   314
{
hgs
parents:
diff changeset
   315
    DM_PRIVATE(DownloadBackend);
hgs
parents:
diff changeset
   316
    // The client if it doesn't want progress events then it can set the DownloadMgrProgress mode as quiet
hgs
parents:
diff changeset
   317
    // If it wants progress events at regular intervals then it has to specify the KiloBytes at which it requires the progress event.
hgs
parents:
diff changeset
   318
    // By default, the DownloadMgrProgress Mode is non-quiet with progress events being sent at every 5KB downloaded. 
hgs
parents:
diff changeset
   319
    DownloadMgrProgressMode mode = (DownloadMgrProgressMode)(priv->m_download->downloadManager()->getAttribute(DlMgrProgressMode)).toInt();
hgs
parents:
diff changeset
   320
    if ((mode == Quiet) && (type == Progress))
hgs
parents:
diff changeset
   321
        return;
hgs
parents:
diff changeset
   322
hgs
parents:
diff changeset
   323
    qlonglong kBytes = priv->m_download->getAttribute(DlProgressInterval).toLongLong() * 1024;
hgs
parents:
diff changeset
   324
    if (mode == NonQuiet && type == Progress)
hgs
parents:
diff changeset
   325
    {
hgs
parents:
diff changeset
   326
        if ((priv->m_currentDownloadedSize/(kBytes * priv->m_progressCounter)) > 0)
hgs
parents:
diff changeset
   327
        {
hgs
parents:
diff changeset
   328
            priv->m_progressCounter++;
hgs
parents:
diff changeset
   329
            postDownloadEvent(type, attrMap);
hgs
parents:
diff changeset
   330
        }
hgs
parents:
diff changeset
   331
    }
hgs
parents:
diff changeset
   332
    else
hgs
parents:
diff changeset
   333
        postDownloadEvent(type, attrMap);
hgs
parents:
diff changeset
   334
}
hgs
parents:
diff changeset
   335
hgs
parents:
diff changeset
   336
DownloadState DownloadBackend::downloadState(void)
hgs
parents:
diff changeset
   337
{
hgs
parents:
diff changeset
   338
    DM_PRIVATE(DownloadBackend);
hgs
parents:
diff changeset
   339
    return priv->m_downloadState;
hgs
parents:
diff changeset
   340
}
hgs
parents:
diff changeset
   341
hgs
parents:
diff changeset
   342
void DownloadBackend::setDownloadState(DownloadState state)
hgs
parents:
diff changeset
   343
{
hgs
parents:
diff changeset
   344
    DM_PRIVATE(DownloadBackend);
hgs
parents:
diff changeset
   345
    priv->m_downloadState = state;
hgs
parents:
diff changeset
   346
    // save the download state
hgs
parents:
diff changeset
   347
    setValue(DownloadInfo::EDlState, priv->m_downloadState);
hgs
parents:
diff changeset
   348
    if((state == DlFailed) || (state == DlCompleted) || (state == DlCancelled))
hgs
parents:
diff changeset
   349
    {
hgs
parents:
diff changeset
   350
        // remove dl info
hgs
parents:
diff changeset
   351
            deleteInfo();
hgs
parents:
diff changeset
   352
    }
hgs
parents:
diff changeset
   353
}
hgs
parents:
diff changeset
   354
hgs
parents:
diff changeset
   355
void DownloadBackend::setDownloadedDataSize(qint64 size)
hgs
parents:
diff changeset
   356
{
hgs
parents:
diff changeset
   357
    DM_PRIVATE(DownloadBackend);
hgs
parents:
diff changeset
   358
    priv->m_currentDownloadedSize = size;
hgs
parents:
diff changeset
   359
    return;
hgs
parents:
diff changeset
   360
}
hgs
parents:
diff changeset
   361
hgs
parents:
diff changeset
   362
void DownloadBackend::setTotalSize(qint64 size)
hgs
parents:
diff changeset
   363
{
hgs
parents:
diff changeset
   364
    DM_PRIVATE(DownloadBackend);
hgs
parents:
diff changeset
   365
    priv->m_totalSize = size;
hgs
parents:
diff changeset
   366
    setValue(DownloadInfo::ETotalSize, priv->m_totalSize);
hgs
parents:
diff changeset
   367
    return;
hgs
parents:
diff changeset
   368
}
hgs
parents:
diff changeset
   369
hgs
parents:
diff changeset
   370
void DownloadBackend::setStartTime()
hgs
parents:
diff changeset
   371
{
hgs
parents:
diff changeset
   372
    DM_PRIVATE(DownloadBackend);
hgs
parents:
diff changeset
   373
    priv->m_startTime = QDateTime::currentDateTime();
hgs
parents:
diff changeset
   374
}
hgs
parents:
diff changeset
   375
hgs
parents:
diff changeset
   376
ClientDownload* DownloadBackend::download(void)
hgs
parents:
diff changeset
   377
{
hgs
parents:
diff changeset
   378
    DM_PRIVATE(DownloadBackend);
hgs
parents:
diff changeset
   379
    return priv->m_download;
hgs
parents:
diff changeset
   380
}
hgs
parents:
diff changeset
   381
hgs
parents:
diff changeset
   382
/* Helper functions to access download info */
hgs
parents:
diff changeset
   383
hgs
parents:
diff changeset
   384
int DownloadBackend::setValue(DownloadInfo::Key aKey, const QString& aStrValue)
hgs
parents:
diff changeset
   385
{
hgs
parents:
diff changeset
   386
    DM_PRIVATE(DownloadBackend);
hgs
parents:
diff changeset
   387
    if(priv->m_infoDeleted)
hgs
parents:
diff changeset
   388
        return 0;
hgs
parents:
diff changeset
   389
    return priv->m_dlInfo->setValue(priv->m_download->id(), aKey, aStrValue, priv->m_download->parentId());
hgs
parents:
diff changeset
   390
}
hgs
parents:
diff changeset
   391
hgs
parents:
diff changeset
   392
int DownloadBackend::setValueForChild(DownloadInfo::Key aKey, const QString& aStrValue, int aChildId)
hgs
parents:
diff changeset
   393
{
hgs
parents:
diff changeset
   394
    DM_PRIVATE(DownloadBackend);
hgs
parents:
diff changeset
   395
    if(priv->m_infoDeleted)
hgs
parents:
diff changeset
   396
        return 0;
hgs
parents:
diff changeset
   397
    return priv->m_dlInfo->setValueForChild(priv->m_download->id(), aKey, aStrValue, aChildId);
hgs
parents:
diff changeset
   398
}
hgs
parents:
diff changeset
   399
hgs
parents:
diff changeset
   400
int DownloadBackend::setValue(DownloadInfo::Key aKey, long aIntValue)
hgs
parents:
diff changeset
   401
{
hgs
parents:
diff changeset
   402
    DM_PRIVATE(DownloadBackend);
hgs
parents:
diff changeset
   403
    if(priv->m_infoDeleted)
hgs
parents:
diff changeset
   404
        return 0;
hgs
parents:
diff changeset
   405
    return priv->m_dlInfo->setValue(priv->m_download->id(), aKey, aIntValue, priv->m_download->parentId());
hgs
parents:
diff changeset
   406
}
hgs
parents:
diff changeset
   407
hgs
parents:
diff changeset
   408
int DownloadBackend::setValueForChild(DownloadInfo::Key aKey, long aIntValue, int aChildId)
hgs
parents:
diff changeset
   409
{
hgs
parents:
diff changeset
   410
    DM_PRIVATE(DownloadBackend);
hgs
parents:
diff changeset
   411
    if(priv->m_infoDeleted)
hgs
parents:
diff changeset
   412
        return 0;
hgs
parents:
diff changeset
   413
    return priv->m_dlInfo->setValueForChild(priv->m_download->id(), aKey, aIntValue, aChildId);
hgs
parents:
diff changeset
   414
}
hgs
parents:
diff changeset
   415
hgs
parents:
diff changeset
   416
int DownloadBackend::setValue(DownloadInfo::Key aKey, const QList<QVariant>& aChildIds)
hgs
parents:
diff changeset
   417
{
hgs
parents:
diff changeset
   418
    DM_PRIVATE(DownloadBackend);
hgs
parents:
diff changeset
   419
    if(priv->m_infoDeleted)
hgs
parents:
diff changeset
   420
        return 0;
hgs
parents:
diff changeset
   421
    return priv->m_dlInfo->setValue(priv->m_download->id(), aKey, aChildIds);
hgs
parents:
diff changeset
   422
}
hgs
parents:
diff changeset
   423
hgs
parents:
diff changeset
   424
int DownloadBackend::getValue(DownloadInfo::Key aKey, QString& aStrValue)
hgs
parents:
diff changeset
   425
{
hgs
parents:
diff changeset
   426
    DM_PRIVATE(DownloadBackend);
hgs
parents:
diff changeset
   427
    if(priv->m_infoDeleted)
hgs
parents:
diff changeset
   428
        return 0;
hgs
parents:
diff changeset
   429
    return priv->m_dlInfo->getValue(priv->m_download->id(), aKey, aStrValue, priv->m_download->parentId());
hgs
parents:
diff changeset
   430
}
hgs
parents:
diff changeset
   431
hgs
parents:
diff changeset
   432
int DownloadBackend::getValueForChild(DownloadInfo::Key aKey, QString& aStrValue, int aChildId)
hgs
parents:
diff changeset
   433
{
hgs
parents:
diff changeset
   434
    DM_PRIVATE(DownloadBackend);
hgs
parents:
diff changeset
   435
    if(priv->m_infoDeleted)
hgs
parents:
diff changeset
   436
        return 0;
hgs
parents:
diff changeset
   437
    return priv->m_dlInfo->getValueForChild(priv->m_download->id(), aKey, aStrValue, aChildId);
hgs
parents:
diff changeset
   438
}
hgs
parents:
diff changeset
   439
hgs
parents:
diff changeset
   440
int DownloadBackend::getValue(DownloadInfo::Key aKey, long& aIntValue)
hgs
parents:
diff changeset
   441
{
hgs
parents:
diff changeset
   442
    DM_PRIVATE(DownloadBackend);
hgs
parents:
diff changeset
   443
    if(priv->m_infoDeleted)
hgs
parents:
diff changeset
   444
        return 0;
hgs
parents:
diff changeset
   445
    return priv->m_dlInfo->getValue(priv->m_download->id(), aKey, aIntValue, priv->m_download->parentId());
hgs
parents:
diff changeset
   446
}
hgs
parents:
diff changeset
   447
hgs
parents:
diff changeset
   448
int DownloadBackend::getValueForChild(DownloadInfo::Key aKey, long& aIntValue, int aChildId)
hgs
parents:
diff changeset
   449
{
hgs
parents:
diff changeset
   450
    DM_PRIVATE(DownloadBackend);
hgs
parents:
diff changeset
   451
    if(priv->m_infoDeleted)
hgs
parents:
diff changeset
   452
        return 0;
hgs
parents:
diff changeset
   453
    return priv->m_dlInfo->getValueForChild(priv->m_download->id(), aKey, aIntValue, aChildId);
hgs
parents:
diff changeset
   454
 
hgs
parents:
diff changeset
   455
}
hgs
parents:
diff changeset
   456
hgs
parents:
diff changeset
   457
int DownloadBackend::getValue(DownloadInfo::Key aKey, QList<QVariant>& aChildIds)
hgs
parents:
diff changeset
   458
{
hgs
parents:
diff changeset
   459
    DM_PRIVATE(DownloadBackend);
hgs
parents:
diff changeset
   460
    if(priv->m_infoDeleted)
hgs
parents:
diff changeset
   461
        return 0;
hgs
parents:
diff changeset
   462
    return priv->m_dlInfo->getValue(priv->m_download->id(), aKey, aChildIds);
hgs
parents:
diff changeset
   463
}
hgs
parents:
diff changeset
   464
hgs
parents:
diff changeset
   465
int DownloadBackend::deleteInfo()
hgs
parents:
diff changeset
   466
{
hgs
parents:
diff changeset
   467
    DM_PRIVATE(DownloadBackend);
hgs
parents:
diff changeset
   468
    if ((InActive == priv->m_download->downloadManager()->getAttribute(DlMgrPersistantMode))
hgs
parents:
diff changeset
   469
        && (DlCompleted == priv->m_downloadState))
hgs
parents:
diff changeset
   470
        return 0;
hgs
parents:
diff changeset
   471
    priv->m_infoDeleted = true;
hgs
parents:
diff changeset
   472
    return priv->m_dlInfo->remove(priv->m_download->id(), priv->m_download->parentId()); 
hgs
parents:
diff changeset
   473
}
hgs
parents:
diff changeset
   474
void DownloadBackend::postDownloadEvent(DEventType type, DlEventAttributeMap* attrMap)
hgs
parents:
diff changeset
   475
{
hgs
parents:
diff changeset
   476
    DM_PRIVATE(DownloadBackend);
hgs
parents:
diff changeset
   477
    EventReceiverList list = priv->m_download->eventReceivers();
hgs
parents:
diff changeset
   478
    for(int i=0; i<list.size(); i++)
hgs
parents:
diff changeset
   479
    {
hgs
parents:
diff changeset
   480
        if(list[i])
hgs
parents:
diff changeset
   481
        {
hgs
parents:
diff changeset
   482
            DownloadEvent *event = new DownloadEvent(type, attrMap, priv->m_download->id());
hgs
parents:
diff changeset
   483
            QCoreApplication::postEvent(list[i], event);
hgs
parents:
diff changeset
   484
        }
hgs
parents:
diff changeset
   485
    }
hgs
parents:
diff changeset
   486
}