[PATCH] Implement a 'Save As' entry in the context menu shown when right click on a dive

Pierre-Yves Chibon pingou at pingoured.fr
Thu Jan 31 23:22:31 PST 2013


Something which is nice especially when asked on the list to share an
interesting dive is the possibility to save just some dives into a file.

This commit adds to the context menu showns with right-click the 'Save As'
entry. This entry allows to save selected dives.

Signed-off-by: Pierre-Yves Chibon <pingou at pingoured.fr>
---

I think this approach is nicer than the previous one, only the name of the
function 'save_dives_logic' is not really nice, suggestions welcome.


PS: It seems I managed to mistype the email address twice, so you actually
did not receive the first attempt (which is fine by me :)).

 dive.h     |  1 +
 divelist.c | 48 ++++++++++++++++++++++++++++++++++++++++++++++++
 save-xml.c | 36 +++++++++++++++++++++++++-----------
 3 files changed, 74 insertions(+), 11 deletions(-)

diff --git a/dive.h b/dive.h
index e719f22..90d5cd7 100644
--- a/dive.h
+++ b/dive.h
@@ -515,6 +515,7 @@ extern void show_yearly_stats(void);
 
 extern void update_dive(struct dive *new_dive);
 extern void save_dives(const char *filename);
+extern void save_dives_logic(const char *filename, gboolean select_only);
 
 extern timestamp_t utc_mktime(struct tm *tm);
 extern void utc_mkdate(timestamp_t, struct tm *tm);
diff --git a/divelist.c b/divelist.c
index 26ccd3d..3697557 100644
--- a/divelist.c
+++ b/divelist.c
@@ -1621,6 +1621,47 @@ gboolean icon_click_cb(GtkWidget *w, GdkEventButton *event, gpointer data)
 	return FALSE;
 }
 
+static void save_as_cb(GtkWidget *menuitem, struct dive *dive)
+{
+	GtkWidget *dialog;
+	char *filename = NULL;
+	char *current_file;
+	char *current_dir;
+
+	dialog = gtk_file_chooser_dialog_new(_("Save File As"),
+		GTK_WINDOW(main_window),
+		GTK_FILE_CHOOSER_ACTION_SAVE,
+		GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
+		GTK_STOCK_SAVE, GTK_RESPONSE_ACCEPT,
+		NULL);
+	gtk_file_chooser_set_do_overwrite_confirmation(GTK_FILE_CHOOSER(dialog), TRUE);
+
+	if (existing_filename) {
+		current_dir = g_path_get_dirname(existing_filename);
+		current_file = g_path_get_basename(existing_filename);
+	} else {
+		const char *current_default = prefs.default_filename;
+		current_dir = g_path_get_dirname(current_default);
+		current_file = g_path_get_basename(current_default);
+	}
+	gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(dialog), current_dir);
+	gtk_file_chooser_set_current_name(GTK_FILE_CHOOSER(dialog), current_file);
+
+	free(current_dir);
+	free(current_file);
+
+	if (gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_ACCEPT) {
+		filename = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dialog));
+	}
+	gtk_widget_destroy(dialog);
+
+	if (filename){
+		set_filename(filename, TRUE);
+		save_dives_logic(filename, TRUE);
+		g_free(filename);
+	}
+}
+
 static void expand_all_cb(GtkWidget *menuitem, GtkTreeView *tree_view)
 {
 	gtk_tree_view_expand_all(tree_view);
@@ -2405,10 +2446,16 @@ static void popup_divelist_menu(GtkTreeView *tree_view, GtkTreeModel *model, int
 				menuitem = gtk_menu_item_new_with_label(_("Edit dive date/time"));
 				g_signal_connect(menuitem, "activate", G_CALLBACK(edit_dive_when_cb), dive);
 				gtk_menu_shell_append(GTK_MENU_SHELL(menu), menuitem);
+
 			} else {
 				deletelabel = _(deleteplurallabel);
 				editlabel = _(editplurallabel);
 			}
+
+			menuitem = gtk_menu_item_new_with_label(_("Save as"));
+			g_signal_connect(menuitem, "activate", G_CALLBACK(save_as_cb), dive);
+			gtk_menu_shell_append(GTK_MENU_SHELL(menu), menuitem);
+
 			menuitem = gtk_menu_item_new_with_label(deletelabel);
 			g_signal_connect(menuitem, "activate", G_CALLBACK(delete_selected_dives_cb), path);
 			gtk_menu_shell_append(GTK_MENU_SHELL(menu), menuitem);
@@ -2473,6 +2520,7 @@ static void popup_divelist_menu(GtkTreeView *tree_view, GtkTreeModel *model, int
 			}
 		}
 	}
+
 	menuitem = gtk_menu_item_new_with_label(_("Expand all"));
 	g_signal_connect(menuitem, "activate", G_CALLBACK(expand_all_cb), tree_view);
 	gtk_menu_shell_append(GTK_MENU_SHELL(menu), menuitem);
diff --git a/save-xml.c b/save-xml.c
index 6e33cd5..b95a380 100644
--- a/save-xml.c
+++ b/save-xml.c
@@ -536,6 +536,11 @@ static void save_device_info(FILE *f)
 
 void save_dives(const char *filename)
 {
+	save_dives_logic(filename, FALSE);
+}
+
+void save_dives_logic(const char *filename, const gboolean select_only)
+{
 	int i;
 	struct dive *dive;
 	dive_trip_t *trip;
@@ -561,21 +566,30 @@ void save_dives(const char *filename)
 
 	/* save the dives */
 	for_each_dive(i, dive) {
-		trip = dive->divetrip;
 
-		/* Bare dive without a trip? */
-		if (!trip) {
+		if (select_only) {
+
+			if(!dive->selected)
+				continue;
 			save_dive(f, dive);
-			continue;
-		}
 
-		/* Have we already seen this trip (and thus saved this dive?) */
-		if (trip->index)
-			continue;
+		} else {
+			trip = dive->divetrip;
+
+			/* Bare dive without a trip? */
+			if (!trip) {
+				save_dive(f, dive);
+				continue;
+			}
 
-		/* We haven't seen this trip before - save it and all dives */
-		trip->index = 1;
-		save_trip(f, trip);
+			/* Have we already seen this trip (and thus saved this dive?) */
+			if (trip->index)
+				continue;
+
+			/* We haven't seen this trip before - save it and all dives */
+			trip->index = 1;
+			save_trip(f, trip);
+		}
 	}
 	fprintf(f, "</dives>\n</divelog>\n");
 	fclose(f);
-- 
1.8.1




More information about the subsurface mailing list