Client.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /*
  2. * Copyright (c) 2021, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibConfig/Client.h>
  7. #include <LibConfig/Listener.h>
  8. namespace Config {
  9. static RefPtr<Client> s_the = nullptr;
  10. Client& Client::the()
  11. {
  12. if (!s_the || !s_the->is_open()) {
  13. s_the = Client::try_create().release_value_but_fixme_should_propagate_errors();
  14. }
  15. return *s_the;
  16. }
  17. void Client::pledge_domains(Vector<ByteString> const& domains)
  18. {
  19. async_pledge_domains(domains);
  20. }
  21. void Client::monitor_domain(ByteString const& domain)
  22. {
  23. async_monitor_domain(domain);
  24. }
  25. Vector<ByteString> Client::list_keys(StringView domain, StringView group)
  26. {
  27. return list_config_keys(domain, group);
  28. }
  29. Vector<ByteString> Client::list_groups(StringView domain)
  30. {
  31. return list_config_groups(domain);
  32. }
  33. ByteString Client::read_string(StringView domain, StringView group, StringView key, StringView fallback)
  34. {
  35. return read_string_value(domain, group, key).value_or(fallback);
  36. }
  37. i32 Client::read_i32(StringView domain, StringView group, StringView key, i32 fallback)
  38. {
  39. return read_i32_value(domain, group, key).value_or(fallback);
  40. }
  41. u32 Client::read_u32(StringView domain, StringView group, StringView key, u32 fallback)
  42. {
  43. return read_u32_value(domain, group, key).value_or(fallback);
  44. }
  45. bool Client::read_bool(StringView domain, StringView group, StringView key, bool fallback)
  46. {
  47. return read_bool_value(domain, group, key).value_or(fallback);
  48. }
  49. void Client::write_string(StringView domain, StringView group, StringView key, StringView value)
  50. {
  51. write_string_value(domain, group, key, value);
  52. }
  53. void Client::write_i32(StringView domain, StringView group, StringView key, i32 value)
  54. {
  55. write_i32_value(domain, group, key, value);
  56. }
  57. void Client::write_u32(StringView domain, StringView group, StringView key, u32 value)
  58. {
  59. write_u32_value(domain, group, key, value);
  60. }
  61. void Client::write_bool(StringView domain, StringView group, StringView key, bool value)
  62. {
  63. write_bool_value(domain, group, key, value);
  64. }
  65. void Client::remove_key(StringView domain, StringView group, StringView key)
  66. {
  67. remove_key_entry(domain, group, key);
  68. }
  69. void Client::remove_group(StringView domain, StringView group)
  70. {
  71. remove_group_entry(domain, group);
  72. }
  73. void Client::add_group(StringView domain, StringView group)
  74. {
  75. add_group_entry(domain, group);
  76. }
  77. void Client::notify_changed_string_value(ByteString const& domain, ByteString const& group, ByteString const& key, ByteString const& value)
  78. {
  79. Listener::for_each([&](auto& listener) {
  80. listener.config_string_did_change(domain, group, key, value);
  81. });
  82. }
  83. void Client::notify_changed_i32_value(ByteString const& domain, ByteString const& group, ByteString const& key, i32 value)
  84. {
  85. Listener::for_each([&](auto& listener) {
  86. listener.config_i32_did_change(domain, group, key, value);
  87. });
  88. }
  89. void Client::notify_changed_u32_value(ByteString const& domain, ByteString const& group, ByteString const& key, u32 value)
  90. {
  91. Listener::for_each([&](auto& listener) {
  92. listener.config_u32_did_change(domain, group, key, value);
  93. });
  94. }
  95. void Client::notify_changed_bool_value(ByteString const& domain, ByteString const& group, ByteString const& key, bool value)
  96. {
  97. Listener::for_each([&](auto& listener) {
  98. listener.config_bool_did_change(domain, group, key, value);
  99. });
  100. }
  101. void Client::notify_removed_key(ByteString const& domain, ByteString const& group, ByteString const& key)
  102. {
  103. Listener::for_each([&](auto& listener) {
  104. listener.config_key_was_removed(domain, group, key);
  105. });
  106. }
  107. void Client::notify_removed_group(ByteString const& domain, ByteString const& group)
  108. {
  109. Listener::for_each([&](auto& listener) {
  110. listener.config_group_was_removed(domain, group);
  111. });
  112. }
  113. void Client::notify_added_group(ByteString const& domain, ByteString const& group)
  114. {
  115. Listener::for_each([&](auto& listener) {
  116. listener.config_group_was_added(domain, group);
  117. });
  118. }
  119. }