Clean code
[irreco.git] / irreco / src / core / irreco_theme.c
blobc098e131ace7e0aa024d9ea16e9c052ae956ded3
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);
76 IRRECO_RETURN_PTR(self);
80 # if 0
81 IrrecoTheme *irreco_theme_new()
83 IrrecoTheme *self;
84 IRRECO_ENTER
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);
102 /*Get buttons*/
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);
118 directory = NULL;
120 irreco_string_table_sort_abc(self->buttons);
123 /*Get backgrounds*/
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);
139 directory = NULL;
141 irreco_string_table_sort_abc(self->backgrounds);
144 IRRECO_RETURN_PTR(self);
146 #endif
147 void irreco_theme_free(IrrecoTheme *self)
149 IRRECO_ENTER
151 g_assert(self != NULL);
153 g_string_free(self->name, TRUE);
154 self->name = NULL;
156 g_string_free(self->path, TRUE);
157 self->path = NULL;
159 g_string_free(self->source, TRUE);
160 self->source = NULL;
162 g_string_free(self->author, TRUE);
163 self->author = NULL;
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);
178 self = NULL;
180 IRRECO_RETURN
183 /** @} */
185 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
186 /* Private Functions */
187 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
190 * @name Private Functions
191 * @{
195 /** @} */
197 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
198 /* Public Functions */
199 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
202 * @name Public Functions
203 * @{
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();
209 IRRECO_ENTER
211 g_string_append(keyfile_path, "/theme.conf");
213 irreco_gkeyfile_set_string(keyfile, "theme" , "name",
214 self->name->str);
216 if (self->source->len > 0) {
217 irreco_gkeyfile_set_string(keyfile, "theme" , "source",
218 self->source->str);
221 if (self->author->len > 0) {
222 irreco_gkeyfile_set_string(keyfile, "theme", "author",
223 self->author->str);
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",
233 self->version->str);
236 if (self->comment->len > 0) {
237 irreco_gkeyfile_set_string(keyfile, "theme", "comment",
238 self->comment->str);
241 irreco_write_keyfile(keyfile, keyfile_path->str);
243 g_key_file_free(keyfile);
244 g_string_free(keyfile_path, TRUE);
245 IRRECO_RETURN
248 void irreco_theme_print(IrrecoTheme *self)
250 IRRECO_ENTER
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);
262 IRRECO_RETURN
265 void irreco_theme_read_button_keyfile_foreach(IrrecoDirForeachData * dir_data)
267 IrrecoTheme *self = (IrrecoTheme*) dir_data->user_data_1;
268 IrrecoThemeButton *button = NULL;
269 IRRECO_ENTER
271 button = irreco_theme_button_new_from_dir(dir_data->directory,
272 self->name->str);
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",
277 button->name->str);
278 irreco_theme_button_free(button);
279 } else {
280 irreco_string_table_add(self->buttons,
281 button->style_name->str, button);
284 IRRECO_RETURN
287 void irreco_theme_read_bg_keyfile_foreach(IrrecoDirForeachData * dir_data)
289 IrrecoTheme *self = (IrrecoTheme*) dir_data->user_data_1;
290 IrrecoThemeBg *bg = NULL;
291 IRRECO_ENTER
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);
300 } else {
301 irreco_string_table_add(self->backgrounds,
302 bg->image_name->str, bg);
304 IRRECO_RETURN
307 IrrecoStringTable* irreco_theme_get_buttons(IrrecoTheme *self)
309 IRRECO_ENTER
310 IRRECO_RETURN_PTR(self->buttons);
313 IrrecoThemeButton *irreco_theme_get_button(IrrecoTheme *self,
314 const char *button_name)
316 IrrecoThemeButton *button = NULL;
317 IRRECO_ENTER
318 IRRECO_STRING_TABLE_FOREACH_DATA(self->buttons, IrrecoThemeButton *,
319 pointer)
320 if (g_utf8_collate(pointer->name->str, button_name) == 0) {
321 button = pointer;
323 IRRECO_STRING_TABLE_FOREACH_END
324 IRRECO_RETURN_PTR(button);
327 IrrecoStringTable* irreco_theme_get_backgrounds(IrrecoTheme *self)
329 IRRECO_ENTER
330 IRRECO_RETURN_PTR(self->backgrounds);
333 IrrecoThemeBg *irreco_theme_get_background(IrrecoTheme *self,
334 const char *bg_name)
336 IrrecoThemeBg *bg = NULL;
337 IRRECO_ENTER
338 IRRECO_STRING_TABLE_FOREACH_DATA(self->backgrounds, IrrecoThemeBg *,
339 pointer)
340 if (g_utf8_collate(pointer->image_name->str, bg_name) == 0) {
341 bg = pointer;
343 IRRECO_STRING_TABLE_FOREACH_END
344 IRRECO_RETURN_PTR(bg);
347 void irreco_theme_set_author(IrrecoTheme *self, const char * author)
349 IRRECO_ENTER
350 if (author != NULL) {
351 g_string_printf(self->author, "%s", author);
353 irreco_theme_update_keyfile(self);
355 IRRECO_RETURN
358 void irreco_theme_set_comment(IrrecoTheme *self, const char * comment)
360 IRRECO_ENTER
361 if (comment != NULL) {
362 g_string_printf(self->comment, "%s", comment);
364 irreco_theme_update_keyfile(self);
366 IRRECO_RETURN
369 void irreco_theme_set_preview_button(IrrecoTheme *self,
370 const char * button_name)
372 IRRECO_ENTER
373 if (button_name != NULL) {
374 g_string_printf(self->preview_button_name, "%s", button_name);
376 irreco_theme_update_keyfile(self);
378 IRRECO_RETURN
382 #if 0
383 /* This function will work after IrrecoButtonStyle destruction*/
385 void irreco_theme_set_name(IrrecoTheme *self, IrrecoData *irreco_data,
386 const char * name)
388 IRRECO_ENTER
389 if (name != NULL) {
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",
405 self->name->str,
406 button->name->str);
407 IRRECO_PRINTF("style: %s\n",button->style_name->str);
408 IRRECO_PAUSE
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);
416 IRRECO_RETURN
418 #endif
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,
423 const char *version)
425 IRRECO_ENTER
427 if (name != NULL) {
428 g_string_printf(self->name, "%s", name);
429 } else {
430 g_string_erase(self->name, 0, -1);
433 if (path != NULL) {
434 g_string_printf(self->path, "%s", path);
435 } else {
436 g_string_erase(self->path, 0, -1);
439 if (source != NULL) {
440 g_string_printf(self->source, "%s", source);
441 } else {
442 g_string_erase(self->source, 0, -1);
445 if (author != NULL) {
446 g_string_printf(self->author, "%s", author);
447 } else {
448 g_string_erase(self->author, 0, -1);
451 if (comment != NULL) {
452 g_string_printf(self->comment, "%s", comment);
453 } else {
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);
460 } else {
461 g_string_erase(self->preview_button_name, 0, -1);
464 if (version != NULL) {
465 g_string_printf(self->version, "%s", version);
466 } else {
467 g_string_erase(self->version, 0, -1);
470 irreco_theme_update_keyfile(self);
472 /* Update buttons */
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);
488 directory = NULL;
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);
509 directory = NULL;
511 irreco_string_table_sort_abc(self->backgrounds);
514 IRRECO_RETURN
517 void irreco_theme_check(IrrecoTheme *self)
519 IRRECO_ENTER
521 /* Check if some background is deleted */
522 IRRECO_STRING_TABLE_FOREACH(self->backgrounds, key, IrrecoThemeBg *,
523 theme_bg)
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 *,
531 theme_button)
532 if(!irreco_is_file(theme_button->image_up->str)) {
533 irreco_string_table_remove(self->buttons, key);
535 IRRECO_STRING_TABLE_FOREACH_END
537 IRRECO_RETURN
540 IrrecoTheme *irreco_theme_copy(IrrecoTheme *self)
542 IrrecoTheme *new = NULL;
544 IRRECO_ENTER
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,
551 self->version->str);
554 IRRECO_STRING_TABLE_FOREACH(self->backgrounds, key, IrrecoThemeBg *,
555 theme_bg)
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 *,
563 theme_button)
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;
581 IRRECO_ENTER
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;
591 char *name = NULL;
592 char *source = NULL;
593 char *author = NULL;
594 char *comment = NULL;
595 char *preview_button = NULL;
596 char *version = NULL;
597 GString *conf = NULL;
598 IRRECO_ENTER
600 conf = g_string_new(dir);
601 g_string_append_printf(conf, "/theme.conf");
602 keyfile = irreco_keyfile_create(dir,
603 conf->str,
604 "theme");
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);
628 IRRECO_RETURN
632 gboolean irreco_theme_save(IrrecoTheme *self, IrrecoData *irreco_data,
633 const gchar *theme_path)
635 gboolean rvalue = FALSE;
636 IrrecoTheme *theme;
637 GString *path;
638 IrrecoStringTable *bg_list = NULL;
639 IrrecoStringTable *button_list = NULL;
640 IRRECO_ENTER
642 /*Create new theme*/
643 theme = irreco_theme_new();
644 irreco_theme_set(theme,
645 self->name->str,
646 theme_path,
647 self->source->str,
648 self->author->str,
649 self->comment->str,
650 self->preview_button_name->str,
651 NULL);
653 irreco_theme_update_keyfile(theme);
656 /* Get buttons and backgrounds */
657 /* Get backrounds */
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
673 /* Get buttons */
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,
684 button->name->str);
685 rvalue = TRUE;
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);
702 /** @} */
704 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
705 /* Events and Callbacks */
706 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
708 /** @} */