utilities/downloadmanager/src/backgrounddownload.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 "backgrounddownload.h"
hgs
parents:
diff changeset
    18
#include "download.h"
hgs
parents:
diff changeset
    19
#include "downloadmanager.h"
hgs
parents:
diff changeset
    20
#include "backgrounddownloadmanager.h"
hgs
parents:
diff changeset
    21
#include "downloadmanagerclient.h"
hgs
parents:
diff changeset
    22
#include "downloadevent.h"
hgs
parents:
diff changeset
    23
#include "downloadinfo.h"
hgs
parents:
diff changeset
    24
#include <QNetworkReply>
hgs
parents:
diff changeset
    25
#include <QCoreApplication>
hgs
parents:
diff changeset
    26
hgs
parents:
diff changeset
    27
// private implementation
hgs
parents:
diff changeset
    28
class BackgroundDownloadPrivate
hgs
parents:
diff changeset
    29
{
hgs
parents:
diff changeset
    30
    DM_DECLARE_PUBLIC(BackgroundDownload);
hgs
parents:
diff changeset
    31
public:
hgs
parents:
diff changeset
    32
    BackgroundDownloadPrivate();
hgs
parents:
diff changeset
    33
    ~BackgroundDownloadPrivate();
hgs
parents:
diff changeset
    34
    int m_downloadId;
hgs
parents:
diff changeset
    35
    QString m_url;
hgs
parents:
diff changeset
    36
    DownloadManager *m_downloadManager; // not owned
hgs
parents:
diff changeset
    37
    BackgroundDownloadManager* m_backgroundMgr; // not owned
hgs
parents:
diff changeset
    38
    EventReceiverList m_eventReceiverList; // list of event listeners
hgs
parents:
diff changeset
    39
    DownloadManagerClient* m_downloadMgrClient; // not owned
hgs
parents:
diff changeset
    40
    DownloadInfo *m_dlInfo;         // not owned
hgs
parents:
diff changeset
    41
};
hgs
parents:
diff changeset
    42
hgs
parents:
diff changeset
    43
BackgroundDownloadPrivate::BackgroundDownloadPrivate()
hgs
parents:
diff changeset
    44
{
hgs
parents:
diff changeset
    45
    m_downloadId = INVALID_DL_ID;
hgs
parents:
diff changeset
    46
    m_downloadManager = 0;
hgs
parents:
diff changeset
    47
    m_backgroundMgr = 0;
hgs
parents:
diff changeset
    48
    m_downloadMgrClient = 0;
hgs
parents:
diff changeset
    49
}
hgs
parents:
diff changeset
    50
hgs
parents:
diff changeset
    51
BackgroundDownloadPrivate::~BackgroundDownloadPrivate()
hgs
parents:
diff changeset
    52
{
hgs
parents:
diff changeset
    53
}
hgs
parents:
diff changeset
    54
hgs
parents:
diff changeset
    55
/*!
hgs
parents:
diff changeset
    56
 * \class BackgroundDownload
hgs
parents:
diff changeset
    57
 *
hgs
parents:
diff changeset
    58
 * \brief The public APIs for managing a background download
hgs
parents:
diff changeset
    59
 *
hgs
parents:
diff changeset
    60
 * This class has the public APIs for managing a single background download
hgs
parents:
diff changeset
    61
 */
hgs
parents:
diff changeset
    62
hgs
parents:
diff changeset
    63
