From 6c60fa5161fbbc5e35aa13ecf4c64fde7bd67f80 Mon Sep 17 00:00:00 2001 From: Chocobo1 Date: Mon, 12 Sep 2022 12:22:45 +0800 Subject: [PATCH] Don't reorder URLs when copied from the clipboard Also process URLs in the same order as from the text edit widget. Closes #17693. PR #17700. --- src/gui/downloadfromurldialog.cpp | 43 +++++++++++++++++++++---------- 1 file changed, 29 insertions(+), 14 deletions(-) diff --git a/src/gui/downloadfromurldialog.cpp b/src/gui/downloadfromurldialog.cpp index 590745239..56b8123a2 100644 --- a/src/gui/downloadfromurldialog.cpp +++ b/src/gui/downloadfromurldialog.cpp @@ -36,6 +36,7 @@ #include #include #include +#include #include "ui_downloadfromurldialog.h" #include "utils.h" @@ -76,19 +77,23 @@ DownloadFromURLDialog::DownloadFromURLDialog(QWidget *parent) const QString clipboardText = qApp->clipboard()->text(); const QList clipboardList = QStringView(clipboardText).split(u'\n'); - QSet uniqueURLs; - for (QStringView strRef : clipboardList) + // show urls in the original order as from the clipboard + QStringList urls; + urls.reserve(clipboardList.size()); + + for (QStringView url : clipboardList) { - strRef = strRef.trimmed(); - if (strRef.isEmpty()) continue; + url = url.trimmed(); + + if (url.isEmpty()) + continue; - const QString str = strRef.toString(); - if (isDownloadable(str)) - uniqueURLs << str; + if (const QString urlString = url.toString(); isDownloadable(urlString)) + urls << urlString; } - const QString text = uniqueURLs.values().join(u'\n') - + (!uniqueURLs.isEmpty() ? u"\n" : u""); + const QString text = urls.join(u'\n') + + (!urls.isEmpty() ? u"\n" : u""); m_ui->textUrls->setText(text); m_ui->textUrls->moveCursor(QTextCursor::End); @@ -108,22 +113,32 @@ void DownloadFromURLDialog::onSubmit() const QString plainText = m_ui->textUrls->toPlainText(); const QList urls = QStringView(plainText).split(u'\n'); - QSet uniqueURLs; + // process urls in the original order as from the widget + QSet uniqueURLs; + QStringList urlList; + urlList.reserve(urls.size()); + for (QStringView url : urls) { url = url.trimmed(); - if (url.isEmpty()) continue; - uniqueURLs << url.toString(); + if (url.isEmpty()) + continue; + + if (!uniqueURLs.contains(url)) + { + uniqueURLs.insert(url); + urlList << url.toString(); + } } - if (uniqueURLs.isEmpty()) + if (urlList.isEmpty()) { QMessageBox::warning(this, tr("No URL entered"), tr("Please type at least one URL.")); return; } - emit urlsReadyToBeDownloaded(uniqueURLs.values()); + emit urlsReadyToBeDownloaded(urlList); accept(); }