~ubuntu-branches/ubuntu/saucy/epiphany-browser/saucy

« back to all changes in this revision

Viewing changes to lib/ephy-print-utils.c

  • Committer: Package Import Robot
  • Author(s): Jeremy Bicha
  • Date: 2012-10-02 20:30:38 UTC
  • mfrom: (1.6.14)
  • Revision ID: package-import@ubuntu.com-20121002203038-ijw6x1x3vgv6tyi5
Tags: 3.6.0-0ubuntu1
* New upstream release (LP: #1033909)
  - New overview with most visited sites as start page (LP: #343397)
* Rename gir1.2-epiphany-3.4 -> gir1.2-epiphany-3.6
* debian/control.in:
  - Bump minimum webkit and libsoup
  - Drop build-depends on gnome-doc-utils and seed
  - Build-depend on gcr and gnome-desktop3
* debian/rules:
  - Copy Debian fix to rename epiphany.desktop to
    epiphany-browser.desktop so that the shell recognizes it.
* debian/patches/00_epiphany-browser.patch: Refreshed
* debian/patches/14_pkglibdir.patch:
  - Dropped, applied in new version

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 *  Copyright © 2006 Christian Persch
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, or (at your option)
7
 
 *  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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17
 
 *
18
 
 */
19
 
 
20
 
#include "config.h"
21
 
 
22
 
#include <string.h>
23
 
 
24
 
#include <glib.h>
25
 
#include <gtk/gtk.h>
26
 
 
27
 
#include "ephy-debug.h"
28
 
#include "ephy-string.h"
29
 
 
30
 
#include "ephy-print-utils.h"
31
 
 
32
 
#define PRINT_SETTINGS_GROUP    "Print Settings"
33
 
#define PAGE_SETUP_GROUP        "Page Setup"
34
 
#define PAPER_SIZE_GROUP        "Paper Size"
35
 
 
36
 
#define ERROR_QUARK             (g_quark_from_static_string ("ephy-print-utils-error"))
37
 
 
38
 
/**
39
 
 * ephy_print_utils_page_setup_new_from_file:
40
 
 * @file_name: the filename to read the page_setup from
41
 
 * @error:
42
 
 * 
43
 
 * Reads the print page_setup from @filename. Returns a new #GtkPageSetup
44
 
 * object with the restored page_setup, or %NULL if an error occurred.
45
 
 *
46
 
 * Return value: the restored #GtkPageSetup
47
 
 * 
48
 
 * Since: 2.10
49
 
 */
50
 
GtkPageSetup *
51
 
ephy_print_utils_page_setup_new_from_file (const gchar *file_name,
52
 
                                           GError     **error)
53
 
{
54
 
  GtkPageSetup *page_setup;
55
 
  GKeyFile *key_file;
56
 
 
57
 
  g_return_val_if_fail (file_name != NULL, NULL);
58
 
 
59
 
  key_file = g_key_file_new ();
60
 
  if (!g_key_file_load_from_file (key_file, file_name, 0, error))
61
 
    {
62
 
      g_key_file_free (key_file);
63
 
      return NULL;
64
 
    }
65
 
 
66
 
  page_setup = ephy_print_utils_page_setup_new_from_key_file (key_file, error);
67
 
  g_key_file_free (key_file);
68
 
 
69
 
  return page_setup;
70
 
}
71
 
 
72
 
/**
73
 
 * ephy_print_utils_page_setup_new_from_key_file:
74
 
 * @key_file: the #GKeyFile to retrieve the page_setup from
75
 
 * @error:
76
 
 * 
77
 
 * Reads the print page_setup from @key_file. Returns a new #GtkPageSetup
78
 
 * object with the restored page_setup, or %NULL if an error occurred.
79
 
 *
80
 
 * Return value: the restored #GtkPageSetup
81
 
 * 
82
 
 * Since: 2.10
83
 
 */
84
 
GtkPageSetup *
85
 
ephy_print_utils_page_setup_new_from_key_file (GKeyFile *key_file,
86
 
                                               GError  **error)
87
 
{
88
 
  GtkPageSetup *page_setup = NULL;
89
 
  GtkPaperSize *paper_size = NULL;
90
 
  gdouble width, height, top, bottom, left, right;
91
 
  char *name = NULL, *ppd_name = NULL, *display_name = NULL, *orientation = NULL;
92
 
  GError *err = NULL;
93
 
 
94
 
  g_return_val_if_fail (key_file != NULL, NULL);
95
 
 
96
 
  if (!g_key_file_has_group (key_file, PAGE_SETUP_GROUP) ||
97
 
      !g_key_file_has_group (key_file, PAPER_SIZE_GROUP)) {
98
 
    g_set_error (error, ERROR_QUARK, 0, "Not a valid epiphany page setup file");
99
 
    goto out;
100
 
  }
101
 
 
102
 
#define GET_DOUBLE(kf, group, name, v) \
103
 
v = g_key_file_get_double (kf, group, name, &err); \
104
 
if (err != NULL) {\
105
 
  g_propagate_error (error, err);\
106
 
  goto out;\
107
 
}
108
 
 
109
 
  GET_DOUBLE (key_file, PAPER_SIZE_GROUP, "Width", width);
110
 
  GET_DOUBLE (key_file, PAPER_SIZE_GROUP, "Height", height);
111
 
  GET_DOUBLE (key_file, PAGE_SETUP_GROUP, "MarginTop", top);
112
 
  GET_DOUBLE (key_file, PAGE_SETUP_GROUP, "MarginBottom", bottom);
113
 
  GET_DOUBLE (key_file, PAGE_SETUP_GROUP, "MarginLeft", left);
114
 
  GET_DOUBLE (key_file, PAGE_SETUP_GROUP, "MarginRight", right);
115
 
 
116
 
#undef GET_DOUBLE
117
 
 
118
 
  name = g_key_file_get_string (key_file, PAPER_SIZE_GROUP,
119
 
                                "Name", NULL);
120
 
  ppd_name = g_key_file_get_string (key_file, PAPER_SIZE_GROUP,
121
 
                                    "PPDName", NULL);
122
 
  display_name = g_key_file_get_string (key_file, PAPER_SIZE_GROUP,
123
 
                                        "DisplayName", NULL);
124
 
  orientation = g_key_file_get_string (key_file, PAGE_SETUP_GROUP,
125
 
                                       "Orientation", NULL);
126
 
 
127
 
  if ((ppd_name == NULL && name == NULL) || orientation == NULL)
128
 
    {
129
 
      g_set_error (error, ERROR_QUARK, 0, "Not a valid epiphany page setup file");
130
 
      goto out;
131
 
    }
132
 
 
133
 
  if (ppd_name != NULL) {
134
 
    paper_size = gtk_paper_size_new_from_ppd (ppd_name, display_name,
135
 
                                              width, height);
136
 
  } else {
137
 
    paper_size = gtk_paper_size_new_custom (name, display_name,
138
 
                                            width, height, GTK_UNIT_MM);
139
 
  }
140
 
  g_assert (paper_size != NULL);
141
 
 
142
 
  page_setup = gtk_page_setup_new ();
143
 
  gtk_page_setup_set_paper_size (page_setup, paper_size);
144
 
  gtk_paper_size_free (paper_size);
145
 
 
146
 
  gtk_page_setup_set_top_margin (page_setup, top, GTK_UNIT_MM);
147
 
  gtk_page_setup_set_bottom_margin (page_setup, bottom, GTK_UNIT_MM);
148
 
  gtk_page_setup_set_left_margin (page_setup, left, GTK_UNIT_MM);
149
 
  gtk_page_setup_set_right_margin (page_setup, right, GTK_UNIT_MM);
150
 
 
151
 
  gtk_page_setup_set_orientation (page_setup,
152
 
                                  ephy_string_enum_from_string (GTK_TYPE_PAGE_ORIENTATION,
153
 
                                                                orientation));
154
 
out:
155
 
  g_free (ppd_name);
156
 
  g_free (name);
157
 
  g_free (display_name);
158
 
  g_free (orientation);
159
 
 
160
 
  return page_setup;
161
 
}
162
 
 
163
 
/**
164
 
 * ephy_print_utils_page_setup_to_file:
165
 
 * @page_setup: a #GtkPageSetup
166
 
 * @file_name: the file to save to
167
 
 * @error:
168
 
 * 
169
 
 * This function saves the print page_setup from @page_setup to @file_name.
170
 
 * 
171
 
 * Return value: %TRUE on success
172
 
 *
173
 
 * Since: 2.10
174
 
 */
175
 
gboolean
176
 
ephy_print_utils_page_setup_to_file (GtkPageSetup     *page_setup,
177
 
                                     const char           *file_name,
178
 
                                     GError              **error)
179
 
{
180
 
  GKeyFile *keyfile;
181
 
  gboolean retval;
182
 
  char *data = NULL;
183
 
  gsize len;
184
 
 
185
 
  g_return_val_if_fail (GTK_IS_PAGE_SETUP (page_setup), FALSE);
186
 
  g_return_val_if_fail (file_name != NULL, FALSE);
187
 
 
188
 
  keyfile = g_key_file_new ();
189
 
  retval = ephy_print_utils_page_setup_to_key_file (page_setup, keyfile, error);
190
 
  if (!retval) goto out;
191
 
 
192
 
  data = g_key_file_to_data (keyfile, &len, error);
193
 
  if (!data) goto out;
194
 
 
195
 
  retval = g_file_set_contents (file_name, data, len, error);
196
 
 
197
 
out:
198
 
  g_key_file_free (keyfile);
199
 
  g_free (data);
200
 
 
201
 
  return retval;
202
 
}
203
 
 
204
 
/**
205
 
 * ephy_print_utils_page_setup_to_key_file:
206
 
 * @page_setup: a #GtkPageSetup
207
 
 * @key_file: the #GKeyFile to save the print page_setup to
208
 
 * @error:
209
 
 * 
210
 
 * This function adds the print page_setup from @page_setup to @key_file.
211
 
 * 
212
 
 * Return value: %TRUE on success
213
 
 *
214
 
 * Since: 2.10
215
 
 */
216
 
gboolean
217
 
ephy_print_utils_page_setup_to_key_file (GtkPageSetup  *page_setup,
218
 
                                         GKeyFile          *key_file,
219
 
                                         GError           **error)
220
 
{
221
 
  GtkPaperSize *paper_size;
222
 
  const char *name, *ppd_name, *display_name;
223
 
  char *orientation;
224
 
 
225
 
  g_return_val_if_fail (GTK_IS_PAGE_SETUP (page_setup), FALSE);
226
 
  g_return_val_if_fail (key_file != NULL, FALSE);
227
 
 
228
 
  paper_size = gtk_page_setup_get_paper_size (page_setup);
229
 
  g_assert (paper_size != NULL);
230
 
 
231
 
  name = gtk_paper_size_get_name (paper_size);
232
 
  display_name = gtk_paper_size_get_display_name (paper_size);
233
 
  ppd_name = gtk_paper_size_get_ppd_name (paper_size);
234
 
 
235
 
  if (ppd_name != NULL) {
236
 
    g_key_file_set_string (key_file, PAPER_SIZE_GROUP,
237
 
                           "PPDName", ppd_name);
238
 
  } else {
239
 
    g_key_file_set_string (key_file, PAPER_SIZE_GROUP,
240
 
                           "Name", name);
241
 
  }
242
 
 
243
 
  if (display_name) {
244
 
    g_key_file_set_string (key_file, PAPER_SIZE_GROUP,
245
 
                           "DisplayName", display_name);
246
 
  }
247
 
 
248
 
  g_key_file_set_double (key_file, PAPER_SIZE_GROUP,
249
 
                         "Width", gtk_paper_size_get_width (paper_size, GTK_UNIT_MM));
250
 
  g_key_file_set_double (key_file, PAPER_SIZE_GROUP,
251
 
                         "Height", gtk_paper_size_get_height (paper_size, GTK_UNIT_MM));
252
 
 
253
 
  g_key_file_set_double (key_file, PAGE_SETUP_GROUP,
254
 
                         "MarginTop", gtk_page_setup_get_top_margin (page_setup, GTK_UNIT_MM));
255
 
  g_key_file_set_double (key_file, PAGE_SETUP_GROUP,
256
 
                         "MarginBottom", gtk_page_setup_get_bottom_margin (page_setup, GTK_UNIT_MM));
257
 
  g_key_file_set_double (key_file, PAGE_SETUP_GROUP,
258
 
                         "MarginLeft", gtk_page_setup_get_left_margin (page_setup, GTK_UNIT_MM));
259
 
  g_key_file_set_double (key_file, PAGE_SETUP_GROUP,
260
 
                         "MarginRight", gtk_page_setup_get_right_margin (page_setup, GTK_UNIT_MM));
261
 
 
262
 
  orientation = ephy_string_enum_to_string (GTK_TYPE_PAGE_ORIENTATION,
263
 
                                            gtk_page_setup_get_orientation (page_setup));
264
 
  g_key_file_set_string (key_file, PAGE_SETUP_GROUP,
265
 
                         "Orientation", orientation);
266
 
  g_free (orientation);
267
 
 
268
 
  return TRUE;
269
 
}