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