Forráskód Böngészése

LibConfig+LibCore+ConfigServer: Support u32 configuration entries

Timothy Flynn 2 éve
szülő
commit
69beda23c3

+ 17 - 0
Userland/Libraries/LibConfig/Client.cpp

@@ -50,6 +50,11 @@ i32 Client::read_i32(StringView domain, StringView group, StringView key, i32 fa
     return read_i32_value(domain, group, key).value_or(fallback);
 }
 
+u32 Client::read_u32(StringView domain, StringView group, StringView key, u32 fallback)
+{
+    return read_u32_value(domain, group, key).value_or(fallback);
+}
+
 bool Client::read_bool(StringView domain, StringView group, StringView key, bool fallback)
 {
     return read_bool_value(domain, group, key).value_or(fallback);
@@ -65,6 +70,11 @@ void Client::write_i32(StringView domain, StringView group, StringView key, i32
     write_i32_value(domain, group, key, value);
 }
 
+void Client::write_u32(StringView domain, StringView group, StringView key, u32 value)
+{
+    write_u32_value(domain, group, key, value);
+}
+
 void Client::write_bool(StringView domain, StringView group, StringView key, bool value)
 {
     write_bool_value(domain, group, key, value);
@@ -99,6 +109,13 @@ void Client::notify_changed_i32_value(DeprecatedString const& domain, Deprecated
     });
 }
 
