Clean code and added a few amendments (if()).
[irreco.git] / irreco / src / core / irreco_theme.c
blob368f723f0c639dac6cf30a27623ad315cf485f9e
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 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
42 /* Construction & Destruction */
43 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
45 /**
46 * @name Construction & Destruction
47 * @{
50 IrrecoTheme *irreco_theme_new(const char *name, const char *path,
51 const char *source, const char *author,
52 const char *comment,
53 const char *preview_button_name,
54 const char *version)
56 IrrecoTheme *self;
57 IRRECO_ENTER
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);
75 /*Get buttons*/
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);
91 directory = NULL;
93 irreco_string_table_sort_abc(self->buttons);
96 /*Get backgrounds*/
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);
112 directory = NULL;
114 irreco_string_table_sort_abc(self->backgrounds);
117 IRRECO_RETURN_PTR(self);
120 void irreco_theme_free(IrrecoTheme *self)
122 IRRECO_ENTER
124 g_assert(self != NULL);
126 g_string_free(self->name, TRUE);
127 self->name = NULL;
129 g_string_free(self->path, TRUE);
130 self->path = NULL;
132 g_string_free(self->source, TRUE);
133 self->source = NULL;
135 g_string_free(self->author, TRUE);
136 self->author = NULL;
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);
151 self = NULL;
153 IRRECO_RETURN
156 /** @} */
158 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
159 /* Private Functions */
160 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
163 * @name Private Functions
164 * @{
168 /** @} */
170 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
171 /* Public Functions */
172 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
175 * @name Public Functions
176 * @{
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();
182 IRRECO_ENTER
184 g_string_append(keyfile_path, "/theme.conf");
186 irreco_gkeyfile_set_string(keyfile, "theme" , "name",
187 self->name->str);
189 if (self->source->len > 0) {
190 irreco_gkeyfile_set_string(keyfile, "theme" , "source",
191 self->source->str);
194 if (self->author->len > 0) {
195 irreco_gkeyfile_set_string(keyfile, "theme", "author",
196 self->author->str);
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",
206 self->version->str);
209 if (self->comment->len > 0) {
210 irreco_gkeyfile_set_string(keyfile, "theme", "comment",
211 self->comment->str);
214 irreco_write_keyfile(keyfile, keyfile_path->str);
216 g_key_file_free(keyfile);
217 g_string_free(keyfile_path, TRUE);
218 IRRECO_RETURN
221 void irreco_theme_print(IrrecoTheme *self)
223 IRRECO_ENTER
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);
235 IRRECO_RETURN
238 void irreco_theme_read_button_keyfile_foreach(IrrecoDirForeachData * dir_data)
240 IrrecoTheme *self = (IrrecoTheme*) dir_data->user_data_1;
241 IrrecoKeyFile *keyfile;
242 gchar *name = NULL;
243 gchar *up = NULL;
244 gchar *down = NULL;
245 gboolean allow_text;
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);
254 IRRECO_ENTER
256 keyfile = irreco_keyfile_create(dir_data->directory,
257 dir_data->filepath,
258 "theme-button");
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",
265 keyfile->group);
266 goto end;
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,
283 name, allow_text,
284 up, down,
285 text_format_up, text_format_down,
286 text_padding,
287 text_h_align,
288 text_v_align);
289 } else {
290 theme_button = irreco_theme_button_new(self->name->str);
291 irreco_theme_button_set(theme_button, style_name->str,
292 name, allow_text,
293 up, down,
294 text_format_up, text_format_down,
295 text_padding,
296 text_h_align,
297 text_v_align);
298 irreco_string_table_add(self->buttons,
299 theme_button->style_name->str,
300 theme_button);
303 irreco_theme_button_print(theme_button);
305 end:
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);
315 IRRECO_RETURN
318 void irreco_theme_read_bg_keyfile_foreach(IrrecoDirForeachData * dir_data)
320 IrrecoTheme *self = (IrrecoTheme*) dir_data->user_data_1;
321 IrrecoKeyFile *keyfile;
322 IrrecoThemeBg *bg;
323 char *name = NULL;
324 char *image = NULL;
325 IRRECO_ENTER
327 keyfile = irreco_keyfile_create(dir_data->directory,
328 dir_data->filepath,
329 "theme-bg");
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",
336 keyfile->group);
337 goto end;
339 if (irreco_string_table_get(self->backgrounds, name,
340 (gpointer *) &bg)) {
341 irreco_theme_bg_set(bg, name, image);
342 } else {
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);
350 end:
351 if(keyfile != NULL) irreco_keyfile_destroy(keyfile);
352 if(name != NULL) g_free(name);
353 if(image != NULL) g_free(image);
354 IRRECO_RETURN
357 IrrecoStringTable* irreco_theme_get_buttons(IrrecoTheme *self)
359 IRRECO_ENTER
360 IRRECO_RETURN_PTR(self->buttons);
363 IrrecoThemeButton *irreco_theme_get_button(IrrecoTheme *self,
364 const char *button_name)
366 IrrecoThemeButton *button = NULL;
367 IRRECO_ENTER
368 IRRECO_STRING_TABLE_FOREACH_DATA(self->buttons, IrrecoThemeButton *,
369 pointer)
370 if (g_utf8_collate(pointer->name->str, button_name) == 0) {
371 button = pointer;
373 IRRECO_STRING_TABLE_FOREACH_END
374 IRRECO_RETURN_PTR(button);
377 IrrecoStringTable* irreco_theme_get_backgrounds(IrrecoTheme *self)
379 IRRECO_ENTER
380 IRRECO_RETURN_PTR(self->backgrounds);
383 IrrecoThemeBg *irreco_theme_get_background(IrrecoTheme *self,
384 const char *bg_name)
386 IrrecoThemeBg *bg = NULL;
387 IRRECO_ENTER
388 IRRECO_STRING_TABLE_FOREACH_DATA(self->backgrounds, IrrecoThemeBg *,
389 pointer)
390 if (g_utf8_collate(pointer->image_name->str, bg_name) == 0) {
391 bg = pointer;
393 IRRECO_STRING_TABLE_FOREACH_END
394 IRRECO_RETURN_PTR(bg);
397 void irreco_theme_set_author(IrrecoTheme *self, const char * author)
399 IRRECO_ENTER
400 if (author != NULL) {
401 g_string_printf(self->author, "%s", author);
403 irreco_theme_update_keyfile(self);
405 IRRECO_RETURN
408 void irreco_theme_set_comment(IrrecoTheme *self, const char * comment)
410 IRRECO_ENTER
411 if (comment != NULL) {
412 g_string_printf(self->comment, "%s", comment);
414 irreco_theme_update_keyfile(self);
416 IRRECO_RETURN
419 void irreco_theme_set_preview_button(IrrecoTheme *self,
420 const char * button_name)
422 IRRECO_ENTER
423 if (button_name != NULL) {
424 g_string_printf(self->preview_button_name, "%s", button_name);
426 irreco_theme_update_keyfile(self);
428 IRRECO_RETURN
432 #if 0
433 /* This function will work after IrrecoButtonStyle destruction*/
435 void irreco_theme_set_name(IrrecoTheme *self, IrrecoData *irreco_data,
436 const char * name)
438 IRRECO_ENTER
439 if (name != NULL) {
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",
455 self->name->str,
456 button->name->str);
457 IRRECO_PRINTF("style: %s\n",button->style_name->str);
458 IRRECO_PAUSE
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);
466 IRRECO_RETURN
468 #endif
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,
473 const char *version)
475 IRRECO_ENTER
477 if (name != NULL) {
478 g_string_printf(self->name, "%s", name);
479 } else {
480 g_string_erase(self->name, 0, -1);
483 if (path != NULL) {
484 g_string_printf(self->path, "%s", path);
485 } else {
486 g_string_erase(self->path, 0, -1);
489 if (source != NULL) {
490 g_string_printf(self->source, "%s", source);
491 } else {
492 g_string_erase(self->source, 0, -1);
495 if (author != NULL) {
496 g_string_printf(self->author, "%s", author);
497 } else {
498 g_string_erase(self->author, 0, -1);
501 if (comment != NULL) {
502 g_string_printf(self->comment, "%s", comment);
503 } else {
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);
510 } else {
511 g_string_erase(self->preview_button_name, 0, -1);
514 if (version != NULL) {
515 g_string_printf(self->version, "%s", version);
516 } else {
517 g_string_erase(self->version, 0, -1);
520 irreco_theme_update_keyfile(self);
522 /* Update buttons */
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);
538 directory = NULL;
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);
559 directory = NULL;
561 irreco_string_table_sort_abc(self->backgrounds);
564 IRRECO_RETURN
567 void irreco_theme_check(IrrecoTheme *self)
569 IRRECO_ENTER
571 /* Check if some background is deleted */
572 IRRECO_STRING_TABLE_FOREACH(self->backgrounds, key, IrrecoThemeBg *,
573 theme_bg)
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 *,
581 theme_button)
582 if(!irreco_is_file(theme_button->image_up->str)) {
583 irreco_string_table_remove(self->buttons, key);
585 IRRECO_STRING_TABLE_FOREACH_END
587 IRRECO_RETURN
590 IrrecoTheme *irreco_theme_copy(IrrecoTheme *self)
592 IrrecoTheme *new = NULL;
594 IRRECO_ENTER
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,
599 self->version->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,
604 self->version->str);
607 IRRECO_STRING_TABLE_FOREACH(self->backgrounds, key, IrrecoThemeBg *,
608 theme_bg)
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 *,
616 theme_button)
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);
627 /** @} */
629 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
630 /* Events and Callbacks */
631 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
633 /** @} */