options.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. // This file is part of Desktop App Toolkit,
  2. // a set of libraries for developing nice desktop applications.
  3. //
  4. // For license and copyright information please follow this link:
  5. // https://github.com/desktop-app/legal/blob/master/LEGAL
  6. //
  7. #include "base/options.h"
  8. #include "base/call_delayed.h"
  9. #include "base/variant.h"
  10. #include "base/debug_log.h"
  11. #include <QtCore/QJsonDocument>
  12. #include <QtCore/QJsonObject>
  13. #include <QtCore/QJsonValue>
  14. #include <QtCore/QFile>
  15. namespace base::options {
  16. namespace details {
  17. namespace {
  18. constexpr auto kSaveDelay = crl::time(1000);
  19. bool WriteScheduled/* = false*/;
  20. struct Compare {
  21. bool operator()(const char *a, const char *b) const noexcept {
  22. return strcmp(a, b) < 0;
  23. }
  24. };
  25. using MapType = base::flat_map<const char*, not_null<BasicOption*>, Compare>;
  26. [[nodiscard]] MapType &Map() {
  27. static auto result = MapType();
  28. return result;
  29. }
  30. [[nodiscard]] QString &LocalPath() {
  31. static auto result = QString();
  32. return result;
  33. }
  34. void Read(const QString &path) {
  35. auto file = QFile(path);
  36. if (!file.exists()) {
  37. return;
  38. } else if (!file.open(QIODevice::ReadOnly)) {
  39. LOG(("Experimental: Error opening file from '%1'.").arg(path));
  40. return;
  41. }
  42. auto error = QJsonParseError();
  43. const auto parsed = QJsonDocument::fromJson(file.readAll(), &error);
  44. if (error.error != QJsonParseError::NoError) {
  45. LOG(("Experimental: Error parsing json from '%1': %2 (%3)"
  46. ).arg(path
  47. ).arg(error.error
  48. ).arg(error.errorString()));
  49. return;
  50. } else if (!parsed.isObject()) {
  51. LOG(("Experimental: Non object in json from '%1'.").arg(path));
  52. return;
  53. }
  54. auto &map = Map();
  55. const auto values = parsed.object();
  56. for (auto i = values.begin(); i != values.end(); ++i) {
  57. const auto key = i.key().toLatin1() + char(0);
  58. const auto j = map.find(key.data());
  59. if (j == end(map)) {
  60. LOG(("Experimental: Unknown option '%1'.").arg(i.key()));
  61. continue;
  62. }
  63. const auto value = *i;
  64. v::match(j->second->value(), [&](const auto &current) {
  65. using T = std::remove_cvref_t<decltype(current)>;
  66. if constexpr (std::is_same_v<T, bool>) {
  67. if (value.isBool()) {
  68. j->second->set(value.toBool());
  69. return;
  70. }
  71. } else if constexpr (std::is_same_v<T, int>) {
  72. if (value.isDouble()) {
  73. j->second->set(value.toInt());
  74. return;
  75. }
  76. } else if constexpr (std::is_same_v<T, QString>) {
  77. if (value.isString()) {
  78. j->second->set(value.toString());
  79. return;
  80. }
  81. } else {
  82. static_assert(unsupported_type(T()));
  83. }
  84. LOG(("Experimental: Wrong option value type for '%1'."
  85. ).arg(i.key()));
  86. });
  87. }
  88. }
  89. void Write() {
  90. const auto &path = LocalPath();
  91. if (!WriteScheduled || path.isEmpty()) {
  92. return;
  93. }
  94. WriteScheduled = false;
  95. auto map = QJsonObject();
  96. for (const auto &[name, option] : Map()) {
  97. const auto &value = option->value();
  98. if (value != option->defaultValue()) {
  99. map.insert(name, v::match(value, [](const auto &current) {
  100. using T = std::remove_cvref_t<decltype(current)>;
  101. if constexpr (std::is_same_v<T, bool>
  102. || std::is_same_v<T, int>
  103. || std::is_same_v<T, QString>) {
  104. return QJsonValue(current);
  105. } else {
  106. static_assert(unsupported_type(T()));
  107. }
  108. }));
  109. }
  110. }
  111. if (map.isEmpty()) {
  112. QFile(path).remove();
  113. } else if (auto file = QFile(path); file.open(QIODevice::WriteOnly)) {
  114. file.write(QJsonDocument(map).toJson(QJsonDocument::Indented));
  115. } else {
  116. LOG(("Experimental: Could not write '%1'.").arg(path));
  117. }
  118. }
  119. } // namespace
  120. BasicOption::BasicOption(
  121. const char id[],
  122. const char name[],
  123. const char description[],
  124. ValueType defaultValue,
  125. Scope scope,
  126. bool restartRequired)
  127. : _value(defaultValue)
  128. , _defaultValue(std::move(defaultValue))
  129. , _id(QString::fromUtf8(id))
  130. , _name(QString::fromUtf8(name))
  131. , _description(QString::fromUtf8(description))
  132. , _scope(scope)
  133. , _restartRequired(restartRequired) {
  134. const auto [i, ok] = Map().emplace(id, this);
  135. Ensures(ok);
  136. }
  137. void BasicOption::set(ValueType value) {
  138. Expects(value.index() == _value.index());
  139. _value = std::move(value);
  140. if (!WriteScheduled && !LocalPath().isEmpty()) {
  141. WriteScheduled = true;
  142. call_delayed(kSaveDelay, [] { Write(); });
  143. }
  144. }
  145. const ValueType &BasicOption::value() const {
  146. return _value;
  147. }
  148. const ValueType &BasicOption::defaultValue() const {
  149. return _defaultValue;
  150. }
  151. const QString &BasicOption::id() const {
  152. return _id;
  153. }
  154. const QString &BasicOption::name() const {
  155. return _name;
  156. }
  157. const QString &BasicOption::description() const {
  158. return _description;
  159. }
  160. bool BasicOption::relevant() const {
  161. const auto scopeFn = std::get_if<ScopeFn>(&_scope);
  162. if (scopeFn) {
  163. return (*scopeFn)();
  164. }
  165. const auto scopeFlags = v::get<ScopeFlags>(_scope);
  166. #ifdef Q_OS_WIN
  167. return scopeFlags & windows;
  168. #elif defined Q_OS_MAC // Q_OS_WIN
  169. return scopeFlags & macos;
  170. #else // Q_OS_MAC || Q_OS_WIN
  171. return scopeFlags & linux;
  172. #endif // Q_OS_MAC || Q_OS_WIN
  173. }
  174. bool BasicOption::restartRequired() const {
  175. return _restartRequired;
  176. }
  177. Scope BasicOption::scope() const {
  178. return _scope;
  179. }
  180. BasicOption &Lookup(const char id[]) {
  181. const auto i = Map().find(id);
  182. Ensures(i != end(Map()));
  183. return *i->second;
  184. }
  185. } // namespace details
  186. bool changed() {
  187. for (const auto &[name, option] : details::Map()) {
  188. if (option->value() != option->defaultValue()) {
  189. return true;
  190. }
  191. }
  192. return false;
  193. }
  194. void reset() {
  195. for (const auto &[name, option] : details::Map()) {
  196. if (option->value() != option->defaultValue()) {
  197. option->set(option->defaultValue());
  198. }
  199. }
  200. }
  201. void init(const QString &path) {
  202. Expects(details::LocalPath().isEmpty());
  203. if (!path.isEmpty()) {
  204. details::Read(path);
  205. details::LocalPath() = path;
  206. static const auto guard = gsl::finally([] {
  207. details::Write();
  208. });
  209. }
  210. }
  211. } // namespace base::options