outsourced routings to locate additional config files

This commit is contained in:
Martchus 2015-09-16 17:32:33 +02:00
parent d5b880a22a
commit 2a7e8f14ed
4 changed files with 33 additions and 28 deletions

View File

@ -3,6 +3,7 @@
#include "../network/download.h" #include "../network/download.h"
#include "../network/groovesharkdownload.h" #include "../network/groovesharkdownload.h"
#include <qtutilities/resources/resources.h>
#include <qtutilities/settingsdialog/optioncategory.h> #include <qtutilities/settingsdialog/optioncategory.h>
#include <qtutilities/settingsdialog/optioncategorymodel.h> #include <qtutilities/settingsdialog/optioncategorymodel.h>
#include <qtutilities/widgets/clearlineedit.h> #include <qtutilities/widgets/clearlineedit.h>
@ -464,32 +465,15 @@ void restoreSettings()
GeneralUiOptionPage::multiSelection() = settings.value("multiselection").toBool(); GeneralUiOptionPage::multiSelection() = settings.value("multiselection").toBool();
// load grooveshark authentication file // load grooveshark authentication file
QString groovesharkAuthenticationFile = QStringLiteral("groovesharkauthenticationinfo.json"); 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.");
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 groovesharkAuthenticationFile = ConfigFile::locateConfigFile(QStringLiteral("videodownloader"), QStringLiteral("json/groovesharkauthenticationinfo.json"), &settings);
QString reason; QString reason;
if(!QFile::exists(groovesharkAuthenticationFile)) { if(!groovesharkAuthenticationFile.isEmpty()) {
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(!GroovesharkDownload::loadAuthenticationInformationFromFile(groovesharkAuthenticationFile, &reason)) { if(!GroovesharkDownload::loadAuthenticationInformationFromFile(groovesharkAuthenticationFile, &reason)) {
QMessageBox::warning(nullptr, QApplication::applicationName(), errorMsg.arg(errorMsg)); QMessageBox::warning(nullptr, QApplication::applicationName(), errorMsg.arg(reason));
} }
} else { } else {
QDir settingsDir = QFileInfo(settings.fileName()).absoluteDir(); QMessageBox::warning(nullptr, QApplication::applicationName(), errorMsg.arg(QApplication::translate("QtGui::Settings", "Unable to find \"groovesharkauthenticationinfo.json\".")));
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\".")));
}
} }
} }

View File

@ -41,8 +41,10 @@ public:
static QString &targetDirectory(); static QString &targetDirectory();
static bool &overwriteWithoutAsking(); static bool &overwriteWithoutAsking();
static bool &determineTargetFileWithoutAsking(); static bool &determineTargetFileWithoutAsking();
protected: protected:
QWidget *setupWidget(); QWidget *setupWidget();
private: private:
void selectTargetDirectory(); void selectTargetDirectory();
}; };
@ -59,8 +61,10 @@ public:
static QByteArray &mainWindowGeometry(); static QByteArray &mainWindowGeometry();
static QByteArray &mainWindowState(); static QByteArray &mainWindowState();
static bool &multiSelection(); static bool &multiSelection();
protected: protected:
QWidget *setupWidget(); QWidget *setupWidget();
private: private:
QCheckBox *m_multiSelectionCheckBox; QCheckBox *m_multiSelectionCheckBox;
}; };
@ -75,8 +79,10 @@ public:
bool apply(); bool apply();
void reset(); void reset();
static QNetworkProxy &proxy(); static QNetworkProxy &proxy();
protected: protected:
QWidget *setupWidget(); QWidget *setupWidget();
private: private:
void updateProxy(); void updateProxy();
}; };
@ -104,8 +110,10 @@ public:
bool apply(); bool apply();
void reset(); void reset();
static bool &redirectWithoutAsking(); static bool &redirectWithoutAsking();
protected: protected:
QWidget *setupWidget(); QWidget *setupWidget();
private: private:
QCheckBox *m_redirectCheckBox; QCheckBox *m_redirectCheckBox;
}; };
@ -120,8 +128,10 @@ public:
bool apply(); bool apply();
void reset(); void reset();
static quint64 &bytesReceived(); static quint64 &bytesReceived();
protected: protected:
QWidget *setupWidget(); QWidget *setupWidget();
private: private:
QLabel *m_receivedLabel; QLabel *m_receivedLabel;
}; };

View File

@ -460,7 +460,7 @@ void GroovesharkDownload::setupFinalRequest()
setHeader("Refer", m_referer); setHeader("Refer", m_referer);
setHeader("Accept", m_accept); setHeader("Accept", m_accept);
//setHeader("Connection", "keep-alive"); //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; break;
} }
} }

View File

@ -2,6 +2,8 @@
#include "../application/utils.h" #include "../application/utils.h"
#include <qtutilities/resources/resources.h>
#include <QUrlQuery> #include <QUrlQuery>
#include <QJsonDocument> #include <QJsonDocument>
@ -55,7 +57,12 @@ Download *YoutubeDownload::infoRequestDownload(bool &sucess, QString &reasonForF
void YoutubeDownload::evalVideoInformation(Download *, QBuffer *videoInfoBuffer) void YoutubeDownload::evalVideoInformation(Download *, QBuffer *videoInfoBuffer)
{ {
if(m_itagInfo.isEmpty()) { 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); videoInfoBuffer->seek(0);
QString videoInfo(videoInfoBuffer->readAll()); QString videoInfo(videoInfoBuffer->readAll());
@ -166,17 +173,21 @@ QString YoutubeDownload::videoInfo(QString field, const QString &defaultValue)
QString YoutubeDownload::suitableFilename() const QString YoutubeDownload::suitableFilename() const
{ {
QString filename = Download::suitableFilename(); auto filename = Download::suitableFilename();
// get chosen option, the original option (not the redirection!) is required // get chosen option, the original option (not the redirection!) is required
size_t originalOption = chosenOption(); auto originalOption = chosenOption();
while(originalOption != options().at(originalOption).redirectionOf()) { while(originalOption != options().at(originalOption).redirectionOf()) {
originalOption = options().at(originalOption).redirectionOf(); originalOption = options().at(originalOption).redirectionOf();
} }
QString extension; QString extension;
if(originalOption < static_cast<size_t>(m_itags.size())) { if(originalOption < static_cast<size_t>(m_itags.size())) {
QString itag = m_itags.at(originalOption); const auto itag = m_itags.at(originalOption);
if(m_itagInfo.contains(itag)) { 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()) { if(extension.isEmpty()) {