daemon: add some logs
[ladish.git] / gui / control_proxy.c
blobcea6a4e7ca58731d327fa0e57c0714709b84d88f
1 /* -*- Mode: C ; c-basic-offset: 2 -*- */
2 /*
3 * LADI Session Handler (ladish)
5 * Copyright (C) 2009 Nedko Arnaudov <nedko@arnaudov.name>
7 **************************************************************************
8 * This file contains implementation of code that interfaces
9 * the ladishd Control object through D-Bus
10 **************************************************************************
12 * LADI Session Handler is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
17 * LADI Session Handler is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with LADI Session Handler. If not, see <http://www.gnu.org/licenses/>
24 * or write to the Free Software Foundation, Inc.,
25 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
28 #include "control_proxy.h"
29 #include "../dbus/helpers.h"
30 #include "../dbus_constants.h"
32 static const char * g_signals[] =
34 "StudioAppeared",
35 "StudioDisappeared",
36 NULL
39 static DBusHandlerResult message_hook(DBusConnection * connection, DBusMessage * message, void * data)
41 const char * object_path;
43 object_path = dbus_message_get_path(message);
44 if (object_path == NULL || strcmp(object_path, CONTROL_OBJECT_PATH) != 0)
46 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
49 if (dbus_message_is_signal(message, IFACE_CONTROL, "StudioAppeared"))
51 log_info("StudioAppeared");
52 control_proxy_on_studio_appeared();
53 return DBUS_HANDLER_RESULT_HANDLED;
56 if (dbus_message_is_signal(message, IFACE_CONTROL, "StudioDisappeared"))
58 log_info("StudioDisappeared");
59 control_proxy_on_studio_disappeared();
60 return DBUS_HANDLER_RESULT_HANDLED;
63 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
66 static bool control_proxy_is_studio_loaded(bool * present_ptr)
68 dbus_bool_t present;
70 if (!dbus_call(SERVICE_NAME, CONTROL_OBJECT_PATH, IFACE_CONTROL, "IsStudioLoaded", "", "b", &present))
72 return false;
75 *present_ptr = present;
77 return true;
80 bool control_proxy_init(void)
82 bool studio_present;
84 if (!control_proxy_is_studio_loaded(&studio_present))
86 return false;
89 if (studio_present)
91 log_info("Initial studio appear");
92 control_proxy_on_studio_appeared();
95 if (!dbus_register_object_signal_handler(g_dbus_connection, SERVICE_NAME, CONTROL_OBJECT_PATH, IFACE_CONTROL, g_signals, message_hook, NULL))
97 if (studio_present)
99 control_proxy_on_studio_disappeared();
102 return false;
105 return true;
108 void control_proxy_uninit(void)
110 dbus_unregister_object_signal_handler(g_dbus_connection, SERVICE_NAME, CONTROL_OBJECT_PATH, IFACE_CONTROL, g_signals, message_hook, NULL);
113 bool control_proxy_get_studio_list(void (* callback)(void * context, const char * studio_name), void * context)
115 DBusMessage * reply_ptr;
116 const char * reply_signature;
117 DBusMessageIter top_iter;
118 DBusMessageIter struct_iter;
119 DBusMessageIter array_iter;
120 const char * studio_name;
122 if (!dbus_call(SERVICE_NAME, CONTROL_OBJECT_PATH, IFACE_CONTROL, "GetStudioList", "", NULL, &reply_ptr))
124 log_error("GetStudioList() failed.");
125 return false;
128 reply_signature = dbus_message_get_signature(reply_ptr);
129 if (strcmp(reply_signature, "a(sa{sv})") != 0)
131 log_error("GetStudioList() reply signature mismatch. '%s'", reply_signature);
132 dbus_message_unref(reply_ptr);
133 return false;
136 dbus_message_iter_init(reply_ptr, &top_iter);
137 for (dbus_message_iter_recurse(&top_iter, &array_iter);
138 dbus_message_iter_get_arg_type(&array_iter) != DBUS_TYPE_INVALID;
139 dbus_message_iter_next(&array_iter))
141 dbus_message_iter_recurse(&array_iter, &struct_iter);
142 dbus_message_iter_get_basic(&struct_iter, &studio_name);
143 callback(context, studio_name);
144 dbus_message_iter_next(&struct_iter);
145 dbus_message_iter_next(&struct_iter);
148 dbus_message_unref(reply_ptr);
149 return true;
152 bool control_proxy_new_studio(const char * studio_name)
154 if (studio_name == NULL)
156 studio_name = "";
159 if (!dbus_call(SERVICE_NAME, CONTROL_OBJECT_PATH, IFACE_CONTROL, "NewStudio", "s", &studio_name, ""))
161 log_error("NewStudio() failed.");
162 return false;
165 return true;
168 bool control_proxy_load_studio(const char * studio_name)
170 if (!dbus_call(SERVICE_NAME, CONTROL_OBJECT_PATH, IFACE_CONTROL, "LoadStudio", "s", &studio_name, ""))
172 log_error("LoadStudio() failed.");
173 return false;
176 return true;
179 bool control_proxy_delete_studio(const char * studio_name)
181 if (!dbus_call(SERVICE_NAME, CONTROL_OBJECT_PATH, IFACE_CONTROL, "DeleteStudio", "s", &studio_name, ""))
183 log_error("DeleteStudio() failed.");
184 return false;
187 return true;
190 bool control_proxy_exit(void)
192 if (!dbus_call(SERVICE_NAME, CONTROL_OBJECT_PATH, IFACE_CONTROL, "Exit", "", ""))
194 log_error("Exit() failed.");
195 return false;
198 return true;