summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibConfig/Client.h
blob: 6f01a05aa28a81a1a2c839c118aee1651f9202c6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/*
 * Copyright (c) 2021, Andreas Kling <kling@serenityos.org>
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#pragma once

#include <ConfigServer/ConfigClientEndpoint.h>
#include <ConfigServer/ConfigServerEndpoint.h>
#include <LibCore/File.h>
#include <LibCore/Promise.h>
#include <LibCore/StandardPaths.h>
#include <LibIPC/ServerConnection.h>

namespace Config {

class Client final
    : public IPC::ServerConnection<ConfigClientEndpoint, ConfigServerEndpoint>
    , public ConfigClientEndpoint {
    C_OBJECT(Client);

public:
    void pledge_domains(Vector<String> const&);
    void monitor_domain(String const&);

    Vector<String> list_groups(StringView domain);
    Vector<String> list_keys(StringView domain, StringView group);

    String read_string(StringView domain, StringView group, StringView key, StringView fallback);
    i32 read_i32(StringView domain, StringView group, StringView key, i32 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_bool(StringView domain, StringView group, StringView key, bool value);
    void remove_key(StringView domain, StringView group, StringView key);

    static Client& the();

private:
    explicit Client()
        : IPC::ServerConnection<ConfigClientEndpoint, ConfigServerEndpoint>(*this, "/tmp/portal/config")
    {
    }

    void notify_changed_string_value(String const& domain, String const& group, String const& key, String const& value) override;
    void notify_changed_i32_value(String const& domain, String const& group, String const& key, i32 value) override;
    void notify_changed_bool_value(String const& domain, String const& group, String const& key, bool value) override;
    void notify_removed_key(String const& domain, String const& group, String const& key) override;
};

inline Vector<String> list_groups(StringView domain)
{
    return Client::the().list_groups(domain);
}

inline Vector<String> list_keys(StringView domain, StringView group)
{
    return Client::the().list_keys(domain, group);
}

inline String read_string(StringView domain, StringView group, StringView key, StringView fallback = {})
{
    return Client::the().read_string(domain, group, key, fallback);
}

inline i32 read_i32(StringView domain, StringView group, StringView key, i32 fallback = 0)
{
    return Client::the().read_i32(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);
}

inline void write_string(StringView domain, StringView group, StringView key, StringView value)
{
    Client::the().write_string(domain, group, key, value);
}

inline void write_i32(StringView domain, StringView group, StringView key, i32 value)
{
    Client::the().write_i32(domain, group, key, value);
}

inline void write_bool(StringView domain, StringView group, StringView key, bool value)
{
    Client::the().write_bool(domain, group, key, value);
}

inline void remove_key(StringView domain, StringView group, StringView key)
{
    Client::the().remove_key(domain, group, key);
}

inline void pledge_domains(Vector<String> const& domains)
{
    Client::the().pledge_domains(domains);
}

inline void pledge_domains(String const& domains)
{
    Client::the().pledge_domains({ domains });
}

inline void monitor_domain(String const& domain)
{
    Client::the().monitor_domain(domain);
}

}