2 * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the License);
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an AS IS BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
21 #include <AppCommon.h>
23 #define ADDRESS_BUF_MAX 128
25 /* FIXME: fix seat id, it should be changed */
26 #define TLM_SEAT_ID "seat0"
27 #define TLM_DBUS_SOCKET_PATH "/var/run/tlm/"
33 bool CBus::t_GetContent(GVariantIter *it, SContent *content)
38 const gchar *thumbnail;
41 r = g_variant_iter_next(it, "(ys)", &type, &thumbnail);
45 content->type = (EContentType)type;
46 content->thumbnail = thumbnail;
52 bool CBus::t_GetContentArray(SContent contents[], int arraymax, int *cnt)
60 val = g_variant_iter_next_value(Iter());
62 if (g_variant_is_of_type(val, G_VARIANT_TYPE_ARRAY)) {
64 g_variant_iter_init(&_it, val);
66 for (i = 0; i < arraymax; i++) {
67 if (t_GetContent(&_it, &contents[i]) == false)
79 return i ? true : false;
83 void CBus::t_ReleaseContentArray(SContent contents[], int arraymax)
87 for (i = 0; i < arraymax; i++)
88 g_free((gpointer)contents[i].thumbnail);
91 bool CBus::t_OnEvent(const char *method, GVariant *param)
95 if (strcmp(BUS_METHOD_ADD_ALBUM, method) == 0) {
96 ret = t_EventAddAlbum(param);
98 else if (strcmp(BUS_METHOD_ADD_LABEL, method) == 0) {
99 ret = t_EventAddLabel(param);
101 else if (strcmp(BUS_METHOD_ADD_PIN, method) == 0) {
102 ret = t_EventAddPin(param);
104 else if (strcmp(BUS_METHOD_DEL_ALL, method) == 0) {
105 ret = t_EventDelAllContent(param);
107 else if (strcmp(BUS_METHOD_ADD_CONTENTS, method) == 0) {
108 ret = t_EventAddContents(param);
114 bool CBus::t_EventAddLabel(GVariant *param)
120 memset(&data, 0, sizeof(data));
123 data.svcid = String();
124 data.title = String();
125 if (!data.svcid || !data.title) {
126 _ERR("svcid: 0x%x, title: 0x%x / get failed", data.svcid, data.title);
130 return OnAddLabel(&data);
133 bool CBus::t_EventAddAlbum(GVariant *param)
141 memset(&cAlbum, 0x00, sizeof(cAlbum));
142 memset(&cAction, 0x00, sizeof(cAction));
145 cAlbum.svcid = String();
146 cAlbum.title = String();
147 if (!cAlbum.svcid || !cAlbum.title) {
148 _ERR("svcid: 0x%x, title: 0x%x / id get failed", cAlbum.svcid, cAlbum.title);
152 ret = t_GetContentArray(cAlbum.contents,
153 BUS_DATA_ALBUM_CONTENT_MAX, &cAlbum.content_cnt);
155 _ERR("contents get failed");
159 cAction.action = (EActionOp)Byte();
160 cAction.actor = String();
161 cAction.args = String();
163 ret = OnAddAlbum(&cAlbum, &cAction);
164 t_ReleaseContentArray(cAlbum.contents, BUS_DATA_ALBUM_CONTENT_MAX);
169 bool CBus::t_EventAddContents(GVariant *param)
173 GVariantIter iter,_it;
175 const gchar *svcid, *title, *subtitle, *descr, *detail, *icon, *thumb, *actor, *args;
180 g_variant_iter_init(&iter, param);
181 val = g_variant_iter_next_value(&iter);
182 g_variant_iter_init(&_it, val);
186 while (g_variant_iter_next(&_it, "(sssssyssyss)", &svcid, &title, &subtitle,
187 &descr, &detail, &type, &thumb, &icon, &action, &actor, &args)) {
189 _ERR("failed to get service id");
193 cContent = (SContent *)calloc(1, sizeof(*cContent));
195 _ERR("calloc failed");
199 cContent->svcid = svcid;
200 cContent->title = title ? title : "";
201 cContent->subtitle = subtitle ? subtitle : "";
202 cContent->description = descr ? descr : "";
203 cContent->detail = detail ? detail : "";
204 cContent->type = (EContentType)type;
205 cContent->thumbnail = thumb ? thumb : "";
206 cContent->icon = icon ? icon : "";
207 cContent->action = (EActionOp)action;
208 cContent->actor = actor ? actor : "";
209 cContent->args = args ? args : "";
211 list = eina_list_append(list, cContent);
214 g_variant_unref(val);
215 g_variant_unref(param);
217 return OnAddContents(list);
220 bool CBus::t_EventAddPin(GVariant *param)
226 memset(&cPin, 0x00, sizeof(cPin));
229 cPin.svcid = String();
230 cPin.title = String();
231 if (!cPin.svcid || !cPin.title) {
232 _ERR("svcid: 0x%x, title: 0x%x / id get failed", cPin.svcid, cPin.title);
236 cPin.type = (EPinType)Byte();
237 cPin.icon = String();
238 cPin.pinid = String();
239 cAction.action = (EActionOp)Byte();
240 cAction.actor = String();
241 cAction.args = String();
243 return OnAddPin(&cPin, &cAction);
246 bool CBus::t_EventDelAllContent(GVariant *param)
256 _ERR("Service id get failed");
262 return OnDelAll(svcid, reload);
265 bool CBus::Create(const SCallback *cb)
267 ASSERT(!FlagCreate());
271 _ERR("calloc failed");
275 if (!CBaseBus::Create(cb, BUS_NAME, BUS_OBJECT_PATH, BUS_INTERFACE, BUS_TYPE)) {
286 void CBus::Destroy(void)
288 ASSERT(FlagCreate());
295 bool CBus::OnAddLabel(SLabel *opdata)
299 if (m->cb.cbAddLabel)
300 ret = m->cb.cbAddLabel(opdata, m->cb.cookie);
305 bool CBus::OnAddAlbum(SAlbum *opdata, SAction *act)
309 if (m->cb.cbAddAlbum)
310 ret = m->cb.cbAddAlbum(opdata, act, m->cb.cookie);
315 bool CBus::OnAddContents(Eina_List *list)
319 if (m->cb.cbAddContents)
320 ret = m->cb.cbAddContents(list, m->cb.cookie);
325 bool CBus::OnAddPin(SPin *opdata, SAction *act)
330 ret = m->cb.cbAddPin(opdata, act, m->cb.cookie);
335 bool CBus::OnDelAll(const char *svcid, bool reload)
340 ret = m->cb.cbDelAll(svcid, reload, m->cb.cookie);
346 * bus send changed user, client program start
348 static GDBusConnection *_get_bus_connection(GError **error)
350 gchar address[ADDRESS_BUF_MAX];
352 g_snprintf(address, ADDRESS_BUF_MAX - 1, "unix:path=%s%s-%u",
353 TLM_DBUS_SOCKET_PATH, TLM_SEAT_ID, getuid());
355 return g_dbus_connection_new_for_address_sync(address,
356 G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT, NULL,
360 static GVariant *_build_env_param(const char *username, const char *password)
362 GVariantBuilder *builder;
363 GVariant *param, *env;
365 if (!username || !password) {
366 _ERR("Invalid arguement");
370 builder = g_variant_builder_new(G_VARIANT_TYPE_ARRAY);
372 _ERR("builder new failed");
375 g_variant_builder_add(builder, "{ss}", "", "");
376 env = g_variant_builder_end(builder);
377 g_variant_builder_unref(builder);
379 param = g_variant_new("(sss@a{ss})",
380 TLM_SEAT_ID, username, password, env);
385 int bus_change_user(const char *username, const char *password)
387 GDBusConnection *conn;
390 GDBusMessage *msg, *res;
393 if (!username || !password) {
394 _ERR("Invalid argument");
399 conn = _get_bus_connection(&error);
401 _ERR("failed to get connection, %s",
402 error ? error->message : "");
407 param = _build_env_param(username, password);
409 _ERR("failed to build env param");
410 g_object_unref(conn);
414 msg = g_dbus_message_new_method_call("org.O1.Tlm.Login",
415 "/org/O1/Tlm/Login", "org.O1.Tlm.Login",
417 g_dbus_message_set_body(msg, param);
420 res = g_dbus_connection_send_message_with_reply_sync(conn, msg,
421 G_DBUS_SEND_MESSAGE_FLAGS_NONE,
422 1000, &serial, NULL, &error);
424 _ERR("failed to switch user, %s", error ? error->message : "");
426 g_object_unref(conn);
431 _DBG("User changed successfully: %u", serial);
435 g_object_unref(conn);