2 * irreco - Ir Remote Control
3 * Copyright (C) 2007 Arto Karppinen (arto.karppinen@iki.fi),
4 * Harri Vattulainen (t5vaha01@students.oamk.fi)
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 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
40 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
41 void irreco_config_read_style_from_keyfile(IrrecoData
* irreco_data
,
42 IrrecoKeyFile
*keyfile
,
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
,
57 gint
* hardkey_index
);
58 void irreco_config_save_hardkey_to_file(IrrecoHardkeyMap
*hardkey_map
,
60 IrrecoCmdChainId chain_id
,
61 IrrecoCmdChain
*chain
,
63 void irreco_config_save_button_to_keyfile(GKeyFile
* keyfile
,
64 IrrecoButton
* button
,
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 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
78 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
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
;
92 keyfile
= irreco_keyfile_create(dir_data
->directory
,
95 if (keyfile
!= NULL
) {
96 irreco_config_read_style_from_keyfile(irreco_data
, keyfile
,
98 irreco_keyfile_destroy(keyfile
);
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("");
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
);
132 * Reads configs from subdirectories ONLY
135 void irreco_config_read_styles_from_dir(IrrecoData * irreco_data,
138 IrrecoDirForeachData dir_data;
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);
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
,
160 IrrecoDirForeachData dir_data
;
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
);
172 void irreco_config_read_style_from_file(IrrecoData
* irreco_data
,
176 IrrecoKeyFile
*keyfile
;
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
,
184 irreco_keyfile_destroy(keyfile
);
191 gboolean
irreco_config_read_layouts(IrrecoData
* irreco_data
)
194 IrrecoKeyFile
*keyfile
= NULL
;
195 gchar
*config_dir
= NULL
;
196 gchar
*config_file
= NULL
;
197 gchar
** groups
= NULL
;
198 gsize group_count
= 0;
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
) {
208 irreco_keyfile_destroy(keyfile
);
209 IRRECO_RETURN_BOOL(FALSE
);
212 IRRECO_PRINTF("Reading layout configuration file \"%s\"\n",
214 groups
= g_key_file_get_groups(keyfile
->keyfile
, &group_count
);
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
,
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
,
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
,
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",
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
);
274 irreco_keyfile_destroy(keyfile
);
277 IRRECO_RETURN_BOOL(TRUE
);
280 void irreco_config_read_style_from_keyfile(IrrecoData
* irreco_data
,
281 IrrecoKeyFile
*keyfile
,
284 IrrecoButtonStyle
* style
;
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",
295 irreco_button_style_destroy(style
);
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
,
317 IRRECO_ERROR("Dublicate style name \"%s\"\n", style
->name
);
318 irreco_button_style_destroy(style
);
324 void irreco_config_read_layout_from_keyfile(IrrecoData
* irreco_data
,
325 IrrecoKeyFile
* keyfile
)
328 IrrecoButtonLayout
*layout
= NULL
;
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
);
342 if (irreco_keyfile_get_int(keyfile
, "bg-type", &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
);
351 IRRECO_ERROR("Unknown background type \"%i\".\n",
357 if (irreco_keyfile_get_str(keyfile
, "bg-image", &image
)) {
358 irreco_button_layout_set_bg_image(layout
, image
);
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
),
373 IRRECO_ERROR("Layout \"%s\" already exists.",
374 irreco_button_layout_get_name(layout
));
375 irreco_button_layout_destroy(layout
);
381 void irreco_config_read_button_from_keyfile(IrrecoData
* irreco_data
,
382 IrrecoKeyFile
* keyfile
)
384 gchar
*layout_name
= NULL
;
385 gchar
*style_name
= NULL
;
387 gchar
*command
= NULL
;
388 IrrecoCmdChainId chain_id
;
392 IrrecoButton
*button
= NULL
;
393 IrrecoButtonLayout
*layout
= NULL
;
394 IrrecoButtonStyle
*style
= NULL
;
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
)) {
403 IRRECO_PRINTF("Position: x%i y%i\n", x
, y
);
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
);
413 IRRECO_PRINTF("Style: \"%s\"\n",
414 irreco_button_layout_get_name(layout
));
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
);
424 IRRECO_ERROR("Button uses unknown style \"%s\"\n",
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
);
446 void irreco_config_read_hardkey_from_keyfile(IrrecoData
* irreco_data
,
447 IrrecoKeyFile
* keyfile
)
449 IrrecoButtonLayout
*layout
= NULL
;
450 gchar
*layout_name
= NULL
;
453 gchar
*hardkey_str
= NULL
;
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
)) {
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",
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
),
481 irreco_hardkey_map_assosiate_chain_with_id(layout
->hardkey_map
,
487 * Support LEGACY configuration.
489 * This still exists in order to read layout configs from old versions of
492 * TODO: Remove sometime next year.
494 void irreco_config_read_command_from_keyfile(IrrecoData
* irreco_data
,
495 IrrecoKeyFile
* keyfile
)
499 gchar
*layout_name
= NULL
;
500 gchar
*type_name
= NULL
;
503 IrrecoButton
*button
;
504 IrrecoButtonLayout
*layout
;
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
)) {
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
);
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
);
534 if (button_index
>= layout
->button_array
->len
) {
535 IRRECO_ERROR("Invalid button index \"%i\"\n", button_index
);
538 button
= g_ptr_array_index(layout
->button_array
, button_index
);
540 /* Append command to button command chain. */
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
),
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",
560 !irreco_keyfile_get_str(keyfile
, "device-name",
562 !irreco_keyfile_get_str(keyfile
, "command-name",
564 g_free(instance_name
);
566 g_free(command_name
);
570 if (!irreco_backend_manager_find_instance(
571 irreco_data
->irreco_backend_manager
,
573 &backend_instance
)) {
574 IRRECO_ERROR("Could not find instance with name "
575 "\"%s\"\n", instance_name
);
578 command
= irreco_cmd_create();
579 irreco_cmd_set_backend(command
,
583 irreco_cmd_chain_append(irreco_button_get_cmd_chain(button
),
586 g_free(instance_name
);
588 g_free(command_name
);
591 case IRRECO_COMMAND_SHOW_LAYOUT
: {
593 if (!irreco_keyfile_get_str(keyfile
, "show-layout",
598 command
= irreco_cmd_create();
599 irreco_cmd_set_layout(command
, show_layout
);
600 irreco_cmd_chain_append(irreco_button_get_cmd_chain(button
),
605 case IRRECO_COMMAND_WAIT
: {
607 if (!irreco_keyfile_get_uint(keyfile
, "delay", &delay
)) {
611 command
= irreco_cmd_create();
612 irreco_cmd_set_wait(command
, delay
);
613 irreco_cmd_chain_append(irreco_button_get_cmd_chain(button
),
624 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
626 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
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
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;
643 IrrecoStringTable
*table
;
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
,
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
{
671 IrrecoButtonLayout
*layout
;
674 void irreco_config_save_layout_to_keyfile(GKeyFile
* keyfile
,
675 IrrecoButtonLayout
* layout
,
678 gint
* command_index
,
679 gint
* hardkey_index
)
682 gchar group
[IRRECO_CONFIG_GROUP_LEN
];
683 IrrecoConfigSaveHardkeyData hardkey_data
;
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
);
701 irreco_button_layout_reindex(layout
);
702 IRRECO_PTR_ARRAY_FORWARDS(layout
->button_array
, IrrecoButton
*, button
)
703 irreco_config_save_button_to_keyfile(keyfile
,
707 *button_index
= *button_index
+ 1;
708 IRRECO_PTR_ARRAY_FORWARDS_END
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
,
720 void irreco_config_save_hardkey_to_file(IrrecoHardkeyMap
*hardkey_map
,
722 IrrecoCmdChainId chain_id
,
723 IrrecoCmdChain
*chain
,
726 gchar group
[IRRECO_CONFIG_GROUP_LEN
];
727 IrrecoConfigSaveHardkeyData
*hardkey_data
;
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;
745 void irreco_config_save_button_to_keyfile(GKeyFile
* keyfile
,
746 IrrecoButton
* button
,
748 gint
* command_index
)
750 gchar group
[IRRECO_CONFIG_GROUP_LEN
];
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
);
774 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
775 /* Save / Read active layout. */
776 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
778 gboolean
irreco_config_save_active_layout(IrrecoButtonLayout
* irreco_layout
)
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
;
801 IrrecoButtonLayout
*irreco_layout
= NULL
;
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");
820 IRRECO_DEBUG("No matching layout found.\n");
824 irreco_keyfile_destroy(keyfile
);
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
)
849 gchar group
[IRRECO_CONFIG_GROUP_LEN
];
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",
859 irreco_gkeyfile_set_string(keyfile
, group
, "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
)
872 IrrecoKeyFile
*keyfile
= NULL
;
873 gchar
*config_dir
= NULL
;
874 gchar
*config_file
= NULL
;
875 gchar
** groups
= NULL
;
876 gsize group_count
= 0;
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
) {
886 irreco_keyfile_destroy(keyfile
);
887 IRRECO_RETURN_BOOL(FALSE
);
890 IRRECO_PRINTF("Reading backend configuration file \"%s\"\n",
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
,
906 irreco_keyfile_destroy(keyfile
);
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
;
918 IrrecoBackendLib
*lib
;
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
)) {
932 if (!irreco_backend_manager_find_lib(manager
, library
, &lib
)) {
933 IRRECO_ERROR("Cannot find backend library \"%s\"\n", library
);
941 /* Create instance. */
942 irreco_backend_manager_create_instance(manager
, lib
, name
, config
);