From 8c25c63acf839b09e10d398cfa44d5a45d3ba7c0 Mon Sep 17 00:00:00 2001 From: Aqua-sama Date: Sun, 15 Jan 2017 15:39:42 +0100 Subject: Download manager --- src/forms/downloaddialog.cpp | 46 ++++++++++++++++++++++++++ src/forms/downloaddialog.h | 26 +++++++++++++++ src/forms/downloaddialog.ui | 67 ++++++++++++++++++++++++++++++++++++++ src/mainwindow.cpp | 5 +++ src/mainwindow.h | 2 ++ src/smolbote.pro | 12 +++++-- src/webengine/downloaditemform.cpp | 57 ++++++++++++++++++++++++++++++++ src/webengine/downloaditemform.h | 28 ++++++++++++++++ src/webengine/downloaditemform.ui | 57 ++++++++++++++++++++++++++++++++ 9 files changed, 297 insertions(+), 3 deletions(-) create mode 100644 src/forms/downloaddialog.cpp create mode 100644 src/forms/downloaddialog.h create mode 100644 src/forms/downloaddialog.ui create mode 100644 src/webengine/downloaditemform.cpp create mode 100644 src/webengine/downloaditemform.h create mode 100644 src/webengine/downloaditemform.ui diff --git a/src/forms/downloaddialog.cpp b/src/forms/downloaddialog.cpp new file mode 100644 index 0000000..4943fcc --- /dev/null +++ b/src/forms/downloaddialog.cpp @@ -0,0 +1,46 @@ +#include "downloaddialog.h" +#include "ui_downloaddialog.h" + +#include +#include +#include +#include +#include +#include "webengine/downloaditemform.h" + +DownloadDialog::DownloadDialog(QWidget *parent) : + QDialog(parent), + ui(new Ui::DownloadDialog) +{ + ui->setupUi(this); +} + +DownloadDialog::~DownloadDialog() +{ + delete ui; +} + +void DownloadDialog::addDownload(QWebEngineDownloadItem *item) +{ + qDebug("download item: %s", qUtf8Printable(item->url().toString())); + QString filepath = QFileDialog::getSaveFileName(this, tr("Save")); + + if(filepath.isEmpty()) { + // user cancelled the save dialog + item->cancel(); + return; + } + + item->setPath(filepath); + + QListWidgetItem *listItem = new QListWidgetItem(); + int rowIndex = ui->listWidget->count(); + ui->listWidget->insertItem(rowIndex, listItem); + + DownloadItemForm *form = new DownloadItemForm(item, this); + listItem->setSizeHint(form->size()); + ui->listWidget->setItemWidget(listItem, form); + + item->accept(); + this->show(); +} diff --git a/src/forms/downloaddialog.h b/src/forms/downloaddialog.h new file mode 100644 index 0000000..bec0037 --- /dev/null +++ b/src/forms/downloaddialog.h @@ -0,0 +1,26 @@ +#ifndef DOWNLOADDIALOG_H +#define DOWNLOADDIALOG_H + +#include + +namespace Ui { +class DownloadDialog; +} + +class QWebEngineDownloadItem; +class DownloadDialog : public QDialog +{ + Q_OBJECT + +public: + explicit DownloadDialog(QWidget *parent = 0); + ~DownloadDialog(); + +public slots: + void addDownload(QWebEngineDownloadItem *item); + +private: + Ui::DownloadDialog *ui; +}; + +#endif // DOWNLOADDIALOG_H diff --git a/src/forms/downloaddialog.ui b/src/forms/downloaddialog.ui new file mode 100644 index 0000000..75a6faa --- /dev/null +++ b/src/forms/downloaddialog.ui @@ -0,0 +1,67 @@ + + + DownloadDialog + + + + 0 + 0 + 640 + 480 + + + + Dialog + + + + + + + + + Qt::Horizontal + + + QDialogButtonBox::Close + + + + + + + + + buttonBox + accepted() + DownloadDialog + accept() + + + 248 + 254 + + + 157 + 274 + + + + + buttonBox + rejected() + DownloadDialog + reject() + + + 316 + 260 + + + 286 + 274 + + + + + diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp index 638ba7f..9e028e4 100644 --- a/src/mainwindow.cpp +++ b/src/mainwindow.cpp @@ -9,9 +9,11 @@ #include "forms/profiledialog.h" #include #include +#include MainWindow::MainWindow(Browser *instance, QUrl defaultUrl, QWidget *parent) : QMainWindow(parent), + downloadManager(new DownloadDialog(this)), ui(new Ui::MainWindow), navigationToolBar(new QToolBar(this)), tabToolBar(new QToolBar(this)), @@ -43,6 +45,7 @@ MainWindow::MainWindow(Browser *instance, QUrl defaultUrl, QWidget *parent) : ui->menuBar->setCornerWidget(rightBar); profileMenu->addAction(tr("Edit profile"), this, SLOT(createProfileDialog())); profileMenu->addAction(tr("Load profile"), this, SLOT(handleLoadProfile())); + profileMenu->addAction(tr("Downloads"), downloadManager, SLOT(show())); //profileMenu->addAction(tr("Settings")); //profileMenu->addAction(tr("Cookies")); @@ -99,6 +102,8 @@ void MainWindow::loadProfile(const QString &name) qDebug("Using profile: %s", qUtf8Printable(profileName)); profile = new WebEngineProfile(profileName, this); } + + connect(profile, SIGNAL(downloadRequested(QWebEngineDownloadItem*)), downloadManager, SLOT(addDownload(QWebEngineDownloadItem*))); } void MainWindow::handleLoadProfile() diff --git a/src/mainwindow.h b/src/mainwindow.h index 2eedebf..2ad88b8 100644 --- a/src/mainwindow.h +++ b/src/mainwindow.h @@ -8,6 +8,7 @@ #include "webengine/webengineprofile.h" #include #include "widgets/webviewtabbar.h" +#include "forms/downloaddialog.h" namespace Ui { class MainWindow; @@ -42,6 +43,7 @@ private slots: private: Browser *browserInstance; + DownloadDialog *downloadManager; QString profileName; WebEngineProfile *profile = nullptr; diff --git a/src/smolbote.pro b/src/smolbote.pro index d2eaa8c..bf7a05d 100644 --- a/src/smolbote.pro +++ b/src/smolbote.pro @@ -17,14 +17,20 @@ SOURCES += main.cpp \ widgets/webviewtabbar.cpp \ settings.cpp \ forms/profiledialog.cpp \ - webengine/webengineprofile.cpp + webengine/webengineprofile.cpp \ + forms/downloaddialog.cpp \ + webengine/downloaditemform.cpp HEADERS += mainwindow.h \ browser.h \ widgets/webviewtabbar.h \ settings.h \ forms/profiledialog.h \ - webengine/webengineprofile.h + webengine/webengineprofile.h \ + forms/downloaddialog.h \ + webengine/downloaditemform.h FORMS += mainwindow.ui \ - forms/profiledialog.ui + forms/profiledialog.ui \ + forms/downloaddialog.ui \ + webengine/downloaditemform.ui diff --git a/src/webengine/downloaditemform.cpp b/src/webengine/downloaditemform.cpp new file mode 100644 index 0000000..717a41d --- /dev/null +++ b/src/webengine/downloaditemform.cpp @@ -0,0 +1,57 @@ +#include "downloaditemform.h" +#include "ui_downloaditemform.h" + +#include +#include + +DownloadItemForm::DownloadItemForm(QWebEngineDownloadItem *item, QWidget *parent) : + QWidget(parent), + ui(new Ui::DownloadItemForm) +{ + ui->setupUi(this); + ui->url_lineEdit->setText(item->url().toString()); + + connect(item, SIGNAL(stateChanged(QWebEngineDownloadItem::DownloadState)), this, SLOT(updateState(QWebEngineDownloadItem::DownloadState))); + connect(item, SIGNAL(downloadProgress(qint64,qint64)), this, SLOT(updateProgress(qint64,qint64))); + connect(item, SIGNAL(finished()), this, SLOT(updateFinished())); +} + +DownloadItemForm::~DownloadItemForm() +{ + delete ui; +} + +void DownloadItemForm::updateState(QWebEngineDownloadItem::DownloadState state) +{ + switch (state) { + case QWebEngineDownloadItem::DownloadRequested: + ui->status_label->setText(tr("Requested")); + break; + case QWebEngineDownloadItem::DownloadInProgress: + ui->status_label->setText(tr("In progress")); + break; + case QWebEngineDownloadItem::DownloadCompleted: + ui->status_label->setText(tr("Completed")); + break; + case QWebEngineDownloadItem::DownloadCancelled: + ui->status_label->setText(tr("Cancelled")); + break; + case QWebEngineDownloadItem::DownloadInterrupted: + ui->status_label->setText(tr("Interrupted")); + break; + default: + break; + } +} + +void DownloadItemForm::updateProgress(qint64 value, qint64 total) +{ + ui->progressBar->setMaximum(total); + ui->progressBar->setValue(value); +} + +void DownloadItemForm::updateFinished() +{ + ui->progressBar->setMaximum(100); + ui->progressBar->setValue(100); +} diff --git a/src/webengine/downloaditemform.h b/src/webengine/downloaditemform.h new file mode 100644 index 0000000..996b876 --- /dev/null +++ b/src/webengine/downloaditemform.h @@ -0,0 +1,28 @@ +#ifndef DOWNLOADITEMFORM_H +#define DOWNLOADITEMFORM_H + +#include +#include + +namespace Ui { +class DownloadItemForm; +} + +class DownloadItemForm : public QWidget +{ + Q_OBJECT + +public: + explicit DownloadItemForm(QWebEngineDownloadItem *item, QWidget *parent = 0); + ~DownloadItemForm(); + +private slots: + void updateState(QWebEngineDownloadItem::DownloadState state); + void updateProgress(qint64 value, qint64 total); + void updateFinished(); + +private: + Ui::DownloadItemForm *ui; +}; + +#endif // DOWNLOADITEMFORM_H diff --git a/src/webengine/downloaditemform.ui b/src/webengine/downloaditemform.ui new file mode 100644 index 0000000..5aa46bd --- /dev/null +++ b/src/webengine/downloaditemform.ui @@ -0,0 +1,57 @@ + + + DownloadItemForm + + + + 0 + 0 + 600 + 80 + + + + Form + + + + + + + + URL + + + + + + + false + + + + + + + + + + + Unknown + + + + + + + 24 + + + + + + + + + + -- cgit v1.2.1