1 /* this file is part of evince, a gnome document viewer
3 * Copyright (C) 2004 Martin Kretzschmar
6 * Martin Kretzschmar <martink@gnome.org>
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.
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.
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.
27 #include "ev-application.h"
29 #include "ev-document-types.h"
30 #include "ev-file-helpers.h"
33 #include <glib/gi18n.h>
34 #include <glib-object.h>
35 #include <gtk/gtkfilechooserdialog.h>
36 #include <gtk/gtkstock.h>
37 #include <gtk/gtkwidget.h>
38 #include <gtk/gtkmain.h>
39 #include <libgnomeui/gnome-client.h>
43 #include "ev-application-service.h"
44 #include <dbus/dbus-glib-bindings.h>
47 G_DEFINE_TYPE (EvApplication, ev_application, G_TYPE_OBJECT);
49 #define EV_APPLICATION_GET_PRIVATE(object) \
50 (G_TYPE_INSTANCE_GET_PRIVATE ((object), EV_TYPE_APPLICATION, EvApplicationPrivate))
52 #define APPLICATION_SERVICE_NAME "org.gnome.evince.ApplicationService"
56 ev_application_register_service (EvApplication *application)
58 DBusGConnection *connection;
59 DBusGProxy *driver_proxy;
61 guint request_name_result;
63 connection = dbus_g_bus_get (DBUS_BUS_STARTER, &err);
64 if (connection == NULL) {
65 g_warning ("Service registration failed.");
71 driver_proxy = dbus_g_proxy_new_for_name (connection,
76 if (!org_freedesktop_DBus_request_name (driver_proxy,
77 APPLICATION_SERVICE_NAME,
78 0, &request_name_result, &err)) {
79 g_warning ("Service registration failed.");
83 if (request_name_result == DBUS_REQUEST_NAME_REPLY_EXISTS) {
87 #if DBUS_VERSION == 33
88 dbus_g_object_class_install_info (G_OBJECT_GET_CLASS (application),
89 &dbus_glib_ev_application_object_info);
91 dbus_g_object_type_install_info (EV_TYPE_APPLICATION,
92 &dbus_glib_ev_application_object_info);
95 dbus_g_connection_register_g_object (connection,
96 "/org/gnome/evince/Evince",
97 G_OBJECT (application));
101 #endif /* ENABLE_DBUS */
104 ev_application_get_instance (void)
106 static EvApplication *instance;
109 instance = EV_APPLICATION (g_object_new (EV_TYPE_APPLICATION, NULL));
116 removed_from_session (GnomeClient *client, EvApplication *application)
118 ev_application_shutdown (application);
122 save_session (GnomeClient *client, gint phase, GnomeSaveStyle save_style, gint shutdown,
123 GnomeInteractStyle interact_style, gint fast, EvApplication *application)
129 windows = ev_application_get_windows (application);
130 restart_argv = g_new (char *, g_list_length (windows) + 1);
131 restart_argv[argc++] = g_strdup ("evince");
133 for (l = windows; l != NULL; l = l->next) {
134 EvWindow *window = EV_WINDOW (l->data);
135 restart_argv[argc++] = g_strdup (ev_window_get_uri (window));
138 gnome_client_set_restart_command (client, argc, restart_argv);
140 for (k = 0; k < argc; k++) {
141 g_free (restart_argv[k]);
144 g_list_free (windows);
145 g_free (restart_argv);
151 init_session (EvApplication *application)
155 client = gnome_master_client ();
157 g_signal_connect (client, "save_yourself",
158 G_CALLBACK (save_session), application);
159 g_signal_connect (client, "die",
160 G_CALLBACK (removed_from_session), application);
164 ev_application_open_window (EvApplication *application,
168 GtkWidget *new_window = ev_window_new ();
170 gtk_widget_show (new_window);
172 #ifdef HAVE_GTK_WINDOW_PRESENT_WITH_TIME
173 gtk_window_present_with_time (GTK_WINDOW (new_window),
176 gtk_window_present (GTK_WINDOW (new_window));
183 ev_application_get_empty_window (EvApplication *application)
185 EvWindow *empty_window = NULL;
186 GList *windows = ev_application_get_windows (application);
189 for (l = windows; l != NULL; l = l->next) {
190 EvWindow *window = EV_WINDOW (l->data);
192 if (ev_window_is_empty (window)) {
193 empty_window = window;
198 g_list_free (windows);
204 ev_application_get_uri_window (EvApplication *application, const char *uri)
206 EvWindow *uri_window = NULL;
207 GList *windows = gtk_window_list_toplevels ();
210 g_return_val_if_fail (uri != NULL, NULL);
212 for (l = windows; l != NULL; l = l->next) {
213 if (EV_IS_WINDOW (l->data)) {
214 EvWindow *window = EV_WINDOW (l->data);
215 const char *window_uri = ev_window_get_uri (window);
217 if (window_uri && strcmp (window_uri, uri) == 0 && !ev_window_is_empty (window)) {
224 g_list_free (windows);
230 ev_application_open_uri (EvApplication *application,
232 const char *page_label,
236 EvWindow *new_window;
238 g_return_val_if_fail (uri != NULL, FALSE);
240 new_window = ev_application_get_uri_window (application, uri);
241 if (new_window != NULL) {
242 #ifdef HAVE_GTK_WINDOW_PRESENT_WITH_TIME
243 gtk_window_present_with_time (GTK_WINDOW (new_window),
246 gtk_window_present (GTK_WINDOW (new_window));
251 new_window = ev_application_get_empty_window (application);
253 if (new_window == NULL) {
254 new_window = EV_WINDOW (ev_window_new ());
257 /* We need to load uri before showing the window, so
258 we can restore window size without flickering */
259 ev_window_open_uri (new_window, uri);
261 gtk_widget_show (GTK_WIDGET (new_window));
263 #ifdef HAVE_GTK_WINDOW_PRESENT_WITH_TIME
264 gtk_window_present_with_time (GTK_WINDOW (new_window),
267 gtk_window_present (GTK_WINDOW (new_window));
270 if (page_label != NULL) {
271 ev_window_open_page_label (new_window, page_label);
278 ev_application_open_uri_list (EvApplication *application,
284 for (l = uri_list; l != NULL; l = l->next) {
285 ev_application_open_uri (application, (char *)l->data,
293 ev_application_shutdown (EvApplication *application)
295 if (application->toolbars_model) {
296 g_object_unref (application->toolbars_model);
297 g_free (application->toolbars_file);
298 application->toolbars_model = NULL;
299 application->toolbars_file = NULL;
302 if (application->recent_model) {
303 g_object_unref (application->recent_model);
304 application->recent_model = NULL;
307 g_object_unref (application);
312 ev_application_class_init (EvApplicationClass *ev_application_class)
317 ev_application_init (EvApplication *ev_application)
319 init_session (ev_application);
321 ev_application->toolbars_model = egg_toolbars_model_new ();
323 ev_application->toolbars_file = g_build_filename
324 (ev_dot_dir (), "evince_toolbar.xml", NULL);
326 if (!egg_toolbars_model_load (ev_application->toolbars_model,
327 ev_application->toolbars_file)) {
328 egg_toolbars_model_load (ev_application->toolbars_model,
329 DATADIR"/evince-toolbar.xml");
332 egg_toolbars_model_set_flags (ev_application->toolbars_model, 0,
333 EGG_TB_MODEL_NOT_REMOVABLE);
335 ev_application->recent_model = egg_recent_model_new (EGG_RECENT_MODEL_SORT_MRU);
336 /* FIXME we should add a mime type filter but current eggrecent
337 has only a varargs style api which does not work well when
338 the list of mime types is dynamic */
339 egg_recent_model_set_limit (ev_application->recent_model, 5);
340 egg_recent_model_set_filter_groups (ev_application->recent_model,
345 ev_application_get_windows (EvApplication *application)
347 GList *l, *toplevels;
348 GList *windows = NULL;
350 toplevels = gtk_window_list_toplevels ();
352 for (l = toplevels; l != NULL; l = l->next) {
353 if (EV_IS_WINDOW (l->data)) {
354 windows = g_list_append (windows, l->data);
358 g_list_free (toplevels);
363 EggToolbarsModel *ev_application_get_toolbars_model (EvApplication *application)
365 return application->toolbars_model;
368 EggRecentModel *ev_application_get_recent_model (EvApplication *application)
370 return application->recent_model;
373 void ev_application_save_toolbars_model (EvApplication *application)
375 egg_toolbars_model_save (application->toolbars_model,
376 application->toolbars_file, "1.0");