From 2c7a89d9b9346856534515ee9e3221a4c276edda Mon Sep 17 00:00:00 2001
From: Armin Luntzer <armin.luntzer@univie.ac.at>
Date: Thu, 4 Jul 2019 15:27:19 +0200
Subject: [PATCH] XYPlot: * add dialog feedback if file cannot be opened 
 * set default path to Documents directory

---
 src/client/widgets/xyplot/xyplot.c | 93 ++++++++++++++++++++++++++----
 1 file changed, 83 insertions(+), 10 deletions(-)

diff --git a/src/client/widgets/xyplot/xyplot.c b/src/client/widgets/xyplot/xyplot.c
index 0a2b9ed..5e7c1e1 100644
--- a/src/client/widgets/xyplot/xyplot.c
+++ b/src/client/widgets/xyplot/xyplot.c
@@ -272,10 +272,12 @@ static void xyplot_export_pdf(GtkWidget *w, XYPlot *p)
 
 	chooser = GTK_FILE_CHOOSER(dia);
 
+
 	gtk_file_chooser_set_do_overwrite_confirmation(chooser, TRUE);
 
 	gtk_file_chooser_set_current_name(chooser, "plot.pdf");
 
+	gtk_file_chooser_set_current_folder(chooser,g_get_user_special_dir(G_USER_DIRECTORY_DOCUMENTS));
 
 	res = gtk_dialog_run(GTK_DIALOG(dia));
 
@@ -285,6 +287,24 @@ static void xyplot_export_pdf(GtkWidget *w, XYPlot *p)
 
 		fname = gtk_file_chooser_get_filename(chooser);
 		cs = cairo_pdf_surface_create(fname, 1280, 720);
+
+
+		if (cairo_surface_status(cs) != CAIRO_STATUS_SUCCESS) {
+			GtkWidget *d;
+
+			d = gtk_message_dialog_new(GTK_WINDOW(win),
+						   GTK_DIALOG_MODAL,
+						   GTK_MESSAGE_ERROR,
+						   GTK_BUTTONS_CLOSE,
+						   "Could not open file %s",
+						   fname);
+
+			gtk_dialog_run(GTK_DIALOG(d));
+			gtk_widget_destroy(d);
+		}
+
+
+
 		cr = cairo_create(cs);
 
 		xyplot_plot_render(p, cr, 1280, 720);
@@ -323,7 +343,21 @@ static void xyplot_import_graph_xy_asc(const gchar *fname, XYPlot *p)
 	f = g_fopen(fname, "r");
 
 	if (!f) {
-		g_warning("%s: error opening file %s", __func__, fname);
+
+		GtkWidget *dia;
+		GtkWindow * win;
+
+		win = GTK_WINDOW(gtk_widget_get_toplevel(GTK_WIDGET(p)));
+		dia = gtk_message_dialog_new(win,
+					     GTK_DIALOG_MODAL,
+					     GTK_MESSAGE_ERROR,
+					     GTK_BUTTONS_CLOSE,
+					     "Could not open file %s",
+					     fname);
+
+		gtk_dialog_run(GTK_DIALOG(dia));
+		gtk_widget_destroy(dia);
+
 		return;
 	}
 
@@ -419,8 +453,8 @@ static void xyplot_import_graph_xy_asc(const gchar *fname, XYPlot *p)
 	g_array_free(gc, FALSE);
 }
 
