2 * irreco - Ir Remote Control
3 * Copyright (C) 2008 Joni Kokko (t5kojo01@students.oamk.fi)
4 * Sami Mäki (kasmra@xob.kapsi.fi)
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_manager.h"
24 * @addtogroup IrrecoThemeManager
27 * Contains information of themes.
32 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
34 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
36 void irreco_theme_manager_read_file_foreach(IrrecoDirForeachData
* dir_data
);
38 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
39 /* Construction & Destruction */
40 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
43 * @name Construction & Destruction
47 IrrecoThemeManager
*irreco_theme_manager_new(IrrecoData
* irreco_data
)
49 IrrecoThemeManager
*self
;
52 self
= g_slice_new0(IrrecoThemeManager
);
54 self
->irreco_data
= irreco_data
;
56 self
->themes
= irreco_string_table_new(
57 (GDestroyNotify
)irreco_theme_free
, NULL
);
59 irreco_theme_manager_read_themes_from_dir(self
, IRRECO_THEME_DIR
);
60 irreco_theme_manager_read_themes_from_dir(self
, "/media/mmc1/irreco");
61 irreco_theme_manager_read_themes_from_dir(self
, "/media/mmc2/irreco");
63 IRRECO_RETURN_PTR(self
);
66 void irreco_theme_manager_free(IrrecoThemeManager
*self
)
70 g_assert(self
!= NULL
);
72 irreco_string_table_free(self
->themes
);
75 g_slice_free(IrrecoThemeManager
, self
);
83 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
84 /* Private Functions */
85 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
88 * @name Private Functions
92 void irreco_theme_manager_read_file_foreach(IrrecoDirForeachData
* dir_data
)
94 IrrecoThemeManager
*self
= (IrrecoThemeManager
*) dir_data
->user_data_1
;
96 IrrecoKeyFile
*keyfile
= NULL
;
100 char *comment
= NULL
;
101 char *preview_button
= NULL
;
102 char *version
= NULL
;
103 GString
*conf
= g_string_new(dir_data
->filepath
);
106 g_string_append_printf(conf
, "/theme.conf");
107 keyfile
= irreco_keyfile_create(dir_data
->filepath
,
110 if (keyfile
== NULL
) goto end
;
112 /* Required fields. */
113 if (!irreco_keyfile_get_str(keyfile
, "name", &name
)) {
114 IRRECO_PRINTF("Could not read theme \"%s\"\n",
119 /* Optional fields. */
120 irreco_keyfile_get_str(keyfile
, "source", &source
);
121 irreco_keyfile_get_str(keyfile
, "author", &author
);
122 irreco_keyfile_get_str(keyfile
, "comment", &comment
);
123 irreco_keyfile_get_str(keyfile
, "preview-button", &preview_button
);
124 irreco_keyfile_get_str(keyfile
, "version", &version
);
126 if (irreco_string_table_get(self
->themes
, name
, (gpointer
*) &theme
)) {
127 irreco_theme_set(theme
, name
, dir_data
->filepath
, source
,
128 author
, comment
, preview_button
, version
);
130 theme
= irreco_theme_new(name
, dir_data
->filepath
, source
,
131 author
, comment
, preview_button
, version
);
132 irreco_string_table_add(self
->themes
, theme
->name
->str
, theme
);
135 irreco_theme_print(theme
);
138 g_string_free(conf
, TRUE
);
139 if (keyfile
!= NULL
) irreco_keyfile_destroy(keyfile
);
140 if (name
!= NULL
) g_free(name
);
141 if (source
!= NULL
) g_free(source
);
142 if (author
!= NULL
) g_free(author
);
143 if (comment
!= NULL
) g_free(comment
);
144 if (preview_button
!= NULL
) g_free(preview_button
);
145 if (version
!= NULL
) g_free(version
);
152 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
153 /* Public Functions */
154 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
157 * @name Public Functions
161 void irreco_theme_manager_print(IrrecoThemeManager
*self
)
166 irreco_string_table_print(self
->themes
);
170 IRRECO_STRING_TABLE_FOREACH_DATA(self
->themes
, void *, theme
)
171 IRRECO_PRINTF("Theme: %s \n", theme
->name
->str
);
172 irreco_theme_print(theme
);
173 IRRECO_STRING_TABLE_FOREACH_END
179 IrrecoStringTable
*irreco_theme_manager_get_themes(IrrecoThemeManager
*self
)
183 IRRECO_RETURN_PTR(self
->themes
);
186 IrrecoStringTable
*irreco_theme_manager_get_buttons(IrrecoThemeManager
*self
,
187 const gchar
*theme_name
)
189 IrrecoStringTable
*buttons
= NULL
;
190 IrrecoTheme
*theme
= NULL
;
193 IRRECO_PRINTF("theme_name: %s\n", theme_name
);
194 if (irreco_string_table_get(self
->themes
, theme_name
,
195 (gpointer
*) &theme
)) {
196 buttons
= theme
->buttons
;
199 IRRECO_RETURN_PTR(buttons
);
202 IrrecoStringTable
*irreco_theme_manager_get_backgrounds(IrrecoThemeManager
*self
,
203 const gchar
*theme_name
)
205 IrrecoStringTable
*backgrounds
= NULL
;
206 IrrecoTheme
*theme
= NULL
;
209 if (irreco_string_table_get(self
->themes
, theme_name
,
210 (gpointer
*)&theme
)) {
211 backgrounds
= theme
->backgrounds
;
214 IRRECO_RETURN_PTR(backgrounds
);
217 void irreco_theme_manager_read_themes_from_dir(IrrecoThemeManager
*self
,
220 IrrecoDirForeachData themes
;
223 IRRECO_DEBUG("THEME_DIR = %s\n", dir
);
224 themes
.directory
= dir
;
226 themes
.filesuffix
= "";
227 themes
.user_data_1
= self
;
229 irreco_dir_foreach(&themes
, irreco_theme_manager_read_file_foreach
);
231 irreco_string_table_sort_abc(self
->themes
);
235 gboolean
irreco_theme_manager_get_button_style(IrrecoThemeManager
*self
,
236 const gchar
*style_name
,
237 IrrecoThemeButton
**style
)
239 IrrecoThemeButton
*button
= NULL
;
240 gboolean rvalue
= FALSE
;
242 IRRECO_STRING_TABLE_FOREACH_DATA(self
->themes
, void *, theme
) {
243 IrrecoStringTable
* buttons
= irreco_theme_get_buttons(theme
);
245 if (irreco_string_table_get(buttons
, style_name
,
246 (gpointer
*) &button
)) {
251 IRRECO_STRING_TABLE_FOREACH_END
252 IRRECO_RETURN_BOOL(rvalue
);
255 gboolean
irreco_theme_manager_does_deb_exist(IrrecoThemeManager
*self
)
257 gboolean rvalue
= FALSE
;
259 IRRECO_STRING_TABLE_FOREACH_DATA(self
->themes
, IrrecoTheme
*, theme
) {
260 if (g_utf8_collate(theme
->source
->str
, "deb") == 0 ||
261 g_utf8_collate(theme
->source
->str
, "DEB") == 0) {
265 IRRECO_STRING_TABLE_FOREACH_END
266 IRRECO_RETURN_BOOL(rvalue
);
269 gboolean
irreco_theme_manager_does_web_exist(IrrecoThemeManager
*self
)
271 gboolean rvalue
= FALSE
;
273 IRRECO_STRING_TABLE_FOREACH_DATA(self
->themes
, IrrecoTheme
*, theme
) {
274 if (g_utf8_collate(theme
->source
->str
, "web") == 0 ||
275 g_utf8_collate(theme
->source
->str
, "WEB") == 0) {
279 IRRECO_STRING_TABLE_FOREACH_END
280 IRRECO_RETURN_BOOL(rvalue
);
283 gboolean
irreco_theme_manager_does_user_exist(IrrecoThemeManager
*self
)
285 gboolean rvalue
= FALSE
;
287 IRRECO_STRING_TABLE_FOREACH_DATA(self
->themes
, IrrecoTheme
*, theme
) {
288 if (g_utf8_collate(theme
->source
->str
, "user") == 0 ||
289 g_utf8_collate(theme
->source
->str
, "USER") == 0) {
293 IRRECO_STRING_TABLE_FOREACH_END
294 IRRECO_RETURN_BOOL(rvalue
);
297 gboolean
irreco_theme_manager_remove_theme(IrrecoThemeManager
*self
,
298 const gchar
*theme_name
)
300 IrrecoTheme
*rmtheme
;
302 gboolean rvalue
= FALSE
;
303 IrrecoStringTable
*table
;
307 if (irreco_string_table_get(self
->themes
, theme_name
,
308 (gpointer
*) &rmtheme
)) {
309 /* Remove styles from layouts */
310 table
= self
->irreco_data
->irreco_layout_array
;
311 IRRECO_STRING_TABLE_FOREACH(table
, key
,
312 IrrecoButtonLayout
*, layout
)
313 IRRECO_PTR_ARRAY_FORWARDS(layout
->button_array
,
314 IrrecoButton
*, button
)
315 if (button
->style
!= NULL
&&
316 g_str_equal(button
->style
->theme_name
->str
,
318 button
->type
= IRRECO_BTYPE_GTK_BUTTON
;
319 irreco_button_set_style(button
, NULL
);
320 irreco_button_create_widget(button
);
323 IRRECO_PTR_ARRAY_FORWARDS_END
324 IRRECO_STRING_TABLE_FOREACH_END
327 irreco_config_save_layouts(self
->irreco_data
);
329 /* Build remove command */
330 rm_cmd
= g_strconcat("rm -r ", rmtheme
->path
->str
, NULL
);
332 /* Remove theme directory and its contents from device */
337 /* And from the string table */
338 irreco_string_table_remove(self
->themes
, theme_name
);
343 IRRECO_RETURN_BOOL(rvalue
);
346 void irreco_theme_manager_update_theme_manager(IrrecoThemeManager
*self
)
350 irreco_theme_manager_read_themes_from_dir(self
, IRRECO_THEME_DIR
);
351 irreco_theme_manager_read_themes_from_dir(self
, "/media/mmc1/irreco");
352 irreco_theme_manager_read_themes_from_dir(self
, "/media/mmc2/irreco");
354 /* Check if some theme is deleted */
355 IRRECO_STRING_TABLE_FOREACH(self
->themes
, key
, IrrecoTheme
*,
357 if(!irreco_is_dir(theme
->path
->str
)) {
358 irreco_string_table_remove(self
->themes
, key
);
360 IRRECO_STRING_TABLE_FOREACH_END
367 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
368 /* Events and Callbacks */
369 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/