Don't overwrite stored layout of main window with incorrect one
[qBittorrent.git] / src / gui / downloadfromurldialog.cpp
blobac073777437feb06b388ec7857978dadbc9be4a5
1 /*
2 * Bittorrent Client using Qt and libtorrent.
3 * Copyright (C) 2006 Christophe Dumez <chris@qbittorrent.org>
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 * In addition, as a special exception, the copyright holders give permission to
20 * link this program with the OpenSSL project's "OpenSSL" library (or with
21 * modified versions of it that use the same license as the "OpenSSL" library),
22 * and distribute the linked executables. You must obey the GNU General Public
23 * License in all respects for all of the code used other than "OpenSSL". If you
24 * modify file(s), you may extend this exception to your version of the file(s),
25 * but you are not obligated to do so. If you do not wish to do so, delete this
26 * exception statement from your version.
29 #include "downloadfromurldialog.h"
31 #include <QClipboard>
32 #include <QKeyEvent>
33 #include <QMessageBox>
34 #include <QPushButton>
35 #include <QRegularExpression>
36 #include <QSet>
37 #include <QString>
38 #include <QStringList>
39 #include <QStringView>
41 #include "base/net/downloadmanager.h"
42 #include "ui_downloadfromurldialog.h"
43 #include "utils.h"
45 #define SETTINGS_KEY(name) u"DownloadFromURLDialog/" name
47 namespace
49 bool isDownloadable(const QString &str)
51 return (Net::DownloadManager::hasSupportedScheme(str)
52 || str.startsWith(u"magnet:", Qt::CaseInsensitive)
53 #ifdef QBT_USES_LIBTORRENT2
54 || ((str.size() == 64) && !str.contains(QRegularExpression(u"[^0-9A-Fa-f]"_s))) // v2 hex-encoded SHA-256 info-hash
55 #endif
56 || ((str.size() == 40) && !str.contains(QRegularExpression(u"[^0-9A-Fa-f]"_s))) // v1 hex-encoded SHA-1 info-hash
57 || ((str.size() == 32) && !str.contains(QRegularExpression(u"[^2-7A-Za-z]"_s)))); // v1 Base32 encoded SHA-1 info-hash
61 DownloadFromURLDialog::DownloadFromURLDialog(QWidget *parent)
62 : QDialog(parent)
63 , m_ui(new Ui::DownloadFromURLDialog)
64 , m_storeDialogSize(SETTINGS_KEY(u"Size"_s))
66 m_ui->setupUi(this);
68 m_ui->buttonBox->button(QDialogButtonBox::Ok)->setText(tr("Download"));
69 connect(m_ui->buttonBox, &QDialogButtonBox::accepted, this, &DownloadFromURLDialog::onSubmit);
70 connect(m_ui->buttonBox, &QDialogButtonBox::rejected, this, &QDialog::reject);
72 m_ui->textUrls->setWordWrapMode(QTextOption::NoWrap);
74 // Paste clipboard if there is an URL in it
75 const QString clipboardText = qApp->clipboard()->text();
76 const QList<QStringView> clipboardList = QStringView(clipboardText).split(u'\n');
78 // show urls in the original order as from the clipboard
79 QStringList urls;
80 urls.reserve(clipboardList.size());
82 for (QStringView url : clipboardList)
84 url = url.trimmed();
86 if (url.isEmpty())
87 continue;
89 if (const QString urlString = url.toString(); isDownloadable(urlString))
90 urls << urlString;
93 if (!urls.isEmpty())
95 m_ui->textUrls->setText(urls.join(u'\n') + u"\n");
96 m_ui->textUrls->moveCursor(QTextCursor::End);
97 m_ui->buttonBox->setFocus();
100 if (const QSize dialogSize = m_storeDialogSize; dialogSize.isValid())
101 resize(dialogSize);
104 DownloadFromURLDialog::~DownloadFromURLDialog()
106 m_storeDialogSize = size();
107 delete m_ui;
110 void DownloadFromURLDialog::onSubmit()
112 const QString plainText = m_ui->textUrls->toPlainText();
113 const QList<QStringView> urls = QStringView(plainText).split(u'\n');
115 // process urls in the original order as from the widget
116 QSet<QStringView> uniqueURLs;
117 QStringList urlList;
118 urlList.reserve(urls.size());
120 for (QStringView url : urls)
122 url = url.trimmed();
124 if (url.isEmpty())
125 continue;
127 if (!uniqueURLs.contains(url))
129 uniqueURLs.insert(url);
130 urlList << url.toString();
134 if (urlList.isEmpty())
136 QMessageBox::warning(this, tr("No URL entered"), tr("Please type at least one URL."));
137 return;
140 emit urlsReadyToBeDownloaded(urlList);
141 accept();
144 void DownloadFromURLDialog::keyPressEvent(QKeyEvent *event)
146 if ((event->modifiers() == Qt::ControlModifier) && (event->key() == Qt::Key_Return))
148 onSubmit();
149 return;
152 QDialog::keyPressEvent(event);