X-Osso-... tags are wrong
[gps-tracker] / gps-tracker.c
index 9b4746f..881164d 100644 (file)
@@ -1,4 +1,5 @@
 #include <glib.h>
+#include <glib/gstdio.h>
 
 #include <hildon/hildon.h>
 #include <hildon/hildon-file-chooser-dialog.h>
@@ -13,16 +14,28 @@ typedef struct {
     
     GtkWidget * main_vbox, *btn_hbox;
     GtkWidget *status_label;
-    GtkButton *start_button;
+    GtkButton *start_stop_button, *save_button;
+    gboolean tracking_is_on;
+    FILE *outf_p;
+    gchar *working_dir;
+    gchar *intermediate_gpx_data_filename;
 } AppData;
 
 static gchar * interface_file_chooser (AppData * appdata, GtkFileChooserAction action)
 {
     GtkWidget *dialog;
     gchar *filename = NULL;
+    gchar tmpname[PATH_MAX];
+    time_t t;
+    struct tm *tmp;
     
+    t = time(NULL);
+    tmp = localtime(&t);
+    strftime(tmpname, sizeof(tmpname), "gps-tracker-%F-%H-%M.gpx", tmp);
     dialog = hildon_file_chooser_dialog_new (GTK_WINDOW (appdata->window), action);
     gtk_widget_show_all (GTK_WIDGET (dialog));
+    gtk_file_chooser_set_current_name(GTK_FILE_CHOOSER (dialog), tmpname);
+    gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(dialog), appdata->working_dir);
 
     if (gtk_dialog_run (GTK_DIALOG (dialog)) == GTK_RESPONSE_OK) {
         filename = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (dialog));
@@ -32,97 +45,164 @@ static gchar * interface_file_chooser (AppData * appdata, GtkFileChooserAction a
     return filename;
 }
 
