Remove/fix legacy code and style issues

e.g. don't use deprecated APIs
This commit is contained in:
Martchus 2019-12-30 22:27:20 +01:00
parent 94a87052b0
commit 29a80c029b
17 changed files with 93 additions and 309 deletions

View File

@ -37,7 +37,6 @@ set(HEADER_FILES
network/optiondata.h
network/permissionstatus.h
network/socksharedownload.h
network/spotifydownload.h
network/testdownload.h
network/vimeodownload.h
network/youtubedownload.h
@ -63,7 +62,6 @@ set(SRC_FILES
network/misc/contentdispositionparser.cpp
network/optiondata.cpp
network/socksharedownload.cpp
network/spotifydownload.cpp
network/testdownload.cpp
network/vimeodownload.cpp
network/youtubedownload.cpp
@ -105,16 +103,6 @@ set(WIDGETS_UI_FILES
gui/useragentpage.ui
)
#set(QUICK_HEADER_FILES
#)
#set(QUICK_SRC_FILES
#)
#set(TS_FILES
# translations/${META_PROJECT_NAME}_de_DE.ts
# translations/${META_PROJECT_NAME}_en_US.ts
#)
set(ICON_FILES
resources/icons/hicolor/scalable/apps/${META_PROJECT_NAME}.svg
)

View File

@ -35,7 +35,7 @@ void replaceHtmlEntities(QString &text)
QTextDocument textDocument;
textDocument.setHtml(text);
text = textDocument.toPlainText();
foreach (const QChar &c, text) {
for (const QChar &c : text) {
if (!c.isSpace()) {
return;
}

View File

@ -74,7 +74,7 @@ void CliDownloadInteraction::downloadHasSslErrors(Download *download, size_t opt
// TODO
const string downloadName = (download->downloadUrl().isEmpty() ? download->id() : download->downloadUrl().toString()).toStdString();
cout << "The download \"" << downloadName << "\" has SSL errors:" << endl;
foreach (const QSslError &error, sslErrors) {
for (const QSslError &error : sslErrors) {
cout << "- " << error.errorString().toStdString() << ":" << endl;
cout << " " << error.certificate().toText().toStdString() << endl;
}

View File

@ -6,9 +6,6 @@
#include "../network/socksharedownload.h"
#include "../network/vimeodownload.h"
#include "../network/youtubedownload.h"
#ifdef UNDER_CONSTRUCTION
#include "../network/spotifydownload.h"
#endif
#include "ui_adddownloaddialog.h"
@ -24,13 +21,7 @@ using namespace Network;
namespace QtGui {
QStringList AddDownloadDialog::m_knownDownloadTypeNames = QStringList()
<< QStringLiteral("standard http(s)/ftp") << QStringLiteral("Youtube") << QStringLiteral("Vimeo") << QStringLiteral("Grooveshark")
<< QStringLiteral("Sockshare/Putlocker") << QStringLiteral("Bitshare") << QStringLiteral("FileNuke")
#ifdef UNDER_CONSTRUCTION
<< QStringLiteral("Spotify")
#endif
;
QStringList AddDownloadDialog::s_knownDownloadTypeNames = QStringList();
AddDownloadDialog::AddDownloadDialog(QWidget *parent)
: QDialog(parent)
@ -40,6 +31,11 @@ AddDownloadDialog::AddDownloadDialog(QWidget *parent)
, m_validInput(false)
, m_selectDownloadTypeInputDialog(nullptr)
{
if (s_knownDownloadTypeNames.isEmpty()) {
s_knownDownloadTypeNames << tr("Standard http(s)") << QStringLiteral("Youtube") << QStringLiteral("Vimeo") << QStringLiteral("Grooveshark")
<< QStringLiteral("Sockshare/Putlocker") << QStringLiteral("Bitshare") << QStringLiteral("FileNuke");
}
m_ui->setupUi(this);
makeHeading(m_ui->mainInstructionLabel);
setStyleSheet(dialogStyle());
@ -89,10 +85,6 @@ Download *AddDownloadDialog::result() const
return new BitshareDownload(QUrl(res));
case 6:
return new FileNukeDownload(QUrl(res));
#ifdef UNDER_CONSTRUCTION
case 7:
return new SpotifyDownload(res);
#endif
default:
return nullptr;
}
@ -187,7 +179,7 @@ void AddDownloadDialog::textChanged(const QString &text)
}
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(m_knownDownloadTypeNames.at(m_downloadTypeIndex)));
.arg(s_knownDownloadTypeNames.at(m_downloadTypeIndex)));
} else {
m_ui->downloadTypeLabel->setText(tr("The entered url will be added as standard %1 download.").arg(scheme));
}
@ -214,16 +206,16 @@ void AddDownloadDialog::adjustDetectedDownloadType()
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(m_knownDownloadTypeNames);
m_selectDownloadTypeInputDialog->setComboBoxItems(s_knownDownloadTypeNames);
m_selectDownloadTypeInputDialog->setComboBoxEditable(false);
m_selectDownloadTypeInputDialog->setOption(QInputDialog::UseListViewForComboBoxItems);
}
m_selectDownloadTypeInputDialog->setTextValue(m_knownDownloadTypeNames.at(m_downloadTypeIndex));
m_selectDownloadTypeInputDialog->setTextValue(s_knownDownloadTypeNames.at(m_downloadTypeIndex));
if (m_selectDownloadTypeInputDialog->exec()) {
m_downloadTypeIndex = m_knownDownloadTypeNames.indexOf(m_selectDownloadTypeInputDialog->textValue());
m_downloadTypeIndex = s_knownDownloadTypeNames.indexOf(m_selectDownloadTypeInputDialog->textValue());
m_ui->downloadTypeLabel->setText(
tr("The entered url will be added as %1 download (adjusted).").arg(m_knownDownloadTypeNames.at(m_downloadTypeIndex)));
tr("The entered url will be added as %1 download (adjusted).").arg(s_knownDownloadTypeNames.at(m_downloadTypeIndex)));
m_downloadTypeIndexAdjustedManually = true;
}
}

