~ubuntu-branches/ubuntu/jaunty/gimp/jaunty-security

« back to all changes in this revision

Viewing changes to plug-ins/print/print-settings.c

  • Committer: Bazaar Package Importer
  • Author(s): Daniel Holbach
  • Date: 2007-05-02 16:33:03 UTC
  • mfrom: (1.1.4 upstream)
  • Revision ID: james.westby@ubuntu.com-20070502163303-bvzhjzbpw8qglc4y
Tags: 2.3.16-1ubuntu1
* Resynchronized with Debian, remaining Ubuntu changes:
  - debian/rules: i18n magic.
* debian/control.in:
  - Maintainer: Ubuntu Core Developers <ubuntu-devel@lists.ubuntu.com>
* debian/patches/02_help-message.patch,
  debian/patches/03_gimp.desktop.in.in.patch,
  debian/patches/10_dont_show_wizard.patch: updated.
* debian/patches/04_composite-signedness.patch,
  debian/patches/05_add-letter-spacing.patch: dropped, used upstream.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* GIMP - The GNU Image Manipulation Program
 
2
 * Copyright (C) 1995 Spencer Kimball and Peter Mattis
 
3
 *
 
4
 * This program is free software; you can redistribute it and/or modify
 
5
 * it under the terms of the GNU General Public License as published by
 
6
 * the Free Software Foundation; either version 2 of the License, or
 
7
 * (at your option) any later version.
 
8
 *
 
9
 * This program is distributed in the hope that it will be useful,
 
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
12
 * GNU General Public License for more details.
 
13
 *
 
14
 * You should have received a copy of the GNU General Public License
 
15
 * along with this program; if not, write to the Free Software
 
16
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 
17
 */
 
18
 
 
19
#define PRINT_SETTINGS_MAJOR_VERSION 0
 
20
#define PRINT_SETTINGS_MINOR_VERSION 2
 
21
 
 
22
#include "config.h"
 
23
 
 
24
#include <libgimp/gimp.h>
 
25
#include <libgimp/gimpui.h>
 
26
 
 
27
#include "print.h"
 
28
#include "print-settings.h"
 
29
 
 
30
 
 
31
static GKeyFile * print_settings_key_file_from_settings      (PrintData         *data);
 
32
 
 
33
static void       save_print_settings_resource_file          (GKeyFile          *settings_key_file);
 
34
 
 
35
static void       save_print_settings_as_parasite            (GKeyFile          *settings_key_file,
 
36
                                                              gint32             image_ID);
 
37
 
 
38
static void       add_print_setting_to_key_file              (const gchar       *key,
 
39
                                                              const gchar       *value,
 
40
                                                              gpointer           data);
 
41
 
 
42
static GKeyFile * print_settings_key_file_from_resource_file (void);
 
43
 
 
44
static GKeyFile * print_settings_key_file_from_parasite      (gint32             image_ID);
 
45
 
 
46
static gboolean   load_print_settings_from_key_file          (PrintData         *data,
 
47
                                                              GKeyFile          *key_file);
 
48
 
 
49
static GKeyFile * check_version                              (GKeyFile          *key_file);
 
50
 
 
51
/*
 
52
 * set GtkPrintSettings from the contents of a "print-settings"
 
53
 * image parasite, or, if none exists, from a resource
 
54
 * file of the same name
 
55
 */
 
56
gboolean
 
57
load_print_settings (PrintData *data)
 
58
{
 
59
  GKeyFile *key_file;
 
60
 
 
61
  key_file = print_settings_key_file_from_parasite (data->image_id);
 
62
 
 
63
  if (! key_file)
 
64
    key_file = print_settings_key_file_from_resource_file ();
 
65
 
 
66
  if (key_file)
 
67
    {
 
68
      load_print_settings_from_key_file (data, key_file);
 
69
      g_key_file_free (key_file);
 
70
      return TRUE;
 
71
    }
 
72
 
 
73
  return FALSE;
 
74
}
 
