From 8eac211c434358cbe536eb6f1448f1d565a5f26f Mon Sep 17 00:00:00 2001 From: Andrea Diamantini Date: Tue, 21 Apr 2009 11:27:17 +0200 Subject: Moving new download system to mainline. I did some changes to fit things as simple as possible. We can obviously improve things in a second moment.. --- src/download.cpp | 184 +++++++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 158 insertions(+), 26 deletions(-) (limited to 'src/download.cpp') diff --git a/src/download.cpp b/src/download.cpp index 9de8dd36..dba7f98f 100644 --- a/src/download.cpp +++ b/src/download.cpp @@ -18,26 +18,165 @@ * * ============================================================ */ + // local Includes #include "download.h" #include "download.moc" // KDE Includes #include +#include +#include +#include +#include +#include // Qt Includes #include #include +// Local Includes +#include "application.h" +#include "mainwindow.h" + + +DownloadManager::DownloadManager() + : QObject() +{ +} + + +DownloadManager::~DownloadManager() +{ + foreach(Download *download, m_downloads) + { + // cancel all unfinished downloads + download->cancel(); + delete download; + } +} + + +void DownloadManager::newDownload(const KUrl &srcUrl, const KUrl &destUrl) +{ + KUrl destination = destUrl; + Download::DownloadType type; + + KSharedPtr mimeType = KMimeType::findByPath(srcUrl.prettyUrl()); + + QString typeText = KMimeType::extractKnownExtension(srcUrl.fileName()); + typeText += " (" + mimeType->name() + ")"; + + int answer = KMessageBox::questionYesNoCancel( NULL, + i18n("Download '%1'?\n""Type: %2", srcUrl.prettyUrl(), typeText ), + i18n("Download '%1'...", srcUrl.fileName() ), + KStandardGuiItem::save(), + KStandardGuiItem::open(), + KStandardGuiItem::cancel(), + "showOpenSaveDownloadDialog" + ); + + switch(answer) + { + case KMessageBox::Cancel: + return; + break; + + case KMessageBox::Yes: // ----- SAVE + // if destination is empty than ask for download path (if showOpenSaveDownloadDialog setting enabled) + if (destination.isEmpty()) + { + destination = downloadDestination(srcUrl.fileName()); + } + type = Download::Save; + break; + + case KMessageBox::No: // ----- OPEN + // Download file to tmp dir + destination.setDirectory(KStandardDirs::locateLocal("tmp", "", true)); + destination.addPath(srcUrl.fileName()); + type = Download::Open; + break; + + default: + // impossible + break; + }; + + // if user canceled download than abort + if (destination.isEmpty()) + return; + + Download *download = new Download(srcUrl, destination, type); + connect(download, SIGNAL(downloadFinished(int)), this, SLOT(slotDownloadFinished(int))); + m_downloads.append(download); +} + -Download::Download(const KUrl &srcUrl, const KUrl &destUrl) +const QList &DownloadManager::downloads() const +{ + return m_downloads; +}; + + +KUrl DownloadManager::downloadDestination(const QString &filename) +{ + KUrl destination = ReKonfig::downloadDir(); + if (destination.isEmpty()) + destination = KGlobalSettings::downloadPath(); + destination.addPath(filename); + + if (!ReKonfig::downloadToDefaultDir()) + { + destination = KFileDialog::getSaveUrl(destination); + // if user canceled the download return empty url + if (destination.isEmpty()) + return KUrl(); + } + return destination; +} + + +void DownloadManager::slotDownloadFinished(int errorCode) +{ + Q_UNUSED(errorCode) + + // if sender exists and list contains it - (open and) delete it + Download *download = qobject_cast(sender()); + if (download && m_downloads.contains(download)) + { + if (download->type() == Download::Open) + { + KSharedPtr mimeType = KMimeType::findByPath(download->destUrl().prettyUrl()); + KRun::runUrl(download->destUrl(), mimeType->name(), NULL, true); + } + disconnect(download, SIGNAL(downloadFinished(int)), this, SLOT(slotDownloadFinished(int))); + int index = m_downloads.indexOf(download); + delete m_downloads.takeAt(index); + return; + } + kWarning() << "Could not find download or invalid sender. Sender:" << sender(); +} + + +//---- + +#include +#include +#include + + +Download::Download(const KUrl &srcUrl, const KUrl &destUrl, DownloadType type) : QObject() , m_srcUrl(srcUrl) , m_destUrl(destUrl) + , m_type(type) { - kWarning() << "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&))); + Q_ASSERT(!m_srcUrl.isEmpty()); + Q_ASSERT(!m_destUrl.isEmpty()); + kDebug() << "DownloadFile: " << m_srcUrl.url() << " to dest: " << m_destUrl.url(); + + m_copyJob = KIO::file_copy(m_srcUrl, m_destUrl); connect(m_copyJob, SIGNAL(result(KJob *)), SLOT(slotResult(KJob *))); } @@ -47,45 +186,38 @@ Download::~Download() } -void Download::slotData(KIO::Job *job, const QByteArray& data) +void Download::cancel() { - Q_UNUSED(job); - m_data.append(data); + bool result = m_copyJob->kill(KJob::EmitResult); + Q_ASSERT(result); } -void Download::slotResult(KJob * job) +void Download::slotResult(KJob *job) { switch (job->error()) { - case 0://The download has finished + case 0: //The download has finished { - kDebug(5001) << "Downloading successfully finished: " << m_destUrl.url(); - QFile destFile(m_destUrl.path()); - int n = 1; - QString fn = destFile.fileName(); - while (destFile.exists()) - { - destFile.setFileName(fn + "." + QString::number(n)); - n++; - } - if (destFile.open(QIODevice::WriteOnly | QIODevice::Text)) - { - destFile.write(m_data); - destFile.close(); - } - m_data = 0; + kDebug() << "Downloading successfully finished: " << m_destUrl.url(); break; } case KIO::ERR_FILE_ALREADY_EXIST: { kWarning() << "ERROR - File already exists"; - m_data = 0; + break; + } + case KIO::ERR_USER_CANCELED: + { + kWarning() << "ERROR - User canceled the downlaod"; break; } default: kWarning() << "We are sorry to say you, that there were errors while downloading :("; - m_data = 0; break; } + + // inform the world + emit downloadFinished(job->error()); } + -- cgit v1.2.1