don't start GPS when there is no working connection
[azimuth] / src / controlpanel-applet.c
1 /*
2  * controlpanel-applet.c.c - Source for Azimuth's control panel applet
3  * Copyright (C) 2010 Collabora
4  * @author Alban Crequy <alban.crequy@collabora.co.uk>
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library 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 GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19  */
20
21 #include <libintl.h>
22 #include <libosso.h>
23 #include <gconf/gconf-client.h>
24 #include <hildon/hildon.h>
25 #include <hildon-cp-plugin/hildon-cp-plugin-interface.h>
26
27 #include "azimuth-gconf.h"
28
29 static GtkWidget* button_enabled = NULL;
30 static GtkWidget* button_blur = NULL;
31 static GtkWidget* button_start_gps = NULL;
32 static GConfClient *gconf_client;
33
34 static void
35 enabled_toggled (HildonCheckButton *button, gpointer user_data)
36 {
37   gboolean active;
38
39   active = hildon_check_button_get_active (button);
40   if (active)
41     {
42       gtk_widget_set_sensitive (button_blur, TRUE);
43       gtk_widget_set_sensitive (button_start_gps, TRUE);
44     }
45   else
46     {
47       gtk_widget_set_sensitive (button_blur, FALSE);
48       gtk_widget_set_sensitive (button_start_gps, FALSE);
49     }
50 }
51
52 static GtkWidget*
53 azimuth_create_check_button (const gchar *text,
54                              GCallback cb,
55                              const gchar *gconf_key)
56 {
57   GtkWidget *b;
58   gboolean checked;
59
60   b = hildon_check_button_new (HILDON_SIZE_FINGER_HEIGHT |
61       HILDON_SIZE_AUTO_WIDTH);
62   gtk_button_set_label (GTK_BUTTON (b), text);
63   if (cb != NULL)
64     g_signal_connect (b, "toggled", cb, NULL);
65   checked = gconf_client_get_bool (gconf_client, gconf_key,
66       NULL);
67   hildon_check_button_set_active (HILDON_CHECK_BUTTON (b), checked);
68
69   return b;
70 }
71
72 static GtkWidget*
73 create_main_dialog (gpointer window, osso_context_t *osso)
74 {
75   GtkWidget *dialog;
76   GtkWidget *bSave;
77
78   dialog = g_object_new (GTK_TYPE_DIALOG,
79       "transient-for", GTK_WINDOW (window),
80       "destroy-with-parent", TRUE,
81       "resizable", TRUE,
82       "has-separator", FALSE,
83       "modal", TRUE,
84       NULL);
85   gtk_window_set_title (GTK_WINDOW (dialog),
86       "Publish position to contacts");
87
88   bSave = gtk_dialog_add_button (GTK_DIALOG (dialog),
89       dgettext ("hildon-libs", "wdgt_bd_save"),
90       GTK_RESPONSE_OK);
91
92   button_blur = azimuth_create_check_button (
93       "Truncate your position", NULL, AZIMUTH_GCONF_KEY_BLUR);
94   button_start_gps = azimuth_create_check_button (
95       "Start GPS (drains battery)", NULL, AZIMUTH_GCONF_KEY_START_GPS);
96
97   button_enabled = azimuth_create_check_button (
98       "Publish position when GPS is running", G_CALLBACK (enabled_toggled),
99       AZIMUTH_GCONF_KEY_ENABLED);
100   enabled_toggled (HILDON_CHECK_BUTTON (button_enabled), NULL);
101
102   gtk_box_pack_start (GTK_BOX (GTK_DIALOG(dialog)->vbox), button_enabled,
103       FALSE, FALSE, 0);
104   gtk_box_pack_start (GTK_BOX (GTK_DIALOG(dialog)->vbox), button_blur,
105       FALSE, FALSE, 0);
106   gtk_box_pack_start (GTK_BOX (GTK_DIALOG(dialog)->vbox), button_start_gps,
107       FALSE, FALSE, 0);
108
109   gtk_widget_show_all (dialog);
110
111   return dialog;
112 }
113
114 static void
115 save (void)
116 {
117   gboolean enabled;
118   gboolean blur;
119   gboolean start_gps;
120
121   enabled = hildon_check_button_get_active (HILDON_CHECK_BUTTON (
122         button_enabled));
123   blur = hildon_check_button_get_active (HILDON_CHECK_BUTTON (
124         button_blur));
125   start_gps = hildon_check_button_get_active (HILDON_CHECK_BUTTON (
126         button_start_gps));
127
128   gconf_client_set_bool (gconf_client, AZIMUTH_GCONF_KEY_ENABLED, enabled,
129       NULL);
130   gconf_client_set_bool (gconf_client, AZIMUTH_GCONF_KEY_BLUR, blur,
131       NULL);
132   gconf_client_set_bool (gconf_client, AZIMUTH_GCONF_KEY_START_GPS, start_gps,
133       NULL);
134 }
135
136 osso_return_t
137 execute (osso_context_t *osso, gpointer data,
138     gboolean user_activated)
139 {
140   GtkWidget *dialog;
141   gint ret;
142
143   gconf_client = gconf_client_get_default ();
144
145   dialog = create_main_dialog (data, osso);
146
147   ret = gtk_dialog_run (GTK_DIALOG (dialog));
148
149   if (ret == GTK_RESPONSE_OK)
150     save ();
151
152   gtk_widget_destroy (dialog);
153
154   g_object_unref (gconf_client);
155   gconf_client = NULL;
156
157   return OSSO_OK;
158 }