]> www.fi.muni.cz Git - evince.git/blob - shell/ev-application.c
Transfer remote documents to tmp directory to display them later
[evince.git] / shell / ev-application.c
1 /* this file is part of evince, a gnome document viewer
2  *
3  *  Copyright (C) 2004 Martin Kretzschmar
4  *
5  *  Author:
6  *    Martin Kretzschmar <martink@gnome.org>
7  *
8  * Evince is free software; you can redistribute it and/or modify it
9  * under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * Evince is distributed in the hope that it will be useful, but
14  * WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
21  */
22
23 #ifdef HAVE_CONFIG_H
24 #include "config.h"
25 #endif
26
27 #include "ev-application.h"
28
29 #include <glib.h>
30 #include <glib/gi18n.h>
31 #include <glib-object.h>
32 #include <gtk/gtkfilechooserdialog.h>
33 #include <gtk/gtkstock.h>
34 #include <gtk/gtkwidget.h>
35 #include <gtk/gtkmain.h>
36
37 struct _EvApplicationPrivate {
38         GList *windows;
39 };
40
41 G_DEFINE_TYPE (EvApplication, ev_application, G_TYPE_OBJECT);
42
43 #define EV_APPLICATION_GET_PRIVATE(object) \
44         (G_TYPE_INSTANCE_GET_PRIVATE ((object), EV_TYPE_APPLICATION, EvApplicationPrivate))
45
46 EvApplication *
47 ev_application_get_instance (void)
48 {
49         static EvApplication *instance;
50
51         if (!instance)
52                 instance = EV_APPLICATION (
53                         g_object_new (EV_TYPE_APPLICATION, NULL));
54
55         return instance;
56 }
57
58 static void
59 window_destroy_cb (GtkObject *object, gpointer user_data)
60 {
61         EvApplication *application;
62         
63         g_return_if_fail (EV_IS_WINDOW (object));
64         g_return_if_fail (EV_IS_APPLICATION (user_data));
65
66         application = EV_APPLICATION (user_data);
67         application->priv->windows =
68                 g_list_remove (application->priv->windows, object);
69
70         if (application->priv->windows == NULL)
71                 gtk_main_quit ();
72 }
73
74 EvWindow *
75 ev_application_new_window (EvApplication *application)
76 {
77         EvWindow *ev_window;
78
79         ev_window = EV_WINDOW (g_object_new (EV_TYPE_WINDOW,
80                                              "type", GTK_WINDOW_TOPLEVEL,
81                                              "default-height", 600,
82                                              "default-width", 600,
83                                              NULL));
84         application->priv->windows =
85                 g_list_prepend (application->priv->windows, ev_window);
86         g_signal_connect (G_OBJECT (ev_window), "destroy",
87                           G_CALLBACK (window_destroy_cb), application);
88
89         return ev_window;
90 }
91
92 static int
93 is_window_empty (const EvWindow *ev_window, gconstpointer dummy)
94 {
95         g_return_val_if_fail (EV_IS_WINDOW (ev_window), 0);
96
97         return ev_window_is_empty (ev_window)
98                 ? 0
99                 : -1;
100 }
101
102 EvWindow *
103 ev_application_get_empty_window (EvApplication *application)
104 {
105         GList *node;
106
107         node = g_list_find_custom (application->priv->windows, NULL,
108                                    (GCompareFunc)is_window_empty);
109
110         return node && node->data
111                 ? EV_WINDOW (node->data)
112                 : ev_application_new_window (application);
113 }
114
115 void
116 ev_application_open (EvApplication *application, GError *err)
117 {
118         EvWindow *ev_window;
119         GtkWidget *chooser;
120         GtkFileFilter *documents_filter;
121         GtkFileFilter *pdf_filter;
122         GtkFileFilter *ps_filter;
123         GtkFileFilter *pixbuf_filter;
124         GtkFileFilter *all_filter;
125         static gchar  *folder = NULL;
126 #ifdef ENABLE_DJVU
127         GtkFileFilter *djvu_filter;
128 #endif
129 #ifdef ENABLE_DVI
130         GtkFileFilter *dvi_filter;
131 #endif
132
133         ev_window = ev_application_get_empty_window (application);
134
135         chooser = gtk_file_chooser_dialog_new (_("Open document"),
136                                                GTK_WINDOW (ev_window),
137                                                GTK_FILE_CHOOSER_ACTION_OPEN,
138                                                GTK_STOCK_CANCEL,
139                                                GTK_RESPONSE_CANCEL,
140                                                GTK_STOCK_OPEN, GTK_RESPONSE_OK,
141                                                NULL);
142
143         if (folder) {
144                 gtk_file_chooser_set_current_folder (GTK_FILE_CHOOSER (chooser),
145                                                      folder);
146         }
147
148         documents_filter = gtk_file_filter_new ();
149         gtk_file_filter_set_name (documents_filter,
150                                   _("All Documents"));
151         gtk_file_filter_add_mime_type (documents_filter, "application/postscript");
152         gtk_file_filter_add_mime_type (documents_filter, "application/x-gzpostscript");
153         gtk_file_filter_add_mime_type (documents_filter, "image/x-eps");
154         gtk_file_filter_add_mime_type (documents_filter, "application/pdf");
155 #ifdef ENABLE_DVI
156         gtk_file_filter_add_mime_type (documents_filter, "application/x-dvi");
157 #endif
158         gtk_file_filter_add_pixbuf_formats (documents_filter);
159 #ifdef ENABLE_DJVU
160         gtk_file_filter_add_mime_type (documents_filter, "image/vnd.djvu");
161 #endif
162         gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (chooser), documents_filter);
163
164         ps_filter = gtk_file_filter_new ();
165         gtk_file_filter_set_name (ps_filter, _("PostScript Documents"));
166         gtk_file_filter_add_mime_type (ps_filter, "application/postscript");
167         gtk_file_filter_add_mime_type (ps_filter, "application/x-gzpostscript");
168         gtk_file_filter_add_mime_type (ps_filter, "image/x-eps");
169         gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (chooser), ps_filter);
170
171         pdf_filter = gtk_file_filter_new ();
172         gtk_file_filter_set_name (pdf_filter, _("PDF Documents"));
173         gtk_file_filter_add_mime_type (pdf_filter, "application/pdf");
174         gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (chooser), pdf_filter);
175
176 #ifdef ENABLE_DVI
177         dvi_filter = gtk_file_filter_new ();
178         gtk_file_filter_set_name (dvi_filter, _("DVI Documents"));
179         gtk_file_filter_add_mime_type (dvi_filter, "application/x-dvi");
180         gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (chooser), dvi_filter);
181 #endif
182
183         pixbuf_filter = gtk_file_filter_new ();
184         gtk_file_filter_set_name (pixbuf_filter, _("Images"));
185         gtk_file_filter_add_pixbuf_formats (pixbuf_filter);
186         gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (chooser), pixbuf_filter);
187
188 #ifdef ENABLE_DJVU
189         djvu_filter = gtk_file_filter_new ();
190         gtk_file_filter_set_name (djvu_filter, _("Djvu Documents"));
191         gtk_file_filter_add_mime_type (djvu_filter, "image/vnd.djvu");
192         gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (chooser), djvu_filter);
193 #endif  
194         
195         all_filter = gtk_file_filter_new ();
196         gtk_file_filter_set_name (all_filter, _("All Files"));
197         gtk_file_filter_add_pattern (all_filter, "*");
198
199         gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (chooser), all_filter);
200         gtk_file_chooser_set_filter (GTK_FILE_CHOOSER (chooser), documents_filter);
201
202         gtk_file_chooser_set_select_multiple (GTK_FILE_CHOOSER (chooser), TRUE);
203         gtk_file_chooser_set_local_only (GTK_FILE_CHOOSER (chooser), FALSE);
204
205         if (gtk_dialog_run (GTK_DIALOG (chooser)) == GTK_RESPONSE_OK) {
206                 GSList *uris;
207
208                 uris = gtk_file_chooser_get_uris (GTK_FILE_CHOOSER (chooser));
209                 folder = gtk_file_chooser_get_current_folder (GTK_FILE_CHOOSER (chooser));
210
211                 ev_window_open_uri_list (ev_window, uris);
212                 
213                 g_slist_free (uris);
214         } else {
215                 if (!GTK_WIDGET_VISIBLE (ev_window))
216                         gtk_widget_destroy (GTK_WIDGET (ev_window));
217         }
218
219         gtk_widget_destroy (GTK_WIDGET (chooser));
220 }
221
222 static void
223 ev_application_class_init (EvApplicationClass *ev_application_class)
224 {
225         GObjectClass *g_object_class;
226
227         g_object_class = G_OBJECT_CLASS (ev_application_class);
228
229         g_type_class_add_private (g_object_class,
230                                   sizeof (EvApplicationPrivate));
231 }
232
233 static void
234 ev_application_init (EvApplication *ev_application)
235 {
236         ev_application->priv = EV_APPLICATION_GET_PRIVATE (ev_application);
237 }
238