ginebra2/Downloads.cpp
changeset 3 0954f5dd2cd0
parent 0 1450b09d0cfd
--- a/ginebra2/Downloads.cpp	Fri May 14 15:40:36 2010 +0300
+++ b/ginebra2/Downloads.cpp	Tue Jun 29 00:46:29 2010 -0400
@@ -1,33 +1,39 @@
 /*
 * Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
 * All rights reserved.
-* This component and the accompanying materials are made available
-* under the terms of "Eclipse Public License v1.0"
-* which accompanies this distribution, and is available
-* at the URL "http://www.eclipse.org/legal/epl-v10.html".
+*
+* This program is free software: you can redistribute it and/or modify
+* it under the terms of the GNU Lesser General Public License as published by
+* the Free Software Foundation, version 2.1 of the License.
 *
-* Initial Contributors:
-* Nokia Corporation - initial contribution.
+* This program is distributed in the hope that it will be useful,
+* but WITHOUT ANY WARRANTY; without even the implied warranty of
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+* GNU Lesser General Public License for more details.
 *
-* Contributors:
+* You should have received a copy of the GNU Lesser General Public License
+* along with this program.  If not,
+* see "http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html/".
 *
-* Description: 
+* Description:
 *
 */
 
-
 #include "Downloads.h"
 #include "Utilities.h"
 
 #include <QtDebug>
+#include <QDesktopServices>
+#include <QDir>
+#include <QFileDialog>
+#include <QFileInfo>
 #include <QNetworkAccessManager>
 #include <QNetworkProxy>
 #include <QWebPage>
 
 #include "bedrockprovisioning.h"
 #include "downloadcontroller.h"
-#include "download.h"
-#include "downloadmanager.h"
+#include "downloadproxy.h"
 
 namespace GVA {
 
@@ -39,6 +45,7 @@
 
 Downloads::~Downloads()
 {
+    delete m_downloadController;
 }
 
 void Downloads::handlePage(QWebPage * page)
@@ -51,95 +58,147 @@
 
         m_downloadController = new DownloadController(client, proxy);
 
-        safe_connect(m_downloadController, SIGNAL(downloadCreated(Download *)),
-                this, SLOT(reportDownloadCreated(Download *)));
+        safe_connect(m_downloadController, SIGNAL(downloadCreated(DownloadProxy)),
+                this, SLOT(reportDownloadCreated(DownloadProxy)));
 
-        safe_connect(m_downloadController, SIGNAL(downloadStarted(Download *)),
-                this, SLOT(reportDownloadStarted(Download *)));
+        safe_connect(m_downloadController, SIGNAL(downloadStarted(DownloadProxy)),
+                this, SLOT(reportDownloadStarted(DownloadProxy)));
 
-        safe_connect(m_downloadController, SIGNAL(downloadFinished(Download *)),
-                this, SLOT(reportDownloadSuccess(Download *)));
+        safe_connect(m_downloadController, SIGNAL(downloadFinished(DownloadProxy)),
+                this, SLOT(reportDownloadSuccess(DownloadProxy)));
 
-        safe_connect(m_downloadController, SIGNAL(downloadFailed(Download *, const QString &)),
-                this, SLOT(reportDownloadFailure(Download *, const QString &)));
+        safe_connect(m_downloadController, SIGNAL(downloadFailed(DownloadProxy, const QString &)),
+                this, SLOT(reportDownloadFailure(DownloadProxy, const QString &)));
 
-        safe_connect(m_downloadController, SIGNAL(downloadPaused(Download *, const QString &)),
-                this, SLOT(reportDownloadFailure(Download *, const QString &)));
+        safe_connect(m_downloadController, SIGNAL(downloadPaused(DownloadProxy, const QString &)),
+                this, SLOT(reportDownloadFailure(DownloadProxy, const QString &)));
 
-        safe_connect(m_downloadController, SIGNAL(downloadCancelled(Download *, const QString &)),
-                this, SLOT(reportDownloadFailure(Download *, const QString &)));
+        safe_connect(m_downloadController, SIGNAL(downloadCancelled(DownloadProxy, const QString &)),
+                this, SLOT(reportDownloadFailure(DownloadProxy, const QString &)));
 
-        safe_connect(m_downloadController, SIGNAL(downloadNetworkLoss(Download *, const QString &)),
-                this, SLOT(reportDownloadFailure(Download *, const QString &)));
+        safe_connect(m_downloadController, SIGNAL(downloadNetworkLoss(DownloadProxy, const QString &)),
+                this, SLOT(reportDownloadFailure(DownloadProxy, const QString &)));
 
-        // There is no Download* argument to extract data from so we may as well
+        // There is no argument to extract data from so we may as well
         // just connect signal to signal without going through another slot.
         safe_connect(m_downloadController, SIGNAL(downloadsCleared()),
                 this, SIGNAL(downloadsCleared()));
+
+        safe_connect(m_downloadController, SIGNAL(unsupportedDownload(const QUrl &)),
+                this, SLOT(reportUnsupportedDownload(const QUrl &)));
     }
 
     m_downloadController->handlePage(page);
 }
 
