]> www.fi.muni.cz Git - evince.git/blob - properties/ev-properties-view.c
Use gtk+ builtin paper list to identify the document's paper size. Fixes
[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
35 #ifdef HAVE__NL_MEASUREMENT_MEASUREMENT
36 #include <langinfo.h>
37 #endif
38
39 typedef enum
40 {
41         TITLE_PROPERTY,
42         SUBJECT_PROPERTY,
43         AUTHOR_PROPERTY,
44         KEYWORDS_PROPERTY,
45         PRODUCER_PROPERTY,
46         CREATOR_PROPERTY,
47         CREATION_DATE_PROPERTY,
48         MOD_DATE_PROPERTY,
49         N_PAGES_PROPERTY,
50         LINEARIZED_PROPERTY,
51         FORMAT_PROPERTY,
52         SECURITY_PROPERTY,
53         PAPER_SIZE_PROPERTY
54 } Property;
55
56 typedef struct
57 {
58         Property property;
59         const char *label_id;
60 } PropertyInfo;
61
62 static const PropertyInfo properties_info[] = {
63         { TITLE_PROPERTY, "title" },
64         { SUBJECT_PROPERTY, "subject" },
65         { AUTHOR_PROPERTY, "author" },
66         { KEYWORDS_PROPERTY, "keywords" },
67         { PRODUCER_PROPERTY, "producer" },
68         { CREATOR_PROPERTY, "creator" },
69         { CREATION_DATE_PROPERTY, "created" },
70         { MOD_DATE_PROPERTY, "modified" },
71         { N_PAGES_PROPERTY, "pages" },
72         { LINEARIZED_PROPERTY, "optimized" },
73         { FORMAT_PROPERTY, "version" },
74         { SECURITY_PROPERTY, "security" },
75         { PAPER_SIZE_PROPERTY, "papersize" }
76 };
77
78 struct _EvPropertiesView {
79         GtkVBox base_instance;
80
81         GladeXML *xml;
82 };
83
84 struct _EvPropertiesViewClass {
85         GtkVBoxClass base_class;
86 };
87
88 G_DEFINE_TYPE (EvPropertiesView, ev_properties_view, GTK_TYPE_VBOX)
89
90 static void
91 ev_properties_view_dispose (GObject *object)
92 {
93         EvPropertiesView *properties = EV_PROPERTIES_VIEW (object);
94
95         if (properties->xml) {
96                 g_object_unref (properties->xml);
97                 properties->xml = NULL;
98         }
99
100         G_OBJECT_CLASS (ev_properties_view_parent_class)->dispose (object);
101 }
102
103 static void
104 ev_properties_view_class_init (EvPropertiesViewClass *properties_class)
105 {
106         GObjectClass *g_object_class = G_OBJECT_CLASS (properties_class);
107
108         g_object_class->dispose = ev_properties_view_dispose;
109 }
110
111 /* Returns a locale specific date and time representation */
112 static char *
113 ev_properties_view_format_date (GTime utime)
114 {
115         time_t time = (time_t) utime;
116         struct tm t;
117         char s[256];
118         const char *fmt_hack = "%c";
119         size_t len;
120
121         if (time == 0 || !localtime_r (&time, &t)) return NULL;
122
123         len = strftime (s, sizeof (s), fmt_hack, &t);
124         if (len == 0 || s[0] == '\0') return NULL;
125
126         return g_locale_to_utf8 (s, -1, NULL, NULL, NULL);
127 }
128
129 /* This is cut out of gconvert.c from glib (and mildly modified).  Not all
130    backends give valid UTF-8 for properties, so we make sure that is.
131  */
132 static gchar *
133 make_valid_utf8 (const gchar *name)
134 {
135   GString *string;
136   const gchar *remainder, *invalid;
137   gint remaining_bytes, valid_bytes;
138   
139   string = NULL;
140   remainder = name;
141   remaining_bytes = strlen (name);
142   
143   while (remaining_bytes != 0) 
144     {
145       if (g_utf8_validate (remainder, remaining_bytes, &invalid)) 
146         break;
147       valid_bytes = invalid - remainder;
148     
149       if (string == NULL) 
150         string = g_string_sized_new (remaining_bytes);
151
152       g_string_append_len (string, remainder, valid_bytes);
153       g_string_append_c (string, '?');
154       
155       remaining_bytes -= valid_bytes + 1;
156       remainder = invalid + 1;
157     }
158   
159   if (string == NULL)
160     return g_strdup (name);
161   
162   g_string_append (string, remainder);
163
164   g_assert (g_utf8_validate (string->str, -1, NULL));
165   
166   return g_string_free (string, FALSE);
167 }
168
169 static void
170 set_property (GladeXML *xml, Property property, const char *text)
171 {
172         GtkWidget *widget;
173         char *valid_text;
174
175         widget = glade_xml_get_widget (xml, properties_info[property].label_id);
176         g_return_if_fail (GTK_IS_LABEL (widget));
177
178         if (text == NULL || text[0] == '\000') {
179                 gchar *markup;
180
181                 markup = g_markup_printf_escaped ("<i>%s</i>", _("None"));
182                 gtk_label_set_markup (GTK_LABEL (widget), markup);
183                 g_free (markup);
184
185                 return;
186         }
187         text = text ? text : "";
188
189         valid_text = make_valid_utf8 (text);
190
191         gtk_label_set_text (GTK_LABEL (widget), valid_text);
192
193         g_free (valid_text);
194 }
195
196 #if HAVE_GTK_PAPER_SIZE_GET_PAPER_SIZES
197 static GtkUnit
198 get_default_user_units (void)
199 {
200         /* Translate to the default units to use for presenting
201          * lengths to the user. Translate to default:inch if you
202          * want inches, otherwise translate to default:mm.
203          * Do *not* translate it to "predefinito:mm", if it
204          * it isn't default:mm or default:inch it will not work
205          */
206         gchar *e = _("default:mm");
207
208 #ifdef HAVE__NL_MEASUREMENT_MEASUREMENT
209         gchar *imperial = NULL;
210         
211         imperial = nl_langinfo (_NL_MEASUREMENT_MEASUREMENT);
212         if (imperial && imperial[0] == 2)
213                 return GTK_UNIT_INCH;  /* imperial */
214         if (imperial && imperial[0] == 1)
215                 return GTK_UNIT_MM;  /* metric */
216 #endif
217
218         if (strcmp (e, "default:inch") == 0)
219                 return GTK_UNIT_INCH;
220         else if (strcmp (e, "default:mm") == 0)
221                 g_warning ("Whoever translated default:mm did so wrongly.\n");
222                                 
223         return GTK_UNIT_MM;
224 }
225
226 static gdouble
227 get_tolerance (gdouble size)
228 {
229         if (size < 150.0f)
230                 return 1.5f;
231         else if (size >= 150.0f && size <= 600.0f)
232                 return 2.0f;
233         else
234                 return 3.0f;
235 }
236
237 static char *
238 ev_regular_paper_size (const EvDocumentInfo *info)
239 {
240         GList *paper_sizes, *l;
241         gchar *exact_size;
242         gchar *str = NULL;
243         GtkUnit units;
244
245         units = get_default_user_units ();
246
247         if (units == GTK_UNIT_MM) {
248                 exact_size = g_strdup_printf(_("%.0f x %.0f mm"),
249                                              info->paper_width,
250                                              info->paper_height);
251         } else {
252                 exact_size = g_strdup_printf (_("%.2f x %.2f inch"),
253                                               info->paper_width  / 25.4f,
254                                               info->paper_height / 25.4f);
255         }
256
257         paper_sizes = gtk_paper_size_get_paper_sizes (FALSE);
258         
259         for (l = paper_sizes; l && l->data; l = g_list_next (l)) {
260                 GtkPaperSize *size = (GtkPaperSize *) l->data;
261                 gdouble paper_width;
262                 gdouble paper_height;
263                 gdouble width_tolerance;
264                 gdouble height_tolerance;
265
266                 paper_width = gtk_paper_size_get_width (size, GTK_UNIT_MM);
267                 paper_height = gtk_paper_size_get_height (size, GTK_UNIT_MM);
268
269                 width_tolerance = get_tolerance (paper_width);
270                 height_tolerance = get_tolerance (paper_height);
271
272                 if (ABS (info->paper_height - paper_height) <= height_tolerance &&
273                     ABS (info->paper_width  - paper_width) <= width_tolerance) {
274                         /* Note to translators: first placeholder is the paper name (eg.
275                          * A4), second placeholder is the paper size (eg. 297x210 mm) */
276                         str = g_strdup_printf (_("%s, Portrait (%s)"),
277                                                gtk_paper_size_get_display_name (size),
278                                                exact_size);
279                 } else if (ABS (info->paper_width  - paper_height) <= height_tolerance &&
280                            ABS (info->paper_height - paper_width) <= width_tolerance) {
281                         /* Note to translators: first placeholder is the paper name (eg.
282                          * A4), second placeholder is the paper size (eg. 297x210 mm) */
283                         str = g_strdup_printf ( _("%s, Landscape (%s)"),
284                                                 gtk_paper_size_get_display_name (size),
285                                                 exact_size);
286                 }
287         }
288
289         g_list_foreach (paper_sizes, (GFunc) gtk_paper_size_free, NULL);
290         g_list_free (paper_sizes);
291
292         if (str != NULL) {
293                 g_free (exact_size);
294                 return str;
295         }
296         
297         return exact_size;
298 }
299 #else
300 /*
301  * All values are in mm. 
302  * Source: http://en.wikipedia.org/wiki/Paper_size
303  */
304 struct regular_paper_size {
305         double width;
306         double height;
307         double width_tolerance;
308         double height_tolerance;
309         const char *description;
310 } const regular_paper_sizes[] = {
311         // ISO 216 paper sizes
312         {  841.0f, 1189.0f, 3.0f, 3.0f, "A0"  },
313         {  594.0f,  841.0f, 2.0f, 3.0f, "A1"  },
314         {  420.0f,  594.0f, 2.0f, 2.0f, "A2"  },
315         {  297.0f,  420.0f, 2.0f, 2.0f, "A3"  },
316         {  210.0f,  297.0f, 2.0f, 2.0f, "A4"  },
317         {  148.0f,  210.0f, 1.5f, 2.0f, "A5"  },
318         {  105.0f,  148.0f, 1.5f, 1.5f, "A6"  },
319         {   74.0f,  105.0f, 1.5f, 1.5f, "A7"  },
320         {   52.0f,   74.0f, 1.5f, 1.5f, "A8"  },
321         {   37.0f,   52.0f, 1.5f, 1.5f, "A9"  },
322         {   26.0f,   37.0f, 1.5f, 1.5f, "A10" },
323         { 1000.0f, 1414.0f, 3.0f, 3.0f, "B0"  },
324         {  707.0f, 1000.0f, 3.0f, 3.0f, "B1"  },
325         {  500.0f,  707.0f, 2.0f, 3.0f, "B2"  },
326         {  353.0f,  500.0f, 2.0f, 2.0f, "B3"  },
327         {  250.0f,  353.0f, 2.0f, 2.0f, "B4"  },
328         {  176.0f,  250.0f, 2.0f, 2.0f, "B5"  },
329         {  125.0f,  176.0f, 1.5f, 2.0f, "B6"  },
330         {   88.0f,  125.0f, 1.5f, 1.5f, "B7"  },
331         {   62.0f,   88.0f, 1.5f, 1.5f, "B8"  },
332         {   44.0f,   62.0f, 1.5f, 1.5f, "B9"  },
333         {   31.0f,   44.0f, 1.5f, 1.5f, "B10" },
334         {  917.0f, 1297.0f, 3.0f, 3.0f, "C0"  },
335         {  648.0f,  917.0f, 3.0f, 3.0f, "C1"  },
336         {  458.0f,  648.0f, 2.0f, 3.0f, "C2"  },
337         {  324.0f,  458.0f, 2.0f, 2.0f, "C3"  },
338         {  229.0f,  324.0f, 2.0f, 2.0f, "C4"  },
339         {  162.0f,  229.0f, 2.0f, 2.0f, "C5"  },
340         {  114.0f,  162.0f, 1.5f, 2.0f, "C6"  },
341         {   81.0f,  114.0f, 1.5f, 1.5f, "C7"  },
342         {   57.0f,   81.0f, 1.5f, 1.5f, "C8"  },
343         {   40.0f,   57.0f, 1.5f, 1.5f, "C9"  },
344         {   28.0f,   40.0f, 1.5f, 1.5f, "C10" },
345
346         // US paper sizes
347         {  279.0f,  216.0f, 3.0f, 3.0f, "Letter" },
348         {  356.0f,  216.0f, 3.0f, 3.0f, "Legal"  },
349         {  432.0f,  279.0f, 3.0f, 3.0f, "Ledger" }
350 };
351
352 typedef enum {
353   EV_UNIT_INCH,
354   EV_UNIT_MM
355 } EvUnit; 
356
357 static EvUnit
358 ev_get_default_user_units (void)
359 {
360   /* Translate to the default units to use for presenting
361    * lengths to the user. Translate to default:inch if you
362    * want inches, otherwise translate to default:mm.
363    * Do *not* translate it to "predefinito:mm", if it
364    * it isn't default:mm or default:inch it will not work
365    */
366   gchar *e = _("default:mm");
367
368 #ifdef HAVE__NL_MEASUREMENT_MEASUREMENT
369   gchar *imperial = NULL;
370
371   imperial = nl_langinfo (_NL_MEASUREMENT_MEASUREMENT);
372   if (imperial && imperial[0] == 2 )
373     return EV_UNIT_INCH;  /* imperial */
374   if (imperial && imperial[0] == 1 )
375     return EV_UNIT_MM;  /* metric */
376 #endif
377
378   if (strcmp (e, "default:inch")==0)
379     return EV_UNIT_INCH;
380   else if (strcmp (e, "default:mm"))
381     g_warning ("Whoever translated default:mm did so wrongly.\n");
382   return EV_UNIT_MM;
383 }
384
385 static char *
386 ev_regular_paper_size (const EvDocumentInfo *info)
387 {
388         const struct regular_paper_size *size;   
389         EvUnit unit;
390         char *exact_size = NULL;
391         char *str = NULL;
392         int i;
393
394         unit = ev_get_default_user_units ();    
395
396         if (unit == EV_UNIT_INCH)
397                 /* Imperial measurement (inches) */
398                 exact_size = g_strdup_printf( _("%.2f x %.2f in"),
399                                               info->paper_width  / 25.4f,
400                                               info->paper_height / 25.4f );
401         else
402                 /* Metric measurement (millimeters) */
403                 exact_size = g_strdup_printf( _("%.0f x %.0f mm"),
404                                               info->paper_width,
405                                               info->paper_height );
406         
407         for (i = G_N_ELEMENTS ( regular_paper_sizes ) - 1; i >= 0; i--) {
408                 size = &regular_paper_sizes[i];
409
410                 if ( ABS( info->paper_height - size->height ) <= size->height_tolerance &&
411                      ABS( info->paper_width  - size->width  ) <= size->width_tolerance ) {
412                         /* Note to translators: first placeholder is the paper name (eg.
413                          * A4), second placeholder is the paper size (eg. 297x210 mm) */
414                         str = g_strdup_printf ( _("%s, Portrait (%s)"),
415                                                 size->description,
416                                                 exact_size );
417                 } else if ( ABS( info->paper_width  - size->height ) <= size->height_tolerance &&
418                             ABS( info->paper_height - size->width  ) <= size->width_tolerance ) {
419                         /* Note to translators: first placeholder is the paper name (eg.
420                          * A4), second placeholder is the paper size (eg. 297x210 mm) */
421                         str = g_strdup_printf ( _("%s, Landscape (%s)"),
422                                                 size->description,
423                                                 exact_size );
424                 }
425         }
426
427         if (str != NULL) {
428                 g_free (exact_size);
429                 return str;
430         } else
431                 return exact_size;
432 }
433 #endif /* HAVE_GTK_PAPER_SIZE_GET_PAPER_SIZES */
434
435 void
436 ev_properties_view_set_info (EvPropertiesView *properties, const EvDocumentInfo *info)
437 {
438         GladeXML *xml = properties->xml;
439         char *text;
440
441         if (info->fields_mask & EV_DOCUMENT_INFO_TITLE) {
442                 set_property (xml, TITLE_PROPERTY, info->title);
443         }
444         if (info->fields_mask & EV_DOCUMENT_INFO_SUBJECT) {
445                 set_property (xml, SUBJECT_PROPERTY, info->subject);
446         }
447         if (info->fields_mask & EV_DOCUMENT_INFO_AUTHOR) {
448                 set_property (xml, AUTHOR_PROPERTY, info->author);
449         }
450         if (info->fields_mask & EV_DOCUMENT_INFO_KEYWORDS) {
451                 set_property (xml, KEYWORDS_PROPERTY, info->keywords);
452         }
453         if (info->fields_mask & EV_DOCUMENT_INFO_PRODUCER) {
454                 set_property (xml, PRODUCER_PROPERTY, info->producer);
455         }
456         if (info->fields_mask & EV_DOCUMENT_INFO_CREATOR) {
457                 set_property (xml, CREATOR_PROPERTY, info->creator);
458         }
459         if (info->fields_mask & EV_DOCUMENT_INFO_CREATION_DATE) {
460                 text = ev_properties_view_format_date (info->creation_date);
461                 set_property (xml, CREATION_DATE_PROPERTY, text);
462                 g_free (text);
463         }
464         if (info->fields_mask & EV_DOCUMENT_INFO_MOD_DATE) {
465                 text = ev_properties_view_format_date (info->modified_date);
466                 set_property (xml, MOD_DATE_PROPERTY, text);
467                 g_free (text);
468         }
469         if (info->fields_mask & EV_DOCUMENT_INFO_FORMAT) {
470                 text = g_strdup_printf ("%s", info->format);
471                 set_property (xml, FORMAT_PROPERTY, text);
472                 g_free (text);
473         }
474         if (info->fields_mask & EV_DOCUMENT_INFO_N_PAGES) {
475                 text = g_strdup_printf ("%d", info->n_pages);
476                 set_property (xml, N_PAGES_PROPERTY, text);
477                 g_free (text);
478         }
479         if (info->fields_mask & EV_DOCUMENT_INFO_LINEARIZED) {
480                 set_property (xml, LINEARIZED_PROPERTY, info->linearized);
481         }
482         if (info->fields_mask & EV_DOCUMENT_INFO_SECURITY) {
483                 set_property (xml, SECURITY_PROPERTY, info->security);
484         }
485         if (info->fields_mask & EV_DOCUMENT_INFO_PAPER_SIZE) {
486                 text = ev_regular_paper_size (info);
487                 set_property (xml, PAPER_SIZE_PROPERTY, text);
488                 g_free (text);
489         }
490 }
491
492 static void
493 ev_properties_view_init (EvPropertiesView *properties)
494 {
495         GladeXML *xml;
496
497         /* Create a new GladeXML object from XML file glade_file */
498         xml = glade_xml_new (DATADIR "/evince-properties.glade", "general_page_root", GETTEXT_PACKAGE);
499         properties->xml = xml;
500         g_assert (xml != NULL);
501
502         gtk_box_pack_start (GTK_BOX (properties),
503                             glade_xml_get_widget (xml, "general_page_root"),
504                             TRUE, TRUE, 0);
505 }
506
507 void
508 ev_properties_view_register_type (GTypeModule *module)
509 {
510         ev_properties_view_get_type ();
511 }
512
513 GtkWidget *
514 ev_properties_view_new (void)
515 {
516         EvPropertiesView *properties;
517
518         properties = g_object_new (EV_TYPE_PROPERTIES, NULL);
519
520         return GTK_WIDGET (properties);
521 }