clean code
[irreco.git] / irreco / src / core / irreco_theme.c
blob6754837118fe5b08382853c5b690a8ea8613e267
1 /*
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"
23 /**
24 * @addtogroup IrrecoTheme
25 * @ingroup Irreco
27 * Contains information of theme.
29 * @{
32 /**
33 * @file
34 * Source file of @ref IrrecoTheme.
37 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
38 /* Prototypes */
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 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
48 /**
49 * @name Construction & Destruction
50 * @{
53 /**
54 * Create new theme
56 IrrecoTheme *irreco_theme_new()
58 IrrecoTheme *self;
59 IRRECO_ENTER
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);
79 # if 0
80 IrrecoTheme *irreco_theme_new()
82 IrrecoTheme *self;
83 IRRECO_ENTER
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);
101 /*Get buttons*/
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);
117 directory = NULL;
119 irreco_string_table_sort_abc(self->buttons);
122 /*Get backgrounds*/
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);
138 directory = NULL;
140 irreco_string_table_sort_abc(self->backgrounds);
143 IRRECO_RETURN_PTR(self);
145 #endif
146 void irreco_theme_free(IrrecoTheme *self)
148 IRRECO_ENTER
150 g_assert(self != NULL);
152 g_string_free(self->name, TRUE);
153 self->name = NULL;
155 g_string_free(self->path, TRUE);
156 self->path = NULL;
158 g_string_free(self->source, TRUE);
159 self->source = NULL;
161 g_string_free(self->author, TRUE);
162 self->author = NULL;
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);
177 self = NULL;
179 IRRECO_RETURN
182 /** @} */
184 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
185 /* Private Functions */
186 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
189 * @name Private Functions
190 * @{
194 /** @} */
196 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
197 /* Public Functions */
198 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
201 * @name Public Functions
202 * @{
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();
208 IRRECO_ENTER
210 g_string_append(keyfile_path, "/theme.conf");
212 irreco_gkeyfile_set_string(keyfile, "theme" , "name",
213 self->name->str);
215 if (self->source->len > 0) {
216 irreco_gkeyfile_set_string(keyfile, "theme" , "source",
217 self->source->str);
220 if (self->author->len > 0) {
221 irreco_gkeyfile_set_string(keyfile, "theme", "author",
222 self->author->str);
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",
232 self->version->str);
235 if (self->comment->len > 0) {
236 irreco_gkeyfile_set_string(keyfile, "theme", "comment",
237 self->comment->str);
240 irreco_write_keyfile(keyfile, keyfile_path->str);
242 g_key_file_free(keyfile);
243 g_string_free(keyfile_path, TRUE);
244 IRRECO_RETURN
247 void irreco_theme_print(IrrecoTheme *self)
249 IRRECO_ENTER
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);
261 IRRECO_RETURN
264 void irreco_theme_read_button_keyfile_foreach(IrrecoDirForeachData * dir_data)
266 IrrecoTheme *self = (IrrecoTheme*) dir_data->user_data_1;
267 IrrecoThemeButton *button = NULL;
268 IRRECO_ENTER
270 button = irreco_theme_button_new_from_dir(dir_data->directory,
271 self->name->str);
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",
276 button->name->str);
277 irreco_theme_button_free(button);
278 } else {
279 irreco_string_table_add(self->buttons,
280 button->style_name->str, button);
283 IRRECO_RETURN
286 void irreco_theme_read_bg_keyfile_foreach(IrrecoDirForeachData * dir_data)
288 IrrecoTheme *self = (IrrecoTheme*) dir_data->user_data_1;
289 IrrecoThemeBg *bg = NULL;
290 IRRECO_ENTER
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);
299 } else {
300 irreco_string_table_add(self->backgrounds,
301 bg->image_name->str, bg);
303 IRRECO_RETURN
306 IrrecoStringTable* irreco_theme_get_buttons(IrrecoTheme *self)
308 IRRECO_ENTER
309 IRRECO_RETURN_PTR(self->buttons);
312 IrrecoThemeButton *irreco_theme_get_button(IrrecoTheme *self,
313 const char *button_name)
315 IrrecoThemeButton *button = NULL;
316 IRRECO_ENTER
317 IRRECO_STRING_TABLE_FOREACH_DATA(self->buttons, IrrecoThemeButton *,
318 pointer)
319 if (g_utf8_collate(pointer->name->str, button_name) == 0) {
320 button = pointer;
322 IRRECO_STRING_TABLE_FOREACH_END
323 IRRECO_RETURN_PTR(button);
326 IrrecoStringTable* irreco_theme_get_backgrounds(IrrecoTheme *self)
328 IRRECO_ENTER
329 IRRECO_RETURN_PTR(self->backgrounds);
332 IrrecoThemeBg *irreco_theme_get_background(IrrecoTheme *self,
333 const char *bg_name)
335 IrrecoThemeBg *bg = NULL;
336 IRRECO_ENTER
337 IRRECO_STRING_TABLE_FOREACH_DATA(self->backgrounds, IrrecoThemeBg *,
338 pointer)
339 if (g_utf8_collate(pointer->image_name->str, bg_name) == 0) {
340 bg = pointer;
342 IRRECO_STRING_TABLE_FOREACH_END
343 IRRECO_RETURN_PTR(bg);
346 void irreco_theme_set_author(IrrecoTheme *self, const char * author)
348 IRRECO_ENTER
349 if (author != NULL) {
350 g_string_printf(self->author, "%s", author);
352 irreco_theme_update_keyfile(self);
354 IRRECO_RETURN
357 void irreco_theme_set_comment(IrrecoTheme *self, const char * comment)
359 IRRECO_ENTER
360 if (comment != NULL) {
361 g_string_printf(self->comment, "%s", comment);
363 irreco_theme_update_keyfile(self);
365 IRRECO_RETURN
368 void irreco_theme_set_preview_button(IrrecoTheme *self,
369 const char * button_name)
371 IRRECO_ENTER
372 if (button_name != NULL) {
373 g_string_printf(self->preview_button_name, "%s", button_name);
375 irreco_theme_update_keyfile(self);
377 IRRECO_RETURN
381 #if 0
382 /* This function will work after IrrecoButtonStyle destruction*/
384 void irreco_theme_set_name(IrrecoTheme *self, IrrecoData *irreco_data,
385 const char * name)
387 IRRECO_ENTER
388 if (name != NULL) {
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",
404 self->name->str,
405 button->name->str);
406 IRRECO_PRINTF("style: %s\n",button->style_name->str);
407 IRRECO_PAUSE
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);
415 IRRECO_RETURN
417 #endif
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,
422 const char *version)
424 IRRECO_ENTER
426 if (name != NULL) {
427 g_string_printf(self->name, "%s", name);
428 } else {
429 g_string_erase(self->name, 0, -1);
432 if (path != NULL) {
433 g_string_printf(self->path, "%s", path);
434 } else {
435 g_string_erase(self->path, 0, -1);
438 if (source != NULL) {
439 g_string_printf(self->source, "%s", source);
440 } else {
441 g_string_erase(self->source, 0, -1);
444 if (author != NULL) {
445 g_string_printf(self->author, "%s", author);
446 } else {
447 g_string_erase(self->author, 0, -1);
450 if (comment != NULL) {
451 g_string_printf(self->comment, "%s", comment);
452 } else {
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);
459 } else {
460 g_string_erase(self->preview_button_name, 0, -1);
463 if (version != NULL) {
464 g_string_printf(self->version, "%s", version);
465 } else {
466 g_string_erase(self->version, 0, -1);
469 irreco_theme_update_keyfile(self);
471 /* Update buttons */
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);
487 directory = NULL;
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);
508 directory = NULL;
510 irreco_string_table_sort_abc(self->backgrounds);
513 IRRECO_RETURN
516 void irreco_theme_check(IrrecoTheme *self)
518 IRRECO_ENTER
520 /* Check if some background is deleted */
521 IRRECO_STRING_TABLE_FOREACH(self->backgrounds, key, IrrecoThemeBg *,
522 theme_bg)
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 *,
530 theme_button)
531 if(!irreco_is_file(theme_button->image_up->str)) {
532 irreco_string_table_remove(self->buttons, key);
534 IRRECO_STRING_TABLE_FOREACH_END
536 IRRECO_RETURN
539 IrrecoTheme *irreco_theme_copy(IrrecoTheme *self)
541 IrrecoTheme *new = NULL;
543 IRRECO_ENTER
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,
550 self->version->str);
553 IRRECO_STRING_TABLE_FOREACH(self->backgrounds, key, IrrecoThemeBg *,
554 theme_bg)
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 *,
562 theme_button)
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;
580 IRRECO_ENTER
581 self = irreco_theme_new();
582 irreco_theme_read(self, dir);
583 IRRECO_RETURN_PTR(self);
586 void irreco_theme_read(IrrecoTheme *self, const gchar *dir)
588 IrrecoKeyFile *keyfile = NULL;
589 char *name = NULL;
590 char *source = NULL;
591 char *author = NULL;
592 char *comment = NULL;
593 char *preview_button = NULL;
594 char *version = NULL;
595 GString *conf = NULL;
596 IRRECO_ENTER
598 conf = g_string_new(dir);
599 g_string_append_printf(conf, "/theme.conf");
600 keyfile = irreco_keyfile_create(dir,
601 conf->str,
602 "theme");
604 /* Required fields. */
605 irreco_keyfile_get_str(keyfile, "name", &name);
607 /* Optional fields. */
608 irreco_keyfile_get_str(keyfile, "source", &source);
609 irreco_keyfile_get_str(keyfile, "author", &author);
610 irreco_keyfile_get_str(keyfile, "comment", &comment);
611 irreco_keyfile_get_str(keyfile, "preview-button", &preview_button);
612 irreco_keyfile_get_str(keyfile, "version", &version);
614 /* call irreco_theme_set() */
615 irreco_theme_set(self, name, dir, source,
616 author, comment, preview_button, version);
618 g_string_free(conf, TRUE);
619 if (keyfile != NULL) irreco_keyfile_destroy(keyfile);
620 if (name != NULL) g_free(name);
621 if (source != NULL) g_free(source);
622 if (author != NULL) g_free(author);
623 if (comment != NULL) g_free(comment);
624 if (preview_button != NULL) g_free(preview_button);
625 if (version != NULL) g_free(version);
626 IRRECO_RETURN
630 gboolean irreco_theme_save(IrrecoTheme *self,
631 const gchar *theme_path)
633 gboolean rvalue = FALSE;
634 IrrecoTheme *theme;
635 GString *path;
636 IrrecoStringTable *bg_list = NULL;
637 IrrecoStringTable *button_list = NULL;
638 IRRECO_ENTER
640 /*Create new theme*/
641 theme = irreco_theme_new();
642 irreco_theme_set(theme,
643 self->name->str,
644 theme_path,
645 self->source->str,
646 self->author->str,
647 self->comment->str,
648 self->preview_button_name->str,
649 NULL);
651 irreco_theme_update_keyfile(theme);
654 /* Get buttons and backgrounds */
655 /* Get backrounds */
656 bg_list = irreco_theme_get_backgrounds(self);
658 path = g_string_new("");
659 g_string_printf(path, "%s/bg", theme_path);
660 g_mkdir(path->str, 0777);
662 IRRECO_STRING_TABLE_FOREACH_DATA(bg_list, IrrecoThemeBg *, background)
664 irreco_theme_bg_print(background);
666 irreco_theme_bg_save(background, path->str);
669 IRRECO_STRING_TABLE_FOREACH_END
671 /* Get buttons */
672 button_list = irreco_theme_get_buttons(self);
674 g_string_printf(path, "%s/buttons", theme_path);
675 g_mkdir(path->str, 0777);
677 IRRECO_STRING_TABLE_FOREACH_DATA(button_list, IrrecoThemeButton *, button)
679 if (g_str_equal(self->preview_button_name->str,
680 button->image_up->str) && !rvalue) {
681 irreco_theme_set_preview_button(theme,
682 button->name->str);
683 rvalue = TRUE;
686 irreco_theme_button_save(button, path->str);
687 irreco_theme_button_print(button);
689 IRRECO_STRING_TABLE_FOREACH_END
693 g_string_free(path, TRUE);
694 irreco_theme_free(theme);
696 IRRECO_RETURN_BOOL(rvalue);
700 /** @} */
702 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
703 /* Events and Callbacks */
704 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
706 /** @} */