~aleteoryx/muditaos

ref: 4ad0f29ab49cde21debe3f98a8b0ba99e3aa585c muditaos/module-services/service-db/agents/settings/SettingsProxy.cpp -rw-r--r-- 2.5 KiB
4ad0f29a — Lefucjusz [BH-1898] Fix of GUI freeze after intensive volume change 1 year, 11 months ago
                                                                                
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
// Copyright (c) 2017-2024, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

#include <service-db/EntryPath.hpp>
#include <service-db/SettingsMessages.hpp>
#include <service-db/Settings.hpp>
#include <service-db/SettingsCache.hpp>
#include <service-db/DBServiceName.hpp>
#include <Service/ServiceProxy.hpp>
#include <Service/Service.hpp>
#include <utility>

namespace settings
{
    SettingsProxy::SettingsProxy(const service::ServiceProxy &interface) : service::ServiceProxy(interface)
    {}

    SettingsProxy::~SettingsProxy()
    {
        deinit();
    }

    std::string SettingsProxy::ownerName()
    {
        return getService()->GetName();
    }

    void SettingsProxy::init(std::function<void(EntryPath, std::string)> onChangeHandler)
    {
        this->onChangeHandler = std::move(onChangeHandler);

        getService()->bus.channels.push_back(sys::BusChannel::ServiceDBNotifications);
        getService()->connect(typeid(settings::Messages::VariableChanged), [this](sys::Message *req) {
            if (auto msg = dynamic_cast<settings::Messages::VariableChanged *>(req)) {
                onChange(msg->getPath(), msg->getValue().value_or(""));
            }
            return std::make_shared<sys::ResponseMessage>();
        });
    }

    void SettingsProxy::deinit()
    {
        if (isValid()) {
            getService()->disconnect(typeid(settings::Messages::VariableChanged));
        }
    }

    [[nodiscard]] bool SettingsProxy::isValid() const noexcept
    {
        return service::ServiceProxy::isValid();
    }

    void SettingsProxy::onChange(EntryPath path, std::string value)
    {
        if (onChangeHandler) {
            onChangeHandler(std::move(path), std::move(value));
        }
    }

    void SettingsProxy::registerValueChange(EntryPath path)
    {
        auto msg = std::make_shared<Messages::RegisterOnVariableChange>(std::move(path));
        getService()->bus.sendUnicast(std::move(msg), service::name::db);
    }

    void SettingsProxy::unregisterValueChange(EntryPath path)
    {
        auto msg = std::make_shared<Messages::UnregisterOnVariableChange>(std::move(path));
        getService()->bus.sendUnicast(std::move(msg), service::name::db);
    }

    void SettingsProxy::setValue(const EntryPath &path, const std::string &value)
    {
        auto msg = std::make_shared<Messages::SetVariable>(path, value);
        getService()->bus.sendUnicast(std::move(msg), service::name::db);
    }
} // namespace settings