evolution-3.6.4/e-util/e-print.c

No issues found

  1 /*
  2  *
  3  * This program is free software; you can redistribute it and/or
  4  * modify it under the terms of the GNU Lesser General Public
  5  * License as published by the Free Software Foundation; either
  6  * version 2 of the License, or (at your option) version 3.
  7  *
  8  * This program is distributed in the hope that it will be useful,
  9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
 10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 11  * Lesser General Public License for more details.
 12  *
 13  * You should have received a copy of the GNU Lesser General Public
 14  * License along with the program; if not, see <http://www.gnu.org/licenses/>
 15  *
 16  *
 17  * Authors:
 18  *		JP Rosevear <jpr@novell.com>
 19  *
 20  * Copyright (C) 1999-2008 Novell, Inc. (www.novell.com)
 21  *
 22  */
 23 
 24 #ifdef HAVE_CONFIG_H
 25 #include <config.h>
 26 #endif
 27 
 28 #include "e-print.h"
 29 
 30 #include <stdio.h>
 31 #include <string.h>
 32 #include <gtk/gtk.h>
 33 #include <glib/gi18n.h>
 34 
 35 #include "e-util.h"
 36 
 37 /* XXX Would be better if GtkPrint exposed these. */
 38 #define PAGE_SETUP_GROUP_NAME		"Page Setup"
 39 #define PRINT_SETTINGS_GROUP_NAME	"Print Settings"
 40 
 41 static gchar *
 42 key_file_get_filename (void)
 43 {
 44 	return g_build_filename (e_get_user_data_dir (), "printing.ini", NULL);
 45 }
 46 
 47 static void
 48 load_key_file (GKeyFile *key_file)
 49 {
 50 	gchar *filename;
 51 	GError *error = NULL;
 52 
 53 	filename = key_file_get_filename ();
 54 
 55 	if (!g_file_test (filename, G_FILE_TEST_EXISTS))
 56 		goto exit;
 57 
 58 	g_key_file_load_from_file (
 59 		key_file, filename, G_KEY_FILE_KEEP_COMMENTS |
 60 		G_KEY_FILE_KEEP_TRANSLATIONS, &error);
 61 
 62 	if (error != NULL) {
 63 		g_warning ("%s", error->message);
 64 		g_error_free (error);
 65 	}
 66 
 67 exit:
 68 	g_free (filename);
 69 }
 70 
 71 static void
 72 save_key_file (GKeyFile *key_file)
 73 {
 74 	gchar *contents;
 75 	gchar *filename;
 76 	gsize length;
 77 	GError *error = NULL;
 78 
 79 	filename = key_file_get_filename ();
 80 	contents = g_key_file_to_data (key_file, &length, NULL);
 81 
 82 	g_file_set_contents (filename, contents, length, &error);
 83 
 84 	if (error != NULL) {
 85 		g_warning ("%s", error->message);
 86 		g_error_free (error);
 87 	}
 88 
 89 	g_free (contents);
 90 	g_free (filename);
 91 }
 92 
 93 static GtkPrintSettings *
 94 load_settings (GKeyFile *key_file)
 95 {
 96 	GtkPrintSettings *settings;
 97 	GError *error = NULL;
 98 
 99 	settings = gtk_print_settings_new ();
100 
101 	if (g_key_file_has_group (key_file, PRINT_SETTINGS_GROUP_NAME))
102 		gtk_print_settings_load_key_file (
103 			settings, key_file, PRINT_SETTINGS_GROUP_NAME, &error);
104 
105 	if (error != NULL) {
106 		g_warning ("%s", error->message);
107 		g_error_free (error);
108 	}
109 
110 	return settings;
111 }
112 
113 static void
114 save_settings (GtkPrintSettings *settings,
115                GKeyFile *key_file)
116 {
117 	/* XXX GtkPrintSettings does not distinguish between settings
118 	 *     that should persist and one-time-only settings, such as
119 	 *     page range or number of copies.  All print settings are
120 	 *     persistent by default and we opt out particular keys by
121 	 *     popular demand. */
122 
123 	gtk_print_settings_unset (settings, GTK_PRINT_SETTINGS_N_COPIES);
124 	gtk_print_settings_unset (settings, GTK_PRINT_SETTINGS_PAGE_RANGES);
125 	gtk_print_settings_unset (settings, GTK_PRINT_SETTINGS_PAGE_SET);
126 	gtk_print_settings_unset (settings, GTK_PRINT_SETTINGS_PRINT_PAGES);
127 
128 	gtk_print_settings_to_key_file (settings, key_file, NULL);
129 }
130 
131 static GtkPageSetup *
132 load_page_setup (GKeyFile *key_file)
133 {
134 	GtkPageSetup *page_setup;
135 
136 	page_setup = gtk_page_setup_new ();
137 
138 	if (g_key_file_has_group (key_file, PAGE_SETUP_GROUP_NAME))
139 		gtk_page_setup_load_key_file (
140 			page_setup, key_file, PAGE_SETUP_GROUP_NAME, NULL);
141 
142 	return page_setup;
143 }
144 
145 static void
146 save_page_setup (GtkPageSetup *page_setup,
147                  GKeyFile *key_file)
148 {
149 	gtk_page_setup_to_key_file (page_setup, key_file, NULL);
150 }
151 
152 static void
153 handle_error (GtkPrintOperation *operation)
154 {
155 	GtkWidget *dialog;
156 	GError *error = NULL;
157 
158 	dialog = gtk_message_dialog_new_with_markup (
159 		NULL, 0, GTK_MESSAGE_ERROR, GTK_BUTTONS_OK,
160 		"<span weight=\"bold\" size=\"larger\">%s</span>",
161 		_("An error occurred while printing"));
162 
163 	gtk_print_operation_get_error (operation, &error);
164 
165 	if (error != NULL && error->message != NULL)
166 		gtk_message_dialog_format_secondary_text (
167 			GTK_MESSAGE_DIALOG (dialog), "%s\n\n%s",
168 			_("The printing system reported the "
169 			"following details about the error:"),
170 			error->message);
171 	else
172 		gtk_message_dialog_format_secondary_text (
173 			GTK_MESSAGE_DIALOG (dialog), "%s",
174 			_("The printing system did not report "
175 			"any additional details about the error."));
176 
177 	if (error != NULL)
178 		g_error_free (error);
179 
180 	gtk_dialog_run (GTK_DIALOG (dialog));
181 
182 	gtk_widget_destroy (dialog);
183 }
184 
185 static void
186 print_done_cb (GtkPrintOperation *operation,
187                GtkPrintOperationResult result,
188                GKeyFile *key_file)
189 {
190 	GtkPrintSettings *settings;
191 
192 	settings = gtk_print_operation_get_print_settings (operation);
193 
194 	if (result == GTK_PRINT_OPERATION_RESULT_APPLY)
195 		save_settings (settings, key_file);
196 	if (result == GTK_PRINT_OPERATION_RESULT_ERROR)
197 		handle_error (operation);
198 
199 	save_key_file (key_file);
200 	g_key_file_free (key_file);
201 }
202 
203 GtkPrintOperation *
204 e_print_operation_new (void)
205 {
206 	GtkPrintOperation *operation;
207 	GtkPrintSettings *settings;
208 	GtkPageSetup *page_setup;
209 	GKeyFile *key_file;
210 
211 	operation = gtk_print_operation_new ();
212 
213 	key_file = g_key_file_new ();
214 	load_key_file (key_file);
215 
216 	settings = load_settings (key_file);
217 	gtk_print_operation_set_print_settings (operation, settings);
218 	g_object_unref (settings);
219 
220 	page_setup = load_page_setup (key_file);
221 	gtk_print_operation_set_default_page_setup (operation, page_setup);
222 	g_object_unref (page_setup);
223 
224 	g_signal_connect (
225 		operation, "done",
226 		G_CALLBACK (print_done_cb), key_file);
227 
228 	return operation;
229 }
230 
231 void
232 e_print_run_page_setup_dialog (GtkWindow *parent)
233 {
234 	GtkPageSetup *new_page_setup;
235 	GtkPageSetup *old_page_setup;
236 	GtkPrintSettings *settings;
237 	GKeyFile *key_file;
238 
239 	key_file = g_key_file_new ();
240 	load_key_file (key_file);
241 
242 	settings = load_settings (key_file);
243 	old_page_setup = load_page_setup (key_file);
244 	new_page_setup = gtk_print_run_page_setup_dialog (
245 		parent, old_page_setup, settings);
246 	save_page_setup (new_page_setup, key_file);
247 	save_settings (settings, key_file);
248 
249 	g_object_unref (new_page_setup);
250 	g_object_unref (old_page_setup);
251 	g_object_unref (settings);
252 
253 	save_key_file (key_file);
254 	g_key_file_free (key_file);
255 }