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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 #include "ev-window-title.h"
22 #include "ev-backends-manager.h"
27 #include <glib/gi18n.h>
29 /* Known backends (for bad extensions fix) */
30 #define EV_BACKEND_PS "psdocument"
31 #define EV_BACKEND_PDF "pdfdocument"
42 EvWindowTitleType type;
47 static const BadTitleEntry bad_extensions[] = {
48 { EV_BACKEND_PS, ".dvi" },
49 { EV_BACKEND_PDF, ".doc" },
50 { EV_BACKEND_PDF, ".dvi" },
51 { EV_BACKEND_PDF, ".indd" },
52 { EV_BACKEND_PDF, ".rtf" }
55 static const BadTitleEntry bad_prefixes[] = {
56 { EV_BACKEND_PDF, "Microsoft Word - " },
57 { EV_BACKEND_PDF, "Microsoft PowerPoint - " }
61 ev_window_title_new (EvWindow *window)
63 EvWindowTitle *window_title;
65 window_title = g_new0 (EvWindowTitle, 1);
66 window_title->window = window;
67 window_title->type = EV_WINDOW_TITLE_DOCUMENT;
73 get_filename_from_uri (const char *uri)
78 filename = g_uri_unescape_string (uri, NULL);
79 basename = g_path_get_basename (filename);
85 /* Some docs report titles with confusing extensions (ex. .doc for pdf).
86 Erase the confusing extension of the title */
88 ev_window_title_sanitize_title (EvWindowTitle *window_title, char **title) {
92 backend = ev_backends_manager_get_document_module_name (window_title->document);
94 for (i = 0; i < G_N_ELEMENTS (bad_extensions); i++) {
95 if (g_ascii_strcasecmp (bad_extensions[i].backend, backend) == 0 &&
96 g_str_has_suffix (*title, bad_extensions[i].text)) {
98 char *filename = get_filename_from_uri (window_title->uri);
100 new_title = g_strndup (*title, strlen(*title) - strlen(bad_extensions[i].text));
107 for (i = 0; i < G_N_ELEMENTS (bad_prefixes); i++) {
108 if (g_ascii_strcasecmp (bad_prefixes[i].backend, backend) == 0 &&
109 g_str_has_prefix (*title, bad_prefixes[i].text)) {
111 int len = strlen(bad_prefixes[i].text);
113 new_title = g_strdup_printf ("%s", (*title) + len);
121 ev_window_title_update (EvWindowTitle *window_title)
123 GtkWindow *window = GTK_WINDOW (window_title->window);
124 char *title = NULL, *password_title, *p;
126 if (window_title->document != NULL) {
129 doc_title = g_strdup (ev_document_get_title (window_title->document));
131 /* Make sure we get a valid title back */
132 if (doc_title != NULL) {
133 doc_title = g_strstrip (doc_title);
135 if (doc_title[0] != '\0' &&
136 g_utf8_validate (doc_title, -1, NULL)) {
137 title = g_strdup (doc_title);
144 if (title && window_title->uri) {
146 char *filename = get_filename_from_uri (window_title->uri);
148 ev_window_title_sanitize_title (window_title, &title);
149 tmp_title = g_strdup_printf ("%s — %s", filename, title);
154 } else if (window_title->uri) {
155 title = get_filename_from_uri (window_title->uri);
157 title = g_strdup (_("Document Viewer"));
160 for (p = title; *p; ++p) {
161 /* an '\n' byte is always ASCII, no need for UTF-8 special casing */
162 if (*p == '\n') *p = ' ';
165 switch (window_title->type) {
166 case EV_WINDOW_TITLE_DOCUMENT:
167 gtk_window_set_title (window, title);
169 case EV_WINDOW_TITLE_PASSWORD:
170 password_title = g_strdup_printf (_("%s — Password Required"), title);
171 gtk_window_set_title (window, password_title);
172 g_free (password_title);
180 ev_window_title_set_type (EvWindowTitle *window_title, EvWindowTitleType type)
182 window_title->type = type;
184 ev_window_title_update (window_title);
188 ev_window_title_set_document (EvWindowTitle *window_title,
189 EvDocument *document)
191 window_title->document = document;
193 ev_window_title_update (window_title);
197 ev_window_title_set_uri (EvWindowTitle *window_title,
200 g_free (window_title->uri);
201 window_title->uri = g_strdup (uri);
203 ev_window_title_update (window_title);
207 ev_window_title_free (EvWindowTitle *window_title)
209 g_free (window_title->uri);
210 g_free (window_title);