]> www.fi.muni.cz Git - evince.git/blob - shell/ev-window-title.c
Test for .dvi in title of pdf documents.
[evince.git] / shell / ev-window-title.c
1 /* this file is part of evince, a gnome document viewer
2  *
3  *  Copyright (C) 2005 Red Hat, Inc
4  *
5  * Evince is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * Evince is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
18  */
19
20 #include "ev-window-title.h"
21 #include "ev-document-factory.h"
22
23 #include <glib/gi18n.h>
24 #include <libgnomevfs/gnome-vfs-utils.h>
25
26 typedef struct
27 {
28         EvBackend backend;
29         const char *ext;
30 } BadExtensionEntry;
31
32 struct _EvWindowTitle
33 {
34         EvWindow *window;
35         EvWindowTitleType type;
36         EvDocument *document;
37         char *uri;
38 };
39
40 static const BadExtensionEntry bad_extensions[] = {
41         { EV_BACKEND_PS, ".dvi" },
42         { EV_BACKEND_PDF, ".doc" },
43         { EV_BACKEND_PDF, ".dvi" },
44         { EV_BACKEND_PDF, ".indd" },
45         { EV_BACKEND_PDF, ".rtf" }
46 };
47
48 EvWindowTitle *
49 ev_window_title_new (EvWindow *window)
50 {
51         EvWindowTitle *window_title;
52
53         window_title = g_new0 (EvWindowTitle, 1);
54         window_title->window = window;
55         window_title->type = EV_WINDOW_TITLE_DOCUMENT;
56
57         return window_title;
58 }
59
60 static char *
61 get_filename_from_uri (const char *uri)
62 {
63         char *filename;
64         char *display_name;
65
66         display_name = gnome_vfs_format_uri_for_display (uri);
67         filename = g_path_get_basename (display_name);
68         g_free (display_name);
69
70         return filename;
71 }
72
73 /* Some docs report titles with confusing extensions (ex. .doc for pdf).
74    Let's show the filename in this case */
75 static void
76 ev_window_title_sanitize_extension (EvWindowTitle *window_title, char **title) {
77         EvBackend backend;
78         int i;
79
80         backend = ev_document_factory_get_backend (window_title->document);
81         for (i = 0; i < G_N_ELEMENTS (bad_extensions); i++) {
82                 if (bad_extensions[i].backend == backend &&
83                     g_str_has_suffix (*title, bad_extensions[i].ext)) {
84                         char *new_title;
85                         char *filename = get_filename_from_uri (window_title->uri);
86
87                         new_title = g_strdup_printf ("%s (%s)", *title, filename);
88                         g_free (*title);
89                         *title = new_title;
90
91                         g_free (filename);
92                 }
93         }
94 }
95
96 static void
97 ev_window_title_update (EvWindowTitle *window_title)
98 {
99         GtkWindow *window = GTK_WINDOW (window_title->window);
100         char *title = NULL, *password_title, *p;
101         EvPageCache *page_cache;
102
103         if (window_title->document != NULL) {
104                 char *doc_title;
105
106                 page_cache = ev_page_cache_get (window_title->document);
107                 g_return_if_fail (page_cache != NULL);
108                 doc_title = (char *)ev_page_cache_get_title (page_cache);
109
110                 /* Make sure we get a valid title back */
111                 if (doc_title != NULL) {
112                         doc_title = g_strstrip (doc_title);
113
114                         if (doc_title[0] != '\0' &&
115                             g_utf8_validate (doc_title, -1, NULL)) {
116                                 title = g_strdup (doc_title);
117                         }
118                 }
119         }
120
121         if (title && window_title->uri) {
122                 ev_window_title_sanitize_extension (window_title, &title);
123         } else {
124                 if (window_title->uri) {
125                         title = get_filename_from_uri (window_title->uri);
126                 } else {
127                         title = g_strdup (_("Document Viewer"));
128                 }
129         }
130
131         for (p = title; *p; ++p) {
132                 /* an '\n' byte is always ASCII, no need for UTF-8 special casing */
133                 if (*p == '\n') *p = ' ';
134         }
135
136         switch (window_title->type) {
137         case EV_WINDOW_TITLE_DOCUMENT:
138                 gtk_window_set_title (window, title);
139                 break;
140         case EV_WINDOW_TITLE_PASSWORD:
141                 password_title = g_strdup_printf (_("%s - Password Required"), title);
142                 gtk_window_set_title (window, password_title);
143                 g_free (password_title);
144                 break;
145         }
146
147         g_free (title);
148 }
149
150 void
151 ev_window_title_set_type (EvWindowTitle *window_title, EvWindowTitleType type)
152 {
153         window_title->type = type;
154
155         ev_window_title_update (window_title);
156 }
157
158 void
159 ev_window_title_set_document (EvWindowTitle *window_title,
160                               EvDocument    *document)
161 {
162         window_title->document = document;
163
164         ev_window_title_update (window_title);
165 }
166
167 void
168 ev_window_title_set_uri (EvWindowTitle *window_title,
169                          const char    *uri)
170 {
171         g_free (window_title->uri);
172         window_title->uri = g_strdup (uri);
173
174         ev_window_title_update (window_title);
175 }
176
177 void
178 ev_window_title_free (EvWindowTitle *window_title)
179 {
180         g_free (window_title->uri);
181         g_free (window_title);
182 }