75
 
 
76
/*
 
77
 * save all settings as a resource file "print-settings"
 
78
 * and as an image parasite
 
79
 */
 
80
void
 
81
save_print_settings (PrintData *data)
 
82
{
 
83
  GKeyFile *key_file;
 
84
 
 
85
  key_file = print_settings_key_file_from_settings (data);
 
86
 
 
87
  save_print_settings_resource_file (key_file);
 
88
 
 
89
  /* image setup */
 
90
  g_key_file_set_integer (key_file, "image-setup", "unit", data->unit);
 
91
  g_key_file_set_double (key_file, "image-setup", "x-resolution", data->xres);
 
92
  g_key_file_set_double (key_file, "image-setup", "y-resolution", data->yres);
 
93
 
 
94
  save_print_settings_as_parasite (key_file, data->image_id);
 
95
 
 
96
  g_key_file_free (key_file);
 
97
}
 
98
 
 
99
/*
 
100
 * serialize print settings into a GKeyFile
 
101
 */
 
102
static GKeyFile *
 
103
print_settings_key_file_from_settings (PrintData *data)
 
104
{
 
105
  GtkPrintOperation *operation = data->operation;
 
106
  GtkPrintSettings  *settings;
 
107
  GKeyFile          *key_file  = g_key_file_new ();
 
108
  GtkPageSetup      *page_setup;
 
109
 
 
110
  g_key_file_set_list_separator (key_file, '=');
 
111
 
 
112
  /* put version information into the file */
 
113
  g_key_file_set_integer (key_file, "meta", "major-version",
 
114
                          PRINT_SETTINGS_MAJOR_VERSION);
 
115
  g_key_file_set_integer (key_file, "meta", "minor-version",
 
116
                          PRINT_SETTINGS_MINOR_VERSION);
 
117
 
 
118
  /* save the contents of the GtkPrintSettings for the operation */
 
119
  settings = gtk_print_operation_get_print_settings (operation);
 
120
  if (settings)
 
121
    {
 
122
      gtk_print_settings_foreach (settings, add_print_setting_to_key_file,
 
123
                                  key_file);
 
124
    }
 
125
 
 
126
  /* page setup */
 
127
  page_setup = gtk_print_operation_get_default_page_setup (operation);
 
128
  if (page_setup)
 
129
    {
 
130
      GtkPageOrientation orientation;
 
131
 
 
132
      orientation = gtk_page_setup_get_orientation (page_setup);
 
133
      g_key_file_set_integer (key_file, "page-setup", "orientation",
 
134
                              orientation);
 
135
    }
 
136
 
 
137
#if 0
 
138
  /* other settings */
 
139
  g_key_file_set_boolean (key_file, "other-settings", "show-header",
 
140
                          data->show_info_header);
 
141
#endif
 
142
 
 
143
  return key_file;
 
144
}
 
145
 
 
146
/*
 
147
 * create a resource file from a GKeyFile holding the settings
 
148
 */
 
149
static void
 
150
save_print_settings_resource_file (GKeyFile *settings_key_file)
 
151
{
 
152
  gchar  *filename;
 
153
  gchar  *contents;
 
154
  gsize   length;
 
155
  GError *error = NULL;
 
156
 
 
157
  contents = g_key_file_to_data (settings_key_file, &length, &error);
 
158
  if (error)
 
159
    {
 
160
      g_warning ("Unable to get contents of settings key file: %s",
 
161
                 error->message);
 
162
      g_error_free (error);
 
163
      return;
 
164
    }
 
165
 
 
166
  filename = g_build_filename (gimp_directory (), "print-settings", NULL);
 
167
 
 
168
  if (! g_file_set_contents (filename, contents, length, &error))
 
169
    {
 
170
      g_warning ("Unable to write print settings to '%s': %s",
 
171
                 gimp_filename_to_utf8 (filename), error->message);
 
172
      g_error_free (error);
 
173
    }
 
174
 
 
175
  g_free (filename);
 
176
  g_free (contents);
 
177
}
 
