videodownloader/gui/adddownloaddialog.cpp

232 lines
8.6 KiB
C++
Raw Normal View History

2015-09-08 17:05:59 +02:00
#include "./adddownloaddialog.h"
2015-04-22 19:32:04 +02:00
2015-09-08 17:05:59 +02:00
#include "../network/bitsharedownload.h"
#include "../network/filenukedownload.h"
2017-05-01 03:22:50 +02:00
#include "../network/groovesharkdownload.h"
#include "../network/socksharedownload.h"
#include "../network/vimeodownload.h"
#include "../network/youtubedownload.h"
2015-04-22 19:32:04 +02:00
2015-12-05 22:56:32 +01:00
#include "ui_adddownloaddialog.h"
2015-09-08 17:05:59 +02:00
#include <qtutilities/misc/dialogutils.h>
2015-04-22 19:32:04 +02:00
#include <QClipboard>
2017-05-01 03:22:50 +02:00
#include <QInputDialog>
2015-04-22 19:32:04 +02:00
#include <QMessageBox>
2017-05-01 03:22:50 +02:00
#include <QSettings>
2015-04-22 19:32:04 +02:00
2019-06-10 22:50:15 +02:00
using namespace QtUtilities;
2015-04-22 19:32:04 +02:00
using namespace Network;
namespace QtGui {
QStringList AddDownloadDialog::s_knownDownloadTypeNames = QStringList();
2017-05-01 03:22:50 +02:00
AddDownloadDialog::AddDownloadDialog(QWidget *parent)
: QDialog(parent)
, m_ui(new Ui::AddDownloadDialog)
, m_downloadTypeIndex(0)
, m_downloadTypeIndexAdjustedManually(false)
, m_validInput(false)
, m_selectDownloadTypeInputDialog(nullptr)
2015-04-22 19:32:04 +02:00
{
if (s_knownDownloadTypeNames.isEmpty()) {
s_knownDownloadTypeNames << tr("Standard http(s)") << QStringLiteral("Youtube") << QStringLiteral("Vimeo") << QStringLiteral("Grooveshark")
<< QStringLiteral("Sockshare/Putlocker") << QStringLiteral("Bitshare") << QStringLiteral("FileNuke");
}
2015-04-22 19:32:04 +02:00
m_ui->setupUi(this);
makeHeading(m_ui->mainInstructionLabel);
setStyleSheet(dialogStyle());
2015-04-22 19:32:04 +02:00
connect(m_ui->backPushButton, &QPushButton::clicked, this, &AddDownloadDialog::back);
connect(m_ui->addPushButton, &QPushButton::clicked, this, &AddDownloadDialog::setLastUrl);
connect(m_ui->addPushButton, &QPushButton::clicked, this, &AddDownloadDialog::addDownloadClicked);
connect(m_ui->addPushButton, &QPushButton::clicked, this, &AddDownloadDialog::accept);
connect(m_ui->urlLineEdit, &QLineEdit::textChanged, this, &AddDownloadDialog::textChanged);
connect(m_ui->urlLineEdit, &QLineEdit::returnPressed, this, &AddDownloadDialog::returnPressed);
connect(m_ui->adjustDetectedTypePushButton, &QPushButton::clicked, this, &AddDownloadDialog::adjustDetectedDownloadType);
connect(m_ui->insertTextFromClipboardPushButton, &QPushButton::clicked, this, &AddDownloadDialog::insertTextFromClipboard);
2017-05-01 03:22:50 +02:00
QSettings settings(QSettings::IniFormat, QSettings::UserScope, QApplication::organizationName(), QApplication::applicationName());
2015-04-22 19:32:04 +02:00
settings.beginGroup("adddownloaddialog");
m_lastUrl = settings.value("lasturl", QString()).toString();
m_ui->downloadTypeInfoWidget->setHidden(true);
m_ui->urlLineEdit->setText(m_lastUrl);
m_ui->urlLineEdit->setInputMethodHints(Qt::ImhUrlCharactersOnly);
m_ui->urlLineEdit->selectAll();
settings.endGroup();
}
AddDownloadDialog::~AddDownloadDialog()
2017-05-01 03:22:50 +02:00
{
}
2015-04-22 19:32:04 +02:00
Download *AddDownloadDialog::result() const
{
2017-05-01 03:22:50 +02:00
if (!hasValidInput()) {
2015-04-22 19:32:04 +02:00
return nullptr;
}
2015-04-22 19:32:04 +02:00
QString res(m_ui->urlLineEdit->text());
2017-05-01 03:22:50 +02:00
switch (m_downloadTypeIndex) {
2015-04-22 19:32:04 +02:00
case 0:
return new HttpDownload(QUrl(res));
case 1:
return new YoutubeDownload(QUrl(res));
case 2:
2015-10-14 22:58:55 +02:00
return new VimeoDownload(QUrl(res));
2015-04-22 19:32:04 +02:00
case 3:
2015-10-14 22:58:55 +02:00
return new GroovesharkDownload(res);
case 4:
2015-10-14 22:58:55 +02:00
return new SockshareDownload(QUrl(res));
case 5:
2015-10-14 22:58:55 +02:00
return new BitshareDownload(QUrl(res));
case 6:
2015-04-22 19:32:04 +02:00
return new FileNukeDownload(QUrl(res));
default:
return nullptr;
}
}
void AddDownloadDialog::reset()
{
m_downloadTypeIndexAdjustedManually = false;
2017-05-01 03:22:50 +02:00
if (m_ui->urlLineEdit->text() != m_lastUrl) {
2015-04-22 19:32:04 +02:00
m_validInput = false;
m_ui->urlLineEdit->setText(m_lastUrl);
}
}
bool AddDownloadDialog::hasValidInput() const
{
return m_validInput;
}
void AddDownloadDialog::back()
{
setLastUrl();
close();
}
void AddDownloadDialog::returnPressed()
{
2017-05-01 03:22:50 +02:00
if (hasValidInput()) {
2015-04-22 19:32:04 +02:00
setLastUrl();
emit addDownloadClicked();
accept();
}
}
void AddDownloadDialog::insertTextFromClipboard()
{
QClipboard *clipboard = QApplication::clipboard();
QString text = clipboard->text();
2017-05-01 03:22:50 +02:00
if (text.isEmpty()) {
2015-04-22 19:32:04 +02:00
QMessageBox::warning(this, windowTitle(), tr("The clipboard does not contain any text."));
} else {
m_ui->urlLineEdit->setText(text);
}
}
void AddDownloadDialog::textChanged(const QString &text)
{
2017-05-01 03:22:50 +02:00
if (!text.isEmpty()) {
2015-04-22 19:32:04 +02:00
// entered value might be a grooveshark song id
bool isValidGroovesharkId = true;
2017-05-01 03:22:50 +02:00
for (const auto c : text) {
if (!c.isDigit()) {
2015-04-22 19:32:04 +02:00
isValidGroovesharkId = false;
break;
}
2015-10-14 22:58:55 +02:00
}
2017-05-01 03:22:50 +02:00
if (isValidGroovesharkId) {
2015-04-22 19:32:04 +02:00
m_ui->downloadTypeInfoWidget->setHidden(false);
m_ui->addPushButton->setEnabled(true);
m_validInput = true;
2015-10-14 22:58:55 +02:00
m_downloadTypeIndex = 3;
2015-04-22 19:32:04 +02:00
m_ui->downloadTypeLabel->setText(tr("The entered number will be treated as Grooveshark song id."));
return;
}
// parse entered text as url
QUrl url(text);
QString host = url.host();
QString scheme = url.scheme();
2017-05-01 03:22:50 +02:00
if (!(host.isEmpty() || scheme.isEmpty())) {
2015-04-22 19:32:04 +02:00
m_ui->downloadTypeInfoWidget->setHidden(false);
// check if the protocol is supported
2017-05-01 03:22:50 +02:00
if (scheme == QLatin1String("http") || scheme == QLatin1String("https") || scheme == QLatin1String("ftp")) {
2015-04-22 19:32:04 +02:00
m_ui->addPushButton->setEnabled(true);
m_validInput = true;
// detect download type (if not adjusted manually)
2017-05-01 03:22:50 +02:00
if (!m_downloadTypeIndexAdjustedManually) {
2015-04-22 19:32:04 +02:00
m_downloadTypeIndex = 0;
2017-05-01 03:22:50 +02:00
if (host.contains(QLatin1String("youtube"), Qt::CaseInsensitive) || host.startsWith(QLatin1String("youtu.be"))) {
2015-04-22 19:32:04 +02:00
m_downloadTypeIndex = 1;
2017-05-01 03:22:50 +02:00
} else if (host.contains(QLatin1String("vimeo"))) {
2015-10-14 22:58:55 +02:00
m_downloadTypeIndex = 2;
2017-05-01 03:22:50 +02:00
} else if (host.contains(QLatin1String("sockshare")) || host.contains(QLatin1String("putlocker"))) {
2015-04-22 19:32:04 +02:00
m_downloadTypeIndex = 5;
2017-05-01 03:22:50 +02:00
} else if (host.contains(QLatin1String("bitshare"))) {
2015-04-22 19:32:04 +02:00
m_downloadTypeIndex = 6;
2017-05-01 03:22:50 +02:00
} else if (host.contains(QLatin1String("filenuke"))) {
2015-10-14 22:58:55 +02:00
m_downloadTypeIndex = 7;
2015-04-22 19:32:04 +02:00
}
2017-05-01 03:22:50 +02:00
if (m_downloadTypeIndex) {
m_ui->downloadTypeLabel->setText(tr("The entered url seems to be from %1 so it will be added as %1 download.")
.arg(s_knownDownloadTypeNames.at(m_downloadTypeIndex)));
2015-04-22 19:32:04 +02:00
} else {
m_ui->downloadTypeLabel->setText(tr("The entered url will be added as standard %1 download.").arg(scheme));
}
}
} else {
m_ui->addPushButton->setEnabled(false);
m_validInput = false;
m_ui->downloadTypeLabel->setText(tr("The entered protocol is not supported."));
m_downloadTypeIndexAdjustedManually = false;
}
return;
}
}
m_ui->downloadTypeInfoWidget->setHidden(true);
m_ui->addPushButton->setEnabled(false);
m_validInput = false;
m_downloadTypeIndexAdjustedManually = false;
}
void AddDownloadDialog::adjustDetectedDownloadType()
{
2017-05-01 03:22:50 +02:00
if (!m_selectDownloadTypeInputDialog) {
2015-04-22 19:32:04 +02:00
m_selectDownloadTypeInputDialog = new QInputDialog(this);
m_selectDownloadTypeInputDialog->setWindowTitle(tr("Select download type"));
m_selectDownloadTypeInputDialog->setLabelText(tr("Select as which kind of download the entered url should be treated."));
m_selectDownloadTypeInputDialog->setComboBoxItems(s_knownDownloadTypeNames);
2015-04-22 19:32:04 +02:00
m_selectDownloadTypeInputDialog->setComboBoxEditable(false);
m_selectDownloadTypeInputDialog->setOption(QInputDialog::UseListViewForComboBoxItems);
}
m_selectDownloadTypeInputDialog->setTextValue(s_knownDownloadTypeNames.at(m_downloadTypeIndex));
2017-05-01 03:22:50 +02:00
if (m_selectDownloadTypeInputDialog->exec()) {
m_downloadTypeIndex = s_knownDownloadTypeNames.indexOf(m_selectDownloadTypeInputDialog->textValue());
2017-05-01 03:22:50 +02:00
m_ui->downloadTypeLabel->setText(
tr("The entered url will be added as %1 download (adjusted).").arg(s_knownDownloadTypeNames.at(m_downloadTypeIndex)));
2015-04-22 19:32:04 +02:00
m_downloadTypeIndexAdjustedManually = true;
}
}
void AddDownloadDialog::setLastUrl()
{
2017-05-01 03:22:50 +02:00
QSettings settings(QSettings::IniFormat, QSettings::UserScope, QApplication::organizationName(), QApplication::applicationName());
2015-04-22 19:32:04 +02:00
settings.beginGroup("adddownloaddialog");
m_lastUrl = m_ui->urlLineEdit->text();
settings.setValue("lasturl", m_lastUrl);
settings.endGroup();
}
2019-07-20 20:20:58 +02:00
} // namespace QtGui