utilities/downloadmanager/src/paralleldownloadmanager.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 "downloadmanager.h"
hgs
parents:
diff changeset
    18
#include "download.h"
hgs
parents:
diff changeset
    19
#include "paralleldownloadmanager.h"
hgs
parents:
diff changeset
    20
#include "clientdownload.h"
hgs
parents:
diff changeset
    21
hgs
parents:
diff changeset
    22
class ParallelDownloadManagerPrivate
hgs
parents:
diff changeset
    23
{
hgs
parents:
diff changeset
    24
    //declare public implementation
hgs
parents:
diff changeset
    25
    DM_DECLARE_PUBLIC(ParallelDownloadManager);
hgs
parents:
diff changeset
    26
public:
hgs
parents:
diff changeset
    27
    ParallelDownloadManagerPrivate();
hgs
parents:
diff changeset
    28
    ~ParallelDownloadManagerPrivate();
hgs
parents:
diff changeset
    29
   
hgs
parents:
diff changeset
    30
    QList<Download*> m_downloads; // list of downloads
hgs
parents:
diff changeset
    31
};
hgs
parents:
diff changeset
    32
hgs
parents:
diff changeset
    33
ParallelDownloadManagerPrivate::ParallelDownloadManagerPrivate()
hgs
parents:
diff changeset
    34
{ }
hgs
parents:
diff changeset
    35
hgs
parents:
diff changeset
    36
ParallelDownloadManagerPrivate::~ParallelDownloadManagerPrivate()
hgs
parents:
diff changeset
    37
{
hgs
parents:
diff changeset
    38
    int size = m_downloads.size();
hgs
parents:
diff changeset
    39
    for(int i=0; i<size; i++) {
hgs
parents:
diff changeset
    40
        DownloadState state = (DownloadState)((m_downloads[i]->getAttribute(DlDownloadState)).toInt());
hgs
parents:
diff changeset
    41
        if(state == DlInprogress) {
hgs
parents:
diff changeset
    42
            // pause the download if it is in progress
hgs
parents:
diff changeset
    43
            m_downloads[i]->pause();
hgs
parents:
diff changeset
    44
        } else if(state == DlFailed) {
hgs
parents:
diff changeset
    45
            // cancel the download if it has failed
hgs
parents:
diff changeset
    46
            m_downloads[i]->cancel();
hgs
parents:
diff changeset
    47
        }
hgs
parents:
diff changeset
    48
        delete (m_downloads[i]);
hgs
parents:
diff changeset
    49
    }
hgs
parents:
diff changeset
    50
    m_downloads.clear();
hgs
parents:
diff changeset
    51
}
hgs
parents:
diff changeset
    52
hgs
parents:
diff changeset
    53
/*
hgs
parents:
diff changeset
    54
  constructor
hgs
parents:
diff changeset
    55
*/
hgs
parents:
diff changeset
    56
ParallelDownloadManager::ParallelDownloadManager()
hgs
parents:
diff changeset
    57
{
hgs
parents:
diff changeset
    58
    DM_INITIALIZE(ParallelDownloadManager);
hgs
parents:
diff changeset
    59
}
hgs
parents:
diff changeset
    60
hgs
parents:
diff changeset
    61
/*
hgs
parents:
diff changeset
    62
  destructor
hgs
parents:
diff changeset
    63
*/
hgs
parents:
diff changeset
    64
ParallelDownloadManager::~ParallelDownloadManager()
hgs
parents:
diff changeset
    65
{
hgs
parents:
diff changeset
    66
    DM_UNINITIALIZE(ParallelDownloadManager);
hgs
parents:
diff changeset
    67
}
hgs
parents:
diff changeset
    68
hgs
parents:
diff changeset
    69
/*
hgs
parents:
diff changeset
    70
  returns all the current downloads
hgs
parents:
diff changeset
    71
*/
hgs
parents:
diff changeset
    72
QList<Download*>& ParallelDownloadManager::currentDownloads()
hgs
parents:
diff changeset
    73
{
hgs
parents:
diff changeset
    74
    DM_PRIVATE(ParallelDownloadManager);
hgs
parents:
diff changeset
    75
    return priv->m_downloads;
hgs
parents:
diff changeset
    76
}
hgs
parents:
diff changeset
    77
hgs
parents:
diff changeset
    78
/*
hgs
parents:
diff changeset
    79
  finds a download provided id if exists
hgs
parents:
diff changeset
    80
  \a id indicates identifier for download
hgs
parents:
diff changeset
    81
*/
hgs
parents:
diff changeset
    82
Download* ParallelDownloadManager::findDownload(int dlId)
hgs
parents:
diff changeset
    83
{
hgs
parents:
diff changeset
    84
    DM_PRIVATE(ParallelDownloadManager);
hgs
parents:
diff changeset
    85
    for (int i = 0; i < priv->m_downloads.size(); ++i) {
hgs
parents:
diff changeset
    86
        if (priv->m_downloads[i]->id() == dlId)
hgs
parents:
diff changeset
    87
            return priv->m_downloads[i];
hgs
parents:
diff changeset
    88
    }
hgs
parents:
diff changeset
    89
    return 0;
hgs
parents:
diff changeset
    90
}
hgs
parents:
diff changeset
    91
hgs
parents:
diff changeset
    92
/*
hgs
parents:
diff changeset
    93
  cancels all the downloads
hgs
parents:
diff changeset
    94
*/
hgs
parents:
diff changeset
    95
