Some documenting thingies.
[irreco.git] / irreco / trunk / src / core / irreco_config.c
blobb4fe2ee611eb5fba8f425c4969a56af02d04b3a2
1 /*
2 * irreco - Ir Remote Control
3 * Copyright (C) 2007 Arto Karppinen (arto.karppinen@iki.fi),
4 * Harri Vattulainen (t5vaha01@students.oamk.fi)
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 #include "irreco_config.h"
22 #include "irreco_button.h"
25 * Configuration saving and reading.
28 #define IRRECO_CONFIG_GROUP_LEN 20
30 gchar* irreco_config_layout_file = "layouts.conf";
31 gchar* irreco_config_active_layout_file = "active-layout.conf";
32 gchar* irreco_config_backend_file = "backends.conf";
33 gchar* irreco_config_cmd_chain_file = "command-chains.conf";
38 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
39 /* Prototypes */
40 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
41 void irreco_config_read_style_from_keyfile(IrrecoData * irreco_data,
42 IrrecoKeyFile *keyfile,
43 gboolean editable);
44 void irreco_config_read_button_from_keyfile(IrrecoData * irreco_data,
45 IrrecoKeyFile * keyfile);
46 void irreco_config_read_hardkey_from_keyfile(IrrecoData * irreco_data,
47 IrrecoKeyFile * keyfile);
48 void irreco_config_read_layout_from_keyfile(IrrecoData * irreco_data,
49 IrrecoKeyFile * keyfile);
50 void irreco_config_read_command_from_keyfile(IrrecoData * irreco_data,
51 IrrecoKeyFile * keyfile);
52 void irreco_config_save_layout_to_keyfile(GKeyFile * keyfile,
53 IrrecoButtonLayout * layout,
54 gint * layout_index,
55 gint * button_index,
56 gint * command_index,
57 gint * hardkey_index);
58 void irreco_config_save_hardkey_to_file(IrrecoHardkeyMap *hardkey_map,
59 guint keyval,
60 IrrecoCmdChainId chain_id,
61 IrrecoCmdChain *chain,
62 gpointer user_data);
63 void irreco_config_save_button_to_keyfile(GKeyFile * keyfile,
64 IrrecoButton * button,
65 gint * button_index,
66 gint * command_index);
67 void irreco_config_save_cmd_chain_to_keyfile(GKeyFile * keyfile,
68 IrrecoButton * button,
69 gint * command_index);
70 void irreco_config_read_backend_from_keyfile(
71 IrrecoBackendManager * backend_manager,
72 IrrecoKeyFile * keyfile);
76 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
77 /* Layout reading. */
78 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
80 /**
81 * Read bstyleconf file from directory.
83 * One subdirectory may contain only one IrrecoButtonStyle configuration and
84 * "button-style" group.
86 void irreco_config_read_styles_from_dir_foreach(IrrecoDirForeachData * dir_data)
88 IrrecoData * irreco_data = (IrrecoData*) dir_data->user_data_1;
89 IrrecoKeyFile *keyfile;
90 IRRECO_ENTER
92 keyfile = irreco_keyfile_create(dir_data->directory,
93 dir_data->filepath,
94 "button-style");
95 if (keyfile != NULL) {
96 irreco_config_read_style_from_keyfile(irreco_data, keyfile,
97 FALSE);
98 irreco_keyfile_destroy(keyfile);
101 IRRECO_RETURN
105 * Reads buttons-folder inside themefolder
107 * This function uses irreco_dir_foreach() which calls
108 * irreco_config_read_styles_from_dir_foreach()
110 void irreco_config_read_buttons_from_dir_foreach(IrrecoDirForeachData * dir_data)
112 IrrecoDirForeachData button_styles;
113 GString * directory = g_string_new("");
114 IRRECO_ENTER
116 g_string_printf(directory, "%s/buttons/", dir_data->filepath);
117 IRRECO_DEBUG("Directory = %s\n", directory->str);
118 button_styles.directory = directory->str;
120 button_styles.filesuffix = "bstyleconf";
121 button_styles.user_data_1 = dir_data->user_data_1;
123 irreco_dir_foreach_subdirectories(&button_styles,
124 irreco_config_read_styles_from_dir_foreach);
126 g_string_free(directory, TRUE);
127 directory = NULL;
128 IRRECO_RETURN
132 * Reads configs from subdirectories ONLY
135 void irreco_config_read_styles_from_dir(IrrecoData * irreco_data,
136 const gchar *dir)
138 IrrecoDirForeachData dir_data;
139 IRRECO_ENTER
141 dir_data.directory = dir;
142 dir_data.filesuffix = ".bstyleconf";
143 dir_data.user_data_1 = irreco_data;
144 irreco_dir_foreach_subdirectories(&dir_data,
145 irreco_config_read_styles_from_dir_foreach);
147 IRRECO_RETURN
152 * Reads themefolders from subdirectories
154 * This function uses irreco_dir_foreach() which calls
155 * irreco_config_read_buttons_from_dir_foreach()
157 void irreco_config_read_themes_from_dir(IrrecoData * irreco_data,
158 const gchar *dir)
160 IrrecoDirForeachData dir_data;
161 IRRECO_ENTER
163 dir_data.directory = dir;
164 dir_data.filesuffix = "";
165 dir_data.user_data_1 = irreco_data;
166 irreco_dir_foreach(&dir_data,
167 irreco_config_read_buttons_from_dir_foreach);
169 IRRECO_RETURN
172 void irreco_config_read_style_from_file(IrrecoData * irreco_data,
173 const gchar *file)
175 gchar *dir;
176 IrrecoKeyFile *keyfile;
177 IRRECO_ENTER
179 dir = g_path_get_dirname(file);
180 keyfile = irreco_keyfile_create(dir, file, "button-style");
181 if (keyfile != NULL) {
182 irreco_config_read_style_from_keyfile(irreco_data, keyfile,
183 FALSE);
184 irreco_keyfile_destroy(keyfile);
186 g_free(dir);
188 IRRECO_RETURN
191 gboolean irreco_config_read_layouts(IrrecoData * irreco_data)
193 guint i;
194 IrrecoKeyFile *keyfile = NULL;
195 gchar *config_dir = NULL;
196 gchar *config_file = NULL;
197 gchar** groups = NULL;
198 gsize group_count = 0;
199 IRRECO_ENTER
201 config_dir = irreco_get_config_dir("irreco");
202 config_file = irreco_get_config_file("irreco",
203 irreco_config_layout_file);
204 keyfile = irreco_keyfile_create(config_dir, config_file, NULL);
205 if (config_dir == NULL || config_file == NULL || keyfile == NULL) {
206 g_free(config_dir);
207 g_free(config_file);
208 irreco_keyfile_destroy(keyfile);
209 IRRECO_RETURN_BOOL(FALSE);
212 IRRECO_PRINTF("Reading layout configuration file \"%s\"\n",
213 config_file);
214 groups = g_key_file_get_groups(keyfile->keyfile, &group_count);
216 /* Read layouts. */
217 for (i = 0; i < group_count; i++) {
218 gchar *group = groups[i];
219 if (g_str_has_prefix(group, "layout")) {
220 IRRECO_PRINTF("Reading layout group \"%s\"\n", group);
221 irreco_keyfile_set_group(keyfile, group);
222 irreco_config_read_layout_from_keyfile(irreco_data,
223 keyfile);
227 /* Read Buttons. */
228 for (i = 0; i < group_count; i++) {
229 gchar *group = groups[i];
230 if (g_str_has_prefix(group, "button")) {
231 IRRECO_PRINTF("Reading button group \"%s\"\n", group);
232 irreco_keyfile_set_group(keyfile, group);
233 irreco_config_read_button_from_keyfile(irreco_data,
234 keyfile);
238 /* Read Hardkeys. */
239 for (i = 0; i < group_count; i++) {
240 gchar *group = groups[i];
241 if (g_str_has_prefix(group, "hardkey")) {
242 IRRECO_PRINTF("Reading hardkey group \"%s\"\n", group);
243 irreco_keyfile_set_group(keyfile, group);
244 irreco_config_read_hardkey_from_keyfile(irreco_data,
245 keyfile);
250 * Read LEGACY command group configuration.
253 gchar *chain_config = irreco_get_config_file(
254 "irreco", irreco_config_cmd_chain_file);
255 if (irreco_file_exists(chain_config) == FALSE) {
256 for (i = 0; i < group_count; i++) {
257 gchar *group = groups[i];
258 if (g_str_has_prefix(group, "command")) {
259 IRRECO_PRINTF("Reading command group \"%s\"\n",
260 group);
261 irreco_keyfile_set_group(keyfile, group);
262 irreco_config_read_command_from_keyfile(
263 irreco_data, keyfile);
267 g_free(chain_config);
270 /* Read command chains. */
271 irreco_cmd_chain_manager_from_config(irreco_data);
273 g_strfreev(groups);
274 irreco_keyfile_destroy(keyfile);
275 g_free(config_dir);
276 g_free(config_file);
277 IRRECO_RETURN_BOOL(TRUE);
280 void irreco_config_read_style_from_keyfile(IrrecoData * irreco_data,
281 IrrecoKeyFile *keyfile,
282 gboolean editable)
284 IrrecoButtonStyle* style;
285 IRRECO_ENTER
287 style = irreco_button_style_create(
288 irreco_data, NULL, NULL, NULL, TRUE, editable);
290 /* Required fields. */
291 if (!irreco_keyfile_get_str(keyfile, "name", &style->name) ||
292 !irreco_keyfile_get_path(keyfile, "up", &style->up)) {
293 IRRECO_PRINTF("Could not read style from group \"%s\"\n",
294 keyfile->group);
295 irreco_button_style_destroy(style);
296 IRRECO_RETURN
298 IRRECO_PRINTF("Name = \"%s\"\n", style->name);
299 IRRECO_PRINTF("Up = \"%s\"\n", style->up);
301 /* Optional fields. */
302 irreco_keyfile_get_path(keyfile, "down", &style->down);
303 IRRECO_PRINTF("Down = \"%s\"\n", style->down);
304 irreco_keyfile_get_bool(keyfile, "allow-text", &style->allow_text);
305 IRRECO_PRINTF("Allow-text = \"%i\"\n", style->allow_text);
306 irreco_keyfile_get_str(keyfile, "text-format-up", &style->text_format_up);
307 IRRECO_PRINTF("text-format-up = \"%s\"\n", style->text_format_up);
308 irreco_keyfile_get_str(keyfile, "text-format-down",
309 &style->text_format_down);
310 IRRECO_PRINTF("text-format-down = \"%s\"\n", style->text_format_down);
311 irreco_keyfile_get_int(keyfile, "text-padding", &style->text_padding);
312 IRRECO_PRINTF("Text-padding = \"%i\"\n", style->text_padding);
314 /* Attempt to add the style to the style array. */
315 if (!irreco_string_table_add(irreco_data->button_style_array, style->name,
316 style)) {
317 IRRECO_ERROR("Dublicate style name \"%s\"\n", style->name);
318 irreco_button_style_destroy(style);
321 IRRECO_RETURN
324 void irreco_config_read_layout_from_keyfile(IrrecoData * irreco_data,
325 IrrecoKeyFile * keyfile)
327 gchar *name = NULL;
328 IrrecoButtonLayout *layout = NULL;
329 GdkColor color;
330 gint type;
331 gchar *image = NULL;
332 IRRECO_ENTER
334 /* Required fields. */
335 if (!irreco_keyfile_get_str(keyfile, "name", &name)) IRRECO_RETURN
336 layout = irreco_button_layout_create(NULL,
337 irreco_data->cmd_chain_manager);
338 irreco_button_layout_set_name(layout, name);
339 g_free(name);
340 name = NULL;
342 if (irreco_keyfile_get_int(keyfile, "bg-type", &type)) {
343 switch (type) {
344 case IRRECO_BACKGROUND_DEFAULT:
345 case IRRECO_BACKGROUND_COLOR:
346 case IRRECO_BACKGROUND_IMAGE:
347 irreco_button_layout_set_bg_type(layout, type);
348 break;
350 default:
351 IRRECO_ERROR("Unknown background type \"%i\".\n",
352 type);
353 break;
357 if (irreco_keyfile_get_str(keyfile, "bg-image", &image)) {
358 irreco_button_layout_set_bg_image(layout, image);
359 g_free(image);
360 image = NULL;
363 color.pixel = 0;
364 if (irreco_keyfile_get_uint16(keyfile, "bg-color-red", &color.red) &&
365 irreco_keyfile_get_uint16(keyfile, "bg-color-green", &color.green)&&
366 irreco_keyfile_get_uint16(keyfile, "bg-color-blue", &color.blue)) {
367 irreco_button_layout_set_bg_color(layout, &color);
370 if (!irreco_string_table_add(irreco_data->irreco_layout_array,
371 irreco_button_layout_get_name(layout),
372 layout)) {
373 IRRECO_ERROR("Layout \"%s\" already exists.",
374 irreco_button_layout_get_name(layout));
375 irreco_button_layout_destroy(layout);
378 IRRECO_RETURN
381 void irreco_config_read_button_from_keyfile(IrrecoData * irreco_data,
382 IrrecoKeyFile * keyfile)
384 gchar *layout_name = NULL;
385 gchar *style_name = NULL;
386 gchar *title = NULL;
387 gchar *command = NULL;
388 IrrecoCmdChainId chain_id;
389 gint x;
390 gint y;
392 IrrecoButton *button = NULL;
393 IrrecoButtonLayout *layout = NULL;
394 IrrecoButtonStyle *style = NULL;
395 IRRECO_ENTER
397 /* Required fields. */
398 if (!irreco_keyfile_get_int(keyfile, "x", &x) ||
399 !irreco_keyfile_get_int(keyfile, "y", &y) ||
400 !irreco_keyfile_get_str(keyfile, "layout", &layout_name)) {
401 IRRECO_RETURN
403 IRRECO_PRINTF("Position: x%i y%i\n", x, y);
405 /* Find layout. */
406 if (!irreco_string_table_get(irreco_data->irreco_layout_array,
407 layout_name, (gpointer *) &layout)) {
408 IRRECO_ERROR("Button uses unknown layout \"%s\"\n", layout_name);
409 g_free(layout_name);
410 IRRECO_RETURN
412 g_free(layout_name);
413 IRRECO_PRINTF("Style: \"%s\"\n",
414 irreco_button_layout_get_name(layout));
416 /* Find style. */
417 irreco_keyfile_get_str(keyfile, "style", &style_name);
418 if (style_name != NULL) {
420 if (irreco_string_table_get(irreco_data->button_style_array,
421 style_name, (gpointer *) &style)) {
422 IRRECO_PRINTF("Style: \"%s\"\n", style->name);
423 } else {
424 IRRECO_ERROR("Button uses unknown style \"%s\"\n",
425 style_name);
427 g_free(style_name);
430 /* Get title and command. */
431 irreco_keyfile_get_str(keyfile, "title", &title);
432 IRRECO_PRINTF("title: \"%s\"\n", title);
434 button = irreco_button_create(layout, x, y, title, style,
435 irreco_data->cmd_chain_manager);
437 if (irreco_keyfile_get_int(keyfile, "command-chain-id", &chain_id)) {
438 irreco_button_set_cmd_chain_id(button, chain_id);
441 g_free(title);
442 g_free(command);
443 IRRECO_RETURN
446 void irreco_config_read_hardkey_from_keyfile(IrrecoData * irreco_data,
447 IrrecoKeyFile * keyfile)
449 IrrecoButtonLayout *layout = NULL;
450 gchar *layout_name = NULL;
451 IrrecoCmdChainId id;
452 guint keyval;
453 gchar *hardkey_str = NULL;
454 IRRECO_ENTER
456 /* Required fields. */
457 if (!irreco_keyfile_get_str(keyfile, "layout", &layout_name) ||
458 !irreco_keyfile_get_int(keyfile, "chain-id", &id) ||
459 !irreco_keyfile_get_uint(keyfile, "key-value", &keyval)) {
460 g_free(layout_name);
461 IRRECO_RETURN
464 /* Find layout. */
465 if (!irreco_string_table_get(irreco_data->irreco_layout_array,
466 layout_name, (gpointer *) &layout)) {
467 IRRECO_ERROR("Hardkey uses unknown layout \"%s\"\n",
468 layout_name);
469 g_free(layout_name);
470 IRRECO_RETURN
472 g_free(layout_name);
474 hardkey_str = irreco_hardkey_to_str(keyval);
475 IRRECO_PRINTF("Associating layout \"%s\" hardkey \"%s\" with "
476 "command chain \"%i\".\n",
477 irreco_button_layout_get_name(layout),
478 hardkey_str, id);
479 g_free(hardkey_str);
481 irreco_hardkey_map_assosiate_chain_with_id(layout->hardkey_map,
482 keyval, id);
483 IRRECO_RETURN
487 * Support LEGACY configuration.
489 * This still exists in order to read layout configs from old versions of
490 * irreco.
492 * TODO: Remove sometime next year.
494 void irreco_config_read_command_from_keyfile(IrrecoData * irreco_data,
495 IrrecoKeyFile * keyfile)
497 gint button_index;
498 IrrecoCmdType type;
499 gchar *layout_name = NULL;
500 gchar *type_name = NULL;
502 IrrecoCmd *command;
503 IrrecoButton *button;
504 IrrecoButtonLayout *layout;
505 IRRECO_ENTER
507 if (!irreco_keyfile_get_str(keyfile, "layout", &layout_name) ||
508 !irreco_keyfile_get_str(keyfile, "type", &type_name) ||
509 !irreco_keyfile_get_int(keyfile, "button", &button_index)) {
510 g_free(layout_name);
511 g_free(type_name);
512 IRRECO_RETURN
515 /* Convert type name to type ID. */
516 if ((type = irreco_cmd_str_to_type(type_name)) == -1) {
517 IRRECO_ERROR("Unknown command type \"%s\"\n", type_name);
518 g_free(layout_name);
519 g_free(type_name);
520 IRRECO_RETURN
522 g_free(type_name);
524 /* Find layout. */
525 if (!irreco_string_table_get(irreco_data->irreco_layout_array,
526 layout_name, (gpointer *) &layout)) {
527 IRRECO_ERROR("Unknown layout \"%s\"\n", layout_name);
528 g_free(layout_name);
529 IRRECO_RETURN
531 g_free(layout_name);
533 /* Find Button. */
534 if (button_index >= layout->button_array->len) {
535 IRRECO_ERROR("Invalid button index \"%i\"\n", button_index);
536 IRRECO_RETURN
538 button = g_ptr_array_index(layout->button_array, button_index);
540 /* Append command to button command chain. */
541 switch (type) {
542 case IRRECO_COMMAND_NONE:
543 case IRRECO_COMMAND_NEXT_REMOTE:
544 case IRRECO_COMMAND_PREVIOUS_REMOTE:
545 case IRRECO_COMMAND_FULLSCREEN_TOGGLE:
546 command = irreco_cmd_create();
547 irreco_cmd_set_builtin(command, type);
548 irreco_cmd_chain_append(irreco_button_get_cmd_chain(button),
549 command);
550 break;
552 case IRRECO_COMMAND_BACKEND: {
553 gchar *instance_name = NULL;
554 gchar *device_name = NULL;
555 gchar *command_name = NULL;
556 IrrecoBackendInstance * backend_instance;
558 if (!irreco_keyfile_get_str(keyfile, "instance-name",
559 &instance_name) ||
560 !irreco_keyfile_get_str(keyfile, "device-name",
561 &device_name) ||
562 !irreco_keyfile_get_str(keyfile, "command-name",
563 &command_name)) {
564 g_free(instance_name);
565 g_free(device_name);
566 g_free(command_name);
567 IRRECO_RETURN
570 if (!irreco_backend_manager_find_instance(
571 irreco_data->irreco_backend_manager,
572 instance_name,
573 &backend_instance)) {
574 IRRECO_ERROR("Could not find instance with name "
575 "\"%s\"\n", instance_name);
576 IRRECO_RETURN
578 command = irreco_cmd_create();
579 irreco_cmd_set_backend(command,
580 backend_instance,
581 device_name,
582 command_name);
583 irreco_cmd_chain_append(irreco_button_get_cmd_chain(button),
584 command);
586 g_free(instance_name);
587 g_free(device_name);
588 g_free(command_name);
589 } break;
591 case IRRECO_COMMAND_SHOW_LAYOUT: {
592 gchar *show_layout;
593 if (!irreco_keyfile_get_str(keyfile, "show-layout",
594 &show_layout)) {
595 IRRECO_RETURN
598 command = irreco_cmd_create();
599 irreco_cmd_set_layout(command, show_layout);
600 irreco_cmd_chain_append(irreco_button_get_cmd_chain(button),
601 command);
602 g_free(show_layout);
603 } break;
605 case IRRECO_COMMAND_WAIT: {
606 guint delay;
607 if (!irreco_keyfile_get_uint(keyfile, "delay", &delay)) {
608 IRRECO_RETURN
611 command = irreco_cmd_create();
612 irreco_cmd_set_wait(command, delay);
613 irreco_cmd_chain_append(irreco_button_get_cmd_chain(button),
614 command);
615 } break;
618 IRRECO_RETURN
624 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
625 /* Layout saving. */
626 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
629 * Save layout.
631 * Since GKeyFile does not allow two groups with the same name,
632 * layout_index and button_index variables are used to make sure that each
633 * button and layout group has unique name. The index is ignored when reading
634 * the layout config.
636 gboolean irreco_config_save_layouts(IrrecoData * irreco_data)
638 gint layout_index = 1;
639 gint button_index = 1;
640 gint command_index = 1;
641 gint hardkey_index = 1;
642 GKeyFile *keyfile;
643 IrrecoStringTable *table;
644 gboolean success;
645 IRRECO_ENTER
647 irreco_cmd_chain_manager_to_config(irreco_data->cmd_chain_manager);
649 keyfile = g_key_file_new();
650 table = irreco_data->irreco_layout_array;
651 IRRECO_STRING_TABLE_FOREACH(table, key, IrrecoButtonLayout *, layout)
652 irreco_config_save_layout_to_keyfile(keyfile,
653 layout,
654 &layout_index,
655 &button_index,
656 &command_index,
657 &hardkey_index);
658 layout_index++;
659 IRRECO_STRING_TABLE_FOREACH_END
661 success = irreco_gkeyfile_write_to_config_file(keyfile, "irreco",
662 irreco_config_layout_file);
663 g_key_file_free(keyfile);
664 IRRECO_RETURN_BOOL(success);
667 typedef struct _IrrecoConfigSaveHardkeyData IrrecoConfigSaveHardkeyData;
668 struct _IrrecoConfigSaveHardkeyData {
669 GKeyFile *keyfile;
670 gint *hardkey_index;
671 IrrecoButtonLayout *layout;
674 void irreco_config_save_layout_to_keyfile(GKeyFile * keyfile,
675 IrrecoButtonLayout * layout,
676 gint * layout_index,
677 gint * button_index,
678 gint * command_index,
679 gint * hardkey_index)
681 GdkColor *color;
682 gchar group[IRRECO_CONFIG_GROUP_LEN];
683 IrrecoConfigSaveHardkeyData hardkey_data;
684 IRRECO_ENTER
686 /* Save layout. */
687 g_assert(irreco_button_layout_get_name(layout) != NULL);
688 g_snprintf(group, IRRECO_CONFIG_GROUP_LEN, "layout-%i", *layout_index);
689 irreco_gkeyfile_set_string(keyfile, group, "name",
690 irreco_button_layout_get_name(layout));
691 g_key_file_set_integer(keyfile, group, "bg-type",
692 irreco_button_layout_get_bg_type(layout));
693 irreco_gkeyfile_set_string(keyfile, group, "bg-image",
694 irreco_button_layout_get_bg_image(layout));
695 color = irreco_button_layout_get_bg_color(layout);
696 g_key_file_set_integer(keyfile, group, "bg-color-red", color->red);
697 g_key_file_set_integer(keyfile, group, "bg-color-green", color->green);
698 g_key_file_set_integer(keyfile, group, "bg-color-blue", color->blue);
700 /* Save buttons. */
701 irreco_button_layout_reindex(layout);
702 IRRECO_PTR_ARRAY_FORWARDS(layout->button_array, IrrecoButton *, button)
703 irreco_config_save_button_to_keyfile(keyfile,
704 button,
705 button_index,
706 command_index);
707 *button_index = *button_index + 1;
708 IRRECO_PTR_ARRAY_FORWARDS_END
710 /* Save Hardkeys. */
711 hardkey_data.keyfile = keyfile;
712 hardkey_data.hardkey_index = hardkey_index;
713 hardkey_data.layout = layout;
714 irreco_hardkey_map_chain_foreach(layout->hardkey_map,
715 irreco_config_save_hardkey_to_file,
716 &hardkey_data);
717 IRRECO_RETURN
720 void irreco_config_save_hardkey_to_file(IrrecoHardkeyMap *hardkey_map,
721 guint keyval,
722 IrrecoCmdChainId chain_id,
723 IrrecoCmdChain *chain,
724 gpointer user_data)
726 gchar group[IRRECO_CONFIG_GROUP_LEN];
727 IrrecoConfigSaveHardkeyData *hardkey_data;
728 IRRECO_ENTER
730 hardkey_data = (IrrecoConfigSaveHardkeyData*) user_data;
732 g_snprintf(group, IRRECO_CONFIG_GROUP_LEN, "hardkey-%i",
733 *hardkey_data->hardkey_index);
734 irreco_gkeyfile_set_string(hardkey_data->keyfile, group, "layout",
735 irreco_button_layout_get_name(hardkey_data->layout));
736 g_key_file_set_integer(hardkey_data->keyfile, group,
737 "chain-id", chain_id);
738 irreco_gkeyfile_set_guint(hardkey_data->keyfile, group,
739 "key-value", keyval);
741 *hardkey_data->hardkey_index = *hardkey_data->hardkey_index + 1;
742 IRRECO_RETURN
745 void irreco_config_save_button_to_keyfile(GKeyFile * keyfile,
746 IrrecoButton * button,
747 gint * button_index,
748 gint * command_index)
750 gchar group[IRRECO_CONFIG_GROUP_LEN];
751 IRRECO_ENTER
753 g_snprintf(group, IRRECO_CONFIG_GROUP_LEN, "button-%i", *button_index);
754 irreco_gkeyfile_set_string(keyfile, group, "layout",
755 irreco_button_layout_get_name(button->irreco_layout));
756 irreco_gkeyfile_set_string(keyfile, group, "title", button->title);
757 g_key_file_set_integer(keyfile, group, "index", button->index);
758 g_key_file_set_integer(keyfile, group, "command-chain-id",
759 button->cmd_chain_id);
760 g_key_file_set_integer(keyfile, group, "x", button->x);
761 g_key_file_set_integer(keyfile, group, "y", button->y);
763 /* It is valid for a button to have NULL style. */
764 if (button->style != NULL) {
765 irreco_gkeyfile_set_string(keyfile, group, "style",
766 button->style->name);
769 IRRECO_RETURN
774 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
775 /* Save / Read active layout. */
776 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
778 gboolean irreco_config_save_active_layout(IrrecoButtonLayout * irreco_layout)
780 GKeyFile *keyfile;
781 gboolean success;
782 IRRECO_ENTER
784 keyfile = g_key_file_new();
785 if (irreco_layout != NULL) {
786 irreco_gkeyfile_set_string(keyfile, "active-layout", "name",
787 irreco_button_layout_get_name(irreco_layout));
789 success = irreco_gkeyfile_write_to_config_file(keyfile, "irreco",
790 irreco_config_active_layout_file);
791 g_key_file_free(keyfile);
792 IRRECO_RETURN_BOOL(success);
795 IrrecoButtonLayout *irreco_config_read_active_layout(IrrecoData * irreco_data)
797 IrrecoKeyFile *keyfile = NULL;
798 gchar *config_dir = NULL;
799 gchar *config_file = NULL;
800 gchar *name = NULL;
801 IrrecoButtonLayout *irreco_layout = NULL;
802 IRRECO_ENTER
804 /* Attempt to read layout name, and find layout with the same name. */
805 config_dir = irreco_get_config_dir("irreco");
806 config_file = irreco_get_config_file("irreco",
807 irreco_config_active_layout_file);
808 keyfile = irreco_keyfile_create(config_dir, config_file, NULL);
809 if (config_dir != NULL && config_file != NULL && keyfile != NULL
810 && irreco_keyfile_set_group(keyfile, "active-layout")
811 && irreco_keyfile_get_str(keyfile, "name", &name)) {
813 IRRECO_DEBUG("Read active layout name \"%s\" from \"%s\".\n",
814 name, irreco_config_active_layout_file);
816 if (irreco_string_table_get(irreco_data->irreco_layout_array,
817 name, (gpointer *) &irreco_layout)){
818 IRRECO_DEBUG("Found matching layout.\n");
819 } else {
820 IRRECO_DEBUG("No matching layout found.\n");
824 irreco_keyfile_destroy(keyfile);
825 g_free(name);
826 g_free(config_dir);
827 g_free(config_file);
829 /* If we could not find correct active layout,
830 attempt to get some layout.*/
831 if (irreco_layout == NULL) {
832 irreco_string_table_index(irreco_data->irreco_layout_array, 0,
833 NULL, (gpointer *) &irreco_layout);
835 IRRECO_RETURN_PTR(irreco_layout);
840 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
841 /* Save / Read backend configuration. */
842 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
844 gboolean irreco_config_save_backends(IrrecoBackendManager * backend_manager)
846 gint i = 0;
847 GKeyFile *keyfile;
848 gboolean success;
849 gchar group[IRRECO_CONFIG_GROUP_LEN];
850 IRRECO_ENTER
852 keyfile = g_key_file_new();
853 IRRECO_BACKEND_MANAGER_FOREACH_INSTANCE(backend_manager, instance)
854 g_snprintf(group, IRRECO_CONFIG_GROUP_LEN, "instance-%i", ++i);
855 irreco_gkeyfile_set_string(keyfile, group, "library",
856 instance->lib->filename);
857 irreco_gkeyfile_set_string(keyfile, group, "config",
858 instance->config);
859 irreco_gkeyfile_set_string(keyfile, group, "name",
860 instance->name);
861 IRRECO_BACKEND_MANAGER_FOREACH_END
863 success = irreco_gkeyfile_write_to_config_file(keyfile, "irreco",
864 irreco_config_backend_file);
865 g_key_file_free(keyfile);
866 IRRECO_RETURN_BOOL(success);
869 gboolean irreco_config_read_backends(IrrecoBackendManager * manager)
871 guint i;
872 IrrecoKeyFile *keyfile = NULL;
873 gchar *config_dir = NULL;
874 gchar *config_file = NULL;
875 gchar** groups = NULL;
876 gsize group_count = 0;
877 IRRECO_ENTER
879 config_dir = irreco_get_config_dir("irreco");
880 config_file = irreco_get_config_file("irreco",
881 irreco_config_backend_file);
882 keyfile = irreco_keyfile_create(config_dir, config_file, NULL);
883 if (config_dir == NULL || config_file == NULL || keyfile == NULL) {
884 g_free(config_dir);
885 g_free(config_file);
886 irreco_keyfile_destroy(keyfile);
887 IRRECO_RETURN_BOOL(FALSE);
890 IRRECO_PRINTF("Reading backend configuration file \"%s\"\n",
891 config_file);
892 groups = g_key_file_get_groups(keyfile->keyfile, &group_count);
894 /* Read instances. */
895 for (i = 0; i < group_count; i++) {
896 gchar *group = groups[i];
897 if (g_str_has_prefix(group, "instance")) {
898 IRRECO_PRINTF("Reading instance group \"%s\"\n", group);
899 irreco_keyfile_set_group(keyfile, group);
900 irreco_config_read_backend_from_keyfile(manager,
901 keyfile);
905 g_strfreev(groups);
906 irreco_keyfile_destroy(keyfile);
907 g_free(config_dir);
908 g_free(config_file);
909 IRRECO_RETURN_BOOL(TRUE);
912 void irreco_config_read_backend_from_keyfile(IrrecoBackendManager * manager,
913 IrrecoKeyFile * keyfile)
915 gchar *library = NULL;
916 gchar *config = NULL;
917 gchar *name = NULL;
918 IrrecoBackendLib *lib;
919 IRRECO_ENTER
921 /* Read variables. */
922 if (!irreco_keyfile_get_str(keyfile, "library", &library) ||
923 !irreco_keyfile_get_str(keyfile, "config", &config) ||
924 !irreco_keyfile_get_str(keyfile, "name", &name)) {
925 g_free(library);
926 g_free(config);
927 g_free(name);
928 IRRECO_RETURN
931 /* Find library. */
932 if (!irreco_backend_manager_find_lib(manager, library, &lib)) {
933 IRRECO_ERROR("Cannot find backend library \"%s\"\n", library);
934 g_free(library);
935 g_free(config);
936 g_free(name);
937 IRRECO_RETURN
939 g_free(library);
941 /* Create instance. */
942 irreco_backend_manager_create_instance(manager, lib, name, config);
943 g_free(name);
944 g_free(config);
946 IRRECO_RETURN