Delete studio command. Closes #6
[ladish.git] / daemon / control.c
blobc5e6898da7dc7112f34d35c4ea773254a93bdc3a
1 /* -*- Mode: C ; c-basic-offset: 2 -*- */
2 /*
3 * LADI Session Handler (ladish)
5 * Copyright (C) 2008, 2009 Nedko Arnaudov <nedko@arnaudov.name>
6 * Copyright (C) 2008 Juuso Alasuutari <juuso.alasuutari@gmail.com>
8 **************************************************************************
9 * This file contains code of the D-Bus control interface helpers
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 "common.h"
30 #include "../dbus/interface.h"
31 #include "../dbus/error.h"
32 #include "control.h"
33 #include "../dbus_constants.h"
35 #define INTERFACE_NAME IFACE_CONTROL
37 static void ladish_is_studio_loaded(method_call_t * call_ptr)
39 DBusMessageIter iter;
40 dbus_bool_t is_loaded;
42 is_loaded = studio_is_loaded();
44 call_ptr->reply = dbus_message_new_method_return(call_ptr->message);
45 if (call_ptr->reply == NULL)
47 goto fail;
50 dbus_message_iter_init_append(call_ptr->reply, &iter);
52 if (!dbus_message_iter_append_basic(&iter, DBUS_TYPE_BOOLEAN, &is_loaded))
54 goto fail_unref;
57 return;
59 fail_unref:
60 dbus_message_unref(call_ptr->reply);
61 call_ptr->reply = NULL;
63 fail:
64 lash_error("Ran out of memory trying to construct method return");
67 #define array_iter_ptr ((DBusMessageIter *)context)
69 static bool get_studio_list_callback(void * call_ptr, void * context, const char * studio, uint32_t modtime)
71 DBusMessageIter struct_iter;
72 DBusMessageIter dict_iter;
73 bool ret;
75 ret = false;
77 if (!dbus_message_iter_open_container(array_iter_ptr, DBUS_TYPE_STRUCT, NULL, &struct_iter))
78 goto exit;
80 if (!dbus_message_iter_append_basic(&struct_iter, DBUS_TYPE_STRING, &studio))
81 goto close_struct;
83 if (!dbus_message_iter_open_container(&struct_iter, DBUS_TYPE_ARRAY, "{sv}", &dict_iter))
84 goto close_struct;
86 /* if (!maybe_add_dict_entry_string(&dict_iter, "Description", xxx)) */
87 /* goto close_dict; */
89 if (!dbus_add_dict_entry_uint32(&dict_iter, "Modification Time", modtime))
90 goto close_dict;
92 ret = true;
94 close_dict:
95 if (!dbus_message_iter_close_container(&struct_iter, &dict_iter))
96 ret = false;
98 close_struct:
99 if (!dbus_message_iter_close_container(array_iter_ptr, &struct_iter))
100 ret = false;
102 exit:
103 return ret;
106 static void ladish_get_studio_list(method_call_t * call_ptr)
108 DBusMessageIter iter, array_iter;
110 call_ptr->reply = dbus_message_new_method_return(call_ptr->message);
111 if (call_ptr->reply == NULL)
113 goto fail;
116 dbus_message_iter_init_append(call_ptr->reply, &iter);
118 if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "(sa{sv})", &array_iter))
120 goto fail_unref;
123 if (!studios_iterate(call_ptr, &array_iter, get_studio_list_callback))
125 dbus_message_iter_close_container(&iter, &array_iter);
126 if (call_ptr->reply == NULL)
127 goto fail_unref;
129 /* studios_iterate or get_studio_list_callback() composed error reply */
130 return;
133 if (!dbus_message_iter_close_container(&iter, &array_iter))
135 goto fail_unref;
138 return;
140 fail_unref:
141 dbus_message_unref(call_ptr->reply);
142 call_ptr->reply = NULL;
144 fail:
145 lash_error("Ran out of memory trying to construct method return");
148 static void ladish_load_studio(method_call_t * call_ptr)
150 const char * name;
152 dbus_error_init(&g_dbus_error);
154 if (!dbus_message_get_args(call_ptr->message, &g_dbus_error, DBUS_TYPE_STRING, &name, DBUS_TYPE_INVALID))
156 lash_dbus_error(call_ptr, LASH_DBUS_ERROR_INVALID_ARGS, "Invalid arguments to method \"%s\": %s", call_ptr->method_name, g_dbus_error.message);
157 dbus_error_free(&g_dbus_error);
158 return;
161 if (studio_load(call_ptr, name))
163 method_return_new_void(call_ptr);
167 static void ladish_delete_studio(method_call_t * call_ptr)
169 const char * name;
171 dbus_error_init(&g_dbus_error);
173 if (!dbus_message_get_args(call_ptr->message, &g_dbus_error, DBUS_TYPE_STRING, &name, DBUS_TYPE_INVALID))
175 lash_dbus_error(call_ptr, LASH_DBUS_ERROR_INVALID_ARGS, "Invalid arguments to method \"%s\": %s", call_ptr->method_name, g_dbus_error.message);
176 dbus_error_free(&g_dbus_error);
177 return;
180 if (studio_delete(call_ptr, name))
182 method_return_new_void(call_ptr);
186 static void ladish_get_application_list(method_call_t * call_ptr)
188 DBusMessageIter iter;
189 DBusMessageIter array_iter;
190 #if 0
191 DBusMessageIter struct_iter;
192 DBusMessageIter dict_iter;
193 struct list_head * node_ptr;
194 struct lash_appdb_entry * entry_ptr;
195 #endif
197 lash_info("Getting applications list");
199 call_ptr->reply = dbus_message_new_method_return(call_ptr->message);
200 if (call_ptr->reply == NULL)
202 goto fail;
205 dbus_message_iter_init_append(call_ptr->reply, &iter);
207 if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "(sa{sv})", &array_iter))
209 goto fail_unref;
212 #if 0
213 list_for_each(node_ptr, &g_server->appdb)
215 entry_ptr = list_entry(node_ptr, struct lash_appdb_entry, siblings);
217 if (!dbus_message_iter_open_container(&array_iter, DBUS_TYPE_STRUCT, NULL, &struct_iter))
218 goto fail_unref;
220 if (!dbus_message_iter_append_basic(&struct_iter, DBUS_TYPE_STRING, (const void *) &entry_ptr->name))
222 dbus_message_iter_close_container(&iter, &array_iter);
223 goto fail_unref;
226 if (!dbus_message_iter_open_container(&struct_iter, DBUS_TYPE_ARRAY, "{sv}", &dict_iter))
227 goto fail_unref;
229 if (!maybe_add_dict_entry_string(&dict_iter, "GenericName", entry_ptr->generic_name))
230 goto fail_unref;
232 if (!maybe_add_dict_entry_string(&dict_iter, "Comment", entry_ptr->comment))
233 goto fail_unref;
235 if (!maybe_add_dict_entry_string(&dict_iter, "Icon", entry_ptr->icon))
236 goto fail_unref;
238 if (!dbus_message_iter_close_container(&struct_iter, &dict_iter))
239 goto fail_unref;
241 if (!dbus_message_iter_close_container(&array_iter, &struct_iter))
242 goto fail_unref;
244 #endif
246 if (!dbus_message_iter_close_container(&iter, &array_iter))
248 goto fail_unref;
251 return;
253 fail_unref:
254 dbus_message_unref(call_ptr->reply);
255 call_ptr->reply = NULL;
256 fail:
257 return;
260 static void ladish_exit(method_call_t * call_ptr)
262 lash_info("Exit command received through D-Bus");
263 g_quit = true;
266 void emit_studio_appeared()
268 signal_new_valist(g_dbus_connection, CONTROL_OBJECT_PATH, INTERFACE_NAME, "StudioAppeared", DBUS_TYPE_INVALID);
271 void emit_studio_disappeared()
273 signal_new_valist(g_dbus_connection, CONTROL_OBJECT_PATH, INTERFACE_NAME, "StudioDisappeared", DBUS_TYPE_INVALID);
276 METHOD_ARGS_BEGIN(IsStudioLoaded, "Check whether studio D-Bus object is present")
277 METHOD_ARG_DESCRIBE_OUT("present", "b", "Whether studio D-Bus object is present")
278 METHOD_ARGS_END
280 METHOD_ARGS_BEGIN(GetStudioList, "Get list of studios")
281 METHOD_ARG_DESCRIBE_OUT("studio_list", "a(sa{sv})", "List of studios, name and properties")
282 METHOD_ARGS_END
284 METHOD_ARGS_BEGIN(LoadStudio, "Load studio")
285 METHOD_ARG_DESCRIBE_IN("studio_name", "s", "Name of studio to load")
286 METHOD_ARG_DESCRIBE_IN("options", "a{sv}", "Load options")
287 METHOD_ARGS_END
289 METHOD_ARGS_BEGIN(DeleteStudio, "Delete studio")
290 METHOD_ARG_DESCRIBE_IN("studio_name", "s", "Name of studio to delete")
291 METHOD_ARGS_END
293 METHOD_ARGS_BEGIN(GetApplicationList, "Get list of applications that can be launched")
294 METHOD_ARG_DESCRIBE_OUT("applications", "a(sa{sv})", "List of applications, name and properties")
295 METHOD_ARGS_END
297 METHOD_ARGS_BEGIN(Exit, "Tell ladish D-Bus service to exit")
298 METHOD_ARGS_END
300 METHODS_BEGIN
301 METHOD_DESCRIBE(IsStudioLoaded, ladish_is_studio_loaded)
302 METHOD_DESCRIBE(GetStudioList, ladish_get_studio_list)
303 METHOD_DESCRIBE(LoadStudio, ladish_load_studio)
304 METHOD_DESCRIBE(DeleteStudio, ladish_delete_studio)
305 METHOD_DESCRIBE(GetApplicationList, ladish_get_application_list)
306 METHOD_DESCRIBE(Exit, ladish_exit)
307 METHODS_END
309 SIGNAL_ARGS_BEGIN(StudioAppeared, "Studio D-Bus object appeared")
310 SIGNAL_ARGS_END
312 SIGNAL_ARGS_BEGIN(StudioDisappeared, "Studio D-Bus object disappeared")
313 SIGNAL_ARGS_END
315 SIGNALS_BEGIN
316 SIGNAL_DESCRIBE(StudioAppeared)
317 SIGNAL_DESCRIBE(StudioDisappeared)
318 SIGNALS_END
321 * Interface description.
324 INTERFACE_BEGIN(g_lashd_interface_control, INTERFACE_NAME)
325 INTERFACE_DEFAULT_HANDLER
326 INTERFACE_EXPOSE_METHODS
327 INTERFACE_EXPOSE_SIGNALS
328 INTERFACE_END