From 22bc40e17d463ec817a9c9c1f461b4f7990c5450 Mon Sep 17 00:00:00 2001 From: Andrea Diamantini Date: Wed, 21 Jan 2009 02:03:30 +0100 Subject: Reimplemented download system based on KGet one. Thanks Lucas ;) Rekonq now has its one! --- src/CMakeLists.txt | 3 +- src/browserapplication.cpp | 34 ++----------------- src/browserapplication.h | 8 +---- src/download.cpp | 82 ++++++++++++++++++++++++++++++++++++++++++++++ src/download.h | 56 +++++++++++++++++++++++++++++++ src/mainwindow.cpp | 5 ++- src/webview.cpp | 10 ++++-- 7 files changed, 156 insertions(+), 42 deletions(-) create mode 100644 src/download.cpp create mode 100644 src/download.h diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 50fc2333..b18f6275 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -7,7 +7,8 @@ SET( rekonq_SRCS cookiejar.cpp edittableview.cpp edittreeview.cpp - history.cpp + history.cpp + download.cpp bookmarks.cpp modelmenu.cpp networkaccessmanager.cpp diff --git a/src/browserapplication.cpp b/src/browserapplication.cpp index 008917e1..a82b095c 100644 --- a/src/browserapplication.cpp +++ b/src/browserapplication.cpp @@ -30,6 +30,7 @@ #include "networkaccessmanager.h" #include "mainview.h" #include "webview.h" +#include "download.h" // KDE Includes #include @@ -147,41 +148,12 @@ void BrowserApplication::postLaunch() } -void BrowserApplication::downloadUrl(const KUrl &url) +void BrowserApplication::downloadUrl(const KUrl &srcUrl, const KUrl &destUrl) { -// QString path = ReKonfig::downloadDir() + QString("/") + url.fileName(); -// KIO::NetAccess::download( url , path , mainWindow() ); - m_downloadUrl = url; - KIO::TransferJob * job = KIO::get( m_downloadUrl, KIO::NoReload); - connect(job, SIGNAL( result(KJob*) ), this, SLOT( slotResult(KJob*) ) ); - connect(job, SIGNAL(data(KIO::Job*,const QByteArray &)), this, SLOT(slotData(KIO::Job*, const QByteArray&))); + new Download( srcUrl, destUrl ); } -void BrowserApplication::slotResult(KJob* job) -{ - if ( job->error() ) - { - kDebug() << job->errorString(); - } - else - { - QString path = ReKonfig::downloadDir() + QString("/") + m_downloadUrl.fileName(); - QFile destFile( path ); - destFile.write(m_downloadData); - destFile.close(); - m_downloadData = 0; - } -} - - -void BrowserApplication::slotData(KIO::Job*, const QByteArray& data) -{ - m_downloadData.append(data); -} - - - QList BrowserApplication::mainWindows() { clean(); diff --git a/src/browserapplication.h b/src/browserapplication.h index 7b8b98bc..9b47bfcc 100644 --- a/src/browserapplication.h +++ b/src/browserapplication.h @@ -56,7 +56,7 @@ public: MainWindow *mainWindow(); QList mainWindows(); KIcon icon(const KUrl &url) const; - void downloadUrl(const KUrl &url); + void downloadUrl(const KUrl &srcUrl, const KUrl &destUrl); void saveSession(); bool canRestoreSession() const; @@ -73,8 +73,6 @@ private slots: void postLaunch(); void openUrl(const KUrl &url); void newLocalSocketConnection(); - void slotResult(KJob*); - void slotData(KIO::Job*, const QByteArray&); private: void clean(); @@ -86,10 +84,6 @@ private: QLocalServer *m_localServer; QByteArray m_lastSession; mutable KIcon m_defaultIcon; - - // about download - KUrl m_downloadUrl; - QByteArray m_downloadData; }; #endif // BROWSERAPPLICATION_H diff --git a/src/download.cpp b/src/download.cpp new file mode 100644 index 00000000..aedd0a63 --- /dev/null +++ b/src/download.cpp @@ -0,0 +1,82 @@ +/* ============================================================ + * + * This file is a part of the rekonq project + * + * Copyright (C) 2007 Lukas Appelhans + * Copyright (C) 2008 by Andrea Diamantini + * + * + * This program is free software; you can redistribute it + * and/or modify it under the terms of the GNU General + * Public License as published by the Free Software Foundation; + * either version 2, or (at your option) any later version. + * + * 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 General Public License for more details. + * + * ============================================================ */ + +// local Includes +#include "download.h" +#include "download.moc" + +// KDE Includes +#include + +// Qt Includes +#include +#include + +Download::Download(const KUrl &srcUrl, const KUrl &destUrl) + : m_srcUrl(srcUrl), + m_destUrl(destUrl) +{ + kDebug(5001) << "DownloadFile: " << m_srcUrl.url() << " to dest: " << m_destUrl.url(); + m_copyJob = KIO::get(m_srcUrl); + connect(m_copyJob, SIGNAL(data(KIO::Job*,const QByteArray &)), SLOT(slotData(KIO::Job*, const QByteArray&))); + connect(m_copyJob, SIGNAL(result(KJob *)), SLOT(slotResult(KJob *))); +} + +Download::~Download() +{ +} + +void Download::slotData(KIO::Job *job, const QByteArray& data) +{ + Q_UNUSED(job); + m_data.append(data); +} + +void Download::slotResult(KJob * job) +{ + kDebug(5001); + switch (job->error()) + { + case 0://The download has finished + { + kDebug(5001) << "Downloading successfully finished" << m_destUrl.url(); + QFile torrentFile(m_destUrl.path()); + if (!torrentFile.open(QIODevice::WriteOnly | QIODevice::Text)) {} + torrentFile.write(m_data); + torrentFile.close(); + emit finishedSuccessfully(m_destUrl, m_data); + m_data = 0; + break; + } + case KIO::ERR_FILE_ALREADY_EXIST: + { + kDebug(5001) << "ERROR - File already exists"; + QFile file(m_destUrl.path()); + emit finishedSuccessfully(m_destUrl, file.readAll()); + m_data = 0; + break; + } + default: + kDebug(5001) << "We are sorry to say you, that there were errors while downloading :("; + m_data = 0; + emit finishedWithError(); + break; + } +} diff --git a/src/download.h b/src/download.h new file mode 100644 index 00000000..7986341d --- /dev/null +++ b/src/download.h @@ -0,0 +1,56 @@ +/* ============================================================ + * + * This file is a part of the rekonq project + * + * Copyright (C) 2007 Lukas Appelhans + * Copyright (C) 2008 by Andrea Diamantini + * + * + * This program is free software; you can redistribute it + * and/or modify it under the terms of the GNU General + * Public License as published by the Free Software Foundation; + * either version 2, or (at your option) any later version. + * + * 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 General Public License for more details. + * + * ============================================================ */ + +#ifndef DOWNLOAD_H +#define DOWNLOAD_H + +// KDE Includes +#include +#include + +// Qt Includes +#include +#include + + +class Download : public QObject +{ + Q_OBJECT + public: + Download(const KUrl &srcUrl, const KUrl &destUrl); + ~Download(); + + Q_SIGNALS: + void finishedSuccessfully(KUrl dest, QByteArray data); + void finishedWithError(); + + private slots: + void slotResult(KJob * job); + void slotData(KIO::Job *job, const QByteArray& data); + + private: + KIO::TransferJob *m_copyJob; + KUrl m_srcUrl; + KUrl m_destUrl; + KUrl m_destFile; + QByteArray m_data; +}; + +#endif diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp index 482c351d..7a7ec427 100644 --- a/src/mainwindow.cpp +++ b/src/mainwindow.cpp @@ -384,7 +384,10 @@ void MainWindow::slotOpenLocation() void MainWindow::slotFileSaveAs() { - BrowserApplication::instance()->downloadUrl( currentTab()->url() ); + KUrl srcUrl = currentTab()->url(); + QString destPath = KFileDialog::getSaveFileName(); + KUrl destUrl = KUrl(destPath); + BrowserApplication::instance()->downloadUrl( srcUrl, destUrl ); } diff --git a/src/webview.cpp b/src/webview.cpp index c7202fd8..436db54e 100644 --- a/src/webview.cpp +++ b/src/webview.cpp @@ -124,7 +124,10 @@ void WebPage::handleUnsupportedContent(QNetworkReply *reply) { if (reply->error() == QNetworkReply::NoError) { - BrowserApplication::instance()->downloadUrl( reply->url() ); + KUrl srcUrl = reply->url(); + QString path = ReKonfig::downloadDir() + QString("/") + srcUrl.fileName(); + KUrl destUrl = KUrl(path); + BrowserApplication::instance()->downloadUrl( srcUrl, destUrl ); return; } @@ -305,7 +308,10 @@ void WebView::setStatusBarText(const QString &string) void WebView::downloadRequested(const QNetworkRequest &request) { - BrowserApplication::instance()->downloadUrl( request.url() ); + KUrl srcUrl = request.url(); + QString path = ReKonfig::downloadDir() + QString("/") + srcUrl.fileName(); + KUrl destUrl = KUrl(path); + BrowserApplication::instance()->downloadUrl( srcUrl, destUrl ); } -- cgit v1.2.1