Start/Stop functionality added
[gps-tracker] / gps-tracker.c
1 #include <glib.h>
2
3 #include <hildon/hildon.h>
4 #include <hildon/hildon-file-chooser-dialog.h>
5 #include <location/location-gpsd-control.h>
6 #include <location/location-gps-device.h>
7 #include <location/location-misc.h>
8 #include <location/location-distance-utils.h>
9
10 typedef struct {
11     HildonProgram *program;
12     HildonWindow *window;
13     
14     GtkWidget * main_vbox, *btn_hbox;
15     GtkWidget *status_label;
16     GtkButton *start_stop_button, *save_button;
17     gboolean tracking_is_on;
18 } AppData;
19
20 static gchar * interface_file_chooser (AppData * appdata, GtkFileChooserAction action)
21 {
22     GtkWidget *dialog;
23     gchar *filename = NULL;
24     
25     dialog = hildon_file_chooser_dialog_new (GTK_WINDOW (appdata->window), action);
26     gtk_widget_show_all (GTK_WIDGET (dialog));
27
28     if (gtk_dialog_run (GTK_DIALOG (dialog)) == GTK_RESPONSE_OK) {
29         filename = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (dialog));
30     }
31
32     gtk_widget_destroy(dialog);
33     return filename;
34 }
35
36 static void cb_start_stop (GtkWidget * w, AppData * data)
37 {
38   data->tracking_is_on = !data->tracking_is_on;
39   if(data->tracking_is_on) {
40     hildon_banner_show_information(GTK_WIDGET(data->window), NULL, "Tracking started");
41   }
42   else {
43     hildon_banner_show_information(GTK_WIDGET(data->window), NULL, "Tracking stopped");
44   }
45 }
46
47 static void cb_file_save (GtkWidget * w, AppData * data)
48 {
49     gchar *filename = NULL;
50     filename = interface_file_chooser (data, GTK_FILE_CHOOSER_ACTION_SAVE);
51
52     if (filename == NULL) {
53         filename = "NULL";
54     }
55     else {
56         FILE * f = fopen (filename, "w");
57         fprintf (f, "This file was generated by Hildon File Chooser example.");
58         fclose (f);
59     }
60
61     g_print ("File saved as %s\n", filename);
62 }
63
64 static void
65 on_gps_device_changed (LocationGPSDevice *device, gpointer data)
66 {
67   AppData *app_data = data;
68   GtkLabel *info = (GtkLabel*)app_data->status_label;
69   GString *msg;
70         if (!device)
71                 return;
72
73   msg = g_string_sized_new (512);
74         if (device->fix) {
75                 if (device->fix->fields & LOCATION_GPS_DEVICE_TIME_SET) {
76                         g_print ("time = %f\n", device->fix->time);
77       g_string_append_printf(msg, "time = %f\n", device->fix->time);
78     }
79
80                 if (device->fix->fields & LOCATION_GPS_DEVICE_LATLONG_SET) {
81                         g_print ("lat = %f, long = %f\n",
82                                         device->fix->latitude,
83                                         device->fix->longitude);
84                         g_string_append_printf (msg, "lat = %f, long = %f\n",
85                                         device->fix->latitude,
86                                         device->fix->longitude);
87     }
88
89                 if (device->fix->fields & LOCATION_GPS_DEVICE_ALTITUDE_SET) {
90                         g_print ("alt = %f\n", device->fix->altitude);
91                         g_string_append_printf (msg, "alt = %f\n", device->fix->altitude);
92     }
93
94                 if (device->fix->fields & LOCATION_GPS_DEVICE_SPEED_SET) {
95                         g_print ("speed = %f\n", device->fix->speed);
96                         g_string_append_printf (msg, "speed = %f, ", device->fix->speed);
97     }
98
99                 if (device->fix->fields & LOCATION_GPS_DEVICE_TRACK_SET) {
100                         g_print ("track = %f\n", device->fix->track);
101                         g_string_append_printf (msg, "track = %f, ", device->fix->track);
102     }
103
104                 if (device->fix->fields & LOCATION_GPS_DEVICE_CLIMB_SET) {
105                         g_print ("climb = %f\n", device->fix->climb);
106                         g_string_append_printf (msg, "climb = %f\n", device->fix->climb);
107     }
108
109                 g_print ("Accuracy values:\n");
110                 g_print ("\tept = %e, eph = %e, epv = %e, epd = %e, "
111                                 "eps = %e, epc = %e\n",
112                                 device->fix->ept,
113                                 device->fix->eph,
114                                 device->fix->epv,
115                                 device->fix->epd,
116                                 device->fix->eps,
117                                 device->fix->epc);
118         }
119         
120         g_print ("Satellites in view: %d\n", device->satellites_in_view);
121         g_print ("Satellites in use: %d\n", device->satellites_in_use);
122   g_string_append_printf (msg, "Satellites = % 2d/% 2d\n", device->satellites_in_use, device->satellites_in_view);
123         g_print ("GPS status: %d\n", device->status);
124         g_string_append_printf (msg, "GPS status: %d\n", device->status);
125
126   hildon_gtk_window_set_progress_indicator(GTK_WINDOW(app_data->window), device->status == LOCATION_GPS_DEVICE_STATUS_NO_FIX);
127
128
129         if (device->cell_info) {
130                 if (device->cell_info->flags & LOCATION_CELL_INFO_GSM_CELL_INFO_SET)
131                         g_print ("Mobile Coutry Code GSM: %d\n", device->cell_info->gsm_cell_info.mcc);
132
133                 if (device->cell_info->flags & LOCATION_CELL_INFO_WCDMA_CELL_INFO_SET)
134                         g_print ("Mobile Coutry Code WCDMA: %d\n", device->cell_info->wcdma_cell_info.mcc);
135         }
136
137   gtk_label_set_text(info, msg->str);
138   //gtk_widget_show(GTK_WIDGET(info));
139   g_string_free(msg, TRUE);
140 }
141
142 static void
143 on_gps_error (LocationGPSDevice *device, gpointer data)
144 {
145         g_error ("GPS error");
146 }
147
148 static void
149 on_gps_stop (LocationGPSDevice *device, gpointer data)
150 {
151         g_warning ("GPS stopped");
152 }
153
154 static void
155 on_gps_start (LocationGPSDevice *device, gpointer data)
156 {
157         g_warning ("GPS started");
158 }
159
160 int main (int argc, char **argv)
161 {
162         GtkWidget *picker_button = NULL;
163   AppData * data = g_new0 (AppData, 1);
164
165         hildon_gtk_init (&argc, &argv);
166         LocationGPSDControl *control;
167         LocationGPSDevice *device;
168
169         data->program = hildon_program_get_instance ();
170         g_set_application_name("GPS tracker");
171
172         data->window = HILDON_WINDOW(hildon_stackable_window_new ());
173         hildon_program_add_window (data->program, HILDON_WINDOW (data->window));
174
175   data->main_vbox = (void*)gtk_vbox_new(FALSE, 0);
176   data->btn_hbox = (void*)gtk_hbox_new(TRUE, 0);
177
178         /* Create a picker button */
179         picker_button = hildon_date_button_new (HILDON_SIZE_AUTO,
180                         HILDON_BUTTON_ARRANGEMENT_VERTICAL);
181
182         /* Set a title to the button*/
183         hildon_button_set_title (HILDON_BUTTON (picker_button), "Pick a date");
184
185   data->start_stop_button = GTK_BUTTON (hildon_gtk_button_new (HILDON_SIZE_THUMB_HEIGHT | HILDON_SIZE_HALFSCREEN_WIDTH));
186   data->save_button = GTK_BUTTON (hildon_gtk_button_new (HILDON_SIZE_THUMB_HEIGHT | HILDON_SIZE_HALFSCREEN_WIDTH));
187   gtk_button_set_label (data->start_stop_button, "Start");
188   gtk_button_set_label (data->save_button, "Save");
189   gtk_box_pack_start(GTK_BOX(data->btn_hbox), GTK_WIDGET(data->start_stop_button), FALSE, FALSE, 8);
190   gtk_box_pack_start(GTK_BOX(data->btn_hbox), GTK_WIDGET(data->save_button), FALSE, FALSE, 8);
191   gtk_box_pack_start(GTK_BOX(data->main_vbox), GTK_WIDGET(data->btn_hbox), FALSE, FALSE, 0);
192   gtk_box_pack_start(GTK_BOX(data->main_vbox), GTK_WIDGET(picker_button), FALSE, FALSE, 0);
193   data->status_label = gtk_label_new("Hier kommt der Status hin\nUnd hier ist die 2. Zeile");
194   gtk_box_pack_start_defaults(GTK_BOX(data->main_vbox), data->status_label);
195         /* Add vbox to main window */
196         gtk_container_add (GTK_CONTAINER (data->window), GTK_WIDGET(data->main_vbox));
197
198   g_signal_connect (G_OBJECT (data->save_button), "clicked", G_CALLBACK (cb_file_save), data);
199   g_signal_connect (G_OBJECT (data->start_stop_button), "clicked", G_CALLBACK (cb_start_stop), data);
200         g_signal_connect (G_OBJECT (data->window), "destroy",
201                         G_CALLBACK (gtk_main_quit), NULL);
202
203         control = location_gpsd_control_get_default ();
204   //hildon_banner_show_information(GTK_WIDGET(data->window), NULL, "Hi there!");
205         location_gpsd_control_start (control);
206
207         /*
208          * Note that in real life one may want to use some other method and interval
209          * than LOCATION_METHOD_USER_SELECTED and LOCATION_INTERVAL_DEFAULT,
210          * respectively. For more information on possible values for these parameters
211          * please see liblocation online documentation.
212          */
213         g_object_set (G_OBJECT (control), 
214                         "preferred-method", LOCATION_METHOD_USER_SELECTED,
215                         "preferred-interval", LOCATION_INTERVAL_DEFAULT,
216                         NULL);
217
218         device  = g_object_new (LOCATION_TYPE_GPS_DEVICE, NULL);
219
220         g_signal_connect (control, "error",             G_CALLBACK (on_gps_error),              NULL);
221         g_signal_connect (control, "gpsd-running",      G_CALLBACK (on_gps_start),              NULL);
222         g_signal_connect (control, "gpsd-stopped",      G_CALLBACK (on_gps_stop),               NULL);
223         g_signal_connect (device,  "changed",           G_CALLBACK (on_gps_device_changed),     data);
224
225         gtk_widget_show_all (GTK_WIDGET (data->window));
226
227   hildon_gtk_window_set_progress_indicator(GTK_WINDOW(data->window), 1);
228         gtk_main ();
229
230         location_gpsd_control_stop (control);
231
232         g_object_unref (device);
233         g_object_unref (control);
234   g_free(data);
235
236         return 0;
237 }