GpgFrontend Project
A Free, Powerful, Easy-to-Use, Compact, Cross-Platform, and Installation-Free OpenPGP(pgp) Crypto Tool.
GlobalSettingStation.h
1 
29 #ifndef GPGFRONTEND_GLOBALSETTINGSTATION_H
30 #define GPGFRONTEND_GLOBALSETTINGSTATION_H
31 
32 #include <sys/_types/_int64_t.h>
33 
34 #include <filesystem>
35 
36 #include "GpgFrontendBuildInstallInfo.h"
37 #include "core/GpgFrontendCore.h"
38 #include "core/GpgFunctionObject.h"
39 
40 namespace GpgFrontend {
41 
46 class GPGFRONTEND_CORE_EXPORT GlobalSettingStation
47  : public SingletonFunctionObject<GlobalSettingStation> {
48  public:
53  explicit GlobalSettingStation(
54  int channel = SingletonFunctionObject::GetDefaultChannel()) noexcept;
55 
60  ~GlobalSettingStation() noexcept override;
61 
67  libconfig::Setting &GetUISettings() noexcept;
68 
74  template <typename T>
75  T LookupSettings(std::string path, T default_value) noexcept {
76  T value = default_value;
77  try {
78  value = static_cast<T>(GetUISettings().lookup(path));
79  } catch (...) {
80  SPDLOG_WARN("setting not found: {}", path);
81  }
82  return value;
83  }
84 
90  [[nodiscard]] std::filesystem::path GetAppDir() const { return app_path_; }
91 
92  [[nodiscard]] std::filesystem::path GetAppDataPath() const {
93  return app_data_path_;
94  }
95 
101  [[nodiscard]] std::filesystem::path GetLogDir() const {
102  return app_log_path_;
103  }
104 
110  [[nodiscard]] std::filesystem::path GetStandaloneDatabaseDir() const {
111  auto db_path = app_configure_path_ / "db";
112  if (!std::filesystem::exists(db_path)) {
113  std::filesystem::create_directory(db_path);
114  }
115  return db_path;
116  }
117 
118  [[nodiscard]] std::filesystem::path GetAppConfigPath() const {
119  return app_configure_path_;
120  }
121 
127  [[nodiscard]] std::filesystem::path GetStandaloneGpgBinDir() const {
128  return app_resource_path_ / "gpg1.4" / "gpg";
129  }
130 
136  [[nodiscard]] std::filesystem::path GetLocaleDir() const {
137  return app_locale_path_;
138  }
139 
145  [[nodiscard]] std::filesystem::path GetResourceDir() const {
146  return app_resource_path_;
147  }
148 
154  [[nodiscard]] std::filesystem::path GetCertsDir() const {
155  return app_resource_path_ / "certs";
156  }
157 
158  [[nodiscard]] std::string GetLogFilesSize() const;
159 
160  [[nodiscard]] std::string GetDataObjectsFilesSize() const;
161 
162  void ClearAllLogFiles() const;
163 
164  void ClearAllDataObjects() const;
165 
170  void SyncSettings() noexcept;
171 
172  private:
173  std::filesystem::path app_path_ = QCoreApplication::applicationDirPath()
174  .toStdString();
175  std::filesystem::path app_data_path_ =
176  QStandardPaths::writableLocation(QStandardPaths::AppLocalDataLocation)
177  .toStdString();
178  std::filesystem::path app_log_path_ =
179  app_data_path_ / "logs";
180  std::filesystem::path app_data_objs_path_ =
181  app_data_path_ / "data_objs";
182 
183 #ifdef LINUX_INSTALL_BUILD
184  std::filesystem::path app_resource_path_ =
185  std::filesystem::path(APP_LOCALSTATE_PATH) /
186  "gpgfrontend";
187 #else
188  std::filesystem::path app_resource_path_ =
189  RESOURCE_DIR_BOOST_PATH(app_path_);
190 #endif
191 
192 #ifdef LINUX_INSTALL_BUILD
193  std::filesystem::path app_locale_path_ =
194  std::string(APP_LOCALE_PATH);
195 #else
196  std::filesystem::path app_locale_path_ =
197  app_resource_path_ / "locales";
198 #endif
199 
200  std::filesystem::path app_configure_path_ =
201  QStandardPaths::writableLocation(QStandardPaths::AppConfigLocation)
202  .toStdString();
203  std::filesystem::path ui_config_dir_path_ =
204  app_configure_path_ / "conf";
205  std::filesystem::path ui_config_path_ =
206  ui_config_dir_path_ / "main.cfg";
207 
208  libconfig::Config ui_cfg_;
209 
214  void init_app_secure_key();
215 
220  int64_t get_files_size_at_path(std::filesystem::path path,
221  std::string filename_pattern) const;
222 
227  std::string get_human_readable_size(int64_t size) const;
228 
233  void delete_all_files(std::filesystem::path path,
234  std::string filename_pattern) const;
235 };
236 } // namespace GpgFrontend
237 
238 #endif // GPGFRONTEND_GLOBALSETTINGSTATION_H
Definition: GlobalSettingStation.h:47
std::filesystem::path GetLocaleDir() const
Get the Locale Dir object.
Definition: GlobalSettingStation.h:136
libconfig::Config ui_cfg_
UI Configure File.
Definition: GlobalSettingStation.h:208
std::filesystem::path GetCertsDir() const
Get the Certs Dir object.
Definition: GlobalSettingStation.h:154
std::filesystem::path GetLogDir() const
Get the Log Dir object.
Definition: GlobalSettingStation.h:101
std::filesystem::path GetStandaloneGpgBinDir() const
Get the Standalone Gpg Bin Dir object.
Definition: GlobalSettingStation.h:127
std::filesystem::path GetAppDir() const
Get the App Dir object.
Definition: GlobalSettingStation.h:90
std::filesystem::path GetStandaloneDatabaseDir() const
Get the Standalone Database Dir object.
Definition: GlobalSettingStation.h:110
~GlobalSettingStation() noexcept override
Destroy the Global Setting Station object.
std::filesystem::path GetResourceDir() const
Get the Resource Dir object.
Definition: GlobalSettingStation.h:145
Definition: GpgFunctionObject.h:150
static int GetDefaultChannel()
Get the Default Channel object.
Definition: GpgFunctionObject.h:251
Definition: CoreCommonUtil.cpp:31