fixed with additional button
[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), _("Video"));
66       return selector;
67 }
68 /*******************************************************************************/
69 void
70 theme_button_clicked(GtkButton *button, Animation_WallpaperPrivate *priv){
71
72     lw_theme_settings(priv, button);
73 }
74 /********************************************************************************/
75 void
76 set_button_image(GtkWidget * button, gchar *theme, gboolean enable){
77     GtkWidget * image = NULL; 
78     GdkPixbuf * pixbuf = NULL;
79     gchar *str = NULL;
80     if (enable)
81         str = g_strdup_printf( "%s/%s/%s", THEME_PATH, 
82                         theme, "icon.png");
83     else 
84         str = g_strdup_printf( "%s/%s/%s", THEME_PATH, 
85                         theme, "icond.png");
86     pixbuf = gdk_pixbuf_new_from_file_at_size (str, 
87                                              100, 
88                                              60, 
89                                              NULL);
90     if (str)
91         g_free(str);
92     if (pixbuf){
93         image = gtk_image_new_from_pixbuf (pixbuf);
94         g_object_unref(G_OBJECT(pixbuf));
95     }
96
97    hildon_button_set_image (HILDON_BUTTON (button), image);
98 }
99 /********************************************************************************/
100 GtkWidget *
101 create_image_button (gint view, DBusConnection *conn_sess){
102     GtkWidget *button;
103     GtkWidget *image;
104     gchar * str;
105     GdkPixbuf * pixbuf;
106     
107     Animation_WallpaperPrivate *priv = g_new0(Animation_WallpaperPrivate, 1);
108     priv->view = view;
109     priv->dbus_conn_session = conn_sess;
110     read_config(priv);
111     button = hildon_button_new(HILDON_SIZE_AUTO_WIDTH | HILDON_SIZE_FINGER_HEIGHT,
112                                     HILDON_BUTTON_ARRANGEMENT_VERTICAL);
113     g_object_set_data(G_OBJECT(button), "view", GINT_TO_POINTER(view));
114     set_button_image(button, priv->theme, check_applet_state(view));
115     g_signal_connect(button, "clicked", G_CALLBACK(theme_button_clicked), priv);
116     hildon_button_set_image_position (HILDON_BUTTON (button), GTK_POS_RIGHT);
117     return button;
118
119 }
120 /********************************************************************************/
121 void
122 changed_value_theme_cb (HildonPickerButton *picker, Animation_WallpaperPrivate *priv)
123 {
124     const gchar *choice = hildon_button_get_value(HILDON_BUTTON (picker));
125     GtkWidget *vbox = NULL;
126         
127     vbox = g_object_get_data(G_OBJECT(priv->window), "custom_vbox");
128     if (vbox)
129         gtk_widget_destroy(vbox);
130     vbox = gtk_vbox_new (FALSE, 5);
131     g_object_set_data(G_OBJECT(priv->window), "custom_vbox", vbox);
132     gtk_box_pack_start(GTK_BOX(GTK_DIALOG(priv->window)->vbox),
133                                    vbox, TRUE, TRUE, 5);
134     if (choice) {
135         if (!strcmp(choice, "Berlin")){
136             rich_animation_additional_parametr(vbox,priv);
137         }
138         if (!strcmp(choice, "Modern")){
139             rich_animation_additional_parametr(vbox,priv);
140         }
141         if (!strcmp(choice, "Matrix")){
142             rich_animation_additional_parametr(vbox,priv);
143         }
144         if (!strcmp(choice, "Video")){
145             additional_parametr_for_theme_video(vbox, priv);
146         }
147
148     }
149     gtk_widget_show(vbox);
150 }
151
152 /********************************************************************************/
153 GtkWidget *
154 create_themes_button (gchar *theme){
155
156     GtkWidget *button;
157     GtkWidget *selector;
158
159     selector = create_theme_selector();
160     button = hildon_picker_button_new (HILDON_SIZE_AUTO, HILDON_BUTTON_ARRANGEMENT_VERTICAL);
161     hildon_button_set_title (HILDON_BUTTON (button), _("Theme"));
162     hildon_picker_button_set_selector (HILDON_PICKER_BUTTON (button),
163                                                        HILDON_TOUCH_SELECTOR (selector));
164     if (theme) {
165         if (!strcmp(theme, "Berlin")){
166             hildon_touch_selector_set_active (HILDON_TOUCH_SELECTOR (selector), 0, 0);
167             hildon_button_set_value(HILDON_BUTTON(button), _("Berlin"));
168         }
169         if (!strcmp(theme, "Modern")){
170             hildon_touch_selector_set_active (HILDON_TOUCH_SELECTOR (selector), 0, 1);
171             hildon_button_set_value(HILDON_BUTTON(button), _("Modern"));
172         }
173         if (!strcmp(theme, "Matrix")){
174             hildon_touch_selector_set_active (HILDON_TOUCH_SELECTOR (selector), 0, 2);
175             hildon_button_set_value(HILDON_BUTTON(button), _("Matrix"));
176         }
177         if (!strcmp(theme, "Video")){
178             hildon_touch_selector_set_active (HILDON_TOUCH_SELECTOR (selector), 0, 3);
179             hildon_button_set_value(HILDON_BUTTON(button), _("Video"));
180         }
181
182     }
183     
184     return button;
185 }
186
187 /*******************************************************************************/
188 GtkWidget *
189 create_rich_animation_button (gboolean active)
190 {
191     GtkWidget *button;
192     button = hildon_check_button_new (HILDON_SIZE_AUTO);
193     gtk_button_set_label (GTK_BUTTON (button), _("Rich Animation"));
194     hildon_check_button_set_active(HILDON_CHECK_BUTTON(button), active);
195     return button;
196 }
197 /*******************************************************************************/
198 GtkWidget *
199 create_enable_button (gboolean active)
200 {
201     GtkWidget *button;
202     button = hildon_check_button_new (HILDON_SIZE_AUTO);
203     gtk_button_set_label (GTK_BUTTON (button), _("Enable"));
204     hildon_check_button_set_active(HILDON_CHECK_BUTTON(button), active);
205     return button;
206 }
207
208 /*******************************************************************************/
209 void
210 show_settings(GtkWidget *widget, Animation_WallpaperPrivate *priv){
211     lw_main_settings(priv, NULL);
212 }
213 /*******************************************************************************/
214 void 
215 lw_main_settings(Animation_WallpaperPrivate *priv, gpointer data){
216     gint result;
217     GtkWidget *window = NULL;
218     GtkWidget *theme_button1;
219     GtkWidget *theme_button2;
220     GtkWidget *theme_button3;
221     GtkWidget *theme_button4;
222     GtkWidget *hbox;
223
224     window = gtk_dialog_new();
225
226     gtk_window_set_title(GTK_WINDOW(window), _("Live Wallpaper Settings"));
227     gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
228     gtk_window_set_modal(GTK_WINDOW(window), TRUE);
229     /* Create Theme button */
230     hbox = gtk_hbox_new(FALSE, 5);
231     theme_button1 = create_image_button(1, priv->dbus_conn_session);
232     gtk_box_pack_start(GTK_BOX(hbox),
233                                    theme_button1, TRUE, TRUE, 5);
234     theme_button2 = create_image_button(2, priv->dbus_conn_session);
235     gtk_box_pack_start(GTK_BOX(hbox),
236                                    theme_button2, TRUE, TRUE, 5);
237     theme_button3 = create_image_button(3, priv->dbus_conn_session);
238     gtk_box_pack_start(GTK_BOX(hbox),
239                                    theme_button3, TRUE, TRUE, 5);
240     theme_button4 = create_image_button(4, priv->dbus_conn_session);
241     gtk_box_pack_start(GTK_BOX(hbox),
242                                    theme_button4, TRUE, TRUE, 5);
243     gtk_box_pack_start(GTK_BOX(GTK_DIALOG(window)->vbox),
244                                    hbox, TRUE, TRUE, 5);
245
246     gtk_widget_show (theme_button1);
247     gtk_widget_show (theme_button2);
248     gtk_widget_show (theme_button3);
249     gtk_widget_show (theme_button4);
250     gtk_widget_show_all (hbox);
251     gtk_widget_show (window);
252     gtk_dialog_add_button(GTK_DIALOG(window), _("About"), GTK_RESPONSE_NO);
253
254     result = gtk_dialog_run(GTK_DIALOG(window));
255
256     switch(result){
257         case GTK_RESPONSE_NO:
258             gtk_widget_destroy(window);
259             window = NULL;
260             lw_about();
261         break;
262     }
263     if (window)
264         gtk_widget_destroy(window);
265 }
266 /*******************************************************************************/
267 void
268 file_button_clicked(GtkButton *button, Animation_WallpaperPrivate *priv){
269
270     GtkWidget *dialog = hildon_file_chooser_dialog_new(GTK_WINDOW (priv->window), GTK_FILE_CHOOSER_ACTION_OPEN);
271
272     if (priv->theme_string_parametr1)
273         gtk_file_chooser_set_current_name(GTK_FILE_CHOOSER(dialog), priv->theme_string_parametr1);
274
275     gtk_widget_show_all (GTK_WIDGET (dialog));
276
277     if (gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_OK)
278     {
279       hildon_button_set_value (button, gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dialog)));
280     }
281     gtk_widget_destroy (dialog);
282
283 }
284 /*******************************************************************************/
285 void
286 rich_animation_additional_parametr(GtkWidget *vbox, Animation_WallpaperPrivate *priv){
287     GtkWidget *rich_animation_button;
288     /* Create rich animation button */  
289     rich_animation_button = create_rich_animation_button(priv->rich_animation);
290     gtk_box_pack_start(GTK_BOX(vbox),
291                                    rich_animation_button, TRUE, TRUE, 5);
292     g_object_set_data(G_OBJECT(priv->window), "rich_animation_button", rich_animation_button);
293     gtk_widget_show (rich_animation_button);
294 }
295 /*******************************************************************************/
296 void
297 additional_parametr_for_theme_video(GtkWidget *vbox, Animation_WallpaperPrivate *priv){
298
299     GtkWidget *file_button;
300
301     if (priv->theme_string_parametr1)
302         file_button = hildon_button_new_with_text (HILDON_SIZE_FINGER_HEIGHT, HILDON_BUTTON_ARRANGEMENT_VERTICAL,
303                                                    _("Play file"), priv->theme_string_parametr1);
304     else
305         file_button = hildon_button_new_with_text (HILDON_SIZE_FINGER_HEIGHT,HILDON_BUTTON_ARRANGEMENT_VERTICAL,
306                                                    _("Play file"),"");
307
308     g_signal_connect (file_button, "clicked", G_CALLBACK (file_button_clicked), priv);
309
310     gtk_box_pack_start(GTK_BOX(vbox),
311                                    file_button, TRUE, TRUE, 5);
312     g_object_set_data(G_OBJECT(priv->window), "filename_button", file_button);
313     gtk_widget_show (file_button);
314
315 }
316 /*******************************************************************************/
317 void 
318 lw_theme_settings(Animation_WallpaperPrivate *priv, GtkWidget *button){
319     gint result;
320     GtkWidget *window = NULL;
321     GtkWidget *save_button;
322     GtkWidget *theme_button;
323     GtkWidget *enable_button;
324     GtkWidget *vbox;
325     GtkWidget *button1 = NULL;
326     GtkWidget *rich_animation_button = NULL;
327     gint view = priv->view;
328
329     window = gtk_dialog_new();
330     priv->window = window;
331
332     gtk_window_set_title(GTK_WINDOW(window), _("View Settings"));
333     gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
334     gtk_window_set_modal(GTK_WINDOW(window), TRUE);
335     /* Create Enable button */
336     enable_button = create_enable_button(check_applet_state(view)); 
337     gtk_box_pack_start(GTK_BOX(GTK_DIALOG(window)->vbox),
338                                    enable_button, TRUE, TRUE, 5);
339     /* Create Theme button */
340     theme_button = create_themes_button(priv->theme);
341     g_signal_connect (G_OBJECT (theme_button), "value-changed",  G_CALLBACK (changed_value_theme_cb), priv);
342     gtk_box_pack_start(GTK_BOX(GTK_DIALOG(window)->vbox),
343                                    theme_button, TRUE, TRUE, 5);
344
345     /* Create custom vbox */
346     vbox = gtk_vbox_new (FALSE, 5);
347     g_object_set_data(G_OBJECT(window), "custom_vbox", vbox);
348     gtk_box_pack_start(GTK_BOX(GTK_DIALOG(window)->vbox),
349                                    vbox, TRUE, TRUE, 5);
350     if (!strcmp(hildon_button_get_value(HILDON_BUTTON (theme_button)), _("Berlin"))||
351         !strcmp(hildon_button_get_value(HILDON_BUTTON (theme_button)), _("Matrix"))||
352         !strcmp(hildon_button_get_value(HILDON_BUTTON (theme_button)), _("Modern"))){
353         rich_animation_additional_parametr(vbox, priv);
354     }
355
356     if (!strcmp(hildon_button_get_value(HILDON_BUTTON (theme_button)), _("Video"))){
357         additional_parametr_for_theme_video(vbox, priv);
358     } 
359     gtk_widget_show (enable_button);
360     gtk_widget_show (theme_button);
361     gtk_widget_show (vbox);
362     gtk_widget_show (window);
363     save_button = gtk_dialog_add_button(GTK_DIALOG(window), _("Save"), GTK_RESPONSE_YES);
364
365     result = gtk_dialog_run(GTK_DIALOG(window));
366
367     switch(result){
368         case GTK_RESPONSE_YES:
369             /* Check theme */
370             if (hildon_button_get_value(HILDON_BUTTON (theme_button))){
371
372                 if (priv->theme)
373                     g_free(priv->theme);
374
375                 if (!strcmp(hildon_button_get_value(HILDON_BUTTON (theme_button)), _("Berlin")))
376                     priv->theme = g_strdup("Berlin");
377                 if (!strcmp(hildon_button_get_value(HILDON_BUTTON (theme_button)), _("Modern")))
378                     priv->theme = g_strdup("Modern");
379                 if (!strcmp(hildon_button_get_value(HILDON_BUTTON (theme_button)), _("Matrix")))
380                     priv->theme = g_strdup("Matrix");
381                 if (!strcmp(hildon_button_get_value(HILDON_BUTTON (theme_button)), _("Video"))){
382                     priv->theme = g_strdup("Video");
383                     button1 = g_object_get_data(G_OBJECT(priv->window), "filename_button");
384                     if (button1){
385                         if (priv->theme_string_parametr1)
386                             g_free(priv->theme_string_parametr1);
387                         priv->theme_string_parametr1 = hildon_button_get_value (button1);
388                     }
389                 }
390
391             }
392
393             rich_animation_button = g_object_get_data(G_OBJECT(priv->window), "filename_button");
394             if (rich_animation_button){
395                 /* Check rich animation */
396                 if (hildon_check_button_get_active (HILDON_CHECK_BUTTON(rich_animation_button)))
397                     priv->rich_animation = TRUE;
398                 else
399                     priv->rich_animation = FALSE;
400             }
401             /* Save config */
402             save_config(priv);
403             /* action with applet */
404             if (hildon_check_button_get_active (HILDON_CHECK_BUTTON(enable_button))){
405                     if (!check_applet_state(view)){
406                         start_applet(view);
407                     }else {
408                         send_dbus_signal (priv,
409                               LIVEWP_SIGNAL_INTERFACE,
410                               LIVEWP_SIGNAL_PATH,
411                               LIVEWP_RELOAD_CONFIG);
412                     }
413             }else
414                     if (check_applet_state(view))
415                         stop_applet(view);
416
417             set_button_image(button, priv->theme, check_applet_state(view));
418             break;
419         default:
420         case GTK_RESPONSE_OK:
421         break;
422         case GTK_RESPONSE_NO:
423             gtk_widget_destroy(window);
424             window = NULL;
425             lw_about();
426         break;
427     }
428     if (window)
429         gtk_widget_destroy(window);
430 }
431 /*******************************************************************************/
432 gboolean
433 check_applet_state(gint number){
434
435     HDConfigFile *config_file = NULL;
436     GKeyFile *gkey_file = NULL;
437     gchar *str = NULL;
438     gboolean result = FALSE;
439     if (number > 4 || number < 1)
440         return FALSE;
441
442     str = g_strdup_printf("livewp-home-widget.desktop-%i", (number - 1));
443     config_file = hd_config_file_new(NULL, "/home/user/.config/hildon-desktop/", "home.plugins");
444     
445     gkey_file = hd_config_file_load_file(config_file, FALSE);
446     if (gkey_file && str){
447         result = g_key_file_has_group(gkey_file, str);
448         g_free(str);
449     }
450     return result;
451 #if 0
452     FILE    *file_in = NULL;
453     gchar buffer[2048];
454
455     file_in = fopen("/home/user/.config/hildon-desktop/home.plugins","r");
456     if (file_in){
457
458         fprintf(stderr,"Check applet state\n");
459         while (!feof(file_in)) {
460                 memset(buffer, 0, sizeof(buffer));
461                 fgets(buffer, sizeof(buffer) - 1, file_in);
462                 if (!strcmp(buffer, 
463                             "X-Desktop-File=/usr/share/applications/hildon-home/livewp-home-widget.desktop\n")){
464                     result = TRUE;
465                     break;
466                 }
467        }
468         fclose(file_in);
469     }
470 #endif
471 }
472 /*******************************************************************************/
473 void
474 start_applet(gint number){
475
476     HDConfigFile *config_file = NULL;
477     GKeyFile *gkey_file = NULL;
478     gchar *str = NULL;
479
480     if (number > 4 || number < 1)
481         return;
482     str = g_strdup_printf("livewp-home-widget.desktop-%i",(number - 1));
483     config_file = hd_config_file_new(NULL, "/home/user/.config/hildon-desktop/", "home.plugins");
484     
485     gkey_file = hd_config_file_load_file(config_file, FALSE);
486     if (gkey_file){
487         g_key_file_set_string (gkey_file, str, "X-Desktop-File", "/usr/share/applications/hildon-home/livewp-home-widget.desktop");
488         hd_config_file_save_file( config_file, gkey_file);
489         g_key_file_free(gkey_file);
490     }else
491         fprintf(stderr, "Problem with config file");
492     if (str)
493         g_free(str);
494     g_object_unref(config_file);
495 }
496 /*******************************************************************************/
497 void
498 stop_applet(gint number){
499     HDConfigFile *config_file = NULL;
500     GKeyFile *gkey_file = NULL;
501     gchar *str = NULL;
502
503     if (number > 4 || number < 1)
504         return;
505     str = g_strdup_printf("livewp-home-widget.desktop-%i", (number - 1));
506     config_file = hd_config_file_new(NULL, "/home/user/.config/hildon-desktop/", "home.plugins");
507     
508     gkey_file = hd_config_file_load_file(config_file, FALSE);
509     if (gkey_file){
510          g_key_file_remove_group(gkey_file, str, NULL);  
511         hd_config_file_save_file( config_file, gkey_file);
512         g_key_file_free(gkey_file);
513     }else
514         fprintf(stderr, "Problem with config file");
515     if (str)
516         g_free(str);
517     g_object_unref(config_file);
518
519 #if 0    
520     FILE    *file_in;
521     FILE    *file_out;
522     gchar buffer[2048];
523     gchar * str = NULL;
524
525     file_in = fopen("/home/user/.config/hildon-desktop/home.plugins","r");
526     file_out = fopen("/tmp/livewallpaper.plugins","w");
527     if (file_in && file_out){
528         while (!feof(file_in)) {
529             memset(buffer, 0, sizeof(buffer));
530             fgets(buffer, sizeof(buffer) - 1, file_in);
531             str = g_strdup_printf("[livewp-home-widget.desktop-%i]\n", number);
532             if (strcmp(buffer, str) &&
533                 strcmp(buffer, "X-Desktop-File=/usr/share/applications/hildon-home/livewp-home-widget.desktop\n"))
534                 fputs(buffer, file_out);
535         }
536         if (str){
537             g_free(str);
538         }
539         fclose(file_out);
540         fclose(file_in);
541         file_in = fopen("/tmp/livewallpaper.plugins","r");
542         file_out = fopen("/home/user/.config/hildon-desktop/home.plugins","w");
543         if (file_in && file_out){
544             while (!feof(file_in)){
545                 memset(buffer, 0, sizeof(buffer));
546                 fgets(buffer, sizeof(buffer) - 1, file_in);
547                 fputs(buffer, file_out);
548             }
549             fclose(file_out);
550             fclose(file_in);
551             unlink ("/tmp/livewallpaper.plugins");
552         }
553     }
554 #endif
555 }