ginebra2/Downloads.cpp
changeset 3 0954f5dd2cd0
parent 0 1450b09d0cfd
equal deleted inserted replaced
1:b0dd75e285d2 3:0954f5dd2cd0
     1 /*
     1 /*
     2 * Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
     2 * Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
     3 * All rights reserved.
     3 * All rights reserved.
     4 * This component and the accompanying materials are made available
     4 *
     5 * under the terms of "Eclipse Public License v1.0"
     5 * This program is free software: you can redistribute it and/or modify
     6 * which accompanies this distribution, and is available
     6 * it under the terms of the GNU Lesser General Public License as published by
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
     7 * the Free Software Foundation, version 2.1 of the License.
     8 *
     8 *
     9 * Initial Contributors:
     9 * This program is distributed in the hope that it will be useful,
    10 * Nokia Corporation - initial contribution.
    10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
    11 *
    11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    12 * Contributors:
    12 * GNU Lesser General Public License for more details.
    13 *
    13 *
    14 * Description: 
    14 * You should have received a copy of the GNU Lesser General Public License
       
    15 * along with this program.  If not,
       
    16 * see "http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html/".
       
    17 *
       
    18 * Description:
    15 *
    19 *
    16 */
    20 */
    17 
       
    18 
    21 
    19 #include "Downloads.h"
    22 #include "Downloads.h"
    20 #include "Utilities.h"
    23 #include "Utilities.h"
    21 
    24 
    22 #include <QtDebug>
    25 #include <QtDebug>
       
    26 #include <QDesktopServices>
       
    27 #include <QDir>
       
    28 #include <QFileDialog>
       
    29 #include <QFileInfo>
    23 #include <QNetworkAccessManager>
    30 #include <QNetworkAccessManager>
    24 #include <QNetworkProxy>
    31 #include <QNetworkProxy>
    25 #include <QWebPage>
    32 #include <QWebPage>
    26 
    33 
    27 #include "bedrockprovisioning.h"
    34 #include "bedrockprovisioning.h"
    28 #include "downloadcontroller.h"
    35 #include "downloadcontroller.h"
    29 #include "download.h"
    36 #include "downloadproxy.h"
    30 #include "downloadmanager.h"
       
    31 
    37 
    32 namespace GVA {
    38 namespace GVA {
    33 
    39 
    34 Downloads::Downloads()
    40 Downloads::Downloads()
    35 : m_downloadController(0)
    41 : m_downloadController(0)
    37     setObjectName("downloads");
    43     setObjectName("downloads");
    38 }
    44 }
    39 
    45 
    40 Downloads::~Downloads()
    46 Downloads::~Downloads()
    41 {
    47 {
       
    48     delete m_downloadController;
    42 }
    49 }
    43 
    50 
    44 void Downloads::handlePage(QWebPage * page)
    51 void Downloads::handlePage(QWebPage * page)
    45 {
    52 {
    46     if (m_downloadController == 0) {
    53     if (m_downloadController == 0) {
    49         QNetworkAccessManager * accessManager = page->networkAccessManager();
    56         QNetworkAccessManager * accessManager = page->networkAccessManager();
    50         QNetworkProxy proxy = accessManager->proxy();
    57         QNetworkProxy proxy = accessManager->proxy();
    51 
    58 
    52         m_downloadController = new DownloadController(client, proxy);
    59         m_downloadController = new DownloadController(client, proxy);
    53 
    60 
    54         safe_connect(m_downloadController, SIGNAL(downloadCreated(Download *)),
    61         safe_connect(m_downloadController, SIGNAL(downloadCreated(DownloadProxy)),
    55                 this, SLOT(reportDownloadCreated(Download *)));
    62                 this, SLOT(reportDownloadCreated(DownloadProxy)));
    56 
    63 
    57         safe_connect(m_downloadController, SIGNAL(downloadStarted(Download *)),
    64         safe_connect(m_downloadController, SIGNAL(downloadStarted(DownloadProxy)),
    58                 this, SLOT(reportDownloadStarted(Download *)));
    65                 this, SLOT(reportDownloadStarted(DownloadProxy)));
    59 
    66 
    60         safe_connect(m_downloadController, SIGNAL(downloadFinished(Download *)),
    67         safe_connect(m_downloadController, SIGNAL(downloadFinished(DownloadProxy)),
    61                 this, SLOT(reportDownloadSuccess(Download *)));
    68                 this, SLOT(reportDownloadSuccess(DownloadProxy)));
    62 
    69 
    63         safe_connect(m_downloadController, SIGNAL(downloadFailed(Download *, const QString &)),
    70         safe_connect(m_downloadController, SIGNAL(downloadFailed(DownloadProxy, const QString &)),
    64                 this, SLOT(reportDownloadFailure(Download *, const QString &)));
    71                 this, SLOT(reportDownloadFailure(DownloadProxy, const QString &)));
    65 
    72 
    66         safe_connect(m_downloadController, SIGNAL(downloadPaused(Download *, const QString &)),
    73         safe_connect(m_downloadController, SIGNAL(downloadPaused(DownloadProxy, const QString &)),
    67                 this, SLOT(reportDownloadFailure(Download *, const QString &)));
    74                 this, SLOT(reportDownloadFailure(DownloadProxy, const QString &)));
    68 
    75 
    69         safe_connect(m_downloadController, SIGNAL(downloadCancelled(Download *, const QString &)),
    76         safe_connect(m_downloadController, SIGNAL(downloadCancelled(DownloadProxy, const QString &)),
    70                 this, SLOT(reportDownloadFailure(Download *, const QString &)));
    77                 this, SLOT(reportDownloadFailure(DownloadProxy, const QString &)));
    71 
    78 
    72         safe_connect(m_downloadController, SIGNAL(downloadNetworkLoss(Download *, const QString &)),
    79         safe_connect(m_downloadController, SIGNAL(downloadNetworkLoss(DownloadProxy, const QString &)),
    73                 this, SLOT(reportDownloadFailure(Download *, const QString &)));
    80                 this, SLOT(reportDownloadFailure(DownloadProxy, const QString &)));
    74 
    81 
    75         // There is no Download* argument to extract data from so we may as well
    82         // There is no argument to extract data from so we may as well
    76         // just connect signal to signal without going through another slot.
    83         // just connect signal to signal without going through another slot.
    77         safe_connect(m_downloadController, SIGNAL(downloadsCleared()),
    84         safe_connect(m_downloadController, SIGNAL(downloadsCleared()),
    78                 this, SIGNAL(downloadsCleared()));
    85                 this, SIGNAL(downloadsCleared()));
       
    86 
       
    87         safe_connect(m_downloadController, SIGNAL(unsupportedDownload(const QUrl &)),
       
    88                 this, SLOT(reportUnsupportedDownload(const QUrl &)));
    79     }
    89     }
    80 
    90 
    81     m_downloadController->handlePage(page);
    91     m_downloadController->handlePage(page);
    82 }
    92 }
    83 
    93 
    84 void Downloads::reportDownloadCreated(Download * download)
    94 static QString imageFileName(const QUrl & url)
    85 {
    95 {
    86     // Localize dialog message.
    96     QFileInfo info(url.path());
    87 
    97 
    88     QString fmt = qtTrId("fmt_browser_downloading_file");
    98     QString fileName = info.fileName();
    89     QString msg = fmt.arg(download->getAttribute(DlFileName).toString());
    99 
       
   100     if (fileName.isEmpty()) {
       
   101         return "image"; // ;;; localize?
       
   102     }
       
   103 
       
   104     return fileName;
       
   105 }
       
   106 
       
   107 static bool getSaveFileForImage(const QUrl & url, QFileInfo & saveInfo)
       
   108 {
       
   109     QDir defaultDir = QDesktopServices::storageLocation(QDesktopServices::PicturesLocation);
       
   110 
       
   111     QString defaultFile = imageFileName(url);
       
   112 
       
   113     QString saveFile = QFileDialog::getSaveFileName(
       
   114             0,          // parent
       
   115             QString(),  // caption (doesn't show on Symbian)
       
   116             defaultDir.filePath(defaultFile),
       
   117             QString(),  // filter
       
   118             0,          // selected filter
       
   119             QFileDialog::DontConfirmOverwrite);
       
   120 
       
   121     if (saveFile.isEmpty()) {
       
   122         return false;
       
   123     }
       
   124 
       
   125     saveInfo.setFile(saveFile);
       
   126     return true;
       
   127 }
       
   128 
       
   129 void Downloads::downloadImage(const QString & imageUrl)
       
   130 {
       
   131     QUrl url(imageUrl);
       
   132 
       
   133     QFileInfo saveInfo;
       
   134 
       
   135     if (!getSaveFileForImage(url, saveInfo)) {
       
   136         return;
       
   137     }
       
   138 
       
   139     m_downloadController->startDownload(url, saveInfo);
       
   140 }
       
   141 
       
   142 void Downloads::reportDownloadCreated(DownloadProxy downloadProxy)
       
   143 {
       
   144     // Localize dialog message.
       
   145 
       
   146     QString fmt = qtTrId("txt_browser_downloading_file");
       
   147     QString msg = fmt.arg(downloadProxy.fileName());
    90 
   148 
    91     emit downloadCreated(msg);
   149     emit downloadCreated(msg);
    92 }
   150 }
    93 
   151 
    94 void Downloads::reportDownloadStarted(Download * download)
   152 void Downloads::reportDownloadStarted(DownloadProxy downloadProxy)
    95 {
   153 {
    96     // Localize dialog message.
   154     // Localize dialog message.
    97 
   155 
    98     QString fmt = qtTrId("fmt_browser_downloading_file");
   156     QString fmt = qtTrId("txt_browser_downloading_file");
    99     QString msg = fmt.arg(download->getAttribute(DlFileName).toString());
   157     QString msg = fmt.arg(downloadProxy.fileName());
   100 
   158 
   101     emit downloadCreated(msg);
   159     emit downloadCreated(msg);
   102 }
   160 }
   103 
   161 
   104 void Downloads::reportDownloadSuccess(Download * download)
   162 void Downloads::reportDownloadSuccess(DownloadProxy downloadProxy)
   105 {
   163 {
   106     // Localize dialog message.
   164     // Localize dialog message.
   107 
   165 
   108     QString fmt = qtTrId("fmt_browser_file_has_finished_downloading");
   166     QString fmt = qtTrId("txt_browser_file_has_finished_downloading");
   109     QString msg = fmt.arg(download->getAttribute(DlFileName).toString());
   167     QString msg = fmt.arg(downloadProxy.fileName());
   110 
   168 
   111     emit downloadSuccess(msg);
   169     emit downloadSuccess(msg);
   112 
   170 
   113     // Don't forget to remove the download; otherwise the download
   171     // Don't forget to remove the download; otherwise the download
   114     // controller won't know to emit the DownloadsCleared signal.
   172     // controller won't know to emit the DownloadsCleared signal.
   115 
   173 
   116     DownloadManager * manager = download->downloadManager();
   174     downloadProxy.remove();
   117 
   175 }
   118     manager->removeOne(download);
   176 
   119 }
   177 void Downloads::reportDownloadFailure(DownloadProxy downloadProxy, const QString & error)
   120 
       
   121 void Downloads::reportDownloadFailure(Download * download, const QString & error)
       
   122 {
   178 {
   123     // What went wrong?
   179     // What went wrong?
   124 
   180 
   125     DownloadController::debugDownload(download);
   181     downloadProxy.debug();
   126 
   182 
   127     // Localize dialog message.
   183     // Localize dialog message.
   128 
   184 
   129     QString fmt = qtTrId("fmt_browser_tag_error_tag_file_could_not_be_downloaded");
   185     QString fmt = qtTrId("txt_browser_tag_error_tag_file_could_not_be_downloaded");
   130     QString msg = fmt.arg(
   186     QString msg = fmt.arg(
   131             "<span style=\"color:red\">",
   187             "<span style=\"color:red\">",
   132             "</span>",
   188             "</span>",
   133             download->getAttribute(DlFileName).toString());
   189             downloadProxy.fileName());
   134 
   190 
   135     emit downloadFailure(msg);
   191     emit downloadFailure(msg);
   136 
   192 
   137     // Don't forget to remove the download; otherwise the download
   193     // Don't forget to remove the download; otherwise the download
   138     // controller won't know to emit the DownloadsCleared signal.
   194     // controller won't know to emit the DownloadsCleared signal.
   139 
   195 
   140     DownloadManager * manager = download->downloadManager();
   196     downloadProxy.remove();
   141 
   197 }
   142     manager->removeOne(download);
   198 
       
   199 void Downloads::reportUnsupportedDownload(const QUrl & url)
       
   200 {
       
   201     emit unsupportedDownload("Unsupported content"); // ;;; localize? or not b/c this is temporary?
   143 }
   202 }
   144 
   203 
   145 } // namespace GVA
   204 } // namespace GVA