2 * irreco - Ir Remote Control
3 * Copyright (C) 2008 Joni Kokko (t5kojo01@students.oamk.fi)
4 * Pekka Gehör (pegu6@msn.com)
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_theme.h"
24 * @addtogroup IrrecoTheme
27 * Contains information of theme.
34 * Source file of @ref IrrecoTheme.
37 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
39 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
40 void irreco_theme_read_button_keyfile_foreach(IrrecoDirForeachData
* dir_data
);
41 void irreco_theme_read_bg_keyfile_foreach(IrrecoDirForeachData
* dir_data
);
42 void irreco_theme_read(IrrecoTheme
*self
, const gchar
*dir
);
44 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
45 /* Construction & Destruction */
46 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
49 * @name Construction & Destruction
56 IrrecoTheme
*irreco_theme_new()
61 self
= g_slice_new0(IrrecoTheme
);
63 self
->name
= g_string_new(NULL
);
64 self
->path
= g_string_new(NULL
);
65 self
->source
= g_string_new(NULL
);
66 self
->author
= g_string_new(NULL
);
67 self
->comment
= g_string_new(NULL
);
68 self
->preview_button_name
= g_string_new(NULL
);
69 self
->version
= g_string_new(NULL
);
70 self
->backgrounds
= irreco_string_table_new(
71 (GDestroyNotify
)irreco_theme_bg_free
, NULL
);
72 self
->buttons
= irreco_string_table_new(
73 (GDestroyNotify
)irreco_theme_button_free
, NULL
);
75 IRRECO_RETURN_PTR(self
);
80 IrrecoTheme
*irreco_theme_new()
85 self
= g_slice_new0(IrrecoTheme
);
87 self
->name
= g_string_new(name
);
88 self
->path
= g_string_new(path
);
89 self
->source
= g_string_new(source
);
90 self
->author
= g_string_new(author
);
91 self
->comment
= g_string_new(comment
);
92 self
->preview_button_name
= g_string_new(preview_button_name
);
93 self
->version
= g_string_new(version
);
94 self
->backgrounds
= irreco_string_table_new(
95 (GDestroyNotify
)irreco_theme_bg_free
, NULL
);
96 self
->buttons
= irreco_string_table_new(
97 (GDestroyNotify
)irreco_theme_button_free
, NULL
);
99 IRRECO_DEBUG("Themepath: %s\n", path
);
103 IrrecoDirForeachData button_styles
;
104 GString
* directory
= g_string_new("");
106 g_string_printf(directory
, "%s/buttons/", path
);
107 IRRECO_DEBUG("Directory = %s\n", directory
->str
);
108 button_styles
.directory
= directory
->str
;
110 button_styles
.filesuffix
= "button.conf";
111 button_styles
.user_data_1
= self
;
113 irreco_dir_foreach_subdirectories(&button_styles
,
114 irreco_theme_read_button_keyfile_foreach
);
116 g_string_free(directory
, TRUE
);
119 irreco_string_table_sort_abc(self
->buttons
);
124 IrrecoDirForeachData bg_styles
;
125 GString
* directory
= g_string_new("");
127 g_string_printf(directory
, "%s/bg/", path
);
128 IRRECO_DEBUG("Directory = %s\n", directory
->str
);
129 bg_styles
.directory
= directory
->str
;
131 bg_styles
.filesuffix
= "bg.conf";
132 bg_styles
.user_data_1
= self
;
134 irreco_dir_foreach_subdirectories(&bg_styles
,
135 irreco_theme_read_bg_keyfile_foreach
);
137 g_string_free(directory
, TRUE
);
140 irreco_string_table_sort_abc(self
->backgrounds
);
143 IRRECO_RETURN_PTR(self
);
146 void irreco_theme_free(IrrecoTheme
*self
)
150 g_assert(self
!= NULL
);
152 g_string_free(self
->name
, TRUE
);
155 g_string_free(self
->path
, TRUE
);
158 g_string_free(self
->source
, TRUE
);
161 g_string_free(self
->author
, TRUE
);
164 g_string_free(self
->comment
, TRUE
);
165 self
->comment
= NULL
;
167 g_string_free(self
->preview_button_name
, TRUE
);
168 self
->preview_button_name
= NULL
;
170 irreco_string_table_free(self
->backgrounds
);
171 self
->backgrounds
= NULL
;
173 irreco_string_table_free(self
->buttons
);
174 self
->buttons
= NULL
;
176 g_slice_free(IrrecoTheme
, self
);
184 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
185 /* Private Functions */
186 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
189 * @name Private Functions
196 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
197 /* Public Functions */
198 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
201 * @name Public Functions
204 void irreco_theme_update_keyfile(IrrecoTheme
*self
)
206 GString
*keyfile_path
= g_string_new(self
->path
->str
);
207 GKeyFile
*keyfile
= g_key_file_new();
210 g_string_append(keyfile_path
, "/theme.conf");
212 irreco_gkeyfile_set_string(keyfile
, "theme" , "name",
215 if (self
->source
->len
> 0) {
216 irreco_gkeyfile_set_string(keyfile
, "theme" , "source",
220 if (self
->author
->len
> 0) {
221 irreco_gkeyfile_set_string(keyfile
, "theme", "author",
225 if (self
->preview_button_name
->len
> 0) {
226 irreco_gkeyfile_set_string(keyfile
, "theme", "preview-button",
227 self
->preview_button_name
->str
);
230 if (self
->version
->len
> 0) {
231 irreco_gkeyfile_set_string(keyfile
, "theme", "version",
235 if (self
->comment
->len
> 0) {
236 irreco_gkeyfile_set_string(keyfile
, "theme", "comment",
240 irreco_write_keyfile(keyfile
, keyfile_path
->str
);
242 g_key_file_free(keyfile
);
243 g_string_free(keyfile_path
, TRUE
);
247 void irreco_theme_print(IrrecoTheme
*self
)
251 IRRECO_DEBUG("Themename: %s \n", self
->name
->str
);
252 IRRECO_DEBUG("Folder: %s \n", self
->path
->str
);
253 IRRECO_DEBUG("Source: %s \n", self
->source
->str
);
254 IRRECO_DEBUG("Author: %s \n", self
->author
->str
);
255 IRRECO_DEBUG("Comment: %s \n", self
->comment
->str
);
256 IRRECO_DEBUG("Previewbutton: %s \n", self
->preview_button_name
->str
);
257 IRRECO_DEBUG("Version: %s \n", self
->version
->str
);
258 irreco_string_table_print(self
->backgrounds
);
259 irreco_string_table_print(self
->buttons
);
264 void irreco_theme_read_button_keyfile_foreach(IrrecoDirForeachData
* dir_data
)
266 IrrecoTheme
*self
= (IrrecoTheme
*) dir_data
->user_data_1
;
267 IrrecoThemeButton
*button
= NULL
;
270 button
= irreco_theme_button_new_from_dir(dir_data
->directory
,
273 if (irreco_string_table_exists(self
->buttons
, button
->name
->str
)) {
274 IRRECO_ERROR("Error: Button %s has already been read. "
275 "You cannot have two buttons with the same name.\n",
277 irreco_theme_button_free(button
);
279 irreco_string_table_add(self
->buttons
,
280 button
->style_name
->str
, button
);
286 void irreco_theme_read_bg_keyfile_foreach(IrrecoDirForeachData
* dir_data
)
288 IrrecoTheme
*self
= (IrrecoTheme
*) dir_data
->user_data_1
;
289 IrrecoThemeBg
*bg
= NULL
;
292 bg
= irreco_theme_bg_new_from_dir(dir_data
->directory
);
294 if (irreco_string_table_exists(self
->backgrounds
, bg
->image_name
->str
)) {
295 IRRECO_ERROR("Error: Background %s has already been read. "
296 "You cannot have two backgrounds with the same name.\n",
297 bg
->image_name
->str
);
298 irreco_theme_bg_free(bg
);
300 irreco_string_table_add(self
->backgrounds
,
301 bg
->image_name
->str
, bg
);
306 IrrecoStringTable
* irreco_theme_get_buttons(IrrecoTheme
*self
)
309 IRRECO_RETURN_PTR(self
->buttons
);
312 IrrecoThemeButton
*irreco_theme_get_button(IrrecoTheme
*self
,
313 const char *button_name
)
315 IrrecoThemeButton
*button
= NULL
;
317 IRRECO_STRING_TABLE_FOREACH_DATA(self
->buttons
, IrrecoThemeButton
*,
319 if (g_utf8_collate(pointer
->name
->str
, button_name
) == 0) {
322 IRRECO_STRING_TABLE_FOREACH_END
323 IRRECO_RETURN_PTR(button
);
326 IrrecoStringTable
* irreco_theme_get_backgrounds(IrrecoTheme
*self
)
329 IRRECO_RETURN_PTR(self
->backgrounds
);
332 IrrecoThemeBg
*irreco_theme_get_background(IrrecoTheme
*self
,
335 IrrecoThemeBg
*bg
= NULL
;
337 IRRECO_STRING_TABLE_FOREACH_DATA(self
->backgrounds
, IrrecoThemeBg
*,
339 if (g_utf8_collate(pointer
->image_name
->str
, bg_name
) == 0) {
342 IRRECO_STRING_TABLE_FOREACH_END
343 IRRECO_RETURN_PTR(bg
);
346 void irreco_theme_set_author(IrrecoTheme
*self
, const char * author
)
349 if (author
!= NULL
) {
350 g_string_printf(self
->author
, "%s", author
);
352 irreco_theme_update_keyfile(self
);
357 void irreco_theme_set_comment(IrrecoTheme
*self
, const char * comment
)
360 if (comment
!= NULL
) {
361 g_string_printf(self
->comment
, "%s", comment
);
363 irreco_theme_update_keyfile(self
);
368 void irreco_theme_set_preview_button(IrrecoTheme
*self
,
369 const char * button_name
)
372 if (button_name
!= NULL
) {
373 g_string_printf(self
->preview_button_name
, "%s", button_name
);
375 irreco_theme_update_keyfile(self
);
382 /* This function will work after IrrecoButtonStyle destruction*/
384 void irreco_theme_set_name(IrrecoTheme
*self
, IrrecoData
*irreco_data
,
389 GString
*style_name
= g_string_new("");
392 *TODO Move this part to IrrecoThemeManager and call this
393 *funtion from ThemeManager*/
395 irreco_string_table_change_key(
396 irreco_data
->theme_manager
->themes
,
397 self
->name
->str
, name
);
399 g_string_printf(self
->name
, "%s", name
);
401 IRRECO_STRING_TABLE_FOREACH_DATA(self
->buttons
,
402 IrrecoThemeButton
*, button
) {
403 g_string_printf(button
->style_name
,"%s/%s",
406 IRRECO_PRINTF("style: %s\n",button
->style_name
->str
);
409 IRRECO_STRING_TABLE_FOREACH_END
411 irreco_theme_update_keyfile(self
);
412 irreco_config_save_layouts(irreco_data
);
413 g_string_free(style_name
, TRUE
);
419 void irreco_theme_set(IrrecoTheme
*self
, const char *name
, const char *path
,
420 const char *source
, const char *author
,
421 const char *comment
, const char *preview_button_name
,
427 g_string_printf(self
->name
, "%s", name
);
429 g_string_erase(self
->name
, 0, -1);
433 g_string_printf(self
->path
, "%s", path
);
435 g_string_erase(self
->path
, 0, -1);
438 if (source
!= NULL
) {
439 g_string_printf(self
->source
, "%s", source
);
441 g_string_erase(self
->source
, 0, -1);
444 if (author
!= NULL
) {
445 g_string_printf(self
->author
, "%s", author
);
447 g_string_erase(self
->author
, 0, -1);
450 if (comment
!= NULL
) {
451 g_string_printf(self
->comment
, "%s", comment
);
453 g_string_erase(self
->comment
, 0, -1);
456 if (preview_button_name
!= NULL
) {
457 g_string_printf(self
->preview_button_name
, "%s",
458 preview_button_name
);
460 g_string_erase(self
->preview_button_name
, 0, -1);
463 if (version
!= NULL
) {
464 g_string_printf(self
->version
, "%s", version
);
466 g_string_erase(self
->version
, 0, -1);
469 irreco_theme_update_keyfile(self
);
473 IrrecoDirForeachData button_styles
;
474 GString
* directory
= g_string_new("");
476 g_string_printf(directory
, "%s/buttons/", path
);
477 IRRECO_DEBUG("Directory = %s\n", directory
->str
);
478 button_styles
.directory
= directory
->str
;
480 button_styles
.filesuffix
= "button.conf";
481 button_styles
.user_data_1
= self
;
483 irreco_dir_foreach_subdirectories(&button_styles
,
484 irreco_theme_read_button_keyfile_foreach
);
486 g_string_free(directory
, TRUE
);
489 irreco_string_table_sort_abc(self
->buttons
);
492 /* Update backgrounds */
494 IrrecoDirForeachData bg_styles
;
495 GString
* directory
= g_string_new("");
497 g_string_printf(directory
, "%s/bg/", path
);
498 IRRECO_DEBUG("Directory = %s\n", directory
->str
);
499 bg_styles
.directory
= directory
->str
;
501 bg_styles
.filesuffix
= "bg.conf";
502 bg_styles
.user_data_1
= self
;
504 irreco_dir_foreach_subdirectories(&bg_styles
,
505 irreco_theme_read_bg_keyfile_foreach
);
507 g_string_free(directory
, TRUE
);
510 irreco_string_table_sort_abc(self
->backgrounds
);
516 void irreco_theme_check(IrrecoTheme
*self
)
520 /* Check if some background is deleted */
521 IRRECO_STRING_TABLE_FOREACH(self
->backgrounds
, key
, IrrecoThemeBg
*,
523 if(!irreco_is_file(theme_bg
->image_path
->str
)) {
524 irreco_string_table_remove(self
->backgrounds
, key
);
526 IRRECO_STRING_TABLE_FOREACH_END
528 /* Check if some button is deleted */
529 IRRECO_STRING_TABLE_FOREACH(self
->buttons
, key
, IrrecoThemeButton
*,
531 if(!irreco_is_file(theme_button
->image_up
->str
)) {
532 irreco_string_table_remove(self
->buttons
, key
);
534 IRRECO_STRING_TABLE_FOREACH_END
539 IrrecoTheme
*irreco_theme_copy(IrrecoTheme
*self
)
541 IrrecoTheme
*new = NULL
;
545 new = irreco_theme_new();
547 irreco_theme_set(new, self
->name
->str
, self
->path
->str
,
548 self
->source
->str
, self
->author
->str
,
549 self
->comment
->str
, self
->preview_button_name
->str
,
553 IRRECO_STRING_TABLE_FOREACH(self
->backgrounds
, key
, IrrecoThemeBg
*,
555 irreco_string_table_add(new->backgrounds
, key
,
556 irreco_theme_bg_copy(theme_bg
));
558 IRRECO_STRING_TABLE_FOREACH_END
561 IRRECO_STRING_TABLE_FOREACH(self
->buttons
, key
, IrrecoThemeButton
*,
563 irreco_string_table_add(new->buttons
, key
,
564 irreco_theme_button_copy(theme_button
));
566 IRRECO_STRING_TABLE_FOREACH_END
569 IRRECO_RETURN_PTR(new);
573 * IrrecoTheme new from dir
576 IrrecoTheme
*irreco_theme_new_from_dir(const gchar
*dir
)
578 IrrecoTheme
*self
= NULL
;
582 self
= irreco_theme_new();
583 irreco_theme_read(self
, dir
);
584 IRRECO_RETURN_PTR(self
);
587 void irreco_theme_read(IrrecoTheme
*self
, const gchar
*dir
)
589 IrrecoKeyFile
*keyfile
= NULL
;
593 char *comment
= NULL
;
594 char *preview_button
= NULL
;
595 char *version
= NULL
;
596 GString
*conf
= NULL
;
599 conf
= g_string_new(dir
);
600 g_string_append_printf(conf
, "/theme.conf");
601 keyfile
= irreco_keyfile_create(dir
,
605 /* Required fields. */
606 irreco_keyfile_get_str(keyfile
, "name", &name
);
608 /* Optional fields. */
609 irreco_keyfile_get_str(keyfile
, "source", &source
);
610 irreco_keyfile_get_str(keyfile
, "author", &author
);
611 irreco_keyfile_get_str(keyfile
, "comment", &comment
);
612 irreco_keyfile_get_str(keyfile
, "preview-button", &preview_button
);
613 irreco_keyfile_get_str(keyfile
, "version", &version
);
615 /* call irreco_theme_set() */
616 irreco_theme_set(self
, name
, dir
, source
,
617 author
, comment
, preview_button
, version
);
619 g_string_free(conf
, TRUE
);
620 if (keyfile
!= NULL
) irreco_keyfile_destroy(keyfile
);
621 if (name
!= NULL
) g_free(name
);
622 if (source
!= NULL
) g_free(source
);
623 if (author
!= NULL
) g_free(author
);
624 if (comment
!= NULL
) g_free(comment
);
625 if (preview_button
!= NULL
) g_free(preview_button
);
626 if (version
!= NULL
) g_free(version
);
631 gboolean
irreco_theme_save(IrrecoTheme
*self
, IrrecoData
*irreco_data
,
632 const gchar
*theme_path
)
634 gboolean rvalue
= FALSE
;
637 IrrecoStringTable
*bg_list
= NULL
;
638 IrrecoStringTable
*button_list
= NULL
;
642 theme
= irreco_theme_new();
643 irreco_theme_set(theme
,
649 self
->preview_button_name
->str
,
652 irreco_theme_update_keyfile(theme
);
655 /* Get buttons and backgrounds */
657 bg_list
= irreco_theme_get_backgrounds(self
);
659 path
= g_string_new("");
660 g_string_printf(path
, "%s/bg", theme_path
);
661 g_mkdir(path
->str
, 0777);
663 IRRECO_STRING_TABLE_FOREACH_DATA(bg_list
, IrrecoThemeBg
*, background
)
665 irreco_theme_bg_print(background
);
667 irreco_theme_bg_save(background
, path
->str
);
670 IRRECO_STRING_TABLE_FOREACH_END
673 button_list
= irreco_theme_get_buttons(self
);
675 g_string_printf(path
, "%s/buttons", theme_path
);
676 g_mkdir(path
->str
, 0777);
678 IRRECO_STRING_TABLE_FOREACH_DATA(button_list
, IrrecoThemeButton
*, button
)
680 if (g_str_equal(self
->preview_button_name
->str
,
681 button
->image_up
->str
) && !rvalue
) {
682 irreco_theme_set_preview_button(theme
,
687 irreco_theme_button_save(button
, path
->str
);
688 irreco_theme_button_print(button
);
690 IRRECO_STRING_TABLE_FOREACH_END
694 g_string_free(path
, TRUE
);
695 irreco_theme_free(theme
);
697 IRRECO_RETURN_BOOL(rvalue
);
703 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
704 /* Events and Callbacks */
705 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/