main_app_config.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /*
  2. This file is part of Telegram Desktop,
  3. the official desktop application for the Telegram messaging service.
  4. For license and copyright information please follow this link:
  5. https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
  6. */
  7. #pragma once
  8. #include "mtproto/sender.h"
  9. #include "base/algorithm.h"
  10. namespace Ui {
  11. struct ColorIndicesCompressed;
  12. } // namespace Ui
  13. namespace Main {
  14. class Account;
  15. class AppConfig final {
  16. public:
  17. explicit AppConfig(not_null<Account*> account);
  18. ~AppConfig();
  19. void start();
  20. template <typename Type>
  21. [[nodiscard]] Type get(const QString &key, Type fallback) const {
  22. if constexpr (std::is_same_v<Type, double>) {
  23. return getDouble(key, fallback);
  24. } else if constexpr (std::is_same_v<Type, int>) {
  25. return int(base::SafeRound(getDouble(key, double(fallback))));
  26. } else if constexpr (std::is_same_v<Type, QString>) {
  27. return getString(key, fallback);
  28. } else if constexpr (std::is_same_v<Type, std::vector<QString>>) {
  29. return getStringArray(key, std::move(fallback));
  30. } else if constexpr (
  31. std::is_same_v<Type, base::flat_map<QString, QString>>) {
  32. return getStringMap(key, std::move(fallback));
  33. } else if constexpr (std::is_same_v<Type, std::vector<int>>) {
  34. return getIntArray(key, std::move(fallback));
  35. } else if constexpr (std::is_same_v<Type, bool>) {
  36. return getBool(key, fallback);
  37. }
  38. }
  39. [[nodiscard]] rpl::producer<> refreshed() const;
  40. [[nodiscard]] rpl::producer<> value() const;
  41. [[nodiscard]] bool suggestionCurrent(const QString &key) const;
  42. [[nodiscard]] rpl::producer<> suggestionRequested(
  43. const QString &key) const;
  44. void dismissSuggestion(const QString &key);
  45. [[nodiscard]] bool newRequirePremiumFree() const;
  46. [[nodiscard]] auto ignoredRestrictionReasons() const
  47. -> const std::vector<QString> & {
  48. return _ignoreRestrictionReasons;
  49. }
  50. [[nodiscard]] auto ignoredRestrictionReasonsChanges() const {
  51. return _ignoreRestrictionChanges.events();
  52. }
  53. [[nodiscard]] int quoteLengthMax() const;
  54. [[nodiscard]] int stargiftConvertPeriodMax() const;
  55. [[nodiscard]] const std::vector<QString> &startRefPrefixes();
  56. [[nodiscard]] bool starrefSetupAllowed() const;
  57. [[nodiscard]] bool starrefJoinAllowed() const;
  58. [[nodiscard]] int starrefCommissionMin() const;
  59. [[nodiscard]] int starrefCommissionMax() const;
  60. [[nodiscard]] float64 starsWithdrawRate() const;
  61. [[nodiscard]] bool paidMessagesAvailable() const;
  62. [[nodiscard]] int paidMessageStarsMax() const;
  63. [[nodiscard]] int paidMessageCommission() const;
  64. [[nodiscard]] int pinnedGiftsLimit() const;
  65. void refresh(bool force = false);
  66. private:
  67. void refreshDelayed();
  68. template <typename Extractor>
  69. [[nodiscard]] auto getValue(
  70. const QString &key,
  71. Extractor &&extractor) const;
  72. [[nodiscard]] bool getBool(
  73. const QString &key,
  74. bool fallback) const;
  75. [[nodiscard]] double getDouble(
  76. const QString &key,
  77. double fallback) const;
  78. [[nodiscard]] QString getString(
  79. const QString &key,
  80. const QString &fallback) const;
  81. [[nodiscard]] std::vector<QString> getStringArray(
  82. const QString &key,
  83. std::vector<QString> &&fallback) const;
  84. [[nodiscard]] base::flat_map<QString, QString> getStringMap(
  85. const QString &key,
  86. base::flat_map<QString, QString> &&fallback) const;
  87. [[nodiscard]] std::vector<int> getIntArray(
  88. const QString &key,
  89. std::vector<int> &&fallback) const;
  90. void updateIgnoredRestrictionReasons(std::vector<QString> was);
  91. const not_null<Account*> _account;
  92. std::optional<MTP::Sender> _api;
  93. mtpRequestId _requestId = 0;
  94. int32 _hash = 0;
  95. bool _pendingRefresh = false;
  96. base::flat_map<QString, MTPJSONValue> _data;
  97. rpl::event_stream<> _refreshed;
  98. base::flat_set<QString> _dismissedSuggestions;
  99. std::vector<QString> _ignoreRestrictionReasons;
  100. rpl::event_stream<std::vector<QString>> _ignoreRestrictionChanges;
  101. std::vector<QString> _startRefPrefixes;
  102. rpl::lifetime _lifetime;
  103. };
  104. } // namespace Main