]> www.fi.muni.cz Git - evince.git/blob - properties/ev-properties-view.c
Adds a paper size field to the properties dialog. Fixes bug #307315. Patch
[evince.git] / properties / ev-properties-view.c
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8; c-indent-level: 8 -*- */
2 /* this file is part of evince, a gnome document viewer
3  *
4  *  Copyright (C) 2005 Red Hat, Inc
5  *
6  * Evince is free software; you can redistribute it and/or modify it
7  * under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * Evince is distributed in the hope that it will be useful, but
12  * WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
19  */
20
21 #ifdef HAVE_CONFIG_H
22 #include "config.h"
23 #endif
24
25 #include "ev-properties-view.h"
26 #include "ev-document-fonts.h"
27
28 #include <glib/gi18n.h>
29 #include <gtk/gtk.h>
30 #include <glade/glade.h>
31 #include <time.h>
32 #include <sys/time.h>
33 #include <string.h>
34 #include <langinfo.h>
35
36 typedef enum
37 {
38         TITLE_PROPERTY,
39         SUBJECT_PROPERTY,
40         AUTHOR_PROPERTY,
41         KEYWORDS_PROPERTY,
42         PRODUCER_PROPERTY,
43         CREATOR_PROPERTY,
44         CREATION_DATE_PROPERTY,
45         MOD_DATE_PROPERTY,
46         N_PAGES_PROPERTY,
47         LINEARIZED_PROPERTY,
48         FORMAT_PROPERTY,
49         SECURITY_PROPERTY,
50         PAPER_SIZE_PROPERTY
51 } Property;
52
53 typedef struct
54 {
55         Property property;
56         const char *label_id;
57 } PropertyInfo;
58
59 static const PropertyInfo properties_info[] = {
60         { TITLE_PROPERTY, "title" },
61         { SUBJECT_PROPERTY, "subject" },
62         { AUTHOR_PROPERTY, "author" },
63         { KEYWORDS_PROPERTY, "keywords" },
64         { PRODUCER_PROPERTY, "producer" },
65         { CREATOR_PROPERTY, "creator" },
66         { CREATION_DATE_PROPERTY, "created" },
67         { MOD_DATE_PROPERTY, "modified" },
68         { N_PAGES_PROPERTY, "pages" },
69         { LINEARIZED_PROPERTY, "optimized" },
70         { FORMAT_PROPERTY, "version" },
71         { SECURITY_PROPERTY, "security" },
72         { PAPER_SIZE_PROPERTY, "papersize" }
73 };
74
75 struct _EvPropertiesView {
76         GtkVBox base_instance;
77
78         GladeXML *xml;
79 };
80
81 struct _EvPropertiesViewClass {
82         GtkVBoxClass base_class;
83 };
84
85 G_DEFINE_TYPE (EvPropertiesView, ev_properties_view, GTK_TYPE_VBOX)
86
87 static void
88 ev_properties_view_dispose (GObject *object)
89 {
90         EvPropertiesView *properties = EV_PROPERTIES_VIEW (object);
91
92         if (properties->xml) {
93                 g_object_unref (properties->xml);
94                 properties->xml = NULL;
95         }
96 }
97
98 static void
99 ev_properties_view_class_init (EvPropertiesViewClass *properties_class)
100 {
101         GObjectClass *g_object_class = G_OBJECT_CLASS (properties_class);
102
103         g_object_class->dispose = ev_properties_view_dispose;
104 }
105
106 /* Returns a locale specific date and time representation */
107 static char *
108 ev_properties_view_format_date (GTime utime)
109 {
110         time_t time = (time_t) utime;
111         struct tm t;
112         char s[256];
113         const char *fmt_hack = "%c";
114         size_t len;
115
116         if (time == 0 || !localtime_r (&time, &t)) return NULL;
117
118         len = strftime (s, sizeof (s), fmt_hack, &t);
119         if (len == 0 || s[0] == '\0') return NULL;
120
121         return g_locale_to_utf8 (s, -1, NULL, NULL, NULL);
122 }
123
124 /* This is cut out of gconvert.c from glib (and mildly modified).  Not all
125    backends give valid UTF-8 for properties, so we make sure that is.
126  */
127 static gchar *
128 make_valid_utf8 (const gchar *name)
129 {
130   GString *string;
131   const gchar *remainder, *invalid;
132   gint remaining_bytes, valid_bytes;
133   
134   string = NULL;
135   remainder = name;
136   remaining_bytes = strlen (name);
137   
138   while (remaining_bytes != 0) 
139     {
140       if (g_utf8_validate (remainder, remaining_bytes, &invalid)) 
141         break;
142       valid_bytes = invalid - remainder;
143     
144       if (string == NULL) 
145         string = g_string_sized_new (remaining_bytes);
146
147       g_string_append_len (string, remainder, valid_bytes);
148       g_string_append_c (string, '?');
149       
150       remaining_bytes -= valid_bytes + 1;
151       remainder = invalid + 1;
152     }
153   
154   if (string == NULL)
155     return g_strdup (name);
156   
157   g_string_append (string, remainder);
158
159   g_assert (g_utf8_validate (string->str, -1, NULL));
160   
161   return g_string_free (string, FALSE);
162 }
163
164 static void
165 set_property (GladeXML *xml, Property property, const char *text)
166 {
167         GtkWidget *widget;
168         char *valid_text;
169
170         widget = glade_xml_get_widget (xml, properties_info[property].label_id);
171         g_return_if_fail (GTK_IS_LABEL (widget));
172
173         if (text == NULL || text[0] == '\000') {
174                 gchar *markup;
175
176                 markup = g_markup_printf_escaped ("<i>%s</i>", _("None"));
177                 gtk_label_set_markup (GTK_LABEL (widget), markup);
178                 g_free (markup);
179
180                 return;
181         }
182         text = text ? text : "";
183
184         valid_text = make_valid_utf8 (text);
185
186         gtk_label_set_text (GTK_LABEL (widget), valid_text);
187
188         g_free (valid_text);
189 }
190
191 /*
192  * All values are in mm. 
193  * Source: http://en.wikipedia.org/wiki/Paper_size
194  */
195 struct regular_paper_size {
196         double width;
197         double height;
198         double width_tolerance;
199         double height_tolerance;
200         const char *description;
201 } const regular_paper_sizes[] = {
202         // ISO 216 paper sizes
203         {  841.0f, 1189.0f, 3.0f, 3.0f, "A0"  },
204         {  594.0f,  841.0f, 2.0f, 3.0f, "A1"  },
205         {  420.0f,  594.0f, 2.0f, 2.0f, "A2"  },
206         {  297.0f,  420.0f, 2.0f, 2.0f, "A3"  },
207         {  210.0f,  297.0f, 2.0f, 2.0f, "A4"  },
208         {  148.0f,  210.0f, 1.5f, 2.0f, "A5"  },
209         {  105.0f,  148.0f, 1.5f, 1.5f, "A6"  },
210         {   74.0f,  105.0f, 1.5f, 1.5f, "A7"  },
211         {   52.0f,   74.0f, 1.5f, 1.5f, "A8"  },
212         {   37.0f,   52.0f, 1.5f, 1.5f, "A9"  },
213         {   26.0f,   37.0f, 1.5f, 1.5f, "A10" },
214         { 1000.0f, 1414.0f, 3.0f, 3.0f, "B0"  },
215         {  707.0f, 1000.0f, 3.0f, 3.0f, "B1"  },
216         {  500.0f,  707.0f, 2.0f, 3.0f, "B2"  },
217         {  353.0f,  500.0f, 2.0f, 2.0f, "B3"  },
218         {  250.0f,  353.0f, 2.0f, 2.0f, "B4"  },
219         {  176.0f,  250.0f, 2.0f, 2.0f, "B5"  },
220         {  125.0f,  176.0f, 1.5f, 2.0f, "B6"  },
221         {   88.0f,  125.0f, 1.5f, 1.5f, "B7"  },
222         {   62.0f,   88.0f, 1.5f, 1.5f, "B8"  },
223         {   44.0f,   62.0f, 1.5f, 1.5f, "B9"  },
224         {   31.0f,   44.0f, 1.5f, 1.5f, "B10" },
225         {  917.0f, 1297.0f, 3.0f, 3.0f, "C0"  },
226         {  648.0f,  917.0f, 3.0f, 3.0f, "C1"  },
227         {  458.0f,  648.0f, 2.0f, 3.0f, "C2"  },
228         {  324.0f,  458.0f, 2.0f, 2.0f, "C3"  },
229         {  229.0f,  324.0f, 2.0f, 2.0f, "C4"  },
230         {  162.0f,  229.0f, 2.0f, 2.0f, "C5"  },
231         {  114.0f,  162.0f, 1.5f, 2.0f, "C6"  },
232         {   81.0f,  114.0f, 1.5f, 1.5f, "C7"  },
233         {   57.0f,   81.0f, 1.5f, 1.5f, "C8"  },
234         {   40.0f,   57.0f, 1.5f, 1.5f, "C9"  },
235         {   28.0f,   40.0f, 1.5f, 1.5f, "C10" },
236
237         // US paper sizes
238         {  279.0f,  216.0f, 3.0f, 3.0f, "Letter" },
239         {  356.0f,  216.0f, 3.0f, 3.0f, "Legal"  },
240         {  432.0f,  279.0f, 3.0f, 3.0f, "Ledger" }
241 };
242
243 static char *
244 ev_regular_paper_size (const EvDocumentInfo *info)
245 {
246         const struct regular_paper_size *size;   
247         char *exact_size = NULL;
248         char *imperial = NULL;
249         char *str = NULL;
250         int i;
251         
252         imperial = nl_langinfo(_NL_MEASUREMENT_MEASUREMENT);
253         if ( imperial && imperial[0] == 2 )
254                 /* Imperial measurement (inches) */
255                 exact_size = g_strdup_printf( _("%.2f x %.2f in"),
256                                               info->paper_width  / 25.4f,
257                                               info->paper_height / 25.4f );
258         else
259                 /* Metric measurement (millimeters) */
260                 exact_size = g_strdup_printf( _("%.0f x %.0f mm"),
261                                               info->paper_width,
262                                               info->paper_height );
263         
264         for (i = G_N_ELEMENTS ( regular_paper_sizes ) - 1; i >= 0; i--) {
265                 size = &regular_paper_sizes[i];
266
267                 if ( ABS( info->paper_height - size->height ) <= size->height_tolerance &&
268                      ABS( info->paper_width  - size->width  ) <= size->width_tolerance ) {
269                         /* Note to translators: first placeholder is the paper name (eg.
270                          * A4), second placeholder is the paper size (eg. 297x210 mm) */
271                         str = g_strdup_printf ( _("%s, Portrait (%s)"),
272                                                 size->description,
273                                                 exact_size );
274                 } else if ( ABS( info->paper_width  - size->height ) <= size->height_tolerance &&
275                             ABS( info->paper_height - size->width  ) <= size->width_tolerance ) {
276                         /* Note to translators: first placeholder is the paper name (eg.
277                          * A4), second placeholder is the paper size (eg. 297x210 mm) */
278                         str = g_strdup_printf ( _("%s, Landscape (%s)"),
279                                                 size->description,
280                                                 exact_size );
281                 }
282         }
283
284         if (str != NULL) {
285                 g_free (exact_size);
286                 return str;
287         } else
288                 return exact_size;
289 }
290
291 void
292 ev_properties_view_set_info (EvPropertiesView *properties, const EvDocumentInfo *info)
293 {
294         GladeXML *xml = properties->xml;
295         char *text;
296
297         if (info->fields_mask & EV_DOCUMENT_INFO_TITLE) {
298                 set_property (xml, TITLE_PROPERTY, info->title);
299         }
300         if (info->fields_mask & EV_DOCUMENT_INFO_SUBJECT) {
301                 set_property (xml, SUBJECT_PROPERTY, info->subject);
302         }
303         if (info->fields_mask & EV_DOCUMENT_INFO_AUTHOR) {
304                 set_property (xml, AUTHOR_PROPERTY, info->author);
305         }
306         if (info->fields_mask & EV_DOCUMENT_INFO_KEYWORDS) {
307                 set_property (xml, KEYWORDS_PROPERTY, info->keywords);
308         }
309         if (info->fields_mask & EV_DOCUMENT_INFO_PRODUCER) {
310                 set_property (xml, PRODUCER_PROPERTY, info->producer);
311         }
312         if (info->fields_mask & EV_DOCUMENT_INFO_CREATOR) {
313                 set_property (xml, CREATOR_PROPERTY, info->creator);
314         }
315         if (info->fields_mask & EV_DOCUMENT_INFO_CREATION_DATE) {
316                 text = ev_properties_view_format_date (info->creation_date);
317                 set_property (xml, CREATION_DATE_PROPERTY, text);
318                 g_free (text);
319         }
320         if (info->fields_mask & EV_DOCUMENT_INFO_MOD_DATE) {
321                 text = ev_properties_view_format_date (info->modified_date);
322                 set_property (xml, MOD_DATE_PROPERTY, text);
323                 g_free (text);
324         }
325         if (info->fields_mask & EV_DOCUMENT_INFO_FORMAT) {
326                 text = g_strdup_printf ("%s", info->format);
327                 set_property (xml, FORMAT_PROPERTY, text);
328                 g_free (text);
329         }
330         if (info->fields_mask & EV_DOCUMENT_INFO_N_PAGES) {
331                 text = g_strdup_printf ("%d", info->n_pages);
332                 set_property (xml, N_PAGES_PROPERTY, text);
333                 g_free (text);
334         }
335         if (info->fields_mask & EV_DOCUMENT_INFO_LINEARIZED) {
336                 set_property (xml, LINEARIZED_PROPERTY, info->linearized);
337         }
338         if (info->fields_mask & EV_DOCUMENT_INFO_SECURITY) {
339                 set_property (xml, SECURITY_PROPERTY, info->security);
340         }
341         if (info->fields_mask & EV_DOCUMENT_INFO_PAPER_SIZE) {
342                 text = ev_regular_paper_size (info);
343                 set_property (xml, PAPER_SIZE_PROPERTY, text);
344                 g_free (text);
345         }
346 }
347
348 static void
349 ev_properties_view_init (EvPropertiesView *properties)
350 {
351         GladeXML *xml;
352
353         /* Create a new GladeXML object from XML file glade_file */
354         xml = glade_xml_new (DATADIR "/evince-properties.glade", "general_page_root", GETTEXT_PACKAGE);
355         properties->xml = xml;
356         g_assert (xml != NULL);
357
358         gtk_box_pack_start (GTK_BOX (properties),
359                             glade_xml_get_widget (xml, "general_page_root"),
360                             TRUE, TRUE, 0);
361 }
362
363 void
364 ev_properties_view_register_type (GTypeModule *module)
365 {
366         ev_properties_view_get_type ();
367 }
368
369 GtkWidget *
370 ev_properties_view_new (void)
371 {
372         EvPropertiesView *properties;
373
374         properties = g_object_new (EV_TYPE_PROPERTIES, NULL);
375
376         return GTK_WIDGET (properties);
377 }