update laditools submodule
[ladish.git] / daemon / cmd_new_studio.c
blob6a5691b126be4dd0bdcba6e328580c6870f893b7
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 the "new studio" command
9 **************************************************************************
11 * LADI Session Handler is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * LADI Session Handler is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with LADI Session Handler. If not, see <http://www.gnu.org/licenses/>
23 * or write to the Free Software Foundation, Inc.,
24 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
27 #include "cmd.h"
28 #include "studio_internal.h"
30 struct ladish_command_new_studio
32 struct ladish_command command; /* must be the first member */
33 char * studio_name;
36 #define cmd_ptr ((struct ladish_command_new_studio *)context)
38 static bool run(void * context)
40 ASSERT(cmd_ptr->command.state == LADISH_COMMAND_STATE_PENDING);
41 ASSERT(g_studio.name == NULL); /* the unload command that was queued should have been cleared here */
43 if (*cmd_ptr->studio_name != 0)
45 g_studio.name = cmd_ptr->studio_name;
46 cmd_ptr->studio_name = NULL;
48 else if (!studio_name_generate(&g_studio.name))
50 log_error("studio_name_generate() failed.");
51 return false;
54 if (!studio_publish())
56 log_error("studio_publish() failed.");
57 free(g_studio.name);
58 g_studio.name = NULL;
59 return false;
62 cmd_ptr->command.state = LADISH_COMMAND_STATE_DONE;
63 return true;
66 static void destructor(void * context)
68 log_info("studio name command destructor");
69 if (cmd_ptr->studio_name != NULL)
71 free(cmd_ptr->studio_name);
75 #undef cmd_ptr
77 bool ladish_command_new_studio(void * call_ptr, struct ladish_cqueue * queue_ptr, const char * studio_name)
79 struct ladish_command_new_studio * cmd_ptr;
80 char * studio_name_dup;
82 if (!ladish_command_unload_studio(call_ptr, queue_ptr))
84 goto fail;
87 studio_name_dup = strdup(studio_name);
88 if (studio_name_dup == NULL)
90 lash_dbus_error(call_ptr, LASH_DBUS_ERROR_GENERIC, "strdup('%s') failed.", studio_name);
91 goto fail_drop_unload_command;
94 cmd_ptr = ladish_command_new(sizeof(struct ladish_command_new_studio));
95 if (cmd_ptr == NULL)
97 log_error("ladish_command_new() failed.");
98 goto fail_free_name;
101 cmd_ptr->command.run = run;
102 cmd_ptr->command.destructor = destructor;
103 cmd_ptr->studio_name = studio_name_dup;
105 if (!ladish_cqueue_add_command(queue_ptr, &cmd_ptr->command))
107 lash_dbus_error(call_ptr, LASH_DBUS_ERROR_GENERIC, "ladish_cqueue_add_command() failed.");
108 goto fail_destroy_command;
111 return true;
113 fail_destroy_command:
114 free(cmd_ptr);
116 fail_free_name:
117 free(studio_name_dup);
119 fail_drop_unload_command:
120 ladish_cqueue_drop_command(queue_ptr);
122 fail:
123 return false;