-void Downloads::reportDownloadCreated(Download * download)
+static QString imageFileName(const QUrl & url)
+{
+    QFileInfo info(url.path());
+
+    QString fileName = info.fileName();
+
+    if (fileName.isEmpty()) {
+        return "image"; // ;;; localize?
+    }
+
+    return fileName;
+}
+
+static bool getSaveFileForImage(const QUrl & url, QFileInfo & saveInfo)
+{
+    QDir defaultDir = QDesktopServices::storageLocation(QDesktopServices::PicturesLocation);
+
+    QString defaultFile = imageFileName(url);
+
+    QString saveFile = QFileDialog::getSaveFileName(
+            0,          // parent
+            QString(),  // caption (doesn't show on Symbian)
+            defaultDir.filePath(defaultFile),
+            QString(),  // filter
+            0,          // selected filter
+            QFileDialog::DontConfirmOverwrite);
+
+    if (saveFile.isEmpty()) {
+        return false;
+    }
+
+    saveInfo.setFile(saveFile);
+    return true;
+}
+
+void Downloads::downloadImage(const QString & imageUrl)
+{
+    QUrl url(imageUrl);
+
+    QFileInfo saveInfo;
+
+    if (!getSaveFileForImage(url, saveInfo)) {
+        return;
+    }
+
+    m_downloadController->startDownload(url, saveInfo);
+}
+
+void Downloads::reportDownloadCreated(DownloadProxy downloadProxy)
 {
     // Localize dialog message.
 
-    QString fmt = qtTrId("fmt_browser_downloading_file");
-    QString msg = fmt.arg(download->getAttribute(DlFileName).toString());
+    QString fmt = qtTrId("txt_browser_downloading_file");
+    QString msg = fmt.arg(downloadProxy.fileName());
 
     emit downloadCreated(msg);
 }
 
-void Downloads::reportDownloadStarted(Download * download)
+void Downloads::reportDownloadStarted(DownloadProxy downloadProxy)
 {
     // Localize dialog message.
 
-    QString fmt = qtTrId("fmt_browser_downloading_file");
-    QString msg = fmt.arg(download->getAttribute(DlFileName).toString());
+    QString fmt = qtTrId("txt_browser_downloading_file");
+    QString msg = fmt.arg(downloadProxy.fileName());
 
     emit downloadCreated(msg);
 }
 
-void Downloads::reportDownloadSuccess(Download * download)
+void Downloads::reportDownloadSuccess(DownloadProxy downloadProxy)
 {
     // Localize dialog message.
 
-    QString fmt = qtTrId("fmt_browser_file_has_finished_downloading");
-    QString msg = fmt.arg(download->getAttribute(DlFileName).toString());
+    QString fmt = qtTrId("txt_browser_file_has_finished_downloading");
+    QString msg = fmt.arg(downloadProxy.fileName());
 
     emit downloadSuccess(msg);
 
     // Don't forget to remove the download; otherwise the download
     // controller won't know to emit the DownloadsCleared signal.
 
-    DownloadManager * manager = download->downloadManager();
-
-    manager->removeOne(download);
+    downloadProxy.remove();
 }
 
-void Downloads::reportDownloadFailure(Download * download, const QString & error)
+void Downloads::reportDownloadFailure(DownloadProxy downloadProxy, const QString & error)
 {
     // What went wrong?
 
-    DownloadController::debugDownload(download);
+    downloadProxy.debug();
 
     // Localize dialog message.
 
-    QString fmt = qtTrId("fmt_browser_tag_error_tag_file_could_not_be_downloaded");
+    QString fmt = qtTrId("txt_browser_tag_error_tag_file_could_not_be_downloaded");
     QString msg = fmt.arg(
             "<span style=\"color:red\">",
             "</span>",
-            download->getAttribute(DlFileName).toString());
+            downloadProxy.fileName());
 
     emit downloadFailure(msg);
 
     // Don't forget to remove the download; otherwise the download
     // controller won't know to emit the DownloadsCleared signal.
 
-    DownloadManager * manager = download->downloadManager();
+    downloadProxy.remove();
+}
 
-    manager->removeOne(download);
+void Downloads::reportUnsupportedDownload(const QUrl & url)
+{
+    emit unsupportedDownload("Unsupported content"); // ;;; localize? or not b/c this is temporary?
 }
 
 } // namespace GVA