1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
|
/*
* Copyright (C) 2002 Marco Pesenti Gritti
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
* $Id$
*/
#include "config.h"
#include <string.h>
#include <glib.h>
#include <gtk/gtktypebuiltins.h>
#include "ephy-debug.h"
#include "ephy-string.h"
#include "ephy-print-utils.h"
#define PRINT_SETTINGS_GROUP "Print Settings"
#define PAGE_SETUP_GROUP "Page Setup"
#define PAPER_SIZE_GROUP "Paper Size"
#define ERROR_QUARK (g_quark_from_static_string ("ephy-print-utils-error"))
/**
* ephy_print_utils_settings_new_from_file:
* @file_name: the filename to read the settings from
* @error:
*
* Reads the print settings from @filename. Returns a new #GtkPrintSettings
* object with the restored settings, or %NULL if an error occurred.
*
* Return value: the restored #GtkPrintSettings
*
* Since: 2.10
*/
GtkPrintSettings *
ephy_print_utils_settings_new_from_file (const gchar *file_name,
GError **error)
{
GtkPrintSettings *settings;
GKeyFile *key_file;
g_return_val_if_fail (file_name != NULL, NULL);
key_file = g_key_file_new ();
if (!g_key_file_load_from_file (key_file, file_name, 0, error))
{
g_key_file_free (key_file);
return NULL;
}
settings = ephy_print_utils_settings_new_from_key_file (key_file, error);
g_key_file_free (key_file);
return settings;
}
/**
* ephy_print_utils_settings_new_from_key_file:
* @key_file: the #GKeyFile to retrieve the settings from
* @error:
*
* Reads the print settings from @key_file. Returns a new #GtkPrintSettings
* object with the restored settings, or %NULL if an error occurred.
*
* Return value: the restored #GtkPrintSettings
*
* Since: 2.10
*/
GtkPrintSettings *
ephy_print_utils_settings_new_from_key_file (GKeyFile *key_file,
GError **error)
{
GtkPrintSettings *settings;
gchar **keys;
gsize n_keys, i;
GError *err = NULL;
g_return_val_if_fail (key_file != NULL, NULL);
keys = g_key_file_get_keys (key_file,
PRINT_SETTINGS_GROUP,
&n_keys,
&err);
if (err != NULL)
{
g_propagate_error (error, err);
return NULL;
}
settings = gtk_print_settings_new ();
for (i = 0 ; i < n_keys; ++i)
{
gchar *value;
value = g_key_file_get_string (key_file,
PRINT_SETTINGS_GROUP,
keys[i],
NULL);
if (!value)
continue;
gtk_print_settings_set (settings, keys[i], value);
g_free (value);
}
g_strfreev (keys);
return settings;
}
/**
* ephy_print_utils_settings_to_file:
* @settings: a #GtkPrintSettings
* @file_name: the file to save to
* @error:
*
* This function saves the print settings from @settings to @file_name.
*
* Return value: %TRUE on success
*
* Since: 2.10
*/
gboolean
ephy_print_utils_settings_to_file (GtkPrintSettings *settings,
const char *file_name,
GError **error)
{
GKeyFile *keyfile;
gboolean retval;
char *data = NULL;
gsize len;
GError *err = NULL;
g_return_val_if_fail (GTK_IS_PRINT_SETTINGS (settings), FALSE);
g_return_val_if_fail (file_name != NULL, FALSE);
keyfile = g_key_file_new ();
retval = ephy_print_utils_settings_to_key_file (settings, keyfile, &err);
if (!retval) goto out;
data = g_key_file_to_data (keyfile, &len, &err);
if (!data) goto out;
retval = g_file_set_contents (file_name, data, len, &err);
out:
if (err != NULL)
g_propagate_error (error, err);
g_key_file_free (keyfile);
g_free (data);
return retval;
}
static void
add_value_to_keyfile (const gchar *key,
const gchar *value,
GKeyFile *keyfile)
{
g_key_file_set_string (keyfile, PRINT_SETTINGS_GROUP, key, value);
}
/**
* ephy_print_utils_settings_to_key_file:
* @settings: a #GtkPrintSettings
* @key_file: the #GKeyFile to save the print settings to
* @error:
*
* This function adds the print settings from @settings to @key_file.
*
* Return value: %TRUE on success
*
* Since: 2.10
*/
gboolean
ephy_print_utils_settings_to_key_file (GtkPrintSettings *settings,
GKeyFile *key_file,
GError **error)
{
g_return_val_if_fail (GTK_IS_PRINT_SETTINGS (settings), FALSE);
g_return_val_if_fail (key_file != NULL, FALSE);
gtk_print_settings_foreach (settings,
(GtkPrintSettingsFunc) add_value_to_keyfile,
key_file);
return TRUE;
}
/**
* ephy_print_utils_page_setup_new_from_file:
* @file_name: the filename to read the page_setup from
* @error:
*
* Reads the print page_setup from @filename. Returns a new #GtkPageSetup
* object with the restored page_setup, or %NULL if an error occurred.
*
* Return value: the restored #GtkPageSetup
*
* Since: 2.10
*/
GtkPageSetup *
ephy_print_utils_page_setup_new_from_file (const gchar *file_name,
GError **error)
{
GtkPageSetup *page_setup;
GKeyFile *key_file;
g_return_val_if_fail (file_name != NULL, NULL);
key_file = g_key_file_new ();
if (!g_key_file_load_from_file (key_file, file_name, 0, error))
{
g_key_file_free (key_file);
return NULL;
}
page_setup = ephy_print_utils_page_setup_new_from_key_file (key_file, error);
g_key_file_free (key_file);
return page_setup;
}
/**
* ephy_print_utils_page_setup_new_from_key_file:
* @key_file: the #GKeyFile to retrieve the page_setup from
* @error:
*
* Reads the print page_setup from @key_file. Returns a new #GtkPageSetup
* object with the restored page_setup, or %NULL if an error occurred.
*
* Return value: the restored #GtkPageSetup
*
* Since: 2.10
*/
GtkPageSetup *
ephy_print_utils_page_setup_new_from_key_file (GKeyFile *key_file,
GError **error)
{
GtkPageSetup *page_setup = NULL;
GtkPaperSize *paper_size;
gdouble width, height, top, bottom, left, right;
char *name = NULL, *ppd_name = NULL, *display_name = NULL, *orientation = NULL;
gboolean retval = TRUE;
GError *err = NULL;
g_return_val_if_fail (key_file != NULL, NULL);
if (!g_key_file_has_group (key_file, PAGE_SETUP_GROUP) ||
!g_key_file_has_group (key_file, PAPER_SIZE_GROUP)) {
g_set_error (error, ERROR_QUARK, 0, "Not a valid epiphany page setup file");
retval = FALSE;
goto out;
}
#define GET_DOUBLE(kf, group, name, v) \
v = g_key_file_get_double (kf, group, name, &err); \
if (err != NULL) {\
g_propagate_error (error, err);\
retval = FALSE;\
goto out;\
}
GET_DOUBLE (key_file, PAPER_SIZE_GROUP, "Width", width);
GET_DOUBLE (key_file, PAPER_SIZE_GROUP, "Height", height);
GET_DOUBLE (key_file, PAGE_SETUP_GROUP, "MarginTop", top);
GET_DOUBLE (key_file, PAGE_SETUP_GROUP, "MarginBottom", bottom);
GET_DOUBLE (key_file, PAGE_SETUP_GROUP, "MarginLeft", left);
GET_DOUBLE (key_file, PAGE_SETUP_GROUP, "MarginRight", right);
#undef GET_DOUBLE
name = g_key_file_get_string (key_file, PAPER_SIZE_GROUP,
"Name", NULL);
ppd_name = g_key_file_get_string (key_file, PAPER_SIZE_GROUP,
"PPDName", NULL);
display_name = g_key_file_get_string (key_file, PAPER_SIZE_GROUP,
"DisplayName", NULL);
orientation = g_key_file_get_string (key_file, PAGE_SETUP_GROUP,
"Orientation", NULL);
if ((ppd_name == NULL && name == NULL) || orientation == NULL)
{
g_set_error (error, ERROR_QUARK, 0, "Not a valid epiphany page setup file");
retval = FALSE;
goto out;
}
if (ppd_name != NULL) {
paper_size = gtk_paper_size_new_from_ppd (ppd_name, display_name,
width, height);
} else {
paper_size = gtk_paper_size_new_custom (name, display_name,
width, height, GTK_UNIT_MM);
}
g_assert (paper_size != NULL);
page_setup = gtk_page_setup_new ();
gtk_page_setup_set_paper_size (page_setup, paper_size);
gtk_paper_size_free (paper_size);
gtk_page_setup_set_top_margin (page_setup, top, GTK_UNIT_MM);
gtk_page_setup_set_bottom_margin (page_setup, bottom, GTK_UNIT_MM);
gtk_page_setup_set_left_margin (page_setup, left, GTK_UNIT_MM);
gtk_page_setup_set_right_margin (page_setup, right, GTK_UNIT_MM);
gtk_page_setup_set_orientation (page_setup,
ephy_string_enum_from_string (GTK_TYPE_PAGE_ORIENTATION,
orientation));
out:
if (!retval) g_warning ("page_setup_new_from_keyfile failed"); //XXX
g_free (ppd_name);
g_free (name);
g_free (display_name);
g_free (orientation);
return page_setup;
}
/**
* ephy_print_utils_page_setup_to_file:
* @page_setup: a #GtkPageSetup
* @file_name: the file to save to
* @error:
*
* This function saves the print page_setup from @page_setup to @file_name.
*
* Return value: %TRUE on success
*
* Since: 2.10
*/
gboolean
ephy_print_utils_page_setup_to_file (GtkPageSetup *page_setup,
const char *file_name,
GError **error)
{
GKeyFile *keyfile;
gboolean retval;
char *data = NULL;
gsize len;
g_return_val_if_fail (GTK_IS_PAGE_SETUP (page_setup), FALSE);
g_return_val_if_fail (file_name != NULL, FALSE);
keyfile = g_key_file_new ();
retval = ephy_print_utils_page_setup_to_key_file (page_setup, keyfile, error);
if (!retval) goto out;
data = g_key_file_to_data (keyfile, &len, error);
if (!data) goto out;
retval = g_file_set_contents (file_name, data, len, error);
out:
g_key_file_free (keyfile);
g_free (data);
return retval;
}
/**
* ephy_print_utils_page_setup_to_key_file:
* @page_setup: a #GtkPageSetup
* @key_file: the #GKeyFile to save the print page_setup to
* @error:
*
* This function adds the print page_setup from @page_setup to @key_file.
*
* Return value: %TRUE on success
*
* Since: 2.10
*/
gboolean
ephy_print_utils_page_setup_to_key_file (GtkPageSetup *page_setup,
GKeyFile *key_file,
GError **error)
{
GtkPaperSize *paper_size;
const char *name, *ppd_name, *display_name;
char *orientation;
g_return_val_if_fail (GTK_IS_PAGE_SETUP (page_setup), FALSE);
g_return_val_if_fail (key_file != NULL, FALSE);
paper_size = gtk_page_setup_get_paper_size (page_setup);
g_assert (paper_size != NULL);
name = gtk_paper_size_get_name (paper_size);
display_name = gtk_paper_size_get_display_name (paper_size);
ppd_name = gtk_paper_size_get_ppd_name (paper_size);
if (ppd_name != NULL) {
g_key_file_set_string (key_file, PAPER_SIZE_GROUP,
"PPDName", ppd_name);
} else {
g_key_file_set_string (key_file, PAPER_SIZE_GROUP,
"Name", name);
}
if (display_name) {
g_key_file_set_string (key_file, PAPER_SIZE_GROUP,
"DisplayName", display_name);
}
g_key_file_set_double (key_file, PAPER_SIZE_GROUP,
"Width", gtk_paper_size_get_width (paper_size, GTK_UNIT_MM));
g_key_file_set_double (key_file, PAPER_SIZE_GROUP,
"Height", gtk_paper_size_get_height (paper_size, GTK_UNIT_MM));
g_key_file_set_double (key_file, PAGE_SETUP_GROUP,
"MarginTop", gtk_page_setup_get_top_margin (page_setup, GTK_UNIT_MM));
g_key_file_set_double (key_file, PAGE_SETUP_GROUP,
"MarginBottom", gtk_page_setup_get_bottom_margin (page_setup, GTK_UNIT_MM));
g_key_file_set_double (key_file, PAGE_SETUP_GROUP,
"MarginLeft", gtk_page_setup_get_left_margin (page_setup, GTK_UNIT_MM));
g_key_file_set_double (key_file, PAGE_SETUP_GROUP,
"MarginRight", gtk_page_setup_get_right_margin (page_setup, GTK_UNIT_MM));
orientation = ephy_string_enum_to_string (GTK_TYPE_PAGE_ORIENTATION,
gtk_page_setup_get_orientation (page_setup));
g_key_file_set_string (key_file, PAGE_SETUP_GROUP,
"Orientation", orientation);
g_free (orientation);
return TRUE;
}
|