[Glade-devel] [patch, glade3] confirmation dialog on quit

paolo borelli pborelli@katamail.com
12 Apr 2003 20:55:50 +0200


--=-FCMhdGLTMpVMWMvJDOAh
Content-Type: text/plain
Content-Transfer-Encoding: 7bit

Hi!

The patch attached adds a confirmation dialog with "Don't
Save/Cancel/Save" buttons that is displayed before quitting if there is
any project with changes not saved. 

ciao
	paolo

--=-FCMhdGLTMpVMWMvJDOAh
Content-Disposition: attachment; filename=confirm_close.patch
Content-Type: text/x-patch; name=confirm_close.patch; charset=UTF-8
Content-Transfer-Encoding: 7bit

diff -upr gnome2/glade3/ChangeLog glade3/ChangeLog
--- gnome2/glade3/ChangeLog	2003-04-03 18:44:13.000000000 +0200
+++ glade3/ChangeLog	2003-04-12 20:42:38.000000000 +0200
@@ -1,3 +1,10 @@
+2003-04-12  Paolo Borelli <pborelli@katamail.com>
+
+	* src/glade-project.[ch]: properly clear the changed flag on save; make 
+	open/save functions return TRUE on success.
+	* src/glade-project-window.c: implement a confirmation dialog to be
+	displayed on quit if any open project need saving.
+
 2003-04-02  Joaquin Cuenca Abela  <e98cuenc@yahoo.com>
 
 	* src/glade-menu-editor.c: Fix the segfault when adding a new menu item.
diff -upr gnome2/glade3/src/glade-project.c glade3/src/glade-project.c
--- gnome2/glade3/src/glade-project.c	2003-04-03 18:44:52.000000000 +0200
+++ glade3/src/glade-project.c	2003-04-12 15:57:56.000000000 +0200
@@ -635,6 +635,8 @@ glade_project_save_to_file (GladeProject
 	g_free (project->name);
 	project->name = g_path_get_basename (project->path);
 
+	project->changed = FALSE;
+
 	return TRUE;
 }
 