BackgroundDownload::BackgroundDownload(DownloadManager *mgr, const QString& url, int dlId)
hgs
parents:
diff changeset
    64
{
hgs
parents:
diff changeset
    65
    DM_INITIALIZE(BackgroundDownload);
hgs
parents:
diff changeset
    66
    priv->m_downloadId = dlId;
hgs
parents:
diff changeset
    67
    priv->m_url = url;
hgs
parents:
diff changeset
    68
    priv->m_downloadManager = mgr;
hgs
parents:
diff changeset
    69
    priv->m_backgroundMgr = mgr->backgroundManager();
hgs
parents:
diff changeset
    70
    priv->m_downloadMgrClient = priv->m_backgroundMgr->downloadManagerClient();
hgs
parents:
diff changeset
    71
    if (mgr)
hgs
parents:
diff changeset
    72
        priv->m_dlInfo = mgr->downloadInfo();
hgs
parents:
diff changeset
    73
    if (priv->m_dlInfo) {
hgs
parents:
diff changeset
    74
        priv->m_dlInfo->setValue(priv->m_downloadId, DownloadInfo::EUrl, (const QString&)url);
hgs
parents:
diff changeset
    75
        priv->m_dlInfo->setValue(priv->m_downloadId, DownloadInfo::EScope, (long)Background);
hgs
parents:
diff changeset
    76
    }
hgs
parents:
diff changeset
    77
}
hgs
parents:
diff changeset
    78
hgs
parents:
diff changeset
    79
BackgroundDownload::~BackgroundDownload()
hgs
parents:
diff changeset
    80
{
hgs
parents:
diff changeset
    81
    DM_UNINITIALIZE(BackgroundDownload);
hgs
parents:
diff changeset
    82
}
hgs
parents:
diff changeset
    83
hgs
parents:
diff changeset
    84
/*!
hgs
parents:
diff changeset
    85
  starts the background download, returns the success status
hgs
parents:
diff changeset
    86
*/
hgs
parents:
diff changeset
    87
int BackgroundDownload::start()
hgs
parents:
diff changeset
    88
{
hgs
parents:
diff changeset
    89
    DM_PRIVATE(BackgroundDownload);
hgs
parents:
diff changeset
    90
    if (priv->m_downloadMgrClient)
hgs
parents:
diff changeset
    91
        return priv->m_downloadMgrClient->startDownload(priv->m_downloadId);
hgs
parents:
diff changeset
    92
    return -1;
hgs
parents:
diff changeset
    93
}
hgs
parents:
diff changeset
    94
hgs
parents:
diff changeset
    95
/*!
hgs
parents:
diff changeset
    96
  returns id of the background download
hgs
parents:
diff changeset
    97
*/
hgs
parents:
diff changeset
    98
int BackgroundDownload::id()
hgs
parents:
diff changeset
    99
{
hgs
parents:
diff changeset
   100
    DM_PRIVATE(BackgroundDownload);
hgs
parents:
diff changeset
   101
    return priv->m_downloadId;
hgs
parents:
diff changeset
   102
}
hgs
parents:
diff changeset
   103
hgs
parents:
diff changeset
   104
/*!
hgs
parents:
diff changeset
   105
  sets the attribute for the background download
hgs
parents:
diff changeset
   106
  \a attr indicates attribute
hgs
parents:
diff changeset
   107
  \a value indicates value for the download
hgs
parents:
diff changeset
   108
*/
hgs
parents:
diff changeset
   109
int BackgroundDownload::setAttribute(DownloadAttribute attr, const QVariant& value)
hgs
parents:
diff changeset
   110
{
hgs
parents:
diff changeset
   111
    DM_PRIVATE(BackgroundDownload);
hgs
parents:
diff changeset
   112
    if (priv->m_downloadMgrClient)
hgs
parents:
diff changeset
   113
        return priv->m_downloadMgrClient->setDownloadAttribute(priv->m_downloadId, attr, value);
hgs
parents:
diff changeset
   114
    return -1;
hgs
parents:
diff changeset
   115
}
hgs
parents:
diff changeset
   116
hgs
parents:
diff changeset
   117
/*!
hgs
parents:
diff changeset
   118
  fetches the attribute of the background download
hgs
parents:
diff changeset
   119
  \a attr indicates download attribute
hgs
parents:
diff changeset
   120
*/
hgs
parents:
diff changeset
   121
