Skip to content

Commit dd687dc

Browse files
committed
fix: prevent overwrite if file exists
- handle situation to prevent download overwrite - add clear all downloads - cleanup
1 parent c2bd1a3 commit dd687dc

File tree

3 files changed

+129
-54
lines changed

3 files changed

+129
-54
lines changed

src/downloadmanagerwidget.cpp

Lines changed: 54 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,12 @@ DownloadManagerWidget::DownloadManagerWidget(QWidget *parent)
1111
setupUi(this);
1212
}
1313

14+
void DownloadManagerWidget::acceptDownload(QWebEngineDownloadItem *download) {
15+
download->accept();
16+
add(new DownloadWidget(download));
17+
show();
18+
}
19+
1420
void DownloadManagerWidget::downloadRequested(
1521
QWebEngineDownloadItem *download) {
1622
Q_ASSERT(download &&
@@ -22,14 +28,39 @@ void DownloadManagerWidget::downloadRequested(
2228
QStandardPaths::DownloadLocation) +
2329
QDir::separator() + QApplication::applicationName())
2430
.toString();
25-
QDir d;
26-
d.mkpath(path);
2731

28-
download->setDownloadFileName(path + QDir::separator() +
29-
download->downloadFileName());
30-
download->accept();
31-
add(new DownloadWidget(download));
32-
show();
32+
QDir().mkpath(path);
33+
34+
auto proposed_file_name =
35+
path + QDir::separator() + download->downloadFileName();
36+
37+
QFileInfo p_file_info(proposed_file_name);
38+
39+
if (p_file_info.exists()) {
40+
41+
QMessageBox msgBox;
42+
msgBox.setText("File with same name already exist!");
43+
msgBox.setInformativeText("Save file with a new name?");
44+
msgBox.setStandardButtons(QMessageBox::Save | QMessageBox::Cancel);
45+
msgBox.setDefaultButton(QMessageBox::Save);
46+
switch (msgBox.exec()) {
47+
case QMessageBox::Save: {
48+
QString n_proposed_file_name = path + QDir::separator() +
49+
utils::generateRandomId(5) + "_" +
50+
download->downloadFileName();
51+
download->setDownloadFileName(n_proposed_file_name);
52+
acceptDownload(download);
53+
break;
54+
}
55+
case QMessageBox::Cancel:
56+
break;
57+
default:
58+
break;
59+
}
60+
} else {
61+
download->setDownloadFileName(proposed_file_name);
62+
acceptDownload(download);
63+
}
3364
}
3465

3566
void DownloadManagerWidget::add(DownloadWidget *downloadWidget) {
@@ -41,8 +72,10 @@ void DownloadManagerWidget::add(DownloadWidget *downloadWidget) {
4172
}
4273

4374
void DownloadManagerWidget::remove(DownloadWidget *downloadWidget) {
44-
m_itemsLayout->removeWidget(downloadWidget);
45-
downloadWidget->deleteLater();
75+
if (downloadWidget != nullptr) {
76+
m_itemsLayout->removeWidget(downloadWidget);
77+
downloadWidget->deleteLater();
78+
}
4679
if (--m_numDownloads == 0)
4780
m_zeroItemsLabel->show();
4881
}
@@ -57,10 +90,17 @@ void DownloadManagerWidget::on_open_download_dir_clicked() {
5790
.toString());
5891
}
5992

