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,111 @@
|
||||
#include "language-manager.h"
|
||||
|
||||
#include "app-settings.h"
|
||||
|
||||
#include <QApplication>
|
||||
#include <QCoreApplication>
|
||||
#include <QDir>
|
||||
#include <QEvent>
|
||||
#include <QFile>
|
||||
#include <QLocale>
|
||||
#include <QStandardPaths>
|
||||
#include <QTranslator>
|
||||
#include <QWidget>
|
||||
|
||||
namespace {
|
||||
|
||||
// Kept alive for the whole process: a QTranslator that is destroyed takes the
|
||||
// installed translation with it.
|
||||
QTranslator* g_translator = nullptr;
|
||||
|
||||
QString catalogueFileName(const QString& code) {
|
||||
return QStringLiteral("translations/pacmanager_%1.qm").arg(code);
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
QStringList LanguageManager::searchPaths() {
|
||||
QStringList paths;
|
||||
|
||||
// Next to the settings the user just chose: where a catalogue installed by
|
||||
// hand (or by a future in-app translation pack) belongs.
|
||||
paths << AppSettings::instance().logDirectory() + QStringLiteral("/translations");
|
||||
|
||||
const QString data = QStandardPaths::writableLocation(
|
||||
QStandardPaths::AppDataLocation);
|
||||
if (!data.isEmpty()) paths << data + QStringLiteral("/translations");
|
||||
|
||||
// Running from a build tree, the catalogues sit beside the binary.
|
||||
paths << QCoreApplication::applicationDirPath() + QStringLiteral("/translations");
|
||||
|
||||
QStringList existing;
|
||||
for (const QString& path : paths) {
|
||||
if (QDir(path).exists()) existing << path;
|
||||
}
|
||||
return existing;
|
||||
}
|
||||
|
||||
QString LanguageManager::resolve(const QString& code) {
|
||||
const QString wanted = AppSettings::sanitizeLanguage(code);
|
||||
if (wanted != QLatin1String("system")) return wanted;
|
||||
|
||||
// "system" follows the desktop, but only into a language the app knows: a
|
||||
// desktop set to Esperanto should leave the interface in its own language
|
||||
// rather than install nothing at all.
|
||||
const QString locale = QLocale::system().name().left(2);
|
||||
if (AppSettings::knownLanguages().contains(locale)) return locale;
|
||||
return QStringLiteral("fr");
|
||||
}
|
||||
|
||||
bool LanguageManager::isTranslationAvailable(const QString& code) {
|
||||
const QString resolved = resolve(code);
|
||||
const QString fileName = catalogueFileName(resolved);
|
||||
for (const QString& path : searchPaths()) {
|
||||
if (QFile::exists(path + QLatin1Char('/') + fileName)) return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
QString LanguageManager::searchPathHint() {
|
||||
return searchPaths().isEmpty() ? QString() : searchPaths().first();
|
||||
}
|
||||
|
||||
QString LanguageManager::activeCode() {
|
||||
return g_translator ? g_translator->language() : QString();
|
||||
}
|
||||
|
||||
bool LanguageManager::apply(const QString& code) {
|
||||
const QString resolved = resolve(code);
|
||||
const QString fileName = catalogueFileName(resolved);
|
||||
|
||||
// The previous catalogue goes first, so a language the user just left does
|
||||
// not keep translating part of the interface.
|
||||
if (g_translator) {
|
||||
QCoreApplication::removeTranslator(g_translator);
|
||||
delete g_translator;
|
||||
g_translator = nullptr;
|
||||
}
|
||||
|
||||
bool installed = false;
|
||||
for (const QString& path : searchPaths()) {
|
||||
if (!g_translator) g_translator = new QTranslator();
|
||||
if (g_translator->load(fileName, path)) {
|
||||
QCoreApplication::installTranslator(g_translator);
|
||||
installed = true;
|
||||
break;
|
||||
}
|
||||
delete g_translator;
|
||||
g_translator = nullptr;
|
||||
}
|
||||
|
||||
// No catalogue (the common case today): the widgets still get the event, so
|
||||
// switching back to an untranslated language restores the original wording
|
||||
// instead of leaving the previous translation in place.
|
||||
const QWidgetList widgets = QApplication::allWidgets();
|
||||
for (QWidget* widget : widgets) {
|
||||
QEvent event(QEvent::LanguageChange);
|
||||
QCoreApplication::sendEvent(widget, &event);
|
||||
}
|
||||
|
||||
return installed;
|
||||
}
|
||||
Reference in new issue
Block a user