From 2a7e8f14edb7d909f901ef609d43c52fc6c9b419 Mon Sep 17 00:00:00 2001 From: Martchus Date: Wed, 16 Sep 2015 17:32:33 +0200 Subject: [PATCH] outsourced routings to locate additional config files --- gui/settings.cpp | 28 ++++++---------------------- gui/settings.h | 10 ++++++++++ network/groovesharkdownload.cpp | 2 +- network/youtubedownload.cpp | 21 ++++++++++++++++----- 4 files changed, 33 insertions(+), 28 deletions(-) diff --git a/gui/settings.cpp b/gui/settings.cpp index ab0974d..00c630b 100644 --- a/gui/settings.cpp +++ b/gui/settings.cpp @@ -3,6 +3,7 @@ #include "../network/download.h" #include "../network/groovesharkdownload.h" +#include #include #include #include @@ -464,32 +465,15 @@ void restoreSettings() GeneralUiOptionPage::multiSelection() = settings.value("multiselection").toBool(); // load grooveshark authentication file - QString groovesharkAuthenticationFile = QStringLiteral("groovesharkauthenticationinfo.json"); - QString errorMsg = QApplication::translate("QtGui::Settings", "Unable to read Grooveshark authentication information file.\n\nReason: %1\n\nThe values stored in this file are required when connection to Grooveshark. Built-in will values be used instead, but these might be deprecated."); + const auto errorMsg = QApplication::translate("QtGui::Settings", "Unable to read Grooveshark authentication information file.\n\nReason: %1\n\nThe values stored in this file are required when connection to Grooveshark. Built-in will values be used instead, but these might be deprecated."); + const auto groovesharkAuthenticationFile = ConfigFile::locateConfigFile(QStringLiteral("videodownloader"), QStringLiteral("json/groovesharkauthenticationinfo.json"), &settings); QString reason; - if(!QFile::exists(groovesharkAuthenticationFile)) { - groovesharkAuthenticationFile = QStringLiteral("./res/groovesharkauthenticationinfo.json"); - if(!QFile::exists(groovesharkAuthenticationFile)) { - groovesharkAuthenticationFile = QStringLiteral("/etc/videodownloader/json/groovesharkauthenticationinfo.json"); - if(!QFile::exists(groovesharkAuthenticationFile)) { - groovesharkAuthenticationFile = QStringLiteral("/usr/share/videodownloader/json/groovesharkauthenticationinfo.json"); - } - } - } - if(QFile::exists(groovesharkAuthenticationFile)) { + if(!groovesharkAuthenticationFile.isEmpty()) { if(!GroovesharkDownload::loadAuthenticationInformationFromFile(groovesharkAuthenticationFile, &reason)) { - QMessageBox::warning(nullptr, QApplication::applicationName(), errorMsg.arg(errorMsg)); + QMessageBox::warning(nullptr, QApplication::applicationName(), errorMsg.arg(reason)); } } else { - QDir settingsDir = QFileInfo(settings.fileName()).absoluteDir(); - QString groovesharkAuthenticationFilePath = settingsDir.absoluteFilePath(groovesharkAuthenticationFile); - if(QFile::exists(groovesharkAuthenticationFilePath)) { - if(!GroovesharkDownload::loadAuthenticationInformationFromFile(groovesharkAuthenticationFilePath, &reason)) { - QMessageBox::warning(nullptr, QApplication::applicationName(), errorMsg.arg(reason)); - } - } else { - QMessageBox::warning(nullptr, QApplication::applicationName(), errorMsg.arg(QApplication::translate("QtGui::Settings", "Unable to find \"groovesharkauthenticationinfo.json\"."))); - } + QMessageBox::warning(nullptr, QApplication::applicationName(), errorMsg.arg(QApplication::translate("QtGui::Settings", "Unable to find \"groovesharkauthenticationinfo.json\"."))); } } diff --git a/gui/settings.h b/gui/settings.h index 68d3172..282c5fb 100644 --- a/gui/settings.h +++ b/gui/settings.h @@ -41,8 +41,10 @@ public: static QString &targetDirectory(); static bool &overwriteWithoutAsking(); static bool &determineTargetFileWithoutAsking(); + protected: QWidget *setupWidget(); + private: void selectTargetDirectory(); }; @@ -59,8 +61,10 @@ public: static QByteArray &mainWindowGeometry(); static QByteArray &mainWindowState(); static bool &multiSelection(); + protected: QWidget *setupWidget(); + private: QCheckBox *m_multiSelectionCheckBox; }; @@ -75,8 +79,10 @@ public: bool apply(); void reset(); static QNetworkProxy &proxy(); + protected: QWidget *setupWidget(); + private: void updateProxy(); }; @@ -104,8 +110,10 @@ public: bool apply(); void reset(); static bool &redirectWithoutAsking(); + protected: QWidget *setupWidget(); + private: QCheckBox *m_redirectCheckBox; }; @@ -120,8 +128,10 @@ public: bool apply(); void reset(); static quint64 &bytesReceived(); + protected: QWidget *setupWidget(); + private: QLabel *m_receivedLabel; }; diff --git a/network/groovesharkdownload.cpp b/network/groovesharkdownload.cpp index eaf4b90..23b4a44 100644 --- a/network/groovesharkdownload.cpp +++ b/network/groovesharkdownload.cpp @@ -460,7 +460,7 @@ void GroovesharkDownload::setupFinalRequest() setHeader("Refer", m_referer); setHeader("Accept", m_accept); //setHeader("Connection", "keep-alive"); - addDownloadUrl(tr("Grooveshark json request"), QUrl(QStringLiteral("http://grooveshark.com/more.php?%1").arg(method))); + addDownloadUrl(tr("Grooveshark json request"), QUrl(QStringLiteral("http://grooveshark.com/more.php?") + method)); break; } } diff --git a/network/youtubedownload.cpp b/network/youtubedownload.cpp index 1185f15..254d711 100644 --- a/network/youtubedownload.cpp +++ b/network/youtubedownload.cpp @@ -2,6 +2,8 @@ #include "../application/utils.h" +#include + #include #include @@ -55,7 +57,12 @@ Download *YoutubeDownload::infoRequestDownload(bool &sucess, QString &reasonForF void YoutubeDownload::evalVideoInformation(Download *, QBuffer *videoInfoBuffer) { if(m_itagInfo.isEmpty()) { - m_itagInfo = loadJsonObjectFromResource(QStringLiteral(":/jsonobjects/itaginfo")); + // allow an external config file to be used instead of built-in values + QString path = ConfigFile::locateConfigFile(QStringLiteral("videodownloader"), QStringLiteral("itaginfo.json")); + if(path.isEmpty()) { + path = QStringLiteral(":/jsonobjects/itaginfo"); + } + m_itagInfo = loadJsonObjectFromResource(path); } videoInfoBuffer->seek(0); QString videoInfo(videoInfoBuffer->readAll()); @@ -166,17 +173,21 @@ QString YoutubeDownload::videoInfo(QString field, const QString &defaultValue) QString YoutubeDownload::suitableFilename() const { - QString filename = Download::suitableFilename(); + auto filename = Download::suitableFilename(); // get chosen option, the original option (not the redirection!) is required - size_t originalOption = chosenOption(); + auto originalOption = chosenOption(); while(originalOption != options().at(originalOption).redirectionOf()) { originalOption = options().at(originalOption).redirectionOf(); } QString extension; if(originalOption < static_cast(m_itags.size())) { - QString itag = m_itags.at(originalOption); + const auto itag = m_itags.at(originalOption); if(m_itagInfo.contains(itag)) { - extension = m_itagInfo.value(itag).toObject().value(QStringLiteral("container")).toString().toLower(); + const auto itagObj = m_itagInfo.value(itag).toObject(); + extension = itagObj.value(QStringLiteral("ext")).toString(); + if(extension.isEmpty()) { + extension = itagObj.value(QStringLiteral("container")).toString().toLower(); + } } } if(extension.isEmpty()) {