print-settings.c 10.2 KB
Newer Older
1
/* GIMP - The GNU Image Manipulation Program
2 3
 * Copyright (C) 1995 Spencer Kimball and Peter Mattis
 *
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 3 of the License, or
7 8 9 10 11 12 13 14
 * (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
15
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
16 17 18 19 20 21 22 23 24
 */

#include "config.h"

#include <libgimp/gimp.h>
#include <libgimp/gimpui.h>

#include "print.h"
#include "print-settings.h"
25
#include "print-utils.h"
26

27

28 29 30
#define PRINT_SETTINGS_MAJOR_VERSION 0
#define PRINT_SETTINGS_MINOR_VERSION 4

31 32
#define PRINT_SETTINGS_NAME          "print-settings"

33

34 35
static GKeyFile * print_settings_key_file_from_settings      (PrintData         *data);

36
static void       print_settings_add_to_key_file             (const gchar       *key,
37 38 39 40 41 42 43
                                                              const gchar       *value,
                                                              gpointer           data);

static GKeyFile * print_settings_key_file_from_resource_file (void);

static GKeyFile * print_settings_key_file_from_parasite      (gint32             image_ID);

44
static gboolean   print_settings_load_from_key_file          (PrintData         *data,
45 46
                                                              GKeyFile          *key_file);

47
static gboolean   print_settings_check_version               (GKeyFile          *key_file);
48 49 50 51 52 53 54

/*
 * set GtkPrintSettings from the contents of a "print-settings"
 * image parasite, or, if none exists, from a resource
 * file of the same name
 */
gboolean
55
print_settings_load (PrintData *data)
56
{
57
  GKeyFile *key_file = print_settings_key_file_from_parasite (data->image_id);
58 59 60 61 62 63

  if (! key_file)
    key_file = print_settings_key_file_from_resource_file ();

  if (key_file)
    {
64
      print_settings_load_from_key_file (data, key_file);
65 66 67 68 69 70 71 72 73 74 75 76
      g_key_file_free (key_file);
      return TRUE;
    }

  return FALSE;
}

/*
 * save all settings as a resource file "print-settings"
 * and as an image parasite
 */
void
77
print_settings_save (PrintData *data)
78
{
79
  GKeyFile *key_file = print_settings_key_file_from_settings (data);
80

81
  /* image setup */
82
  if (gimp_image_is_valid (data->image_id))
83
    {
84 85 86 87 88
      gdouble xres;
      gdouble yres;

      gimp_image_get_resolution (data->image_id, &xres, &yres);

89 90
      g_key_file_set_integer (key_file, "image-setup",
                              "unit", data->unit);
91 92
      /* Do not save the print resolution when it is the expected image
       * resolution so that changing it (i.e. in "print size" dialog)
93
       * is not overridden by any previous prints.
94 95 96 97 98
       */
      if ((data->min_xres <= xres && ABS (xres - data->xres) > 0.1)          ||
          (data->min_yres <= yres && ABS (yres - data->yres) > 0.1)          ||
          (data->min_xres > xres && ABS (data->min_xres - data->xres) > 0.1) ||
          (data->min_yres > yres && ABS (data->min_yres - data->yres) > 0.1))
99 100 101 102 103 104
        {
          g_key_file_set_double  (key_file, "image-setup",
                                  "x-resolution", data->xres);
          g_key_file_set_double  (key_file, "image-setup",
                                  "y-resolution", data->yres);
        }
105 106 107 108 109 110 111 112
      g_key_file_set_double  (key_file, "image-setup",
                              "x-offset", data->offset_x);
      g_key_file_set_double  (key_file, "image-setup",
                              "y-offset", data->offset_y);
      g_key_file_set_integer (key_file, "image-setup",
                              "center-mode", data->center);
      g_key_file_set_boolean (key_file, "image-setup",
                              "use-full-page", data->use_full_page);
113 114
      g_key_file_set_boolean (key_file, "image-setup",
                              "crop-marks", data->draw_crop_marks);
115

116
      print_utils_key_file_save_as_parasite (key_file,
117 118
                                             data->image_id,
                                             PRINT_SETTINGS_NAME);
119
    }
120

121 122 123
  /* some settings shouldn't be made persistent on a global level,
   * so they are only stored in the image, not in the rcfile
   */
124 125 126 127 128 129

  g_key_file_remove_key (key_file, "image-setup", "x-resolution", NULL);
  g_key_file_remove_key (key_file, "image-setup", "y-resolution", NULL);
  g_key_file_remove_key (key_file, "image-setup", "x-offset", NULL);
  g_key_file_remove_key (key_file, "image-setup", "y-offset", NULL);

130 131 132 133
  g_key_file_remove_key (key_file, PRINT_SETTINGS_NAME, "n-copies", NULL);

  print_utils_key_file_save_as_rcfile (key_file, PRINT_SETTINGS_NAME);

134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
  g_key_file_free (key_file);
}

/*
 * serialize print settings into a GKeyFile
 */
static GKeyFile *
print_settings_key_file_from_settings (PrintData *data)
{
  GtkPrintOperation *operation = data->operation;
  GtkPrintSettings  *settings;
  GKeyFile          *key_file  = g_key_file_new ();

  /* put version information into the file */
  g_key_file_set_integer (key_file, "meta", "major-version",
                          PRINT_SETTINGS_MAJOR_VERSION);
  g_key_file_set_integer (key_file, "meta", "minor-version",
                          PRINT_SETTINGS_MINOR_VERSION);

  /* save the contents of the GtkPrintSettings for the operation */
  settings = gtk_print_operation_get_print_settings (operation);
155

156
  if (settings)
157 158
    gtk_print_settings_foreach (settings,
                                print_settings_add_to_key_file, key_file);
159 160 161 162 163 164 165 166

  return key_file;
}

