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