Add missing header text
[ladish.git] / proxies / room_proxy.c
blob91b80afa1124f4d0f3f86307e24e771917bf3b97
1 /* -*- Mode: C ; c-basic-offset: 2 -*- */
2 /*
3 * LADI Session Handler (ladish)
5 * Copyright (C) 2010,2011 Nedko Arnaudov <nedko@arnaudov.name>
7 **************************************************************************
8 * This file contains implementation of code that interfaces
9 * ladishd room objects 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 "room_proxy.h"
30 struct ladish_room_proxy
32 char * service;
33 char * object;
35 void * project_properties_changed_context;
36 void (* project_properties_changed)(
37 void * project_properties_changed_context,
38 const char * project_dir,
39 const char * project_name,
40 const char * project_description,
41 const char * project_notes);
43 uint64_t project_properties_version;
44 char * project_name;
45 char * project_dir;
46 char * project_description;
47 char * project_notes;
50 static bool update_project_properties(struct ladish_room_proxy * proxy_ptr, DBusMessage * message_ptr, const char * context)
52 const char * signature;
53 DBusMessageIter iter;
54 dbus_uint64_t version;
55 const char * name;
56 const char * dir;
57 const char * description;
58 const char * notes;
59 char * name_buffer;
60 char * dir_buffer;
61 char * description_buffer;
62 char * notes_buffer;
64 signature = dbus_message_get_signature(message_ptr);
65 if (strcmp(signature, "ta{sv}") != 0)
67 log_error("%s signature mismatch. '%s'", context, signature);
68 goto fail;
71 dbus_message_iter_init(message_ptr, &iter);
73 dbus_message_iter_get_basic(&iter, &version);
74 dbus_message_iter_next(&iter);
76 if (version == 0)
78 log_error("%s contains project properties version 0", context);
79 goto fail;
82 if (proxy_ptr->project_properties_version >= version)
84 goto fail;
87 if (!cdbus_iter_get_dict_entry_string(&iter, "name", &name))
89 name = "";
92 if (!cdbus_iter_get_dict_entry_string(&iter, "dir", &dir))
94 dir = "";
97 if (!cdbus_iter_get_dict_entry_string(&iter, "description", &description))
99 description = "";
102 if (!cdbus_iter_get_dict_entry_string(&iter, "notes", &notes))
104 notes = "";
107 name_buffer = strdup(name);
108 if (name_buffer == NULL)
110 log_error("strdup() failed for project name");
111 goto fail;
114 dir_buffer = strdup(dir);
115 if (dir_buffer == NULL)
117 log_error("strdup() failed for project dir");
118 goto fail_free_name_buffer;
121 description_buffer = strdup(description);
122 if (description_buffer == NULL)
124 log_error("strdup() failed for project description");
125 goto fail_free_dir_buffer;
128 notes_buffer = strdup(notes);
129 if (notes_buffer == NULL)
131 log_error("strdup() failed for project notes");
132 goto fail_free_description_buffer;
135 proxy_ptr->project_properties_version = version;
137 if (proxy_ptr->project_name != NULL)
139 free(proxy_ptr->project_name);
141 proxy_ptr->project_name = name_buffer;
143 if (proxy_ptr->project_dir != NULL)
145 free(proxy_ptr->project_dir);
147 proxy_ptr->project_dir = dir_buffer;
149 if (proxy_ptr->project_description != NULL)
151 free(proxy_ptr->project_description);
153 proxy_ptr->project_description = description_buffer;
155 if (proxy_ptr->project_notes != NULL)
157 free(proxy_ptr->project_notes);
159 proxy_ptr->project_notes = notes_buffer;
161 /* log_info("Room '%s' project properties changed:", proxy_ptr->object); /\* TODO: cache project name *\/ */
162 /* log_info(" Properties version: %"PRIu64, proxy_ptr->project_properties_version); */
163 /* log_info(" Project name: '%s'", proxy_ptr->project_name); */
164 /* log_info(" Project dir: '%s'", proxy_ptr->project_dir); */
165 /* log_info(" Project description: '%s'", proxy_ptr->project_description); */
166 /* log_info(" Project notes: '%s'", proxy_ptr->project_notes); */
168 proxy_ptr->project_properties_changed(
169 proxy_ptr->project_properties_changed_context,
170 proxy_ptr->project_dir,
171 proxy_ptr->project_name,
172 proxy_ptr->project_description,
173 proxy_ptr->project_notes);
175 return true;
177 fail_free_description_buffer:
178 free(description_buffer);
179 fail_free_dir_buffer:
180 free(dir_buffer);
181 fail_free_name_buffer:
182 free(name_buffer);
183 fail:
184 return false;
187 bool ladish_room_proxy_get_project_properties_internal(struct ladish_room_proxy * proxy_ptr)
189 DBusMessage * reply_ptr;
191 if (!cdbus_call(0, proxy_ptr->service, proxy_ptr->object, IFACE_ROOM, "GetProjectProperties", "", NULL, &reply_ptr))
193 log_error("GetProjectProperties() failed.");
194 return false;
197 if (!update_project_properties(proxy_ptr, reply_ptr, "GetProjectProperties() reply"))
199 dbus_message_unref(reply_ptr);
200 return false;
203 dbus_message_unref(reply_ptr);
205 return true;
208 #define proxy_ptr ((struct ladish_room_proxy *)context)
210 static void on_project_properties_changed(void * context, DBusMessage * message_ptr)
212 log_info("ProjectPropertiesChanged signal received.");
213 update_project_properties(proxy_ptr, message_ptr, "ProjectPropertiesChanged() signal");
216 #undef proxy_ptr
218 /* this must be static because it is referenced by the
219 * dbus helper layer when hooks are active */
220 static struct cdbus_signal_hook g_signal_hooks[] =
222 {"ProjectPropertiesChanged", on_project_properties_changed},
223 {NULL, NULL}
226 bool
227 ladish_room_proxy_create(
228 const char * service,
229 const char * object,
230 void * project_properties_changed_context,
231 void (* project_properties_changed)(
232 void * project_properties_changed_context,
233 const char * project_dir,
234 const char * project_name,
235 const char * project_description,
236 const char * project_notes),
237 ladish_room_proxy_handle * handle_ptr)
239 struct ladish_room_proxy * proxy_ptr;
241 proxy_ptr = malloc(sizeof(struct ladish_room_proxy));
242 if (proxy_ptr == NULL)
244 log_error("malloc() failed to allocate room proxy struct");
245 goto fail;
248 proxy_ptr->service = strdup(service);
249 if (proxy_ptr->service == NULL)
251 log_error("strdup() failed too duplicate service name '%s'", service);
252 goto free_proxy;
255 proxy_ptr->object = strdup(object);
256 if (proxy_ptr->object == NULL)
258 log_error("strdup() failed too duplicate object name '%s'", object);
259 goto free_service;
262 proxy_ptr->project_properties_version = 0;
263 proxy_ptr->project_name = NULL;
264 proxy_ptr->project_dir = NULL;
265 proxy_ptr->project_description = NULL;
266 proxy_ptr->project_notes = NULL;
268 proxy_ptr->project_properties_changed_context = project_properties_changed_context;
269 proxy_ptr->project_properties_changed = project_properties_changed;
271 if (!cdbus_register_object_signal_hooks(
272 cdbus_g_dbus_connection,
273 proxy_ptr->service,
274 proxy_ptr->object,
275 IFACE_ROOM,
276 proxy_ptr,
277 g_signal_hooks))
279 log_error("dbus_register_object_signal_hooks() failed for room");
280 goto free_object;
283 if (!ladish_room_proxy_get_project_properties_internal(proxy_ptr))
285 goto unregister_signal_hooks;
288 *handle_ptr = (ladish_room_proxy_handle)proxy_ptr;
289 return true;
291 unregister_signal_hooks:
292 cdbus_unregister_object_signal_hooks(cdbus_g_dbus_connection, proxy_ptr->service, proxy_ptr->object, IFACE_ROOM);
293 free_object:
294 free(proxy_ptr->object);
295 free_service:
296 free(proxy_ptr->service);
297 free_proxy:
298 free(proxy_ptr);
299 fail:
300 return false;
303 #define proxy_ptr ((struct ladish_room_proxy *)proxy)
305 void ladish_room_proxy_destroy(ladish_room_proxy_handle proxy)
307 cdbus_unregister_object_signal_hooks(cdbus_g_dbus_connection, proxy_ptr->service, proxy_ptr->object, IFACE_ROOM);
309 if (proxy_ptr->project_name != NULL)
311 free(proxy_ptr->project_name);
314 if (proxy_ptr->project_dir != NULL)
316 free(proxy_ptr->project_dir);
319 if (proxy_ptr->project_description != NULL)
321 free(proxy_ptr->project_description);
324 if (proxy_ptr->project_notes != NULL)
326 free(proxy_ptr->project_notes);
329 free(proxy_ptr->object);
330 free(proxy_ptr->service);
331 free(proxy_ptr);
334 char * ladish_room_proxy_get_name(ladish_room_proxy_handle proxy)
336 DBusMessage * reply_ptr;
337 const char * name;
338 char * name_buffer;
340 if (!cdbus_call(0, proxy_ptr->service, proxy_ptr->object, IFACE_ROOM, "GetName", "", NULL, &reply_ptr))
342 log_error("GetName() failed.");
343 return NULL;
346 if (!dbus_message_get_args(
347 reply_ptr,
348 &cdbus_g_dbus_error,
349 DBUS_TYPE_STRING, &name,
350 DBUS_TYPE_INVALID))
352 dbus_message_unref(reply_ptr);
353 dbus_error_free(&cdbus_g_dbus_error);
354 log_error("decoding reply of GetName failed.");
355 return NULL;
358 name_buffer = strdup(name);
359 dbus_message_unref(reply_ptr);
360 if (name_buffer == NULL)
362 log_error("strdup() for app name failed.");
364 return name_buffer;
367 bool ladish_room_proxy_load_project(ladish_room_proxy_handle proxy, const char * project_dir)
369 if (!cdbus_call(0, proxy_ptr->service, proxy_ptr->object, IFACE_ROOM, "LoadProject", "s", &project_dir, ""))
371 log_error("LoadProject() failed.");
372 return false;
375 return true;
378 bool ladish_room_proxy_save_project(ladish_room_proxy_handle proxy, const char * project_dir, const char * project_name)
380 if (!cdbus_call(0, proxy_ptr->service, proxy_ptr->object, IFACE_ROOM, "SaveProject", "ss", &project_dir, &project_name, ""))
382 log_error("SaveProject() failed.");
383 return false;
386 return true;
389 bool ladish_room_proxy_unload_project(ladish_room_proxy_handle proxy)
391 if (!cdbus_call(0, proxy_ptr->service, proxy_ptr->object, IFACE_ROOM, "UnloadProject", "", ""))
393 log_error("UnloadProject() failed.");
394 return false;
397 return true;
400 void
401 ladish_room_proxy_get_project_properties(
402 ladish_room_proxy_handle proxy,
403 const char ** project_dir,
404 const char ** project_name,
405 const char ** project_description,
406 const char ** project_notes)
408 ASSERT(proxy_ptr->project_properties_version > 0);
410 if (project_dir != NULL)
412 *project_dir = proxy_ptr->project_dir;
415 if (project_name != NULL)
417 *project_name = proxy_ptr->project_name;
420 if (project_description != NULL)
422 *project_description = proxy_ptr->project_description;
425 if (project_notes != NULL)
427 *project_notes = proxy_ptr->project_notes;
431 bool
432 ladish_room_proxy_set_project_description(
433 ladish_room_proxy_handle proxy,
434 const char * description)
436 uint64_t new_version;
438 if (!cdbus_call(0, proxy_ptr->service, proxy_ptr->object, IFACE_ROOM, "SetProjectDescription", "s", &description, "t", &new_version))
440 log_error("SetProjectDescription() failed.");
441 return false;
444 return true;
447 bool
448 ladish_room_proxy_set_project_notes(
449 ladish_room_proxy_handle proxy,
450 const char * notes)
452 uint64_t new_version;
454 if (!cdbus_call(0, proxy_ptr->service, proxy_ptr->object, IFACE_ROOM, "SetProjectNotes", "s", &notes, "t", &new_version))
456 log_error("SetProjectNotes(%s) failed.", notes);
457 return false;
460 return true;
463 bool
464 ladish_room_proxy_get_recent_projects(
465 ladish_room_proxy_handle proxy,
466 uint16_t max_items,
467 void (* callback)(
468 void * context,
469 const char * project_name,
470 const char * project_dir),
471 void * context)
473 DBusMessage * reply_ptr;
474 const char * reply_signature;
475 DBusMessageIter top_iter;
476 DBusMessageIter struct_iter;
477 DBusMessageIter array_iter;
478 const char * project_dir;
479 const char * project_name;
481 if (!cdbus_call(0, proxy_ptr->service, proxy_ptr->object, IFACE_RECENT_ITEMS, "get", "q", &max_items, NULL, &reply_ptr))
483 log_error("GetStudioList() failed.");
484 return false;
487 reply_signature = dbus_message_get_signature(reply_ptr);
488 if (strcmp(reply_signature, "a(sa{sv})") != 0)
490 log_error("GetStudioList() reply signature mismatch. '%s'", reply_signature);
491 dbus_message_unref(reply_ptr);
492 return false;
495 dbus_message_iter_init(reply_ptr, &top_iter);
496 for (dbus_message_iter_recurse(&top_iter, &array_iter);
497 dbus_message_iter_get_arg_type(&array_iter) != DBUS_TYPE_INVALID;
498 dbus_message_iter_next(&array_iter))
500 dbus_message_iter_recurse(&array_iter, &struct_iter);
501 dbus_message_iter_get_basic(&struct_iter, &project_dir);
502 dbus_message_iter_next(&struct_iter);
504 if (!cdbus_iter_get_dict_entry_string(&struct_iter, "name", &project_name))
506 project_name = NULL;
509 callback(context, project_name != NULL ? project_name : project_dir, project_dir);
511 dbus_message_iter_next(&struct_iter);
514 dbus_message_unref(reply_ptr);
515 return true;
518 #undef proxy_ptr