-static void xyplot_export_graph_xy_asc(const gchar *fname, struct graph *g,
-				       const gchar *mode)
+static gboolean xyplot_export_graph_xy_asc(const gchar *fname, struct graph *g,
+					   const gchar *mode)
 {
 	FILE *f;
 
@@ -429,10 +463,9 @@ static void xyplot_export_graph_xy_asc(const gchar *fname, struct graph *g,
 
 	f = g_fopen(fname, mode);
 
-	if (!f) {
-		g_message("%s: error opening file %s", __func__, fname);
-		return;
-	}
+	if (!f)
+		return FALSE;
+
 
 	setlocale(LC_ALL, "C");
 
@@ -463,6 +496,8 @@ static void xyplot_export_graph_xy_asc(const gchar *fname, struct graph *g,
 	setlocale(LC_ALL, "");
 
 	fclose(f);
+
+	return TRUE;
 }
 
 
@@ -506,6 +541,7 @@ static void xyplot_export_xy_graph_cb(GtkWidget *w, struct graph *g)
 
 	gtk_file_chooser_set_current_name(chooser, fname);
 
+	gtk_file_chooser_set_current_folder(chooser,g_get_user_special_dir(G_USER_DIRECTORY_DOCUMENTS));
 
 	res = gtk_dialog_run(GTK_DIALOG(dia));
 
@@ -516,7 +552,20 @@ static void xyplot_export_xy_graph_cb(GtkWidget *w, struct graph *g)
 		fname = gtk_file_chooser_get_filename(chooser);
 
 
-		xyplot_export_graph_xy_asc(fname, g, "w");
+		if (!xyplot_export_graph_xy_asc(fname, g, "w")) {
+
+			GtkWidget *d;
+
+			d = gtk_message_dialog_new(GTK_WINDOW(win),
+						   GTK_DIALOG_MODAL,
+						   GTK_MESSAGE_ERROR,
+						   GTK_BUTTONS_CLOSE,
+						   "Could not open file %s",
+						   fname);
+
+			gtk_dialog_run(GTK_DIALOG(d));
+			gtk_widget_destroy(d);
+		}
 
 		g_free(fname);
 	}
@@ -535,6 +584,8 @@ static void xyplot_export_xy_plot_cb(GtkWidget *w, XYPlot *p)
 
 	GtkWidget *win;
 
+	FILE *f;
+
 	gchar *fname;
 
 	GList *elem;
@@ -567,6 +618,7 @@ static void xyplot_export_xy_plot_cb(GtkWidget *w, XYPlot *p)
 
 	gtk_file_chooser_set_current_name(chooser, fname);
 
+	gtk_file_chooser_set_current_folder(chooser,g_get_user_special_dir(G_USER_DIRECTORY_DOCUMENTS));
 
 	res = gtk_dialog_run(GTK_DIALOG(dia));
 
@@ -577,11 +629,30 @@ static void xyplot_export_xy_plot_cb(GtkWidget *w, XYPlot *p)
 		fname = gtk_file_chooser_get_filename(chooser);
 
 		/* clear file */
-		fclose(g_fopen(fname, "w"));
+		f = g_fopen(fname, "w");
+
+		if (!f) {
+			GtkWidget *d;
+
+			d = gtk_message_dialog_new(GTK_WINDOW(win),
+						   GTK_DIALOG_MODAL,
+						   GTK_MESSAGE_ERROR,
+						   GTK_BUTTONS_CLOSE,
+						   "Could not open file %s",
+						   fname);
+
+			gtk_dialog_run(GTK_DIALOG(d));
+			gtk_widget_destroy(d);
+		} else {
+			fclose(f);
+		}
 
 		for (elem = p->graphs; elem; elem = elem->next) {
 			g = (struct graph *) elem->data;
-			xyplot_export_graph_xy_asc(fname, g, "a");
+
+
+			if (!xyplot_export_graph_xy_asc(fname, g, "a"))
+				break;
 		}
 
 		g_free(fname);
@@ -621,6 +692,8 @@ static void xyplot_import_xy_graph_cb(GtkWidget *w, XYPlot *p)
 
 	chooser = GTK_FILE_CHOOSER(dia);
 
+	gtk_file_chooser_set_current_folder(chooser,g_get_user_special_dir(G_USER_DIRECTORY_DOCUMENTS));
+
 	res = gtk_dialog_run(GTK_DIALOG(dia));
 
 
-- 
GitLab