From f19aeca2c443f8f245824a2a50cc31789fa10afb Mon Sep 17 00:00:00 2001 From: Adam Reichold Date: Sun, 6 Jan 2019 12:17:49 +0100 Subject: [PATCH] Try to determine the default video play using xdg-mime instead of hard-coding VLC --- application.cpp | 4 +--- model.cpp | 4 +--- settings.cpp | 41 ++++++++++++++++++++++++++++++++++------- settings.h | 2 ++ settingsdialog.cpp | 42 ++++++++++++++++++++++++++++++++---------- settingsdialog.h | 4 +++- 6 files changed, 73 insertions(+), 24 deletions(-) diff --git a/application.cpp b/application.cpp index 490acea..2af8567 100644 --- a/application.cpp +++ b/application.cpp @@ -132,9 +132,7 @@ Application::Application(int& argc, char** argv) connect(this, &Application::failedToUpdateDatabase, m_mainWindow, &MainWindow::showDatabaseUpdateFailure); } -Application::~Application() -{ -} +Application::~Application() = default; int Application::exec() { diff --git a/model.cpp b/model.cpp index 3df7004..efb0705 100644 --- a/model.cpp +++ b/model.cpp @@ -45,9 +45,7 @@ Model::Model(Database& database, QObject* parent) : QAbstractTableModel(parent), update(); } -Model::~Model() -{ -} +Model::~Model() = default; int Model::columnCount(const QModelIndex& parent) const { diff --git a/settings.cpp b/settings.cpp index 5d17508..6479c7a 100644 --- a/settings.cpp +++ b/settings.cpp @@ -21,7 +21,9 @@ along with QMediathekView. If not, see . #include "settings.h" +#include #include +#include namespace QMediathekView { @@ -73,14 +75,36 @@ const auto partialListUrl = QStringLiteral("http://zdfmediathk.sourceforge.net/d constexpr auto mirrorListUpdateAfterDays = 3; constexpr auto databaseUpdateAfterHours = 3; -const auto playCommand = QStringLiteral("vlc %1"); - -const auto downloadFolder = QDir::homePath(); - constexpr auto preferredUrl = Url::Default; } // Defaults +QString defaultPlayCommand() +{ + QString command("vlc"); + + QProcess shell; + shell.start("sh", QStringList() << "-c" << "grep '^Exec=' /usr/share/applications/`xdg-mime query default video/mp4` | cut -d'=' -f2 | cut -d' ' -f1", QIODevice::ReadOnly); + shell.waitForFinished(); + + if (shell.exitStatus() == QProcess::NormalExit) + { + const auto greppedCommand = QString::fromLocal8Bit(shell.readAll()).trimmed(); + + if (!greppedCommand.isEmpty()) + { + command = greppedCommand; + } + } + + return command.append(" %1"); +} + +QString defaultDownloadFolder() +{ + return QStandardPaths::writableLocation(QStandardPaths::DownloadLocation); +} + } // anonymous Settings::Settings(QObject* parent) : QObject(parent), @@ -88,8 +112,11 @@ Settings::Settings(QObject* parent) : QObject(parent), { } -Settings::~Settings() +Settings::~Settings() = default; + +void Settings::restoreDefaults() { + m_settings->clear(); } QString Settings::userAgent() const @@ -179,7 +206,7 @@ void Settings::resetDatabaseUpdatedOn() QString Settings::playCommand() const { - return m_settings->value(Keys::playCommand, Defaults::playCommand).toString(); + return m_settings->value(Keys::playCommand, defaultPlayCommand()).toString(); } void Settings::setPlayCommand(const QString& command) @@ -199,7 +226,7 @@ void Settings::setDownloadCommand(const QString& command) QDir Settings::downloadFolder() const { - return QDir(m_settings->value(Keys::downloadFolder, Defaults::downloadFolder).toString()); + return QDir(m_settings->value(Keys::downloadFolder, defaultDownloadFolder()).toString()); } void Settings::setDownloadFolder(const QDir& folder) diff --git a/settings.h b/settings.h index 12cb663..76767fe 100644 --- a/settings.h +++ b/settings.h @@ -43,6 +43,8 @@ class Settings : public QObject explicit Settings(QObject* parent = 0); ~Settings(); + void restoreDefaults(); + public: QString userAgent() const; QString fullListUrl() const; diff --git a/settingsdialog.cpp b/settingsdialog.cpp index 4b32b36..938995b 100644 --- a/settingsdialog.cpp +++ b/settingsdialog.cpp @@ -21,6 +21,7 @@ along with QMediathekView. If not, see . #include "settingsdialog.h" +#include #include #include #include @@ -44,29 +45,24 @@ SettingsDialog::SettingsDialog( setLayout(layout); m_mirrorsUpdateAfterDaysBox = new QSpinBox(this); - m_mirrorsUpdateAfterDaysBox->setValue(m_settings.mirrorsUpdateAfterDays()); m_mirrorsUpdateAfterDaysBox->setRange(3, 30); m_mirrorsUpdateAfterDaysBox->setPrefix(tr("after ")); m_mirrorsUpdateAfterDaysBox->setSuffix(tr(" days")); layout->addRow(tr("Mirrors update"), m_mirrorsUpdateAfterDaysBox); m_databaseUpdateAfterHoursBox = new QSpinBox(this); - m_databaseUpdateAfterHoursBox->setValue(m_settings.databaseUpdateAfterHours()); m_databaseUpdateAfterHoursBox->setRange(3, 30); m_databaseUpdateAfterHoursBox->setPrefix(tr("after ")); m_databaseUpdateAfterHoursBox->setSuffix(tr(" hours")); layout->addRow(tr("Database update"), m_databaseUpdateAfterHoursBox); m_playCommandEdit = new QLineEdit(this); - m_playCommandEdit->setText(m_settings.playCommand()); layout->addRow(tr("Play command"), m_playCommandEdit); m_downloadCommandEdit = new QLineEdit(this); - m_downloadCommandEdit->setText(m_settings.downloadCommand()); layout->addRow(tr("Download command"), m_downloadCommandEdit); m_downloadFolderEdit = new QLineEdit(this); - m_downloadFolderEdit->setText(m_settings.downloadFolder().absolutePath()); layout->addRow(tr("Download folder"), m_downloadFolderEdit); const auto selectDownloadFolderAction = m_downloadFolderEdit->addAction(QIcon::fromTheme(QStringLiteral("document-open")), QLineEdit::TrailingPosition); @@ -76,20 +72,27 @@ SettingsDialog::SettingsDialog( m_preferredUrlBox->addItem(tr("Default"), int(Url::Default)); m_preferredUrlBox->addItem(tr("Small"), int(Url::Small)); m_preferredUrlBox->addItem(tr("Large"), int(Url::Large)); - m_preferredUrlBox->setCurrentIndex(m_preferredUrlBox->findData(int(m_settings.preferredUrl()))); layout->addRow(tr("Preferred URL"), m_preferredUrlBox); - auto buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel, this); + updateValues(); + + auto buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel | QDialogButtonBox::RestoreDefaults, this); layout->addWidget(buttonBox); connect(buttonBox, &QDialogButtonBox::accepted, this, &SettingsDialog::accept); connect(buttonBox, &QDialogButtonBox::rejected, this, &SettingsDialog::reject); -} -SettingsDialog::~SettingsDialog() -{ + connect(buttonBox, &QDialogButtonBox::clicked, [this, buttonBox](QAbstractButton* button) + { + if (buttonBox->buttonRole(button) == QDialogButtonBox::ResetRole) + { + restoreDefaults(); + } + }); } +SettingsDialog::~SettingsDialog() = default; + void SettingsDialog::accept() { QDialog::accept(); @@ -105,6 +108,25 @@ void SettingsDialog::accept() m_settings.setPreferredUrl(Url(m_preferredUrlBox->currentData().toInt())); } +void SettingsDialog::updateValues() +{ + m_mirrorsUpdateAfterDaysBox->setValue(m_settings.mirrorsUpdateAfterDays()); + m_databaseUpdateAfterHoursBox->setValue(m_settings.databaseUpdateAfterHours()); + + m_playCommandEdit->setText(m_settings.playCommand()); + m_downloadCommandEdit->setText(m_settings.downloadCommand()); + m_downloadFolderEdit->setText(m_settings.downloadFolder().absolutePath()); + + m_preferredUrlBox->setCurrentIndex(m_preferredUrlBox->findData(int(m_settings.preferredUrl()))); +} + +void SettingsDialog::restoreDefaults() +{ + m_settings.restoreDefaults(); + + updateValues(); +} + void SettingsDialog::selectDownloadFolder() { const auto downloadFolder = QFileDialog::getExistingDirectory( diff --git a/settingsdialog.h b/settingsdialog.h index 82d1657..f22d909 100644 --- a/settingsdialog.h +++ b/settingsdialog.h @@ -43,9 +43,11 @@ class SettingsDialog : public QDialog ~SettingsDialog(); public: - void accept(); + void accept() override; private: + void updateValues(); + void restoreDefaults(); void selectDownloadFolder(); private: