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 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
42 /* Construction & Destruction */
43 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
46 * @name Construction & Destruction
50 IrrecoTheme
*irreco_theme_new(const char *name
, const char *path
,
51 const char *source
, const char *author
,
53 const char *preview_button_name
,
59 self
= g_slice_new0(IrrecoTheme
);
61 self
->name
= g_string_new(name
);
62 self
->path
= g_string_new(path
);
63 self
->source
= g_string_new(source
);
64 self
->author
= g_string_new(author
);
65 self
->comment
= g_string_new(comment
);
66 self
->preview_button_name
= g_string_new(preview_button_name
);
67 self
->version
= g_string_new(version
);
68 self
->backgrounds
= irreco_string_table_new(
69 (GDestroyNotify
)irreco_theme_bg_free
, NULL
);
70 self
->buttons
= irreco_string_table_new(
71 (GDestroyNotify
)irreco_theme_button_free
, NULL
);
73 IRRECO_DEBUG("Themepath: %s\n", path
);
77 IrrecoDirForeachData button_styles
;
78 GString
* directory
= g_string_new("");
80 g_string_printf(directory
, "%s/buttons/", path
);
81 IRRECO_DEBUG("Directory = %s\n", directory
->str
);
82 button_styles
.directory
= directory
->str
;
84 button_styles
.filesuffix
= "button.conf";
85 button_styles
.user_data_1
= self
;
87 irreco_dir_foreach_subdirectories(&button_styles
,
88 irreco_theme_read_button_keyfile_foreach
);
90 g_string_free(directory
, TRUE
);
93 irreco_string_table_sort_abc(self
->buttons
);
98 IrrecoDirForeachData bg_styles
;
99 GString
* directory
= g_string_new("");
101 g_string_printf(directory
, "%s/bg/", path
);
102 IRRECO_DEBUG("Directory = %s\n", directory
->str
);
103 bg_styles
.directory
= directory
->str
;
105 bg_styles
.filesuffix
= "bg.conf";
106 bg_styles
.user_data_1
= self
;
108 irreco_dir_foreach_subdirectories(&bg_styles
,
109 irreco_theme_read_bg_keyfile_foreach
);
111 g_string_free(directory
, TRUE
);
114 irreco_string_table_sort_abc(self
->backgrounds
);
117 IRRECO_RETURN_PTR(self
);
120 void irreco_theme_free(IrrecoTheme
*self
)
124 g_assert(self
!= NULL
);
126 g_string_free(self
->name
, TRUE
);
129 g_string_free(self
->path
, TRUE
);
132 g_string_free(self
->source
, TRUE
);
135 g_string_free(self
->author
, TRUE
);
138 g_string_free(self
->comment
, TRUE
);
139 self
->comment
= NULL
;
141 g_string_free(self
->preview_button_name
, TRUE
);
142 self
->preview_button_name
= NULL
;
144 irreco_string_table_free(self
->backgrounds
);
145 self
->backgrounds
= NULL
;
147 irreco_string_table_free(self
->buttons
);
148 self
->buttons
= NULL
;
150 g_slice_free(IrrecoTheme
, self
);
158 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
159 /* Private Functions */
160 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
163 * @name Private Functions
170 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
171 /* Public Functions */
172 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
175 * @name Public Functions
178 void irreco_theme_update_keyfile(IrrecoTheme
*self
)
180 GString
*keyfile_path
= g_string_new(self
->path
->str
);
181 GKeyFile
*keyfile
= g_key_file_new();
184 g_string_append(keyfile_path
, "/theme.conf");
186 irreco_gkeyfile_set_string(keyfile
, "theme" , "name",
189 if (self
->source
->len
> 0) {
190 irreco_gkeyfile_set_string(keyfile
, "theme" , "source",
194 if (self
->author
->len
> 0) {
195 irreco_gkeyfile_set_string(keyfile
, "theme", "author",
199 if (self
->preview_button_name
->len
> 0) {
200 irreco_gkeyfile_set_string(keyfile
, "theme", "preview-button",
201 self
->preview_button_name
->str
);
204 if (self
->version
->len
> 0) {
205 irreco_gkeyfile_set_string(keyfile
, "theme", "version",
209 if (self
->comment
->len
> 0) {
210 irreco_gkeyfile_set_string(keyfile
, "theme", "comment",
214 irreco_write_keyfile(keyfile
, keyfile_path
->str
);
216 g_key_file_free(keyfile
);
217 g_string_free(keyfile_path
, TRUE
);
221 void irreco_theme_print(IrrecoTheme
*self
)
225 IRRECO_DEBUG("Themename: %s \n", self
->name
->str
);
226 IRRECO_DEBUG("Folder: %s \n", self
->path
->str
);
227 IRRECO_DEBUG("Source: %s \n", self
->source
->str
);
228 IRRECO_DEBUG("Author: %s \n", self
->author
->str
);
229 IRRECO_DEBUG("Comment: %s \n", self
->comment
->str
);
230 IRRECO_DEBUG("Previewbutton: %s \n", self
->preview_button_name
->str
);
231 IRRECO_DEBUG("Version: %s \n", self
->version
->str
);
232 irreco_string_table_print(self
->backgrounds
);
233 irreco_string_table_print(self
->buttons
);
238 void irreco_theme_read_button_keyfile_foreach(IrrecoDirForeachData
* dir_data
)
240 IrrecoTheme
*self
= (IrrecoTheme
*) dir_data
->user_data_1
;
241 IrrecoKeyFile
*keyfile
;
246 gchar
*text_format_up
= NULL
;
247 gchar
*text_format_down
= NULL
;
248 gint text_padding
= 5;
249 gfloat text_h_align
= 0.5;
250 gfloat text_v_align
= 0.5;
251 IrrecoThemeButton
*theme_button
;
252 GString
*style_name
= g_string_new(self
->name
->str
);
256 keyfile
= irreco_keyfile_create(dir_data
->directory
,
259 if (keyfile
== NULL
) goto end
;
261 /* Required fields. */
262 if (!irreco_keyfile_get_str(keyfile
, "name", &name
) ||
263 !irreco_keyfile_get_path(keyfile
, "up", &up
)) {
264 IRRECO_PRINTF("Could not read style from group \"%s\"\n",
269 /* Optional fields. */
270 irreco_keyfile_get_path(keyfile
, "down", &down
);
271 irreco_keyfile_get_bool(keyfile
, "allow-text", &allow_text
);
272 irreco_keyfile_get_str(keyfile
, "text-format-up", &text_format_up
);
273 irreco_keyfile_get_str(keyfile
, "text-format-down", &text_format_down
);
274 irreco_keyfile_get_int(keyfile
, "text-padding", &text_padding
);
275 irreco_keyfile_get_float(keyfile
, "text-h-align", &text_h_align
);
276 irreco_keyfile_get_float(keyfile
, "text-v-align", &text_v_align
);
278 g_string_append_printf(style_name
,"/%s", name
);
280 if (irreco_string_table_get(self
->buttons
, style_name
->str
,
281 (gpointer
*) &theme_button
)) {
282 irreco_theme_button_set(theme_button
, style_name
->str
,
285 text_format_up
, text_format_down
,
290 theme_button
= irreco_theme_button_new(self
->name
->str
);
291 irreco_theme_button_set(theme_button
, style_name
->str
,
294 text_format_up
, text_format_down
,
298 irreco_string_table_add(self
->buttons
,
299 theme_button
->style_name
->str
,
303 irreco_theme_button_print(theme_button
);
306 if(keyfile
!= NULL
) irreco_keyfile_destroy(keyfile
);
307 if(name
!= NULL
) g_free(name
);
308 if(name
!= NULL
) g_free(up
);
309 if(name
!= NULL
) g_free(down
);
310 if(name
!= NULL
) g_free(text_format_up
);
311 if(name
!= NULL
) g_free(text_format_down
);
312 g_string_free(style_name
, TRUE
);
318 void irreco_theme_read_bg_keyfile_foreach(IrrecoDirForeachData
* dir_data
)
320 IrrecoTheme
*self
= (IrrecoTheme
*) dir_data
->user_data_1
;
321 IrrecoKeyFile
*keyfile
;
327 keyfile
= irreco_keyfile_create(dir_data
->directory
,
330 if (keyfile
== NULL
) goto end
;
332 /* Required fields. */
333 if (!irreco_keyfile_get_str(keyfile
, "name", &name
) ||
334 !irreco_keyfile_get_path(keyfile
, "image", &image
)) {
335 IRRECO_PRINTF("Could not read style from group \"%s\"\n",
339 if (irreco_string_table_get(self
->backgrounds
, name
,
341 irreco_theme_bg_set(bg
, name
, image
);
343 bg
= irreco_theme_bg_new();
344 irreco_theme_bg_set(bg
, name
, image
);
345 irreco_string_table_add(self
->backgrounds
, name
, bg
);
348 irreco_theme_bg_print(bg
);
351 if(keyfile
!= NULL
) irreco_keyfile_destroy(keyfile
);
352 if(name
!= NULL
) g_free(name
);
353 if(image
!= NULL
) g_free(image
);
357 IrrecoStringTable
* irreco_theme_get_buttons(IrrecoTheme
*self
)
360 IRRECO_RETURN_PTR(self
->buttons
);
363 IrrecoThemeButton
*irreco_theme_get_button(IrrecoTheme
*self
,
364 const char *button_name
)
366 IrrecoThemeButton
*button
= NULL
;
368 IRRECO_STRING_TABLE_FOREACH_DATA(self
->buttons
, IrrecoThemeButton
*,
370 if (g_utf8_collate(pointer
->name
->str
, button_name
) == 0) {
373 IRRECO_STRING_TABLE_FOREACH_END
374 IRRECO_RETURN_PTR(button
);
377 IrrecoStringTable
* irreco_theme_get_backgrounds(IrrecoTheme
*self
)
380 IRRECO_RETURN_PTR(self
->backgrounds
);
383 IrrecoThemeBg
*irreco_theme_get_background(IrrecoTheme
*self
,
386 IrrecoThemeBg
*bg
= NULL
;
388 IRRECO_STRING_TABLE_FOREACH_DATA(self
->backgrounds
, IrrecoThemeBg
*,
390 if (g_utf8_collate(pointer
->image_name
->str
, bg_name
) == 0) {
393 IRRECO_STRING_TABLE_FOREACH_END
394 IRRECO_RETURN_PTR(bg
);
397 void irreco_theme_set_author(IrrecoTheme
*self
, const char * author
)
400 if (author
!= NULL
) {
401 g_string_printf(self
->author
, "%s", author
);
403 irreco_theme_update_keyfile(self
);
408 void irreco_theme_set_comment(IrrecoTheme
*self
, const char * comment
)
411 if (comment
!= NULL
) {
412 g_string_printf(self
->comment
, "%s", comment
);
414 irreco_theme_update_keyfile(self
);
419 void irreco_theme_set_preview_button(IrrecoTheme
*self
,
420 const char * button_name
)
423 if (button_name
!= NULL
) {
424 g_string_printf(self
->preview_button_name
, "%s", button_name
);
426 irreco_theme_update_keyfile(self
);
433 /* This function will work after IrrecoButtonStyle destruction*/
435 void irreco_theme_set_name(IrrecoTheme
*self
, IrrecoData
*irreco_data
,
440 GString
*style_name
= g_string_new("");
443 *TODO Move this part to IrrecoThemeManager and call this
444 *funtion from ThemeManager*/
446 irreco_string_table_change_key(
447 irreco_data
->theme_manager
->themes
,
448 self
->name
->str
, name
);
450 g_string_printf(self
->name
, "%s", name
);
452 IRRECO_STRING_TABLE_FOREACH_DATA(self
->buttons
,
453 IrrecoThemeButton
*, button
) {
454 g_string_printf(button
->style_name
,"%s/%s",
457 IRRECO_PRINTF("style: %s\n",button
->style_name
->str
);
460 IRRECO_STRING_TABLE_FOREACH_END
462 irreco_theme_update_keyfile(self
);
463 irreco_config_save_layouts(irreco_data
);
464 g_string_free(style_name
, TRUE
);
470 void irreco_theme_set(IrrecoTheme
*self
, const char *name
, const char *path
,
471 const char *source
, const char *author
,
472 const char *comment
, const char *preview_button_name
,
478 g_string_printf(self
->name
, "%s", name
);
480 g_string_erase(self
->name
, 0, -1);
484 g_string_printf(self
->path
, "%s", path
);
486 g_string_erase(self
->path
, 0, -1);
489 if (source
!= NULL
) {
490 g_string_printf(self
->source
, "%s", source
);
492 g_string_erase(self
->source
, 0, -1);
495 if (author
!= NULL
) {
496 g_string_printf(self
->author
, "%s", author
);
498 g_string_erase(self
->author
, 0, -1);
501 if (comment
!= NULL
) {
502 g_string_printf(self
->comment
, "%s", comment
);
504 g_string_erase(self
->comment
, 0, -1);
507 if (preview_button_name
!= NULL
) {
508 g_string_printf(self
->preview_button_name
, "%s",
509 preview_button_name
);
511 g_string_erase(self
->preview_button_name
, 0, -1);
514 if (version
!= NULL
) {
515 g_string_printf(self
->version
, "%s", version
);
517 g_string_erase(self
->version
, 0, -1);
520 irreco_theme_update_keyfile(self
);
524 IrrecoDirForeachData button_styles
;
525 GString
* directory
= g_string_new("");
527 g_string_printf(directory
, "%s/buttons/", path
);
528 IRRECO_DEBUG("Directory = %s\n", directory
->str
);
529 button_styles
.directory
= directory
->str
;
531 button_styles
.filesuffix
= "button.conf";
532 button_styles
.user_data_1
= self
;
534 irreco_dir_foreach_subdirectories(&button_styles
,
535 irreco_theme_read_button_keyfile_foreach
);
537 g_string_free(directory
, TRUE
);
540 irreco_string_table_sort_abc(self
->buttons
);
543 /* Update backgrounds */
545 IrrecoDirForeachData bg_styles
;
546 GString
* directory
= g_string_new("");
548 g_string_printf(directory
, "%s/bg/", path
);
549 IRRECO_DEBUG("Directory = %s\n", directory
->str
);
550 bg_styles
.directory
= directory
->str
;
552 bg_styles
.filesuffix
= "bg.conf";
553 bg_styles
.user_data_1
= self
;
555 irreco_dir_foreach_subdirectories(&bg_styles
,
556 irreco_theme_read_bg_keyfile_foreach
);
558 g_string_free(directory
, TRUE
);
561 irreco_string_table_sort_abc(self
->backgrounds
);
567 void irreco_theme_check(IrrecoTheme
*self
)
571 /* Check if some background is deleted */
572 IRRECO_STRING_TABLE_FOREACH(self
->backgrounds
, key
, IrrecoThemeBg
*,
574 if(!irreco_is_file(theme_bg
->image_path
->str
)) {
575 irreco_string_table_remove(self
->backgrounds
, key
);
577 IRRECO_STRING_TABLE_FOREACH_END
579 /* Check if some button is deleted */
580 IRRECO_STRING_TABLE_FOREACH(self
->buttons
, key
, IrrecoThemeButton
*,
582 if(!irreco_is_file(theme_button
->image_up
->str
)) {
583 irreco_string_table_remove(self
->buttons
, key
);
585 IRRECO_STRING_TABLE_FOREACH_END
590 IrrecoTheme
*irreco_theme_copy(IrrecoTheme
*self
)
592 IrrecoTheme
*new = NULL
;
596 new = irreco_theme_new(self
->name
->str
, self
->path
->str
,
597 self
->source
->str
, self
->author
->str
,
598 self
->comment
->str
, self
->preview_button_name
->str
,
601 irreco_theme_set(new, self
->name
->str
, self
->path
->str
,
602 self
->source
->str
, self
->author
->str
,
603 self
->comment
->str
, self
->preview_button_name
->str
,
607 IRRECO_STRING_TABLE_FOREACH(self
->backgrounds
, key
, IrrecoThemeBg
*,
609 irreco_string_table_add(new->backgrounds
, key
,
610 irreco_theme_bg_copy(theme_bg
));
612 IRRECO_STRING_TABLE_FOREACH_END
615 IRRECO_STRING_TABLE_FOREACH(self
->buttons
, key
, IrrecoThemeButton
*,
617 irreco_string_table_add(new->buttons
, key
,
618 irreco_theme_button_copy(theme_button
));
620 IRRECO_STRING_TABLE_FOREACH_END
623 IRRECO_RETURN_PTR(new);
629 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
630 /* Events and Callbacks */
631 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/