]> www.fi.muni.cz Git - evince.git/blob - shell/ev-window-title.c
Add .indd files (Adobe Indesign) to the list of "bad extensions".
[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, ".indd" },
44         { EV_BACKEND_PDF, ".rtf" }
45 };
46
47 EvWindowTitle *
48 ev_window_title_new (EvWindow *window)
49 {
50         EvWindowTitle *window_title;
51
52         window_title = g_new0 (EvWindowTitle, 1);
53         window_title->window = window;
54         window_title->type = EV_WINDOW_TITLE_DOCUMENT;
55
56         return window_title;
57 }
58
59 static char *
60 get_filename_from_uri (const char *uri)
61 {
62         char *filename;
63         char *display_name;
64
65         display_name = gnome_vfs_format_uri_for_display (uri);
66         filename = g_path_get_basename (display_name);
67         g_free (display_name);
68
69         return filename;
70 }
71
72 /* Some docs report titles with confusing extensions (ex. .doc for pdf).
73    Let's show the filename in this case */
74 static void
75 ev_window_title_sanitize_extension (EvWindowTitle *window_title, char **title) {
76         EvBackend backend;
77         int i;
78
79         backend = ev_document_factory_get_backend (window_title->document);
80         for (i = 0; i < G_N_ELEMENTS (bad_extensions); i++) {
81                 if (bad_extensions[i].backend == backend &&
82                     g_str_has_suffix (*title, bad_extensions[i].ext)) {
83                         char *new_title;
84                         char *filename = get_filename_from_uri (window_title->uri);
85
86                         new_title = g_strdup_printf ("%s (%s)", *title, filename);
87                         g_free (*title);
88                         *title = new_title;
89
90                         g_free (filename);
91                 }
92         }
93 }
94
95 static void
96 ev_window_title_update (EvWindowTitle *window_title)
97 {
98         GtkWindow *window = GTK_WINDOW (window_title->window);
99         char *title = NULL, *password_title, *p;
100         EvPageCache *page_cache;
101
102         if (window_title->document != NULL) {
103                 char *doc_title;
104
105                 page_cache = ev_page_cache_get (window_title->document);
106                 g_return_if_fail (page_cache != NULL);
107                 doc_title = (char *)ev_page_cache_get_title (page_cache);
108
109                 /* Make sure we get a valid title back */
110                 if (doc_title != NULL) {
111                         doc_title = g_strstrip (doc_title);
112
113                         if (doc_title[0] != '\0' &&
114                             g_utf8_validate (doc_title, -1, NULL)) {
115                                 title = g_strdup (doc_title);
116                         }
117                 }
118         }
119
120         if (title) {
121                 ev_window_title_sanitize_extension (window_title, &title);
122         } else {
123                 if (window_title->uri) {
124                         title = get_filename_from_uri (window_title->uri);
125                 } else {
126                         title = g_strdup (_("Document Viewer"));
127                 }
128         }
129
130         for (p = title; *p; ++p) {
131                 /* an '\n' byte is always ASCII, no need for UTF-8 special casing */
132                 if (*p == '\n') *p = ' ';
133         }
134
135         switch (window_title->type) {
136         case EV_WINDOW_TITLE_DOCUMENT:
137                 gtk_window_set_title (window, title);
138                 break;
139         case EV_WINDOW_TITLE_PASSWORD:
140                 password_title = g_strdup_printf (_("%s - Password Required"), title);
141                 gtk_window_set_title (window, password_title);
142                 g_free (password_title);
143                 break;
144         }
145 }
146
147 void
148 ev_window_title_set_type (EvWindowTitle *window_title, EvWindowTitleType type)
149 {
150         window_title->type = type;
151
152         ev_window_title_update (window_title);
153 }
154
155 void
156 ev_window_title_set_document (EvWindowTitle *window_title,
157                               EvDocument    *document)
158 {
159         window_title->document = document;
160
161         ev_window_title_update (window_title);
162 }
163
164 void
165 ev_window_title_set_uri (EvWindowTitle *window_title,
166                          const char    *uri)
167 {
168         g_free (window_title->uri);
169         window_title->uri = g_strdup (uri);
170
171         ev_window_title_update (window_title);
172 }
173
174 void
175 ev_window_title_free (EvWindowTitle *window_title)
176 {
177         g_free (window_title->uri);
178         g_free (window_title);
179 }