Skip to content

Commit

Permalink
Replace settings strings by ids
Browse files Browse the repository at this point in the history
Merge pull request #383 from codeschrat/replace-settings-strings-by-ids
  • Loading branch information
Murmele authored Dec 6, 2022
2 parents 5e5c1c7 + ead51fd commit 3b69f6f
Show file tree
Hide file tree
Showing 32 changed files with 458 additions and 369 deletions.
11 changes: 8 additions & 3 deletions src/app/Application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,8 @@ Application::Application(int &argc, char **argv, bool haltOnParseError)

// Read translation settings.
QSettings settings;
if ((!settings.value("translation/disable", false).toBool()) &&
if ((!settings.value(Setting::key(Setting::Id::DontTranslate), false)
.toBool()) &&
(!parser.isSet("no-translation"))) {
// Load translation files.
QLocale locale;
Expand Down Expand Up @@ -230,7 +231,9 @@ Application::Application(int &argc, char **argv, bool haltOnParseError)

void Application::autoUpdate() {
// Check for updates.
if (Settings::instance()->value("update/check").toBool()) {
if (Settings::instance()
->value(Setting::Id::CheckForUpdatesAutomatically)
.toBool()) {
// Check now.
Updater::instance()->update(true);

Expand Down Expand Up @@ -368,7 +371,9 @@ class CopyDataWindow : public QWindow {
#endif

bool Application::runSingleInstance() {
if (Settings::instance()->value("singleInstance").toBool()) {
if (Settings::instance()
->value(Setting::Id::AllowSingleInstanceOnly)
.toBool()) {
#if defined(Q_OS_LINUX)
QDBusConnection bus = QDBusConnection::sessionBus();

Expand Down
6 changes: 3 additions & 3 deletions src/app/Theme.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -236,17 +236,17 @@ QColor Theme::star() { return QPalette().color(QPalette::Highlight); }
Theme *Theme::create(const QString &defaultName) {
// Upgrade theme key to capital case.
Settings *settings = Settings::instance();
QString key = settings->value("window/theme").toString();
QString key = settings->value(Setting::Id::ColorTheme).toString();
if (key == "default" || key == "dark" || key == "system") {
key[0] = key.at(0).toUpper();
settings->setValue("window/theme", key);
settings->setValue(Setting::Id::ColorTheme, key);
}

QString name = !defaultName.isEmpty() ? defaultName : key;
if (name.isEmpty() && !Application::isInTest()) {
ThemeDialog dialog;
dialog.exec();
name = settings->value("window/theme").toString();
name = settings->value(Setting::Id::ColorTheme).toString();
}

// Load custom theme.
Expand Down
49 changes: 42 additions & 7 deletions src/conf/Setting.cpp
Original file line number Diff line number Diff line change
@@ -1,15 +1,50 @@
#include "Setting.h"

void Setting::initialize(QMap<Id, QString> &keys) {
keys[Id::FetchAutomatically] = "global/autofetch/enable";
keys[Id::AutomaticFetchPeriodInMinutes] = "global/autofetch/minutes";
keys[Id::PushAfterEachCommit] = "global/autopush/enable";
keys[Id::UpdateSubmodulesAfterPullAndClone] = "global/autoupdate/enable";
keys[Id::PruneAfterFetch] = "global/autoprune/enable";
keys[Id::FontFamily] = "editor/font/family";
keys[Id::FontSize] = "editor/font/size";
keys[Id::UseTabsForIndent] = "editor/indent/tabs";
keys[Id::IndentWidth] = "editor/indent/width";
keys[Id::TabWidth] = "editor/indent/tabwidth";
keys[Id::ShowHeatmapInBlameMargin] = "editor/blame/heatmap";
keys[Id::ColorTheme] = "window/theme";
keys[Id::ShowFullRepoPath] = "window/path/full";
keys[Id::HideLogAutomatically] = "window/log/hide";
keys[Id::OpenSubmodulesInTabs] = "window/tabs/submodule";
keys[Id::OpenAllReposInTabs] = "window/tabs/repository";
keys[Id::HideMenuBar] = "window/view/menuBarHidden";
keys[Id::ShowAvatars] = "window/view/avatarsVisible";
keys[Id::AutoCollapseAddedFiles] = "collapse/added";
keys[Id::AutoCollapseDeletedFiles] = "collapse/deleted";
keys[Id::FilemanagerCommand] = "filemanager/command";
keys[Id::TerminalCommand] = "terminal/command";
keys[Id::TerminalName] = "terminal/name";
keys[Id::TerminalPath] = "terminal/path";
keys[Id::DontTranslate] = "translation/disable";
keys[Id::StoreCredentials] = "credential/store";
keys[Id::AllowSingleInstanceOnly] = "singleInstance";
keys[Id::CheckForUpdatesAutomatically] = "update/check";
keys[Id::InstallUpdatesAutomatically] = "update/download";
keys[Id::SkippedUpdates] = "update/skip";
keys[Id::SshConfigFilePath] = "ssh/configFilePath";
keys[Id::SshKeyFilePath] = "ssh/keyFilePath";
keys[Id::CommitMergeImmediately] = "merge/commit";
keys[Id::ShowCommitsInCompactMode] = "commit/compact";
keys[Id::ShowChangedFilesAsList] = "doubletreeview/listview";
keys[Id::ShowChangedFilesInSingleView] = "doubletreeview/single";
}

void Prompt::initialize(QMap<PromptKind, QString> &keys) {
void Prompt::initialize(QMap<Kind, QString> &keys) {
const QString root("window/prompt/");
keys[PromptKind::PromptMerge] = root + "merge";
keys[PromptKind::PromptStash] = root + "stash";
keys[PromptKind::PromptRevert] = root + "revert";
keys[PromptKind::PromptCherryPick] = root + "cherrypick";
keys[PromptKind::PromptDirectories] = root + "directories";
keys[PromptKind::PromptLargeFiles] = root + "largeFiles";
keys[Kind::Merge] = root + "merge";
keys[Kind::Stash] = root + "stash";
keys[Kind::Revert] = root + "revert";
keys[Kind::CherryPick] = root + "cherrypick";
keys[Kind::Directories] = root + "directories";
keys[Kind::LargeFiles] = root + "largeFiles";
}
48 changes: 39 additions & 9 deletions src/conf/Setting.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,42 @@ class Setting : public SettingsTempl<Setting> {

public:
enum class Id {
FetchAutomatically,
AutomaticFetchPeriodInMinutes,
PushAfterEachCommit,
UpdateSubmodulesAfterPullAndClone,
PruneAfterFetch,
FontFamily,
FontSize,
UseTabsForIndent,
IndentWidth,
TabWidth,
ShowHeatmapInBlameMargin,
ColorTheme,
ShowFullRepoPath,
HideLogAutomatically,
OpenSubmodulesInTabs,
OpenAllReposInTabs,
HideMenuBar,
ShowAvatars,
AutoCollapseAddedFiles,
AutoCollapseDeletedFiles,
FilemanagerCommand,
TerminalCommand,
TerminalName,
TerminalPath,
DontTranslate,
StoreCredentials,
AllowSingleInstanceOnly,
CheckForUpdatesAutomatically,
InstallUpdatesAutomatically,
SkippedUpdates,
SshConfigFilePath,
SshKeyFilePath,
CommitMergeImmediately,
ShowCommitsInCompactMode,
ShowChangedFilesAsList,
ShowChangedFilesInSingleView,
};
Q_ENUM(Id)

Expand All @@ -36,18 +71,13 @@ class Setting : public SettingsTempl<Setting> {
};

class Prompt : public SettingsTempl<Prompt> {
Q_GADGET

public:
enum class PromptKind {
PromptStash,
PromptMerge,
PromptRevert,
PromptCherryPick,
PromptDirectories,
PromptLargeFiles
};
enum class Kind { Stash, Merge, Revert, CherryPick, Directories, LargeFiles };
Q_ENUM(Kind)

static void initialize(QMap<PromptKind, QString> &keys);
static void initialize(QMap<Kind, QString> &keys);
};

#endif
42 changes: 22 additions & 20 deletions src/conf/Settings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ QVariant lookup(const QVariantMap &root, const QString &key) {
return QVariant();
}

QString promptKey(Prompt::PromptKind kind) { return Prompt::key(kind); }
QString promptKey(Prompt::Kind kind) { return Prompt::key(kind); }

QDir rootDir() {
QDir dir(QCoreApplication::applicationDirPath());
Expand All @@ -68,16 +68,6 @@ Settings::Settings(QObject *parent) : QObject(parent) {

QString Settings::group() const { return mGroup.join("/"); }

void Settings::beginGroup(const QString &prefix) {
mGroup.append(prefix);
mCurrentMap = lookup(mDefaults, group()).toMap();
}

void Settings::endGroup() {
mGroup.removeLast();
mCurrentMap = lookup(mDefaults, group()).toMap();
}

QVariant Settings::value(const QString &key) const {
return value(key, defaultValue(key));
}
Expand Down Expand Up @@ -117,6 +107,10 @@ QVariant Settings::value(Setting::Id id) const {
return value(Setting::key(id));
}

QVariant Settings::value(Setting::Id id, const QVariant &defaultValue) const {
return value(Setting::key(id), defaultValue);
}

void Settings::setValue(Setting::Id id, const QVariant &value) {
setValue(Setting::key(id), value);
}
Expand Down Expand Up @@ -162,36 +156,44 @@ QString Settings::kind(const QString &filename) {
return lexers.value(key).toMap().value("name").toString();
}

bool Settings::prompt(Prompt::PromptKind kind) const {
bool Settings::prompt(Prompt::Kind kind) const {
return value(promptKey(kind)).toBool();
}

void Settings::setPrompt(Prompt::PromptKind kind, bool prompt) {
void Settings::setPrompt(Prompt::Kind kind, bool prompt) {
setValue(promptKey(kind), prompt);
}

QString Settings::promptDescription(Prompt::PromptKind kind) const {
QString Settings::promptDescription(Prompt::Kind kind) const {
switch (kind) {
case Prompt::PromptKind::PromptStash:
case Prompt::Kind::Stash:
return tr("Prompt to edit stash message before stashing");

case Prompt::PromptKind::PromptMerge:
case Prompt::Kind::Merge:
return tr("Prompt to edit commit message before merging");

case Prompt::PromptKind::PromptRevert:
case Prompt::Kind::Revert:
return tr("Prompt to edit commit message before reverting");

case Prompt::PromptKind::PromptCherryPick:
case Prompt::Kind::CherryPick:
return tr("Prompt to edit commit message before cherry-picking");

case Prompt::PromptKind::PromptDirectories:
case Prompt::Kind::Directories:
return tr("Prompt to stage directories");

case Prompt::PromptKind::PromptLargeFiles:
case Prompt::Kind::LargeFiles:
return tr("Prompt to stage large files");
}
}

void Settings::setHotkey(const QString &action, const QString &hotkey) {
setValue("hotkeys/" + action, hotkey);
}

QString Settings::hotkey(const QString &action) const {
return value("hotkeys/" + action, "").toString();
}

bool Settings::isWhitespaceIgnored() const {
return value(kIgnoreWsKey).toBool();
}
Expand Down
28 changes: 15 additions & 13 deletions src/conf/Settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,27 +20,21 @@ class Settings : public QObject {
Q_OBJECT

public:
QString group() const;
void beginGroup(const QString &prefix);
void endGroup();

QVariant value(const QString &key) const;
QVariant value(const QString &key, const QVariant &defaultValue) const;
QVariant defaultValue(const QString &key) const;
void setValue(const QString &key, const QVariant &value,
bool refresh = false);

QVariant value(Setting::Id id) const;
QVariant value(Setting::Id id, const QVariant &defaultValue) const;
void setValue(Setting::Id id, const QVariant &value);

// Look up lexer name by file name.
QString lexer(const QString &filename);
QString kind(const QString &filename);

// prompt dialogs
bool prompt(Prompt::PromptKind kind) const;
void setPrompt(Prompt::PromptKind kind, bool prompt);
QString promptDescription(Prompt::PromptKind kind) const;
bool prompt(Prompt::Kind kind) const;
void setPrompt(Prompt::Kind kind, bool prompt);
QString promptDescription(Prompt::Kind kind) const;

void setHotkey(const QString &action, const QString &hotkey);
QString hotkey(const QString &action) const;

// ignore whitespace
bool isWhitespaceIgnored() const;
Expand Down Expand Up @@ -72,6 +66,14 @@ class Settings : public QObject {
private:
Settings(QObject *parent = nullptr);

QString group() const;

QVariant value(const QString &key) const;
QVariant value(const QString &key, const QVariant &defaultValue) const;
QVariant defaultValue(const QString &key) const;
void setValue(const QString &key, const QVariant &value,
bool refresh = false);

QStringList mGroup;
QVariantMap mDefaults;
QVariantMap mCurrentMap;
Expand Down
3 changes: 1 addition & 2 deletions src/cred/CredentialHelper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,13 @@
namespace {

const QString kLogKey = "credential/log";
const QString kStoreKey = "credential/store";

} // namespace

CredentialHelper *CredentialHelper::instance() {
static QPointer<CredentialHelper> instance;
if (!instance) {
if (Settings::instance()->value(kStoreKey).toBool()) {
if (Settings::instance()->value(Setting::Id::StoreCredentials).toBool()) {
#if defined(Q_OS_MAC)
instance = new GitCredential("osxkeychain");
#elif defined(Q_OS_WIN)
Expand Down
Loading

0 comments on commit 3b69f6f

Please sign in to comment.