]> www.fi.muni.cz Git - evince.git/blob - properties/ev-properties-view.c
[dualscreen] fix crash on ctrl+w and fix control window closing
[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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19  */
20
21 #include "config.h"
22
23 #include <string.h>
24 #include <sys/time.h>
25 #include <time.h>
26
27 #ifdef HAVE__NL_MEASUREMENT_MEASUREMENT
28 #include <langinfo.h>
29 #endif
30
31 #include <glib/gi18n-lib.h>
32 #include <gtk/gtk.h>
33
34 #include "ev-properties-view.h"
35
36 typedef enum {
37         TITLE_PROPERTY,
38         URI_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         N_PROPERTIES
52 } Property;
53
54 typedef struct {
55         Property property;
56         const char *label;
57 } PropertyInfo;
58
59 static const PropertyInfo properties_info[] = {
60         { TITLE_PROPERTY,         N_("Title:") },
61         { URI_PROPERTY,           N_("Location:") },
62         { SUBJECT_PROPERTY,       N_("Subject:") },
63         { AUTHOR_PROPERTY,        N_("Author:") },
64         { KEYWORDS_PROPERTY,      N_("Keywords:") },
65         { PRODUCER_PROPERTY,      N_("Producer:") },
66         { CREATOR_PROPERTY,       N_("Creator:") },
67         { CREATION_DATE_PROPERTY, N_("Created:") },
68         { MOD_DATE_PROPERTY,      N_("Modified:") },
69         { N_PAGES_PROPERTY,       N_("Number of Pages:") },
70         { LINEARIZED_PROPERTY,    N_("Optimized:") },
71         { FORMAT_PROPERTY,        N_("Format:") },
72         { SECURITY_PROPERTY,      N_("Security:") },
73         { PAPER_SIZE_PROPERTY,    N_("Paper Size:") }
74 };
75
76 struct _EvPropertiesView {
77         GtkVBox base_instance;
78
79         GtkWidget *table;
80         GtkWidget *labels[N_PROPERTIES];
81         gchar     *uri;
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->uri) {
96                 g_free (properties->uri);
97                 properties->uri = 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 /* This is cut out of gconvert.c from glib (and mildly modified).  Not all
112    backends give valid UTF-8 for properties, so we make sure that is.
113  */
114 static gchar *
115 make_valid_utf8 (const gchar *name)
116 {
117   GString *string;
118   const gchar *remainder, *invalid;
119   gint remaining_bytes, valid_bytes;
120   
121   string = NULL;
122   remainder = name;
123   remaining_bytes = strlen (name);
124   
125   while (remaining_bytes != 0) 
126     {
127       if (g_utf8_validate (remainder, remaining_bytes, &invalid)) 
128         break;
129       valid_bytes = invalid - remainder;
130     
131       if (string == NULL) 
132         string = g_string_sized_new (remaining_bytes);
133
134       g_string_append_len (string, remainder, valid_bytes);
135       g_string_append_c (string, '?');
136       
137       remaining_bytes -= valid_bytes + 1;
138       remainder = invalid + 1;
139     }
140   
141   if (string == NULL)
142     return g_strdup (name);
143   
144   g_string_append (string, remainder);
145
146   g_assert (g_utf8_validate (string->str, -1, NULL));
147   
148   return g_string_free (string, FALSE);
149 }
150
151 static void
152 set_property (EvPropertiesView *properties,
153               GtkTable         *table,
154               Property          property,
155               const gchar      *text,
156               gint             *row)
157 {
158         GtkWidget *label;
159         gchar     *markup;
160         gchar     *valid_text;
161
162         if (!properties->labels[property]) {
163                 label = gtk_label_new (NULL);
164                 g_object_set (G_OBJECT (label), "xalign", 0.0, NULL);
165                 markup = g_strdup_printf ("<b>%s</b>", _(properties_info[property].label));
166                 gtk_label_set_markup (GTK_LABEL (label), markup);
167                 g_free (markup);
168
169                 gtk_table_attach (table, label, 0, 1, *row, *row + 1,
170                                   GTK_FILL, GTK_FILL, 0, 0);
171                 gtk_widget_show (label);
172         }
173
174         if (!properties->labels[property]) {
175                 label = gtk_label_new (NULL);
176
177                 g_object_set (G_OBJECT (label),
178                               "xalign", 0.0,
179                               "width_chars", 25,
180                               "selectable", TRUE,
181                               "ellipsize", PANGO_ELLIPSIZE_END,
182                               NULL);
183         } else {
184                 label = properties->labels[property];
185         }
186
187         if (text == NULL || text[0] == '\000') {
188                 markup = g_markup_printf_escaped ("<i>%s</i>", _("None"));
189                 gtk_label_set_markup (GTK_LABEL (label), markup);
190                 g_free (markup);
191         } else {
192                 valid_text = make_valid_utf8 (text ? text : "");
193                 gtk_label_set_text (GTK_LABEL (label), valid_text);
194                 g_free (valid_text);
195         }
196
197         if (!properties->labels[property]) {
198                 gtk_table_attach (table, label, 1, 2, *row, *row + 1,
199                                   GTK_FILL | GTK_EXPAND, GTK_FILL, 0, 0);
200                 properties->labels[property] = label;
201         }
202
203         gtk_widget_show (label);
204
205         *row += 1;
206 }
207
208 static GtkUnit
209 get_default_user_units (void)
210 {
211         /* Translate to the default units to use for presenting
212          * lengths to the user. Translate to default:inch if you
213          * want inches, otherwise translate to default:mm.
214          * Do *not* translate it to "predefinito:mm", if it
215          * it isn't default:mm or default:inch it will not work
216          */
217         gchar *e = _("default:mm");
218
219 #ifdef HAVE__NL_MEASUREMENT_MEASUREMENT
220         gchar *imperial = NULL;
221         
222         imperial = nl_langinfo (_NL_MEASUREMENT_MEASUREMENT);
223         if (imperial && imperial[0] == 2)
224                 return GTK_UNIT_INCH;  /* imperial */
225         if (imperial && imperial[0] == 1)
226                 return GTK_UNIT_MM;  /* metric */
227 #endif
228
229         if (strcmp (e, "default:mm") == 0)
230                 return GTK_UNIT_MM;
231         if (strcmp (e, "default:inch") == 0)
232                 return GTK_UNIT_INCH;
233         
234         g_warning ("Whoever translated default:mm did so wrongly.\n");
235                                 
236         return GTK_UNIT_MM;
237 }
238
239 static gdouble
240 get_tolerance (gdouble size)
241 {
242         if (size < 150.0f)
243                 return 1.5f;
244         else if (size >= 150.0f && size <= 600.0f)
245                 return 2.0f;
246         else
247                 return 3.0f;
248 }
249
250 static char *
251 ev_regular_paper_size (const EvDocumentInfo *info)
252 {
253         GList *paper_sizes, *l;
254         gchar *exact_size;
255         gchar *str = NULL;
256         GtkUnit units;
257
258         units = get_default_user_units ();
259
260         if (units == GTK_UNIT_MM) {
261                 exact_size = g_strdup_printf(_("%.0f × %.0f mm"),
262                                              info->paper_width,
263                                              info->paper_height);
264         } else {
265                 exact_size = g_strdup_printf (_("%.2f × %.2f inch"),
266                                               info->paper_width  / 25.4f,
267                                               info->paper_height / 25.4f);
268         }
269
270         paper_sizes = gtk_paper_size_get_paper_sizes (FALSE);
271         
272         for (l = paper_sizes; l && l->data; l = g_list_next (l)) {
273                 GtkPaperSize *size = (GtkPaperSize *) l->data;
274                 gdouble paper_width;
275                 gdouble paper_height;
276                 gdouble width_tolerance;
277                 gdouble height_tolerance;
278
279                 paper_width = gtk_paper_size_get_width (size, GTK_UNIT_MM);
280                 paper_height = gtk_paper_size_get_height (size, GTK_UNIT_MM);
281
282                 width_tolerance = get_tolerance (paper_width);
283                 height_tolerance = get_tolerance (paper_height);
284
285                 if (ABS (info->paper_height - paper_height) <= height_tolerance &&
286                     ABS (info->paper_width  - paper_width) <= width_tolerance) {
287                         /* Note to translators: first placeholder is the paper name (eg.
288                          * A4), second placeholder is the paper size (eg. 297x210 mm) */
289                         str = g_strdup_printf (_("%s, Portrait (%s)"),
290                                                gtk_paper_size_get_display_name (size),
291                                                exact_size);
292                 } else if (ABS (info->paper_width  - paper_height) <= height_tolerance &&
293                            ABS (info->paper_height - paper_width) <= width_tolerance) {
294                         /* Note to translators: first placeholder is the paper name (eg.
295                          * A4), second placeholder is the paper size (eg. 297x210 mm) */
296                         str = g_strdup_printf ( _("%s, Landscape (%s)"),
297                                                 gtk_paper_size_get_display_name (size),
298                                                 exact_size);
299                 }
300         }
301
302         g_list_foreach (paper_sizes, (GFunc) gtk_paper_size_free, NULL);
303         g_list_free (paper_sizes);
304
305         if (str != NULL) {
306                 g_free (exact_size);
307                 return str;
308         }
309         
310         return exact_size;
311 }
312
313 void
314 ev_properties_view_set_info (EvPropertiesView *properties, const EvDocumentInfo *info)
315 {
316         GtkWidget *table;
317         gchar     *text;
318         gint       row = 0;
319
320         table = properties->table;
321
322         if (info->fields_mask & EV_DOCUMENT_INFO_TITLE) {
323                 set_property (properties, GTK_TABLE (table), TITLE_PROPERTY, info->title, &row);
324         }
325         set_property (properties, GTK_TABLE (table), URI_PROPERTY, properties->uri, &row);
326         if (info->fields_mask & EV_DOCUMENT_INFO_SUBJECT) {
327                 set_property (properties, GTK_TABLE (table), SUBJECT_PROPERTY, info->subject, &row);
328         }
329         if (info->fields_mask & EV_DOCUMENT_INFO_AUTHOR) {
330                 set_property (properties, GTK_TABLE (table), AUTHOR_PROPERTY, info->author, &row);
331         }
332         if (info->fields_mask & EV_DOCUMENT_INFO_KEYWORDS) {
333                 set_property (properties, GTK_TABLE (table), KEYWORDS_PROPERTY, info->keywords, &row);
334         }
335         if (info->fields_mask & EV_DOCUMENT_INFO_PRODUCER) {
336                 set_property (properties, GTK_TABLE (table), PRODUCER_PROPERTY, info->producer, &row);
337         }
338         if (info->fields_mask & EV_DOCUMENT_INFO_CREATOR) {
339                 set_property (properties, GTK_TABLE (table), CREATOR_PROPERTY, info->creator, &row);
340         }
341         if (info->fields_mask & EV_DOCUMENT_INFO_CREATION_DATE) {
342                 text = ev_document_misc_format_date (info->creation_date);
343                 set_property (properties, GTK_TABLE (table), CREATION_DATE_PROPERTY, text, &row);
344                 g_free (text);
345         }
346         if (info->fields_mask & EV_DOCUMENT_INFO_MOD_DATE) {
347                 text = ev_document_misc_format_date (info->modified_date);
348                 set_property (properties, GTK_TABLE (table), MOD_DATE_PROPERTY, text, &row);
349                 g_free (text);
350         }
351         if (info->fields_mask & EV_DOCUMENT_INFO_FORMAT) {
352                 set_property (properties, GTK_TABLE (table), FORMAT_PROPERTY, info->format, &row);
353         }
354         if (info->fields_mask & EV_DOCUMENT_INFO_N_PAGES) {
355                 text = g_strdup_printf ("%d", info->n_pages);
356                 set_property (properties, GTK_TABLE (table), N_PAGES_PROPERTY, text, &row);
357                 g_free (text);
358         }
359         if (info->fields_mask & EV_DOCUMENT_INFO_LINEARIZED) {
360                 set_property (properties, GTK_TABLE (table), LINEARIZED_PROPERTY, info->linearized, &row);
361         }
362         if (info->fields_mask & EV_DOCUMENT_INFO_SECURITY) {
363                 set_property (properties, GTK_TABLE (table), SECURITY_PROPERTY, info->security, &row);
364         }
365         if (info->fields_mask & EV_DOCUMENT_INFO_PAPER_SIZE) {
366                 text = ev_regular_paper_size (info);
367                 set_property (properties, GTK_TABLE (table), PAPER_SIZE_PROPERTY, text, &row);
368                 g_free (text);
369         }
370 }
371
372 static void
373 ev_properties_view_init (EvPropertiesView *properties)
374 {
375         properties->table = gtk_table_new (13, 2, FALSE);
376         gtk_table_set_col_spacings (GTK_TABLE (properties->table), 12);
377         gtk_table_set_row_spacings (GTK_TABLE (properties->table), 6);
378         gtk_container_set_border_width (GTK_CONTAINER (properties->table), 12);
379         gtk_box_pack_start (GTK_BOX (properties), properties->table, 
380                             TRUE, TRUE, 0);
381         gtk_widget_show (properties->table);
382 }
383
384 void
385 ev_properties_view_register_type (GTypeModule *module)
386 {
387         ev_properties_view_get_type ();
388 }
389
390 GtkWidget *
391 ev_properties_view_new (const gchar *uri)
392 {
393         EvPropertiesView *properties;
394
395         properties = g_object_new (EV_TYPE_PROPERTIES, NULL);
396         properties->uri = g_strdup (uri);
397
398         return GTK_WIDGET (properties);
399 }