From 186b8ac84ef4a9fecd520f511eb936f0882749a0 Mon Sep 17 00:00:00 2001 From: Nedko Arnaudov Date: Mon, 24 Aug 2009 01:58:14 +0300 Subject: [PATCH] GetStudioList implemented --- daemon/control.c | 73 ++++++++++++++++++++++++++++++++++---------------------- daemon/studio.c | 56 ++++++++++++++++++++++++++++++++++++++++++- daemon/studio.h | 2 ++ dbus/error.c | 21 ++++++++-------- 4 files changed, 112 insertions(+), 40 deletions(-) diff --git a/daemon/control.c b/daemon/control.c index 99d889eb..bdb66115 100644 --- a/daemon/control.c +++ b/daemon/control.c @@ -64,12 +64,48 @@ fail: lash_error("Ran out of memory trying to construct method return"); } +#define array_iter_ptr ((DBusMessageIter *)context) + +static bool get_studio_list_callback(void * call_ptr, void * context, const char * studio, uint32_t modtime) +{ + DBusMessageIter struct_iter; + DBusMessageIter dict_iter; + bool ret; + + ret = false; + + if (!dbus_message_iter_open_container(array_iter_ptr, DBUS_TYPE_STRUCT, NULL, &struct_iter)) + goto exit; + + if (!dbus_message_iter_append_basic(&struct_iter, DBUS_TYPE_STRING, &studio)) + goto close_struct; + + if (!dbus_message_iter_open_container(&struct_iter, DBUS_TYPE_ARRAY, "{sv}", &dict_iter)) + goto close_struct; + +/* if (!maybe_add_dict_entry_string(&dict_iter, "Description", xxx)) */ +/* goto close_dict; */ + + if (!dbus_add_dict_entry_uint32(&dict_iter, "Modification Time", modtime)) + goto close_dict; + + ret = true; + +close_dict: + if (!dbus_message_iter_close_container(&struct_iter, &dict_iter)) + ret = false; + +close_struct: + if (!dbus_message_iter_close_container(array_iter_ptr, &struct_iter)) + ret = false; + +exit: + return ret; +} + static void ladish_get_studio_list(method_call_t * call_ptr) { DBusMessageIter iter, array_iter; - //DBusMessageIter struct_iter, dict_iter; - //struct list_head * node_ptr; - //project_t * project_ptr; call_ptr->reply = dbus_message_new_method_return(call_ptr->message); if (call_ptr->reply == NULL) @@ -84,36 +120,15 @@ static void ladish_get_studio_list(method_call_t * call_ptr) goto fail_unref; } -#if 0 - list_for_each (node_ptr, &g_server->all_projects) + if (!studios_iterate(call_ptr, &array_iter, get_studio_list_callback)) { - project_ptr = list_entry(node_ptr, project_t, siblings_all); - - if (!dbus_message_iter_open_container(&array_iter, DBUS_TYPE_STRUCT, NULL, &struct_iter)) + dbus_message_iter_close_container(&iter, &array_iter); + if (call_ptr->reply == NULL) goto fail_unref; - if (!dbus_message_iter_append_basic(&struct_iter, DBUS_TYPE_STRING, &project_ptr->name)) - { - dbus_message_iter_close_container(&iter, &array_iter); - goto fail_unref; - } - - if (!dbus_message_iter_open_container(&struct_iter, DBUS_TYPE_ARRAY, "{sv}", &dict_iter)) - goto fail_unref; - - if (!maybe_add_dict_entry_string(&dict_iter, "Description", project_ptr->description)) - goto fail_unref; - - if (!add_dict_entry_uint32(&dict_iter, "Modification Time", project_ptr->last_modify_time)) - goto fail_unref; - - if (!dbus_message_iter_close_container(&struct_iter, &dict_iter)) - goto fail_unref; - - if (!dbus_message_iter_close_container(&array_iter, &struct_iter)) - goto fail_unref; + /* studios_iterate or get_studio_list_callback() composed error reply */ + return; } -#endif if (!dbus_message_iter_close_container(&iter, &array_iter)) { diff --git a/daemon/studio.c b/daemon/studio.c index 5c345346..13442bdc 100644 --- a/daemon/studio.c +++ b/daemon/studio.c @@ -30,6 +30,7 @@ #include #include #include +#include #include "../jack_proxy.h" #include "patchbay.h" @@ -39,7 +40,7 @@ #include "../dbus/error.h" #include "dirhelpers.h" -#define STUDIOS_DIR "/studios" +#define STUDIOS_DIR "/studios/" char * g_studios_dir; #define STUDIO_HEADER_TEXT BASE_NAME " Studio configuration.\n" @@ -988,6 +989,59 @@ exit: return ret; } +bool studios_iterate(void * call_ptr, void * context, bool (* callback)(void * call_ptr, void * context, const char * studio, uint32_t modtime)) +{ + DIR * dir; + struct dirent * dentry; + size_t len; + struct stat st; + char * path; + + dir = opendir(g_studios_dir); + if (dir == NULL) + { + lash_dbus_error(call_ptr, LASH_DBUS_ERROR_GENERIC, "Cannot open directory '%s': %d (%s)", g_studios_dir, errno, strerror(errno)); + return false; + } + + while ((dentry = readdir(dir)) != NULL) + { + if (dentry->d_type != DT_REG) + continue; + + len = strlen(dentry->d_name); + if (len < 4 || strcmp(dentry->d_name + (len - 4), ".xml")) + continue; + + path = catdup(g_studios_dir, dentry->d_name); + if (path == NULL) + { + lash_dbus_error(call_ptr, LASH_DBUS_ERROR_GENERIC, "catdup() failed"); + return false; + } + + dentry->d_name[len - 4] = 0; + + if (stat(path, &st) != 0) + { + lash_dbus_error(call_ptr, LASH_DBUS_ERROR_GENERIC, "failed to stat '%s': %d (%s)", path, errno, strerror(errno)); + free(path); + return false; + } + + free(path); + + if (!callback(call_ptr, context, dentry->d_name, st.st_mtime)) + { + closedir(dir); + return false; + } + } + + closedir(dir); + return true; +} + bool studio_load(const char * file_path) { return false; /* not implemented yet */ diff --git a/daemon/studio.h b/daemon/studio.h index 5d3e6ca6..40fca09d 100644 --- a/daemon/studio.h +++ b/daemon/studio.h @@ -32,4 +32,6 @@ void studio_uninit(void); void studio_run(void); bool studio_is_loaded(void); +bool studios_iterate(void * call_ptr, void * context, bool (* callback)(void * call_ptr, void * context, const char * studio, uint32_t modtime)); + #endif /* #ifndef STUDIO_H__0BEDE85E_4FB3_4D74_BC08_C373A22409C0__INCLUDED */ diff --git a/dbus/error.c b/dbus/error.c index d76bbf59..7d5c70d7 100644 --- a/dbus/error.c +++ b/dbus/error.c @@ -2,7 +2,7 @@ /* * LADI Session Handler (ladish) * - * Copyright (C) 2008 Nedko Arnaudov + * Copyright (C) 2008, 2009 Nedko Arnaudov * Copyright (C) 2008 Juuso Alasuutari * ************************************************************************** @@ -51,15 +51,16 @@ lash_dbus_error(method_call_t *call_ptr, va_end(ap); - interface_name = (call_ptr->interface - && call_ptr->interface->name - && call_ptr->interface->name[0]) - ? call_ptr->interface->name - : ""; + if (call_ptr != NULL) + { + interface_name = (call_ptr->interface && call_ptr->interface->name && call_ptr->interface->name[0]) ? call_ptr->interface->name : ""; - lash_error("In method %s.%s: %s", interface_name, - call_ptr->method_name, message); + lash_error("In method %s.%s: %s", interface_name, call_ptr->method_name, message); - call_ptr->reply = dbus_message_new_error(call_ptr->message, err_name, - message); + call_ptr->reply = dbus_message_new_error(call_ptr->message, err_name, message); + } + else + { + lash_error("%s", message); + } } -- 2.11.4.GIT