+void Client::notify_changed_u32_value(DeprecatedString const& domain, DeprecatedString const& group, DeprecatedString const& key, u32 value)
+{
+    Listener::for_each([&](auto& listener) {
+        listener.config_u32_did_change(domain, group, key, value);
+    });
+}
+
 void Client::notify_changed_bool_value(DeprecatedString const& domain, DeprecatedString const& group, DeprecatedString const& key, bool value)
 {
     Listener::for_each([&](auto& listener) {

+ 13 - 0
Userland/Libraries/LibConfig/Client.h

@@ -29,10 +29,12 @@ public:
 
     DeprecatedString read_string(StringView domain, StringView group, StringView key, StringView fallback);
     i32 read_i32(StringView domain, StringView group, StringView key, i32 fallback);
+    u32 read_u32(StringView domain, StringView group, StringView key, u32 fallback);
     bool read_bool(StringView domain, StringView group, StringView key, bool fallback);
 
     void write_string(StringView domain, StringView group, StringView key, StringView value);
     void write_i32(StringView domain, StringView group, StringView key, i32 value);
+    void write_u32(StringView domain, StringView group, StringView key, u32 value);
     void write_bool(StringView domain, StringView group, StringView key, bool value);
     void remove_key(StringView domain, StringView group, StringView key);
     void remove_group(StringView domain, StringView group);
@@ -48,6 +50,7 @@ private:
 
     void notify_changed_string_value(DeprecatedString const& domain, DeprecatedString const& group, DeprecatedString const& key, DeprecatedString const& value) override;
     void notify_changed_i32_value(DeprecatedString const& domain, DeprecatedString const& group, DeprecatedString const& key, i32 value) override;
+    void notify_changed_u32_value(DeprecatedString const& domain, DeprecatedString const& group, DeprecatedString const& key, u32 value) override;
     void notify_changed_bool_value(DeprecatedString const& domain, DeprecatedString const& group, DeprecatedString const& key, bool value) override;
     void notify_removed_key(DeprecatedString const& domain, DeprecatedString const& group, DeprecatedString const& key) override;
     void notify_removed_group(DeprecatedString const& domain, DeprecatedString const& group) override;
@@ -74,6 +77,11 @@ inline i32 read_i32(StringView domain, StringView group, StringView key, i32 fal
     return Client::the().read_i32(domain, group, key, fallback);
 }
 
+inline u32 read_u32(StringView domain, StringView group, StringView key, u32 fallback = 0)
+{
+    return Client::the().read_u32(domain, group, key, fallback);
+}
+
 inline bool read_bool(StringView domain, StringView group, StringView key, bool fallback = false)
 {
     return Client::the().read_bool(domain, group, key, fallback);
@@ -89,6 +97,11 @@ inline void write_i32(StringView domain, StringView group, StringView key, i32 v
     Client::the().write_i32(domain, group, key, value);
 }
 
+inline void write_u32(StringView domain, StringView group, StringView key, u32 value)
+{
+    Client::the().write_u32(domain, group, key, value);
+}
+
 inline void write_bool(StringView domain, StringView group, StringView key, bool value)
 {
     Client::the().write_bool(domain, group, key, value);

+ 4 - 0
Userland/Libraries/LibConfig/Listener.cpp

@@ -37,6 +37,10 @@ void Listener::config_i32_did_change(DeprecatedString const&, DeprecatedString c
 {
 }
 
+void Listener::config_u32_did_change(DeprecatedString const&, DeprecatedString const&, DeprecatedString const&, u32)
+{
+}
+
 void Listener::config_bool_did_change(DeprecatedString const&, DeprecatedString const&, DeprecatedString const&, bool)
 {
 }

+ 1 - 0
Userland/Libraries/LibConfig/Listener.h

@@ -18,6 +18,7 @@ public:
 
     virtual void config_string_did_change(DeprecatedString const& domain, DeprecatedString const& group, DeprecatedString const& key, DeprecatedString const& value);
     virtual void config_i32_did_change(DeprecatedString const& domain, DeprecatedString const& group, DeprecatedString const& key, i32 value);
+    virtual void config_u32_did_change(DeprecatedString const& domain, DeprecatedString const& group, DeprecatedString const& key, u32 value);
     virtual void config_bool_did_change(DeprecatedString const& domain, DeprecatedString const& group, DeprecatedString const& key, bool value);
     virtual void config_key_was_removed(DeprecatedString const& domain, DeprecatedString const& group, DeprecatedString const& key);
     virtual void config_group_was_removed(DeprecatedString const& domain, DeprecatedString const& group);

+ 0 - 14
Userland/Libraries/LibCore/ConfigFile.cpp

@@ -150,15 +150,6 @@ DeprecatedString ConfigFile::read_entry(DeprecatedString const& group, Deprecate
     return jt->value;
 }
 
-int ConfigFile::read_num_entry(DeprecatedString const& group, DeprecatedString const& key, int default_value) const
-{
-    if (!has_key(group, key)) {
-        return default_value;
-    }
-
-    return read_entry(group, key).to_int().value_or(default_value);
-}
-
 bool ConfigFile::read_bool_entry(DeprecatedString const& group, DeprecatedString const& key, bool default_value) const
 {
     auto value = read_entry(group, key, default_value ? "true" : "false");
@@ -171,11 +162,6 @@ void ConfigFile::write_entry(DeprecatedString const& group, DeprecatedString con
     m_dirty = true;
 }
 
-void ConfigFile::write_num_entry(DeprecatedString const& group, DeprecatedString const& key, int value)
-{
-    write_entry(group, key, DeprecatedString::number(value));
-}
-
 void ConfigFile::write_bool_entry(DeprecatedString const& group, DeprecatedString const& key, bool value)
 {
     write_entry(group, key, value ? "true" : "false");

+ 18 - 2
Userland/Libraries/LibCore/ConfigFile.h

@@ -42,13 +42,29 @@ public:
     size_t num_groups() const { return m_groups.size(); }
 
     DeprecatedString read_entry(DeprecatedString const& group, DeprecatedString const& key, DeprecatedString const& default_value = DeprecatedString()) const;
-    int read_num_entry(DeprecatedString const& group, DeprecatedString const& key, int default_value = 0) const;
     bool read_bool_entry(DeprecatedString const& group, DeprecatedString const& key, bool default_value = false) const;
 
+    template<Integral T = int>
+    T read_num_entry(DeprecatedString const& group, DeprecatedString const& key, T default_value = 0) const
+    {
+        if (!has_key(group, key))
+            return default_value;
+
+        if constexpr (IsSigned<T>)
+            return read_entry(group, key).to_int<T>().value_or(default_value);
+        else
+            return read_entry(group, key).to_uint<T>().value_or(default_value);
+    }
+
     void write_entry(DeprecatedString const& group, DeprecatedString const& key, DeprecatedString const& value);
-    void write_num_entry(DeprecatedString const& group, DeprecatedString const& key, int value);
     void write_bool_entry(DeprecatedString const& group, DeprecatedString const& key, bool value);
 
+    template<Integral T = int>
+    void write_num_entry(DeprecatedString const& group, DeprecatedString const& key, T value)
+    {
+        write_entry(group, key, DeprecatedString::number(value));
+    }
+
     void dump() const;
 
     bool is_dirty() const { return m_dirty; }

+ 1 - 0
Userland/Services/ConfigServer/ConfigClient.ipc

@@ -2,6 +2,7 @@ endpoint ConfigClient
 {
     notify_changed_string_value(DeprecatedString domain, DeprecatedString group, DeprecatedString key, DeprecatedString value) =|
     notify_changed_i32_value(DeprecatedString domain, DeprecatedString group, DeprecatedString key, i32 value) =|
+    notify_changed_u32_value(DeprecatedString domain, DeprecatedString group, DeprecatedString key, u32 value) =|
     notify_changed_bool_value(DeprecatedString domain, DeprecatedString group, DeprecatedString key, bool value) =|
     notify_removed_key(DeprecatedString domain, DeprecatedString group, DeprecatedString key) =|
     notify_removed_group(DeprecatedString domain, DeprecatedString group) =|

+ 2 - 0
Userland/Services/ConfigServer/ConfigServer.ipc

@@ -9,10 +9,12 @@ endpoint ConfigServer
 
     read_string_value(DeprecatedString domain, DeprecatedString group, DeprecatedString key) => (Optional<DeprecatedString> value)
     read_i32_value(DeprecatedString domain, DeprecatedString group, DeprecatedString key) => (Optional<i32> value)
+    read_u32_value(DeprecatedString domain, DeprecatedString group, DeprecatedString key) => (Optional<u32> value)
     read_bool_value(DeprecatedString domain, DeprecatedString group, DeprecatedString key) => (Optional<bool> value)
 
     write_string_value(DeprecatedString domain, DeprecatedString group, DeprecatedString key, DeprecatedString value) => ()
     write_i32_value(DeprecatedString domain, DeprecatedString group, DeprecatedString key, i32 value) => ()
+    write_u32_value(DeprecatedString domain, DeprecatedString group, DeprecatedString key, u32 value) => ()
     write_bool_value(DeprecatedString domain, DeprecatedString group, DeprecatedString key, bool value)  => ()
     remove_key_entry(DeprecatedString domain, DeprecatedString group, DeprecatedString key) => ()
     remove_group_entry(DeprecatedString domain, DeprecatedString group) => ()

+ 30 - 0
Userland/Services/ConfigServer/ConnectionFromClient.cpp

@@ -173,6 +173,17 @@ Messages::ConfigServer::ReadI32ValueResponse ConnectionFromClient::read_i32_valu
     return Optional<i32> { config.read_num_entry(group, key) };
 }
 
+Messages::ConfigServer::ReadU32ValueResponse ConnectionFromClient::read_u32_value(DeprecatedString const& domain, DeprecatedString const& group, DeprecatedString const& key)
+{
+    if (!validate_access(domain, group, key))
+        return nullptr;
+
+    auto& config = ensure_domain_config(domain);
+    if (!config.has_key(group, key))
+        return Optional<u32> {};
+    return Optional<u32> { config.read_num_entry<u32>(group, key) };
+}
+
 Messages::ConfigServer::ReadBoolValueResponse ConnectionFromClient::read_bool_value(DeprecatedString const& domain, DeprecatedString const& group, DeprecatedString const& key)
 {
     if (!validate_access(domain, group, key))
@@ -230,6 +241,25 @@ void ConnectionFromClient::write_i32_value(DeprecatedString const& domain, Depre
     });
 }
 
+void ConnectionFromClient::write_u32_value(DeprecatedString const& domain, DeprecatedString const& group, DeprecatedString const& key, u32 value)
+{
+    if (!validate_access(domain, group, key))
+        return;
+
+    auto& config = ensure_domain_config(domain);
+
+    if (config.has_key(group, key) && config.read_num_entry<u32>(group, key) == value)
+        return;
+
+    config.write_num_entry(group, key, value);
+    m_dirty_domains.set(domain);
+    start_or_restart_sync_timer();
+
+    for_each_monitoring_connection(domain, this, [&domain, &group, &key, &value](ConnectionFromClient& connection) {
+        connection.async_notify_changed_u32_value(domain, group, key, value);
+    });
+}
+
 void ConnectionFromClient::write_bool_value(DeprecatedString const& domain, DeprecatedString const& group, DeprecatedString const& key, bool value)
 {
     if (!validate_access(domain, group, key))

+ 2 - 0
Userland/Services/ConfigServer/ConnectionFromClient.h

@@ -31,9 +31,11 @@ private:
     virtual Messages::ConfigServer::ListConfigKeysResponse list_config_keys([[maybe_unused]] DeprecatedString const& domain, [[maybe_unused]] DeprecatedString const& group) override;
     virtual Messages::ConfigServer::ReadStringValueResponse read_string_value([[maybe_unused]] DeprecatedString const& domain, [[maybe_unused]] DeprecatedString const& group, [[maybe_unused]] DeprecatedString const& key) override;
     virtual Messages::ConfigServer::ReadI32ValueResponse read_i32_value([[maybe_unused]] DeprecatedString const& domain, [[maybe_unused]] DeprecatedString const& group, [[maybe_unused]] DeprecatedString const& key) override;
+    virtual Messages::ConfigServer::ReadU32ValueResponse read_u32_value([[maybe_unused]] DeprecatedString const& domain, [[maybe_unused]] DeprecatedString const& group, [[maybe_unused]] DeprecatedString const& key) override;
     virtual Messages::ConfigServer::ReadBoolValueResponse read_bool_value([[maybe_unused]] DeprecatedString const& domain, [[maybe_unused]] DeprecatedString const& group, [[maybe_unused]] DeprecatedString const& key) override;
     virtual void write_string_value([[maybe_unused]] DeprecatedString const& domain, [[maybe_unused]] DeprecatedString const& group, [[maybe_unused]] DeprecatedString const& key, [[maybe_unused]] DeprecatedString const& value) override;
     virtual void write_i32_value([[maybe_unused]] DeprecatedString const& domain, [[maybe_unused]] DeprecatedString const& group, [[maybe_unused]] DeprecatedString const& key, [[maybe_unused]] i32 value) override;
+    virtual void write_u32_value([[maybe_unused]] DeprecatedString const& domain, [[maybe_unused]] DeprecatedString const& group, [[maybe_unused]] DeprecatedString const& key, [[maybe_unused]] u32 value) override;
     virtual void write_bool_value([[maybe_unused]] DeprecatedString const& domain, [[maybe_unused]] DeprecatedString const& group, [[maybe_unused]] DeprecatedString const& key, [[maybe_unused]] bool value) override;
     virtual void remove_key_entry([[maybe_unused]] DeprecatedString const& domain, [[maybe_unused]] DeprecatedString const& group, [[maybe_unused]] DeprecatedString const& key) override;
     virtual void remove_group_entry([[maybe_unused]] DeprecatedString const& domain, [[maybe_unused]] DeprecatedString const& group) override;