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