void ParallelDownloadManager::removeAll()
hgs
parents:
diff changeset
    96
{
hgs
parents:
diff changeset
    97
    DM_PRIVATE(ParallelDownloadManager);
hgs
parents:
diff changeset
    98
    int count = priv->m_downloads.count();
hgs
parents:
diff changeset
    99
    //traverse the list and delete each download in the list
hgs
parents:
diff changeset
   100
    for(int i=0; i<count; i++) {
hgs
parents:
diff changeset
   101
        // cancel the download
hgs
parents:
diff changeset
   102
        priv->m_downloads[i]->cancel();
hgs
parents:
diff changeset
   103
        priv->m_downloads[i]->deleteLater();
hgs
parents:
diff changeset
   104
    }
hgs
parents:
diff changeset
   105
    priv->m_downloads.clear();
hgs
parents:
diff changeset
   106
}
hgs
parents:
diff changeset
   107
hgs
parents:
diff changeset
   108
/*
hgs
parents:
diff changeset
   109
  cancels and removes the download
hgs
parents:
diff changeset
   110
  \a dl indicates the download to be canceled and removed
hgs
parents:
diff changeset
   111
*/
hgs
parents:
diff changeset
   112
void ParallelDownloadManager::removeOne(Download *dl)
hgs
parents:
diff changeset
   113
{
hgs
parents:
diff changeset
   114
    if(!dl)
hgs
parents:
diff changeset
   115
        return;
hgs
parents:
diff changeset
   116
    DM_PRIVATE(ParallelDownloadManager);
hgs
parents:
diff changeset
   117
    if (priv->m_downloads.contains(dl)) {
hgs
parents:
diff changeset
   118
        dl->cancel();
hgs
parents:
diff changeset
   119
        priv->m_downloads.removeOne(dl);
hgs
parents:
diff changeset
   120
        delete dl;
hgs
parents:
diff changeset
   121
        dl = 0;
hgs
parents:
diff changeset
   122
    }
hgs
parents:
diff changeset
   123
}
hgs
parents:
diff changeset
   124
hgs
parents:
diff changeset
   125
/*
hgs
parents:
diff changeset
   126
  pauses all the downloads which are in progress
hgs
parents:
diff changeset
   127
*/
hgs
parents:
diff changeset
   128
void ParallelDownloadManager::pauseAll()
hgs
parents:
diff changeset
   129
{
hgs
parents:
diff changeset
   130
    DM_PRIVATE(ParallelDownloadManager);
hgs
parents:
diff changeset
   131
    int count = priv->m_downloads.size();
hgs
parents:
diff changeset
   132
    //traverse the list and pause downloads which are on-going
hgs
parents:
diff changeset
   133
    for(int i=0; i<count; i++) {
hgs
parents:
diff changeset
   134
        DownloadState state = (DownloadState)((priv->m_downloads[i]->getAttribute(DlDownloadState)).toInt());
hgs
parents:
diff changeset
   135
        if((state == DlInprogress) || (state == DlStarted))
hgs
parents:
diff changeset
   136
            priv->m_downloads[i]->pause();
hgs
parents:
diff changeset
   137
    }    
hgs
parents:
diff changeset
   138
}
hgs
parents:
diff changeset
   139
hgs
parents:
diff changeset
   140
/*
hgs
parents:
diff changeset
   141
  resumes all the downloads which are paused
hgs
parents:
diff changeset
   142
*/
hgs
parents:
diff changeset
   143
void ParallelDownloadManager::resumeAll()
hgs
parents:
diff changeset
   144
{
hgs
parents:
diff changeset
   145
    DM_PRIVATE(ParallelDownloadManager);
hgs
parents:
diff changeset
   146
    int count = priv->m_downloads.size();
hgs
parents:
diff changeset
   147
    //traverse the list and resume downloads which are paused
hgs
parents:
diff changeset
   148
    for(int i=0; i<count; i++) {
hgs
parents:
diff changeset
   149
        DownloadState state = (DownloadState)((priv->m_downloads[i]->getAttribute(DlDownloadState)).toInt());
hgs
parents:
diff changeset
   150
        if((state == DlPaused) || (state == DlCancelled)) {
hgs
parents:
diff changeset
   151
            // note that in case of cancelled downloads, it will start from begining
hgs
parents:
diff changeset
   152
            priv->m_downloads[i]->resume();
hgs
parents:
diff changeset
   153
        }              
hgs
parents:
diff changeset
   154
    }
hgs
parents:
diff changeset
   155
}
hgs
parents:
diff changeset
   156
hgs
parents:
diff changeset
   157
void ParallelDownloadManager::addToParallelDownload(Download* dl)
hgs
parents:
diff changeset
   158
{
hgs
parents:
diff changeset
   159
    DM_PRIVATE(ParallelDownloadManager);
hgs
parents:
diff changeset
   160
    // set the download type
hgs
parents:
diff changeset
   161
    ClientDownload* download;
hgs
parents:
diff changeset
   162
    download = dynamic_cast<ClientDownload*>(dl);
hgs
parents:
diff changeset
   163
    if(download)
hgs
parents:
diff changeset
   164
        download->attributes().insert(DlDownloadType, (int)Parallel);
hgs
parents:
diff changeset
   165
    priv->m_downloads.append(dl);
hgs
parents:
diff changeset
   166
}