]> www.fi.muni.cz Git - evince.git/blob - shell/ev-properties.c
A bit different fix for rounding problem but it has no
[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
28 #include <glib/gi18n.h>
29 #include <gtk/gtk.h>
30 #include <glade/glade.h>
31 #include <time.h>
32 #include <sys/time.h>
33
34 typedef enum
35 {
36         TITLE_PROPERTY,
37         SUBJECT_PROPERTY,
38         AUTHOR_PROPERTY,
39         KEYWORDS_PROPERTY,
40         PRODUCER_PROPERTY,
41         CREATOR_PROPERTY,
42         CREATION_DATE_PROPERTY,
43         MOD_DATE_PROPERTY,
44         N_PAGES_PROPERTY,
45         LINEARIZED_PROPERTY,
46         FORMAT_PROPERTY,
47         SECURITY_PROPERTY
48 } Property;
49
50 typedef struct
51 {
52         Property property;
53         const char *label_id;
54 } PropertyInfo;
55
56 static const PropertyInfo properties_info[] = {
57         { TITLE_PROPERTY, "title" },
58         { SUBJECT_PROPERTY, "subject" },
59         { AUTHOR_PROPERTY, "author" },
60         { KEYWORDS_PROPERTY, "keywords" },
61         { PRODUCER_PROPERTY, "producer" },
62         { CREATOR_PROPERTY, "creator" },
63         { CREATION_DATE_PROPERTY, "created" },
64         { MOD_DATE_PROPERTY, "modified" },
65         { N_PAGES_PROPERTY, "pages" },
66         { LINEARIZED_PROPERTY, "optimized" },
67         { FORMAT_PROPERTY, "version" },
68         { SECURITY_PROPERTY, "security" }
69 };
70
71 /* Returns a locale specific date and time representation */
72 static char *
73 ev_properties_format_date (GTime utime)
74 {
75         time_t time = (time_t) utime;
76         struct tm t;
77         char s[256];
78         const char *fmt_hack = "%c";
79         size_t len;
80
81         if (!localtime_r (&time, &t)) return NULL;
82
83         len = strftime (s, sizeof (s), fmt_hack, &t);
84         if (len == 0 || s[0] == '\0') return NULL;
85
86         return g_locale_to_utf8 (s, -1, NULL, NULL, NULL);
87 }
88
89 static void
90 set_property (GladeXML *xml, Property property, const char *text)
91 {
92         GtkWidget *widget;
93
94         widget = glade_xml_get_widget (xml, properties_info[property].label_id);
95         g_return_if_fail (GTK_IS_LABEL (widget));
96
97         gtk_label_set_text (GTK_LABEL (widget), text ? text : "");
98 }
99
100 static void
101 setup_fonts_view (GladeXML *xml, GtkTreeModel *fonts)
102 {
103         GtkWidget *widget;
104         GtkCellRenderer *renderer;
105         GtkTreeViewColumn *column;
106
107         widget = glade_xml_get_widget (xml, "fonts_treeview");
108         gtk_tree_view_set_model (GTK_TREE_VIEW (widget), fonts);
109
110         column = gtk_tree_view_column_new ();
111         gtk_tree_view_column_set_expand (GTK_TREE_VIEW_COLUMN (column), TRUE);
112         gtk_tree_view_append_column (GTK_TREE_VIEW (widget), column);
113
114         renderer = gtk_cell_renderer_text_new ();
115         gtk_tree_view_column_pack_start (GTK_TREE_VIEW_COLUMN (column), renderer, FALSE);
116         gtk_tree_view_column_set_title (GTK_TREE_VIEW_COLUMN (column), _("Name"));
117         gtk_tree_view_column_set_attributes (GTK_TREE_VIEW_COLUMN (column), renderer,
118                                              "text", EV_DOCUMENT_FONTS_COLUMN_NAME,
119                                              NULL);
120 }
121
122 GtkDialog *
123 ev_properties_new (EvDocumentInfo *info, GtkTreeModel *fonts)
124 {
125         GladeXML *xml;
126         GtkWidget *dialog;
127         char *text;
128         
129         /* Create a new GladeXML object from XML file glade_file */
130         xml = glade_xml_new (DATADIR "/evince-properties.glade", NULL, NULL);
131         g_return_val_if_fail (xml != NULL, NULL);
132
133         dialog = glade_xml_get_widget (xml, "properties_dialog");
134         g_return_val_if_fail (GTK_IS_DIALOG (dialog), NULL);
135                                         
136         if (info->fields_mask & EV_DOCUMENT_INFO_TITLE) {
137                 set_property (xml, TITLE_PROPERTY, info->title);
138         }
139         if (info->fields_mask & EV_DOCUMENT_INFO_SUBJECT) {
140                 set_property (xml, SUBJECT_PROPERTY, info->subject);
141         }
142         if (info->fields_mask & EV_DOCUMENT_INFO_AUTHOR) {
143                 set_property (xml, AUTHOR_PROPERTY, info->author);
144         }
145         if (info->fields_mask & EV_DOCUMENT_INFO_KEYWORDS) {
146                 set_property (xml, KEYWORDS_PROPERTY, info->keywords);
147         }
148         if (info->fields_mask & EV_DOCUMENT_INFO_PRODUCER) {
149                 set_property (xml, PRODUCER_PROPERTY, info->producer);
150         }
151         if (info->fields_mask & EV_DOCUMENT_INFO_CREATOR) {
152                 set_property (xml, CREATOR_PROPERTY, info->creator);
153         }
154         if (info->fields_mask & EV_DOCUMENT_INFO_CREATION_DATE) {
155                 text = ev_properties_format_date ((GTime) info->creation_date);
156                 set_property (xml, CREATION_DATE_PROPERTY, text);
157                 g_free (text);
158         }
159         if (info->fields_mask & EV_DOCUMENT_INFO_MOD_DATE) {
160                 text = ev_properties_format_date ((GTime) info->modified_date);
161                 set_property (xml, MOD_DATE_PROPERTY, text);
162                 g_free (text);
163         }
164         if (info->fields_mask & EV_DOCUMENT_INFO_FORMAT) {
165                 char **format_str = g_strsplit (info->format, "-", 2);
166
167                 text = g_strdup_printf (_("%s"), format_str[1]);
168                 set_property (xml, FORMAT_PROPERTY, text);
169
170                 g_free (text);
171                 g_strfreev (format_str);
172         }
173         if (info->fields_mask & EV_DOCUMENT_INFO_N_PAGES) {
174                 text = g_strdup_printf (_("%d"), info->n_pages);
175                 set_property (xml, N_PAGES_PROPERTY, text);
176                 g_free (text);
177         }
178         if (info->fields_mask & EV_DOCUMENT_INFO_LINEARIZED) {
179                 set_property (xml, LINEARIZED_PROPERTY, info->linearized);
180         }
181         if (info->fields_mask & EV_DOCUMENT_INFO_SECURITY) {
182                 set_property (xml, SECURITY_PROPERTY, info->security);
183         }
184
185         if (fonts) {
186                 setup_fonts_view (xml, fonts);
187         }
188
189         return GTK_DIALOG (dialog); 
190 }