File: properties.cpp

package info (click to toggle)
snapcast 0.31.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 4,012 kB
  • sloc: cpp: 37,729; python: 2,543; sh: 455; makefile: 16
file content (189 lines) | stat: -rw-r--r-- 5,854 bytes parent folder | download | duplicates (3)
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
/***
    This file is part of snapcast
    Copyright (C) 2014-2024  Johannes Pohl

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
***/

// prototype/interface header file
#include "properties.hpp"

// local headers
#include "common/aixlog.hpp"

// standard headers
#include <set>


static constexpr auto LOG_TAG = "Properties";

namespace
{
template <typename T>
void readTag(const json& j, const std::string& tag, std::optional<T>& dest)
{
    try
    {
        if (!j.contains(tag))
            dest = std::nullopt;
        else
            dest = j[tag].get<T>();
    }
    catch (const std::exception& e)
    {
        LOG(ERROR, LOG_TAG) << "failed to read tag: '" << tag << "': " << e.what() << '\n';
    }
}

template <typename T>
void readTag(const json& j, const std::string& tag, T& dest, const T& def)
{
    std::optional<T> val;
    readTag(j, tag, val);
    if (val.has_value())
        dest = val.value();
    else
        dest = def;
}

template <typename T>
void addTag(json& j, const std::string& tag, const T& source)
{
    try
    {
        j[tag] = source;
    }
    catch (const std::exception& e)
    {
        LOG(ERROR, LOG_TAG) << "failed to add tag: '" << tag << "': " << e.what() << '\n';
    }
}

template <typename T>
void addTag(json& j, const std::string& tag, const std::optional<T>& source)
{
    if (!source.has_value())
    {
        if (j.contains(tag))
            j.erase(tag);
    }
    else
        addTag(j, tag, source.value());
}
} // namespace


Properties::Properties(const json& j)
{
    fromJson(j);
}


json Properties::toJson() const
{
    json j;
    if (playback_status.has_value())
        addTag(j, "playbackStatus", std::optional<std::string>(to_string(playback_status.value())));
    if (loop_status.has_value())
        addTag(j, "loopStatus", std::optional<std::string>(to_string(loop_status.value())));
    addTag(j, "rate", rate);
    addTag(j, "shuffle", shuffle);
    addTag(j, "volume", volume);
    addTag(j, "mute", mute);
    addTag(j, "position", position);
    addTag(j, "minimumRate", minimum_rate);
    addTag(j, "maximumRate", maximum_rate);
    addTag(j, "canGoNext", can_go_next);
    addTag(j, "canGoPrevious", can_go_previous);
    addTag(j, "canPlay", can_play);
    addTag(j, "canPause", can_pause);
    addTag(j, "canSeek", can_seek);
    addTag(j, "canControl", can_control);
    if (metadata.has_value())
        addTag(j, "metadata", metadata->toJson());
    return j;
}

void Properties::fromJson(const json& j)
{
    static std::set<std::string> rw_props = {"loopStatus", "shuffle", "volume", "mute", "rate"};
    static std::set<std::string> ro_props = {"playbackStatus", "loopStatus",    "shuffle", "volume",   "mute",    "position",   "minimumRate", "maximumRate",
                                             "canGoNext",      "canGoPrevious", "canPlay", "canPause", "canSeek", "canControl", "metadata"};
    for (const auto& element : j.items())
    {
        bool is_rw = (rw_props.find(element.key()) != rw_props.end());
        bool is_ro = (ro_props.find(element.key()) != ro_props.end());
        if (!is_rw && !is_ro)
            LOG(WARNING, LOG_TAG) << "Property not supoorted: " << element.key() << "\n";
    }

    std::optional<std::string> opt;

    readTag(j, "playbackStatus", opt);
    if (opt.has_value())
        playback_status = playback_status_from_string(opt.value());
    else
        playback_status = std::nullopt;

    readTag(j, "loopStatus", opt);
    if (opt.has_value())
        loop_status = loop_status_from_string(opt.value());
    else
        loop_status = std::nullopt;
    readTag(j, "rate", rate);
    readTag(j, "shuffle", shuffle);
    readTag(j, "volume", volume);
    readTag(j, "mute", mute);
    readTag(j, "position", position);
    readTag(j, "minimumRate", minimum_rate);
    readTag(j, "maximumRate", maximum_rate);
    readTag(j, "canGoNext", can_go_next, false);
    readTag(j, "canGoPrevious", can_go_previous, false);
    readTag(j, "canPlay", can_play, false);
    readTag(j, "canPause", can_pause, false);
    readTag(j, "canSeek", can_seek, false);
    readTag(j, "canControl", can_control, false);

    if (j.contains("metadata"))
    {
        Metadata m;
        m.fromJson(j["metadata"]);
        metadata = m;
    }
    else
        metadata = std::nullopt;
}

bool Properties::operator==(const Properties& other) const
{
    // expensive, but not called ofetn and less typing
    return (toJson() == other.toJson());

    // clang-format off
        // return (playback_status == other.playback_status &&
        //         loop_status == other.loop_status &&
        //         rate == other.rate &&
        //         shuffle == other.shuffle &&
        //         volume == other.volume &&
        //         position == other.position &&
        //         minimum_rate == other.minimum_rate &&
        //         maximum_rate == other.maximum_rate &&
        //         can_go_next == other.can_go_next &&
        //         can_go_previous == other.can_go_previous &&
        //         can_play == other.can_play &&
        //         can_pause == other.can_pause &&
        //         can_seek == other.can_seek &&
        //         can_control == other.can_control);
    // clang-format on
}