]> www.fi.muni.cz Git - evince.git/blob - properties/ev-properties-view.c
Reorganize source tree.
[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
101 static void
102 ev_properties_view_class_init (EvPropertiesViewClass *properties_class)
103 {
104         GObjectClass *g_object_class = G_OBJECT_CLASS (properties_class);
105
106         g_object_class->dispose = ev_properties_view_dispose;
107 }
108
109 /* Returns a locale specific date and time representation */
110 static char *
111 ev_properties_view_format_date (GTime utime)
112 {
113         time_t time = (time_t) utime;
114         struct tm t;
115         char s[256];
116         const char *fmt_hack = "%c";
117         size_t len;
118
119         if (time == 0 || !localtime_r (&time, &t)) return NULL;
120
121         len = strftime (s, sizeof (s), fmt_hack, &t);
122         if (len == 0 || s[0] == '\0') return NULL;
123
124         return g_locale_to_utf8 (s, -1, NULL, NULL, NULL);
125 }
126
127 /* This is cut out of gconvert.c from glib (and mildly modified).  Not all
128    backends give valid UTF-8 for properties, so we make sure that is.
129  */
130 static gchar *
131 make_valid_utf8 (const gchar *name)
132 {
133   GString *string;
134   const gchar *remainder, *invalid;
135   gint remaining_bytes, valid_bytes;
136   
137   string = NULL;
138   remainder = name;
139   remaining_bytes = strlen (name);
140   
141   while (remaining_bytes != 0) 
142     {
143       if (g_utf8_validate (remainder, remaining_bytes, &invalid)) 
144         break;
145       valid_bytes = invalid - remainder;
146     
147       if (string == NULL) 
148         string = g_string_sized_new (remaining_bytes);
149
150       g_string_append_len (string, remainder, valid_bytes);
151       g_string_append_c (string, '?');
152       
153       remaining_bytes -= valid_bytes + 1;
154       remainder = invalid + 1;
155     }
156   
157   if (string == NULL)
158     return g_strdup (name);
159   
160   g_string_append (string, remainder);
161
162   g_assert (g_utf8_validate (string->str, -1, NULL));
163   
164   return g_string_free (string, FALSE);
165 }
166
167 static void
168 set_property (GladeXML *xml, Property property, const char *text)
169 {
170         GtkWidget *widget;
171         char *valid_text;
172
173         widget = glade_xml_get_widget (xml, properties_info[property].label_id);
174         g_return_if_fail (GTK_IS_LABEL (widget));
175
176         if (text == NULL || text[0] == '\000') {
177                 gchar *markup;
178
179                 markup = g_markup_printf_escaped ("<i>%s</i>", _("None"));
180                 gtk_label_set_markup (GTK_LABEL (widget), markup);
181                 g_free (markup);
182
183                 return;
184         }
185         text = text ? text : "";
186
187         valid_text = make_valid_utf8 (text);
188
189         gtk_label_set_text (GTK_LABEL (widget), valid_text);
190
191         g_free (valid_text);
192 }
193
194 /*
195  * All values are in mm. 
196  * Source: http://en.wikipedia.org/wiki/Paper_size
197  */
198 struct regular_paper_size {
199         double width;
200         double height;
201         double width_tolerance;
202         double height_tolerance;
203         const char *description;
204 } const regular_paper_sizes[] = {
205         // ISO 216 paper sizes
206         {  841.0f, 1189.0f, 3.0f, 3.0f, "A0"  },
207         {  594.0f,  841.0f, 2.0f, 3.0f, "A1"  },
208         {  420.0f,  594.0f, 2.0f, 2.0f, "A2"  },
209         {  297.0f,  420.0f, 2.0f, 2.0f, "A3"  },
210         {  210.0f,  297.0f, 2.0f, 2.0f, "A4"  },
211         {  148.0f,  210.0f, 1.5f, 2.0f, "A5"  },
212         {  105.0f,  148.0f, 1.5f, 1.5f, "A6"  },
213         {   74.0f,  105.0f, 1.5f, 1.5f, "A7"  },
214         {   52.0f,   74.0f, 1.5f, 1.5f, "A8"  },
215         {   37.0f,   52.0f, 1.5f, 1.5f, "A9"  },
216         {   26.0f,   37.0f, 1.5f, 1.5f, "A10" },
217         { 1000.0f, 1414.0f, 3.0f, 3.0f, "B0"  },
218         {  707.0f, 1000.0f, 3.0f, 3.0f, "B1"  },
219         {  500.0f,  707.0f, 2.0f, 3.0f, "B2"  },
220         {  353.0f,  500.0f, 2.0f, 2.0f, "B3"  },
221         {  250.0f,  353.0f, 2.0f, 2.0f, "B4"  },
222         {  176.0f,  250.0f, 2.0f, 2.0f, "B5"  },
223         {  125.0f,  176.0f, 1.5f, 2.0f, "B6"  },
224         {   88.0f,  125.0f, 1.5f, 1.5f, "B7"  },
225         {   62.0f,   88.0f, 1.5f, 1.5f, "B8"  },
226         {   44.0f,   62.0f, 1.5f, 1.5f, "B9"  },
227         {   31.0f,   44.0f, 1.5f, 1.5f, "B10" },
228         {  917.0f, 1297.0f, 3.0f, 3.0f, "C0"  },
229         {  648.0f,  917.0f, 3.0f, 3.0f, "C1"  },
230         {  458.0f,  648.0f, 2.0f, 3.0f, "C2"  },
231         {  324.0f,  458.0f, 2.0f, 2.0f, "C3"  },
232         {  229.0f,  324.0f, 2.0f, 2.0f, "C4"  },
233         {  162.0f,  229.0f, 2.0f, 2.0f, "C5"  },
234         {  114.0f,  162.0f, 1.5f, 2.0f, "C6"  },
235         {   81.0f,  114.0f, 1.5f, 1.5f, "C7"  },
236         {   57.0f,   81.0f, 1.5f, 1.5f, "C8"  },
237         {   40.0f,   57.0f, 1.5f, 1.5f, "C9"  },
238         {   28.0f,   40.0f, 1.5f, 1.5f, "C10" },
239
240         // US paper sizes
241         {  279.0f,  216.0f, 3.0f, 3.0f, "Letter" },
242         {  356.0f,  216.0f, 3.0f, 3.0f, "Legal"  },
243         {  432.0f,  279.0f, 3.0f, 3.0f, "Ledger" }
244 };
245
246 typedef enum {
247   EV_UNIT_INCH,
248   EV_UNIT_MM
249 } EvUnit; 
250
251 static EvUnit
252 ev_get_default_user_units (void)
253 {
254   /* Translate to the default units to use for presenting
255    * lengths to the user. Translate to default:inch if you
256    * want inches, otherwise translate to default:mm.
257    * Do *not* translate it to "predefinito:mm", if it
258    * it isn't default:mm or default:inch it will not work
259    */
260   gchar *e = _("default:mm");
261
262 #ifdef HAVE__NL_MEASUREMENT_MEASUREMENT
263   gchar *imperial = NULL;
264
265   imperial = nl_langinfo (_NL_MEASUREMENT_MEASUREMENT);
266   if (imperial && imperial[0] == 2 )
267     return EV_UNIT_INCH;  /* imperial */
268   if (imperial && imperial[0] == 1 )
269     return EV_UNIT_MM;  /* metric */
270 #endif
271
272   if (strcmp (e, "default:inch")==0)
273     return EV_UNIT_INCH;
274   else if (strcmp (e, "default:mm"))
275     g_warning ("Whoever translated default:mm did so wrongly.\n");
276   return EV_UNIT_MM;
277 }
278
279 static char *
280 ev_regular_paper_size (const EvDocumentInfo *info)
281 {
282         const struct regular_paper_size *size;   
283         EvUnit unit;
284         char *exact_size = NULL;
285         char *str = NULL;
286         int i;
287
288         unit = ev_get_default_user_units ();    
289
290         if (unit == EV_UNIT_INCH)
291                 /* Imperial measurement (inches) */
292                 exact_size = g_strdup_printf( _("%.2f x %.2f in"),
293                                               info->paper_width  / 25.4f,
294                                               info->paper_height / 25.4f );
295         else
296                 /* Metric measurement (millimeters) */
297                 exact_size = g_strdup_printf( _("%.0f x %.0f mm"),
298                                               info->paper_width,
299                                               info->paper_height );
300         
301         for (i = G_N_ELEMENTS ( regular_paper_sizes ) - 1; i >= 0; i--) {
302                 size = &regular_paper_sizes[i];
303
304                 if ( ABS( info->paper_height - size->height ) <= size->height_tolerance &&
305                      ABS( info->paper_width  - size->width  ) <= size->width_tolerance ) {
306                         /* Note to translators: first placeholder is the paper name (eg.
307                          * A4), second placeholder is the paper size (eg. 297x210 mm) */
308                         str = g_strdup_printf ( _("%s, Portrait (%s)"),
309                                                 size->description,
310                                                 exact_size );
311                 } else if ( ABS( info->paper_width  - size->height ) <= size->height_tolerance &&
312                             ABS( info->paper_height - size->width  ) <= size->width_tolerance ) {
313                         /* Note to translators: first placeholder is the paper name (eg.
314                          * A4), second placeholder is the paper size (eg. 297x210 mm) */
315                         str = g_strdup_printf ( _("%s, Landscape (%s)"),
316                                                 size->description,
317                                                 exact_size );
318                 }
319         }
320
321         if (str != NULL) {
322                 g_free (exact_size);
323                 return str;
324         } else
325                 return exact_size;
326 }
327
328 void
329 ev_properties_view_set_info (EvPropertiesView *properties, const EvDocumentInfo *info)
330 {
331         GladeXML *xml = properties->xml;
332         char *text;
333
334         if (info->fields_mask & EV_DOCUMENT_INFO_TITLE) {
335                 set_property (xml, TITLE_PROPERTY, info->title);
336         }
337         if (info->fields_mask & EV_DOCUMENT_INFO_SUBJECT) {
338                 set_property (xml, SUBJECT_PROPERTY, info->subject);
339         }
340         if (info->fields_mask & EV_DOCUMENT_INFO_AUTHOR) {
341                 set_property (xml, AUTHOR_PROPERTY, info->author);
342         }
343         if (info->fields_mask & EV_DOCUMENT_INFO_KEYWORDS) {
344                 set_property (xml, KEYWORDS_PROPERTY, info->keywords);
345         }
346         if (info->fields_mask & EV_DOCUMENT_INFO_PRODUCER) {
347                 set_property (xml, PRODUCER_PROPERTY, info->producer);
348         }
349         if (info->fields_mask & EV_DOCUMENT_INFO_CREATOR) {
350                 set_property (xml, CREATOR_PROPERTY, info->creator);
351         }
352         if (info->fields_mask & EV_DOCUMENT_INFO_CREATION_DATE) {
353                 text = ev_properties_view_format_date (info->creation_date);
354                 set_property (xml, CREATION_DATE_PROPERTY, text);
355                 g_free (text);
356         }
357         if (info->fields_mask & EV_DOCUMENT_INFO_MOD_DATE) {
358                 text = ev_properties_view_format_date (info->modified_date);
359                 set_property (xml, MOD_DATE_PROPERTY, text);
360                 g_free (text);
361         }
362         if (info->fields_mask & EV_DOCUMENT_INFO_FORMAT) {
363                 text = g_strdup_printf ("%s", info->format);
364                 set_property (xml, FORMAT_PROPERTY, text);
365                 g_free (text);
366         }
367         if (info->fields_mask & EV_DOCUMENT_INFO_N_PAGES) {
368                 text = g_strdup_printf ("%d", info->n_pages);
369                 set_property (xml, N_PAGES_PROPERTY, text);
370                 g_free (text);
371         }
372         if (info->fields_mask & EV_DOCUMENT_INFO_LINEARIZED) {
373                 set_property (xml, LINEARIZED_PROPERTY, info->linearized);
374         }
375         if (info->fields_mask & EV_DOCUMENT_INFO_SECURITY) {
376                 set_property (xml, SECURITY_PROPERTY, info->security);
377         }
378         if (info->fields_mask & EV_DOCUMENT_INFO_PAPER_SIZE) {
379                 text = ev_regular_paper_size (info);
380                 set_property (xml, PAPER_SIZE_PROPERTY, text);
381                 g_free (text);
382         }
383 }
384
385 static void
386 ev_properties_view_init (EvPropertiesView *properties)
387 {
388         GladeXML *xml;
389
390         /* Create a new GladeXML object from XML file glade_file */
391         xml = glade_xml_new (DATADIR "/evince-properties.glade", "general_page_root", GETTEXT_PACKAGE);
392         properties->xml = xml;
393         g_assert (xml != NULL);
394
395         gtk_box_pack_start (GTK_BOX (properties),
396                             glade_xml_get_widget (xml, "general_page_root"),
397                             TRUE, TRUE, 0);
398 }
399
400 void
401 ev_properties_view_register_type (GTypeModule *module)
402 {
403         ev_properties_view_get_type ();
404 }
405
406 GtkWidget *
407 ev_properties_view_new (void)
408 {
409         EvPropertiesView *properties;
410
411         properties = g_object_new (EV_TYPE_PROPERTIES, NULL);
412
413         return GTK_WIDGET (properties);
414 }