@@ -642,31 +644,32 @@ glade_project_save_to_file (GladeProject
  * glade_project_open:
  * @path: 
  * 
- * Open a project at the given path.
+ * Open a project at the given path. Returns TRUE on success.
  **/
-void
+gboolean
 glade_project_open (const gchar *path)
 {
 	GladeProjectWindow *gpw;
 	GladeProject *project;
 
-	g_return_if_fail (path != NULL);
+	g_return_val_if_fail (path != NULL, FALSE);
 
 	gpw = glade_project_window_get ();
 
 	/* If the project is previously loaded, don't re-load */
 	if ((project = glade_project_check_previously_loaded (path)) != NULL) {
 		glade_project_window_set_project (gpw, project);
-		return;
+		return TRUE;
 	}
 
 	project = glade_project_open_from_file (path);
 	if (!project) {
 		glade_util_ui_warn (_("Could not open project."));
-		return;
+		return FALSE;
 	}
 
 	glade_project_window_add_project (gpw, project);
+	return TRUE;
 }
 
 /**
@@ -674,26 +677,28 @@ glade_project_open (const gchar *path)
  * @project:
  * @path 
  * 
- * Save the project to the given path
+ * Save the project to the given path. Returns TRUE on success.
  **/
-void
+gboolean
 glade_project_save (GladeProject *project, const gchar *path)
 {
 	GladeProjectWindow *gpw;
 
-	g_return_if_fail (GLADE_IS_PROJECT (project));
-	g_return_if_fail (path != NULL);
+	g_return_val_if_fail (GLADE_IS_PROJECT (project), FALSE);
+	g_return_val_if_fail (path != NULL, FALSE);
 
 	gpw = glade_project_window_get ();
 
 	if (!glade_project_save_to_file (project, path)) {
 		glade_util_ui_warn (_("Invalid file name"));
-		return;
+		return FALSE;
 	}
 
 	glade_project_refresh_menu_item (project);
 	glade_project_window_refresh_title (gpw);	
 	glade_util_flash_message (gpw->statusbar_actions_context_id,
 				  _("Project '%s' saved"), project->name);
+
+	return TRUE;
 }
 
diff -upr gnome2/glade3/src/glade-project.h glade3/src/glade-project.h
--- gnome2/glade3/src/glade-project.h	2003-04-03 18:44:52.000000000 +0200
+++ glade3/src/glade-project.h	2003-04-12 15:54:58.000000000 +0200
@@ -64,8 +64,8 @@ GladeProject *glade_project_new (gboolea
 
 
 /* Project operations */
-void glade_project_save (GladeProject *project, const gchar *path);
-void glade_project_open (const gchar *path);
+gboolean glade_project_save (GladeProject *project, const gchar *path);
+gboolean glade_project_open (const gchar *path);
 
 
 /* Widget related stuff */
diff -upr gnome2/glade3/src/glade-project-window.c glade3/src/glade-project-window.c
--- gnome2/glade3/src/glade-project-window.c	2003-04-03 18:44:47.000000000 +0200
+++ glade3/src/glade-project-window.c	2003-04-12 20:33:46.000000000 +0200
@@ -164,9 +164,88 @@ gpw_save_as_cb (void)
 	gtk_widget_show (filesel);
 }
 
+static gboolean
+gpw_confirm_close_project (GladeProject *project)
+{
+	GladeProjectWindow *gpw;
+	GtkWidget *dialog;
+	gboolean close;
+	gint ret;
+
+	g_return_val_if_fail (GLADE_IS_PROJECT (project), FALSE);
+
+	gpw = glade_project_window_get ();
+	
+	dialog = gtk_message_dialog_new (GTK_WINDOW (gpw->window),
+					 GTK_DIALOG_MODAL,
+					 GTK_MESSAGE_QUESTION,
+					 GTK_BUTTONS_NONE,
+					 _("Do you want to save the changes you made to the project \"%s\"? \n\n"
+					  "Your changes will be lost if you don't save them."),
+					 project->name);
+
+	gtk_dialog_add_buttons (GTK_DIALOG (dialog),
+				_("Do_n't save"), GTK_RESPONSE_NO,
+				GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
+				GTK_STOCK_SAVE, GTK_RESPONSE_YES, NULL);
+
+	gtk_dialog_set_has_separator (GTK_DIALOG (dialog), FALSE);
+	gtk_dialog_set_default_response	(GTK_DIALOG (dialog), GTK_RESPONSE_YES);
+
+	ret = gtk_dialog_run (GTK_DIALOG (dialog));
+	switch (ret) {
+		case GTK_RESPONSE_YES:
+			/* if YES we save the project: note we cannot use gpw_save_cb
+			 * since it saves the current project, while the modified project
+			 * we are saving may be not the current one.
+			 */
+			if (project->path != NULL) {
+				close = glade_project_save (project, project->path);
+			} else {
+				GtkWidget *filesel;
+
+				filesel = glade_util_file_selection_new (_("Save ..."), GTK_WINDOW (gpw->window));
+				g_signal_connect (G_OBJECT (GTK_FILE_SELECTION (filesel)->ok_button),
+						  "clicked", G_CALLBACK (gpw_on_save_filesel_ok),
+						  project);
+
+				gtk_widget_show (filesel);
+				close = FALSE;
+			}
+			break;
+		case GTK_RESPONSE_NO:
+			close = TRUE;
+			break;
+		case GTK_RESPONSE_CANCEL:
+		default:
+			close = FALSE;
+	}
+
+	gtk_widget_destroy (dialog);
+	return close;
+}
+
 static void
 gpw_quit_cb (void)
 {
+	GladeProjectWindow *gpw;
+	GladeProject *project;
+	GList *list;
+	gboolean quit;
+
+	gpw = glade_project_window_get ();
+	list = gpw->projects;
+
+	for (; list; list = list->next) {
+		project = GLADE_PROJECT (list->data);
+
+		if (project->changed) {
+			quit = gpw_confirm_close_project (project);
+			if (!quit)
+				return;
+		}
+	}
+
 	gtk_main_quit ();
 }
 
@@ -802,10 +881,13 @@ gpw_toggle_clipboard_cb (void)
 		gpw_hide_clipboard_view (gpw);
 }
 
-static void
+static gboolean
 gpw_delete_event (GtkWindow *w, gpointer not_used)
 {
 	gpw_quit_cb ();
+	
+	/* return TRUE to stop other handlers */
+	return TRUE;	
 }
 
 static GtkWidget *

--=-FCMhdGLTMpVMWMvJDOAh--