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"
23 #include "irreco_theme_button.h"
26 * @addtogroup IrrecoConfig
29 * Configuration saving and reading.
36 * Source file of @ref IrrecoConfig.
40 #define IRRECO_CONFIG_GROUP_LEN 20
42 gchar
* irreco_config_layout_file
= "layouts.conf";
43 gchar
* irreco_config_active_layout_file
= "active-layout.conf";
44 gchar
* irreco_config_backend_file
= "backends.conf";
45 gchar
* irreco_config_cmd_chain_file
= "command-chains.conf";
49 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
51 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
53 void irreco_config_read_button_from_keyfile(IrrecoData
* irreco_data
,
54 IrrecoKeyFile
* keyfile
);
55 void irreco_config_read_hardkey_from_keyfile(IrrecoData
* irreco_data
,
56 IrrecoKeyFile
* keyfile
);
57 void irreco_config_read_layout_from_keyfile(IrrecoData
* irreco_data
,
58 IrrecoKeyFile
* keyfile
);
59 void irreco_config_save_layout_to_keyfile(GKeyFile
* keyfile
,
60 IrrecoButtonLayout
* layout
,
64 gint
* hardkey_index
);
65 void irreco_config_save_hardkey_to_file(IrrecoHardkeyMap
*hardkey_map
,
67 IrrecoCmdChainId chain_id
,
68 IrrecoCmdChain
*chain
,
70 void irreco_config_save_button_to_keyfile(GKeyFile
* keyfile
,
71 IrrecoButton
* button
,
73 gint
* command_index
);
74 void irreco_config_save_cmd_chain_to_keyfile(GKeyFile
* keyfile
,
75 IrrecoButton
* button
,
76 gint
* command_index
);
77 void irreco_config_read_backend_from_keyfile(
78 IrrecoBackendManager
* backend_manager
,
79 IrrecoKeyFile
* keyfile
);
83 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
85 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
88 * @name Layout reading
92 gboolean
irreco_config_read_layouts(IrrecoData
* irreco_data
)
95 IrrecoKeyFile
*keyfile
= NULL
;
96 gchar
*config_dir
= NULL
;
97 gchar
*config_file
= NULL
;
98 gchar
** groups
= NULL
;
99 gsize group_count
= 0;
102 config_dir
= irreco_get_config_dir("irreco");
103 config_file
= irreco_get_config_file("irreco",
104 irreco_config_layout_file
);
105 keyfile
= irreco_keyfile_create(config_dir
, config_file
, NULL
);
106 if (config_dir
== NULL
|| config_file
== NULL
|| keyfile
== NULL
) {
109 irreco_keyfile_destroy(keyfile
);
110 IRRECO_RETURN_BOOL(FALSE
);
113 IRRECO_PRINTF("Reading layout configuration file \"%s\"\n",
115 groups
= g_key_file_get_groups(keyfile
->keyfile
, &group_count
);
118 for (i
= 0; i
< group_count
; i
++) {
119 gchar
*group
= groups
[i
];
120 if (g_str_has_prefix(group
, "layout")) {
121 IRRECO_PRINTF("Reading layout group \"%s\"\n", group
);
122 irreco_keyfile_set_group(keyfile
, group
);
123 irreco_config_read_layout_from_keyfile(irreco_data
,
129 for (i
= 0; i
< group_count
; i
++) {
130 gchar
*group
= groups
[i
];
131 if (g_str_has_prefix(group
, "button")) {
132 IRRECO_PRINTF("Reading button group \"%s\"\n", group
);
133 irreco_keyfile_set_group(keyfile
, group
);
134 irreco_config_read_button_from_keyfile(irreco_data
,
140 for (i
= 0; i
< group_count
; i
++) {
141 gchar
*group
= groups
[i
];
142 if (g_str_has_prefix(group
, "hardkey")) {
143 IRRECO_PRINTF("Reading hardkey group \"%s\"\n", group
);
144 irreco_keyfile_set_group(keyfile
, group
);
145 irreco_config_read_hardkey_from_keyfile(irreco_data
,
150 /* Read command chains. */
151 irreco_cmd_chain_manager_from_config(irreco_data
);
154 irreco_keyfile_destroy(keyfile
);
157 IRRECO_RETURN_BOOL(TRUE
);
160 void irreco_config_read_layout_from_keyfile(IrrecoData
* irreco_data
,
161 IrrecoKeyFile
* keyfile
)
164 IrrecoButtonLayout
*layout
= NULL
;
170 /* Required fields. */
171 if (!irreco_keyfile_get_str(keyfile
, "name", &name
)) IRRECO_RETURN
172 layout
= irreco_button_layout_create(NULL
,
173 irreco_data
->cmd_chain_manager
);
174 irreco_button_layout_set_name(layout
, name
);
178 if (irreco_keyfile_get_int(keyfile
, "bg-type", &type
)) {
180 case IRRECO_BACKGROUND_DEFAULT
:
181 case IRRECO_BACKGROUND_COLOR
:
182 case IRRECO_BACKGROUND_IMAGE
:
183 irreco_button_layout_set_bg_type(layout
, type
);
187 IRRECO_ERROR("Unknown background type \"%i\".\n",
193 if (irreco_keyfile_get_str(keyfile
, "bg-image", &image
)) {
194 irreco_button_layout_set_bg_image(layout
, image
);
200 if (irreco_keyfile_get_uint16(keyfile
, "bg-color-red", &color
.red
) &&
201 irreco_keyfile_get_uint16(keyfile
, "bg-color-green", &color
.green
)&&
202 irreco_keyfile_get_uint16(keyfile
, "bg-color-blue", &color
.blue
)) {
203 irreco_button_layout_set_bg_color(layout
, &color
);
206 if (!irreco_string_table_add(irreco_data
->irreco_layout_array
,
207 irreco_button_layout_get_name(layout
),
209 IRRECO_ERROR("Layout \"%s\" already exists.",
210 irreco_button_layout_get_name(layout
));
211 irreco_button_layout_destroy(layout
);
217 void irreco_config_read_button_from_keyfile(IrrecoData
* irreco_data
,
218 IrrecoKeyFile
* keyfile
)
220 gchar
*layout_name
= NULL
;
221 gchar
*style_name
= NULL
;
223 gchar
*command
= NULL
;
224 IrrecoCmdChainId chain_id
;
228 IrrecoButton
*button
= NULL
;
229 IrrecoButtonLayout
*layout
= NULL
;
230 IrrecoThemeButton
*style
= NULL
;
233 /* Required fields. */
234 if (!irreco_keyfile_get_int(keyfile
, "x", &x
) ||
235 !irreco_keyfile_get_int(keyfile
, "y", &y
) ||
236 !irreco_keyfile_get_str(keyfile
, "layout", &layout_name
)) {
239 IRRECO_PRINTF("Position: x%i y%i\n", x
, y
);
242 if (!irreco_string_table_get(irreco_data
->irreco_layout_array
,
243 layout_name
, (gpointer
*) &layout
)) {
244 IRRECO_ERROR("Button uses unknown layout \"%s\"\n",
250 IRRECO_PRINTF("Style: \"%s\"\n",
251 irreco_button_layout_get_name(layout
));
254 irreco_keyfile_get_str(keyfile
, "style", &style_name
);
255 if (style_name
!= NULL
) {
256 if (irreco_theme_manager_get_button_style(
257 irreco_data
->theme_manager
,
258 style_name
, &style
)) {
260 IRRECO_PRINTF("Style: \"%s\"\n", style
->name
->str
);
262 IRRECO_ERROR("Button uses unknown style \"%s\"\n",
269 /* Get title and command. */
270 irreco_keyfile_get_str(keyfile
, "title", &title
);
271 IRRECO_PRINTF("title: \"%s\"\n", title
);
273 button
= irreco_button_create(layout
, x
, y
, title
, style
,
274 irreco_data
->cmd_chain_manager
);
276 if (irreco_keyfile_get_int(keyfile
, "command-chain-id", &chain_id
)) {
277 irreco_button_set_cmd_chain_id(button
, chain_id
);
285 void irreco_config_read_hardkey_from_keyfile(IrrecoData
* irreco_data
,
286 IrrecoKeyFile
* keyfile
)
288 IrrecoButtonLayout
*layout
= NULL
;
289 gchar
*layout_name
= NULL
;
292 gchar
*hardkey_str
= NULL
;
295 /* Required fields. */
296 if (!irreco_keyfile_get_str(keyfile
, "layout", &layout_name
) ||
297 !irreco_keyfile_get_int(keyfile
, "chain-id", &id
) ||
298 !irreco_keyfile_get_uint(keyfile
, "key-value", &keyval
)) {
304 if (!irreco_string_table_get(irreco_data
->irreco_layout_array
,
305 layout_name
, (gpointer
*) &layout
)) {
306 IRRECO_ERROR("Hardkey uses unknown layout \"%s\"\n",
313 hardkey_str
= irreco_hardkey_to_str(keyval
);
314 IRRECO_PRINTF("Associating layout \"%s\" hardkey \"%s\" with "
315 "command chain \"%i\".\n",
316 irreco_button_layout_get_name(layout
),
320 irreco_hardkey_map_assosiate_chain_with_id(layout
->hardkey_map
,
329 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
331 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
334 * @name Layout saving
341 * Since GKeyFile does not allow two groups with the same name,
342 * layout_index and button_index variables are used to make sure that each
343 * button and layout group has unique name. The index is ignored when reading
346 gboolean
irreco_config_save_layouts(IrrecoData
* irreco_data
)
348 gint layout_index
= 1;
349 gint button_index
= 1;
350 gint command_index
= 1;
351 gint hardkey_index
= 1;
353 IrrecoStringTable
*table
;
357 irreco_cmd_chain_manager_to_config(irreco_data
->cmd_chain_manager
);
359 keyfile
= g_key_file_new();
360 table
= irreco_data
->irreco_layout_array
;
361 IRRECO_STRING_TABLE_FOREACH(table
, key
, IrrecoButtonLayout
*, layout
)
362 irreco_config_save_layout_to_keyfile(keyfile
,
369 IRRECO_STRING_TABLE_FOREACH_END
371 success
= irreco_gkeyfile_write_to_config_file(keyfile
, "irreco",
372 irreco_config_layout_file
);
373 g_key_file_free(keyfile
);
374 IRRECO_RETURN_BOOL(success
);
377 typedef struct _IrrecoConfigSaveHardkeyData IrrecoConfigSaveHardkeyData
;
378 struct _IrrecoConfigSaveHardkeyData
{
381 IrrecoButtonLayout
*layout
;
384 void irreco_config_save_layout_to_keyfile(GKeyFile
* keyfile
,
385 IrrecoButtonLayout
* layout
,
388 gint
* command_index
,
389 gint
* hardkey_index
)
392 gchar group
[IRRECO_CONFIG_GROUP_LEN
];
393 IrrecoConfigSaveHardkeyData hardkey_data
;
397 g_assert(irreco_button_layout_get_name(layout
) != NULL
);
398 g_snprintf(group
, IRRECO_CONFIG_GROUP_LEN
, "layout-%i", *layout_index
);
399 irreco_gkeyfile_set_string(keyfile
, group
, "name",
400 irreco_button_layout_get_name(layout
));
401 g_key_file_set_integer(keyfile
, group
, "bg-type",
402 irreco_button_layout_get_bg_type(layout
));
403 irreco_gkeyfile_set_string(keyfile
, group
, "bg-image",
404 irreco_button_layout_get_bg_image(layout
));
405 color
= irreco_button_layout_get_bg_color(layout
);
406 g_key_file_set_integer(keyfile
, group
, "bg-color-red", color
->red
);
407 g_key_file_set_integer(keyfile
, group
, "bg-color-green", color
->green
);
408 g_key_file_set_integer(keyfile
, group
, "bg-color-blue", color
->blue
);
411 irreco_button_layout_reindex(layout
);
412 IRRECO_PTR_ARRAY_FORWARDS(layout
->button_array
, IrrecoButton
*, button
)
413 irreco_config_save_button_to_keyfile(keyfile
,
417 *button_index
= *button_index
+ 1;
418 IRRECO_PTR_ARRAY_FORWARDS_END
421 hardkey_data
.keyfile
= keyfile
;
422 hardkey_data
.hardkey_index
= hardkey_index
;
423 hardkey_data
.layout
= layout
;
424 irreco_hardkey_map_chain_foreach(layout
->hardkey_map
,
425 irreco_config_save_hardkey_to_file
,
430 void irreco_config_save_hardkey_to_file(IrrecoHardkeyMap
*hardkey_map
,
432 IrrecoCmdChainId chain_id
,
433 IrrecoCmdChain
*chain
,
436 gchar group
[IRRECO_CONFIG_GROUP_LEN
];
437 IrrecoConfigSaveHardkeyData
*hardkey_data
;
440 hardkey_data
= (IrrecoConfigSaveHardkeyData
*) user_data
;
442 g_snprintf(group
, IRRECO_CONFIG_GROUP_LEN
, "hardkey-%i",
443 *hardkey_data
->hardkey_index
);
444 irreco_gkeyfile_set_string(hardkey_data
->keyfile
, group
, "layout",
445 irreco_button_layout_get_name(hardkey_data
->layout
));
446 g_key_file_set_integer(hardkey_data
->keyfile
, group
,
447 "chain-id", chain_id
);
448 irreco_gkeyfile_set_guint(hardkey_data
->keyfile
, group
,
449 "key-value", keyval
);
451 *hardkey_data
->hardkey_index
= *hardkey_data
->hardkey_index
+ 1;
455 void irreco_config_save_button_to_keyfile(GKeyFile
* keyfile
,
456 IrrecoButton
* button
,
458 gint
* command_index
)
460 gchar group
[IRRECO_CONFIG_GROUP_LEN
];
463 g_snprintf(group
, IRRECO_CONFIG_GROUP_LEN
, "button-%i", *button_index
);
464 irreco_gkeyfile_set_string(keyfile
, group
, "layout",
465 irreco_button_layout_get_name(button
->irreco_layout
));
466 irreco_gkeyfile_set_string(keyfile
, group
, "title", button
->title
);
467 g_key_file_set_integer(keyfile
, group
, "index", button
->index
);
468 g_key_file_set_integer(keyfile
, group
, "command-chain-id",
469 button
->cmd_chain_id
);
470 g_key_file_set_integer(keyfile
, group
, "x", button
->x
);
471 g_key_file_set_integer(keyfile
, group
, "y", button
->y
);
473 /* It is valid for a button to have NULL style. */
474 if (button
->style
!= NULL
) {
475 irreco_gkeyfile_set_string(keyfile
, group
, "style",
476 button
->style
->style_name
->str
);
486 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
487 /* Save / Read active layout. */
488 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
491 * @name Save / Read active layout
495 gboolean
irreco_config_save_active_layout(IrrecoButtonLayout
* irreco_layout
)
501 keyfile
= g_key_file_new();
502 if (irreco_layout
!= NULL
) {
503 irreco_gkeyfile_set_string(keyfile
, "active-layout", "name",
504 irreco_button_layout_get_name(irreco_layout
));
506 success
= irreco_gkeyfile_write_to_config_file(keyfile
, "irreco",
507 irreco_config_active_layout_file
);
508 g_key_file_free(keyfile
);
509 IRRECO_RETURN_BOOL(success
);
512 IrrecoButtonLayout
*irreco_config_read_active_layout(IrrecoData
* irreco_data
)
514 IrrecoKeyFile
*keyfile
= NULL
;
515 gchar
*config_dir
= NULL
;
516 gchar
*config_file
= NULL
;
518 IrrecoButtonLayout
*irreco_layout
= NULL
;
521 /* Attempt to read layout name, and find layout with the same name. */
522 config_dir
= irreco_get_config_dir("irreco");
523 config_file
= irreco_get_config_file("irreco",
524 irreco_config_active_layout_file
);
525 keyfile
= irreco_keyfile_create(config_dir
, config_file
, NULL
);
526 if (config_dir
!= NULL
&& config_file
!= NULL
&& keyfile
!= NULL
527 && irreco_keyfile_set_group(keyfile
, "active-layout")
528 && irreco_keyfile_get_str(keyfile
, "name", &name
)) {
530 IRRECO_DEBUG("Read active layout name \"%s\" from \"%s\".\n",
531 name
, irreco_config_active_layout_file
);
533 if (irreco_string_table_get(irreco_data
->irreco_layout_array
,
534 name
, (gpointer
*) &irreco_layout
)){
535 IRRECO_DEBUG("Found matching layout.\n");
537 IRRECO_DEBUG("No matching layout found.\n");
541 irreco_keyfile_destroy(keyfile
);
546 /* If we could not find correct active layout,
547 attempt to get some layout.*/
548 if (irreco_layout
== NULL
) {
549 irreco_string_table_index(irreco_data
->irreco_layout_array
, 0,
550 NULL
, (gpointer
*) &irreco_layout
);
552 IRRECO_RETURN_PTR(irreco_layout
);
559 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
560 /* Save / Read backend configuration. */
561 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
564 * @name Save / Read backend configuration
568 gboolean
irreco_config_save_backends(IrrecoBackendManager
* backend_manager
)
573 gchar group
[IRRECO_CONFIG_GROUP_LEN
];
576 keyfile
= g_key_file_new();
577 IRRECO_BACKEND_MANAGER_FOREACH_INSTANCE(backend_manager
, instance
)
578 g_snprintf(group
, IRRECO_CONFIG_GROUP_LEN
, "instance-%i", ++i
);
579 irreco_gkeyfile_set_string(keyfile
, group
, "library",
580 instance
->lib
->filename
);
581 irreco_gkeyfile_set_string(keyfile
, group
, "config",
583 irreco_gkeyfile_set_string(keyfile
, group
, "name",
584 irreco_backend_instance_get_name(instance
));
585 IRRECO_BACKEND_MANAGER_FOREACH_END
587 success
= irreco_gkeyfile_write_to_config_file(keyfile
, "irreco",
588 irreco_config_backend_file
);
589 g_key_file_free(keyfile
);
590 IRRECO_RETURN_BOOL(success
);
593 gboolean
irreco_config_read_backends(IrrecoBackendManager
* manager
)
596 IrrecoKeyFile
*keyfile
= NULL
;
597 gchar
*config_dir
= NULL
;
598 gchar
*config_file
= NULL
;
599 gchar
** groups
= NULL
;
600 gsize group_count
= 0;
603 config_dir
= irreco_get_config_dir("irreco");
604 config_file
= irreco_get_config_file("irreco",
605 irreco_config_backend_file
);
606 keyfile
= irreco_keyfile_create(config_dir
, config_file
, NULL
);
607 if (config_dir
== NULL
|| config_file
== NULL
|| keyfile
== NULL
) {
610 irreco_keyfile_destroy(keyfile
);
611 IRRECO_RETURN_BOOL(FALSE
);
614 IRRECO_PRINTF("Reading backend configuration file \"%s\"\n",
616 groups
= g_key_file_get_groups(keyfile
->keyfile
, &group_count
);
618 /* Read instances. */
619 for (i
= 0; i
< group_count
; i
++) {
620 gchar
*group
= groups
[i
];
621 if (g_str_has_prefix(group
, "instance")) {
622 IRRECO_PRINTF("Reading instance group \"%s\"\n", group
);
623 irreco_keyfile_set_group(keyfile
, group
);
624 irreco_config_read_backend_from_keyfile(manager
,
630 irreco_keyfile_destroy(keyfile
);
633 IRRECO_RETURN_BOOL(TRUE
);
636 void irreco_config_read_backend_from_keyfile(IrrecoBackendManager
* manager
,
637 IrrecoKeyFile
* keyfile
)
639 gchar
*library
= NULL
;
640 gchar
*config
= NULL
;
642 IrrecoBackendLib
*lib
;
645 /* Read variables. */
646 if (!irreco_keyfile_get_str(keyfile
, "library", &library
) ||
647 !irreco_keyfile_get_str(keyfile
, "config", &config
) ||
648 !irreco_keyfile_get_str(keyfile
, "name", &name
)) {
656 if (!irreco_backend_manager_find_lib(manager
, library
, &lib
)) {
657 IRRECO_ERROR("Cannot find backend library \"%s\"\n", library
);
665 /* Create instance. */
666 irreco_backend_manager_create_instance(manager
, lib
, name
, config
);