forked from flameshot-org/flameshot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfighandler.h
More file actions
189 lines (169 loc) · 8.19 KB
/
confighandler.h
File metadata and controls
189 lines (169 loc) · 8.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
// SPDX-License-Identifier: GPL-3.0-or-later
// SPDX-FileCopyrightText: 2017-2019 Alejandro Sirgo Rica & Contributors
#pragma once
#include "src/widgets/capture/capturetoolbutton.h"
#include <QSettings>
#include <QStringList>
#include <QVariant>
#include <QVector>
#define CONFIG_GROUP_GENERAL "General"
#define CONFIG_GROUP_SHORTCUTS "Shortcuts"
class QFileSystemWatcher;
class ValueHandler;
template<class T>
class QSharedPointer;
class QTextStream;
class AbstractLogger;
/**
* Declare and implement a getter for a config option. `KEY` is the option key
* as it appears in the config file, `TYPE` is the C++ type. At the same time
* `KEY` is the name of the generated getter function.
*/
// clang-format off
#define CONFIG_GETTER(KEY, TYPE) \
TYPE KEY() \
{ \
return value(QStringLiteral(#KEY)).value<TYPE>(); \
}
// clang-format on
/**
* Declare and implement a setter for a config option. `FUNC` is the name of the
* generated function, `KEY` is the option key as it appears in the config file
* and `TYPE` is the C++ type.
*/
#define CONFIG_SETTER(FUNC, KEY, TYPE) \
void FUNC(const TYPE& val) \
{ \
QString key = QStringLiteral(#KEY); \
/* Without this check, multiple `flameshot gui` instances running */ \
/* simultaneously would cause an endless loop of fileWatcher calls */ \
if (QVariant::fromValue(val) != value(key)) { \
setValue(key, QVariant::fromValue(val)); \
} \
}
/**
* Combines the functionality of `CONFIG_GETTER` and `CONFIG_SETTER`. `GETFUNC`
* is simultaneously the name of the getter function and the option key as it
* appears in the config file. `SETFUNC` is the name of the setter function.
* `TYPE` is the C++ type of the value.
*/
#define CONFIG_GETTER_SETTER(GETFUNC, SETFUNC, TYPE) \
CONFIG_GETTER(GETFUNC, TYPE) \
CONFIG_SETTER(SETFUNC, GETFUNC, TYPE)
class ConfigHandler : public QObject
{
Q_OBJECT
public:
explicit ConfigHandler();
static ConfigHandler* getInstance();
// Definitions of getters and setters for config options
// Some special cases are implemented regularly, without the macro
// NOTE: When adding new options, make sure to add an entry in
// recognizedGeneralOptions in the cpp file.
CONFIG_GETTER_SETTER(userColors, setUserColors, QVector<QColor>);
CONFIG_GETTER_SETTER(savePath, setSavePath, QString)
CONFIG_GETTER_SETTER(savePathFixed, setSavePathFixed, bool)
CONFIG_GETTER_SETTER(uiColor, setUiColor, QColor)
CONFIG_GETTER_SETTER(contrastUiColor, setContrastUiColor, QColor)
CONFIG_GETTER_SETTER(drawColor, setDrawColor, QColor)
CONFIG_GETTER_SETTER(predefinedColorPaletteLarge,
setPredefinedColorPaletteLarge,
bool)
CONFIG_GETTER_SETTER(fontFamily, setFontFamily, QString)
CONFIG_GETTER_SETTER(showHelp, setShowHelp, bool)
CONFIG_GETTER_SETTER(showSidePanelButton, setShowSidePanelButton, bool)
CONFIG_GETTER_SETTER(showDesktopNotification,
setShowDesktopNotification,
bool)
CONFIG_GETTER_SETTER(filenamePattern, setFilenamePattern, QString)
CONFIG_GETTER_SETTER(disabledTrayIcon, setDisabledTrayIcon, bool)
CONFIG_GETTER_SETTER(disabledGrimWarning, disabledGrimWarning, bool)
CONFIG_GETTER_SETTER(drawThickness, setDrawThickness, int)
CONFIG_GETTER_SETTER(drawFontSize, setDrawFontSize, int)
CONFIG_GETTER_SETTER(keepOpenAppLauncher, setKeepOpenAppLauncher, bool)
#if !defined(DISABLE_UPDATE_CHECKER)
CONFIG_GETTER_SETTER(checkForUpdates, setCheckForUpdates, bool)
#endif
CONFIG_GETTER_SETTER(allowMultipleGuiInstances,
setAllowMultipleGuiInstances,
bool)
CONFIG_GETTER_SETTER(autoCloseIdleDaemon, setAutoCloseIdleDaemon, bool)
CONFIG_GETTER_SETTER(showStartupLaunchMessage,
setShowStartupLaunchMessage,
bool)
CONFIG_GETTER_SETTER(contrastOpacity, setContrastOpacity, int)
CONFIG_GETTER_SETTER(copyURLAfterUpload, setCopyURLAfterUpload, bool)
CONFIG_GETTER_SETTER(historyConfirmationToDelete,
setHistoryConfirmationToDelete,
bool)
CONFIG_GETTER_SETTER(uploadHistoryMax, setUploadHistoryMax, int)
CONFIG_GETTER_SETTER(saveAfterCopy, setSaveAfterCopy, bool)
CONFIG_GETTER_SETTER(copyPathAfterSave, setCopyPathAfterSave, bool)
CONFIG_GETTER_SETTER(saveAsFileExtension, setSaveAsFileExtension, QString)
CONFIG_GETTER_SETTER(antialiasingPinZoom, setAntialiasingPinZoom, bool)
CONFIG_GETTER_SETTER(useJpgForClipboard, setUseJpgForClipboard, bool)
CONFIG_GETTER_SETTER(uploadWithoutConfirmation,
setUploadWithoutConfirmation,
bool)
CONFIG_GETTER_SETTER(ignoreUpdateToVersion,
setIgnoreUpdateToVersion,
QString)
CONFIG_GETTER_SETTER(undoLimit, setUndoLimit, int)
CONFIG_GETTER_SETTER(buttons, setButtons, QList<CaptureTool::Type>)
CONFIG_GETTER_SETTER(showMagnifier, setShowMagnifier, bool)
CONFIG_GETTER_SETTER(squareMagnifier, setSquareMagnifier, bool)
CONFIG_GETTER_SETTER(copyOnDoubleClick, setCopyOnDoubleClick, bool)
CONFIG_GETTER_SETTER(uploadClientSecret, setUploadClientSecret, QString)
CONFIG_GETTER_SETTER(saveLastRegion, setSaveLastRegion, bool)
CONFIG_GETTER_SETTER(showSelectionGeometry, setShowSelectionGeometry, int)
CONFIG_GETTER_SETTER(jpegQuality, setJpegQuality, int)
CONFIG_GETTER_SETTER(showSelectionGeometryHideTime,
showSelectionGeometryHideTime,
int)
// SPECIAL CASES
bool startupLaunch();
void setStartupLaunch(const bool);
void setAllTheButtons();
void setToolSize(CaptureTool::Type toolType, int size);
int toolSize(CaptureTool::Type toolType);
// DEFAULTS
QString filenamePatternDefault();
void setDefaultSettings();
QString configFilePath() const;
// GENERIC GETTERS AND SETTERS
bool setShortcut(const QString& actionName, const QString& shortcut);
QString shortcut(const QString& actionName);
void setValue(const QString& key, const QVariant& value);
QVariant value(const QString& key) const;
void remove(const QString& key);
void resetValue(const QString& key);
// INFO
static QSet<QString>& recognizedGeneralOptions();
static QSet<QString>& recognizedShortcutNames();
QSet<QString> keysFromGroup(const QString& group) const;
// ERROR HANDLING
bool checkForErrors(AbstractLogger* log = nullptr) const;
bool checkUnrecognizedSettings(AbstractLogger* log = nullptr,
QList<QString>* offenders = nullptr) const;
bool checkShortcutConflicts(AbstractLogger* log = nullptr) const;
bool checkSemantics(AbstractLogger* log = nullptr,
QList<QString>* offenders = nullptr) const;
void checkAndHandleError() const;
void setErrorState(bool error) const;
bool hasError() const;
QString errorMessage() const;
signals:
void error() const;
void errorResolved() const;
void fileChanged() const;
private:
mutable QSettings m_settings;
static bool m_hasError, m_errorCheckPending, m_skipNextErrorCheck;
static QSharedPointer<QFileSystemWatcher> m_configWatcher;
void ensureFileWatched() const;
QSharedPointer<ValueHandler> valueHandler(const QString& key) const;
void assertKeyRecognized(const QString& key) const;
bool isShortcut(const QString& key) const;
QString baseName(const QString& key) const;
void cleanUnusedKeys(const QString& group, const QSet<QString>& keys) const;
};