feat(settings): add a dedicated screen for every user preference
The preferences used to be spread across a menu entry and hardcoded paths, so the user could not see them, let alone change them: where the journal goes, whether a dropped Flatpak file is accepted, and when the app checks its own version were all decided in code. They now live in AppSettings, one model the rest of the app already reads, and the new Settings screen only writes to it. Nothing keeps a second copy of a value, so a change is visible everywhere at once — switching directory moves the journal, unchecking drag & drop makes the window refuse the drop, blocking the update has the manual check say why instead of silently doing nothing. The update block is deliberately limited to PacManager itself: blocking a system package update is a decision the app has no business making. The language setting installs a Qt catalogue and asks every widget to retranslate. The interface is still written in French with QStringLiteral, so no catalogue ships yet; a language with none is shown in the list but disabled rather than silently doing nothing, and Qt Linguist stays optional so the build does not depend on it.
This commit is contained in:
27 files changed
+1906
-27
No files matched your search
@@ -0,0 +1,242 @@
|
||||
#include "app-settings.h"
|
||||
|
||||
#include <QCoreApplication>
|
||||
#include <QDir>
|
||||
#include <QFileInfo>
|
||||
#include <QSettings>
|
||||
#include <QStandardPaths>
|
||||
|
||||
namespace {
|
||||
|
||||
// Below this a cap would rotate the journal on practically every line, which
|
||||
// makes the "previous journal" LogManager keeps useless.
|
||||
constexpr qint64 kMinLogMaxBytes = 64 * 1024;
|
||||
|
||||
QString key(const char* name) {
|
||||
return QStringLiteral("settings/") + QString::fromLatin1(name);
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
AppSettings& AppSettings::instance() {
|
||||
// Function-local static: created once, thread-safe since C++11.
|
||||
static AppSettings settings;
|
||||
return settings;
|
||||
}
|
||||
|
||||
AppSettings::AppSettings(QObject* parent) : QObject(parent) {
|
||||
loadUpdateBlock();
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Journal
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
QString AppSettings::defaultLogDirectory() {
|
||||
const QString dir = QStandardPaths::writableLocation(
|
||||
QStandardPaths::AppConfigLocation);
|
||||
if (!dir.isEmpty()) {
|
||||
QDir(dir).mkpath(".");
|
||||
return dir;
|
||||
}
|
||||
return QDir::homePath() + QStringLiteral("/.config/PacManager");
|
||||
}
|
||||
|
||||
QString AppSettings::sanitizeLogDirectory(const QString& directory) {
|
||||
QString path = directory.trimmed();
|
||||
if (path.isEmpty()) return defaultLogDirectory();
|
||||
|
||||
// The dialog hands back a native path, but a hand-edited setting may still
|
||||
// start with "~" — which no other API in the app would expand.
|
||||
if (path == QLatin1String("~")) {
|
||||
path = QDir::homePath();
|
||||
} else if (path.startsWith(QLatin1String("~/"))) {
|
||||
path = QDir::homePath() + path.mid(1);
|
||||
}
|
||||
|
||||
const QString clean = QDir::cleanPath(QFileInfo(path).absoluteFilePath());
|
||||
return clean.isEmpty() ? defaultLogDirectory() : clean;
|
||||
}
|
||||
|
||||
QString AppSettings::logDirectory() const {
|
||||
QSettings settings;
|
||||
return sanitizeLogDirectory(settings.value(key("logDirectory")).toString());
|
||||
}
|
||||
|
||||
void AppSettings::setLogDirectory(const QString& directory) {
|
||||
const QString wanted = sanitizeLogDirectory(directory);
|
||||
// Compared with what is in force *before* writing: comparing with the
|
||||
// stored value afterwards would always match, and every setter would
|
||||
// announce a change that did not happen.
|
||||
if (wanted == logDirectory()) return;
|
||||
|
||||
QSettings settings;
|
||||
// Back to the default: remove the key rather than pinning the current
|
||||
// default path, which would survive a change of XDG_CONFIG_HOME.
|
||||
if (wanted == defaultLogDirectory()) {
|
||||
settings.remove(key("logDirectory"));
|
||||
} else {
|
||||
settings.setValue(key("logDirectory"), wanted);
|
||||
}
|
||||
emit logDirectoryChanged(wanted);
|
||||
}
|
||||
|
||||
qint64 AppSettings::sanitizeLogMaxBytes(qint64 bytes) {
|
||||
if (bytes <= 0) return 0;
|
||||
if (bytes < kMinLogMaxBytes) return kMinLogMaxBytes;
|
||||
return bytes;
|
||||
}
|
||||
|
||||
qint64 AppSettings::logMaxBytes() const {
|
||||
QSettings settings;
|
||||
return sanitizeLogMaxBytes(settings.value(key("logMaxBytes"), 0).toLongLong());
|
||||
}
|
||||
|
||||
void AppSettings::setLogMaxBytes(qint64 bytes) {
|
||||
const qint64 wanted = sanitizeLogMaxBytes(bytes);
|
||||
if (wanted == logMaxBytes()) return;
|
||||
|
||||
QSettings settings;
|
||||
if (wanted == 0) settings.remove(key("logMaxBytes"));
|
||||
else settings.setValue(key("logMaxBytes"), wanted);
|
||||
emit logMaxBytesChanged(wanted);
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Drag & drop
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
bool AppSettings::dragDropEnabled() const {
|
||||
QSettings settings;
|
||||
// On by default: dropping a .flatpakref on the window is the quickest way
|
||||
// to install it, so turning it off has to be a deliberate act.
|
||||
return settings.value(key("dragDrop"), true).toBool();
|
||||
}
|
||||
|
||||
void AppSettings::setDragDropEnabled(bool enabled) {
|
||||
if (enabled == dragDropEnabled()) return;
|
||||
|
||||
QSettings settings;
|
||||
settings.setValue(key("dragDrop"), enabled);
|
||||
emit dragDropEnabledChanged(enabled);
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Language
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
QStringList AppSettings::knownLanguages() {
|
||||
return {QStringLiteral("fr"), QStringLiteral("en")};
|
||||
}
|
||||
|
||||
QString AppSettings::sanitizeLanguage(const QString& code) {
|
||||
const QString wanted = code.trimmed().toLower();
|
||||
// Nothing stored means "follow the desktop", which is what a fresh install
|
||||
// gets; an unknown code is not the desktop, it is a mistake.
|
||||
if (wanted.isEmpty() || wanted == QLatin1String("system")) {
|
||||
return QStringLiteral("system");
|
||||
}
|
||||
return knownLanguages().contains(wanted) ? wanted : QStringLiteral("fr");
|
||||
}
|
||||
|
||||
QString AppSettings::language() const {
|
||||
QSettings settings;
|
||||
return sanitizeLanguage(settings.value(key("language")).toString());
|
||||
}
|
||||
|
||||
void AppSettings::setLanguage(const QString& code) {
|
||||
const QString wanted = sanitizeLanguage(code);
|
||||
if (wanted == language()) return;
|
||||
|
||||
QSettings settings;
|
||||
settings.setValue(key("language"), wanted);
|
||||
emit languageChanged(wanted);
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Application updates
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
bool AppSettings::autoUpdateCheckEnabled() const {
|
||||
QSettings settings;
|
||||
return settings.value(key("autoUpdateCheck"), true).toBool();
|
||||
}
|
||||
|
||||
void AppSettings::setAutoUpdateCheckEnabled(bool enabled) {
|
||||
if (enabled == autoUpdateCheckEnabled()) return;
|
||||
|
||||
QSettings settings;
|
||||
settings.setValue(key("autoUpdateCheck"), enabled);
|
||||
emit autoUpdateCheckEnabledChanged(enabled);
|
||||
}
|
||||
|
||||
QDateTime AppSettings::blockTarget(const QDateTime& now, int hours) {
|
||||
if (hours <= 0) return QDateTime();
|
||||
// Second precision, which is all a snooze needs and is exactly what
|
||||
// survives the round trip through the settings file: a deadline rebuilt
|
||||
// from it therefore compares equal to the one that was written.
|
||||
const QDateTime end = now.addSecs(static_cast<qint64>(hours) * 3600);
|
||||
return QDateTime::fromSecsSinceEpoch(end.toSecsSinceEpoch());
|
||||
}
|
||||
|
||||
bool AppSettings::isBlocked(const QDateTime& now, bool indefinite, const QDateTime& until) {
|
||||
if (indefinite) return true;
|
||||
return until.isValid() && now < until;
|
||||
}
|
||||
|
||||
void AppSettings::loadUpdateBlock() {
|
||||
QSettings settings;
|
||||
m_blockIndefinite = settings.value(key("updateBlockIndefinite"), false).toBool();
|
||||
m_blockUntil = QDateTime::fromString(
|
||||
settings.value(key("updateBlockUntil")).toString(), Qt::ISODate);
|
||||
// A stored date that no longer parses is dropped rather than believed: an
|
||||
// unreadable "until" must never keep the user from checking again.
|
||||
if (m_blockUntil.isNull()) m_blockUntil = QDateTime();
|
||||
}
|
||||
|
||||
void AppSettings::saveUpdateBlock() {
|
||||
QSettings settings;
|
||||
settings.setValue(key("updateBlockIndefinite"), m_blockIndefinite);
|
||||
if (m_blockUntil.isValid()) {
|
||||
settings.setValue(key("updateBlockUntil"), m_blockUntil.toString(Qt::ISODate));
|
||||
} else {
|
||||
settings.remove(key("updateBlockUntil"));
|
||||
}
|
||||
}
|
||||
|
||||
void AppSettings::blockUpdatesFor(int hours) {
|
||||
m_blockIndefinite = hours <= 0;
|
||||
m_blockUntil = blockTarget(QDateTime::currentDateTime(), hours);
|
||||
saveUpdateBlock();
|
||||
emit updateBlockChanged();
|
||||
}
|
||||
|
||||
void AppSettings::unblockUpdates() {
|
||||
m_blockIndefinite = false;
|
||||
m_blockUntil = QDateTime();
|
||||
saveUpdateBlock();
|
||||
emit updateBlockChanged();
|
||||
}
|
||||
|
||||
QString AppSettings::blockDescription(bool indefinite, const QDateTime& until,
|
||||
const QDateTime& now) {
|
||||
if (indefinite) {
|
||||
return QCoreApplication::translate(
|
||||
"AppSettings", "Mises à jour bloquées jusqu'à annulation.");
|
||||
}
|
||||
if (!until.isValid() || now >= until) return QString();
|
||||
|
||||
const qint64 seconds = until.secsTo(now);
|
||||
const qint64 hours = seconds / 3600;
|
||||
if (hours >= 24) {
|
||||
const qint64 days = hours / 24;
|
||||
return QCoreApplication::translate("AppSettings", "Mises à jour bloisées encore %1 jour(s).")
|
||||
.arg(days);
|
||||
}
|
||||
if (hours >= 1) {
|
||||
return QCoreApplication::translate("AppSettings", "Mises à jour bloisées encore %1 heure(s).")
|
||||
.arg(hours);
|
||||
}
|
||||
return QCoreApplication::translate("AppSettings", "Mises à jour bloisées encore %1 minute(s).")
|
||||
.arg(qMax<qint64>(1, seconds / 60));
|
||||
}
|
||||
Reference in new issue
Block a user