60-
void DownloadManagerWidget::keyPressEvent(QKeyEvent *e)
61-
{
62-
if (e->key() == Qt::Key_Escape)
63-
this->close();
93+
void DownloadManagerWidget::keyPressEvent(QKeyEvent *e) {
94+
if (e->key() == Qt::Key_Escape)
95+
this->close();
96+
97+
QWidget::keyPressEvent(e);
98+
}
6499

65-
QWidget::keyPressEvent(e);
100+
void DownloadManagerWidget::on_clear_all_downlads_clicked() {
101+
foreach (auto downloadItem, this->findChildren<DownloadWidget *>()) {
102+
if (downloadItem != nullptr) {
103+
downloadItem->remove();
104+
}
105+
}
66106
}

src/downloadmanagerwidget.h

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,9 @@
5353

5454
#include "ui_downloadmanagerwidget.h"
5555

56-
#include <QWidget>
57-
#include <QSettings>
5856
#include "utils.h"
57+
#include <QSettings>
58+
#include <QWidget>
5959

6060
QT_BEGIN_NAMESPACE
6161
class QWebEngineDownloadItem;
@@ -64,28 +64,32 @@ QT_END_NAMESPACE
6464
class DownloadWidget;
6565

6666
// Displays a list of downloads.
67-
class DownloadManagerWidget final : public QWidget, public Ui::DownloadManagerWidget
68-
{
69-
Q_OBJECT
67+
class DownloadManagerWidget final : public QWidget,
68+
public Ui::DownloadManagerWidget {
69+
Q_OBJECT
7070
public:
71-
explicit DownloadManagerWidget(QWidget *parent = nullptr);
71+
explicit DownloadManagerWidget(QWidget *parent = nullptr);
7272

73-
// Prompts user with a "Save As" dialog. If the user doesn't cancel it, then
74-
// the QWebEngineDownloadItem will be accepted and the DownloadManagerWidget
75-
// will be shown on the screen.
76-
void downloadRequested(QWebEngineDownloadItem *webItem);
73+
// Prompts user with a "Save As" dialog. If the user doesn't cancel it, then
74+
// the QWebEngineDownloadItem will be accepted and the DownloadManagerWidget
75+
// will be shown on the screen.
76+
void downloadRequested(QWebEngineDownloadItem *webItem);
7777

7878
protected slots:
79-
void keyPressEvent(QKeyEvent *e);
79+
void keyPressEvent(QKeyEvent *e);
8080
private slots:
81-
void on_open_download_dir_clicked();
81+
void on_open_download_dir_clicked();
82+
83+
void acceptDownload(QWebEngineDownloadItem *download);
84+
85+
void on_clear_all_downlads_clicked();
8286

8387
private:
84-
void add(DownloadWidget *downloadWidget);
85-
void remove(DownloadWidget *downloadWidget);
88+
void add(DownloadWidget *downloadWidget);
89+
void remove(DownloadWidget *downloadWidget);
8690

87-
int m_numDownloads;
88-
QSettings settings;
91+
int m_numDownloads;
92+
QSettings settings;
8993
};
9094

9195
#endif // DOWNLOADMANAGERWIDGET_H

src/downloadmanagerwidget.ui

Lines changed: 55 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
<rect>
77
<x>0</x>
88
<y>0</y>
9-
<width>450</width>
10-
<height>250</height>
9+
<width>589</width>
10+
<height>363</height>
1111
</rect>
1212
</property>
1313
<property name="minimumSize">
@@ -26,18 +26,6 @@
2626
<property name="sizeConstraint">
2727
<enum>QLayout::SetNoConstraint</enum>
2828
</property>
29-
<property name="leftMargin">
30-
<number>0</number>
31-
</property>
32-
<property name="topMargin">
33-
<number>0</number>
34-
</property>
35-
<property name="rightMargin">
36-
<number>0</number>
37-
</property>
38-
<property name="bottomMargin">
39-
<number>0</number>
40-
</property>
4129
<item>
4230
<widget class="QScrollArea" name="m_scrollArea">
4331
<property name="styleSheet">
@@ -57,8 +45,8 @@
5745
<rect>
5846
<x>0</x>
5947
<y>0</y>
60-
<width>448</width>
61-
<height>248</height>
48+
<width>569</width>
49+
<height>310</height>
6250
</rect>
6351
</property>
6452
<property name="styleSheet">
@@ -109,19 +97,62 @@
10997
</property>
11098
</spacer>
11199
</item>
112-
<item>
113-
<widget class="QPushButton" name="open_download_dir">
114-
<property name="text">
115-
<string>Open Download directory</string>
116-
</property>
117-
</widget>
118-
</item>
119100
</layout>
120101
</widget>
121102
</widget>
122103
</item>
104+
<item>
105+
<layout class="QHBoxLayout" name="horizontalLayout">
106+
<property name="topMargin">
107+
<number>0</number>
108+
</property>
109+
<item>
110+
<spacer name="horizontalSpacer">
111+
<property name="orientation">
112+
<enum>Qt::Horizontal</enum>
113+
</property>
114+
<property name="sizeHint" stdset="0">
115+
<size>
116+
<width>40</width>
117+
<height>20</height>
118+
</size>
119+
</property>
120+
</spacer>
121+
</item>
122+
<item>
123+
<widget class="QPushButton" name="clear_all_downlads">
124+
<property name="toolTip">
125+
<string>Clear download list</string>
126+
</property>
127+
<property name="text">
128+
<string>Clear all</string>
129+
</property>
130+
<property name="icon">
131+
<iconset resource="icons.qrc">
132+
<normaloff>:/icons/close-fill.png</normaloff>:/icons/close-fill.png</iconset>
133+
</property>
134+
</widget>
135+
</item>
136+
<item>
137+
<widget class="QPushButton" name="open_download_dir">
138+
<property name="toolTip">
139+
<string>Open download directory in file manager</string>
140+
</property>
141+
<property name="text">
142+
<string>Open Download directory</string>
143+
</property>
144+
<property name="icon">
145+
<iconset resource="icons.qrc">
146+
<normaloff>:/icons/folder-open-line.png</normaloff>:/icons/folder-open-line.png</iconset>
147+
</property>
148+
</widget>
149+
</item>
150+
</layout>
151+
</item>
123152
</layout>
124153
</widget>
125-
<resources/>
154+
<resources>
155+
<include location="icons.qrc"/>
156+
</resources>
126157
<connections/>
127158
</ui>

0 commit comments

Comments
 (0)