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.
20 #include "ev-window-title.h"
21 #include "ev-backends-manager.h"
23 #include <glib/gi18n.h>
24 #include <libgnomevfs/gnome-vfs-utils.h>
26 /* Known backends (for bad extensions fix) */
27 #define EV_BACKEND_PS "psdocument"
28 #define EV_BACKEND_PDF "pdfdocument"
39 EvWindowTitleType type;
44 static const BadExtensionEntry bad_extensions[] = {
45 { EV_BACKEND_PS, ".dvi" },
46 { EV_BACKEND_PDF, ".doc" },
47 { EV_BACKEND_PDF, ".dvi" },
48 { EV_BACKEND_PDF, ".indd" },
49 { EV_BACKEND_PDF, ".rtf" }
53 ev_window_title_new (EvWindow *window)
55 EvWindowTitle *window_title;
57 window_title = g_new0 (EvWindowTitle, 1);
58 window_title->window = window;
59 window_title->type = EV_WINDOW_TITLE_DOCUMENT;
65 get_filename_from_uri (const char *uri)
70 display_name = gnome_vfs_format_uri_for_display (uri);
71 filename = g_path_get_basename (display_name);
72 g_free (display_name);
77 /* Some docs report titles with confusing extensions (ex. .doc for pdf).
78 Let's show the filename in this case */
80 ev_window_title_sanitize_extension (EvWindowTitle *window_title, char **title) {
84 backend = ev_backends_manager_get_document_module_name (window_title->document);
86 for (i = 0; i < G_N_ELEMENTS (bad_extensions); i++) {
87 if (g_ascii_strcasecmp (bad_extensions[i].backend, backend) == 0 &&
88 g_str_has_suffix (*title, bad_extensions[i].ext)) {
90 char *filename = get_filename_from_uri (window_title->uri);
92 new_title = g_strdup_printf ("%s (%s)", *title, filename);
102 ev_window_title_update (EvWindowTitle *window_title)
104 GtkWindow *window = GTK_WINDOW (window_title->window);
105 char *title = NULL, *password_title, *p;
106 EvPageCache *page_cache;
108 if (window_title->document != NULL) {
111 page_cache = ev_page_cache_get (window_title->document);
112 g_return_if_fail (page_cache != NULL);
113 doc_title = (char *)ev_page_cache_get_title (page_cache);
115 /* Make sure we get a valid title back */
116 if (doc_title != NULL) {
117 doc_title = g_strstrip (doc_title);
119 if (doc_title[0] != '\0' &&
120 g_utf8_validate (doc_title, -1, NULL)) {
121 title = g_strdup (doc_title);
126 if (title && window_title->uri) {
127 ev_window_title_sanitize_extension (window_title, &title);
128 } else if (window_title->uri) {
129 title = get_filename_from_uri (window_title->uri);
131 title = g_strdup (_("Document Viewer"));
134 for (p = title; *p; ++p) {
135 /* an '\n' byte is always ASCII, no need for UTF-8 special casing */
136 if (*p == '\n') *p = ' ';
139 switch (window_title->type) {
140 case EV_WINDOW_TITLE_DOCUMENT:
141 gtk_window_set_title (window, title);
143 case EV_WINDOW_TITLE_PASSWORD:
144 password_title = g_strdup_printf (_("%s - Password Required"), title);
145 gtk_window_set_title (window, password_title);
146 g_free (password_title);
154 ev_window_title_set_type (EvWindowTitle *window_title, EvWindowTitleType type)
156 window_title->type = type;
158 ev_window_title_update (window_title);
162 ev_window_title_set_document (EvWindowTitle *window_title,
163 EvDocument *document)
165 window_title->document = document;
167 ev_window_title_update (window_title);
171 ev_window_title_set_uri (EvWindowTitle *window_title,
174 g_free (window_title->uri);
175 window_title->uri = g_strdup (uri);
177 ev_window_title_update (window_title);
181 ev_window_title_free (EvWindowTitle *window_title)
183 g_free (window_title->uri);
184 g_free (window_title);