BibleTime
btconfig.h
Go to the documentation of this file.
1 /*********
2 *
3 * In the name of the Father, and of the Son, and of the Holy Spirit.
4 *
5 * This file is part of BibleTime's source code, https://bibletime.info/
6 *
7 * Copyright 1999-2021 by the BibleTime developers.
8 * The BibleTime source code is licensed under the GNU General Public License
9 * version 2.0.
10 *
11 **********/
12 
13 #pragma once
14 
15 #include "btconfigcore.h"
16 
17 #include <QCoreApplication>
18 #include <QFont>
19 #include <QHash>
20 #include <QList>
21 #include <QMap>
22 #include <QMetaType>
23 #include <QPair>
24 #include <QString>
25 #include <QStringList>
26 #include "../../util/btassert.h"
27 #include "../btglobal.h"
28 
29 
30 class CSwordModuleInfo;
31 class Language;
32 class QKeySequence;
33 namespace sword { class ListKey; }
34 
35 class BtConfig: public BtConfigCore {
36 
37  Q_DECLARE_TR_FUNCTIONS(BtConfig)
38 
39  friend class BibleTimeApp;
40 
41 public: // types:
42 
43  /*!
44  * The first parameter indicates whether the custom font should be used or not.
45  * The second parameter is the custom font that has been set.
46  */
47  using FontSettingsPair = QPair<bool, QFont>;
48  using StringMap = QMap<QString, QString>;
49  using ShortcutsMap = QHash<QString, QList<QKeySequence> >;
50 
51 private: // types:
52 
53  enum InitState {
55  INIT_OK = 0,
57  };
58 
59 public: // methods:
60 
61  static BtConfig & getInstance();
62 
63  /**
64  \returns the key of the current session.
65  */
66  QString const & currentSessionKey() const { return m_currentSessionKey; }
67 
68  /**
69  \returns the name of the current session.
70  */
71  QString currentSessionName() const {
72  auto it(m_sessionNames.constFind(m_currentSessionKey));
73  BT_ASSERT(it != m_sessionNames.constEnd());
74  return it.value();
75  }
76 
77  /** \returns a hashmap with the keys and printable names of the sessions. */
79 
80  /**
81  \brief Notifies the configuration system that the session settings
82  should be read from and saved to the given session.
83 
84  \pre The session with the given key must exist.
85  \param[in] key the key of the session to switch to.
86  \post the sessionValue() and value() methods will work with the settings
87  of the given session.
88  */
89  void setCurrentSession(const QString & key);
90 
91  /**
92  \brief Creates a new session with the given name.
93 
94  \pre The given name must not be an empty string.
95  \param[in] name the name of the session
96  \returns the key of the created session.
97  */
98  QString addSession(const QString & name);
99 
100  /**
101  \brief Deletes the session with the given key.
102 
103  \pre The session with the given key must exist.
104  \pre The session with the given key must not be the current session.
105  \param[in] key the key of the session to delete.
106  \post The session with the given key and its settings are deleted.
107  \returns whether deleting the session was successful.
108  */
109  void deleteSession(const QString & key);
110 
111  BtConfigCore session() const;
112 
113  /*!
114  * \brief Function to set a module decryption key.
115  *
116  * This helper function will set a module decryption key
117  * in the configuration. Any previous key will be overwritten.
118  *
119  * \param[in] name Name of module to set the key for
120  * \param[in] key Decryption key to set as string
121  */
122  void setModuleEncryptionKey(const QString & name, const QString & key);
123 
124  /*!
125  * \brief Function to get a module decryption key.
126  *
127  * This helper function will retrieve a previously set
128  * module decryption key from the configuration. If the key
129  * is not set it will return a null string.
130  *
131  * \param[in] name Name of module to retrieve the key for
132  * \returns Decryption key as a string
133  */
134  QString getModuleEncryptionKey(const QString & name);
135 
136  /*!
137  * \brief Gets the shortcuts for the given group.
138  *
139  * Returns a hash of shortcuts for strings for the respective
140  * shortcut group.
141  * \param[in] shortcutGroup The group to retrieve shortcuts for.
142  * \returns Hash of strings and lists of shortcuts.
143  */
144  ShortcutsMap getShortcuts(QString const & shortcutGroup);
145 
146  /*!
147  * \brief Sets the shortcuts for the given group.
148  *
149  * Writes a hash of shortcuts for strings for the respective
150  * shortcut group.
151  * \param[in] shortcutGroup The group to retrieve shortcuts for.
152  * \param[in] Hash of strings and lists of shortcuts to write.
153  */
154  void setShortcuts(QString const & shortcutGroup,
155  ShortcutsMap const & shortcuts);
156 
157  /** \returns FilterOptions structure containing filter settings. */
159 
161  auto const group = const_cast<BtConfig *>(this)->session();
163  }
164 
165  /**
166  \brief Saves the current filter options.
167  \param[in] options The filter options to save.
168  */
169  static void storeFilterOptionsToGroup(FilterOptions const & options,
170  BtConfigCore & group);
171 
172  /** \returns DisplayOptions structure containing display settings. */
174  BtConfigCore const & group);
175 
177  auto const group = const_cast<BtConfig *>(this)->session();
179  }
180 
181  /**
182  \brief Saves the current display options.
183  \param[in] options The display options to save.
184  */
185  static void storeDisplayOptionsToGroup(DisplayOptions const & options,
186  BtConfigCore & group);
187 
188  /*!
189  * Returns a default font that is suitable for the current language.
190  * \returns QFont suitable for current language.
191  */
192  QFont const & getDefaultFont() const { return m_defaultFont; }
193 
194  /// \todo: put FontSettingsPair in QVariant directly
195  /*!
196  * \brief Set font for a language.
197  *
198  * Sets a FontSettingsPair for the language given.
199  * \param[in] language pointer to a language to set the font for
200  * \param[in] fontSettings FontSettingsPair to set
201  */
202  void setFontForLanguage(Language const & language,
203  FontSettingsPair const & fontSettings);
204 
205  /*!
206  * \brief Get font for a language.
207  *
208  * Gets a FontSettingsPair for the language given. If no font has been saved
209  * a default font is returned.
210  * \param[in] language pointer to a language to get the font for.
211  * \returns FontSettingsPair for given language
212  */
213  FontSettingsPair getFontForLanguage(Language const & language);
214 
215  /// \todo: unit test these functions
216  /*!
217  * Returns the searchScopes for the current locale.
218  *
219  * This function retrieves the search scopes of the
220  * "properties/searchScopes" property and converts them
221  * to the current locale.
222  *
223  * \returns Search scopes in current locale.
224  */
225  StringMap getSearchScopesForCurrentLocale(const QStringList& scopeModules);
226 
227  /*!
228  * Sets the searchScopes given in the current locale.
229  *
230  * This function sets the search scopes of the
231  * "properties/searchScopes" property, the scopes are
232  * converted to the english locale before saving them.
233  *
234  * \param[in] searchScopes Search scopes in any locale.
235  */
236  void setSearchScopesWithCurrentLocale(const QStringList& scopeModules, StringMap searchScopes);
237 
238  /** \returns the configured language code for book names. */
239  QString booknameLanguage();
240 
241  /*
242  * Ensure that the verse range is valid for at least one of the scopeModules
243  */
244  static sword::ListKey parseVerseListWithModules(const QString& data, const QStringList& scopeModules);
245 
246  /*!
247  * Deletes the searchScopes given in the current locale.
248  */
250 
251  /*!
252  * \brief Returns default sword module info class for a given module type.
253  *
254  * This is basically a convenience function for getting the respective
255  * "settings/defaults/ *" variable and searching that module manually.
256  * If module is not installed 0 will be returned.
257  * \param[in] moduleType module type to return the default sword module info for
258  * \returns sword module info pointer or 0
259  */
260  CSwordModuleInfo * getDefaultSwordModuleByType(const QString & moduleType);
261 
262  /*!
263  * \brief Sets the default sword module for a module type.
264  *
265  * This is basically a convenience function for setting the "settings/defaults/ *"
266  * variables to the module->name() string manually.
267  * 0 is allowed as the module, then the default module will be unset.
268  * \param[in] moduleType module type to set
269  * \param[in] module the sword module info to set as default module
270  */
271  void setDefaultSwordModuleByType(const QString & moduleType,
272  const CSwordModuleInfo * const module);
273 
274 private: // methods:
275 
276  explicit BtConfig(const QString & settingsFile);
277 
278  static InitState initBtConfig();
279  static void forceMigrate();
280 
281  static void destroyInstance();
282 
283 private: // fields:
284 
285  static BtConfig * m_instance; //!< singleton instance
286 
287  QFont m_defaultFont; //!< default font used when no special one is set
288  QHash<Language const *, FontSettingsPair> m_fontCache; //!< a cache for the fonts saved in the configuration file for speed
289 
291 
294 
295 }; /* class BtConfig */
296 
297 // declare types used in configuration as metatype so they can be saved directly into the configuration
298 Q_DECLARE_METATYPE(BtConfig::StringMap)
299 Q_DECLARE_METATYPE(QList<int>)
300 
301 /*!
302  * \brief This is a shortchand for BtConfig::getInstance().
303  * \returns BtConfig singleton instance.
304  */
305 inline BtConfig & btConfig() {
306  return BtConfig::getInstance();
307 }
#define BT_ASSERT(...)
Definition: btassert.h:17
BtConfig & btConfig()
This is a shortchand for BtConfig::getInstance().
Definition: btconfig.h:305
friend class BtConfig
Definition: btconfigcore.h:31
BtConfigCore group(Prefix &&prefix) const &
Definition: btconfigcore.h:93
QMap< QString, QString > StringMap
Definition: btconfig.h:48
QString getModuleEncryptionKey(const QString &name)
Function to get a module decryption key.
Definition: btconfig.cpp:228
static void storeFilterOptionsToGroup(FilterOptions const &options, BtConfigCore &group)
Saves the current filter options.
Definition: btconfig.cpp:301
BtConfigCore session() const
Definition: btconfig.cpp:213
static FilterOptions loadFilterOptionsFromGroup(BtConfigCore const &group)
Definition: btconfig.cpp:283
void setFontForLanguage(Language const &language, FontSettingsPair const &fontSettings)
Set font for a language.
Definition: btconfig.cpp:338
QPair< bool, QFont > FontSettingsPair
Definition: btconfig.h:47
static DisplayOptions loadDisplayOptionsFromGroup(BtConfigCore const &group)
Definition: btconfig.cpp:320
QHash< QString, QString > m_sessionNames
Definition: btconfig.h:292
void setDefaultSwordModuleByType(const QString &moduleType, const CSwordModuleInfo *const module)
Sets the default sword module for a module type.
Definition: btconfig.cpp:512
void setSearchScopesWithCurrentLocale(const QStringList &scopeModules, StringMap searchScopes)
Definition: btconfig.cpp:441
QHash< QString, QString > sessionNames() const
Definition: btconfig.h:78
QString m_currentSessionKey
Definition: btconfig.h:293
QFont const & getDefaultFont() const
Definition: btconfig.h:192
QString currentSessionName() const
Definition: btconfig.h:71
static InitState initBtConfig()
Definition: btconfig.cpp:141
static void destroyInstance()
Definition: btconfig.cpp:216
DisplayOptions getDisplayOptions() const
Definition: btconfig.h:176
QString addSession(const QString &name)
Creates a new session with the given name.
Definition: btconfig.cpp:177
void deleteSearchScopesWithCurrentLocale()
Definition: btconfig.cpp:499
static void forceMigrate()
Definition: btconfig.cpp:161
static sword::ListKey parseVerseListWithModules(const QString &data, const QStringList &scopeModules)
Definition: btconfig.cpp:486
void setModuleEncryptionKey(const QString &name, const QString &key)
Function to set a module decryption key.
Definition: btconfig.cpp:221
QHash< Language const *, FontSettingsPair > m_fontCache
a cache for the fonts saved in the configuration file for speed
Definition: btconfig.h:288
void setShortcuts(QString const &shortcutGroup, ShortcutsMap const &shortcuts)
Sets the shortcuts for the given group.
Definition: btconfig.cpp:267
static BtConfig * m_instance
singleton instance
Definition: btconfig.h:285
QFont m_defaultFont
default font used when no special one is set
Definition: btconfig.h:287
QHash< QString, QList< QKeySequence > > ShortcutsMap
Definition: btconfig.h:49
FontSettingsPair getFontForLanguage(Language const &language)
Get font for a language.
Definition: btconfig.cpp:364
@ INIT_NEED_UNIMPLEMENTED_FORWARD_MIGRATE
Definition: btconfig.h:56
@ INIT_NEED_UNIMPLEMENTED_BACKWARD_MIGRATE
Definition: btconfig.h:54
@ INIT_OK
Definition: btconfig.h:55
void setCurrentSession(const QString &key)
Notifies the configuration system that the session settings should be read from and saved to the give...
Definition: btconfig.cpp:169
QString const & currentSessionKey() const
Definition: btconfig.h:66
void deleteSession(const QString &key)
Deletes the session with the given key.
Definition: btconfig.cpp:205
QString booknameLanguage()
Definition: btconfig.cpp:473
CSwordModuleInfo * getDefaultSwordModuleByType(const QString &moduleType)
Returns default sword module info class for a given module type.
Definition: btconfig.cpp:503
FilterOptions getFilterOptions() const
Definition: btconfig.h:160
ShortcutsMap getShortcuts(QString const &shortcutGroup)
Gets the shortcuts for the given group.
Definition: btconfig.cpp:233
static void storeDisplayOptionsToGroup(DisplayOptions const &options, BtConfigCore &group)
Saves the current display options.
Definition: btconfig.cpp:328
static StringMap m_defaultSearchScopes
Definition: btconfig.h:290
static BtConfig & getInstance()
Definition: btconfig.cpp:164
StringMap getSearchScopesForCurrentLocale(const QStringList &scopeModules)
Definition: btconfig.cpp:412