1 /* -*- Mode: C ; c-basic-offset: 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
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
;
46 char * project_description
;
50 static bool update_project_properties(struct ladish_room_proxy
* proxy_ptr
, DBusMessage
* message_ptr
, const char * context
)
52 const char * signature
;
54 dbus_uint64_t version
;
57 const char * description
;
61 char * description_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
);
71 dbus_message_iter_init(message_ptr
, &iter
);
73 dbus_message_iter_get_basic(&iter
, &version
);
74 dbus_message_iter_next(&iter
);
78 log_error("%s contains project properties version 0", context
);
82 if (proxy_ptr
->project_properties_version
>= version
)
87 if (!cdbus_iter_get_dict_entry_string(&iter
, "name", &name
))
92 if (!cdbus_iter_get_dict_entry_string(&iter
, "dir", &dir
))
97 if (!cdbus_iter_get_dict_entry_string(&iter
, "description", &description
))
102 if (!cdbus_iter_get_dict_entry_string(&iter
, "notes", ¬es
))
107 name_buffer
= strdup(name
);
108 if (name_buffer
== NULL
)
110 log_error("strdup() failed for project name");
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
);
177 fail_free_description_buffer
:
178 free(description_buffer
);
179 fail_free_dir_buffer
:
181 fail_free_name_buffer
:
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.");
197 if (!update_project_properties(proxy_ptr
, reply_ptr
, "GetProjectProperties() reply"))
199 dbus_message_unref(reply_ptr
);
203 dbus_message_unref(reply_ptr
);
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");
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
},
227 ladish_room_proxy_create(
228 const char * service
,
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");
248 proxy_ptr
->service
= strdup(service
);
249 if (proxy_ptr
->service
== NULL
)
251 log_error("strdup() failed too duplicate service name '%s'", service
);
255 proxy_ptr
->object
= strdup(object
);
256 if (proxy_ptr
->object
== NULL
)
258 log_error("strdup() failed too duplicate object name '%s'", object
);
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
,
279 log_error("dbus_register_object_signal_hooks() failed for room");
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
;
291 unregister_signal_hooks
:
292 cdbus_unregister_object_signal_hooks(cdbus_g_dbus_connection
, proxy_ptr
->service
, proxy_ptr
->object
, IFACE_ROOM
);
294 free(proxy_ptr
->object
);
296 free(proxy_ptr
->service
);
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
);
334 char * ladish_room_proxy_get_name(ladish_room_proxy_handle proxy
)
336 DBusMessage
* reply_ptr
;
340 if (!cdbus_call(0, proxy_ptr
->service
, proxy_ptr
->object
, IFACE_ROOM
, "GetName", "", NULL
, &reply_ptr
))
342 log_error("GetName() failed.");
346 if (!dbus_message_get_args(
349 DBUS_TYPE_STRING
, &name
,
352 dbus_message_unref(reply_ptr
);
353 dbus_error_free(&cdbus_g_dbus_error
);
354 log_error("decoding reply of GetName failed.");
358 name_buffer
= strdup(name
);
359 dbus_message_unref(reply_ptr
);
360 if (name_buffer
== NULL
)
362 log_error("strdup() for app name failed.");
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.");
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.");
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.");
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
;
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.");
448 ladish_room_proxy_set_project_notes(
449 ladish_room_proxy_handle proxy
,
452 uint64_t new_version
;
454 if (!cdbus_call(0, proxy_ptr
->service
, proxy_ptr
->object
, IFACE_ROOM
, "SetProjectNotes", "s", ¬es
, "t", &new_version
))
456 log_error("SetProjectNotes(%s) failed.", notes
);
464 ladish_room_proxy_get_recent_projects(
465 ladish_room_proxy_handle proxy
,
469 const char * project_name
,
470 const char * project_dir
),
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.");
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
);
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
))
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
);