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
);
76 IRRECO_RETURN_PTR(self
);
81 IrrecoTheme
*irreco_theme_new()
86 self
= g_slice_new0(IrrecoTheme
);
88 self
->name
= g_string_new(name
);
89 self
->path
= g_string_new(path
);
90 self
->source
= g_string_new(source
);
91 self
->author
= g_string_new(author
);
92 self
->comment
= g_string_new(comment
);
93 self
->preview_button_name
= g_string_new(preview_button_name
);
94 self
->version
= g_string_new(version
);
95 self
->backgrounds
= irreco_string_table_new(
96 (GDestroyNotify
)irreco_theme_bg_free
, NULL
);
97 self
->buttons
= irreco_string_table_new(
98 (GDestroyNotify
)irreco_theme_button_free
, NULL
);
100 IRRECO_DEBUG("Themepath: %s\n", path
);
104 IrrecoDirForeachData button_styles
;
105 GString
* directory
= g_string_new("");
107 g_string_printf(directory
, "%s/buttons/", path
);
108 IRRECO_DEBUG("Directory = %s\n", directory
->str
);
109 button_styles
.directory
= directory
->str
;
111 button_styles
.filesuffix
= "button.conf";
112 button_styles
.user_data_1
= self
;
114 irreco_dir_foreach_subdirectories(&button_styles
,
115 irreco_theme_read_button_keyfile_foreach
);
117 g_string_free(directory
, TRUE
);
120 irreco_string_table_sort_abc(self
->buttons
);
125 IrrecoDirForeachData bg_styles
;
126 GString
* directory
= g_string_new("");
128 g_string_printf(directory
, "%s/bg/", path
);
129 IRRECO_DEBUG("Directory = %s\n", directory
->str
);
130 bg_styles
.directory
= directory
->str
;
132 bg_styles
.filesuffix
= "bg.conf";
133 bg_styles
.user_data_1
= self
;
135 irreco_dir_foreach_subdirectories(&bg_styles
,
136 irreco_theme_read_bg_keyfile_foreach
);
138 g_string_free(directory
, TRUE
);
141 irreco_string_table_sort_abc(self
->backgrounds
);
144 IRRECO_RETURN_PTR(self
);
147 void irreco_theme_free(IrrecoTheme
*self
)
151 g_assert(self
!= NULL
);
153 g_string_free(self
->name
, TRUE
);
156 g_string_free(self
->path
, TRUE
);
159 g_string_free(self
->source
, TRUE
);
162 g_string_free(self
->author
, TRUE
);
165 g_string_free(self
->comment
, TRUE
);
166 self
->comment
= NULL
;
168 g_string_free(self
->preview_button_name
, TRUE
);
169 self
->preview_button_name
= NULL
;
171 irreco_string_table_free(self
->backgrounds
);
172 self
->backgrounds
= NULL
;
174 irreco_string_table_free(self
->buttons
);
175 self
->buttons
= NULL
;
177 g_slice_free(IrrecoTheme
, self
);
185 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
186 /* Private Functions */
187 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
190 * @name Private Functions
197 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
198 /* Public Functions */
199 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
202 * @name Public Functions
205 void irreco_theme_update_keyfile(IrrecoTheme
*self
)
207 GString
*keyfile_path
= g_string_new(self
->path
->str
);
208 GKeyFile
*keyfile
= g_key_file_new();
211 g_string_append(keyfile_path
, "/theme.conf");
213 irreco_gkeyfile_set_string(keyfile
, "theme" , "name",
216 if (self
->source
->len
> 0) {
217 irreco_gkeyfile_set_string(keyfile
, "theme" , "source",
221 if (self
->author
->len
> 0) {
222 irreco_gkeyfile_set_string(keyfile
, "theme", "author",
226 if (self
->preview_button_name
->len
> 0) {
227 irreco_gkeyfile_set_string(keyfile
, "theme", "preview-button",
228 self
->preview_button_name
->str
);
231 if (self
->version
->len
> 0) {
232 irreco_gkeyfile_set_string(keyfile
, "theme", "version",
236 if (self
->comment
->len
> 0) {
237 irreco_gkeyfile_set_string(keyfile
, "theme", "comment",
241 irreco_write_keyfile(keyfile
, keyfile_path
->str
);
243 g_key_file_free(keyfile
);
244 g_string_free(keyfile_path
, TRUE
);
248 void irreco_theme_print(IrrecoTheme
*self
)
252 IRRECO_DEBUG("Themename: %s \n", self
->name
->str
);
253 IRRECO_DEBUG("Folder: %s \n", self
->path
->str
);
254 IRRECO_DEBUG("Source: %s \n", self
->source
->str
);
255 IRRECO_DEBUG("Author: %s \n", self
->author
->str
);
256 IRRECO_DEBUG("Comment: %s \n", self
->comment
->str
);
257 IRRECO_DEBUG("Previewbutton: %s \n", self
->preview_button_name
->str
);
258 IRRECO_DEBUG("Version: %s \n", self
->version
->str
);
259 irreco_string_table_print(self
->backgrounds
);
260 irreco_string_table_print(self
->buttons
);
265 void irreco_theme_read_button_keyfile_foreach(IrrecoDirForeachData
* dir_data
)
267 IrrecoTheme
*self
= (IrrecoTheme
*) dir_data
->user_data_1
;
268 IrrecoThemeButton
*button
= NULL
;
271 button
= irreco_theme_button_new_from_dir(dir_data
->directory
,
274 if (irreco_string_table_exists(self
->buttons
, button
->name
->str
)) {
275 IRRECO_ERROR("Error: Button %s has already been read. "
276 "You cannot have two buttons with the same name.\n",
278 irreco_theme_button_free(button
);
280 irreco_string_table_add(self
->buttons
,
281 button
->style_name
->str
, button
);
287 void irreco_theme_read_bg_keyfile_foreach(IrrecoDirForeachData
* dir_data
)
289 IrrecoTheme
*self
= (IrrecoTheme
*) dir_data
->user_data_1
;
290 IrrecoThemeBg
*bg
= NULL
;
293 bg
= irreco_theme_bg_new_from_dir(dir_data
->directory
);
295 if (irreco_string_table_exists(self
->backgrounds
, bg
->image_name
->str
)) {
296 IRRECO_ERROR("Error: Background %s has already been read. "
297 "You cannot have two backgrounds with the same name.\n",
298 bg
->image_name
->str
);
299 irreco_theme_bg_free(bg
);
301 irreco_string_table_add(self
->backgrounds
,
302 bg
->image_name
->str
, bg
);
307 IrrecoStringTable
* irreco_theme_get_buttons(IrrecoTheme
*self
)
310 IRRECO_RETURN_PTR(self
->buttons
);
313 IrrecoThemeButton
*irreco_theme_get_button(IrrecoTheme
*self
,
314 const char *button_name
)
316 IrrecoThemeButton
*button
= NULL
;
318 IRRECO_STRING_TABLE_FOREACH_DATA(self
->buttons
, IrrecoThemeButton
*,
320 if (g_utf8_collate(pointer
->name
->str
, button_name
) == 0) {
323 IRRECO_STRING_TABLE_FOREACH_END
324 IRRECO_RETURN_PTR(button
);
327 IrrecoStringTable
* irreco_theme_get_backgrounds(IrrecoTheme
*self
)
330 IRRECO_RETURN_PTR(self
->backgrounds
);
333 IrrecoThemeBg
*irreco_theme_get_background(IrrecoTheme
*self
,
336 IrrecoThemeBg
*bg
= NULL
;
338 IRRECO_STRING_TABLE_FOREACH_DATA(self
->backgrounds
, IrrecoThemeBg
*,
340 if (g_utf8_collate(pointer
->image_name
->str
, bg_name
) == 0) {
343 IRRECO_STRING_TABLE_FOREACH_END
344 IRRECO_RETURN_PTR(bg
);
347 void irreco_theme_set_author(IrrecoTheme
*self
, const char * author
)
350 if (author
!= NULL
) {
351 g_string_printf(self
->author
, "%s", author
);
353 irreco_theme_update_keyfile(self
);
358 void irreco_theme_set_comment(IrrecoTheme
*self
, const char * comment
)
361 if (comment
!= NULL
) {
362 g_string_printf(self
->comment
, "%s", comment
);
364 irreco_theme_update_keyfile(self
);
369 void irreco_theme_set_preview_button(IrrecoTheme
*self
,
370 const char * button_name
)
373 if (button_name
!= NULL
) {
374 g_string_printf(self
->preview_button_name
, "%s", button_name
);
376 irreco_theme_update_keyfile(self
);
383 /* This function will work after IrrecoButtonStyle destruction*/
385 void irreco_theme_set_name(IrrecoTheme
*self
, IrrecoData
*irreco_data
,
390 GString
*style_name
= g_string_new("");
393 *TODO Move this part to IrrecoThemeManager and call this
394 *funtion from ThemeManager*/
396 irreco_string_table_change_key(
397 irreco_data
->theme_manager
->themes
,
398 self
->name
->str
, name
);
400 g_string_printf(self
->name
, "%s", name
);
402 IRRECO_STRING_TABLE_FOREACH_DATA(self
->buttons
,
403 IrrecoThemeButton
*, button
) {
404 g_string_printf(button
->style_name
,"%s/%s",
407 IRRECO_PRINTF("style: %s\n",button
->style_name
->str
);
410 IRRECO_STRING_TABLE_FOREACH_END
412 irreco_theme_update_keyfile(self
);
413 irreco_config_save_layouts(irreco_data
);
414 g_string_free(style_name
, TRUE
);
420 void irreco_theme_set(IrrecoTheme
*self
, const char *name
, const char *path
,
421 const char *source
, const char *author
,
422 const char *comment
, const char *preview_button_name
,
428 g_string_printf(self
->name
, "%s", name
);
430 g_string_erase(self
->name
, 0, -1);
434 g_string_printf(self
->path
, "%s", path
);
436 g_string_erase(self
->path
, 0, -1);
439 if (source
!= NULL
) {
440 g_string_printf(self
->source
, "%s", source
);
442 g_string_erase(self
->source
, 0, -1);
445 if (author
!= NULL
) {
446 g_string_printf(self
->author
, "%s", author
);
448 g_string_erase(self
->author
, 0, -1);
451 if (comment
!= NULL
) {
452 g_string_printf(self
->comment
, "%s", comment
);
454 g_string_erase(self
->comment
, 0, -1);
457 if (preview_button_name
!= NULL
) {
458 g_string_printf(self
->preview_button_name
, "%s",
459 preview_button_name
);
461 g_string_erase(self
->preview_button_name
, 0, -1);
464 if (version
!= NULL
) {
465 g_string_printf(self
->version
, "%s", version
);
467 g_string_erase(self
->version
, 0, -1);
470 irreco_theme_update_keyfile(self
);
474 IrrecoDirForeachData button_styles
;
475 GString
* directory
= g_string_new("");
477 g_string_printf(directory
, "%s/buttons/", path
);
478 IRRECO_DEBUG("Directory = %s\n", directory
->str
);
479 button_styles
.directory
= directory
->str
;
481 button_styles
.filesuffix
= "button.conf";
482 button_styles
.user_data_1
= self
;
484 irreco_dir_foreach_subdirectories(&button_styles
,
485 irreco_theme_read_button_keyfile_foreach
);
487 g_string_free(directory
, TRUE
);
490 irreco_string_table_sort_abc(self
->buttons
);
493 /* Update backgrounds */
495 IrrecoDirForeachData bg_styles
;
496 GString
* directory
= g_string_new("");
498 g_string_printf(directory
, "%s/bg/", path
);
499 IRRECO_DEBUG("Directory = %s\n", directory
->str
);
500 bg_styles
.directory
= directory
->str
;
502 bg_styles
.filesuffix
= "bg.conf";
503 bg_styles
.user_data_1
= self
;
505 irreco_dir_foreach_subdirectories(&bg_styles
,
506 irreco_theme_read_bg_keyfile_foreach
);
508 g_string_free(directory
, TRUE
);
511 irreco_string_table_sort_abc(self
->backgrounds
);
517 void irreco_theme_check(IrrecoTheme
*self
)
521 /* Check if some background is deleted */
522 IRRECO_STRING_TABLE_FOREACH(self
->backgrounds
, key
, IrrecoThemeBg
*,
524 if(!irreco_is_file(theme_bg
->image_path
->str
)) {
525 irreco_string_table_remove(self
->backgrounds
, key
);
527 IRRECO_STRING_TABLE_FOREACH_END
529 /* Check if some button is deleted */
530 IRRECO_STRING_TABLE_FOREACH(self
->buttons
, key
, IrrecoThemeButton
*,
532 if(!irreco_is_file(theme_button
->image_up
->str
)) {
533 irreco_string_table_remove(self
->buttons
, key
);
535 IRRECO_STRING_TABLE_FOREACH_END
540 IrrecoTheme
*irreco_theme_copy(IrrecoTheme
*self
)
542 IrrecoTheme
*new = NULL
;
546 new = irreco_theme_new();
548 irreco_theme_set(new, self
->name
->str
, self
->path
->str
,
549 self
->source
->str
, self
->author
->str
,
550 self
->comment
->str
, self
->preview_button_name
->str
,
554 IRRECO_STRING_TABLE_FOREACH(self
->backgrounds
, key
, IrrecoThemeBg
*,
556 irreco_string_table_add(new->backgrounds
, key
,
557 irreco_theme_bg_copy(theme_bg
));
559 IRRECO_STRING_TABLE_FOREACH_END
562 IRRECO_STRING_TABLE_FOREACH(self
->buttons
, key
, IrrecoThemeButton
*,
564 irreco_string_table_add(new->buttons
, key
,
565 irreco_theme_button_copy(theme_button
));
567 IRRECO_STRING_TABLE_FOREACH_END
570 IRRECO_RETURN_PTR(new);
574 * IrrecoTheme new from dir
577 IrrecoTheme
*irreco_theme_new_from_dir(const gchar
*dir
)
579 IrrecoTheme
*self
= NULL
;
583 self
= irreco_theme_new();
584 irreco_theme_read(self
, dir
);
585 IRRECO_RETURN_PTR(self
);
588 void irreco_theme_read(IrrecoTheme
*self
, const gchar
*dir
)
590 IrrecoKeyFile
*keyfile
= NULL
;
594 char *comment
= NULL
;
595 char *preview_button
= NULL
;
596 char *version
= NULL
;
597 GString
*conf
= NULL
;
600 conf
= g_string_new(dir
);
601 g_string_append_printf(conf
, "/theme.conf");
602 keyfile
= irreco_keyfile_create(dir
,
606 /* Required fields. */
607 irreco_keyfile_get_str(keyfile
, "name", &name
);
609 /* Optional fields. */
610 irreco_keyfile_get_str(keyfile
, "source", &source
);
611 irreco_keyfile_get_str(keyfile
, "author", &author
);
612 irreco_keyfile_get_str(keyfile
, "comment", &comment
);
613 irreco_keyfile_get_str(keyfile
, "preview-button", &preview_button
);
614 irreco_keyfile_get_str(keyfile
, "version", &version
);
616 /* call irreco_theme_set() */
617 irreco_theme_set(self
, name
, dir
, source
,
618 author
, comment
, preview_button
, version
);
620 g_string_free(conf
, TRUE
);
621 if (keyfile
!= NULL
) irreco_keyfile_destroy(keyfile
);
622 if (name
!= NULL
) g_free(name
);
623 if (source
!= NULL
) g_free(source
);
624 if (author
!= NULL
) g_free(author
);
625 if (comment
!= NULL
) g_free(comment
);
626 if (preview_button
!= NULL
) g_free(preview_button
);
627 if (version
!= NULL
) g_free(version
);
632 gboolean
irreco_theme_save(IrrecoTheme
*self
, IrrecoData
*irreco_data
,
633 const gchar
*theme_path
)
635 gboolean rvalue
= FALSE
;
638 IrrecoStringTable
*bg_list
= NULL
;
639 IrrecoStringTable
*button_list
= NULL
;
643 theme
= irreco_theme_new();
644 irreco_theme_set(theme
,
650 self
->preview_button_name
->str
,
653 irreco_theme_update_keyfile(theme
);
656 /* Get buttons and backgrounds */
658 bg_list
= irreco_theme_get_backgrounds(self
);
660 path
= g_string_new("");
661 g_string_printf(path
, "%s/bg", theme_path
);
662 g_mkdir(path
->str
, 0777);
664 IRRECO_STRING_TABLE_FOREACH_DATA(bg_list
, IrrecoThemeBg
*, background
)
666 irreco_theme_bg_print(background
);
668 irreco_theme_bg_save(background
, path
->str
);
671 IRRECO_STRING_TABLE_FOREACH_END
674 button_list
= irreco_theme_get_buttons(self
);
676 g_string_printf(path
, "%s/buttons", theme_path
);
677 g_mkdir(path
->str
, 0777);
679 IRRECO_STRING_TABLE_FOREACH_DATA(button_list
, IrrecoThemeButton
*, button
)
681 if (g_str_equal(self
->preview_button_name
->str
,
682 button
->image_up
->str
) && !rvalue
) {
683 irreco_theme_set_preview_button(theme
,
688 irreco_theme_button_save(button
, path
->str
);
689 irreco_theme_button_print(button
);
691 IRRECO_STRING_TABLE_FOREACH_END
695 g_string_free(path
, TRUE
);
696 irreco_theme_free(theme
);
698 IRRECO_RETURN_BOOL(rvalue
);
704 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
705 /* Events and Callbacks */
706 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/