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
4 * Copyright (C) 2005 Red Hat, Inc
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.
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.
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.
25 #include "ev-properties.h"
26 #include "ev-document-fonts.h"
28 #include "ev-job-queue.h"
29 #include "ev-page-cache.h"
31 #include <glib/gi18n.h>
33 #include <glade/glade.h>
52 CREATION_DATE_PROPERTY,
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" }
81 struct _EvProperties {
82 GObject base_instance;
87 GtkWidget *fonts_treeview;
88 GtkWidget *fonts_progress_label;
94 struct _EvPropertiesClass {
95 GObjectClass base_class;
98 G_DEFINE_TYPE (EvProperties, ev_properties, G_TYPE_OBJECT)
101 ev_properties_dispose (GObject *object)
103 EvProperties *properties = EV_PROPERTIES (object);
105 if (properties->xml) {
106 g_object_unref (properties->xml);
107 properties->xml = NULL;
112 ev_properties_class_init (EvPropertiesClass *properties_class)
114 GObjectClass *g_object_class = G_OBJECT_CLASS (properties_class);
116 g_object_class->dispose = ev_properties_dispose;
120 dialog_destroy_cb (GtkWidget *dialog, EvProperties *properties)
122 g_object_unref (properties);
126 ev_properties_init (EvProperties *properties)
129 GtkCellRenderer *renderer;
130 GtkTreeViewColumn *column;
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);
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");
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);
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,
153 g_signal_connect (properties->dialog, "destroy",
154 G_CALLBACK (dialog_destroy_cb), properties);
157 /* Returns a locale specific date and time representation */
159 ev_properties_format_date (GTime utime)
161 time_t time = (time_t) utime;
164 const char *fmt_hack = "%c";
167 if (!localtime_r (&time, &t)) return NULL;
169 len = strftime (s, sizeof (s), fmt_hack, &t);
170 if (len == 0 || s[0] == '\0') return NULL;
172 return g_locale_to_utf8 (s, -1, NULL, NULL, NULL);
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.
179 make_valid_utf8 (const gchar *name)
182 const gchar *remainder, *invalid;
183 gint remaining_bytes, valid_bytes;
187 remaining_bytes = strlen (name);
189 while (remaining_bytes != 0)
191 if (g_utf8_validate (remainder, remaining_bytes, &invalid))
193 valid_bytes = invalid - remainder;
196 string = g_string_sized_new (remaining_bytes);
198 g_string_append_len (string, remainder, valid_bytes);
199 g_string_append_c (string, '?');
201 remaining_bytes -= valid_bytes + 1;
202 remainder = invalid + 1;
206 return g_strdup (name);
208 g_string_append (string, remainder);
210 g_assert (g_utf8_validate (string->str, -1, NULL));
212 return g_string_free (string, FALSE);
216 set_property (GladeXML *xml, Property property, const char *text)
221 widget = glade_xml_get_widget (xml, properties_info[property].label_id);
222 g_return_if_fail (GTK_IS_LABEL (widget));
224 if (text == NULL || text[0] == '\000') {
227 markup = g_markup_printf_escaped ("<i>%s</i>", _("None"));
228 gtk_label_set_markup (GTK_LABEL (widget), markup);
233 text = text ? text : "";
235 valid_text = make_valid_utf8 (text);
237 gtk_label_set_text (GTK_LABEL (widget), valid_text);
243 update_progress_label (GtkWidget *label, double progress)
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);
253 gtk_widget_hide (label);
258 job_fonts_finished_cb (EvJob *job, EvProperties *properties)
260 EvDocumentFonts *document_fonts = EV_DOCUMENT_FONTS (job->document);
263 progress = ev_document_fonts_get_progress (document_fonts);
264 update_progress_label (properties->fonts_progress_label, progress);
266 if (EV_JOB_FONTS (job)->scan_completed) {
267 g_signal_handlers_disconnect_by_func
268 (job, job_fonts_finished_cb, properties);
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);
285 setup_fonts_view (EvProperties *properties)
287 GtkTreeView *tree_view = GTK_TREE_VIEW (properties->fonts_treeview);
288 GtkListStore *list_store;
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));
294 job = ev_job_fonts_new (properties->document);
295 g_signal_connect_object (job, "finished",
296 G_CALLBACK (job_fonts_finished_cb),
298 ev_job_queue_add_job (job, EV_JOB_PRIORITY_LOW);
299 g_object_unref (job);
303 ev_properties_set_document (EvProperties *properties,
304 EvDocument *document)
306 EvPageCache *page_cache;
307 GladeXML *xml = properties->xml;
308 const EvDocumentInfo *info;
311 properties->document = document;
313 page_cache = ev_page_cache_get (document);
314 info = ev_page_cache_get_info (page_cache);
316 if (info->fields_mask & EV_DOCUMENT_INFO_TITLE) {
317 set_property (xml, TITLE_PROPERTY, info->title);
319 if (info->fields_mask & EV_DOCUMENT_INFO_SUBJECT) {
320 set_property (xml, SUBJECT_PROPERTY, info->subject);
322 if (info->fields_mask & EV_DOCUMENT_INFO_AUTHOR) {
323 set_property (xml, AUTHOR_PROPERTY, info->author);
325 if (info->fields_mask & EV_DOCUMENT_INFO_KEYWORDS) {
326 set_property (xml, KEYWORDS_PROPERTY, info->keywords);
328 if (info->fields_mask & EV_DOCUMENT_INFO_PRODUCER) {
329 set_property (xml, PRODUCER_PROPERTY, info->producer);
331 if (info->fields_mask & EV_DOCUMENT_INFO_CREATOR) {
332 set_property (xml, CREATOR_PROPERTY, info->creator);
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);
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);
344 if (info->fields_mask & EV_DOCUMENT_INFO_FORMAT) {
345 text = g_strdup_printf ("%s", info->format);
346 set_property (xml, FORMAT_PROPERTY, text);
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);
353 if (info->fields_mask & EV_DOCUMENT_INFO_LINEARIZED) {
354 set_property (xml, LINEARIZED_PROPERTY, info->linearized);
356 if (info->fields_mask & EV_DOCUMENT_INFO_SECURITY) {
357 set_property (xml, SECURITY_PROPERTY, info->security);
360 if (EV_IS_DOCUMENT_FONTS (document)) {
361 gtk_widget_show (properties->font_page);
362 setup_fonts_view (properties);
364 gtk_widget_hide (properties->font_page);
371 EvProperties *properties;
373 properties = g_object_new (EV_TYPE_PROPERTIES, NULL);
379 ev_properties_show (EvProperties *properties, GtkWidget *parent)
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);