Correct Changelog.
[jack2.git] / dbus / xml_write_raw.c
blob3ef13997ebc6395ec0d0bbb5a4f11ca52142cb22
1 /* -*- Mode: C ; c-basic-offset: 4 -*- */
2 /*
3 Copyright (C) 2007,2008 Nedko Arnaudov
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 #if defined(HAVE_CONFIG_H)
21 #include "config.h"
22 #endif
24 #include <stdbool.h>
25 #include <stdint.h>
26 #include <sys/types.h>
27 #include <sys/stat.h>
28 #include <unistd.h>
29 #include <fcntl.h>
30 #include <errno.h>
31 #include <string.h>
32 #include <dbus/dbus.h>
33 #include <time.h>
35 #include "controller_internal.h"
36 #include "jackdbus.h"
38 bool
39 jack_controller_settings_write_string(int fd, const char * string, void *dbus_call_context_ptr)
41 size_t len;
43 len = strlen(string);
45 if (write(fd, string, len) != len)
47 jack_dbus_error(dbus_call_context_ptr, JACK_DBUS_ERROR_GENERIC, "write() failed to write config file.");
48 return false;
51 return true;
54 struct save_context
56 void * call;
57 int fd;
58 const char * indent;
59 jack_params_handle params;
60 const char * address[PARAM_ADDRESS_SIZE];
61 const char * str;
64 #define ctx_ptr ((struct save_context *)context)
65 #define fd (ctx_ptr->fd)
67 static bool jack_controller_serialize_parameter(void * context, const struct jack_parameter * param_ptr)
69 char value[JACK_PARAM_STRING_MAX + 1];
71 if (!param_ptr->vtable.is_set(param_ptr->obj))
73 return true;
76 jack_controller_serialize_parameter_value(param_ptr, value);
78 return
79 jack_controller_settings_write_string(fd, ctx_ptr->indent, ctx_ptr->call) &&
80 jack_controller_settings_write_string(fd, "<option name=\"", ctx_ptr->call) &&
81 jack_controller_settings_write_string(fd, param_ptr->name, ctx_ptr->call) &&
82 jack_controller_settings_write_string(fd, "\">", ctx_ptr->call) &&
83 jack_controller_settings_write_string(fd, value, ctx_ptr->call) &&
84 jack_controller_settings_write_string(fd, "</option>\n", ctx_ptr->call);
87 bool serialize_modules(void * context, const char * name)
89 ctx_ptr->indent = " ";
90 ctx_ptr->address[1] = name;
91 ctx_ptr->address[2] = NULL;
93 return
94 jack_controller_settings_write_string(fd, " <", ctx_ptr->call) &&
95 jack_controller_settings_write_string(fd, ctx_ptr->str, ctx_ptr->call) &&
96 jack_controller_settings_write_string(fd, " name=\"", ctx_ptr->call) &&
97 jack_controller_settings_write_string(fd, name, ctx_ptr->call) &&
98 jack_controller_settings_write_string(fd, "\">\n", ctx_ptr->call) &&
99 jack_params_iterate_params(ctx_ptr->params, ctx_ptr->address, jack_controller_serialize_parameter, ctx_ptr) &&
100 jack_controller_settings_write_string(fd, " </", ctx_ptr->call) &&
101 jack_controller_settings_write_string(fd, ctx_ptr->str, ctx_ptr->call) &&
102 jack_controller_settings_write_string(fd, ">\n", ctx_ptr->call);
105 #undef fd
106 #undef ctx_ptr
108 bool
109 jack_controller_settings_save(
110 struct jack_controller * controller_ptr,
111 void *dbus_call_context_ptr)
113 char *filename;
114 size_t conf_len;
115 int fd;
116 bool ret;
117 time_t timestamp;
118 char timestamp_str[26];
119 struct save_context context;
120 const char * modules[] = {"driver", "internal", NULL};
121 char buffer[100];
122 unsigned int i;
124 time(&timestamp);
125 ctime_r(&timestamp, timestamp_str);
126 timestamp_str[24] = 0;
128 ret = false;
130 conf_len = strlen(JACKDBUS_CONF);
132 filename = malloc(g_jackdbus_config_dir_len + conf_len + 1);
133 if (filename == NULL)
135 jack_error("Out of memory.");
136 goto exit;
139 memcpy(filename, g_jackdbus_config_dir, g_jackdbus_config_dir_len);
140 memcpy(filename + g_jackdbus_config_dir_len, JACKDBUS_CONF, conf_len);
141 filename[g_jackdbus_config_dir_len + conf_len] = 0;
143 jack_info("Saving settings to \"%s\" ...", filename);
145 fd = open(filename, O_WRONLY | O_TRUNC | O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
146 if (fd == -1)
148 jack_error("open() failed to open conf filename. error is %d (%s)", errno, strerror(errno));
149 goto exit_free_filename;
152 context.fd = fd;
153 context.call = dbus_call_context_ptr;
155 if (!jack_controller_settings_write_string(fd, "<?xml version=\"1.0\"?>\n", dbus_call_context_ptr))
157 goto exit_close;
160 if (!jack_controller_settings_write_string(fd, "<!--\n", dbus_call_context_ptr))
162 goto exit_close;
165 if (!jack_controller_settings_write_string(fd, JACK_CONF_HEADER_TEXT, dbus_call_context_ptr))
167 goto exit_close;
170 if (!jack_controller_settings_write_string(fd, "-->\n", dbus_call_context_ptr))
172 goto exit_close;
175 if (!jack_controller_settings_write_string(fd, "<!-- ", dbus_call_context_ptr))
177 goto exit_close;
180 if (!jack_controller_settings_write_string(fd, timestamp_str, dbus_call_context_ptr))
182 goto exit_close;
185 if (!jack_controller_settings_write_string(fd, " -->\n", dbus_call_context_ptr))
187 goto exit_close;
190 if (!jack_controller_settings_write_string(fd, "<jack>\n", dbus_call_context_ptr))
192 goto exit_close;
195 /* engine */
197 if (!jack_controller_settings_write_string(fd, " <engine>\n", dbus_call_context_ptr))
199 goto exit_close;
202 context.indent = " ";
203 context.address[0] = PTNODE_ENGINE;
204 context.address[1] = NULL;
205 if (!jack_params_iterate_params(controller_ptr->params, context.address, jack_controller_serialize_parameter, &context))
207 goto exit_close;
210 if (!jack_controller_settings_write_string(fd, " </engine>\n", dbus_call_context_ptr))
212 goto exit_close;
215 for (i = 0; modules[i] != NULL; i++)
217 if (!jack_controller_settings_write_string(fd, " <", dbus_call_context_ptr))
219 goto exit_close;
222 if (!jack_controller_settings_write_string(fd, modules[i], dbus_call_context_ptr))
224 goto exit_close;
227 if (!jack_controller_settings_write_string(fd, "s>\n", dbus_call_context_ptr))
229 goto exit_close;
232 context.indent = " ";
233 context.params = controller_ptr->params;
234 context.str = modules[i];
235 strcpy(buffer, modules[i]);
236 strcat(buffer, "s");
237 context.address[0] = buffer;
238 context.address[1] = NULL;
240 if (!jack_params_iterate_container(controller_ptr->params, context.address, serialize_modules, &context))
242 goto exit_close;
245 if (!jack_controller_settings_write_string(fd, " </", dbus_call_context_ptr))
247 goto exit_close;
250 if (!jack_controller_settings_write_string(fd, modules[i], dbus_call_context_ptr))
252 goto exit_close;
255 if (!jack_controller_settings_write_string(fd, "s>\n", dbus_call_context_ptr))
257 goto exit_close;
261 if (!jack_controller_settings_write_string(fd, "</jack>\n", dbus_call_context_ptr))
263 goto exit_close;
266 ret = true;
268 exit_close:
269 close(fd);
271 exit_free_filename:
272 free(filename);
274 exit:
275 return ret;
278 void
279 jack_controller_settings_save_auto(
280 struct jack_controller * controller_ptr)
282 jack_controller_settings_save(controller_ptr, NULL);