WebUI: Minor optimizations to the login page
[qBittorrent.git] / src / gui / downloadfromurldialog.cpp
blob183689b9464021595399c516174ca94ceb45c497
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 const QString text = urls.join(u'\n')
94 + (!urls.isEmpty() ? u"\n" : u"");
96 m_ui->textUrls->setText(text);
97 m_ui->textUrls->moveCursor(QTextCursor::End);
99 if (const QSize dialogSize = m_storeDialogSize; dialogSize.isValid())
100 resize(dialogSize);
103 DownloadFromURLDialog::~DownloadFromURLDialog()
105 m_storeDialogSize = size();
106 delete m_ui;
109 void DownloadFromURLDialog::onSubmit()
111 const QString plainText = m_ui->textUrls->toPlainText();
112 const QList<QStringView> urls = QStringView(plainText).split(u'\n');
114 // process urls in the original order as from the widget
115 QSet<QStringView> uniqueURLs;
116 QStringList urlList;
117 urlList.reserve(urls.size());
119 for (QStringView url : urls)
121 url = url.trimmed();
123 if (url.isEmpty())
124 continue;
126 if (!uniqueURLs.contains(url))
128 uniqueURLs.insert(url);
129 urlList << url.toString();
133 if (urlList.isEmpty())
135 QMessageBox::warning(this, tr("No URL entered"), tr("Please type at least one URL."));
136 return;
139 emit urlsReadyToBeDownloaded(urlList);
140 accept();
143 void DownloadFromURLDialog::keyPressEvent(QKeyEvent *event)
145 if ((event->modifiers() == Qt::ControlModifier) && (event->key() == Qt::Key_Return))
147 onSubmit();
148 return;
151 QDialog::keyPressEvent(event);