blob: 812c47155a96374280b026c5b93c61fa672021a0 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
|
#include "downloadmanagerwidget.h"
#include "downloadwidget.h"
#include <QFileDialog>
#include <QStandardPaths>
#include <QWebEngineDownloadItem>
DownloadManagerWidget::DownloadManagerWidget(QWidget *parent)
: QWidget(parent), m_numDownloads(0) {
setupUi(this);
}
void DownloadManagerWidget::acceptDownload(QWebEngineDownloadItem *download) {
download->accept();
add(new DownloadWidget(download));
show();
}
void DownloadManagerWidget::downloadRequested(
QWebEngineDownloadItem *download) {
Q_ASSERT(download &&
download->state() == QWebEngineDownloadItem::DownloadRequested);
QString path =
settings
.value("defaultDownloadLocation",
QStandardPaths::writableLocation(
QStandardPaths::DownloadLocation) +
QDir::separator() + QApplication::applicationName())
.toString();
QDir().mkpath(path);
auto proposed_file_name =
path + QDir::separator() + download->downloadFileName();
QFileInfo p_file_info(proposed_file_name);
if (p_file_info.exists()) {
QMessageBox msgBox;
msgBox.setText("File with same name already exist!");
msgBox.setInformativeText("Save file with a new name?");
msgBox.setStandardButtons(QMessageBox::Save | QMessageBox::Cancel);
msgBox.setDefaultButton(QMessageBox::Save);
switch (msgBox.exec()) {
case QMessageBox::Save: {
QString n_proposed_file_name = path + QDir::separator() +
utils::generateRandomId(5) + "_" +
download->downloadFileName();
download->setDownloadFileName(n_proposed_file_name);
acceptDownload(download);
break;
}
case QMessageBox::Cancel:
break;
default:
break;
}
} else {
download->setDownloadFileName(proposed_file_name);
acceptDownload(download);
}
}
void DownloadManagerWidget::add(DownloadWidget *downloadWidget) {
connect(downloadWidget, &DownloadWidget::removeClicked, this,
&DownloadManagerWidget::remove);
m_itemsLayout->insertWidget(0, downloadWidget, 0, Qt::AlignTop);
if (m_numDownloads++ == 0)
m_zeroItemsLabel->hide();
}
void DownloadManagerWidget::remove(DownloadWidget *downloadWidget) {
if (downloadWidget != nullptr) {
m_itemsLayout->removeWidget(downloadWidget);
downloadWidget->deleteLater();
}
if (--m_numDownloads == 0)
m_zeroItemsLabel->show();
}
void DownloadManagerWidget::on_open_download_dir_clicked() {
utils::desktopOpenUrl(settings
.value("defaultDownloadLocation",
QStandardPaths::writableLocation(
QStandardPaths::DownloadLocation) +
QDir::separator() +
QApplication::applicationName())
.toString());
}
void DownloadManagerWidget::keyPressEvent(QKeyEvent *e) {
if (e->key() == Qt::Key_Escape)
this->close();
QWidget::keyPressEvent(e);
}
void DownloadManagerWidget::on_clear_all_downlads_clicked() {
foreach (auto downloadItem, this->findChildren<DownloadWidget *>()) {
if (downloadItem != nullptr) {
downloadItem->remove();
}
}
}
|