2 * irreco - Ir Remote Control
3 * Copyright (C) 2008 Joni Kokko (t5kojo01@students.oamk.fi)
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 #include "irreco_theme.h"
23 * @addtogroup IrrecoTheme
26 * Contains information of theme.
33 * Source file of @ref IrrecoTheme.
36 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
38 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
39 void irreco_theme_read_button_keyfile_foreach(IrrecoDirForeachData
* dir_data
);
40 void irreco_theme_read_bg_keyfile_foreach(IrrecoDirForeachData
* dir_data
);
41 void irreco_theme_read(IrrecoTheme
*self
, const gchar
*dir
);
43 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
44 /* Construction & Destruction */
45 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
48 * @name Construction & Destruction
55 IrrecoTheme
*theme_new()
60 self
= g_slice_new0(IrrecoTheme
);
62 self
->name
= g_string_new(NULL
);
63 self
->path
= g_string_new(NULL
);
64 self
->source
= g_string_new(NULL
);
65 self
->author
= g_string_new(NULL
);
66 self
->comment
= g_string_new(NULL
);
67 self
->preview_button_name
= g_string_new(NULL
);
68 self
->version
= g_string_new(NULL
);
69 self
->backgrounds
= irreco_string_table_new(
70 (GDestroyNotify
)irreco_theme_bg_free
, NULL
);
71 self
->buttons
= irreco_string_table_new(
72 (GDestroyNotify
)irreco_theme_button_free
, NULL
);
75 IRRECO_RETURN_PTR(self
);
80 IrrecoTheme
*irreco_theme_new(const char *name
, const char *path
,
81 const char *source
, const char *author
,
83 const char *preview_button_name
,
89 self
= g_slice_new0(IrrecoTheme
);
91 self
->name
= g_string_new(name
);
92 self
->path
= g_string_new(path
);
93 self
->source
= g_string_new(source
);
94 self
->author
= g_string_new(author
);
95 self
->comment
= g_string_new(comment
);
96 self
->preview_button_name
= g_string_new(preview_button_name
);
97 self
->version
= g_string_new(version
);
98 self
->backgrounds
= irreco_string_table_new(
99 (GDestroyNotify
)irreco_theme_bg_free
, NULL
);
100 self
->buttons
= irreco_string_table_new(
101 (GDestroyNotify
)irreco_theme_button_free
, NULL
);
103 IRRECO_DEBUG("Themepath: %s\n", path
);
107 IrrecoDirForeachData button_styles
;
108 GString
* directory
= g_string_new("");
110 g_string_printf(directory
, "%s/buttons/", path
);
111 IRRECO_DEBUG("Directory = %s\n", directory
->str
);
112 button_styles
.directory
= directory
->str
;
114 button_styles
.filesuffix
= "button.conf";
115 button_styles
.user_data_1
= self
;
117 irreco_dir_foreach_subdirectories(&button_styles
,
118 irreco_theme_read_button_keyfile_foreach
);
120 g_string_free(directory
, TRUE
);
123 irreco_string_table_sort_abc(self
->buttons
);
128 IrrecoDirForeachData bg_styles
;
129 GString
* directory
= g_string_new("");
131 g_string_printf(directory
, "%s/bg/", path
);
132 IRRECO_DEBUG("Directory = %s\n", directory
->str
);
133 bg_styles
.directory
= directory
->str
;
135 bg_styles
.filesuffix
= "bg.conf";
136 bg_styles
.user_data_1
= self
;
138 irreco_dir_foreach_subdirectories(&bg_styles
,
139 irreco_theme_read_bg_keyfile_foreach
);
141 g_string_free(directory
, TRUE
);
144 irreco_string_table_sort_abc(self
->backgrounds
);
147 IRRECO_RETURN_PTR(self
);
150 void irreco_theme_free(IrrecoTheme
*self
)
154 g_assert(self
!= NULL
);
156 g_string_free(self
->name
, TRUE
);
159 g_string_free(self
->path
, TRUE
);
162 g_string_free(self
->source
, TRUE
);
165 g_string_free(self
->author
, TRUE
);
168 g_string_free(self
->comment
, TRUE
);
169 self
->comment
= NULL
;
171 g_string_free(self
->preview_button_name
, TRUE
);
172 self
->preview_button_name
= NULL
;
174 irreco_string_table_free(self
->backgrounds
);
175 self
->backgrounds
= NULL
;
177 irreco_string_table_free(self
->buttons
);
178 self
->buttons
= NULL
;
180 g_slice_free(IrrecoTheme
, self
);
188 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
189 /* Private Functions */
190 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
193 * @name Private Functions
200 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
201 /* Public Functions */
202 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
205 * @name Public Functions
208 void irreco_theme_update_keyfile(IrrecoTheme
*self
)
210 GString
*keyfile_path
= g_string_new(self
->path
->str
);
211 GKeyFile
*keyfile
= g_key_file_new();
214 g_string_append(keyfile_path
, "/theme.conf");
216 irreco_gkeyfile_set_string(keyfile
, "theme" , "name",
219 if (self
->source
->len
> 0) {
220 irreco_gkeyfile_set_string(keyfile
, "theme" , "source",
224 if (self
->author
->len
> 0) {
225 irreco_gkeyfile_set_string(keyfile
, "theme", "author",
229 if (self
->preview_button_name
->len
> 0) {
230 irreco_gkeyfile_set_string(keyfile
, "theme", "preview-button",
231 self
->preview_button_name
->str
);
234 if (self
->version
->len
> 0) {
235 irreco_gkeyfile_set_string(keyfile
, "theme", "version",
239 if (self
->comment
->len
> 0) {
240 irreco_gkeyfile_set_string(keyfile
, "theme", "comment",
244 irreco_write_keyfile(keyfile
, keyfile_path
->str
);
246 g_key_file_free(keyfile
);
247 g_string_free(keyfile_path
, TRUE
);
251 void irreco_theme_print(IrrecoTheme
*self
)
255 IRRECO_DEBUG("Themename: %s \n", self
->name
->str
);
256 IRRECO_DEBUG("Folder: %s \n", self
->path
->str
);
257 IRRECO_DEBUG("Source: %s \n", self
->source
->str
);
258 IRRECO_DEBUG("Author: %s \n", self
->author
->str
);
259 IRRECO_DEBUG("Comment: %s \n", self
->comment
->str
);
260 IRRECO_DEBUG("Previewbutton: %s \n", self
->preview_button_name
->str
);
261 IRRECO_DEBUG("Version: %s \n", self
->version
->str
);
262 irreco_string_table_print(self
->backgrounds
);
263 irreco_string_table_print(self
->buttons
);
268 void irreco_theme_read_button_keyfile_foreach(IrrecoDirForeachData
* dir_data
)
270 IrrecoTheme
*self
= (IrrecoTheme
*) dir_data
->user_data_1
;
271 IrrecoKeyFile
*keyfile
;
276 gchar
*text_format_up
= NULL
;
277 gchar
*text_format_down
= NULL
;
278 gint text_padding
= 5;
279 gfloat text_h_align
= 0.5;
280 gfloat text_v_align
= 0.5;
281 IrrecoThemeButton
*theme_button
;
282 GString
*style_name
= g_string_new(self
->name
->str
);
286 keyfile
= irreco_keyfile_create(dir_data
->directory
,
289 if (keyfile
== NULL
) goto end
;
291 /* Required fields. */
292 if (!irreco_keyfile_get_str(keyfile
, "name", &name
) ||
293 !irreco_keyfile_get_path(keyfile
, "up", &up
)) {
294 IRRECO_PRINTF("Could not read style from group \"%s\"\n",
299 /* Optional fields. */
300 irreco_keyfile_get_path(keyfile
, "down", &down
);
301 irreco_keyfile_get_bool(keyfile
, "allow-text", &allow_text
);
302 irreco_keyfile_get_str(keyfile
, "text-format-up", &text_format_up
);
303 irreco_keyfile_get_str(keyfile
, "text-format-down", &text_format_down
);
304 irreco_keyfile_get_int(keyfile
, "text-padding", &text_padding
);
305 irreco_keyfile_get_float(keyfile
, "text-h-align", &text_h_align
);
306 irreco_keyfile_get_float(keyfile
, "text-v-align", &text_v_align
);
308 g_string_append_printf(style_name
,"/%s", name
);
310 if (irreco_string_table_get(self
->buttons
, style_name
->str
,
311 (gpointer
*) &theme_button
)) {
312 irreco_theme_button_set(theme_button
, style_name
->str
,
315 text_format_up
, text_format_down
,
320 theme_button
= irreco_theme_button_new(self
->name
->str
);
321 irreco_theme_button_set(theme_button
, style_name
->str
,
324 text_format_up
, text_format_down
,
328 irreco_string_table_add(self
->buttons
,
329 theme_button
->style_name
->str
,
333 irreco_theme_button_print(theme_button
);
336 if(keyfile
!= NULL
) irreco_keyfile_destroy(keyfile
);
337 if(name
!= NULL
) g_free(name
);
338 if(name
!= NULL
) g_free(up
);
339 if(name
!= NULL
) g_free(down
);
340 if(name
!= NULL
) g_free(text_format_up
);
341 if(name
!= NULL
) g_free(text_format_down
);
342 g_string_free(style_name
, TRUE
);
348 void irreco_theme_read_bg_keyfile_foreach(IrrecoDirForeachData
* dir_data
)
350 IrrecoTheme
*self
= (IrrecoTheme
*) dir_data
->user_data_1
;
351 IrrecoKeyFile
*keyfile
;
357 keyfile
= irreco_keyfile_create(dir_data
->directory
,
360 if (keyfile
== NULL
) goto end
;
362 /* Required fields. */
363 if (!irreco_keyfile_get_str(keyfile
, "name", &name
) ||
364 !irreco_keyfile_get_path(keyfile
, "image", &image
)) {
365 IRRECO_PRINTF("Could not read style from group \"%s\"\n",
369 if (irreco_string_table_get(self
->backgrounds
, name
,
371 irreco_theme_bg_set(bg
, name
, image
);
373 bg
= irreco_theme_bg_new();
374 irreco_theme_bg_set(bg
, name
, image
);
375 irreco_string_table_add(self
->backgrounds
, name
, bg
);
378 irreco_theme_bg_print(bg
);
381 if(keyfile
!= NULL
) irreco_keyfile_destroy(keyfile
);
382 if(name
!= NULL
) g_free(name
);
383 if(image
!= NULL
) g_free(image
);
387 IrrecoStringTable
* irreco_theme_get_buttons(IrrecoTheme
*self
)
390 IRRECO_RETURN_PTR(self
->buttons
);
393 IrrecoThemeButton
*irreco_theme_get_button(IrrecoTheme
*self
,
394 const char *button_name
)
396 IrrecoThemeButton
*button
= NULL
;
398 IRRECO_STRING_TABLE_FOREACH_DATA(self
->buttons
, IrrecoThemeButton
*,
400 if (g_utf8_collate(pointer
->name
->str
, button_name
) == 0) {
403 IRRECO_STRING_TABLE_FOREACH_END
404 IRRECO_RETURN_PTR(button
);
407 IrrecoStringTable
* irreco_theme_get_backgrounds(IrrecoTheme
*self
)
410 IRRECO_RETURN_PTR(self
->backgrounds
);
413 IrrecoThemeBg
*irreco_theme_get_background(IrrecoTheme
*self
,
416 IrrecoThemeBg
*bg
= NULL
;
418 IRRECO_STRING_TABLE_FOREACH_DATA(self
->backgrounds
, IrrecoThemeBg
*,
420 if (g_utf8_collate(pointer
->image_name
->str
, bg_name
) == 0) {
423 IRRECO_STRING_TABLE_FOREACH_END
424 IRRECO_RETURN_PTR(bg
);
427 void irreco_theme_set_author(IrrecoTheme
*self
, const char * author
)
430 if (author
!= NULL
) {
431 g_string_printf(self
->author
, "%s", author
);
433 irreco_theme_update_keyfile(self
);
438 void irreco_theme_set_comment(IrrecoTheme
*self
, const char * comment
)
441 if (comment
!= NULL
) {
442 g_string_printf(self
->comment
, "%s", comment
);
444 irreco_theme_update_keyfile(self
);
449 void irreco_theme_set_preview_button(IrrecoTheme
*self
,
450 const char * button_name
)
453 if (button_name
!= NULL
) {
454 g_string_printf(self
->preview_button_name
, "%s", button_name
);
456 irreco_theme_update_keyfile(self
);
463 /* This function will work after IrrecoButtonStyle destruction*/
465 void irreco_theme_set_name(IrrecoTheme
*self
, IrrecoData
*irreco_data
,
470 GString
*style_name
= g_string_new("");
473 *TODO Move this part to IrrecoThemeManager and call this
474 *funtion from ThemeManager*/
476 irreco_string_table_change_key(
477 irreco_data
->theme_manager
->themes
,
478 self
->name
->str
, name
);
480 g_string_printf(self
->name
, "%s", name
);
482 IRRECO_STRING_TABLE_FOREACH_DATA(self
->buttons
,
483 IrrecoThemeButton
*, button
) {
484 g_string_printf(button
->style_name
,"%s/%s",
487 IRRECO_PRINTF("style: %s\n",button
->style_name
->str
);
490 IRRECO_STRING_TABLE_FOREACH_END
492 irreco_theme_update_keyfile(self
);
493 irreco_config_save_layouts(irreco_data
);
494 g_string_free(style_name
, TRUE
);
500 void irreco_theme_set(IrrecoTheme
*self
, const char *name
, const char *path
,
501 const char *source
, const char *author
,
502 const char *comment
, const char *preview_button_name
,
508 g_string_printf(self
->name
, "%s", name
);
510 g_string_erase(self
->name
, 0, -1);
514 g_string_printf(self
->path
, "%s", path
);
516 g_string_erase(self
->path
, 0, -1);
519 if (source
!= NULL
) {
520 g_string_printf(self
->source
, "%s", source
);
522 g_string_erase(self
->source
, 0, -1);
525 if (author
!= NULL
) {
526 g_string_printf(self
->author
, "%s", author
);
528 g_string_erase(self
->author
, 0, -1);
531 if (comment
!= NULL
) {
532 g_string_printf(self
->comment
, "%s", comment
);
534 g_string_erase(self
->comment
, 0, -1);
537 if (preview_button_name
!= NULL
) {
538 g_string_printf(self
->preview_button_name
, "%s",
539 preview_button_name
);
541 g_string_erase(self
->preview_button_name
, 0, -1);
544 if (version
!= NULL
) {
545 g_string_printf(self
->version
, "%s", version
);
547 g_string_erase(self
->version
, 0, -1);
550 irreco_theme_update_keyfile(self
);
554 IrrecoDirForeachData button_styles
;
555 GString
* directory
= g_string_new("");
557 g_string_printf(directory
, "%s/buttons/", path
);
558 IRRECO_DEBUG("Directory = %s\n", directory
->str
);
559 button_styles
.directory
= directory
->str
;
561 button_styles
.filesuffix
= "button.conf";
562 button_styles
.user_data_1
= self
;
564 irreco_dir_foreach_subdirectories(&button_styles
,
565 irreco_theme_read_button_keyfile_foreach
);
567 g_string_free(directory
, TRUE
);
570 irreco_string_table_sort_abc(self
->buttons
);
573 /* Update backgrounds */
575 IrrecoDirForeachData bg_styles
;
576 GString
* directory
= g_string_new("");
578 g_string_printf(directory
, "%s/bg/", path
);
579 IRRECO_DEBUG("Directory = %s\n", directory
->str
);
580 bg_styles
.directory
= directory
->str
;
582 bg_styles
.filesuffix
= "bg.conf";
583 bg_styles
.user_data_1
= self
;
585 irreco_dir_foreach_subdirectories(&bg_styles
,
586 irreco_theme_read_bg_keyfile_foreach
);
588 g_string_free(directory
, TRUE
);
591 irreco_string_table_sort_abc(self
->backgrounds
);
597 void irreco_theme_check(IrrecoTheme
*self
)
601 /* Check if some background is deleted */
602 IRRECO_STRING_TABLE_FOREACH(self
->backgrounds
, key
, IrrecoThemeBg
*,
604 if(!irreco_is_file(theme_bg
->image_path
->str
)) {
605 irreco_string_table_remove(self
->backgrounds
, key
);
607 IRRECO_STRING_TABLE_FOREACH_END
609 /* Check if some button is deleted */
610 IRRECO_STRING_TABLE_FOREACH(self
->buttons
, key
, IrrecoThemeButton
*,
612 if(!irreco_is_file(theme_button
->image_up
->str
)) {
613 irreco_string_table_remove(self
->buttons
, key
);
615 IRRECO_STRING_TABLE_FOREACH_END
620 IrrecoTheme
*irreco_theme_copy(IrrecoTheme
*self
)
622 IrrecoTheme
*new = NULL
;
628 irreco_theme_set(new, self
->name
->str
, self
->path
->str
,
629 self
->source
->str
, self
->author
->str
,
630 self
->comment
->str
, self
->preview_button_name
->str
,
634 IRRECO_STRING_TABLE_FOREACH(self
->backgrounds
, key
, IrrecoThemeBg
*,
636 irreco_string_table_add(new->backgrounds
, key
,
637 irreco_theme_bg_copy(theme_bg
));
639 IRRECO_STRING_TABLE_FOREACH_END
642 IRRECO_STRING_TABLE_FOREACH(self
->buttons
, key
, IrrecoThemeButton
*,
644 irreco_string_table_add(new->buttons
, key
,
645 irreco_theme_button_copy(theme_button
));
647 IRRECO_STRING_TABLE_FOREACH_END
650 IRRECO_RETURN_PTR(new);
654 * IrrecoTheme new from dir
657 IrrecoTheme
*irreco_theme_new_from_dir(const gchar
*dir
)
659 IrrecoTheme
*self
= NULL
;
664 irreco_theme_read(self
, dir
);
665 IRRECO_RETURN_PTR(self
);
668 void irreco_theme_read(IrrecoTheme
*self
, const gchar
*dir
)
670 IrrecoKeyFile
*keyfile
= NULL
;
674 char *comment
= NULL
;
675 char *preview_button
= NULL
;
676 char *version
= NULL
;
677 GString
*conf
= NULL
;
680 conf
= g_string_new(dir
);
681 g_string_append_printf(conf
, "/theme.conf");
682 keyfile
= irreco_keyfile_create(dir
,
686 /* Required fields. */
687 irreco_keyfile_get_str(keyfile
, "name", &name
);
689 /* Optional fields. */
690 irreco_keyfile_get_str(keyfile
, "source", &source
);
691 irreco_keyfile_get_str(keyfile
, "author", &author
);
692 irreco_keyfile_get_str(keyfile
, "comment", &comment
);
693 irreco_keyfile_get_str(keyfile
, "preview-button", &preview_button
);
694 irreco_keyfile_get_str(keyfile
, "version", &version
);
696 irreco_theme_set(self
, name
, dir
, source
,
697 author
, comment
, preview_button
, version
);
699 irreco_theme_print(self
);
702 g_string_free(conf
, TRUE
);
703 if (keyfile
!= NULL
) irreco_keyfile_destroy(keyfile
);
704 if (name
!= NULL
) g_free(name
);
705 if (source
!= NULL
) g_free(source
);
706 if (author
!= NULL
) g_free(author
);
707 if (comment
!= NULL
) g_free(comment
);
708 if (preview_button
!= NULL
) g_free(preview_button
);
709 if (version
!= NULL
) g_free(version
);
721 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
722 /* Events and Callbacks */
723 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/