+static void write_gpx_header(FILE *fp)
+{
+  g_return_if_fail(fp);
+  g_fprintf(fp,
+      "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
+      "<gpx version=\"1.0\"\n"
+      "creator=\"GPS tracker for Maemo\"\n"
+      "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n"
+      "xmlns=\"http://www.topografix.com/GPX/1/0\"\n"
+      "xsi:schemaLocation=\"http://www.topografix.com/GPX/1/0 http://www.topografix.com/GPX/1/0/gpx.xsd\">\n"
+      "<trk>\n<trkseg>\n"
+      );
+}
+
+static void write_gpx_footer(FILE *fp)
+{
+  g_return_if_fail(fp);
+  g_fprintf(fp, "</trkseg>\n</trk>\n</gpx>\n");
+}
+
+static void cb_start_stop (GtkWidget * w, AppData * data)
+{
+  data->tracking_is_on = !data->tracking_is_on;
+  if(data->tracking_is_on) { /* START pressed */
+    data->outf_p = fopen(data->intermediate_gpx_data_filename, "w");
+    write_gpx_header(data->outf_p);
+    hildon_banner_show_information(GTK_WIDGET(data->window), NULL, "Tracking started");
+    gtk_button_set_label (data->start_stop_button, "Stop");
+    gtk_widget_set_sensitive(GTK_WIDGET(data->save_button), FALSE);
+  }
+  else { /* STOP pressed */
+    if(data->outf_p) {
+      write_gpx_footer(data->outf_p);
+      fclose(data->outf_p);
+      data->outf_p = NULL;
+    }
+    gtk_button_set_label (data->start_stop_button, "Start");
+    hildon_banner_show_information(GTK_WIDGET(data->window), NULL, "Tracking stopped");
+    gtk_widget_set_sensitive(GTK_WIDGET(data->save_button), TRUE);
+  }
+}
+
 static void cb_file_save (GtkWidget * w, AppData * data)
 {
     gchar *filename = NULL;
     filename = interface_file_chooser (data, GTK_FILE_CHOOSER_ACTION_SAVE);
 
-    if (filename == NULL) {
-        filename = "NULL";
+    if (filename) {
+      if(g_file_test(data->intermediate_gpx_data_filename, G_FILE_TEST_IS_REGULAR | G_FILE_TEST_EXISTS)) {
+        g_rename(data->intermediate_gpx_data_filename, filename);
+        //g_print ("File saved as %s\n", filename);
+        hildon_banner_show_information(GTK_WIDGET(data->window), NULL, filename);
+      }
+      else {
+        hildon_banner_show_information(GTK_WIDGET(data->window), NULL, "Temp file not found");
+      }
     }
-    else {
-        FILE * f = fopen (filename, "w");
-        fprintf (f, "This file was generated by Hildon File Chooser example.");
-        fclose (f);
-    }
-
-    g_print ("File saved as %s\n", filename);
+    g_free(filename);
 }
 
 static void
 on_gps_device_changed (LocationGPSDevice *device, gpointer data)
 {
   AppData *app_data = data;
-  GtkLabel *info = (GtkLabel*)data;
+  GtkLabel *info = (GtkLabel*)app_data->status_label;
   GString *msg;
+  FILE *fp = app_data->outf_p;
+  gchar sbuf1[G_ASCII_DTOSTR_BUF_SIZE], sbuf2[G_ASCII_DTOSTR_BUF_SIZE];
        if (!device)
                return;
 
   msg = g_string_sized_new (512);
-       if (device->fix) {
-               if (device->fix->fields & LOCATION_GPS_DEVICE_TIME_SET) {
-                       g_print ("time = %f\n", device->fix->time);
-      g_string_append_printf(msg, "time = %f\n", device->fix->time);
+  if (device->fix && device->status) {
+    if (device->fix->fields & LOCATION_GPS_DEVICE_LATLONG_SET) {
+      //g_print ("lat = %f, long = %f\n", device->fix->latitude, device->fix->longitude);
+      g_string_append_printf (msg, "lat = %f, long = %f\n",
+          device->fix->latitude,
+          device->fix->longitude);
+      if(fp)
+        g_fprintf(fp, "\n<trkpt lat=\"%s\" lon=\"%s\">\n",
+          g_ascii_formatd(sbuf1, sizeof(sbuf1), "%f", device->fix->latitude),
+          g_ascii_formatd(sbuf2, sizeof(sbuf2), "%f", device->fix->longitude));
+
+      if (device->fix->fields & LOCATION_GPS_DEVICE_TIME_SET) {
+        //g_print ("time = %f\n", device->fix->time);
+        g_string_append_printf(msg, "time = %F\n", device->fix->time);
+        if(fp) {
+          gchar st[64];
+          struct tm *tmp;
+          time_t t = device->fix->time;
+          tmp = localtime(&t);
+          strftime(st, sizeof(st), "%FT%T%Z", tmp);
+          /* FIXME Is this really correct */
+          g_fprintf(fp, "<time>%s</time>\n", st);
+        }
+      }
+
+      if (device->fix->fields & LOCATION_GPS_DEVICE_ALTITUDE_SET) {
+        //g_print ("alt = %f\n", device->fix->altitude);
+        g_string_append_printf (msg, "alt = %f\n", device->fix->altitude);
+        if(fp)
+          g_fprintf(fp, "<ele>%s</ele>\n", g_ascii_formatd(sbuf1, sizeof(sbuf1), "%f", device->fix->altitude));
+
+      }
+
+      if (device->fix->fields & LOCATION_GPS_DEVICE_SPEED_SET) {
+        //g_print ("speed = %f\n", device->fix->speed);
+        g_string_append_printf (msg, "speed = %f, ", device->fix->speed);
+        if(fp)
+          g_fprintf(fp, "<speed>%s</speed>\n", g_ascii_formatd(sbuf1, sizeof(sbuf1), "%f", device->fix->speed));
+      }
+
+      if (device->fix->fields & LOCATION_GPS_DEVICE_TRACK_SET) {
+        //g_print ("track = %f\n", device->fix->track);
+        g_string_append_printf (msg, "track = %f, ", device->fix->track);
+        if(fp)
+          g_fprintf(fp, "<course>%s</course>\n", g_ascii_formatd(sbuf1, sizeof(sbuf1), "%f", device->fix->track));
+      }
+
+      if (device->fix->fields & LOCATION_GPS_DEVICE_CLIMB_SET) {
+        //g_print ("climb = %f\n", device->fix->climb);
+        g_string_append_printf (msg, "climb = %f\n", device->fix->climb);
+      }
+
+      //g_print ("Accuracy values:\n");
+      //g_print ("\tept = %e, eph = %e, epv = %e, epd = %e, "
+      //    "eps = %e, epc = %e\n",
+      //    device->fix->ept,
+      //    device->fix->eph,
+      //    device->fix->epv,
+      //    device->fix->epd,
+      //    device->fix->eps,
+      //    device->fix->epc);
+      if(fp)
+        g_fprintf(fp, "</trkpt>\n");
     }
-
-               if (device->fix->fields & LOCATION_GPS_DEVICE_LATLONG_SET) {
-                       g_print ("lat = %f, long = %f\n",
-                                       device->fix->latitude,
-                                       device->fix->longitude);
-                       g_string_append_printf (msg, "lat = %f, long = %f\n",
-                                       device->fix->latitude,
-                                       device->fix->longitude);
-    }
-
-               if (device->fix->fields & LOCATION_GPS_DEVICE_ALTITUDE_SET) {
-                       g_print ("alt = %f\n", device->fix->altitude);
-                       g_string_append_printf (msg, "alt = %f\n", device->fix->altitude);
-    }
-
-               if (device->fix->fields & LOCATION_GPS_DEVICE_SPEED_SET) {
-                       g_print ("speed = %f\n", device->fix->speed);
-                       g_string_append_printf (msg, "speed = %f, ", device->fix->speed);
-    }
-
-               if (device->fix->fields & LOCATION_GPS_DEVICE_TRACK_SET) {
-                       g_print ("track = %f\n", device->fix->track);
-                       g_string_append_printf (msg, "track = %f, ", device->fix->track);
-    }
-
-               if (device->fix->fields & LOCATION_GPS_DEVICE_CLIMB_SET) {
-                       g_print ("climb = %f\n", device->fix->climb);
-                       g_string_append_printf (msg, "climb = %f\n", device->fix->climb);
-    }
-
-               g_print ("Accuracy values:\n");
-               g_print ("\tept = %e, eph = %e, epv = %e, epd = %e, "
-                               "eps = %e, epc = %e\n",
-                               device->fix->ept,
-                               device->fix->eph,
-                               device->fix->epv,
-                               device->fix->epd,
-                               device->fix->eps,
-                               device->fix->epc);
-       }
+  }
        
-       g_print ("Satellites in view: %d\n", device->satellites_in_view);
-       g_print ("Satellites in use: %d\n", device->satellites_in_use);
+       //g_print ("Satellites in view: %d\n", device->satellites_in_view);
+       //g_print ("Satellites in use: %d\n", device->satellites_in_use);
   g_string_append_printf (msg, "Satellites = % 2d/% 2d\n", device->satellites_in_use, device->satellites_in_view);
-       g_print ("GPS status: %d\n", device->status);
+       //g_print ("GPS status: %d\n", device->status);
        g_string_append_printf (msg, "GPS status: %d\n", device->status);
 
   hildon_gtk_window_set_progress_indicator(GTK_WINDOW(app_data->window), device->status == LOCATION_GPS_DEVICE_STATUS_NO_FIX);
 
 
-       if (device->cell_info) {
-               if (device->cell_info->flags & LOCATION_CELL_INFO_GSM_CELL_INFO_SET)
-                       g_print ("Mobile Coutry Code GSM: %d\n", device->cell_info->gsm_cell_info.mcc);
+       //if (device->cell_info) {
+       //      if (device->cell_info->flags & LOCATION_CELL_INFO_GSM_CELL_INFO_SET)
+       //              g_print ("Mobile Coutry Code GSM: %d\n", device->cell_info->gsm_cell_info.mcc);
 
-               if (device->cell_info->flags & LOCATION_CELL_INFO_WCDMA_CELL_INFO_SET)
-                       g_print ("Mobile Coutry Code WCDMA: %d\n", device->cell_info->wcdma_cell_info.mcc);
-       }
+       //      if (device->cell_info->flags & LOCATION_CELL_INFO_WCDMA_CELL_INFO_SET)
+       //              g_print ("Mobile Coutry Code WCDMA: %d\n", device->cell_info->wcdma_cell_info.mcc);
+       //}
 
   gtk_label_set_text(info, msg->str);
+  if(device->status != LOCATION_GPS_DEVICE_STATUS_NO_FIX)
+    gtk_widget_set_sensitive(GTK_WIDGET(app_data->start_stop_button), TRUE);
   //gtk_widget_show(GTK_WIDGET(info));
   g_string_free(msg, TRUE);
 }
@@ -130,30 +210,38 @@ on_gps_device_changed (LocationGPSDevice *device, gpointer data)
 static void
 on_gps_error (LocationGPSDevice *device, gpointer data)
 {
+  //AppData *app_data = data;
        g_error ("GPS error");
 }
 
 static void
 on_gps_stop (LocationGPSDevice *device, gpointer data)
 {
-       g_warning ("GPS stopped");
+  AppData *app_data = data;
+  const gchar *msg = "GPS stopped";
+  hildon_banner_show_information(GTK_WIDGET(app_data->window), NULL, msg);
+       g_warning (msg);
 }
 
 static void
 on_gps_start (LocationGPSDevice *device, gpointer data)
 {
-       g_warning ("GPS started");
+  AppData *app_data = data;
+  const gchar *msg = "GPS started";
+  hildon_banner_show_information(GTK_WIDGET(app_data->window), NULL, msg);
+       g_warning (msg);
 }
 
 int main (int argc, char **argv)
 {
-       GtkWidget *picker_button = NULL;
   AppData * data = g_new0 (AppData, 1);
 
        hildon_gtk_init (&argc, &argv);
        LocationGPSDControl *control;
        LocationGPSDevice *device;
 
+  data->working_dir = g_strdup_printf("%s" G_DIR_SEPARATOR_S "MyDocs", g_get_home_dir());
+  data->intermediate_gpx_data_filename = g_strdup_printf("%s" G_DIR_SEPARATOR_S ".gps-tracker.gpx", data->working_dir);
        data->program = hildon_program_get_instance ();
        g_set_application_name("GPS tracker");
 
@@ -163,29 +251,27 @@ int main (int argc, char **argv)
   data->main_vbox = (void*)gtk_vbox_new(FALSE, 0);
   data->btn_hbox = (void*)gtk_hbox_new(TRUE, 0);
 
-       /* Create a picker button */
-       picker_button = hildon_date_button_new (HILDON_SIZE_AUTO,
-                       HILDON_BUTTON_ARRANGEMENT_VERTICAL);
-
-       /* Set a title to the button*/
-       hildon_button_set_title (HILDON_BUTTON (picker_button), "Pick a date");
-
-  data->start_button = GTK_BUTTON (hildon_gtk_button_new (HILDON_SIZE_THUMB_HEIGHT | HILDON_SIZE_HALFSCREEN_WIDTH));
-  gtk_button_set_label (data->start_button, "Start");
-  gtk_box_pack_start(GTK_BOX(data->btn_hbox), GTK_WIDGET(data->start_button), FALSE, FALSE, 8);
+  data->start_stop_button = GTK_BUTTON (hildon_gtk_button_new (HILDON_SIZE_THUMB_HEIGHT | HILDON_SIZE_HALFSCREEN_WIDTH));
+  gtk_button_set_label (data->start_stop_button, "Start");
+  gtk_widget_set_sensitive(GTK_WIDGET(data->start_stop_button), FALSE);
+  data->save_button = GTK_BUTTON (hildon_gtk_button_new (HILDON_SIZE_THUMB_HEIGHT | HILDON_SIZE_HALFSCREEN_WIDTH));
+  gtk_button_set_label (data->save_button, "Save");
+  gtk_widget_set_sensitive(GTK_WIDGET(data->save_button), FALSE);
+  gtk_box_pack_start(GTK_BOX(data->btn_hbox), GTK_WIDGET(data->start_stop_button), FALSE, FALSE, 8);
+  gtk_box_pack_start(GTK_BOX(data->btn_hbox), GTK_WIDGET(data->save_button), FALSE, FALSE, 8);
   gtk_box_pack_start(GTK_BOX(data->main_vbox), GTK_WIDGET(data->btn_hbox), FALSE, FALSE, 0);
-  gtk_box_pack_start(GTK_BOX(data->main_vbox), GTK_WIDGET(picker_button), FALSE, FALSE, 0);
-  data->status_label = gtk_label_new("Hier kommt der Status hin\nUnd hier ist die 2. Zeile");
+  data->status_label = gtk_label_new("Waiting for GPS ...");
   gtk_box_pack_start_defaults(GTK_BOX(data->main_vbox), data->status_label);
        /* Add vbox to main window */
        gtk_container_add (GTK_CONTAINER (data->window), GTK_WIDGET(data->main_vbox));
 
+  g_signal_connect (G_OBJECT (data->save_button), "clicked", G_CALLBACK (cb_file_save), data);
+  g_signal_connect (G_OBJECT (data->start_stop_button), "clicked", G_CALLBACK (cb_start_stop), data);
        g_signal_connect (G_OBJECT (data->window), "destroy",
                        G_CALLBACK (gtk_main_quit), NULL);
 
        control = location_gpsd_control_get_default ();
-  hildon_banner_show_information(GTK_WIDGET(data->window), NULL, "Hi there!");
-       location_gpsd_control_start (control);
+  location_gpsd_control_start (control);
 
        /*
         * Note that in real life one may want to use some other method and interval
@@ -200,9 +286,9 @@ int main (int argc, char **argv)
 
        device  = g_object_new (LOCATION_TYPE_GPS_DEVICE, NULL);
 
-       g_signal_connect (control, "error",             G_CALLBACK (on_gps_error),              NULL);
-       g_signal_connect (control, "gpsd-running",      G_CALLBACK (on_gps_start),              NULL);
-       g_signal_connect (control, "gpsd-stopped",      G_CALLBACK (on_gps_stop),               NULL);
+       g_signal_connect (control, "error",             G_CALLBACK (on_gps_error),              data);
+       g_signal_connect (control, "gpsd-running",      G_CALLBACK (on_gps_start),              data);
+       g_signal_connect (control, "gpsd-stopped",      G_CALLBACK (on_gps_stop),               data);
        g_signal_connect (device,  "changed",           G_CALLBACK (on_gps_device_changed),     data);
 
        gtk_widget_show_all (GTK_WIDGET (data->window));
@@ -210,11 +296,16 @@ int main (int argc, char **argv)
   hildon_gtk_window_set_progress_indicator(GTK_WINDOW(data->window), 1);
        gtk_main ();
 
-       location_gpsd_control_stop (control);
+  location_gpsd_control_stop (control);
 
+  if(data->outf_p)
+    fclose(data->outf_p);
+  g_unlink(data->intermediate_gpx_data_filename);
+  g_free(data->working_dir);
+  g_free(data->intermediate_gpx_data_filename);
+  g_free(data);
        g_object_unref (device);
        g_object_unref (control);
-  g_free(data);
 
        return 0;
 }