]> www.fi.muni.cz Git - evince.git/blob - shell/ev-properties.c
295194f47888c5dc295c3834fb8ffd985bf78160
[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
30 #include <glib/gi18n.h>
31 #include <gtk/gtk.h>
32 #include <glade/glade.h>
33 #include <time.h>
34 #include <sys/time.h>
35
36 enum
37 {
38         FONT_NAME_COL,
39         NUM_COLS
40 };
41
42 typedef enum
43 {
44         TITLE_PROPERTY,
45         SUBJECT_PROPERTY,
46         AUTHOR_PROPERTY,
47         KEYWORDS_PROPERTY,
48         PRODUCER_PROPERTY,
49         CREATOR_PROPERTY,
50         CREATION_DATE_PROPERTY,
51         MOD_DATE_PROPERTY,
52         N_PAGES_PROPERTY,
53         LINEARIZED_PROPERTY,
54         FORMAT_PROPERTY,
55         SECURITY_PROPERTY
56 } Property;
57
58 typedef struct
59 {
60         Property property;
61         const char *label_id;
62 } PropertyInfo;
63
64 static const PropertyInfo properties_info[] = {
65         { TITLE_PROPERTY, "title" },
66         { SUBJECT_PROPERTY, "subject" },
67         { AUTHOR_PROPERTY, "author" },
68         { KEYWORDS_PROPERTY, "keywords" },
69         { PRODUCER_PROPERTY, "producer" },
70         { CREATOR_PROPERTY, "creator" },
71         { CREATION_DATE_PROPERTY, "created" },
72         { MOD_DATE_PROPERTY, "modified" },
73         { N_PAGES_PROPERTY, "pages" },
74         { LINEARIZED_PROPERTY, "optimized" },
75         { FORMAT_PROPERTY, "version" },
76         { SECURITY_PROPERTY, "security" }
77 };
78
79 /* Returns a locale specific date and time representation */
80 static char *
81 ev_properties_format_date (GTime utime)
82 {
83         time_t time = (time_t) utime;
84         struct tm t;
85         char s[256];
86         const char *fmt_hack = "%c";
87         size_t len;
88
89         if (!localtime_r (&time, &t)) return NULL;
90
91         len = strftime (s, sizeof (s), fmt_hack, &t);
92         if (len == 0 || s[0] == '\0') return NULL;
93
94         return g_locale_to_utf8 (s, -1, NULL, NULL, NULL);
95 }
96
97 static void
98 set_property (GladeXML *xml, Property property, const char *text)
99 {
100         GtkWidget *widget;
101
102         widget = glade_xml_get_widget (xml, properties_info[property].label_id);
103         g_return_if_fail (GTK_IS_LABEL (widget));
104
105         gtk_label_set_text (GTK_LABEL (widget), text ? text : "");
106 }
107
108 static void
109 update_progress_label (GtkWidget *label, double progress)
110 {
111         if (progress > 0) {
112                 char *progress_text;
113                 progress_text = g_strdup_printf (_("Gathering font information... %3d%%"),
114                                                  (int) (progress * 100));
115                 gtk_label_set_text (GTK_LABEL (label), progress_text);
116                 g_free (progress_text);
117         } else {
118                 gtk_label_set_text (GTK_LABEL (label), "");
119         }
120 }
121
122 static void
123 job_fonts_finished_cb (EvJob *job, GtkTreeView *tree_view)
124 {
125         EvDocumentFonts *document_fonts = EV_DOCUMENT_FONTS (job->document);
126         GtkWidget *progress_label;
127         double progress;
128
129         progress_label = g_object_get_data (G_OBJECT (tree_view), "progress_label");
130         progress = ev_document_fonts_get_progress (document_fonts);
131         update_progress_label (progress_label, progress);
132
133         if (EV_JOB_FONTS (job)->scan_completed) {
134                 g_signal_handlers_disconnect_by_func
135                                 (job, job_fonts_finished_cb, tree_view);
136         } else {
137                 EvJob *new_job;
138
139                 ev_document_fonts_fill_model (document_fonts,
140                                               gtk_tree_view_get_model (tree_view));
141                 new_job = ev_job_fonts_new (job->document);
142                 ev_job_queue_add_job (job, EV_JOB_PRIORITY_LOW);
143                 g_object_unref (new_job);
144         }
145 }
146
147 static void
148 setup_fonts_view (GladeXML *xml, EvDocument *document)
149 {
150         GtkCellRenderer *renderer;
151         GtkTreeViewColumn *column;
152         GtkListStore *list_store;
153         EvJob *job;
154         GtkWidget *tree_view;
155         GtkWidget *progress_label;
156
157         tree_view = glade_xml_get_widget (xml, "fonts_treeview");
158         progress_label = glade_xml_get_widget (xml, "font_progress_label");
159
160         column = gtk_tree_view_column_new ();
161         gtk_tree_view_column_set_expand (GTK_TREE_VIEW_COLUMN (column), TRUE);
162         gtk_tree_view_append_column (GTK_TREE_VIEW (tree_view), column);
163
164         renderer = gtk_cell_renderer_text_new ();
165         gtk_tree_view_column_pack_start (GTK_TREE_VIEW_COLUMN (column), renderer, FALSE);
166         gtk_tree_view_column_set_title (GTK_TREE_VIEW_COLUMN (column), _("Name"));
167         gtk_tree_view_column_set_attributes (GTK_TREE_VIEW_COLUMN (column), renderer,
168                                              "text", EV_DOCUMENT_FONTS_COLUMN_NAME,
169                                              NULL);
170
171         list_store = gtk_list_store_new (NUM_COLS, G_TYPE_STRING);
172         gtk_tree_view_set_model (GTK_TREE_VIEW (tree_view),
173                                  GTK_TREE_MODEL (list_store));
174
175         job = ev_job_fonts_new (document);
176         g_object_set_data (G_OBJECT (tree_view), "progress_label",
177                            progress_label);
178         g_signal_connect_object (job, "finished",
179                                  G_CALLBACK (job_fonts_finished_cb),
180                                  tree_view, 0);
181         ev_job_queue_add_job (job, EV_JOB_PRIORITY_LOW);
182         g_object_unref (job);
183 }
184
185 GtkDialog *
186 ev_properties_new (EvDocument *document, const EvDocumentInfo *info)
187 {
188         GladeXML *xml;
189         GtkWidget *dialog;
190         char *text;
191         
192         /* Create a new GladeXML object from XML file glade_file */
193         xml = glade_xml_new (DATADIR "/evince-properties.glade", NULL, NULL);
194         g_return_val_if_fail (xml != NULL, NULL);
195
196         dialog = glade_xml_get_widget (xml, "properties_dialog");
197         g_return_val_if_fail (GTK_IS_DIALOG (dialog), NULL);
198                                         
199         if (info->fields_mask & EV_DOCUMENT_INFO_TITLE) {
200                 set_property (xml, TITLE_PROPERTY, info->title);
201         }
202         if (info->fields_mask & EV_DOCUMENT_INFO_SUBJECT) {
203                 set_property (xml, SUBJECT_PROPERTY, info->subject);
204         }
205         if (info->fields_mask & EV_DOCUMENT_INFO_AUTHOR) {
206                 set_property (xml, AUTHOR_PROPERTY, info->author);
207         }
208         if (info->fields_mask & EV_DOCUMENT_INFO_KEYWORDS) {
209                 set_property (xml, KEYWORDS_PROPERTY, info->keywords);
210         }
211         if (info->fields_mask & EV_DOCUMENT_INFO_PRODUCER) {
212                 set_property (xml, PRODUCER_PROPERTY, info->producer);
213         }
214         if (info->fields_mask & EV_DOCUMENT_INFO_CREATOR) {
215                 set_property (xml, CREATOR_PROPERTY, info->creator);
216         }
217         if (info->fields_mask & EV_DOCUMENT_INFO_CREATION_DATE) {
218                 text = ev_properties_format_date (info->creation_date);
219                 set_property (xml, CREATION_DATE_PROPERTY, text);
220                 g_free (text);
221         }
222         if (info->fields_mask & EV_DOCUMENT_INFO_MOD_DATE) {
223                 text = ev_properties_format_date (info->modified_date);
224                 set_property (xml, MOD_DATE_PROPERTY, text);
225                 g_free (text);
226         }
227         if (info->fields_mask & EV_DOCUMENT_INFO_FORMAT) {
228                 char **format_str = g_strsplit (info->format, "-", 2);
229
230                 text = g_strdup_printf (_("%s"), format_str[1]);
231                 set_property (xml, FORMAT_PROPERTY, text);
232
233                 g_free (text);
234                 g_strfreev (format_str);
235         }
236         if (info->fields_mask & EV_DOCUMENT_INFO_N_PAGES) {
237                 text = g_strdup_printf (_("%d"), info->n_pages);
238                 set_property (xml, N_PAGES_PROPERTY, text);
239                 g_free (text);
240         }
241         if (info->fields_mask & EV_DOCUMENT_INFO_LINEARIZED) {
242                 set_property (xml, LINEARIZED_PROPERTY, info->linearized);
243         }
244         if (info->fields_mask & EV_DOCUMENT_INFO_SECURITY) {
245                 set_property (xml, SECURITY_PROPERTY, info->security);
246         }
247
248         if (EV_IS_DOCUMENT_FONTS (document)) {
249                 setup_fonts_view (xml, document);
250         } else {
251                 gtk_widget_hide (glade_xml_get_widget (xml, "fonts_page"));
252         }
253
254         return GTK_DIALOG (dialog); 
255 }