Disable "save" button after file is saved because temp file is moved
[gps-tracker] / gps-tracker.c
index 57e8f3b..8b8736a 100644 (file)
@@ -21,7 +21,7 @@ typedef struct {
     GtkWidget *speed_val_label, *track_val_label, *climb_val_label;
     //GtkWidget *wp_label;
     GtkButton *wp_set_btn;
-    GString *wp_marker_str;
+    GString *wp_marker_str, *waypoint_block_str;
     gboolean tracking_is_on, has_fix;
     FILE *outf_p;
     guint points_recorded_in_current_segment;
@@ -54,8 +54,9 @@ static gchar * interface_file_chooser (AppData * appdata, GtkFileChooserAction a
     return filename;
 }
 
-static void write_gpx_header(FILE *fp)
+static void write_gpx_header(AppData *app_data)
 {
+  FILE *fp = app_data->outf_p;
   g_return_if_fail(fp);
   g_fprintf(fp,
       "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
@@ -71,10 +72,13 @@ static void write_gpx_header(FILE *fp)
       );
 }
 
-static void write_gpx_footer(FILE *fp)
+static void write_gpx_footer(AppData *app_data)
 {
+  FILE *fp = app_data->outf_p;
   g_return_if_fail(fp);
-  g_fprintf(fp, "</trkseg>\n</trk>\n</gpx>\n");
+  g_fprintf(fp, "</trkseg>\n</trk>\n");
+  g_fprintf(fp, app_data->waypoint_block_str->str);
+  g_fprintf(fp, "</gpx>\n");
 }
 
 static void cb_wp_set_btn (GtkWidget * w, AppData * data)
@@ -99,7 +103,7 @@ static void cb_start_stop (GtkWidget * w, AppData * data)
     data->outf_p = fopen(data->intermediate_gpx_data_filename, "w");
     data->points_recorded_in_current_segment = 0;
     data->last_device_status = LOCATION_GPS_DEVICE_STATUS_NO_FIX;
-    write_gpx_header(data->outf_p);
+    write_gpx_header(data);
     //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);
@@ -109,7 +113,7 @@ static void cb_start_stop (GtkWidget * w, AppData * data)
     data->points_recorded_in_current_segment = 0;
     data->last_device_status = LOCATION_GPS_DEVICE_STATUS_NO_FIX;
     if(data->outf_p) {
-      write_gpx_footer(data->outf_p);
+      write_gpx_footer(data);
       fclose(data->outf_p);
       data->outf_p = NULL;
     }
@@ -119,6 +123,7 @@ static void cb_start_stop (GtkWidget * w, AppData * data)
     gtk_widget_set_sensitive(GTK_WIDGET(data->wp_set_btn), FALSE);
   }
   g_string_truncate(data->wp_marker_str, 0);
+  g_string_truncate(data->waypoint_block_str, 0);
 }
 
 static void cb_file_save (GtkWidget * w, AppData * data)
@@ -131,6 +136,7 @@ static void cb_file_save (GtkWidget * w, AppData * data)
         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);
+        gtk_widget_set_sensitive(GTK_WIDGET(data->save_button), FALSE);
       }
       else {
         hildon_banner_show_information(GTK_WIDGET(data->window), NULL, "Temp file not found");
@@ -193,6 +199,8 @@ static void on_gps_device_changed (LocationGPSDevice *device, gpointer data)
         if(app_data->wp_marker_str->len) {
           g_fprintf(fp, "<name>%s</name>\n", app_data->wp_marker_str->str);
           hildon_banner_show_information(GTK_WIDGET(app_data->window), NULL, app_data->wp_marker_str->str);
+          g_string_append_printf(app_data->waypoint_block_str, "<wpt lat=\"%s\" lon=\"%s\"><name>%s</name></wpt>\n",
+              sbuf1, sbuf2, app_data->wp_marker_str->str);
           g_string_truncate(app_data->wp_marker_str, 0);
         }
       }
@@ -271,6 +279,10 @@ static void on_gps_device_changed (LocationGPSDevice *device, gpointer data)
       }
     }
   }
+  else {
+    app_data->has_fix = FALSE;
+    app_data->last_device_status = LOCATION_GPS_DEVICE_STATUS_NO_FIX;
+  }
        
        //g_print ("Satellites in view: %d\n", device->satellites_in_view);
        //g_print ("Satellites in use: %d\n", device->satellites_in_use);
@@ -342,6 +354,7 @@ int main (int argc, char **argv)
        hildon_program_add_window (data->program, HILDON_WINDOW (data->window));
 
   data->wp_marker_str = g_string_sized_new(64);
+  data->waypoint_block_str = g_string_sized_new(32<<10);
   data->main_vbox = (gpointer)gtk_vbox_new(FALSE, 0);
   data->loc_hbox = (gpointer)gtk_hbox_new(FALSE, 0);
   data->loc_gps_data_table = (gpointer)gtk_table_new(4, 2, FALSE);
@@ -482,6 +495,7 @@ int main (int argc, char **argv)
   g_free(data->working_dir);
   g_free(data->intermediate_gpx_data_filename);
   g_string_free(data->wp_marker_str, TRUE);
+  g_string_free(data->waypoint_block_str, TRUE);
   g_free(data);
        g_object_unref (device);
        g_object_unref (control);