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