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