summaryrefslogtreecommitdiff
path: root/src/downloadmanager.cpp
diff options
context:
space:
mode:
authorAndrea Diamantini <adjam7@gmail.com>2011-11-27 12:00:28 +0100
committerAndrea Diamantini <adjam7@gmail.com>2011-11-29 00:09:12 +0100
commitd76f6bdb544f6ba4fb6b92fbeb36bfe25450eb1f (patch)
treeb6e9b858d5f012599bd5348d69c21ee88b756131 /src/downloadmanager.cpp
parentAdd settings for fixed download path (diff)
downloadrekonq-d76f6bdb544f6ba4fb6b92fbeb36bfe25450eb1f.tar.xz
Download Management ++
- let users choose a "default" download dir - Moved download code to download manager - follow dfaure's suggestions about overwrite handling (kdelibs commit 88e3b372fb9c539) - download label notification (for 3 seconds): this can be further improved with an icon... BUG: 271683 BUG: 283177
Diffstat (limited to 'src/downloadmanager.cpp')
-rw-r--r--src/downloadmanager.cpp144
1 files changed, 144 insertions, 0 deletions
diff --git a/src/downloadmanager.cpp b/src/downloadmanager.cpp
index 490637bf..1f58d99f 100644
--- a/src/downloadmanager.cpp
+++ b/src/downloadmanager.cpp
@@ -29,15 +29,32 @@
#include "downloadmanager.h"
#include "downloadmanager.moc"
+// Auto Includes
+#include "rekonq.h"
+
// KDE Includes
#include <KStandardDirs>
+#include <KToolInvocation>
+#include <KFileDialog>
+
+#include <kio/scheduler.h>
+
+#include <KIO/Job>
+#include <KIO/CopyJob>
+#include <KIO/JobUiDelegate>
// Qt Includes
#include <QDataStream>
#include <QDateTime>
#include <QFile>
+#include <QFileInfo>
#include <QString>
#include <QWebSettings>
+#include <QNetworkReply>
+
+#include <QtDBus/QDBusConnection>
+#include <QtDBus/QDBusConnectionInterface>
+#include <QtDBus/QDBusInterface>
DownloadManager::DownloadManager(QObject *parent)
@@ -103,3 +120,130 @@ bool DownloadManager::clearDownloadsHistory()
QFile downloadFile(downloadFilePath);
return downloadFile.remove();
}
+
+
+void DownloadManager::downloadLinksWithKGet(const QVariant &contentList)
+{
+ if (!QDBusConnection::sessionBus().interface()->isServiceRegistered("org.kde.kget"))
+ {
+ KToolInvocation::kdeinitExecWait("kget");
+ }
+ QDBusInterface kget("org.kde.kget", "/KGet", "org.kde.kget.main");
+ if (kget.isValid())
+ {
+ kget.call("importLinks", contentList);
+ }
+}
+
+
+// NOTE
+// These 2 functions have been copied from the KWebPage class to implement a local version of the downloadResponse method.
+// In this way, we can easily provide the extra functionality we need:
+// 1. KGet Integration
+// 2. Save downloads history
+bool DownloadManager::downloadResource(const KUrl &srcUrl, const KIO::MetaData &metaData, QWidget *parent, const QString &suggestedName)
+{
+ KUrl destUrl;
+
+ int result = KIO::R_OVERWRITE;
+ const QString fileName((suggestedName.isEmpty() ? srcUrl.fileName() : suggestedName));
+
+ do
+ {
+ if (ReKonfig::askDownloadPath())
+ {
+ // follow bug:184202 fixes
+ destUrl = KFileDialog::getSaveFileName(KUrl::fromPath(fileName), QString(), parent);
+ }
+ else
+ {
+ destUrl = KUrl(ReKonfig::downloadPath().path() + QL1C('/') + fileName);
+ }
+
+ kDebug() << "DEST URL: " << destUrl;
+
+ if (destUrl.isEmpty())
+ return false;
+
+ }
+ while (result == KIO::R_CANCEL && destUrl.isValid());
+
+ // Save download history
+ DownloadItem *item = addDownload(srcUrl.pathOrUrl(), destUrl.pathOrUrl());
+ emit notifyDownload(fileName);
+
+ if (!KStandardDirs::findExe("kget").isNull() && ReKonfig::kgetDownload())
+ {
+ //KGet integration:
+ if (!QDBusConnection::sessionBus().interface()->isServiceRegistered("org.kde.kget"))
+ {
+ KToolInvocation::kdeinitExecWait("kget");
+ }
+ QDBusInterface kget("org.kde.kget", "/KGet", "org.kde.kget.main");
+ if (!kget.isValid())
+ return false;
+
+ QDBusMessage transfer = kget.call(QL1S("addTransfer"), srcUrl.prettyUrl(), destUrl.prettyUrl(), true);
+ if (transfer.arguments().isEmpty())
+ return true;
+
+ const QString transferPath = transfer.arguments().first().toString();
+ item->setKGetTransferDbusPath(transferPath);
+ return true;
+ }
+
+ KIO::Job *job = KIO::copy(srcUrl, destUrl, KIO::Overwrite);
+ if (item)
+ {
+ QObject::connect(job, SIGNAL(percent(KJob *, unsigned long)), item, SLOT(updateProgress(KJob *, unsigned long)));
+ QObject::connect(job, SIGNAL(finished(KJob *)), item, SLOT(onFinished(KJob*)));
+ }
+
+ if (!metaData.isEmpty())
+ job->setMetaData(metaData);
+
+ job->addMetaData(QL1S("MaxCacheSize"), QL1S("0")); // Don't store in http cache.
+ job->addMetaData(QL1S("cache"), QL1S("cache")); // Use entry from cache if available.
+ job->uiDelegate()->setAutoErrorHandlingEnabled(true);
+ return true;
+}
+
+
+// -------------------------------------------------------------------------------------------------------------------
+
+
+// STATIC
+void DownloadManager::extractSuggestedFileName(const QNetworkReply* reply, QString& fileName)
+{
+ fileName.clear();
+ const KIO::MetaData& metaData = reply->attribute(static_cast<QNetworkRequest::Attribute>(KIO::AccessManager::MetaData)).toMap();
+ if (metaData.value(QL1S("content-disposition-type")).compare(QL1S("attachment"), Qt::CaseInsensitive) == 0)
+ fileName = metaData.value(QL1S("content-disposition-filename"));
+
+ if (!fileName.isEmpty())
+ return;
+
+ if (!reply->hasRawHeader("Content-Disposition"))
+ return;
+
+ const QString value(QL1S(reply->rawHeader("Content-Disposition").simplified().constData()));
+ if (value.startsWith(QL1S("attachment"), Qt::CaseInsensitive) || value.startsWith(QL1S("inline"), Qt::CaseInsensitive))
+ {
+ const int length = value.size();
+ int pos = value.indexOf(QL1S("filename"), 0, Qt::CaseInsensitive);
+ if (pos > -1)
+ {
+ pos += 9;
+ while (pos < length && (value.at(pos) == QL1C(' ') || value.at(pos) == QL1C('=') || value.at(pos) == QL1C('"')))
+ pos++;
+
+ int endPos = pos;
+ while (endPos < length && value.at(endPos) != QL1C('"') && value.at(endPos) != QL1C(';'))
+ endPos++;
+
+ if (endPos > pos)
+ fileName = value.mid(pos, (endPos - pos)).trimmed();
+ }
+ }
+}
+