The journal and the preferences were two of the many things src/ held, so "where does this live" meant reading the file to find out. They now have a folder each — logging/ and preferences/ — with the header next to its source, the way pages/ has always been laid out. Flat #include still resolves: the folders join the include path.
253 lines
8.7 KiB
C++
253 lines
8.7 KiB
C++
#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() {
|
|
// The languages a catalogue can be written for. French needs no catalogue:
|
|
// it is the language the wording is written in.
|
|
return {QStringLiteral("fr"), QStringLiteral("en")};
|
|
}
|
|
|
|
QString AppSettings::languageName(const QString& code) {
|
|
// Endonyms, not translated names: a French speaker looking for English
|
|
// looks for "English", not for "anglais".
|
|
if (code == QLatin1String("fr")) return QStringLiteral("Français");
|
|
if (code == QLatin1String("en")) return QStringLiteral("English");
|
|
return code;
|
|
}
|
|
|
|
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));
|
|
}
|