fixed conflict merge with master
[livewp] / applet / src / livewp-settings.c
1 /* vim: set sw=4 ts=4 et: */
2 /*
3  * This file is part of Live Wallpaper (livewp)
4  * 
5  * Copyright (C) 2010 Vlad Vasiliev
6  * Copyright (C) 2010 Tanya Makova
7  *       for the code
8  * 
9  * This software is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public License
11  * as published by the Free Software Foundation; either version 2.1 of
12  * the License, or (at your option) any later version.
13  * 
14  * This software is distributed in the hope that it will be useful, but
15  * WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  * Lesser General Public License for more details.
18  * 
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with this software; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
22  * 02110-1301 USA
23 */
24 /*******************************************************************************/
25 #include "livewp-settings.h"
26 /*******************************************************************************/
27 void lw_about(void){
28
29     GtkWidget *window = NULL,
30     *vbox = NULL,
31     *label_about = NULL;
32     window = gtk_dialog_new();
33     gtk_window_set_title(GTK_WINDOW(window), _("About"));
34     gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
35     gtk_window_set_modal(GTK_WINDOW(window), TRUE);
36     vbox = gtk_vbox_new (FALSE, 5);
37     label_about = gtk_label_new (_("Live Wallpaper Version 0.7 \n Copyright(c) 2010\n \
38 Tanya Makova\n Vlad Vasiliev\n \
39 Copyright(c) 2010 for design themes Berlin and Modern Vasya Bobrikov\n \
40 Copyright(c) 2010 for design theme Matrix Andrew Zhilin\n \
41 Translators:\n \
42 Finnish - Marko Vertainen\n \
43 Spain  - Alejandro López\n \
44 Russian - Tanya Makova \n \
45           Vlad Vasiliev\n")); 
46     gtk_box_pack_start (GTK_BOX (vbox), label_about, FALSE, FALSE, 0);
47     gtk_box_pack_start(GTK_BOX(GTK_DIALOG(window)->vbox),
48                                    vbox, TRUE, TRUE, 0);
49     gtk_widget_show (label_about);
50     gtk_widget_show (vbox);
51     gtk_widget_show (window);
52     gtk_dialog_run(GTK_DIALOG(window));
53
54 }
55 /*******************************************************************************/
56 GtkWidget *
57 create_theme_selector (void){
58       GtkWidget *selector;
59
60       selector = hildon_touch_selector_new_text ();
61
62       hildon_touch_selector_append_text (HILDON_TOUCH_SELECTOR (selector), _("Berlin"));
63       hildon_touch_selector_append_text (HILDON_TOUCH_SELECTOR (selector), _("Modern"));
64       hildon_touch_selector_append_text (HILDON_TOUCH_SELECTOR (selector), _("Matrix"));
65       hildon_touch_selector_append_text (HILDON_TOUCH_SELECTOR (selector), _("Accel"));
66       hildon_touch_selector_append_text (HILDON_TOUCH_SELECTOR (selector), _("Video"));
67       return selector;
68 }
69 /*******************************************************************************/
70 void
71 theme_button_clicked(GtkButton *button, Animation_WallpaperPrivate *priv){
72
73     lw_theme_settings(priv, button);
74 }
75 /********************************************************************************/
76 void
77 set_button_image(GtkWidget * button, gchar *theme, gboolean enable){
78     GtkWidget * image = NULL; 
79     GdkPixbuf * pixbuf = NULL;
80     gchar *str = NULL;
81     if (enable)
82         str = g_strdup_printf( "%s/%s/%s", THEME_PATH, 
83                         theme, "icon.png");
84     else 
85         str = g_strdup_printf( "%s/%s/%s", THEME_PATH, 
86                         theme, "icond.png");
87     pixbuf = gdk_pixbuf_new_from_file_at_size (str, 
88                                              100, 
89                                              60, 
90                                              NULL);
91     if (str)
92         g_free(str);
93     if (pixbuf){
94         image = gtk_image_new_from_pixbuf (pixbuf);
95         g_object_unref(G_OBJECT(pixbuf));
96     }
97
98    hildon_button_set_image (HILDON_BUTTON (button), image);
99 }
100 /********************************************************************************/
101 GtkWidget *
102 create_image_button (gint view, DBusConnection *conn_sess){
103     GtkWidget *button;
104     GtkWidget *image;
105     gchar * str;
106     GdkPixbuf * pixbuf;
107     
108     Animation_WallpaperPrivate *priv = g_new0(Animation_WallpaperPrivate, 1);
109     priv->view = view;
110     priv->dbus_conn_session = conn_sess;
111     read_config(priv);
112     button = hildon_button_new(HILDON_SIZE_AUTO_WIDTH | HILDON_SIZE_FINGER_HEIGHT,
113                                     HILDON_BUTTON_ARRANGEMENT_VERTICAL);
114     g_object_set_data(G_OBJECT(button), "view", GINT_TO_POINTER(view));
115     set_button_image(button, priv->theme, check_applet_state(view));
116     g_signal_connect(button, "clicked", G_CALLBACK(theme_button_clicked), priv);
117     hildon_button_set_image_position (HILDON_BUTTON (button), GTK_POS_RIGHT);
118     return button;
119
120 }
121 /********************************************************************************/
122 GtkWidget *
123 create_themes_button (gchar *theme){
124
125     GtkWidget *button;
126     GtkWidget *selector;
127
128     selector = create_theme_selector();
129     button = hildon_picker_button_new (HILDON_SIZE_AUTO, HILDON_BUTTON_ARRANGEMENT_VERTICAL);
130     hildon_button_set_title (HILDON_BUTTON (button), _("Theme"));
131     hildon_picker_button_set_selector (HILDON_PICKER_BUTTON (button),
132                                                        HILDON_TOUCH_SELECTOR (selector));
133     if (theme) {
134         if (!strcmp(theme, "Berlin")){
135             hildon_touch_selector_set_active (HILDON_TOUCH_SELECTOR (selector), 0, 0);
136             hildon_button_set_value(HILDON_BUTTON(button), _("Berlin"));
137         }
138         if (!strcmp(theme, "Modern")){
139             hildon_touch_selector_set_active (HILDON_TOUCH_SELECTOR (selector), 0, 1);
140             hildon_button_set_value(HILDON_BUTTON(button), _("Modern"));
141         }
142         if (!strcmp(theme, "Matrix")){
143             hildon_touch_selector_set_active (HILDON_TOUCH_SELECTOR (selector), 0, 2);
144             hildon_button_set_value(HILDON_BUTTON(button), _("Matrix"));
145         }
146         if (!strcmp(theme, "Accel")){
147             hildon_touch_selector_set_active (HILDON_TOUCH_SELECTOR (selector), 0, 2);
148             hildon_button_set_value(HILDON_BUTTON(button), _("Accel"));
149         }
150
151         if (!strcmp(theme, "Video")){
152             hildon_touch_selector_set_active (HILDON_TOUCH_SELECTOR (selector), 0, 3);
153             hildon_button_set_value(HILDON_BUTTON(button), _("Video"));
154         }
155
156     }
157     return button;
158 }
159
160 /*******************************************************************************/
161 GtkWidget *
162 create_rich_animation_button (gboolean active)
163 {
164     GtkWidget *button;
165     button = hildon_check_button_new (HILDON_SIZE_AUTO);
166     gtk_button_set_label (GTK_BUTTON (button), _("Rich Animation"));
167     hildon_check_button_set_active(HILDON_CHECK_BUTTON(button), active);
168     return button;
169 }
170 /*******************************************************************************/
171 GtkWidget *
172 create_enable_button (gboolean active)
173 {
174     GtkWidget *button;
175     button = hildon_check_button_new (HILDON_SIZE_AUTO);
176     gtk_button_set_label (GTK_BUTTON (button), _("Enable"));
177     hildon_check_button_set_active(HILDON_CHECK_BUTTON(button), active);
178     return button;
179 }
180
181 /*******************************************************************************/
182 void
183 show_settings(GtkWidget *widget, Animation_WallpaperPrivate *priv){
184     lw_main_settings(priv, NULL);
185 }
186 /*******************************************************************************/
187 void 
188 lw_main_settings(Animation_WallpaperPrivate *priv, gpointer data){
189     gint result;
190     GtkWidget *window = NULL;
191     GtkWidget *theme_button1;
192     GtkWidget *theme_button2;
193     GtkWidget *theme_button3;
194     GtkWidget *theme_button4;
195     GtkWidget *hbox;
196
197     window = gtk_dialog_new();
198
199     gtk_window_set_title(GTK_WINDOW(window), _("Live Wallpaper Settings"));
200     gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
201     gtk_window_set_modal(GTK_WINDOW(window), TRUE);
202     /* Create Theme button */
203     hbox = gtk_hbox_new(FALSE, 5);
204     theme_button1 = create_image_button(1, priv->dbus_conn_session);
205     gtk_box_pack_start(GTK_BOX(hbox),
206                                    theme_button1, TRUE, TRUE, 5);
207     theme_button2 = create_image_button(2, priv->dbus_conn_session);
208     gtk_box_pack_start(GTK_BOX(hbox),
209                                    theme_button2, TRUE, TRUE, 5);
210     theme_button3 = create_image_button(3, priv->dbus_conn_session);
211     gtk_box_pack_start(GTK_BOX(hbox),
212                                    theme_button3, TRUE, TRUE, 5);
213     theme_button4 = create_image_button(4, priv->dbus_conn_session);
214     gtk_box_pack_start(GTK_BOX(hbox),
215                                    theme_button4, TRUE, TRUE, 5);
216     gtk_box_pack_start(GTK_BOX(GTK_DIALOG(window)->vbox),
217                                    hbox, TRUE, TRUE, 5);
218
219     gtk_widget_show (theme_button1);
220     gtk_widget_show (theme_button2);
221     gtk_widget_show (theme_button3);
222     gtk_widget_show (theme_button4);
223     gtk_widget_show_all (hbox);
224     gtk_widget_show (window);
225     gtk_dialog_add_button(GTK_DIALOG(window), _("About"), GTK_RESPONSE_NO);
226
227     result = gtk_dialog_run(GTK_DIALOG(window));
228
229     switch(result){
230         case GTK_RESPONSE_NO:
231             gtk_widget_destroy(window);
232             window = NULL;
233             lw_about();
234         break;
235     }
236     if (window)
237         gtk_widget_destroy(window);
238 }
239
240 /*******************************************************************************/
241 void 
242 lw_theme_settings(Animation_WallpaperPrivate *priv, GtkWidget *button){
243     gint result;
244     GtkWidget *window = NULL;
245     GtkWidget *save_button;
246     GtkWidget *theme_button;
247     GtkWidget *enable_button;
248     GtkWidget *rich_animation_button;
249     gint view = priv->view;
250
251     window = gtk_dialog_new();
252
253     gtk_window_set_title(GTK_WINDOW(window), _("View Settings"));
254     gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
255     gtk_window_set_modal(GTK_WINDOW(window), TRUE);
256     /* Create Enable button */
257     enable_button = create_enable_button(check_applet_state(view)); 
258     gtk_box_pack_start(GTK_BOX(GTK_DIALOG(window)->vbox),
259                                    enable_button, TRUE, TRUE, 5);
260     /* Create Theme button */
261     theme_button = create_themes_button(priv->theme);
262     gtk_box_pack_start(GTK_BOX(GTK_DIALOG(window)->vbox),
263                                    theme_button, TRUE, TRUE, 5);
264     /* Create rich animation button */  
265     rich_animation_button = create_rich_animation_button(priv->rich_animation);
266     gtk_box_pack_start(GTK_BOX(GTK_DIALOG(window)->vbox),
267                                    rich_animation_button, TRUE, TRUE, 5);
268     gtk_widget_show (enable_button);
269     gtk_widget_show (theme_button);
270     gtk_widget_show (rich_animation_button);
271     gtk_widget_show (window);
272     save_button = gtk_dialog_add_button(GTK_DIALOG(window), _("Save"), GTK_RESPONSE_YES);
273
274     result = gtk_dialog_run(GTK_DIALOG(window));
275
276     switch(result){
277         case GTK_RESPONSE_YES:
278             /* Check theme */
279             if (hildon_button_get_value(HILDON_BUTTON (theme_button))){
280
281                 if (priv->theme)
282                     g_free(priv->theme);
283                 if (priv->theme_string_parametr1)
284                     g_free(priv->theme_string_parametr1);
285                 priv->theme_string_parametr1 = NULL;
286
287                 if (!strcmp(hildon_button_get_value(HILDON_BUTTON (theme_button)), _("Berlin")))
288                     priv->theme = g_strdup("Berlin");
289                 if (!strcmp(hildon_button_get_value(HILDON_BUTTON (theme_button)), _("Modern")))
290                     priv->theme = g_strdup("Modern");
291                 if (!strcmp(hildon_button_get_value(HILDON_BUTTON (theme_button)), _("Matrix")))
292                     priv->theme = g_strdup("Matrix");
293                 if (!strcmp(hildon_button_get_value(HILDON_BUTTON (theme_button)), _("Accel")))
294                     priv->theme = g_strdup("Accel");
295                 if (!strcmp(hildon_button_get_value(HILDON_BUTTON (theme_button)), _("Video"))){
296                     priv->theme = g_strdup("Video");
297                     priv->theme_string_parametr1 = g_strdup("/home/user/MyDocs/.videos/9.mp4");
298                 }
299
300             }
301             /* Check rich animation */
302             if (hildon_check_button_get_active (HILDON_CHECK_BUTTON(rich_animation_button)))
303                 priv->rich_animation = TRUE;
304             else
305                 priv->rich_animation = FALSE;
306
307             /* Save config */
308             save_config(priv);
309             /* action with applet */
310             if (hildon_check_button_get_active (HILDON_CHECK_BUTTON(enable_button))){
311                     if (!check_applet_state(view)){
312                         start_applet(view);
313                     }else {
314                         send_dbus_signal (priv,
315                               LIVEWP_SIGNAL_INTERFACE,
316                               LIVEWP_SIGNAL_PATH,
317                               LIVEWP_RELOAD_CONFIG);
318                     }
319             }else
320                     if (check_applet_state(view))
321                         stop_applet(view);
322
323             set_button_image(button, priv->theme, check_applet_state(view));
324             break;
325         default:
326         case GTK_RESPONSE_OK:
327         break;
328         case GTK_RESPONSE_NO:
329             gtk_widget_destroy(window);
330             window = NULL;
331             lw_about();
332         break;
333     }
334     if (window)
335         gtk_widget_destroy(window);
336 }
337 /*******************************************************************************/
338 gboolean
339 check_applet_state(gint number){
340
341     HDConfigFile *config_file = NULL;
342     GKeyFile *gkey_file = NULL;
343     gchar *str = NULL;
344     gboolean result = FALSE;
345     if (number > 4 || number < 1)
346         return FALSE;
347
348     str = g_strdup_printf("livewp-home-widget.desktop-%i", (number - 1));
349     config_file = hd_config_file_new(NULL, "/home/user/.config/hildon-desktop/", "home.plugins");
350     
351     gkey_file = hd_config_file_load_file(config_file, FALSE);
352     if (gkey_file && str){
353         result = g_key_file_has_group(gkey_file, str);
354         g_free(str);
355     }
356     return result;
357 #if 0
358     FILE    *file_in = NULL;
359     gchar buffer[2048];
360
361     file_in = fopen("/home/user/.config/hildon-desktop/home.plugins","r");
362     if (file_in){
363
364         fprintf(stderr,"Check applet state\n");
365         while (!feof(file_in)) {
366                 memset(buffer, 0, sizeof(buffer));
367                 fgets(buffer, sizeof(buffer) - 1, file_in);
368                 if (!strcmp(buffer, 
369                             "X-Desktop-File=/usr/share/applications/hildon-home/livewp-home-widget.desktop\n")){
370                     result = TRUE;
371                     break;
372                 }
373        }
374         fclose(file_in);
375     }
376 #endif
377 }
378 /*******************************************************************************/
379 void
380 start_applet(gint number){
381
382     HDConfigFile *config_file = NULL;
383     GKeyFile *gkey_file = NULL;
384     gchar *str = NULL;
385
386     if (number > 4 || number < 1)
387         return;
388     str = g_strdup_printf("livewp-home-widget.desktop-%i",(number - 1));
389     config_file = hd_config_file_new(NULL, "/home/user/.config/hildon-desktop/", "home.plugins");
390     
391     gkey_file = hd_config_file_load_file(config_file, FALSE);
392     if (gkey_file){
393         g_key_file_set_string (gkey_file, str, "X-Desktop-File", "/usr/share/applications/hildon-home/livewp-home-widget.desktop");
394         hd_config_file_save_file( config_file, gkey_file);
395         g_key_file_free(gkey_file);
396     }else
397         fprintf(stderr, "Problem with config file");
398     if (str)
399         g_free(str);
400     g_object_unref(config_file);
401 }
402 /*******************************************************************************/
403 void
404 stop_applet(gint number){
405     HDConfigFile *config_file = NULL;
406     GKeyFile *gkey_file = NULL;
407     gchar *str = NULL;
408
409     if (number > 4 || number < 1)
410         return;
411     str = g_strdup_printf("livewp-home-widget.desktop-%i", (number - 1));
412     config_file = hd_config_file_new(NULL, "/home/user/.config/hildon-desktop/", "home.plugins");
413     
414     gkey_file = hd_config_file_load_file(config_file, FALSE);
415     if (gkey_file){
416          g_key_file_remove_group(gkey_file, str, NULL);  
417         hd_config_file_save_file( config_file, gkey_file);
418         g_key_file_free(gkey_file);
419     }else
420         fprintf(stderr, "Problem with config file");
421     if (str)
422         g_free(str);
423     g_object_unref(config_file);
424
425 #if 0    
426     FILE    *file_in;
427     FILE    *file_out;
428     gchar buffer[2048];
429     gchar * str = NULL;
430
431     file_in = fopen("/home/user/.config/hildon-desktop/home.plugins","r");
432     file_out = fopen("/tmp/livewallpaper.plugins","w");
433     if (file_in && file_out){
434         while (!feof(file_in)) {
435             memset(buffer, 0, sizeof(buffer));
436             fgets(buffer, sizeof(buffer) - 1, file_in);
437             str = g_strdup_printf("[livewp-home-widget.desktop-%i]\n", number);
438             if (strcmp(buffer, str) &&
439                 strcmp(buffer, "X-Desktop-File=/usr/share/applications/hildon-home/livewp-home-widget.desktop\n"))
440                 fputs(buffer, file_out);
441         }
442         if (str){
443             g_free(str);
444         }
445         fclose(file_out);
446         fclose(file_in);
447         file_in = fopen("/tmp/livewallpaper.plugins","r");
448         file_out = fopen("/home/user/.config/hildon-desktop/home.plugins","w");
449         if (file_in && file_out){
450             while (!feof(file_in)){
451                 memset(buffer, 0, sizeof(buffer));
452                 fgets(buffer, sizeof(buffer) - 1, file_in);
453                 fputs(buffer, file_out);
454             }
455             fclose(file_out);
456             fclose(file_in);
457             unlink ("/tmp/livewallpaper.plugins");
458         }
459     }
460 #endif
461 }