178
 
 
179
/*
 
180
 * create an image parasite called "print-settings" from a GKeyFile
 
181
 * holding the print settings
 
182
 */
 
183
static void
 
184
save_print_settings_as_parasite (GKeyFile *settings_key_file,
 
185
                                 gint32    image_ID)
 
186
{
 
187
  gchar     *contents;
 
188
  gsize      length;
 
189
  GError    *error = NULL;
 
190
 
 
191
  contents = g_key_file_to_data (settings_key_file, &length, &error);
 
192
  if (! contents)
 
193
    {
 
194
      g_warning ("Unable to get contents of settings key file: %s",
 
195
                 error->message);
 
196
      g_error_free (error);
 
197
      return;
 
198
    }
 
199
 
 
200
  gimp_image_attach_new_parasite (image_ID, "print-settings",
 
201
                                  0, length, contents);
 
202
  g_free (contents);
 
203
}
 
204
 
 
205
/*
 
206
 * callback used in gtk_print_settings_foreach loop
 
207
 */
 
208
static void
 
209
add_print_setting_to_key_file (const gchar *key,
 
210
                               const gchar *value,
 
211
                               gpointer     data)
 
212
{
 
213
  GKeyFile *key_file = data;
 
214
 
 
215
  g_key_file_set_value (key_file, "print-settings", key, value);
 
216
}
 
217
 
 
218
/*
 
219
 * deserialize a "print-settings" resource file into a GKeyFile
 
220
 */
 
221
static GKeyFile *
 
222
print_settings_key_file_from_resource_file (void)
 
223
{
 
224
  GKeyFile  *key_file = g_key_file_new ();
 
225
  gchar     *filename;
 
226
 
 
227
  g_key_file_set_list_separator (key_file, '=');
 
228
 
 
229
  filename = g_build_filename (gimp_directory (), "print-settings", NULL);
 
230
 
 
231
  if (! g_key_file_load_from_file (key_file, filename, G_KEY_FILE_NONE, NULL))
 
232
    {
 
233
      g_key_file_free (key_file);
 
234
      key_file = NULL;
 
235
    }
 
236
 
 
237
  g_free (filename);
 
238
 
 
239
  return check_version (key_file);
 
240
}
 
241
 
 
242
/* load information from an image parasite called "print-settings"
 
243
 * return a GKeyFile containing the information if a valid parasite is found,
 
244
 * NULL otherwise
 
245
 */
 
246
static GKeyFile *
 
247
print_settings_key_file_from_parasite (gint32 image_ID)
 
248
{
 
249
  GimpParasite *parasite;
 
250
  GKeyFile     *key_file;
 
251
 
 
252
  parasite = gimp_image_parasite_find (image_ID, "print-settings");
 
253
 
 
254
  if (! parasite)
 
255
    return NULL;
 
256
 
 
257
  key_file = g_key_file_new ();
 
258
 
 
259
  g_key_file_set_list_separator (key_file, '=');
 
260
 
 
261
  if (! g_key_file_load_from_data (key_file,
 
262
                                   gimp_parasite_data (parasite),
 
263
                                   gimp_parasite_data_size (parasite),
 
264
                                   G_KEY_FILE_NONE, NULL))
 
265
    {
 
266
      g_key_file_free (key_file);
 
267
      key_file = NULL;;
 
268
    }
 
269
 
 
270
  gimp_parasite_free (parasite);
 
271
 
 
272
  return check_version (key_file);
 
273
}
 
274
 
 
275
static gboolean
 
276
load_print_settings_from_key_file (PrintData *data,
 
277
                                   GKeyFile  *key_file)
 
