clean code and changed version nro.
[irreco.git] / irreco / src / core / irreco_theme_button.c
blob448772bd105b0022a5271ef0669140308d124eed
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(theme_name);
242 irreco_theme_button_read(self, dir);
243 IRRECO_RETURN_PTR(self);
246 void irreco_theme_button_read(IrrecoThemeButton *self, const gchar *dir)
248 IrrecoKeyFile *keyfile;
249 GString *conf = NULL;
250 gchar *name = NULL;
251 gchar *up = NULL;
252 gchar *down = NULL;
253 gboolean allow_text;
254 gchar *text_format_up = NULL;
255 gchar *text_format_down = NULL;
256 gint text_padding = 5;
257 gfloat text_h_align = 0.5;
258 gfloat text_v_align = 0.5;
259 GString *style_name = g_string_new(self->theme_name->str);
260 IRRECO_ENTER
262 conf = g_string_new(dir);
263 g_string_append_printf(conf, "/button.conf");
264 keyfile = irreco_keyfile_create(dir,
265 conf->str,
266 "theme-button");
267 if (keyfile == NULL) goto end;
269 /* Required fields. */
270 if (!irreco_keyfile_get_str(keyfile, "name", &name) ||
271 !irreco_keyfile_get_path(keyfile, "up", &up)) {
272 IRRECO_PRINTF("Could not read style from group \"%s\"\n",
273 keyfile->group);
274 goto end;
277 /* Optional fields. */
278 irreco_keyfile_get_path(keyfile, "down", &down);
279 irreco_keyfile_get_bool(keyfile, "allow-text", &allow_text);
280 irreco_keyfile_get_str(keyfile, "text-format-up", &text_format_up);
281 irreco_keyfile_get_str(keyfile, "text-format-down", &text_format_down);
282 irreco_keyfile_get_int(keyfile, "text-padding", &text_padding);
283 irreco_keyfile_get_float(keyfile, "text-h-align", &text_h_align);
284 irreco_keyfile_get_float(keyfile, "text-v-align", &text_v_align);
286 g_string_append_printf(style_name,"/%s", name);
288 irreco_theme_button_set(self, style_name->str,
289 name, allow_text,
290 up, down,
291 text_format_up, text_format_down,
292 text_padding,
293 text_h_align,
294 text_v_align);
296 irreco_theme_button_print(self);
298 end:
299 if(keyfile != NULL) irreco_keyfile_destroy(keyfile);
300 if(name != NULL) g_free(name);
301 if(name != NULL) g_free(up);
302 if(name != NULL) g_free(down);
303 if(name != NULL) g_free(text_format_up);
304 if(name != NULL) g_free(text_format_down);
305 g_string_free(style_name, TRUE);
307 IRRECO_RETURN
311 * Save button to theme folder
314 gboolean
315 irreco_theme_button_save(IrrecoThemeButton *self, const gchar *path)
317 gboolean rvalue = FALSE;
318 GString *keyfile_path;
319 GString *unpressed_image_path;
320 GString *pressed_image_path;
321 GString *folder;
322 gchar *folder_name;
323 GKeyFile *keyfile;
324 gchar *cp_cmd_unpressed;
325 gchar *cp_cmd_pressed;
326 gchar *type_unpressed;
327 gchar *type_pressed;
328 GString *unpressed_image_name;
329 GString *pressed_image_name;
330 IRRECO_ENTER
332 keyfile = g_key_file_new();
333 unpressed_image_name = g_string_new(NULL);
334 pressed_image_name = g_string_new(NULL);
335 unpressed_image_path = g_string_new(path);
336 pressed_image_path = g_string_new(path);
337 keyfile_path = g_string_new(path);
338 folder = g_string_new(self->name->str);
339 g_string_ascii_down(folder);
340 folder_name = g_strdup(folder->str);
341 g_strdelimit(folder_name, " ", '_');
342 g_strdelimit(folder_name, ",-|> <.", 'a');
344 g_string_printf(folder, "%s", folder_name);
346 /* get file type */
347 type_unpressed = g_strrstr(self->image_up->str, ".");
348 type_pressed = g_strrstr(self->image_down->str, ".");
350 g_string_printf(unpressed_image_name, "%s%s", "unpressed",
351 type_unpressed);
352 g_string_printf(pressed_image_name, "%s%s", "pressed", type_pressed);
354 /* Create Folder */
355 g_string_append_printf(unpressed_image_path, "/%s", folder_name);
356 g_string_append_printf(pressed_image_path, "/%s", folder_name);
358 g_string_append_printf(keyfile_path, "/%s", folder_name);
360 g_mkdir(unpressed_image_path->str, 0777);
362 g_string_append_printf(unpressed_image_path, "/%s%s", "unpressed",
363 type_unpressed);
364 g_string_append_printf(pressed_image_path, "/%s%s", "pressed",
365 type_pressed);
367 /* Copy images to button folder */
369 cp_cmd_unpressed = g_strconcat("cp -f ", self->image_up->str, " ",
370 unpressed_image_path->str, NULL);
371 cp_cmd_pressed = g_strconcat("cp -f ", self->image_down->str, " ",
372 pressed_image_path->str, NULL);
373 system(cp_cmd_unpressed);
374 system(cp_cmd_pressed);
375 g_free(cp_cmd_unpressed);
376 g_free(cp_cmd_pressed);
378 /* Create keyfile and save it to folder*/
380 irreco_gkeyfile_set_string(keyfile, "theme-button",
381 "up", unpressed_image_name->str);
383 irreco_gkeyfile_set_string(keyfile, "theme-button",
384 "down", pressed_image_name->str);
386 irreco_gkeyfile_set_string(keyfile, "theme-button" , "name",
387 self->name->str);
389 if (self->allow_text) {
390 irreco_gkeyfile_set_string(keyfile, "theme-button",
391 "allow-text", "true");
392 } else {
393 irreco_gkeyfile_set_string(keyfile, "theme-button",
394 "allow-text", "false");
397 if (self->text_format_up != NULL && strlen(self->text_format_up->str) > 0) {
398 irreco_gkeyfile_set_string(keyfile, "theme-button",
399 "text-format-up", self->text_format_up->str);
402 if (self->text_format_down != NULL && strlen(self->text_format_down->str) > 0) {
403 irreco_gkeyfile_set_string(keyfile, "theme-button",
404 "text-format-down", self->text_format_down->str);
407 irreco_gkeyfile_set_glong(keyfile, "theme-button",
408 "text-padding", (glong) self->text_padding);
410 irreco_gkeyfile_set_gfloat(keyfile,
411 "theme-button",
412 "text-h-align",
413 (gfloat) self->text_h_align);
415 irreco_gkeyfile_set_gfloat(keyfile,
416 "theme-button",
417 "text-v-align",
418 (gfloat) self->text_v_align);
420 g_string_append_printf(keyfile_path, "/button.conf");
421 irreco_write_keyfile(keyfile, keyfile_path->str);
423 rvalue = TRUE;
424 g_key_file_free(keyfile);
425 g_string_free(unpressed_image_name, TRUE);
426 g_string_free(pressed_image_name, TRUE);
427 g_string_free(keyfile_path, TRUE);
428 g_string_free(unpressed_image_path, TRUE);
429 g_string_free(pressed_image_path, TRUE);
430 IRRECO_RETURN_BOOL(rvalue);
433 /** @} */
435 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
436 /* Events and Callbacks */
437 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
439 /** @} */