-
Notifications
You must be signed in to change notification settings - Fork 3
/
channel.c
206 lines (182 loc) · 6.97 KB
/
channel.c
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
#include "channel.h"
#include "common.h"
#include "helper.h"
#include <vdr/channels.h>
#include <vdr/device.h>
namespace cDBusChannelsHelper
{
static const char *_xmlNodeInfo =
"<!DOCTYPE node PUBLIC \"-//freedesktop//DTD D-BUS Object Introspection 1.0//EN\"\n"
" \"http://www.freedesktop.org/standards/dbus/1.0/introspect.dtd\">\n"
"<node>\n"
" <interface name=\""DBUS_VDR_CHANNEL_INTERFACE"\">\n"
" <method name=\"Count\">\n"
" <arg name=\"count\" type=\"i\" direction=\"out\"/>\n"
" </method>\n"
" <method name=\"Current\">\n"
" <arg name=\"number\" type=\"i\" direction=\"out\"/>\n"
" <arg name=\"text\" type=\"s\" direction=\"out\"/>\n"
" </method>\n"
" <method name=\"GetFromTo\">\n"
" <arg name=\"from_index\" type=\"i\" direction=\"in\"/>\n"
" <arg name=\"to_index\" type=\"i\" direction=\"in\"/>\n"
" <arg name=\"channel\" type=\"a(is)\" direction=\"out\"/>\n"
" </method>\n"
" <method name=\"List\">\n"
" <arg name=\"option\" type=\"s\" direction=\"in\"/>\n"
" <arg name=\"channel\" type=\"a(is)\" direction=\"out\"/>\n"
" <arg name=\"replycode\" type=\"i\" direction=\"out\"/>\n"
" <arg name=\"replymessage\" type=\"s\" direction=\"out\"/>\n"
" </method>\n"
" </interface>\n"
"</node>\n";
static void AddChannel(GVariantBuilder *Array, const cChannel *Channel)
{
if (Channel == NULL)
return;
gint32 index = Channel->GroupSep() ? 0 : Channel->Number();
cString text = Channel->ToText();
int len = strlen(*text);
if ((len > 0) && ((*text)[len - 1] == '\n'))
text.Truncate(-1);
cDBusHelper::ToUtf8(text);
g_variant_builder_add(Array, "(is)", index, *text);
}
static void Count(cDBusObject *Object, GVariant *Parameters, GDBusMethodInvocation *Invocation)
{
const cChannels *channels = NULL;
#if VDRVERSNUM > 20300
LOCK_CHANNELS_READ;
channels = Channels;
#else
channels = &Channels;
#endif
g_dbus_method_invocation_return_value(Invocation, g_variant_new("(i)", channels->Count()));
}
static void Current(cDBusObject *Object, GVariant *Parameters, GDBusMethodInvocation *Invocation)
{
int number = cDevice::CurrentChannel();
cString text = "";
const cChannel* currentChannel = NULL;
#if VDRVERSNUM > 20300
LOCK_CHANNELS_READ;
const cChannels *channels = Channels;
currentChannel = channels->GetByNumber(number);
#else
currentChannel = Channels.GetByNumber(number);
#endif
if (currentChannel != NULL) {
text = currentChannel->ToText();
int len = strlen(*text);
if ((len > 0) && ((*text)[len - 1] == '\n'))
text.Truncate(-1);
cDBusHelper::ToUtf8(text);
}
GVariantBuilder *builder = g_variant_builder_new(G_VARIANT_TYPE("(is)"));
g_variant_builder_add(builder, "i", number);
g_variant_builder_add(builder, "s", *text);
g_dbus_method_invocation_return_value(Invocation, g_variant_builder_end(builder));
g_variant_builder_unref(builder);
}
static void GetFromTo(cDBusObject *Object, GVariant *Parameters, GDBusMethodInvocation *Invocation)
{
gint32 from_index = -1;
gint32 to_index = -1;
g_variant_get(Parameters, "(ii)", &from_index, &to_index);
if (to_index < from_index)
to_index = from_index;
GVariantBuilder *array = g_variant_builder_new(G_VARIANT_TYPE("a(is)"));
const cChannels *channels = NULL;
#if VDRVERSNUM > 20300
LOCK_CHANNELS_READ;
channels = Channels;
#else
channels = &Channels;
#endif
const cChannel *c = channels->Get(from_index);
while ((c != NULL) && (to_index >= from_index)) {
cDBusChannelsHelper::AddChannel(array, c);
c = channels->Next(c);
to_index--;
}
GVariantBuilder *builder = g_variant_builder_new(G_VARIANT_TYPE("(a(is))"));
g_variant_builder_add_value(builder, g_variant_builder_end(array));
g_dbus_method_invocation_return_value(Invocation, g_variant_builder_end(builder));
g_variant_builder_unref(array);
g_variant_builder_unref(builder);
}
static void List(cDBusObject *Object, GVariant *Parameters, GDBusMethodInvocation *Invocation)
{
const gchar *option = NULL;
g_variant_get(Parameters, "(&s)", &option);
bool withGroupSeps = (g_strcmp0(option, ":groups") == 0);
gint32 replyCode = 250;
cString replyMessage = "";
GVariantBuilder *array = g_variant_builder_new(G_VARIANT_TYPE("a(is)"));
#if VDRVERSNUM > 20300
LOCK_CHANNELS_READ;
const cChannels *channels = Channels;
#else
cChannels *channels = &Channels;
#endif
if ((option != NULL) && *option && !withGroupSeps) {
if (isnumber(option)) {
const cChannel *channel = channels->GetByNumber(strtol(option, NULL, 10));
if (channel)
cDBusChannelsHelper::AddChannel(array, channel);
else {
replyCode = 501;
replyMessage = cString::sprintf("Channel \"%s\" not defined", option);
}
}
else {
const cChannel *next = channels->GetByChannelID(tChannelID::FromString(option));
if (!next) {
for (const cChannel *channel = channels->First(); channel; channel = channels->Next(channel)) {
if (!channel->GroupSep()) {
if (strcasestr(channel->Name(), option)) {
if (next)
cDBusChannelsHelper::AddChannel(array, next);
next = channel;
}
}
}
}
if (next)
cDBusChannelsHelper::AddChannel(array, next);
else {
replyCode = 501;
replyMessage = cString::sprintf("Channel \"%s\" not defined", option);
}
}
}
else if (channels->MaxNumber() >= 1) {
for (const cChannel *channel = channels->First(); channel; channel = channels->Next(channel)) {
if (withGroupSeps || !channel->GroupSep())
cDBusChannelsHelper::AddChannel(array, channel);
}
}
else {
replyCode = 550;
replyMessage = "No channels defined";
}
GVariantBuilder *builder = g_variant_builder_new(G_VARIANT_TYPE("(a(is)is)"));
g_variant_builder_add_value(builder, g_variant_builder_end(array));
g_variant_builder_add(builder, "i", replyCode);
g_variant_builder_add(builder, "s", *replyMessage);
g_dbus_method_invocation_return_value(Invocation, g_variant_builder_end(builder));
g_variant_builder_unref(array);
g_variant_builder_unref(builder);
}
}
cDBusChannels::cDBusChannels(void)
:cDBusObject("/Channels", cDBusChannelsHelper::_xmlNodeInfo)
{
AddMethod("Count", cDBusChannelsHelper::Count);
AddMethod("Current", cDBusChannelsHelper::Current);
AddMethod("GetFromTo", cDBusChannelsHelper::GetFromTo);
AddMethod("List", cDBusChannelsHelper::List);
}
cDBusChannels::~cDBusChannels(void)
{
}