QVariant BackgroundDownload::getAttribute(DownloadAttribute attr)
hgs
parents:
diff changeset
   122
{
hgs
parents:
diff changeset
   123
    DM_PRIVATE(BackgroundDownload);
hgs
parents:
diff changeset
   124
    if (priv->m_downloadMgrClient)
hgs
parents:
diff changeset
   125
        return priv->m_downloadMgrClient->getDownloadAttribute(priv->m_downloadId, attr);
hgs
parents:
diff changeset
   126
    return QVariant();
hgs
parents:
diff changeset
   127
}
hgs
parents:
diff changeset
   128
hgs
parents:
diff changeset
   129
/*!
hgs
parents:
diff changeset
   130
  pauses the background download
hgs
parents:
diff changeset
   131
*/
hgs
parents:
diff changeset
   132
int BackgroundDownload::pause()
hgs
parents:
diff changeset
   133
{
hgs
parents:
diff changeset
   134
    DM_PRIVATE(BackgroundDownload);
hgs
parents:
diff changeset
   135
    if (priv->m_downloadMgrClient)
hgs
parents:
diff changeset
   136
        return priv->m_downloadMgrClient->pauseDownload(priv->m_downloadId);
hgs
parents:
diff changeset
   137
    return -1;
hgs
parents:
diff changeset
   138
}
hgs
parents:
diff changeset
   139
hgs
parents:
diff changeset
   140
/*!
hgs
parents:
diff changeset
   141
  resumes the background download
hgs
parents:
diff changeset
   142
*/
hgs
parents:
diff changeset
   143
int BackgroundDownload::resume()
hgs
parents:
diff changeset
   144
{
hgs
parents:
diff changeset
   145
    DM_PRIVATE(BackgroundDownload);
hgs
parents:
diff changeset
   146
    if (priv->m_downloadMgrClient)
hgs
parents:
diff changeset
   147
        return priv->m_downloadMgrClient->resumeDownload(priv->m_downloadId);
hgs
parents:
diff changeset
   148
    return -1;
hgs
parents:
diff changeset
   149
}
hgs
parents:
diff changeset
   150
hgs
parents:
diff changeset
   151
/*!
hgs
parents:
diff changeset
   152
  cancels the background download
hgs
parents:
diff changeset
   153
*/
hgs
parents:
diff changeset
   154
int BackgroundDownload::cancel()
hgs
parents:
diff changeset
   155
{
hgs
parents:
diff changeset
   156
    DM_PRIVATE(BackgroundDownload);
hgs
parents:
diff changeset
   157
    if (priv->m_downloadMgrClient)
hgs
parents:
diff changeset
   158
        return priv->m_downloadMgrClient->cancelDownload(priv->m_downloadId);
hgs
parents:
diff changeset
   159
    return -1;
hgs
parents:
diff changeset
   160
}
hgs
parents:
diff changeset
   161
hgs
parents:
diff changeset
   162
/*!
hgs
parents:
diff changeset
   163
  registers receiver for the background download events
hgs
parents:
diff changeset
   164
  \a reciever indicates reciever which listen to download events
hgs
parents:
diff changeset
   165
*/
hgs
parents:
diff changeset
   166
void BackgroundDownload::registerEventReceiver(QObject *receiver)
hgs
parents:
diff changeset
   167
{
hgs
parents:
diff changeset
   168
    DM_PRIVATE(BackgroundDownload);
hgs
parents:
diff changeset
   169
    if (receiver)
hgs
parents:
diff changeset
   170
       if (!priv->m_eventReceiverList.contains(receiver))
hgs
parents:
diff changeset
   171
            priv->m_eventReceiverList.append(receiver);
hgs
parents:
diff changeset
   172
}
hgs
parents:
diff changeset
   173
hgs
parents:
diff changeset
   174
/*!
hgs
parents:
diff changeset
   175
  unregisters the event listener
hgs
parents:
diff changeset
   176
  \a receiver indicates listener which will be unregistered
hgs
parents:
diff changeset
   177
*/
hgs
parents:
diff changeset
   178
