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"
23 * @addtogroup IrrecoTheme
26 * Contains information of theme.
33 * Source file of @ref IrrecoTheme.
36 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
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 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
46 * @name Construction & Destruction
50 IrrecoTheme
*irreco_theme_new(const char *name
, const char *path
,
51 const char *source
, const char *author
,
53 const char *preview_button_name
)
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
);
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
);
91 irreco_string_table_sort_abc(self
->buttons
);
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
);
112 irreco_string_table_sort_abc(self
->backgrounds
);
115 IRRECO_RETURN_PTR(self
);
118 void irreco_theme_free(IrrecoTheme
*self
)
122 g_assert(self
!= NULL
);
124 g_string_free(self
->name
, TRUE
);
127 g_string_free(self
->path
, TRUE
);
130 g_string_free(self
->source
, TRUE
);
133 g_string_free(self
->author
, TRUE
);
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
);
156 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
157 /* Private Functions */
158 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
161 * @name Private Functions
168 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
169 /* Public Functions */
170 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
173 * @name Public Functions
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();
182 g_string_append(keyfile_path
, "/theme.conf");
184 irreco_gkeyfile_set_string(keyfile
, "theme" , "name",
187 if (self
->source
->len
> 0) {
188 irreco_gkeyfile_set_string(keyfile
, "theme" , "source",
192 if (self
->author
->len
> 0) {
193 irreco_gkeyfile_set_string(keyfile
, "theme", "author",
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",
207 irreco_write_keyfile(keyfile
, keyfile_path
->str
);
209 g_key_file_free(keyfile
);
210 g_string_free(keyfile_path
, TRUE
);
214 void irreco_theme_print(IrrecoTheme
*self
)
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
);
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
239 void irreco_theme_read_button_keyfile_foreach(IrrecoDirForeachData
* dir_data
)
241 IrrecoTheme
*self
= (IrrecoTheme
*) dir_data
->user_data_1
;
242 IrrecoKeyFile
*keyfile
;
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
);
257 keyfile
= irreco_keyfile_create(dir_data
->directory
,
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",
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
,
284 text_format_up
, text_format_down
,
289 irreco_string_table_add(self
->buttons
,
290 theme_button
->style_name
->str
,
293 irreco_theme_button_print(theme_button
);
295 irreco_keyfile_destroy(keyfile
);
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
);
310 void irreco_theme_read_bg_keyfile_foreach(IrrecoDirForeachData
* dir_data
)
312 IrrecoTheme
*self
= (IrrecoTheme
*) dir_data
->user_data_1
;
313 IrrecoKeyFile
*keyfile
;
319 keyfile
= irreco_keyfile_create(dir_data
->directory
,
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",
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
);
346 IrrecoStringTable
* irreco_theme_get_buttons(IrrecoTheme
*self
)
349 IRRECO_RETURN_PTR(self
->buttons
);
352 IrrecoThemeButton
*irreco_theme_get_button(IrrecoTheme
*self
,
353 const char *button_name
)
355 IrrecoThemeButton
*button
= NULL
;
357 IRRECO_STRING_TABLE_FOREACH_DATA(self
->buttons
, IrrecoThemeButton
*,
359 if (g_utf8_collate(pointer
->name
->str
, button_name
) == 0) {
362 IRRECO_STRING_TABLE_FOREACH_END
363 IRRECO_RETURN_PTR(button
);
366 IrrecoStringTable
* irreco_theme_get_backgrounds(IrrecoTheme
*self
)
369 IRRECO_RETURN_PTR(self
->backgrounds
);
372 IrrecoThemeBg
*irreco_theme_get_background(IrrecoTheme
*self
,
375 IrrecoThemeBg
*bg
= NULL
;
377 IRRECO_STRING_TABLE_FOREACH_DATA(self
->backgrounds
, IrrecoThemeBg
*,
379 if (g_utf8_collate(pointer
->image_name
->str
, bg_name
) == 0) {
382 IRRECO_STRING_TABLE_FOREACH_END
383 IRRECO_RETURN_PTR(bg
);
386 void irreco_theme_set_author(IrrecoTheme
*self
, const char * author
)
389 if (author
!= NULL
) {
390 g_string_printf(self
->author
, "%s", author
);
392 irreco_theme_update_keyfile(self
);
397 void irreco_theme_set_comment(IrrecoTheme
*self
, const char * comment
)
400 if (comment
!= NULL
) {
401 g_string_printf(self
->comment
, "%s", comment
);
403 irreco_theme_update_keyfile(self
);
408 void irreco_theme_set_preview_button(IrrecoTheme
*self
,
409 const char * button_name
)
412 if (button_name
!= NULL
) {
413 g_string_printf(self
->preview_button_name
, "%s", button_name
);
415 irreco_theme_update_keyfile(self
);
421 /* This function will work after IrrecoButtonStyle destruction*/
423 void irreco_theme_set_name(IrrecoTheme
*self
, IrrecoData
*irreco_data
,
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",
445 IRRECO_PRINTF("style: %s\n",button
->style_name
->str
);
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
);
459 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
460 /* Events and Callbacks */
461 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/