Fixed button style_name set
[irreco.git] / irreco / src / core / irreco_theme_button.c
blob39b0bdb934e855fb49e0883641d2ad35fab30ebd
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_button.h"
23 /**
24 * @addtogroup IrrecoThemeButton
25 * @ingroup Irreco
27 * Contains information of button.
29 * @{
32 /**
33 * @file
34 * Source file of @ref IrrecoThemeButton.
37 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
38 /* Prototypes */
39 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
41 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
42 /* Construction & Destruction */
43 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
45 /**
46 * @name Construction & Destruction
47 * @{
50 IrrecoThemeButton *irreco_theme_button_new(const char *theme_name)
52 IrrecoThemeButton *self;
53 IRRECO_ENTER
55 self = g_slice_new0(IrrecoThemeButton);
57 self->theme_name = g_string_new(theme_name);
58 self->style_name = g_string_new("");
59 self->name = g_string_new("");
60 self->image_up = g_string_new("");
61 self->image_down = g_string_new("");
62 self->text_format_up = g_string_new("");
63 self->text_format_down = g_string_new("");
64 self->text_padding = 5;
66 /* Default to center alignment. */
67 self->text_h_align = 0.5;
68 self->text_v_align = 0.5;
70 IRRECO_RETURN_PTR(self);
73 void irreco_theme_button_free(IrrecoThemeButton *self)
75 IRRECO_ENTER
77 g_assert(self != NULL);
79 g_string_free(self->theme_name, TRUE);
80 self->theme_name = NULL;
82 g_string_free(self->style_name, TRUE);
83 self->style_name = NULL;
85 g_string_free(self->name, TRUE);
86 self->name = NULL;
88 g_string_free(self->image_up, TRUE);
89 self->image_up = NULL;
91 g_string_free(self->image_down, TRUE);
92 self->image_down = NULL;
94 g_string_free(self->text_format_up, TRUE);
95 self->text_format_up = NULL;
97 g_string_free(self->text_format_down, TRUE);
98 self->text_format_down = NULL;
100 g_slice_free(IrrecoThemeButton, self);
101 self = NULL;
103 IRRECO_RETURN
106 /** @} */
108 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
109 /* Private Functions */
110 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
112 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
113 /* Public Functions */
114 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
117 * @name Public Functions
118 * @{
121 void irreco_theme_button_set(IrrecoThemeButton *self,
122 const char *style_name,
123 const char *name,
124 gboolean allow_text,
125 const char *image_up,
126 const char *image_down,
127 const char *text_format_up,
128 const char *text_format_down,
129 gint text_padding,
130 gfloat text_h_align,
131 gfloat text_v_align)
133 IRRECO_ENTER
135 if (style_name != NULL) {
136 g_string_printf(self->style_name, "%s", style_name);
138 if (name != NULL) {
139 g_string_printf(self->name, "%s", name);
141 self->allow_text = allow_text;
142 if (image_up != NULL) {
143 g_string_printf(self->image_up, "%s", image_up);
145 if (image_down != NULL) {
146 g_string_printf(self->image_down, "%s", image_down);
147 } else {
148 g_string_printf(self->image_down, "%s", image_up);
150 if (text_format_up != NULL) {
151 g_string_printf(self->text_format_up, "%s", text_format_up);
153 if (text_format_down != NULL) {
154 g_string_printf(self->text_format_down, "%s", text_format_down);
157 self->text_padding = text_padding;
159 /* Set Horisontal aligment. */
160 if (self->text_h_align < 0) {
161 self->text_h_align = 0;
162 } else if (self->text_h_align > 1) {
163 self->text_h_align = 1;
164 } else {
165 self->text_h_align = text_h_align;
168 /* Set vertical alignment. */
169 if (self->text_v_align < 0) {
170 self->text_v_align = 0;
171 } else if (self->text_v_align > 1) {
172 self->text_v_align = 1;
173 } else {
174 self->text_v_align = text_v_align;
177 IRRECO_RETURN
179 void irreco_theme_button_set_from_button(IrrecoThemeButton *self,
180 IrrecoThemeButton *button)
182 IRRECO_ENTER
183 irreco_theme_button_set(self,
184 button->style_name->str,
185 button->name->str,
186 button->allow_text,
187 button->image_up->str,
188 button->image_down->str,
189 button->text_format_up->str,
190 button->text_format_down->str,
191 button->text_padding,
192 button->text_h_align,
193 button->text_v_align);
194 IRRECO_RETURN
196 void irreco_theme_button_print(IrrecoThemeButton *self)
198 IRRECO_ENTER
200 IRRECO_DEBUG("--------------------------------------------\n");
201 IRRECO_DEBUG("theme_name: %s\n", self->theme_name->str);
202 IRRECO_DEBUG("style_name: %s\n", self->style_name->str);
203 IRRECO_DEBUG("button_name: %s\n", self->name->str);
204 IRRECO_DEBUG("allow_text: %d\n", self->allow_text);
205 IRRECO_DEBUG("image_up: %s\n",self->image_up->str);
206 IRRECO_DEBUG("image_down: %s\n", self->image_down->str);
207 IRRECO_DEBUG("text_format_up: %s\n", self->text_format_up->str);
208 IRRECO_DEBUG("text_format_down: %s\n", self->text_format_down->str);
209 IRRECO_DEBUG("text_padding: %d\n", self->text_padding);
210 IRRECO_DEBUG("--------------------------------------------\n");
212 IRRECO_RETURN
215 IrrecoThemeButton *irreco_theme_button_copy(IrrecoThemeButton *self)
217 IrrecoThemeButton *new = NULL;
218 IRRECO_ENTER
220 new = irreco_theme_button_new(self->theme_name->str);
221 irreco_theme_button_set(new, self->style_name->str, self->name->str,
222 self->allow_text, self->image_up->str,
223 self->image_down->str, self->text_format_up->str,
224 self->text_format_down->str, self->text_padding,
225 self->text_h_align, self->text_v_align);
227 IRRECO_RETURN_PTR(new);
231 * IrrecoThemeBg new from dir
234 IrrecoThemeButton *
235 irreco_theme_button_new_from_dir(const gchar *dir, const gchar *theme_name)
237 IrrecoThemeButton *self = NULL;
239 IRRECO_ENTER
241 self = irreco_theme_button_new(NULL);
242 irreco_theme_button_read(self, dir, theme_name);
243 IRRECO_RETURN_PTR(self);
246 void irreco_theme_button_read(IrrecoThemeButton *self, const gchar *dir,
247 const gchar *theme_name)
249 IrrecoKeyFile *keyfile;
250 GString *conf = NULL;
251 gchar *name = NULL;
252 gchar *up = NULL;
253 gchar *down = NULL;
254 gboolean allow_text;
255 gchar *text_format_up = NULL;
256 gchar *text_format_down = NULL;
257 gint text_padding = 5;
258 gfloat text_h_align = 0.5;
259 gfloat text_v_align = 0.5;
260 GString *style_name = g_string_new(theme_name);
261 IRRECO_ENTER
263 conf = g_string_new(dir);
264 g_string_append_printf(conf, "/button.conf");
265 keyfile = irreco_keyfile_create(dir,
266 conf->str,
267 "theme-button");
268 if (keyfile == NULL) goto end;
270 /* Required fields. */
271 if (!irreco_keyfile_get_str(keyfile, "name", &name) ||
272 !irreco_keyfile_get_path(keyfile, "up", &up)) {
273 IRRECO_PRINTF("Could not read style from group \"%s\"\n",
274 keyfile->group);
275 goto end;
278 /* Optional fields. */
279 irreco_keyfile_get_path(keyfile, "down", &down);
280 irreco_keyfile_get_bool(keyfile, "allow-text", &allow_text);
281 irreco_keyfile_get_str(keyfile, "text-format-up", &text_format_up);
282 irreco_keyfile_get_str(keyfile, "text-format-down", &text_format_down);
283 irreco_keyfile_get_int(keyfile, "text-padding", &text_padding);
284 irreco_keyfile_get_float(keyfile, "text-h-align", &text_h_align);
285 irreco_keyfile_get_float(keyfile, "text-v-align", &text_v_align);
287 g_string_append_printf(style_name,"/%s", name);
289 irreco_theme_button_set(self, style_name->str,
290 name, allow_text,
291 up, down,
292 text_format_up, text_format_down,
293 text_padding,
294 text_h_align,
295 text_v_align);
297 irreco_theme_button_print(self);
299 end:
300 if(keyfile != NULL) irreco_keyfile_destroy(keyfile);
301 if(name != NULL) g_free(name);
302 if(name != NULL) g_free(up);
303 if(name != NULL) g_free(down);
304 if(name != NULL) g_free(text_format_up);
305 if(name != NULL) g_free(text_format_down);
306 g_string_free(style_name, TRUE);
308 IRRECO_RETURN
312 * Save button to theme folder
315 gboolean
316 irreco_theme_button_save(IrrecoThemeButton *self, const gchar *path)
318 gboolean rvalue = FALSE;
319 GString *keyfile_path;
320 GString *unpressed_image_path;
321 GString *pressed_image_path;
322 GString *folder;
323 gchar *folder_name;
324 GKeyFile *keyfile;
325 gchar *cp_cmd_unpressed;
326 gchar *cp_cmd_pressed;
327 gchar *type_unpressed;
328 gchar *type_pressed;
329 GString *unpressed_image_name;
330 GString *pressed_image_name;
331 IRRECO_ENTER
333 keyfile = g_key_file_new();
334 unpressed_image_name = g_string_new(NULL);
335 pressed_image_name = g_string_new(NULL);
336 unpressed_image_path = g_string_new(path);
337 pressed_image_path = g_string_new(path);
338 keyfile_path = g_string_new(path);
339 folder = g_string_new(self->name->str);
340 g_string_ascii_down(folder);
341 folder_name = g_strdup(folder->str);
342 g_strdelimit(folder_name, " ", '_');
343 g_strdelimit(folder_name, ",-|> <.", 'a');
345 g_string_printf(folder, "%s", folder_name);
347 /* get file type */
348 type_unpressed = g_strrstr(self->image_up->str, ".");
349 type_pressed = g_strrstr(self->image_down->str, ".");
351 g_string_printf(unpressed_image_name, "%s%s", "unpressed",
352 type_unpressed);
353 g_string_printf(pressed_image_name, "%s%s", "pressed", type_pressed);
355 /* Create Folder */
356 g_string_append_printf(unpressed_image_path, "/%s", folder_name);
357 g_string_append_printf(pressed_image_path, "/%s", folder_name);
359 g_string_append_printf(keyfile_path, "/%s", folder_name);
361 g_mkdir(unpressed_image_path->str, 0777);
363 g_string_append_printf(unpressed_image_path, "/%s%s", "unpressed",
364 type_unpressed);
365 g_string_append_printf(pressed_image_path, "/%s%s", "pressed",
366 type_pressed);
368 /* Copy images to button folder */
370 cp_cmd_unpressed = g_strconcat("cp -f ", self->image_up->str, " ",
371 unpressed_image_path->str, NULL);
372 cp_cmd_pressed = g_strconcat("cp -f ", self->image_down->str, " ",
373 pressed_image_path->str, NULL);
374 system(cp_cmd_unpressed);
375 system(cp_cmd_pressed);
376 g_free(cp_cmd_unpressed);
377 g_free(cp_cmd_pressed);
379 /* Create keyfile and save it to folder*/
381 irreco_gkeyfile_set_string(keyfile, "theme-button",
382 "up", unpressed_image_name->str);
384 irreco_gkeyfile_set_string(keyfile, "theme-button",
385 "down", pressed_image_name->str);
387 irreco_gkeyfile_set_string(keyfile, "theme-button" , "name",
388 self->name->str);
390 if (self->allow_text) {
391 irreco_gkeyfile_set_string(keyfile, "theme-button",
392 "allow-text", "true");
393 } else {
394 irreco_gkeyfile_set_string(keyfile, "theme-button",
395 "allow-text", "false");
398 if (self->text_format_up != NULL && strlen(self->text_format_up->str) > 0) {
399 irreco_gkeyfile_set_string(keyfile, "theme-button",
400 "text-format-up", self->text_format_up->str);
403 if (self->text_format_down != NULL && strlen(self->text_format_down->str) > 0) {
404 irreco_gkeyfile_set_string(keyfile, "theme-button",
405 "text-format-down", self->text_format_down->str);
408 irreco_gkeyfile_set_glong(keyfile, "theme-button",
409 "text-padding", (glong) self->text_padding);
411 irreco_gkeyfile_set_gfloat(keyfile,
412 "theme-button",
413 "text-h-align",
414 (gfloat) self->text_h_align);
416 irreco_gkeyfile_set_gfloat(keyfile,
417 "theme-button",
418 "text-v-align",
419 (gfloat) self->text_v_align);
421 g_string_append_printf(keyfile_path, "/button.conf");
422 irreco_write_keyfile(keyfile, keyfile_path->str);
424 rvalue = TRUE;
425 g_key_file_free(keyfile);
426 g_string_free(unpressed_image_name, TRUE);
427 g_string_free(pressed_image_name, TRUE);
428 g_string_free(keyfile_path, TRUE);
429 g_string_free(unpressed_image_path, TRUE);
430 g_string_free(pressed_image_path, TRUE);
431 IRRECO_RETURN_BOOL(rvalue);
434 /** @} */
436 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
437 /* Events and Callbacks */
438 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
440 /** @} */