feat: load the user's mpv.conf
This commit is contained in:
1 parent
fe2f159660
commit
6b5742b2ca
11 files changed
+298
-6
No files matched your search
@@ -1,7 +1,14 @@
|
||||
#include "mpvitem.h"
|
||||
|
||||
#include "mpvuserconfig.h"
|
||||
|
||||
#include <MpvController>
|
||||
|
||||
#include <QDir>
|
||||
#include <QFile>
|
||||
#include <QSaveFile>
|
||||
#include <QStandardPaths>
|
||||
|
||||
#include <cmath>
|
||||
#include <utility>
|
||||
|
||||
@@ -58,6 +65,8 @@ MpvItem::MpvItem(QQuickItem *parent)
|
||||
// time; a file loaded before that plays without video. Hold it until ready() fires.
|
||||
connect(this, &MpvAbstractItem::ready, this, [this] {
|
||||
m_ready = true;
|
||||
// Before the first file, so its options apply from the start.
|
||||
applyUserConfig();
|
||||
if (m_pendingUrl.isValid()) {
|
||||
loadFile(std::exchange(m_pendingUrl, {}));
|
||||
}
|
||||
@@ -208,6 +217,65 @@ void MpvItem::showText(const QString &text)
|
||||
commandAsync(QStringList{QStringLiteral("show-text"), text});
|
||||
}
|
||||
|
||||
bool MpvItem::loadUserConfig() const
|
||||
{
|
||||
return m_loadUserConfig;
|
||||
}
|
||||
|
||||
void MpvItem::setLoadUserConfig(bool load)
|
||||
{
|
||||
if (load != m_loadUserConfig) {
|
||||
m_loadUserConfig = load;
|
||||
Q_EMIT loadUserConfigChanged();
|
||||
}
|
||||
}
|
||||
|
||||
QString MpvItem::userConfigDir() const
|
||||
{
|
||||
return MpvUserConfig::configDir();
|
||||
}
|
||||
|
||||
QStringList MpvItem::loadedUserConfigFiles() const
|
||||
{
|
||||
return m_loadedUserConfigFiles;
|
||||
}
|
||||
|
||||
QStringList MpvItem::skippedUserOptions() const
|
||||
{
|
||||
return m_skippedUserOptions;
|
||||
}
|
||||
|
||||
void MpvItem::applyUserConfig()
|
||||
{
|
||||
if (m_loadUserConfig) {
|
||||
const QString cacheDir = QStandardPaths::writableLocation(QStandardPaths::CacheLocation) + QStringLiteral("/mpv");
|
||||
QDir().mkpath(cacheDir);
|
||||
|
||||
const QStringList files = MpvUserConfig::configFiles();
|
||||
for (qsizetype i = 0; i < files.size(); ++i) {
|
||||
QFile source(files.at(i));
|
||||
if (!source.open(QIODevice::ReadOnly)) {
|
||||
continue;
|
||||
}
|
||||
const MpvUserConfig::Prepared prepared = MpvUserConfig::prepare(QString::fromUtf8(source.readAll()), MpvUserConfig::configDir());
|
||||
|
||||
// mpv only loads config from files, so the adapted copy goes to the cache.
|
||||
QSaveFile copy(QStringLiteral("%1/mpv-%2.conf").arg(cacheDir).arg(i));
|
||||
if (!copy.open(QIODevice::WriteOnly) || copy.write(prepared.content.toUtf8()) < 0 || !copy.commit()) {
|
||||
continue;
|
||||
}
|
||||
commandAsync(QStringList{QStringLiteral("load-config-file"), copy.fileName()});
|
||||
m_loadedUserConfigFiles.append(files.at(i));
|
||||
for (const QString &option : prepared.skippedOptions) {
|
||||
if (!m_skippedUserOptions.contains(option)) {
|
||||
m_skippedUserOptions.append(option);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Q_EMIT userConfigApplied();
|
||||
}
|
||||
|
||||
void MpvItem::onPropertyChanged(const QString &property, const QVariant &value)
|
||||
{
|
||||
if (property == u"media-title") {
|
||||
|
||||
Reference in new issue
Block a user