278
{
 
279
  GtkPrintOperation  *operation = data->operation;
 
280
  GtkPrintSettings   *settings;
 
281
  gchar             **keys;
 
282
  gsize               n_keys;
 
283
  gint                i;
 
284
 
 
285
  settings = gtk_print_operation_get_print_settings (operation);
 
286
  if (! settings)
 
287
    settings = gtk_print_settings_new ();
 
288
 
 
289
  keys = g_key_file_get_keys (key_file, "print-settings", &n_keys, NULL);
 
290
 
 
291
  if (! keys)
 
292
    return FALSE;
 
293
 
 
294
  for (i = 0; i < n_keys; i++)
 
295
    {
 
296
      gchar *value;
 
297
 
 
298
      value = g_key_file_get_value (key_file, "print-settings", keys[i], NULL);
 
299
 
 
300
      if (value)
 
301
        {
 
302
          gtk_print_settings_set (settings, keys[i], value);
 
303
          g_free (value);
 
304
        }
 
305
    }
 
306
 
 
307
  g_strfreev (keys);
 
308
 
 
309
  /* page setup parameters */
 
310
 
 
311
  if (g_key_file_has_key (key_file, "page-setup", "orientation", NULL))
 
312
    {
 
313
      GtkPageSetup       *page_setup;
 
314
      GtkPageOrientation  orientation;
 
315
 
 
316
      page_setup = gtk_print_operation_get_default_page_setup (operation);
 
317
      if (! page_setup)
 
318
        page_setup = gtk_page_setup_new ();
 
319
 
 
320
      orientation = g_key_file_get_integer (key_file,
 
321
                                            "page-setup", "orientation", NULL);
 
322
      gtk_page_setup_set_orientation (page_setup, orientation);
 
323
      gtk_print_settings_set_orientation (settings, orientation);
 
324
      data->orientation = orientation;
 
325
 
 
326
      gtk_print_operation_set_default_page_setup (operation, page_setup);
 
327
    }
 
328
 
 
329
  if (g_key_file_has_key (key_file, "image-setup", "unit", NULL))
 
330
    {
 
331
      data->unit = g_key_file_get_integer (key_file,
 
332
                                           "image-setup", "unit", NULL);
 
333
    }
 
334
 
 
335
  if (g_key_file_has_key (key_file, "image-setup", "x-resolution", NULL) &&
 
336
      g_key_file_has_key (key_file, "image-setup", "y-resolution", NULL))
 
337
    {
 
338
      data->xres = g_key_file_get_double (key_file,
 
339
                                          "image-setup", "x-resolution", NULL);
 
340
      data->yres = g_key_file_get_double (key_file,
 
341
                                          "image-setup", "y-resolution", NULL);
 
342
    }
 
343
 
 
344
#if 0
 
345
  /* other settings */
 
346
  if (g_key_file_has_key (key_file, "other-settings", "show-header", NULL))
 
347
    {
 
348
      data->show_info_header = g_key_file_get_boolean (key_file,
 
349
                                                       "other-settings",
 
350
                                                       "show-header", NULL);
 
351
    }
 
352
  else
 
353
#endif
 
354
    {
 
355
      data->show_info_header = FALSE;
 
356
    }
 
357
 
 
358
  gtk_print_operation_set_print_settings (operation, settings);
 
359
 
 
360
  return TRUE;
 
361
}
 
362
 
 
363
static GKeyFile *
 
364
check_version (GKeyFile *key_file)
 
365
{
 
366
  gint  major_version;
 
367
  gint  minor_version;
 
368
 
 
369
  if (! key_file || ! g_key_file_has_group (key_file, "meta"))
 
370
    return NULL;
 
371
 
 
372
  major_version = g_key_file_get_integer (key_file,
 
373
                                          "meta", "major-version", NULL);
 
374
 
 
375
  if (major_version != PRINT_SETTINGS_MAJOR_VERSION)
 
376
    return NULL;
 
377
 
 
378
  minor_version = g_key_file_get_integer (key_file,
 
379
                                          "meta", "minor-version", NULL);
 
380
 
 
381
  if (minor_version != PRINT_SETTINGS_MINOR_VERSION)
 
382
    return NULL;
 
383
 
 
384
  return key_file;
 
385
}