1 /* this file is part of evince, a gnome document viewer
3 * Copyright (C) 2005 Red Hat, Inc
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.
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.
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.
21 #include "ev-window-title.h"
22 #include "ev-backends-manager.h"
24 #include <glib/gi18n.h>
25 #include <libgnomevfs/gnome-vfs-utils.h>
27 /* Known backends (for bad extensions fix) */
28 #define EV_BACKEND_PS "psdocument"
29 #define EV_BACKEND_PDF "pdfdocument"
40 EvWindowTitleType type;
45 static const BadExtensionEntry bad_extensions[] = {
46 { EV_BACKEND_PS, ".dvi" },
47 { EV_BACKEND_PDF, ".doc" },
48 { EV_BACKEND_PDF, ".dvi" },
49 { EV_BACKEND_PDF, ".indd" },
50 { EV_BACKEND_PDF, ".rtf" }
54 ev_window_title_new (EvWindow *window)
56 EvWindowTitle *window_title;
58 window_title = g_new0 (EvWindowTitle, 1);
59 window_title->window = window;
60 window_title->type = EV_WINDOW_TITLE_DOCUMENT;
66 get_filename_from_uri (const char *uri)
71 display_name = gnome_vfs_format_uri_for_display (uri);
72 filename = g_path_get_basename (display_name);
73 g_free (display_name);
78 /* Some docs report titles with confusing extensions (ex. .doc for pdf).
79 Let's show the filename in this case */
81 ev_window_title_sanitize_extension (EvWindowTitle *window_title, char **title) {
85 backend = ev_backends_manager_get_document_module_name (window_title->document);
87 for (i = 0; i < G_N_ELEMENTS (bad_extensions); i++) {
88 if (g_ascii_strcasecmp (bad_extensions[i].backend, backend) == 0 &&
89 g_str_has_suffix (*title, bad_extensions[i].ext)) {
91 char *filename = get_filename_from_uri (window_title->uri);
93 new_title = g_strdup_printf ("%s (%s)", *title, filename);
103 ev_window_title_update (EvWindowTitle *window_title)
105 GtkWindow *window = GTK_WINDOW (window_title->window);
106 char *title = NULL, *password_title, *p;
107 EvPageCache *page_cache;
109 if (window_title->document != NULL) {
112 page_cache = ev_page_cache_get (window_title->document);
113 g_return_if_fail (page_cache != NULL);
114 doc_title = (char *)ev_page_cache_get_title (page_cache);
116 /* Make sure we get a valid title back */
117 if (doc_title != NULL) {
118 doc_title = g_strstrip (doc_title);
120 if (doc_title[0] != '\0' &&
121 g_utf8_validate (doc_title, -1, NULL)) {
122 title = g_strdup (doc_title);
127 if (title && window_title->uri) {
128 ev_window_title_sanitize_extension (window_title, &title);
129 } else if (window_title->uri) {
130 title = get_filename_from_uri (window_title->uri);
132 title = g_strdup (_("Document Viewer"));
135 for (p = title; *p; ++p) {
136 /* an '\n' byte is always ASCII, no need for UTF-8 special casing */
137 if (*p == '\n') *p = ' ';
140 switch (window_title->type) {
141 case EV_WINDOW_TITLE_DOCUMENT:
142 gtk_window_set_title (window, title);
144 case EV_WINDOW_TITLE_PASSWORD:
145 password_title = g_strdup_printf (_("%s - Password Required"), title);
146 gtk_window_set_title (window, password_title);
147 g_free (password_title);
155 ev_window_title_set_type (EvWindowTitle *window_title, EvWindowTitleType type)
157 window_title->type = type;
159 ev_window_title_update (window_title);
163 ev_window_title_set_document (EvWindowTitle *window_title,
164 EvDocument *document)
166 window_title->document = document;
168 ev_window_title_update (window_title);
172 ev_window_title_set_uri (EvWindowTitle *window_title,
175 g_free (window_title->uri);
176 window_title->uri = g_strdup (uri);
178 ev_window_title_update (window_title);
182 ev_window_title_free (EvWindowTitle *window_title)
184 g_free (window_title->uri);
185 g_free (window_title);