moved irreco_theme_update_keyfile() to public functions
[irreco.git] / irreco / src / core / irreco_theme.c
blob72d050d4caae2c366507c718b99bca40d68ca5b7
1 /*
2 * irreco - Ir Remote Control
3 * Copyright (C) 2008 Joni Kokko (t5kojo01@students.oamk.fi)
4 *
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.
9 *
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)
55 IrrecoTheme *self;
56 IRRECO_ENTER
58 self = g_slice_new0(IrrecoTheme);
60 self->name = g_string_new(name);
61 self->path = g_string_new(path);
62 self->source = g_string_new(source);
63 self->author = g_string_new(author);
64 self->comment = g_string_new(comment);
65 self->preview_button_name = g_string_new(preview_button_name);
66 self->backgrounds = irreco_string_table_new(
67 (GDestroyNotify)irreco_theme_bg_free, NULL);
68 self->buttons = irreco_string_table_new(
69 (GDestroyNotify)irreco_theme_button_free, NULL);
71 IRRECO_DEBUG("Themepath: %s\n", path);
73 /*Get buttons*/
75 IrrecoDirForeachData button_styles;
76 GString * directory = g_string_new("");
78 g_string_printf(directory, "%s/buttons/", path);
79 IRRECO_DEBUG("Directory = %s\n", directory->str);
80 button_styles.directory = directory->str;
82 button_styles.filesuffix = "button.conf";
83 button_styles.user_data_1 = self;
85 irreco_dir_foreach_subdirectories(&button_styles,
86 irreco_theme_read_button_keyfile_foreach);
88 g_string_free(directory, TRUE);
89 directory = NULL;
91 irreco_string_table_sort_abc(self->buttons);
94 /*Get backgrounds*/
96 IrrecoDirForeachData bg_styles;
97 GString * directory = g_string_new("");
99 g_string_printf(directory, "%s/bg/", path);
100 IRRECO_DEBUG("Directory = %s\n", directory->str);
101 bg_styles.directory = directory->str;
103 bg_styles.filesuffix = "bg.conf";
104 bg_styles.user_data_1 = self;
106 irreco_dir_foreach_subdirectories(&bg_styles,
107 irreco_theme_read_bg_keyfile_foreach);
109 g_string_free(directory, TRUE);
110 directory = NULL;
112 irreco_string_table_sort_abc(self->backgrounds);
115 IRRECO_RETURN_PTR(self);
118 void irreco_theme_free(IrrecoTheme *self)
120 IRRECO_ENTER
122 g_assert(self != NULL);
124 g_string_free(self->name, TRUE);
125 self->name = NULL;
127 g_string_free(self->path, TRUE);
128 self->path = NULL;
130 g_string_free(self->source, TRUE);
131 self->source = NULL;
133 g_string_free(self->author, TRUE);
134 self->author = NULL;
136 g_string_free(self->comment, TRUE);
137 self->comment = NULL;
139 g_string_free(self->preview_button_name, TRUE);
140 self->preview_button_name = NULL;
142 irreco_string_table_free(self->backgrounds);
143 self->backgrounds = NULL;
145 irreco_string_table_free(self->buttons);
146 self->buttons = NULL;
148 g_slice_free(IrrecoTheme, self);
149 self = NULL;
151 IRRECO_RETURN
154 /** @} */
156 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
157 /* Private Functions */
158 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
161 * @name Private Functions
162 * @{
166 /** @} */
168 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
169 /* Public Functions */
170 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
173 * @name Public Functions
174 * @{
176 void irreco_theme_update_keyfile(IrrecoTheme *self)
178 GString *keyfile_path = g_string_new(self->path->str);
179 GKeyFile *keyfile = g_key_file_new();
180 IRRECO_ENTER
182 g_string_append(keyfile_path, "/theme.conf");
184 irreco_gkeyfile_set_string(keyfile, "theme" , "name",
185 self->name->str);
187 if (self->source->len > 0) {
188 irreco_gkeyfile_set_string(keyfile, "theme" , "source",
189 self->source->str);
192 if (self->author->len > 0) {
193 irreco_gkeyfile_set_string(keyfile, "theme", "author",
194 self->author->str);
197 if (self->preview_button_name->len > 0) {
198 irreco_gkeyfile_set_string(keyfile, "theme", "preview-button",
199 self->preview_button_name->str);
202 if (self->comment->len > 0) {
203 irreco_gkeyfile_set_string(keyfile, "theme", "comment",
204 self->comment->str);
207 irreco_write_keyfile(keyfile, keyfile_path->str);
209 g_key_file_free(keyfile);
210 g_string_free(keyfile_path, TRUE);
211 IRRECO_RETURN
214 void irreco_theme_print(IrrecoTheme *self)
216 IRRECO_ENTER
218 IRRECO_PRINTF("Themename: %s \n", self->name->str);
219 IRRECO_PRINTF("Folder: %s \n", self->path->str);
220 IRRECO_PRINTF("Source: %s \n", self->source->str);
221 IRRECO_PRINTF("Author: %s \n", self->author->str);
222 IRRECO_PRINTF("Comment: %s \n", self->comment->str);
223 IRRECO_PRINTF("Previewbutton: %s \n", self->preview_button_name->str);
224 irreco_string_table_print(self->backgrounds);
225 irreco_string_table_print(self->buttons);
227 #if 0
228 IRRECO_STRING_TABLE_FOREACH_DATA(self->backgrounds, void *, theme_bg)
229 irreco_theme_button_print(theme_bg);
230 IRRECO_STRING_TABLE_FOREACH_END
232 IRRECO_STRING_TABLE_FOREACH_DATA(self->buttons, void *, theme_button)
233 irreco_theme_button_print(theme_button);
234 IRRECO_STRING_TABLE_FOREACH_END
235 #endif
236 IRRECO_RETURN
239 void irreco_theme_read_button_keyfile_foreach(IrrecoDirForeachData * dir_data)
241 IrrecoTheme *self = (IrrecoTheme*) dir_data->user_data_1;
242 IrrecoKeyFile *keyfile;
243 gchar *name = NULL;
244 gchar *up = NULL;
245 gchar *down = NULL;
246 gboolean allow_text;
247 gchar *text_format_up = NULL;
248 gchar *text_format_down = NULL;
249 gint text_padding = 5;
250 gfloat text_h_align = 0.5;
251 gfloat text_v_align = 0.5;
252 IrrecoThemeButton *theme_button;
253 GString *style_name = g_string_new(self->name->str);
255 IRRECO_ENTER
257 keyfile = irreco_keyfile_create(dir_data->directory,
258 dir_data->filepath,
259 "theme-button");
260 if (keyfile == NULL) goto end;
262 /* Required fields. */
263 if (!irreco_keyfile_get_str(keyfile, "name", &name) ||
264 !irreco_keyfile_get_path(keyfile, "up", &up)) {
265 IRRECO_PRINTF("Could not read style from group \"%s\"\n",
266 keyfile->group);
267 goto end;
270 /* Optional fields. */
271 irreco_keyfile_get_path(keyfile, "down", &down);
272 irreco_keyfile_get_bool(keyfile, "allow-text", &allow_text);
273 irreco_keyfile_get_str(keyfile, "text-format-up", &text_format_up);
274 irreco_keyfile_get_str(keyfile, "text-format-down", &text_format_down);
275 irreco_keyfile_get_int(keyfile, "text-padding", &text_padding);
276 irreco_keyfile_get_float(keyfile, "text-h-align", &text_h_align);
277 irreco_keyfile_get_float(keyfile, "text-v-align", &text_v_align);
279 g_string_append_printf(style_name,"/%s", name);
280 theme_button = irreco_theme_button_new();
281 irreco_theme_button_set(theme_button, style_name->str,
282 name, allow_text,
283 up, down,
284 text_format_up, text_format_down,
285 text_padding,
286 text_h_align,
287 text_v_align);
289 irreco_string_table_add(self->buttons,
290 theme_button->style_name->str,
291 theme_button);
293 irreco_theme_button_print(theme_button);
295 irreco_keyfile_destroy(keyfile);
298 end:
299 if (name != NULL) g_free(name);
300 if (name != NULL) g_free(up);
301 if (name != NULL) g_free(down);
302 if (name != NULL) g_free(text_format_up);
303 if (name != NULL) g_free(text_format_down);
304 g_string_free(style_name, TRUE);
307 IRRECO_RETURN
310 void irreco_theme_read_bg_keyfile_foreach(IrrecoDirForeachData * dir_data)
312 IrrecoTheme *self = (IrrecoTheme*) dir_data->user_data_1;
313 IrrecoKeyFile *keyfile;
314 IrrecoThemeBg *bg;
315 char *name;
316 char *image;
317 IRRECO_ENTER
319 keyfile = irreco_keyfile_create(dir_data->directory,
320 dir_data->filepath,
321 "theme-bg");
322 if (keyfile == NULL) goto end;
324 /* Required fields. */
325 if (!irreco_keyfile_get_str(keyfile, "name", &name) ||
326 !irreco_keyfile_get_path(keyfile, "image", &image)) {
327 IRRECO_PRINTF("Could not read style from group \"%s\"\n",
328 keyfile->group);
329 goto end;
331 bg = irreco_theme_bg_new();
333 irreco_theme_bg_set(bg, name, image);
334 irreco_string_table_add(self->backgrounds, name, bg);
336 irreco_theme_bg_print(bg);
338 irreco_keyfile_destroy(keyfile);
340 end:
341 g_free(name);
342 g_free(image);
343 IRRECO_RETURN
346 IrrecoStringTable* irreco_theme_get_buttons(IrrecoTheme *self)
348 IRRECO_ENTER
349 IRRECO_RETURN_PTR(self->buttons);
352 IrrecoThemeButton *irreco_theme_get_button(IrrecoTheme *self,
353 const char *button_name)
355 IrrecoThemeButton *button = NULL;
356 IRRECO_ENTER
357 IRRECO_STRING_TABLE_FOREACH_DATA(self->buttons, IrrecoThemeButton *,
358 pointer)
359 if (g_utf8_collate(pointer->name->str, button_name) == 0) {
360 button = pointer;
362 IRRECO_STRING_TABLE_FOREACH_END
363 IRRECO_RETURN_PTR(button);
366 IrrecoStringTable* irreco_theme_get_backgrounds(IrrecoTheme *self)
368 IRRECO_ENTER
369 IRRECO_RETURN_PTR(self->backgrounds);
372 IrrecoThemeBg *irreco_theme_get_background(IrrecoTheme *self,
373 const char *bg_name)
375 IrrecoThemeBg *bg = NULL;
376 IRRECO_ENTER
377 IRRECO_STRING_TABLE_FOREACH_DATA(self->backgrounds, IrrecoThemeBg *,
378 pointer)
379 if (g_utf8_collate(pointer->image_name->str, bg_name) == 0) {
380 bg = pointer;
382 IRRECO_STRING_TABLE_FOREACH_END
383 IRRECO_RETURN_PTR(bg);
386 void irreco_theme_set_author(IrrecoTheme *self, const char * author)
388 IRRECO_ENTER
389 if (author != NULL) {
390 g_string_printf(self->author, "%s", author);
392 irreco_theme_update_keyfile(self);
394 IRRECO_RETURN
397 void irreco_theme_set_comment(IrrecoTheme *self, const char * comment)
399 IRRECO_ENTER
400 if (comment != NULL) {
401 g_string_printf(self->comment, "%s", comment);
403 irreco_theme_update_keyfile(self);
405 IRRECO_RETURN
408 void irreco_theme_set_preview_button(IrrecoTheme *self,
409 const char * button_name)
411 IRRECO_ENTER
412 if (button_name != NULL) {
413 g_string_printf(self->preview_button_name, "%s", button_name);
415 irreco_theme_update_keyfile(self);
417 IRRECO_RETURN
420 #if 0
421 /* This function will work after IrrecoButtonStyle destruction*/
423 void irreco_theme_set_name(IrrecoTheme *self, IrrecoData *irreco_data,
424 const char * name)
426 IRRECO_ENTER
427 if (name != NULL) {
428 GString *style_name = g_string_new("");
431 *TODO Move this part to IrrecoThemeManager and call this
432 *funtion from ThemeManager*/
434 irreco_string_table_change_key(
435 irreco_data->theme_manager->themes,
436 self->name->str, name);
438 g_string_printf(self->name, "%s", name);
440 IRRECO_STRING_TABLE_FOREACH_DATA(self->buttons,
441 IrrecoThemeButton *, button) {
442 g_string_printf(button->style_name,"%s/%s",
443 self->name->str,
444 button->name->str);
445 IRRECO_PRINTF("style: %s\n",button->style_name->str);
446 IRRECO_PAUSE
448 IRRECO_STRING_TABLE_FOREACH_END
450 irreco_theme_update_keyfile(self);
451 irreco_config_save_layouts(irreco_data);
452 g_string_free(style_name, TRUE);
454 IRRECO_RETURN
456 #endif
457 /** @} */
459 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
460 /* Events and Callbacks */
461 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
463 /** @} */