Skip to content

Commit

Permalink
Try to determine the default video play using xdg-mime instead of har…
Browse files Browse the repository at this point in the history
…d-coding VLC
  • Loading branch information
adamreichold committed Jan 6, 2019
1 parent 8377275 commit f19aeca
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 24 deletions.
4 changes: 1 addition & 3 deletions application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand Down
4 changes: 1 addition & 3 deletions model.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down
41 changes: 34 additions & 7 deletions settings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ along with QMediathekView. If not, see <http://www.gnu.org/licenses/>.

#include "settings.h"

#include <QProcess>
#include <QSettings>
#include <QStandardPaths>

namespace QMediathekView
{
Expand Down Expand Up @@ -73,23 +75,48 @@ 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),
m_settings(new QSettings(this))
{
}

Settings::~Settings()
Settings::~Settings() = default;

void Settings::restoreDefaults()
{
m_settings->clear();
}

QString Settings::userAgent() const
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down
2 changes: 2 additions & 0 deletions settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ class Settings : public QObject
explicit Settings(QObject* parent = 0);
~Settings();

void restoreDefaults();

public:
QString userAgent() const;
QString fullListUrl() const;
Expand Down
42 changes: 32 additions & 10 deletions settingsdialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ along with QMediathekView. If not, see <http://www.gnu.org/licenses/>.

#include "settingsdialog.h"

#include <QAbstractButton>
#include <QAction>
#include <QComboBox>
#include <QDialogButtonBox>
Expand All @@ -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);
Expand All @@ -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();
Expand All @@ -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(
Expand Down
4 changes: 3 additions & 1 deletion settingsdialog.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,11 @@ class SettingsDialog : public QDialog
~SettingsDialog();

public:
void accept();
void accept() override;

private:
void updateValues();
void restoreDefaults();
void selectDownloadFolder();

private:
Expand Down

0 comments on commit f19aeca

Please sign in to comment.