]> www.fi.muni.cz Git - evince.git/blob - shell/ev-properties.c
d7974c0f1ef5b16bb3777e73075fd9cabac85196
[evince.git] / shell / ev-properties.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.h"
26 #include "ev-document-fonts.h"
27 #include "ev-jobs.h"
28 #include "ev-job-queue.h"
29 #include "ev-page-cache.h"
30
31 #include <glib/gi18n.h>
32 #include <gtk/gtk.h>
33 #include <glade/glade.h>
34 #include <time.h>
35 #include <sys/time.h>
36 #include <string.h>
37
38 enum
39 {
40         FONT_NAME_COL,
41         NUM_COLS
42 };
43
44 typedef enum
45 {
46         TITLE_PROPERTY,
47         SUBJECT_PROPERTY,
48         AUTHOR_PROPERTY,
49         KEYWORDS_PROPERTY,
50         PRODUCER_PROPERTY,
51         CREATOR_PROPERTY,
52         CREATION_DATE_PROPERTY,
53         MOD_DATE_PROPERTY,
54         N_PAGES_PROPERTY,
55         LINEARIZED_PROPERTY,
56         FORMAT_PROPERTY,
57         SECURITY_PROPERTY
58 } Property;
59
60 typedef struct
61 {
62         Property property;
63         const char *label_id;
64 } PropertyInfo;
65
66 static const PropertyInfo properties_info[] = {
67         { TITLE_PROPERTY, "title" },
68         { SUBJECT_PROPERTY, "subject" },
69         { AUTHOR_PROPERTY, "author" },
70         { KEYWORDS_PROPERTY, "keywords" },
71         { PRODUCER_PROPERTY, "producer" },
72         { CREATOR_PROPERTY, "creator" },
73         { CREATION_DATE_PROPERTY, "created" },
74         { MOD_DATE_PROPERTY, "modified" },
75         { N_PAGES_PROPERTY, "pages" },
76         { LINEARIZED_PROPERTY, "optimized" },
77         { FORMAT_PROPERTY, "version" },
78         { SECURITY_PROPERTY, "security" }
79 };
80
81 struct _EvProperties {
82         GObject base_instance;
83
84         GladeXML *xml;
85
86         GtkWidget *dialog;
87         GtkWidget *fonts_treeview;
88         GtkWidget *fonts_progress_label;
89         GtkWidget *font_page;
90
91         EvDocument *document;
92 };
93
94 struct _EvPropertiesClass {
95         GObjectClass base_class;
96 };
97
98 G_DEFINE_TYPE (EvProperties, ev_properties, G_TYPE_OBJECT)
99
100 static void
101 ev_properties_dispose (GObject *object)
102 {
103         EvProperties *properties = EV_PROPERTIES (object);
104
105         if (properties->xml) {
106                 g_object_unref (properties->xml);
107                 properties->xml = NULL;
108         }
109 }
110
111 static void
112 ev_properties_class_init (EvPropertiesClass *properties_class)
113 {
114         GObjectClass *g_object_class = G_OBJECT_CLASS (properties_class);
115
116         g_object_class->dispose = ev_properties_dispose;
117 }
118
119 static void
120 dialog_destroy_cb (GtkWidget *dialog, EvProperties *properties)
121 {
122         g_object_unref (properties);
123 }
124
125 static void
126 ev_properties_init (EvProperties *properties)
127 {
128         GladeXML *xml;
129         GtkCellRenderer *renderer;
130         GtkTreeViewColumn *column;
131
132         /* Create a new GladeXML object from XML file glade_file */
133         xml = glade_xml_new (DATADIR "/evince-properties.glade", NULL, NULL);
134         properties->xml = xml;
135         g_assert (xml != NULL);
136
137         properties->dialog = glade_xml_get_widget (xml, "properties_dialog");
138         properties->fonts_treeview = glade_xml_get_widget (xml, "fonts_treeview");
139         properties->fonts_progress_label = glade_xml_get_widget (xml, "font_progress_label");
140         properties->font_page = glade_xml_get_widget (xml, "fonts_page");
141
142         column = gtk_tree_view_column_new ();
143         gtk_tree_view_column_set_expand (GTK_TREE_VIEW_COLUMN (column), TRUE);
144         gtk_tree_view_append_column (GTK_TREE_VIEW (properties->fonts_treeview), column);
145
146         renderer = gtk_cell_renderer_text_new ();
147         gtk_tree_view_column_pack_start (GTK_TREE_VIEW_COLUMN (column), renderer, FALSE);
148         gtk_tree_view_column_set_title (GTK_TREE_VIEW_COLUMN (column), _("Name"));
149         gtk_tree_view_column_set_attributes (GTK_TREE_VIEW_COLUMN (column), renderer,
150                                              "text", EV_DOCUMENT_FONTS_COLUMN_NAME,
151                                              NULL);
152
153         g_signal_connect (properties->dialog, "destroy",
154                           G_CALLBACK (dialog_destroy_cb), properties);
155 }
156
157 /* Returns a locale specific date and time representation */
158 static char *
159 ev_properties_format_date (GTime utime)
160 {
161         time_t time = (time_t) utime;
162         struct tm t;
163         char s[256];
164         const char *fmt_hack = "%c";
165         size_t len;
166
167         if (!localtime_r (&time, &t)) return NULL;
168
169         len = strftime (s, sizeof (s), fmt_hack, &t);
170         if (len == 0 || s[0] == '\0') return NULL;
171
172         return g_locale_to_utf8 (s, -1, NULL, NULL, NULL);
173 }
174
175 /* This is cut out of gconvert.c from glib (and mildly modified).  Not all
176    backends give valid UTF-8 for properties, so we make sure that is.
177  */
178 static gchar *
179 make_valid_utf8 (const gchar *name)
180 {
181   GString *string;
182   const gchar *remainder, *invalid;
183   gint remaining_bytes, valid_bytes;
184   
185   string = NULL;
186   remainder = name;
187   remaining_bytes = strlen (name);
188   
189   while (remaining_bytes != 0) 
190     {
191       if (g_utf8_validate (remainder, remaining_bytes, &invalid)) 
192         break;
193       valid_bytes = invalid - remainder;
194     
195       if (string == NULL) 
196         string = g_string_sized_new (remaining_bytes);
197
198       g_string_append_len (string, remainder, valid_bytes);
199       g_string_append_c (string, '?');
200       
201       remaining_bytes -= valid_bytes + 1;
202       remainder = invalid + 1;
203     }
204   
205   if (string == NULL)
206     return g_strdup (name);
207   
208   g_string_append (string, remainder);
209
210   g_assert (g_utf8_validate (string->str, -1, NULL));
211   
212   return g_string_free (string, FALSE);
213 }
214
215 static void
216 set_property (GladeXML *xml, Property property, const char *text)
217 {
218         GtkWidget *widget;
219         char *valid_text;
220
221         widget = glade_xml_get_widget (xml, properties_info[property].label_id);
222         g_return_if_fail (GTK_IS_LABEL (widget));
223
224         if (text == NULL || text[0] == '\000') {
225                 gchar *markup;
226
227                 markup = g_markup_printf_escaped ("<i>%s</i>", _("None"));
228                 gtk_label_set_markup (GTK_LABEL (widget), markup);
229                 g_free (markup);
230
231                 return;
232         }
233         text = text ? text : "";
234
235         valid_text = make_valid_utf8 (text);
236
237         gtk_label_set_text (GTK_LABEL (widget), valid_text);
238
239         g_free (valid_text);
240 }
241
242 static void
243 update_progress_label (GtkWidget *label, double progress)
244 {
245         if (progress > 0) {
246                 char *progress_text;
247                 progress_text = g_strdup_printf (_("Gathering font information... %3d%%"),
248                                                  (int) (progress * 100));
249                 gtk_label_set_text (GTK_LABEL (label), progress_text);
250                 g_free (progress_text);
251                 gtk_widget_show (label);
252         } else {
253                 gtk_widget_hide (label);
254         }
255 }
256
257 static void
258 job_fonts_finished_cb (EvJob *job, EvProperties *properties)
259 {       
260         EvDocumentFonts *document_fonts = EV_DOCUMENT_FONTS (job->document);
261         double progress;
262
263         progress = ev_document_fonts_get_progress (document_fonts);
264         update_progress_label (properties->fonts_progress_label, progress);
265
266         if (EV_JOB_FONTS (job)->scan_completed) {
267                 g_signal_handlers_disconnect_by_func
268                                 (job, job_fonts_finished_cb, properties);
269         } else {
270                 GtkTreeModel *model;
271                 EvJob *new_job;
272
273                 model = gtk_tree_view_get_model
274                                 (GTK_TREE_VIEW (properties->fonts_treeview));
275                 ev_document_doc_mutex_lock ();
276                 ev_document_fonts_fill_model (document_fonts, model);
277                 ev_document_doc_mutex_unlock ();
278                 new_job = ev_job_fonts_new (job->document);
279                 ev_job_queue_add_job (job, EV_JOB_PRIORITY_LOW);
280                 g_object_unref (new_job);
281         }
282 }
283
284 static void
285 setup_fonts_view (EvProperties *properties)
286 {
287         GtkTreeView *tree_view = GTK_TREE_VIEW (properties->fonts_treeview);
288         GtkListStore *list_store;
289         EvJob *job;
290
291         list_store = gtk_list_store_new (NUM_COLS, G_TYPE_STRING);
292         gtk_tree_view_set_model (tree_view, GTK_TREE_MODEL (list_store));
293
294         job = ev_job_fonts_new (properties->document);
295         g_signal_connect_object (job, "finished",
296                                  G_CALLBACK (job_fonts_finished_cb),
297                                  properties, 0);
298         ev_job_queue_add_job (job, EV_JOB_PRIORITY_LOW);
299         g_object_unref (job);
300 }
301
302 void
303 ev_properties_set_document (EvProperties *properties,
304                             EvDocument   *document)
305 {
306         EvPageCache *page_cache;
307         GladeXML *xml = properties->xml;
308         const EvDocumentInfo *info;
309         char *text;
310
311         properties->document = document;
312
313         page_cache = ev_page_cache_get (document);
314         info = ev_page_cache_get_info (page_cache);
315
316         if (info->fields_mask & EV_DOCUMENT_INFO_TITLE) {
317                 set_property (xml, TITLE_PROPERTY, info->title);
318         }
319         if (info->fields_mask & EV_DOCUMENT_INFO_SUBJECT) {
320                 set_property (xml, SUBJECT_PROPERTY, info->subject);
321         }
322         if (info->fields_mask & EV_DOCUMENT_INFO_AUTHOR) {
323                 set_property (xml, AUTHOR_PROPERTY, info->author);
324         }
325         if (info->fields_mask & EV_DOCUMENT_INFO_KEYWORDS) {
326                 set_property (xml, KEYWORDS_PROPERTY, info->keywords);
327         }
328         if (info->fields_mask & EV_DOCUMENT_INFO_PRODUCER) {
329                 set_property (xml, PRODUCER_PROPERTY, info->producer);
330         }
331         if (info->fields_mask & EV_DOCUMENT_INFO_CREATOR) {
332                 set_property (xml, CREATOR_PROPERTY, info->creator);
333         }
334         if (info->fields_mask & EV_DOCUMENT_INFO_CREATION_DATE) {
335                 text = ev_properties_format_date (info->creation_date);
336                 set_property (xml, CREATION_DATE_PROPERTY, text);
337                 g_free (text);
338         }
339         if (info->fields_mask & EV_DOCUMENT_INFO_MOD_DATE) {
340                 text = ev_properties_format_date (info->modified_date);
341                 set_property (xml, MOD_DATE_PROPERTY, text);
342                 g_free (text);
343         }
344         if (info->fields_mask & EV_DOCUMENT_INFO_FORMAT) {
345                 text = g_strdup_printf ("%s", info->format);
346                 set_property (xml, FORMAT_PROPERTY, text);
347         }
348         if (info->fields_mask & EV_DOCUMENT_INFO_N_PAGES) {
349                 text = g_strdup_printf ("%d", info->n_pages);
350                 set_property (xml, N_PAGES_PROPERTY, text);
351                 g_free (text);
352         }
353         if (info->fields_mask & EV_DOCUMENT_INFO_LINEARIZED) {
354                 set_property (xml, LINEARIZED_PROPERTY, info->linearized);
355         }
356         if (info->fields_mask & EV_DOCUMENT_INFO_SECURITY) {
357                 set_property (xml, SECURITY_PROPERTY, info->security);
358         }
359
360         if (EV_IS_DOCUMENT_FONTS (document)) {
361                 gtk_widget_show (properties->font_page);
362                 setup_fonts_view (properties);
363         } else {
364                 gtk_widget_hide (properties->font_page);
365         }
366 }
367
368 EvProperties *
369 ev_properties_new ()
370 {
371         EvProperties *properties;
372
373         properties = g_object_new (EV_TYPE_PROPERTIES, NULL);
374
375         return properties;
376 }
377
378 void
379 ev_properties_show (EvProperties *properties, GtkWidget *parent)
380 {
381         gtk_window_set_transient_for (GTK_WINDOW (properties->dialog),
382                                       GTK_WINDOW (parent));
383         g_signal_connect (properties->dialog, "response",
384                           G_CALLBACK (gtk_widget_destroy), NULL);
385         gtk_widget_show (properties->dialog);
386 }