[Planner Dev] Patch for bug 129142: Remember print settings



Hello,

the following patch must be applied against current CVS.
The code is coming directly from gedit. Printer settings are stored into
$HOME/.gnome2/planner-print-config , in libgnomeprint XML format.
The "which views to print" settings are not stored. I think it's
possible to use libgnomeprint to store them, but I didn't try.

Regards,

-- 
Cédric Delfosse, http://cedric.freezope.org
Jabber ID: cedric jabber debian net
Index: src/planner-print-dialog.c
===================================================================
RCS file: /cvs/gnome/planner/src/planner-print-dialog.c,v
retrieving revision 1.2
diff -u -r1.2 planner-print-dialog.c
--- src/planner-print-dialog.c	11 Dec 2003 10:52:46 -0000	1.2
+++ src/planner-print-dialog.c	13 Dec 2003 23:35:00 -0000
@@ -20,8 +20,13 @@
  * Boston, MA 02111-1307, USA.
  */
 
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <unistd.h>
 #include <config.h>
 #include <string.h>
+#include <libgnome/libgnome.h>
 #include <libgnome/gnome-i18n.h>
 #include <gtk/gtk.h>
 #include <libgnomeprint/gnome-print.h>
@@ -32,12 +37,72 @@
 #include "planner-view.h"
 #include "planner-print-dialog.h"
 
+#define PLANNER_PRINT_CONFIG_FILE "planner-print-config"
+
 static GtkWidget *   print_dialog_create_page  (GtkWidget *dialog,
 						GList     *views);
 
 static GtkNotebook * print_dialog_get_notebook (GtkWidget *dialog);
 
 
+/*
+ * Load printer configuration from a file.
+ * Return a GnomePrintConfig object containing the configuration.
+ */
+GnomePrintConfig *
+load_planner_print_config_from_file ()
+{
+	gchar *filename;
+	gboolean res;
+	gchar *contents;
+	GnomePrintConfig *planner_print_config;
+	
+	filename = gnome_util_home_file (PLANNER_PRINT_CONFIG_FILE);
+	
+	res = g_file_get_contents (filename, &contents, NULL, NULL);
+	g_free (filename);
+	
+	if (res) {
+		planner_print_config = gnome_print_config_from_string (contents, 0);
+		g_free (contents);
+	} else planner_print_config = gnome_print_config_default ();
+	
+	return planner_print_config;
+}
+
+/*
+ * Save printer configuration into a file in the .gnome2 user directory
+ */
+void
+save_planner_print_config_to_file (GnomePrintConfig *planner_print_config)
+{
+	gint fd;
+	gchar *str;
+	gint bytes;
+	gchar *filename;
+	gchar save_error[] = "planner cannot save print config file: ";
+	
+	g_return_if_fail (planner_print_config != NULL);
+	
+	str = gnome_print_config_to_string (planner_print_config, 0);
+	g_return_if_fail (str != NULL);
+	
+ 	filename = gnome_util_home_file (PLANNER_PRINT_CONFIG_FILE);
+	
+	fd = open (filename, O_WRONLY | O_CREAT | O_TRUNC, 0600);
+	g_free (filename);
+	
+	if (fd < 0) g_message ("%s can't create file '%s'.", save_error, filename);
+	else {
+		bytes = strlen (str);
+	
+		/* Save the file content */
+		if (write (fd, str, bytes) == bytes) close (fd);
+		else g_message ("%s can't save config into '%s'.", save_error, filename);
+	}
+	g_free (str);
+}
+
 GtkWidget *
 planner_print_dialog_new (PlannerWindow  *window,
 		     GnomePrintJob *job,
Index: src/planner-print-dialog.h
===================================================================
RCS file: /cvs/gnome/planner/src/planner-print-dialog.h,v
retrieving revision 1.2
diff -u -r1.2 planner-print-dialog.h
--- src/planner-print-dialog.h	11 Dec 2003 10:52:46 -0000	1.2
+++ src/planner-print-dialog.h	13 Dec 2003 23:35:00 -0000
@@ -29,6 +29,8 @@
 						 GList         *views);
 GList *     planner_print_dialog_get_print_selection (GtkDialog     *dialog,
 						 gboolean      *summary);
+GnomePrintConfig * load_planner_print_config_from_file (void);
+void save_planner_print_config_to_file (GnomePrintConfig *planner_print_config);
 
 
 #endif /* __PLANNER_PRINT_DIALOG_H__ */
Index: src/planner-window.c
===================================================================
RCS file: /cvs/gnome/planner/src/planner-window.c,v
retrieving revision 1.2
diff -u -r1.2 planner-window.c
--- src/planner-window.c	11 Dec 2003 21:53:03 -0000	1.2
+++ src/planner-window.c	13 Dec 2003 23:35:03 -0000
@@ -770,6 +770,7 @@
 	PlannerWindow     *window;
 	PlannerWindowPriv *priv;
 	GnomePrintJob    *gpj;
+	GnomePrintConfig *config;
 	GtkWidget        *dialog;
 	gint              response;
 	gboolean          summary;
@@ -783,20 +784,28 @@
 	window = PLANNER_WINDOW (data);
 	priv = window->priv;
 
-	gpj = gnome_print_job_new (NULL);
-	
+	/* Load printer settings */
+	config = load_planner_print_config_from_file ();
+	gpj = gnome_print_job_new (config);
+	gnome_print_config_unref (config);
+
 	dialog = planner_print_dialog_new (data, gpj, priv->views);
 
 	response = gtk_dialog_run (GTK_DIALOG (dialog));
 
 	if (response == GTK_RESPONSE_CANCEL) {
 		gtk_widget_destroy (dialog);
+		g_object_unref (gpj);
 		return;
 	}
 	else if (response == GTK_RESPONSE_DELETE_EVENT) {
+		g_object_unref (gpj);
 		return;
 	}
 	
+	/* Save printer settings */
+	save_planner_print_config_to_file (config);
+
 	views = planner_print_dialog_get_print_selection (GTK_DIALOG (dialog), &summary);
 
 	if (summary) {

Attachment: signature.asc
Description: Ceci est une partie de message =?ISO-8859-1?Q?num=E9riquement?= =?ISO-8859-1?Q?_sign=E9e=2E?=



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]