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"
33 #include <QMessageBox>
34 #include <QPushButton>
35 #include <QRegularExpression>
38 #include <QStringList>
39 #include <QStringView>
41 #include "base/net/downloadmanager.h"
42 #include "ui_downloadfromurldialog.h"
45 #define SETTINGS_KEY(name) u"DownloadFromURLDialog/" name
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
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
)
63 , m_ui(new Ui::DownloadFromURLDialog
)
64 , m_storeDialogSize(SETTINGS_KEY(u
"Size"_s
))
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
80 urls
.reserve(clipboardList
.size());
82 for (QStringView url
: clipboardList
)
89 if (const QString urlString
= url
.toString(); isDownloadable(urlString
))
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())
104 DownloadFromURLDialog::~DownloadFromURLDialog()
106 m_storeDialogSize
= size();
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
;
118 urlList
.reserve(urls
.size());
120 for (QStringView url
: urls
)
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."));
140 emit
urlsReadyToBeDownloaded(urlList
);
144 void DownloadFromURLDialog::keyPressEvent(QKeyEvent
*event
)
146 if ((event
->modifiers() == Qt::ControlModifier
) && (event
->key() == Qt::Key_Return
))
152 QDialog::keyPressEvent(event
);