Sync translations from Transifex and run lupdate
[qBittorrent.git] / src / gui / downloadfromurldialog.cpp
blobbfcdb4c5a42731b56d696b7ef961c0384daaa1b0
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 <QMessageBox>
33 #include <QPushButton>
34 #include <QRegularExpression>
35 #include <QSet>
36 #include <QString>
37 #include <QStringList>
39 #include "ui_downloadfromurldialog.h"
40 #include "utils.h"
42 #define SETTINGS_KEY(name) "DownloadFromURLDialog/" name
44 namespace
46 bool isDownloadable(const QString &str)
48 return (str.startsWith("http://", Qt::CaseInsensitive)
49 || str.startsWith("https://", Qt::CaseInsensitive)
50 || str.startsWith("ftp://", Qt::CaseInsensitive)
51 || str.startsWith("magnet:", Qt::CaseInsensitive)
52 || ((str.size() == 40) && !str.contains(QRegularExpression("[^0-9A-Fa-f]"))) // v1 hex-encoded SHA-1 info-hash
53 #ifdef QBT_USES_LIBTORRENT2
54 || ((str.size() == 64) && !str.contains(QRegularExpression("[^0-9A-Fa-f]"))) // v2 hex-encoded SHA-256 info-hash
55 #endif
56 || ((str.size() == 32) && !str.contains(QRegularExpression("[^2-7A-Za-z]")))); // 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("Size"))
66 m_ui->setupUi(this);
67 setAttribute(Qt::WA_DeleteOnClose);
68 setModal(true);
70 m_ui->buttonBox->button(QDialogButtonBox::Ok)->setText(tr("Download"));
71 connect(m_ui->buttonBox, &QDialogButtonBox::accepted, this, &DownloadFromURLDialog::downloadButtonClicked);
72 connect(m_ui->buttonBox, &QDialogButtonBox::rejected, this, &QDialog::reject);
74 m_ui->textUrls->setWordWrapMode(QTextOption::NoWrap);
76 // Paste clipboard if there is an URL in it
77 const QString clipboardText = qApp->clipboard()->text();
78 const QList<QStringView> clipboardList = QStringView(clipboardText).split(u'\n');
80 QSet<QString> uniqueURLs;
81 for (QStringView strRef : clipboardList)
83 strRef = strRef.trimmed();
84 if (strRef.isEmpty()) continue;
86 const QString str = strRef.toString();
87 if (isDownloadable(str))
88 uniqueURLs << str;
91 const QString text = uniqueURLs.values().join(QLatin1Char('\n'))
92 + (!uniqueURLs.isEmpty() ? QLatin1String("\n") : QLatin1String(""));
94 m_ui->textUrls->setText(text);
95 m_ui->textUrls->moveCursor(QTextCursor::End);
97 Utils::Gui::resize(this, m_storeDialogSize);
98 show();
101 DownloadFromURLDialog::~DownloadFromURLDialog()
103 m_storeDialogSize = size();
104 delete m_ui;
107 void DownloadFromURLDialog::downloadButtonClicked()
109 const QString plainText = m_ui->textUrls->toPlainText();
110 const QList<QStringView> urls = QStringView(plainText).split(u'\n');
112 QSet<QString> uniqueURLs;
113 for (QStringView url : urls)
115 url = url.trimmed();
116 if (url.isEmpty()) continue;
118 uniqueURLs << url.toString();
121 if (uniqueURLs.isEmpty())
123 QMessageBox::warning(this, tr("No URL entered"), tr("Please type at least one URL."));
124 return;
127 emit urlsReadyToBeDownloaded(uniqueURLs.values());
128 accept();