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