created new functions inside irreco_theme.c
[irreco.git] / irreco / src / core / irreco_theme.c
blobe7f2215594edf4db06d7ac799e7f4ddf7c2b324d
1 /*
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"
22 /**
23 * @addtogroup IrrecoTheme
24 * @ingroup Irreco
26 * Contains information of theme.
28 * @{
31 /**
32 * @file
33 * Source file of @ref IrrecoTheme.
36 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
37 /* Prototypes */
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 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
47 /**
48 * @name Construction & Destruction
49 * @{
52 /**
53 * Create new theme
55 IrrecoTheme *theme_new()
57 IrrecoTheme *self;
58 IRRECO_ENTER
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,
82 const char *comment,
83 const char *preview_button_name,
84 const char *version)
86 IrrecoTheme *self;
87 IRRECO_ENTER
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);
105 /*Get buttons*/
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);
121 directory = NULL;
123 irreco_string_table_sort_abc(self->buttons);
126 /*Get backgrounds*/
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);
142 directory = NULL;
144 irreco_string_table_sort_abc(self->backgrounds);
147 IRRECO_RETURN_PTR(self);
150 void irreco_theme_free(IrrecoTheme *self)
152 IRRECO_ENTER
154 g_assert(self != NULL);
156 g_string_free(self->name, TRUE);
157 self->name = NULL;
159 g_string_free(self->path, TRUE);
160 self->path = NULL;
162 g_string_free(self->source, TRUE);
163 self->source = NULL;
165 g_string_free(self->author, TRUE);
166 self->author = NULL;
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);
181 self = NULL;
183 IRRECO_RETURN
186 /** @} */
188 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
189 /* Private Functions */
190 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
193 * @name Private Functions
194 * @{
198 /** @} */
200 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
201 /* Public Functions */
202 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
205 * @name Public Functions
206 * @{
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();
212 IRRECO_ENTER
214 g_string_append(keyfile_path, "/theme.conf");
216 irreco_gkeyfile_set_string(keyfile, "theme" , "name",
217 self->name->str);
219 if (self->source->len > 0) {
220 irreco_gkeyfile_set_string(keyfile, "theme" , "source",
221 self->source->str);
224 if (self->author->len > 0) {
225 irreco_gkeyfile_set_string(keyfile, "theme", "author",
226 self->author->str);
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",
236 self->version->str);
239 if (self->comment->len > 0) {
240 irreco_gkeyfile_set_string(keyfile, "theme", "comment",
241 self->comment->str);
244 irreco_write_keyfile(keyfile, keyfile_path->str);
246 g_key_file_free(keyfile);
247 g_string_free(keyfile_path, TRUE);
248 IRRECO_RETURN
251 void irreco_theme_print(IrrecoTheme *self)
253 IRRECO_ENTER
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);
265 IRRECO_RETURN
268 void irreco_theme_read_button_keyfile_foreach(IrrecoDirForeachData * dir_data)
270 IrrecoTheme *self = (IrrecoTheme*) dir_data->user_data_1;
271 IrrecoKeyFile *keyfile;
272 gchar *name = NULL;
273 gchar *up = NULL;
274 gchar *down = NULL;
275 gboolean allow_text;
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);
284 IRRECO_ENTER
286 keyfile = irreco_keyfile_create(dir_data->directory,
287 dir_data->filepath,
288 "theme-button");
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",
295 keyfile->group);
296 goto end;
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,
313 name, allow_text,
314 up, down,
315 text_format_up, text_format_down,
316 text_padding,
317 text_h_align,
318 text_v_align);
319 } else {
320 theme_button = irreco_theme_button_new(self->name->str);
321 irreco_theme_button_set(theme_button, style_name->str,
322 name, allow_text,
323 up, down,
324 text_format_up, text_format_down,
325 text_padding,
326 text_h_align,
327 text_v_align);
328 irreco_string_table_add(self->buttons,
329 theme_button->style_name->str,
330 theme_button);
333 irreco_theme_button_print(theme_button);
335 end:
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);
345 IRRECO_RETURN
348 void irreco_theme_read_bg_keyfile_foreach(IrrecoDirForeachData * dir_data)
350 IrrecoTheme *self = (IrrecoTheme*) dir_data->user_data_1;
351 IrrecoKeyFile *keyfile;
352 IrrecoThemeBg *bg;
353 char *name = NULL;
354 char *image = NULL;
355 IRRECO_ENTER
357 keyfile = irreco_keyfile_create(dir_data->directory,
358 dir_data->filepath,
359 "theme-bg");
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",
366 keyfile->group);
367 goto end;
369 if (irreco_string_table_get(self->backgrounds, name,
370 (gpointer *) &bg)) {
371 irreco_theme_bg_set(bg, name, image);
372 } else {
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);
380 end:
381 if(keyfile != NULL) irreco_keyfile_destroy(keyfile);
382 if(name != NULL) g_free(name);
383 if(image != NULL) g_free(image);
384 IRRECO_RETURN
387 IrrecoStringTable* irreco_theme_get_buttons(IrrecoTheme *self)
389 IRRECO_ENTER
390 IRRECO_RETURN_PTR(self->buttons);
393 IrrecoThemeButton *irreco_theme_get_button(IrrecoTheme *self,
394 const char *button_name)
396 IrrecoThemeButton *button = NULL;
397 IRRECO_ENTER
398 IRRECO_STRING_TABLE_FOREACH_DATA(self->buttons, IrrecoThemeButton *,
399 pointer)
400 if (g_utf8_collate(pointer->name->str, button_name) == 0) {
401 button = pointer;
403 IRRECO_STRING_TABLE_FOREACH_END
404 IRRECO_RETURN_PTR(button);
407 IrrecoStringTable* irreco_theme_get_backgrounds(IrrecoTheme *self)
409 IRRECO_ENTER
410 IRRECO_RETURN_PTR(self->backgrounds);
413 IrrecoThemeBg *irreco_theme_get_background(IrrecoTheme *self,
414 const char *bg_name)
416 IrrecoThemeBg *bg = NULL;
417 IRRECO_ENTER
418 IRRECO_STRING_TABLE_FOREACH_DATA(self->backgrounds, IrrecoThemeBg *,
419 pointer)
420 if (g_utf8_collate(pointer->image_name->str, bg_name) == 0) {
421 bg = pointer;
423 IRRECO_STRING_TABLE_FOREACH_END
424 IRRECO_RETURN_PTR(bg);
427 void irreco_theme_set_author(IrrecoTheme *self, const char * author)
429 IRRECO_ENTER
430 if (author != NULL) {
431 g_string_printf(self->author, "%s", author);
433 irreco_theme_update_keyfile(self);
435 IRRECO_RETURN
438 void irreco_theme_set_comment(IrrecoTheme *self, const char * comment)
440 IRRECO_ENTER
441 if (comment != NULL) {
442 g_string_printf(self->comment, "%s", comment);
444 irreco_theme_update_keyfile(self);
446 IRRECO_RETURN
449 void irreco_theme_set_preview_button(IrrecoTheme *self,
450 const char * button_name)
452 IRRECO_ENTER
453 if (button_name != NULL) {
454 g_string_printf(self->preview_button_name, "%s", button_name);
456 irreco_theme_update_keyfile(self);
458 IRRECO_RETURN
462 #if 0
463 /* This function will work after IrrecoButtonStyle destruction*/
465 void irreco_theme_set_name(IrrecoTheme *self, IrrecoData *irreco_data,
466 const char * name)
468 IRRECO_ENTER
469 if (name != NULL) {
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",
485 self->name->str,
486 button->name->str);
487 IRRECO_PRINTF("style: %s\n",button->style_name->str);
488 IRRECO_PAUSE
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);
496 IRRECO_RETURN
498 #endif
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,
503 const char *version)
505 IRRECO_ENTER
507 if (name != NULL) {
508 g_string_printf(self->name, "%s", name);
509 } else {
510 g_string_erase(self->name, 0, -1);
513 if (path != NULL) {
514 g_string_printf(self->path, "%s", path);
515 } else {
516 g_string_erase(self->path, 0, -1);
519 if (source != NULL) {
520 g_string_printf(self->source, "%s", source);
521 } else {
522 g_string_erase(self->source, 0, -1);
525 if (author != NULL) {
526 g_string_printf(self->author, "%s", author);
527 } else {
528 g_string_erase(self->author, 0, -1);
531 if (comment != NULL) {
532 g_string_printf(self->comment, "%s", comment);
533 } else {
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);
540 } else {
541 g_string_erase(self->preview_button_name, 0, -1);
544 if (version != NULL) {
545 g_string_printf(self->version, "%s", version);
546 } else {
547 g_string_erase(self->version, 0, -1);
550 irreco_theme_update_keyfile(self);
552 /* Update buttons */
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);
568 directory = NULL;
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);
589 directory = NULL;
591 irreco_string_table_sort_abc(self->backgrounds);
594 IRRECO_RETURN
597 void irreco_theme_check(IrrecoTheme *self)
599 IRRECO_ENTER
601 /* Check if some background is deleted */
602 IRRECO_STRING_TABLE_FOREACH(self->backgrounds, key, IrrecoThemeBg *,
603 theme_bg)
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 *,
611 theme_button)
612 if(!irreco_is_file(theme_button->image_up->str)) {
613 irreco_string_table_remove(self->buttons, key);
615 IRRECO_STRING_TABLE_FOREACH_END
617 IRRECO_RETURN
620 IrrecoTheme *irreco_theme_copy(IrrecoTheme *self)
622 IrrecoTheme *new = NULL;
624 IRRECO_ENTER
626 new = theme_new();
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,
631 self->version->str);
634 IRRECO_STRING_TABLE_FOREACH(self->backgrounds, key, IrrecoThemeBg *,
635 theme_bg)
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 *,
643 theme_button)
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;
661 IRRECO_ENTER
663 self = theme_new();
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;
671 char *name = NULL;
672 char *source = NULL;
673 char *author = NULL;
674 char *comment = NULL;
675 char *preview_button = NULL;
676 char *version = NULL;
677 GString *conf = NULL;
678 IRRECO_ENTER
680 conf = g_string_new(dir);
681 g_string_append_printf(conf, "/theme.conf");
682 keyfile = irreco_keyfile_create(dir,
683 conf->str,
684 "theme");
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);
700 IRRECO_PAUSE
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);
713 IRRECO_RETURN
719 /** @} */
721 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
722 /* Events and Callbacks */
723 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
725 /** @} */