View File

@ -40,7 +40,7 @@ private slots:
private:
std::unique_ptr<Ui::AddDownloadDialog> m_ui;
static QStringList m_knownDownloadTypeNames;
static QStringList s_knownDownloadTypeNames;
int m_downloadTypeIndex;
bool m_downloadTypeIndexAdjustedManually;
bool m_validInput;

View File

@ -104,13 +104,12 @@ void AddMultipleDownloadsEnterSearchTermPage::initializePage()
if (AddMultipleDownloadsWizard *w = qobject_cast<AddMultipleDownloadsWizard *>(wizard())) {
source = w->source();
}
// source = downloadSourceFromField(field(QStringLiteral("source"))); // does not work for some reason
switch (source) {
case DownloadSource::WebpageLinks:
setTitle(tr("Specify the webpage"));
setSubTitle(tr("Enter the URL."));
setEnabled(true);
m_searchTermLineEdit->setPlaceholderText(QStringLiteral("e. g. http://localhost"));
m_searchTermLineEdit->setPlaceholderText(QStringLiteral("URL"));
m_byIdCheckBox->setHidden(true);
m_verifiedOnlyCheckBox->setHidden(true);
break;
@ -118,7 +117,7 @@ void AddMultipleDownloadsEnterSearchTermPage::initializePage()
setTitle(tr("Specify YouTube playlist"));
setSubTitle(tr("Enter the playlist URL or ID."));
setEnabled(true);
m_searchTermLineEdit->setPlaceholderText(QStringLiteral("e. g. https://www.youtube.com/playlist?list=PLg2u737x4TjfLFFEbEK9p77V2NRcLcTkr"));
m_searchTermLineEdit->setPlaceholderText(QStringLiteral("URL"));
m_byIdCheckBox->setHidden(false);
m_verifiedOnlyCheckBox->setHidden(true);
break;
@ -253,7 +252,7 @@ QList<Download *> AddMultipleDownloadsResultsPage::results() const
if (m_finder) {
QModelIndexList selectedRows = m_view->selectionModel()->selectedRows();
const QList<Download *> &allResults = m_finder->results();
foreach (const QModelIndex &index, selectedRows) {
for (const QModelIndex &index : selectedRows) {
if (index.row() < allResults.size()) {
results << allResults.at(index.row());
}

View File

@ -215,7 +215,7 @@ void DownloadInteraction::downloadHasSslErrors(Download *download, size_t option
{
QString downloadName = download->downloadUrl().isEmpty() ? download->id() : download->downloadUrl().toString();
QString details;
foreach (const QSslError &error, sslErrors) {
for (const QSslError &error : sslErrors) {
if (!details.isEmpty()) {
details.append(QStringLiteral("\n\n"));
}

View File

@ -69,7 +69,6 @@ MainWindow::MainWindow(QWidget *parent)
, m_initiatingDownloads(0)
, m_totalSpeed(0)
, m_stillToReceive(0)
, m_remainingTime(TimeSpan())
, m_downloadInteraction(new DownloadInteraction(this))
, m_addDownloadDlg(nullptr)
, m_addMultipleDownloadsWizard(nullptr)
@ -114,7 +113,7 @@ MainWindow::MainWindow(QWidget *parent)
m_autoSpinBox->setMinimumSize(QSize(245, 0));
m_autoSpinBox->setMaximum(10);
m_autoSpinBox->setSuffix(tr(" download(s) automatically"));
m_autoSpinBox->setPrefix(tr("start up to "));
m_autoSpinBox->setPrefix(tr("Start up to "));
QWidget *spacer = new QWidget(m_ui->autoToolBar);
spacer->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);
spacer->setVisible(true);
@ -159,18 +158,18 @@ MainWindow::MainWindow(QWidget *parent)
m_ui->statusBar->addWidget(m_downloadStatusLabel);
m_elapsedTime.start();
// Connect signals and slots
// Application
// connect signals and slots
// application
connect(m_ui->actionSettings, &QAction::triggered, this, &MainWindow::showSettingsDialog);
connect(m_ui->actionQuit, &QAction::triggered, &QApplication::quit);
// Add
// add
connect(addDownloadToolButton, &QToolButton::clicked, this, &MainWindow::showAddDownloadDialog);
connect(m_ui->actionAdd_Download, &QAction::triggered, this, &MainWindow::showAddDownloadDialog);
connect(m_ui->actionAdd_multiple_downloads, &QAction::triggered, this, &MainWindow::showAddMultipleDownloadsDialog);
#ifdef CONFIG_TESTDOWNLOAD
connect(action, &QAction::triggered, [this] { this->addDownload(new TestDownload()); });
#endif
// Downloads
// downloads
connect(m_ui->actionStart_selected, &QAction::triggered, this, &MainWindow::startOrStopSelectedDownloads);
connect(m_ui->actionResume_selected_downloads, &QAction::triggered, this, &MainWindow::interruptOrResumeSelectedDownloads);
connect(m_ui->actionRemove_selected_downloads_from_list, &QAction::triggered, this, &MainWindow::removeSelectedDownloads);
@ -196,7 +195,8 @@ MainWindow::~MainWindow()
void MainWindow::showAboutDialog()
{
if (!m_aboutDlg) {
m_aboutDlg = new AboutDialog(this, QStringLiteral(APP_URL), tr("A video downloader with Qt GUI (currently only YouTube is maintained)."),
m_aboutDlg = new AboutDialog(this, QStringLiteral(APP_URL),
tr("Simple video downloader with Qt GUI and backends for multiple platforms, e.g. YouTube and Vimeo."),
QImage(QStringLiteral(":/icons/hicolor/128x128/apps/videodownloader.png")));
}
if (m_aboutDlg->isHidden()) {
@ -267,7 +267,7 @@ void MainWindow::addMultipleDownloadsWizardResults()
show();
}
if (m_addMultipleDownloadsWizard) {
foreach (Download *res, m_addMultipleDownloadsWizard->results()) {
for (Download *const res : m_addMultipleDownloadsWizard->results()) {
addDownload(res);
}
m_addMultipleDownloadsWizard->restart();
@ -331,7 +331,7 @@ void MainWindow::startOrStopSelectedDownloads()
return;
}
foreach (Download *download, selectedDownloads) {
for (Download *const download : selectedDownloads) {
switch (download->status()) {
case DownloadStatus::None:
// retrieve initial information when that still has to be done
@ -397,7 +397,7 @@ void MainWindow::interruptOrResumeSelectedDownloads()
return;
}
foreach (Download *download, selectedDownloads) {
for (Download *const download : selectedDownloads) {
switch (download->status()) {
case DownloadStatus::Downloading:
download->interrupt();
@ -431,9 +431,10 @@ void MainWindow::removeSelectedDownloads()
int removed = 0;
QModelIndexList selectedIndexes = m_ui->downloadsTreeView->selectionModel()->selectedRows();
QList<QPersistentModelIndex> persistendIndexes;
foreach (QModelIndex selectedIndex, selectedIndexes)
for (const QModelIndex &selectedIndex : selectedIndexes) {
persistendIndexes << QPersistentModelIndex(selectedIndex);
foreach (QPersistentModelIndex selectedIndex, persistendIndexes) {
}
for (const QPersistentModelIndex &selectedIndex : persistendIndexes) {
if (Download *download = m_model->download(selectedIndex)) {
switch (download->status()) {
case DownloadStatus::Downloading:
@ -477,7 +478,7 @@ void MainWindow::updateStartStopControls()
int withTargetPath = 0;
bool downloadsSelected = !selectedDownloads.isEmpty();
if (downloadsSelected) {
foreach (Download *download, selectedDownloads) {
for (Download *const download : selectedDownloads) {
switch (download->status()) {
case DownloadStatus::None:
++toInit;
@ -668,7 +669,7 @@ void MainWindow::clipboardDataChanged()
if (!m_internalClipboardChange && m_superviseClipboardToolButton->isChecked()) {
QString data = QApplication::clipboard()->text();
QStringList lines = data.split(QChar('\n'), QString::SkipEmptyParts);
foreach (QString line, lines) {
for (const QString &line : lines) {
if (Download *download = Download::fromUrl(line)) {
addDownload(download);
if (m_trayIcon && m_trayIcon->isVisible()) {
@ -733,7 +734,7 @@ void MainWindow::clearTargetPath()
{
QList<Download *> downloads = selectedDownloads();
if (!downloads.isEmpty()) {
foreach (Download *download, downloads) {
for (Download *const download : downloads) {
download->setTargetPath(QString());
}
updateStartStopControls();
@ -744,7 +745,7 @@ void MainWindow::clearTargetPath()
void MainWindow::showYoutubeItagsInfo()
{
QDesktopServices::openUrl(QUrl(QStringLiteral("https://en.wikipedia.org/wiki/YouTube#Quality_and_formats")));
QDesktopServices::openUrl(QUrl(QStringLiteral("https://gist.github.com/sidneys/7095afe4da4ae58694d128b1034e01e2")));
}
void MainWindow::resetGroovesharkSession()
@ -882,7 +883,7 @@ QList<Download *> MainWindow::selectedDownloads() const
{
QList<Download *> res;
QModelIndexList selectedRows = m_ui->downloadsTreeView->selectionModel()->selectedRows();
foreach (QModelIndex index, selectedRows) {
for (const QModelIndex &index : selectedRows) {
if (Download *download = m_model->download(index)) {
res << download;
}

View File

@ -3,10 +3,10 @@
#include <c++utilities/chrono/timespan.h>
#include <QElapsedTimer>
#include <QList>
#include <QMainWindow>
#include <QSystemTrayIcon>
#include <QTime>
#include <memory>
@ -112,7 +112,7 @@ private:
double m_totalSpeed;
qint64 m_stillToReceive;
CppUtilities::TimeSpan m_remainingTime;
QTime m_elapsedTime;
QElapsedTimer m_elapsedTime;
DownloadInteraction *m_downloadInteraction;
AddDownloadDialog *m_addDownloadDlg;
AddMultipleDownloadsWizard *m_addMultipleDownloadsWizard;

View File

@ -45,7 +45,7 @@ void DownloadFinderResultsModel::setFinder(DownloadFinder *finder)
});
// new results are available
connect(m_finder, &DownloadFinder::newResultsAvailable, [this](const QList<Download *> &results) {
foreach (Download *download, results) {
for (Download *const download : results) {
connect(download, &Download::statusChanged, this, &DownloadFinderResultsModel::downloadChangedStatus);
// initiate the download if not done yet and instant initiation is recommendable
if (!download->isInitiated() && download->isInitiatingInstantlyRecommendable()) {

View File

@ -71,7 +71,7 @@ namespace Network {
* using the provideAuthenticationCredentials() method.
*/
int Download::m_defaultUserAgent = -1;
int Download::s_defaultUserAgent = -1;
/*!
* \brief Returns a random default user agent string.
@ -91,10 +91,10 @@ const QString &Download::defaultUserAgent()
QStringLiteral("Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2062.120 Safari/537.36"),
QStringLiteral("Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/534.34 (KHTML, like Gecko) konqueror/4.14.0 Safari/534.34"),
QStringLiteral("Opera/9.80 (Windows NT 6.0) Presto/2.12.388 Version/12.14") };
if (m_defaultUserAgent < 0 || m_defaultUserAgent > 6) {
if (s_defaultUserAgent < 0 || s_defaultUserAgent > 6) {
scrambleDefaultUserAgent();
}
return agents[m_defaultUserAgent];
return agents[s_defaultUserAgent];
}
/*!
@ -106,7 +106,7 @@ void Download::scrambleDefaultUserAgent()
random_device rd;
default_random_engine engine(rd());
uniform_int_distribution<int> distribution(0, 6);
m_defaultUserAgent = distribution(engine);
s_defaultUserAgent = distribution(engine);
}
/*!
@ -369,9 +369,6 @@ Download::Download(const QUrl &url, QObject *parent)
, m_newBytesToReceive(0)
, m_speed(0.0)
, m_shiftSpeed(0.0)
, m_shiftRemainingTime(TimeSpan())
, m_statusInfo(QString())
, m_time(QTime())
, m_networkError(QNetworkReply::NoError)
, m_initiated(false)
, m_progressUpdateInterval(300)
@ -387,7 +384,7 @@ Download::Download(const QUrl &url, QObject *parent)
*
* Needs to be implemented when subclassing.
*/
bool Download::followRedirection(size_t)
bool Download::followRedirection(std::size_t)
{
return false;
}

View File

@ -8,11 +8,11 @@
#include <QAuthenticator>
#include <QBuffer>
#include <QElapsedTimer>
#include <QFile>
#include <QNetworkProxy>
#include <QNetworkReply>
#include <QObject>
#include <QTime>
#include <tuple>
@ -78,20 +78,20 @@ public:
void setTargetPath(const QString &targetPath);
const QUrl &initialUrl() const;
const QUrl &downloadUrl() const;
const QUrl &downloadUrl(size_t optionIndex) const;
const QUrl &downloadUrl(std::size_t optionIndex) const;
const std::vector<OptionData> &options() const;
std::vector<OptionData> &options();
const QString &id() const;
const QString &title() const;
const QString &collectionName() const;
size_t availableOptionCount() const;
std::size_t availableOptionCount() const;
bool areOptionsAvailable() const;
const QString &optionName(size_t optionIndex) const;
const QString &optionName(std::size_t optionIndex) const;
bool haveAvailableOptionsChanged();
bool isValidOptionChosen() const;
size_t chosenOption() const;
std::size_t chosenOption() const;
const QString &chosenOptionName() const;
bool setChosenOption(size_t optionIndex);
bool setChosenOption(std::size_t optionIndex);
bool hasSelectedOptionChanged();
const QString &userAgent();
void setCustomUserAgent(const QString &value);
@ -116,12 +116,12 @@ public:
void provideMetaData(const QString &title = QString(), const QString &uploader = QString(),
CppUtilities::TimeSpan duration = CppUtilities::TimeSpan(), const QString &collectionName = QString(), int positionInCollection = 0,
int views = 0, const QString &rating = QString());
void setOverwritePermission(size_t optionIndex, PermissionStatus permission);
void setAppendPermission(size_t optionIndex, PermissionStatus permission);
void setRedirectPermission(size_t originalOptionIndex, PermissionStatus permission);
void setIgnoreSslErrorsPermission(size_t optionIndex, PermissionStatus permission);
void provideOutputDevice(size_t optionIndex, QIODevice *device, bool giveOwnership = false);
void provideAuthenticationCredentials(size_t optionIndex, const AuthenticationCredentials &credentials);
void setOverwritePermission(std::size_t optionIndex, PermissionStatus permission);
void setAppendPermission(std::size_t optionIndex, PermissionStatus permission);
void setRedirectPermission(std::size_t originalOptionIndex, PermissionStatus permission);
void setIgnoreSslErrorsPermission(std::size_t optionIndex, PermissionStatus permission);
void provideOutputDevice(std::size_t optionIndex, QIODevice *device, bool giveOwnership = false);
void provideAuthenticationCredentials(std::size_t optionIndex, const AuthenticationCredentials &credentials);
const AuthenticationCredentials &initialAuthenticationCredentials() const;
AuthenticationCredentials &initialAuthenticationCredentials();
virtual bool isInitiatingInstantlyRecommendable() const;
@ -133,12 +133,12 @@ signals:
void statusChanged(Download *download);
void progressChanged(Download *download);
void statusInfoChanged(Download *download);
void overwriteingPermissionRequired(Download *download, size_t optionIndex, const QString &file);
void appendingPermissionRequired(Download *download, size_t optionIndex, const QString &file, quint64 offset, quint64 fileSize);
void redirectionPermissonRequired(Download *download, size_t optionIndex, int redirectionOptionIndex);
void outputDeviceRequired(Download *concerningDownload, size_t optionIndex);
void authenticationRequired(Download *concerningDownload, size_t optionIndex, const QString &statusInfo);
void sslErrors(Download *concerningDownload, size_t optionIndex, const QList<QSslError> &sslErrors);
void overwriteingPermissionRequired(Download *download, std::size_t optionIndex, const QString &file);
void appendingPermissionRequired(Download *download, std::size_t optionIndex, const QString &file, quint64 offset, quint64 fileSize);
void redirectionPermissonRequired(Download *download, std::size_t optionIndex, int redirectionOptionIndex);
void outputDeviceRequired(Download *concerningDownload, std::size_t optionIndex);
void authenticationRequired(Download *concerningDownload, std::size_t optionIndex, const QString &statusInfo);
void sslErrors(Download *concerningDownload, std::size_t optionIndex, const QList<QSslError> &sslErrors);
protected:
// c'tor
@ -147,25 +147,25 @@ protected:
// protected methods
// to be implemented by derived classes
virtual void doDownload() = 0;
virtual bool followRedirection(size_t originalOptionIndex);
virtual bool followRedirection(std::size_t originalOptionIndex);
virtual void abortDownload() = 0;
virtual void doInit() = 0;
virtual void checkStatusAndClear(size_t optionIndex) = 0;
virtual void checkStatusAndClear(std::size_t optionIndex) = 0;
// meant to be called by derived classes
size_t addDownloadUrl(const QString &optionName, const QUrl &url, size_t redirectionOf = InvalidOptionIndex);
void changeDownloadUrl(size_t optionIndex, const QUrl &value);
std::size_t addDownloadUrl(const QString &optionName, const QUrl &url, std::size_t redirectionOf = InvalidOptionIndex);
void changeDownloadUrl(std::size_t optionIndex, const QUrl &value);
void reportInitiated(
bool success, const QString &reasonIfNot = QString(), const QNetworkReply::NetworkError &networkError = QNetworkReply::NoError);
void reportFinalDownloadStatus(size_t optionIndex, bool success, const QString &statusDescription = QString(),
void reportFinalDownloadStatus(std::size_t optionIndex, bool success, const QString &statusDescription = QString(),
QNetworkReply::NetworkError networkError = QNetworkReply::NoError);
protected slots:
void reportDownloadInterrupted(size_t optionIndex);
void reportNewDataToBeWritten(size_t optionIndex, QIODevice *inputDevice);
void reportRedirectionAvailable(size_t originalOptionIndex);
void reportAuthenticationRequired(size_t optionIndex, const QString &realm);
void reportSslErrors(size_t optionIndex, QNetworkReply *reply, const QList<QSslError> &sslErrors);
void reportDownloadComplete(size_t optionIndex);
void finalizeOutputDevice(size_t optionIndex);
void reportDownloadInterrupted(std::size_t optionIndex);
void reportNewDataToBeWritten(std::size_t optionIndex, QIODevice *inputDevice);
void reportRedirectionAvailable(std::size_t originalOptionIndex);
void reportAuthenticationRequired(std::size_t optionIndex, const QString &realm);
void reportSslErrors(std::size_t optionIndex, QNetworkReply *reply, const QList<QSslError> &sslErrors);
void reportDownloadComplete(std::size_t optionIndex);
void finalizeOutputDevice(std::size_t optionIndex);
void setId(const QString &value);
void setTitle(const QString &value);
void setTitleFromFilename(const QString &valueObtainedFromFilename);
@ -175,7 +175,7 @@ protected slots:
void setRating(const QString &value);
void setPositionInCollection(int value);
void setCollectionName(const QString &value);
void reportDownloadProgressUpdate(size_t optionIndex, qint64 bytesReceived, qint64 bytesToReceive);
void reportDownloadProgressUpdate(std::size_t optionIndex, qint64 bytesReceived, qint64 bytesToReceive);
private:
// private static methods
@ -184,9 +184,9 @@ private:
// private methods
// to handle the output device
bool writeBufferToOutputDevice(size_t optionIndex);
bool prepareOutputDevice(size_t optionIndex, QIODevice *device, bool takeOwnership);
void ensureOutputDeviceIsPrepared(size_t optionIndex);
bool writeBufferToOutputDevice(std::size_t optionIndex);
bool prepareOutputDevice(std::size_t optionIndex, QIODevice *device, bool takeOwnership);
void ensureOutputDeviceIsPrepared(std::size_t optionIndex);
// to set status information
void setBytesWritten(qint64 value);
void setProgress(qint64 m_bytesReceived = -1, qint64 m_bytesToReceive = -1);
@ -211,7 +211,7 @@ private:
// concerning available options
std::vector<OptionData> m_optionData;
size_t m_chosenOption;
std::size_t m_chosenOption;
bool m_selectedOptionChanged;
bool m_availableOptionsChanged;
@ -226,7 +226,7 @@ private:
double m_shiftSpeed;
CppUtilities::TimeSpan m_shiftRemainingTime;
QString m_statusInfo;
QTime m_time;
QElapsedTimer m_time;
QNetworkReply::NetworkError m_networkError;
bool m_initiated;
int m_progressUpdateInterval;
@ -234,7 +234,7 @@ private:
// concerning download
bool m_useDefaultUserAgent;
QString m_userAgent;
static int m_defaultUserAgent;
static int s_defaultUserAgent;
QNetworkProxy m_proxy;
QString m_targetPath;
DownloadRange m_range;
@ -287,7 +287,7 @@ inline const QUrl &Download::downloadUrl() const
* - Some downloads might use additional information (for instance if the POST method is used).
* - This information is possibly not available before the download is initiated.
*/
inline const QUrl &Download::downloadUrl(size_t optionIndex) const
inline const QUrl &Download::downloadUrl(std::size_t optionIndex) const
{
return m_optionData.at(optionIndex).m_url;
}
@ -348,7 +348,7 @@ inline const QString &Download::collectionName() const
* \remarks This information is possibly not available before the download is initiated.
* \sa availableOption()
*/
inline size_t Download::availableOptionCount() const
inline std::size_t Download::availableOptionCount() const
{
return m_optionData.size();
}
@ -372,7 +372,7 @@ inline bool Download::areOptionsAvailable() const
* \remarks This information is possibly not available before the download is initiated.
* \sa availableOptionCount()
*/
inline const QString &Download::optionName(size_t optionIndex) const
inline const QString &Download::optionName(std::size_t optionIndex) const
{
if (optionIndex < m_optionData.size()) {
return m_optionData.at(optionIndex).m_name;
@ -405,7 +405,7 @@ inline bool Download::isValidOptionChosen() const
*
* The index might be invalid. Check isValidOptionChosen() before.
*/
inline size_t Download::chosenOption() const
inline std::size_t Download::chosenOption() const
{
return m_chosenOption;
}

View File

@ -132,7 +132,7 @@ DownloadFinder::ParsingResult GroovesharkSearcher::parseResults(const QByteArray
QJsonObject itemObj;
QJsonValue idVal;
QJsonValue verifiedVal;
foreach (QJsonValue itemVal, collectionArray) {
for (const QJsonValue &itemVal : collectionArray) {
if (itemVal.isObject()) {
itemObj = itemVal.toObject();
if (m_verified) { // skip unverified items if verified option used
@ -184,7 +184,7 @@ DownloadFinder::ParsingResult GroovesharkSearcher::parseResults(const QByteArray
QJsonValue titleVal;
QJsonValue durationVal;
QJsonValue trackNumVal;
foreach (QJsonValue songVal, songsArray) {
for (const QJsonValue songVal : songsArray) {
if (songVal.isObject()) {
songObj = songVal.toObject();
idVal = songObj.value(QStringLiteral("SongID"));

View File

@ -51,7 +51,7 @@ void ContentDispositionParser::pharse()
PharsingPosition pos = FieldName;
QString fieldName;
QString value;
foreach (QChar c, m_contentDisposition) {
for (QChar c : m_contentDisposition) {
if (c == '\"') {
inQuotationMarks = !inQuotationMarks;
} else if (c == ';') {

View File

@ -1,153 +0,0 @@
#include "./spotifydownload.h"
#include "../application/utils.h"
#include <QApplication>
#include <QClipboard>
#include <QJsonArray>
#include <QJsonDocument>
#include <QJsonObject>
#include <QJsonParseError>
#include <QUrlQuery>
using namespace Application;
namespace Network {
const QUrl SpotifyDownload::m_spotifyUrl = QUrl(QStringLiteral("https://play.spotify.com/"));
bool SpotifyDownload::m_authenticationCredentialsValidated = false;
QString SpotifyDownload::m_csrftoken = QString();
QString SpotifyDownload::m_trackingId = QString();
QString SpotifyDownload::m_referrer = QString();
QString SpotifyDownload::m_creationFlow = QString();
QStringList SpotifyDownload::m_supportedUseragents = QStringList()
<< QStringLiteral("Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:23.0) Gecko/20100101 Firefox/23.0");
/*!
* \class SpotifyDownload
* \brief Download implementation for Spotify songs.
* \remarks Under construction (not even rudimental finished).
*/
/*!
* \brief Constructs a new SpotifyDownload for the specified \a songid.
*/
SpotifyDownload::SpotifyDownload(const QString &songid, QObject *parent)
: HttpDownloadWithInfoRequst(QUrl(), parent)
, m_currentRequest(SpotifyRequestType::Invalid)
, m_triesToGetAuthenticationData(0)
{
setId(songid);
}
QString SpotifyDownload::typeName() const
{
return tr("Spotify");
}
void SpotifyDownload::resetSession()
{
m_authenticationCredentialsValidated = false;
m_csrftoken.clear();
m_trackingId.clear();
m_referrer.clear();
m_creationFlow.clear();
}
Download *SpotifyDownload::infoRequestDownload(bool &success, QString &reasonForFail)
{
HttpDownload *download = nullptr;
if (m_csrftoken.isEmpty() || m_trackingId.isEmpty() /* || creationFlow.isEmpty()*/) {
if (m_triesToGetAuthenticationData < 2) {
download = new HttpDownload(m_spotifyUrl);
download->setCustomUserAgent(m_supportedUseragents.at(0));
success = true;
++m_triesToGetAuthenticationData;
m_currentRequest = SpotifyRequestType::GetAuthenticationData;
} else {
reasonForFail = tr("Unable to find data required for autentication.");
success = false;
}
} else if (!m_authenticationCredentialsValidated) {
if (initialAuthenticationCredentials().isIncomplete()) {
reportAuthenticationRequired(
-1, tr("To download songs from Spotify authentication is required so you have to enter the credentials of your Spotify account."));
reasonForFail = tr("Authentication credentials not given.");
//networkError = QNetworkReply::AuthenticationRequiredError;
success = false;
} else {
QUrl url(m_spotifyUrl);
url.setPath(QStringLiteral("/xhr/json/auth.php"));
download = new HttpDownload(url);
QByteArray postData;
QUrlQuery query;
query.addQueryItem(QStringLiteral("type"), QStringLiteral("sp"));
query.addQueryItem(QStringLiteral("username"), initialAuthenticationCredentials().userName());
query.addQueryItem(QStringLiteral("password"), initialAuthenticationCredentials().password());
query.addQueryItem(QStringLiteral("secret"), m_csrftoken);
query.addQueryItem(QStringLiteral("trackingId"), m_trackingId);
query.addQueryItem(QStringLiteral("landingURL"), m_spotifyUrl.toString());
query.addQueryItem(QStringLiteral("cf"), m_creationFlow);
postData.append(query.toString(QUrl::FullyEncoded));
download->setMethod(HttpDownloadMethod::Post);
download->setPostData(postData);
download->setCustomUserAgent(m_supportedUseragents.at(0));
success = true;
m_currentRequest = SpotifyRequestType::Authenticate;
}
}
return download;
}
void SpotifyDownload::evalVideoInformation(Download *, QBuffer *videoInfoBuffer)
{
if (m_currentRequest == SpotifyRequestType::Invalid)
reportInitiated(false, tr("Interal error (current request type not set)."));
else {
switch (m_currentRequest) {
case SpotifyRequestType::GetAuthenticationData: {
QString responseData(videoInfoBuffer->readAll());
substring(responseData, m_csrftoken, 0, QStringLiteral("\"csrftoken\":\""), QStringLiteral("\""));
substring(responseData, m_trackingId, 0, QStringLiteral("\"trackingId\":\""), QStringLiteral("\""));
substring(responseData, m_creationFlow, 0, QStringLiteral("\"creationFlow\":\""), QStringLiteral("\""));
substring(responseData, m_referrer, 0, QStringLiteral("\"referrer\":\""), QStringLiteral("\""));
m_currentRequest = SpotifyRequestType::Invalid;
doInit();
break;
}
case SpotifyRequestType::Authenticate: {
QByteArray responseData(videoInfoBuffer->readAll());
QApplication::clipboard()->setText(QString(responseData));
QJsonParseError error;
QJsonDocument document = QJsonDocument::fromJson(responseData, &error);
if (error.error == QJsonParseError::NoError) {
QJsonObject obj = document.object();
QJsonValue statusVal = obj.value(QStringLiteral("status"));
if (statusVal.toString().compare(QLatin1String("ok"), Qt::CaseInsensitive) == 0) {
m_authenticationCredentialsValidated = true;
reportInitiated(true);
} else {
QString error = obj.value(QStringLiteral("error")).toString();
QString message;
if (error.isEmpty()) {
message = tr("Authentication failed. Spotify returned no error message.");
} else if (error.compare(QLatin1String("invalid_credentials"), Qt::CaseInsensitive) == 0) {
message = tr("Authentication failed because the entered credentials are invalid.");
} else {
message = tr("Authentication failed. Error message returned by Spotify: %1").arg(error);
}
reportInitiated(false, message);
}
} else {
reportInitiated(false,
tr("Authentication failed because the response by Spotify is no valid Json document (parse error: %1).")
.arg(error.errorString()));
}
break;
}
case SpotifyRequestType::Invalid:;
}
}
}
} // namespace Network

View File

@ -1,40 +0,0 @@
#ifndef SPOTIFYDOWNLOAD_H
#define SPOTIFYDOWNLOAD_H
#include "./httpdownloadwithinforequst.h"
namespace Network {
enum class SpotifyRequestType { Invalid, GetAuthenticationData, Authenticate };
class SpotifyDownload : public HttpDownloadWithInfoRequst {
Q_OBJECT
public:
explicit SpotifyDownload(const QString &songid, QObject *parent = nullptr);
Download *infoRequestDownload(bool &success, QString &reasonForFail);
QString typeName() const;
static void resetSession();
protected:
void evalVideoInformation(Download *, QBuffer *videoInfoBuffer);
private:
// static fields
static const QUrl m_spotifyUrl;
static bool m_authenticationCredentialsValidated;
static QString m_csrftoken;
static QString m_trackingId;
static QString m_referrer;
static QString m_creationFlow;
static QStringList m_supportedUseragents;
// fields
SpotifyRequestType m_currentRequest;
int m_triesToGetAuthenticationData;
};
} // namespace Network
#endif // SPOTIFYDOWNLOAD_H

View File

@ -63,7 +63,7 @@ void YoutubeDownload::evalVideoInformation(Download *, QBuffer *videoInfoBuffer)
}
QString videoInfo(videoInfoBuffer->readAll());
QStringList completeFields = videoInfo.split(QChar('&'), QString::SkipEmptyParts, Qt::CaseSensitive);
foreach (QString completeField, completeFields) {
for (const QString &completeField : completeFields) {
QStringList fieldParts = completeField.split(QChar('='), QString::SkipEmptyParts, Qt::CaseSensitive);
if (fieldParts.count() < 2) {
continue;
@ -90,15 +90,15 @@ void YoutubeDownload::evalVideoInformation(Download *, QBuffer *videoInfoBuffer)
setRating(rating);
}
QStringList fmtFieldIds = QStringList() << QStringLiteral("url_encoded_fmt_stream_map") << QStringLiteral("adaptive_fmts");
foreach (const QString &fmtFieldId, fmtFieldIds) {
for (const QString &fmtFieldId : fmtFieldIds) {
QString fmtField = m_fields.value(fmtFieldId, QString());
if (!fmtField.isEmpty()) {
QStringList sections = fmtField.split(QChar(','), QString::SkipEmptyParts, Qt::CaseSensitive);
foreach (QString section, sections) {
for (const QString &section : sections) {
QStringList fmtParts = section.split(QChar('&'), QString::SkipEmptyParts, Qt::CaseSensitive);
QString itag, urlPart1, urlPart2, name;
QJsonObject itagObj;
foreach (QString fmtPart, fmtParts) {
for (const QString fmtPart : fmtParts) {
QStringList fmtSubParts = fmtPart.split(QChar('='), QString::SkipEmptyParts, Qt::CaseSensitive);
if (fmtSubParts.count() >= 2) {
QString fieldIdentifier = fmtSubParts.at(0).toLower();
@ -189,7 +189,7 @@ QString YoutubeDownload::suitableFilename() const
originalOption = options().at(originalOption).redirectionOf();
}
QString extension;
if (originalOption < static_cast<size_t>(m_itags.size())) {
if (originalOption < static_cast<std::size_t>(m_itags.size())) {
const auto itag = m_itags.at(originalOption);
if (m_itagInfo.contains(itag)) {
const auto itagObj = m_itagInfo.value(itag).toObject();