utilities/downloadmanager/src/paralleldownloadmanager.cpp
changeset 16 3c88a81ff781
equal deleted inserted replaced
14:6aeb7a756187 16:3c88a81ff781
       
     1 /**
       
     2    This file is part of CWRT package **
       
     3 
       
     4    Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). **
       
     5 
       
     6    This program is free software: you can redistribute it and/or modify
       
     7    it under the terms of the GNU (Lesser) General Public License as 
       
     8    published by the Free Software Foundation, version 2.1 of the License. 
       
     9    This program is distributed in the hope that it will be useful, but
       
    10    WITHOUT ANY WARRANTY; without even the implied warranty of 
       
    11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 
       
    12    (Lesser) General Public License for more details. You should have 
       
    13    received a copy of the GNU (Lesser) General Public License along 
       
    14    with this program. If not, see <http://www.gnu.org/licenses/>.
       
    15 */
       
    16 
       
    17 #include "downloadmanager.h"
       
    18 #include "download.h"
       
    19 #include "paralleldownloadmanager.h"
       
    20 #include "clientdownload.h"
       
    21 
       
    22 class ParallelDownloadManagerPrivate
       
    23 {
       
    24     //declare public implementation
       
    25     DM_DECLARE_PUBLIC(ParallelDownloadManager);
       
    26 public:
       
    27     ParallelDownloadManagerPrivate();
       
    28     ~ParallelDownloadManagerPrivate();
       
    29    
       
    30     QList<Download*> m_downloads; // list of downloads
       
    31 };
       
    32 
       
    33 ParallelDownloadManagerPrivate::ParallelDownloadManagerPrivate()
       
    34 { }
       
    35 
       
    36 ParallelDownloadManagerPrivate::~ParallelDownloadManagerPrivate()
       
    37 {
       
    38     int size = m_downloads.size();
       
    39     for(int i=0; i<size; i++) {
       
    40         DownloadState state = (DownloadState)((m_downloads[i]->getAttribute(DlDownloadState)).toInt());
       
    41         if(state == DlInprogress) {
       
    42             // pause the download if it is in progress
       
    43             m_downloads[i]->pause();
       
    44         } else if(state == DlFailed) {
       
    45             // cancel the download if it has failed
       
    46             m_downloads[i]->cancel();
       
    47         }
       
    48         delete (m_downloads[i]);
       
    49     }
       
    50     m_downloads.clear();
       
    51 }
       
    52 
       
    53 /*
       
    54   constructor
       
    55 */
       
    56 ParallelDownloadManager::ParallelDownloadManager()
       
    57 {
       
    58     DM_INITIALIZE(ParallelDownloadManager);
       
    59 }
       
    60 
       
    61 /*
       
    62   destructor
       
    63 */
       
    64 ParallelDownloadManager::~ParallelDownloadManager()
       
    65 {
       
    66     DM_UNINITIALIZE(ParallelDownloadManager);
       
    67 }
       
    68 
       
    69 /*
       
    70   returns all the current downloads
       
    71 */
       
    72 QList<Download*>& ParallelDownloadManager::currentDownloads()
       
    73 {
       
    74     DM_PRIVATE(ParallelDownloadManager);
       
    75     return priv->m_downloads;
       
    76 }
       
    77 
       
    78 /*
       
    79   finds a download provided id if exists
       
    80   \a id indicates identifier for download
       
    81 */
       
    82 Download* ParallelDownloadManager::findDownload(int dlId)
       
    83 {
       
    84     DM_PRIVATE(ParallelDownloadManager);
       
    85     for (int i = 0; i < priv->m_downloads.size(); ++i) {
       
    86         if (priv->m_downloads[i]->id() == dlId)
       
    87             return priv->m_downloads[i];
       
    88     }
       
    89     return 0;
       
    90 }
       
    91 
       
    92 /*
       
    93   cancels all the downloads
       
    94 */
       
    95 void ParallelDownloadManager::removeAll()
       
    96 {
       
    97     DM_PRIVATE(ParallelDownloadManager);
       
    98     int count = priv->m_downloads.count();
       
    99     //traverse the list and delete each download in the list
       
   100     for(int i=0; i<count; i++) {
       
   101         // cancel the download
       
   102         priv->m_downloads[i]->cancel();
       
   103         priv->m_downloads[i]->deleteLater();
       
   104     }
       
   105     priv->m_downloads.clear();
       
   106 }
       
   107 
       
   108 /*
       
   109   cancels and removes the download
       
   110   \a dl indicates the download to be canceled and removed
       
   111 */
       
   112 void ParallelDownloadManager::removeOne(Download *dl)
       
   113 {
       
   114     if(!dl)
       
   115         return;
       
   116     DM_PRIVATE(ParallelDownloadManager);
       
   117     if (priv->m_downloads.contains(dl)) {
       
   118         dl->cancel();
       
   119         priv->m_downloads.removeOne(dl);
       
   120         delete dl;
       
   121         dl = 0;
       
   122     }
       
   123 }
       
   124 
       
   125 /*
       
   126   pauses all the downloads which are in progress
       
   127 */
       
   128 void ParallelDownloadManager::pauseAll()
       
   129 {
       
   130     DM_PRIVATE(ParallelDownloadManager);
       
   131     int count = priv->m_downloads.size();
       
   132     //traverse the list and pause downloads which are on-going
       
   133     for(int i=0; i<count; i++) {
       
   134         DownloadState state = (DownloadState)((priv->m_downloads[i]->getAttribute(DlDownloadState)).toInt());
       
   135         if((state == DlInprogress) || (state == DlStarted))
       
   136             priv->m_downloads[i]->pause();
       
   137     }    
       
   138 }
       
   139 
       
   140 /*
       
   141   resumes all the downloads which are paused
       
   142 */
       
   143 void ParallelDownloadManager::resumeAll()
       
   144 {
       
   145     DM_PRIVATE(ParallelDownloadManager);
       
   146     int count = priv->m_downloads.size();
       
   147     //traverse the list and resume downloads which are paused
       
   148     for(int i=0; i<count; i++) {
       
   149         DownloadState state = (DownloadState)((priv->m_downloads[i]->getAttribute(DlDownloadState)).toInt());
       
   150         if((state == DlPaused) || (state == DlCancelled)) {
       
   151             // note that in case of cancelled downloads, it will start from begining
       
   152             priv->m_downloads[i]->resume();
       
   153         }              
       
   154     }
       
   155 }
       
   156 
       
   157 void ParallelDownloadManager::addToParallelDownload(Download* dl)
       
   158 {
       
   159     DM_PRIVATE(ParallelDownloadManager);
       
   160     // set the download type
       
   161     ClientDownload* download;
       
   162     download = dynamic_cast<ClientDownload*>(dl);
       
   163     if(download)
       
   164         download->attributes().insert(DlDownloadType, (int)Parallel);
       
   165     priv->m_downloads.append(dl);
       
   166 }