Client.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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::enable_permissive_mode()
  18. {
  19. async_enable_permissive_mode();
  20. }
  21. void Client::pledge_domains(Vector<ByteString> const& domains)
  22. {
  23. async_pledge_domains(domains);
  24. }
  25. void Client::monitor_domain(ByteString const& domain)
  26. {
  27. async_monitor_domain(domain);
  28. }
  29. Vector<ByteString> Client::list_keys(StringView domain, StringView group)
  30. {
  31. return list_config_keys(domain, group);
  32. }
  33. Vector<ByteString> Client::list_groups(StringView domain)
  34. {
  35. return list_config_groups(domain);
  36. }
  37. ByteString Client::read_string(StringView domain, StringView group, StringView key, StringView fallback)
  38. {
  39. return read_string_value(domain, group, key).value_or(fallback);
  40. }
  41. i32 Client::read_i32(StringView domain, StringView group, StringView key, i32 fallback)
  42. {
  43. return read_i32_value(domain, group, key).value_or(fallback);
  44. }
  45. u32 Client::read_u32(StringView domain, StringView group, StringView key, u32 fallback)
  46. {
  47. return read_u32_value(domain, group, key).value_or(fallback);
  48. }
  49. bool Client::read_bool(StringView domain, StringView group, StringView key, bool fallback)
  50. {
  51. return read_bool_value(domain, group, key).value_or(fallback);
  52. }
  53. void Client::write_string(StringView domain, StringView group, StringView key, StringView value)
  54. {
  55. write_string_value(domain, group, key, value);
  56. }
  57. void Client::write_i32(StringView domain, StringView group, StringView key, i32 value)
  58. {
  59. write_i32_value(domain, group, key, value);
  60. }
  61. void Client::write_u32(StringView domain, StringView group, StringView key, u32 value)
  62. {
  63. write_u32_value(domain, group, key, value);
  64. }
  65. void Client::write_bool(StringView domain, StringView group, StringView key, bool value)
  66. {
  67. write_bool_value(domain, group, key, value);
  68. }
  69. void Client::remove_key(StringView domain, StringView group, StringView key)
  70. {
  71. remove_key_entry(domain, group, key);
  72. }
  73. void Client::remove_group(StringView domain, StringView group)
  74. {
  75. remove_group_entry(domain, group);
  76. }
  77. void Client::add_group(StringView domain, StringView group)
  78. {
  79. add_group_entry(domain, group);
  80. }
  81. void Client::notify_changed_string_value(ByteString const& domain, ByteString const& group, ByteString const& key, ByteString const& value)
  82. {
  83. Listener::for_each([&](auto& listener) {
  84. listener.config_string_did_change(domain, group, key, value);
  85. });
  86. }
  87. void Client::notify_changed_i32_value(ByteString const& domain, ByteString const& group, ByteString const& key, i32 value)
  88. {
  89. Listener::for_each([&](auto& listener) {
  90. listener.config_i32_did_change(domain, group, key, value);
  91. });
  92. }
  93. void Client::notify_changed_u32_value(ByteString const& domain, ByteString const& group, ByteString const& key, u32 value)
  94. {
  95. Listener::for_each([&](auto& listener) {
  96. listener.config_u32_did_change(domain, group, key, value);
  97. });
  98. }
  99. void Client::notify_changed_bool_value(ByteString const& domain, ByteString const& group, ByteString const& key, bool value)
  100. {
  101. Listener::for_each([&](auto& listener) {
  102. listener.config_bool_did_change(domain, group, key, value);
  103. });
  104. }
  105. void Client::notify_removed_key(ByteString const& domain, ByteString const& group, ByteString const& key)
  106. {
  107. Listener::for_each([&](auto& listener) {
  108. listener.config_key_was_removed(domain, group, key);
  109. });
  110. }
  111. void Client::notify_removed_group(ByteString const& domain, ByteString const& group)
  112. {
  113. Listener::for_each([&](auto& listener) {
  114. listener.config_group_was_removed(domain, group);
  115. });
  116. }
  117. void Client::notify_added_group(ByteString const& domain, ByteString const& group)
  118. {
  119. Listener::for_each([&](auto& listener) {
  120. listener.config_group_was_added(domain, group);
  121. });
  122. }
  123. }