void BackgroundDownload::unregisterEventReceiver(QObject *receiver)
hgs
parents:
diff changeset
   179
{
hgs
parents:
diff changeset
   180
    DM_PRIVATE(BackgroundDownload);
hgs
parents:
diff changeset
   181
    priv->m_eventReceiverList.removeOne(receiver);
hgs
parents:
diff changeset
   182
}
hgs
parents:
diff changeset
   183
hgs
parents:
diff changeset
   184
/*!
hgs
parents:
diff changeset
   185
  returns download manager
hgs
parents:
diff changeset
   186
*/
hgs
parents:
diff changeset
   187
DownloadManager* BackgroundDownload::downloadManager()
hgs
parents:
diff changeset
   188
{
hgs
parents:
diff changeset
   189
    DM_PRIVATE(BackgroundDownload);
hgs
parents:
diff changeset
   190
    return priv->m_downloadManager;
hgs
parents:
diff changeset
   191
}
hgs
parents:
diff changeset
   192
hgs
parents:
diff changeset
   193
/*!
hgs
parents:
diff changeset
   194
  returns the child downloads i.e if download has any media objects
hgs
parents:
diff changeset
   195
  \a list indicates list of child downloads
hgs
parents:
diff changeset
   196
*/
hgs
parents:
diff changeset
   197
void BackgroundDownload::getChildren(QList<Download*>& /*list*/)
hgs
parents:
diff changeset
   198
{
hgs
parents:
diff changeset
   199
    // This feature is not supported at present
hgs
parents:
diff changeset
   200
    return;
hgs
parents:
diff changeset
   201
}
hgs
parents:
diff changeset
   202
hgs
parents:
diff changeset
   203
// posts the event to recievers event loop
hgs
parents:
diff changeset
   204
void BackgroundDownload::postEvent(DEventType type, DlEventAttributeMap* attrMap)
hgs
parents:
diff changeset
   205
{
hgs
parents:
diff changeset
   206
    DM_PRIVATE(BackgroundDownload);
hgs
parents:
diff changeset
   207
    if((type == Failed) || (type == Completed) || (type == Cancelled)) {
hgs
parents:
diff changeset
   208
        if (!(type == Completed && InActive == priv->m_downloadManager->getAttribute(DlMgrPersistantMode)))
hgs
parents:
diff changeset
   209
            removeDownloadInfo();
hgs
parents:
diff changeset
   210
    }
hgs
parents:
diff changeset
   211
    EventReceiverList list = eventReceivers();
hgs
parents:
diff changeset
   212
    for (int i=0; i<list.size(); i++) {
hgs
parents:
diff changeset
   213
        if (list[i]) {
hgs
parents:
diff changeset
   214
            DownloadEvent *event = new DownloadEvent(type, attrMap, priv->m_downloadId);
hgs
parents:
diff changeset
   215
            QCoreApplication::postEvent(list[i], event);
hgs
parents:
diff changeset
   216
        }
hgs
parents:
diff changeset
   217
    }
hgs
parents:
diff changeset
   218
}
hgs
parents:
diff changeset
   219
hgs
parents:
diff changeset
   220
// returns the event listeners
hgs
parents:
diff changeset
   221
EventReceiverList& BackgroundDownload::eventReceivers()
hgs
parents:
diff changeset
   222
{
hgs
parents:
diff changeset
   223
    DM_PRIVATE(BackgroundDownload);
hgs
parents:
diff changeset
   224
    return priv->m_eventReceiverList;
hgs
parents:
diff changeset
   225
}
hgs
parents:
diff changeset
   226
hgs
parents:
diff changeset
   227
// remove info of this download
hgs
parents:
diff changeset
   228
void BackgroundDownload::removeDownloadInfo()
hgs
parents:
diff changeset
   229
{
hgs
parents:
diff changeset
   230
    DM_PRIVATE(BackgroundDownload);
hgs
parents:
diff changeset
   231
    priv->m_dlInfo->remove(priv->m_downloadId);
hgs
parents:
diff changeset
   232
}