/*
 * callback used in gtk_print_settings_foreach loop
 */
static void
167 168 169
print_settings_add_to_key_file (const gchar *key,
                                const gchar *value,
                                gpointer     data)
170 171 172
{
  GKeyFile *key_file = data;

173
  g_key_file_set_value (key_file, PRINT_SETTINGS_NAME, key, value);
174 175 176 177 178 179 180 181
}

/*
 * deserialize a "print-settings" resource file into a GKeyFile
 */
static GKeyFile *
print_settings_key_file_from_resource_file (void)
{
182 183 184
  GKeyFile *key_file;

  key_file = print_utils_key_file_load_from_rcfile (PRINT_SETTINGS_NAME);
185

186
  if (key_file && ! print_settings_check_version (key_file))
187 188 189 190 191 192
    {
      g_key_file_free (key_file);
      return NULL;
    }

  return key_file;
193 194 195 196 197 198 199 200 201
}

/* load information from an image parasite called "print-settings"
 * return a GKeyFile containing the information if a valid parasite is found,
 * NULL otherwise
 */
static GKeyFile *
print_settings_key_file_from_parasite (gint32 image_ID)
{
202
  GKeyFile *key_file;
203

204
  key_file = print_utils_key_file_load_from_parasite (image_ID,
205
                                                      PRINT_SETTINGS_NAME);
206

207
  if (key_file && ! print_settings_check_version (key_file))
208 209
    {
      g_key_file_free (key_file);
210
      return NULL;
211 212 213
    }

  return key_file;
214 215 216
}

static gboolean
217
print_settings_load_from_key_file (PrintData *data,
218
                                   GKeyFile  *key_file)
219 220
{
  GtkPrintOperation  *operation = data->operation;
221
  GtkPrintSettings   *settings;
222 223 224 225 226 227 228 229
  gchar             **keys;
  gsize               n_keys;
  gint                i;

  settings = gtk_print_operation_get_print_settings (operation);
  if (! settings)
    settings = gtk_print_settings_new ();

230
  keys = g_key_file_get_keys (key_file, PRINT_SETTINGS_NAME, &n_keys, NULL);
231 232 233 234 235 236

  if (! keys)
    return FALSE;

  for (i = 0; i < n_keys; i++)
    {
237
      gchar *value;
238

239 240
      value = g_key_file_get_value (key_file,
                                    PRINT_SETTINGS_NAME, keys[i], NULL);
241

242 243 244 245 246
      if (value)
        {
          gtk_print_settings_set (settings, keys[i], value);
          g_free (value);
        }
247
    }
248

249 250
  g_strfreev (keys);

251 252
  if (g_key_file_has_key (key_file, "image-setup", "unit", NULL))
    {
253 254
      data->unit = g_key_file_get_integer (key_file, "image-setup",
                                           "unit", NULL);
255
    }
256

257 258 259
  if (g_key_file_has_key (key_file, "image-setup", "x-resolution", NULL) &&
      g_key_file_has_key (key_file, "image-setup", "y-resolution", NULL))
    {
260 261 262 263
      data->xres = g_key_file_get_double (key_file, "image-setup",
                                          "x-resolution", NULL);
      data->yres = g_key_file_get_double (key_file, "image-setup",
                                          "y-resolution", NULL);
264 265
    }

266 267 268
  if (g_key_file_has_key (key_file, "image-setup", "x-offset", NULL) &&
      g_key_file_has_key (key_file, "image-setup", "y-offset", NULL))
    {
269 270 271 272
      data->offset_x = g_key_file_get_double (key_file, "image-setup",
                                              "x-offset", NULL);
      data->offset_y = g_key_file_get_double (key_file, "image-setup",
                                              "y-offset", NULL);
273 274
    }

275 276
  if (g_key_file_has_key (key_file, "image-setup", "center-mode", NULL))
    {
277 278
      data->center = g_key_file_get_integer (key_file, "image-setup",
                                             "center-mode", NULL);
279 280
    }

281 282
  if (g_key_file_has_key (key_file, "image-setup", "use-full-page", NULL))
    {
283 284 285 286 287 288 289 290
      data->use_full_page = g_key_file_get_boolean (key_file, "image-setup",
                                                    "use-full-page", NULL);
    }

  if (g_key_file_has_key (key_file, "image-setup", "crop-marks", NULL))
    {
      data->draw_crop_marks = g_key_file_get_boolean (key_file, "image-setup",
                                                      "crop-marks", NULL);
291 292
    }

293 294
  gtk_print_operation_set_print_settings (operation, settings);

295 296 297
  return TRUE;
}

298 299
static gboolean
print_settings_check_version (GKeyFile *key_file)
300
{
301 302
  gint  major_version;
  gint  minor_version;
303

304 305
  if (! g_key_file_has_group (key_file, "meta"))
    return FALSE;
306

307
  major_version = g_key_file_get_integer (key_file,
308
                                          "meta", "major-version", NULL);
309 310

  if (major_version != PRINT_SETTINGS_MAJOR_VERSION)
311
    return FALSE;
312

313
  minor_version = g_key_file_get_integer (key_file,
314
                                          "meta", "minor-version", NULL);
315 316

  if (minor_version != PRINT_SETTINGS_MINOR_VERSION)
317
    return FALSE;
318

319
  return TRUE;
320
}