]> www.fi.muni.cz Git - evince.git/blob - shell/ev-window-title.c
Factor out and cleanup window title code
[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
22 #include <glib/gi18n.h>
23 #include <libgnomevfs/gnome-vfs-utils.h>
24
25 struct _EvWindowTitle
26 {
27         EvWindow *window;
28         EvDocument *document;
29         EvWindowTitleType type;
30         char *title;
31 };
32
33 EvWindowTitle *
34 ev_window_title_new (EvWindow *window)
35 {
36         EvWindowTitle *window_title;
37
38         window_title = g_new0 (EvWindowTitle, 1);
39         window_title->window = window;
40         window_title->type = EV_WINDOW_TITLE_DOCUMENT;
41
42         return window_title;
43 }
44
45 static void
46 ev_window_title_update (EvWindowTitle *window_title)
47 {
48         GtkWindow *window = GTK_WINDOW (window_title->window);
49         char *password_title;
50
51         switch (window_title->type) {
52         case EV_WINDOW_TITLE_DOCUMENT:
53                 gtk_window_set_title (window, window_title->title);
54                 break;
55         case EV_WINDOW_TITLE_PASSWORD:
56                 password_title = g_strdup_printf (_("%s - Password Required"),
57                                                   window_title->title);
58                 gtk_window_set_title (window, password_title);
59                 g_free (password_title);
60                 break;
61         }
62 }
63
64 void
65 ev_window_title_set_type (EvWindowTitle *window_title, EvWindowTitleType type)
66 {
67         window_title->type = type;
68
69         ev_window_title_update (window_title);
70 }
71
72 void
73 ev_window_title_set_document (EvWindowTitle *window_title,
74                               EvDocument    *document,
75                               const char    *uri)
76 {
77         EvPageCache *page_cache;
78         const char *title;
79
80         window_title->document = document;
81
82         g_free (window_title->title);
83
84         if (document == NULL) {
85                 window_title->title = NULL;
86                 return;
87         }
88
89         page_cache = ev_page_cache_get (document);
90         g_return_if_fail (page_cache != NULL);
91
92         title = ev_page_cache_get_title (page_cache);
93
94         /* Make sure we get a valid title back */
95         if (title && title[0] != '\000' && g_utf8_validate (title, -1, NULL)) {
96                 window_title->title = g_strdup (title);
97         }
98
99         if (window_title->title) {
100                 char *p;
101
102                 for (p = window_title->title; *p; ++p) {
103                         /* an '\n' byte is always ASCII, no need for UTF-8 special casing */
104                         if (*p == '\n')
105                                 *p = ' ';
106                 }
107         }
108
109         if (window_title->title == NULL && uri) {
110                 char *display_name;
111
112                 display_name = gnome_vfs_format_uri_for_display (uri);
113                 window_title->title = g_path_get_basename (display_name);
114                 g_free (display_name);
115         }
116
117         if (window_title->title == NULL) {
118                 window_title->title = g_strdup (_("Document Viewer"));
119         }
120
121         ev_window_title_update (window_title);
122 }
123
124 void
125 ev_window_title_free (EvWindowTitle *window_title)
126 {
127         g_free (window_title->title);
128         g_free (window_title);
129 }