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-document-factory.h"
23 #include <glib/gi18n.h>
24 #include <libgnomevfs/gnome-vfs-utils.h>
36 EvWindowTitleType type;
40 static const BadExtensionEntry bad_extensions[] = {
41 { EV_BACKEND_PS, ".dvi" },
42 { EV_BACKEND_PDF, ".doc" }
46 ev_window_title_new (EvWindow *window)
48 EvWindowTitle *window_title;
50 window_title = g_new0 (EvWindowTitle, 1);
51 window_title->window = window;
52 window_title->type = EV_WINDOW_TITLE_DOCUMENT;
58 ev_window_title_update (EvWindowTitle *window_title)
60 GtkWindow *window = GTK_WINDOW (window_title->window);
63 switch (window_title->type) {
64 case EV_WINDOW_TITLE_DOCUMENT:
65 gtk_window_set_title (window, window_title->title);
67 case EV_WINDOW_TITLE_PASSWORD:
68 password_title = g_strdup_printf (_("%s - Password Required"),
70 gtk_window_set_title (window, password_title);
71 g_free (password_title);
77 ev_window_title_set_type (EvWindowTitle *window_title, EvWindowTitleType type)
79 window_title->type = type;
81 ev_window_title_update (window_title);
85 get_filename_from_uri (const char *uri)
90 display_name = gnome_vfs_format_uri_for_display (uri);
91 filename = g_path_get_basename (display_name);
92 g_free (display_name);
98 ev_window_title_set_document (EvWindowTitle *window_title,
102 EvPageCache *page_cache;
106 window_title->document = document;
108 g_free (window_title->title);
110 if (document == NULL) {
111 window_title->title = NULL;
115 page_cache = ev_page_cache_get (document);
116 g_return_if_fail (page_cache != NULL);
118 title = ev_page_cache_get_title (page_cache);
120 /* Make sure we get a valid title back */
121 if (title && title[0] != '\000' && g_utf8_validate (title, -1, NULL)) {
122 window_title->title = g_strdup (title);
125 if (window_title->title) {
128 /* Some docs report titles with confusing extensions (ex. .doc for pdf).
129 Let's show the filename in this case */
130 for (i = 0; i < G_N_ELEMENTS (bad_extensions); i++) {
131 if (bad_extensions[i].backend == ev_document_factory_get_backend (document) &&
132 g_str_has_suffix (window_title->title, bad_extensions[i].ext)) {
134 char *filename = get_filename_from_uri (uri);
136 new_title = g_strdup_printf ("%s (%s)", window_title->title, filename);
137 g_free (window_title->title);
138 window_title->title = new_title;
144 for (p = window_title->title; *p; ++p) {
145 /* an '\n' byte is always ASCII, no need for UTF-8 special casing */
151 if (window_title->title == NULL && uri) {
152 window_title->title = get_filename_from_uri (uri);
155 if (window_title->title == NULL) {
156 window_title->title = g_strdup (_("Document Viewer"));
159 ev_window_title_update (window_title);
163 ev_window_title_free (EvWindowTitle *window_title)
165 g_free (window_title->title);
166 g_free (window_title);