diff options
author | 2023-03-22 20:22:51 +0530 | |
---|---|---|
committer | 2023-03-22 20:22:51 +0530 | |
commit | dd687dc983614e133b841b6b942a13ff2dfd0ebf (patch) | |
tree | 401869a54f06edb143ca0adb8dd9371cc55574a6 /src | |
parent | c2bd1a32cada12f8d4b5e57b589911e96e2153c2 (diff) | |
download | whatsie-dd687dc983614e133b841b6b942a13ff2dfd0ebf.tar.gz whatsie-dd687dc983614e133b841b6b942a13ff2dfd0ebf.zip |
fix: prevent overwrite if file exists
- handle situation to prevent download overwrite
- add clear all downloads
- cleanup
Diffstat (limited to 'src')
-rw-r--r-- | src/downloadmanagerwidget.cpp | 68 | ||||
-rw-r--r-- | src/downloadmanagerwidget.h | 36 | ||||
-rw-r--r-- | src/downloadmanagerwidget.ui | 79 |
3 files changed, 129 insertions, 54 deletions
diff --git a/src/downloadmanagerwidget.cpp b/src/downloadmanagerwidget.cpp index e9865c8..812c471 100644 --- a/src/downloadmanagerwidget.cpp +++ b/src/downloadmanagerwidget.cpp @@ -11,6 +11,12 @@ DownloadManagerWidget::DownloadManagerWidget(QWidget *parent) setupUi(this);
}
+void DownloadManagerWidget::acceptDownload(QWebEngineDownloadItem *download) {
+ download->accept();
+ add(new DownloadWidget(download));
+ show();
+}
+
void DownloadManagerWidget::downloadRequested(
QWebEngineDownloadItem *download) {
Q_ASSERT(download &&
@@ -22,14 +28,39 @@ void DownloadManagerWidget::downloadRequested( QStandardPaths::DownloadLocation) +
QDir::separator() + QApplication::applicationName())
.toString();
- QDir d;
- d.mkpath(path);
- download->setDownloadFileName(path + QDir::separator() +
- download->downloadFileName());
- download->accept();
- add(new DownloadWidget(download));
- show();
+ 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) {
@@ -41,8 +72,10 @@ void DownloadManagerWidget::add(DownloadWidget *downloadWidget) { }
void DownloadManagerWidget::remove(DownloadWidget *downloadWidget) {
- m_itemsLayout->removeWidget(downloadWidget);
- downloadWidget->deleteLater();
+ if (downloadWidget != nullptr) {
+ m_itemsLayout->removeWidget(downloadWidget);
+ downloadWidget->deleteLater();
+ }
if (--m_numDownloads == 0)
m_zeroItemsLabel->show();
}
@@ -57,10 +90,17 @@ void DownloadManagerWidget::on_open_download_dir_clicked() { .toString());
}
-void DownloadManagerWidget::keyPressEvent(QKeyEvent *e)
-{
- if (e->key() == Qt::Key_Escape)
- this->close();
+void DownloadManagerWidget::keyPressEvent(QKeyEvent *e) {
+ if (e->key() == Qt::Key_Escape)
+ this->close();
+
+ QWidget::keyPressEvent(e);
+}
- QWidget::keyPressEvent(e);
+void DownloadManagerWidget::on_clear_all_downlads_clicked() {
+ foreach (auto downloadItem, this->findChildren<DownloadWidget *>()) {
+ if (downloadItem != nullptr) {
+ downloadItem->remove();
+ }
+ }
}
diff --git a/src/downloadmanagerwidget.h b/src/downloadmanagerwidget.h index 4a8d059..76464e8 100644 --- a/src/downloadmanagerwidget.h +++ b/src/downloadmanagerwidget.h @@ -53,9 +53,9 @@ #include "ui_downloadmanagerwidget.h"
-#include <QWidget>
-#include <QSettings>
#include "utils.h"
+#include <QSettings>
+#include <QWidget>
QT_BEGIN_NAMESPACE
class QWebEngineDownloadItem;
@@ -64,28 +64,32 @@ QT_END_NAMESPACE class DownloadWidget;
// Displays a list of downloads.
-class DownloadManagerWidget final : public QWidget, public Ui::DownloadManagerWidget
-{
- Q_OBJECT
+class DownloadManagerWidget final : public QWidget,
+ public Ui::DownloadManagerWidget {
+ Q_OBJECT
public:
- explicit DownloadManagerWidget(QWidget *parent = nullptr);
+ explicit DownloadManagerWidget(QWidget *parent = nullptr);
- // Prompts user with a "Save As" dialog. If the user doesn't cancel it, then
- // the QWebEngineDownloadItem will be accepted and the DownloadManagerWidget
- // will be shown on the screen.
- void downloadRequested(QWebEngineDownloadItem *webItem);
+ // Prompts user with a "Save As" dialog. If the user doesn't cancel it, then
+ // the QWebEngineDownloadItem will be accepted and the DownloadManagerWidget
+ // will be shown on the screen.
+ void downloadRequested(QWebEngineDownloadItem *webItem);
protected slots:
- void keyPressEvent(QKeyEvent *e);
+ void keyPressEvent(QKeyEvent *e);
private slots:
- void on_open_download_dir_clicked();
+ void on_open_download_dir_clicked();
+
+ void acceptDownload(QWebEngineDownloadItem *download);
+
+ void on_clear_all_downlads_clicked();
private:
- void add(DownloadWidget *downloadWidget);
- void remove(DownloadWidget *downloadWidget);
+ void add(DownloadWidget *downloadWidget);
+ void remove(DownloadWidget *downloadWidget);
- int m_numDownloads;
- QSettings settings;
+ int m_numDownloads;
+ QSettings settings;
};
#endif // DOWNLOADMANAGERWIDGET_H
diff --git a/src/downloadmanagerwidget.ui b/src/downloadmanagerwidget.ui index c87286b..7020d03 100644 --- a/src/downloadmanagerwidget.ui +++ b/src/downloadmanagerwidget.ui @@ -6,8 +6,8 @@ <rect>
<x>0</x>
<y>0</y>
- <width>450</width>
- <height>250</height>
+ <width>589</width>
+ <height>363</height>
</rect>
</property>
<property name="minimumSize">
@@ -26,18 +26,6 @@ <property name="sizeConstraint">
<enum>QLayout::SetNoConstraint</enum>
</property>
- <property name="leftMargin">
- <number>0</number>
- </property>
- <property name="topMargin">
- <number>0</number>
- </property>
- <property name="rightMargin">
- <number>0</number>
- </property>
- <property name="bottomMargin">
- <number>0</number>
- </property>
<item>
<widget class="QScrollArea" name="m_scrollArea">
<property name="styleSheet">
@@ -57,8 +45,8 @@ <rect>
<x>0</x>
<y>0</y>
- <width>448</width>
- <height>248</height>
+ <width>569</width>
+ <height>310</height>
</rect>
</property>
<property name="styleSheet">
@@ -109,19 +97,62 @@ </property>
</spacer>
</item>
- <item>
- <widget class="QPushButton" name="open_download_dir">
- <property name="text">
- <string>Open Download directory</string>
- </property>
- </widget>
- </item>
</layout>
</widget>
</widget>
</item>
+ <item>
+ <layout class="QHBoxLayout" name="horizontalLayout">
+ <property name="topMargin">
+ <number>0</number>
+ </property>
+ <item>
+ <spacer name="horizontalSpacer">
+ <property name="orientation">
+ <enum>Qt::Horizontal</enum>
+ </property>
+ <property name="sizeHint" stdset="0">
+ <size>
+ <width>40</width>
+ <height>20</height>
+ </size>
+ </property>
+ </spacer>
+ </item>
+ <item>
+ <widget class="QPushButton" name="clear_all_downlads">
+ <property name="toolTip">
+ <string>Clear download list</string>
+ </property>
+ <property name="text">
+ <string>Clear all</string>
+ </property>
+ <property name="icon">
+ <iconset resource="icons.qrc">
+ <normaloff>:/icons/close-fill.png</normaloff>:/icons/close-fill.png</iconset>
+ </property>
+ </widget>
+ </item>
+ <item>
+ <widget class="QPushButton" name="open_download_dir">
+ <property name="toolTip">
+ <string>Open download directory in file manager</string>
+ </property>
+ <property name="text">
+ <string>Open Download directory</string>
+ </property>
+ <property name="icon">
+ <iconset resource="icons.qrc">
+ <normaloff>:/icons/folder-open-line.png</normaloff>:/icons/folder-open-line.png</iconset>
+ </property>
+ </widget>
+ </item>
+ </layout>
+ </item>
</layout>
</widget>
- <resources/>
+ <resources>
+ <include location="icons.qrc"/>
+ </resources>
<connections/>
</ui>
|