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-file-helpers.h"
30 #include "ev-document-factory.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 DBUS_VERSION >= 60
77 if (!org_freedesktop_DBus_request_name (driver_proxy,
78 APPLICATION_SERVICE_NAME,
79 DBUS_NAME_FLAG_DO_NOT_QUEUE,
80 &request_name_result, &err)) {
81 g_warning ("Service registration failed.");
85 if (!org_freedesktop_DBus_request_name (driver_proxy,
86 APPLICATION_SERVICE_NAME,
87 0, &request_name_result, &err)) {
88 g_warning ("Service registration failed.");
93 if (request_name_result == DBUS_REQUEST_NAME_REPLY_EXISTS) {
97 #if DBUS_VERSION == 33
98 dbus_g_object_class_install_info (G_OBJECT_GET_CLASS (application),
99 &dbus_glib_ev_application_object_info);
101 dbus_g_object_type_install_info (EV_TYPE_APPLICATION,
102 &dbus_glib_ev_application_object_info);
105 dbus_g_connection_register_g_object (connection,
106 "/org/gnome/evince/Evince",
107 G_OBJECT (application));
111 #endif /* ENABLE_DBUS */
114 ev_application_get_instance (void)
116 static EvApplication *instance;
119 instance = EV_APPLICATION (g_object_new (EV_TYPE_APPLICATION, NULL));
126 removed_from_session (GnomeClient *client, EvApplication *application)
128 ev_application_shutdown (application);
132 save_session (GnomeClient *client, gint phase, GnomeSaveStyle save_style, gint shutdown,
133 GnomeInteractStyle interact_style, gint fast, EvApplication *application)
139 windows = ev_application_get_windows (application);
140 restart_argv = g_new (char *, g_list_length (windows) + 1);
141 restart_argv[argc++] = g_strdup ("evince");
143 for (l = windows; l != NULL; l = l->next) {
144 EvWindow *window = EV_WINDOW (l->data);
145 restart_argv[argc++] = g_strdup (ev_window_get_uri (window));
148 gnome_client_set_restart_command (client, argc, restart_argv);
150 for (k = 0; k < argc; k++) {
151 g_free (restart_argv[k]);
154 g_list_free (windows);
155 g_free (restart_argv);
161 init_session (EvApplication *application)
165 client = gnome_master_client ();
167 g_signal_connect (client, "save_yourself",
168 G_CALLBACK (save_session), application);
169 g_signal_connect (client, "die",
170 G_CALLBACK (removed_from_session), application);
174 ev_application_open_window (EvApplication *application,
178 GtkWidget *new_window = ev_window_new ();
180 gtk_widget_show (new_window);
182 #ifdef HAVE_GTK_WINDOW_PRESENT_WITH_TIME
183 gtk_window_present_with_time (GTK_WINDOW (new_window),
186 gtk_window_present (GTK_WINDOW (new_window));
193 ev_application_get_empty_window (EvApplication *application)
195 EvWindow *empty_window = NULL;
196 GList *windows = ev_application_get_windows (application);
199 for (l = windows; l != NULL; l = l->next) {
200 EvWindow *window = EV_WINDOW (l->data);
202 if (ev_window_is_empty (window)) {
203 empty_window = window;
208 g_list_free (windows);
214 ev_application_get_uri_window (EvApplication *application, const char *uri)
216 EvWindow *uri_window = NULL;
217 GList *windows = gtk_window_list_toplevels ();
220 g_return_val_if_fail (uri != NULL, NULL);
222 for (l = windows; l != NULL; l = l->next) {
223 if (EV_IS_WINDOW (l->data)) {
224 EvWindow *window = EV_WINDOW (l->data);
225 const char *window_uri = ev_window_get_uri (window);
227 if (window_uri && strcmp (window_uri, uri) == 0 && !ev_window_is_empty (window)) {
234 g_list_free (windows);
240 ev_application_open_uri (EvApplication *application,
242 const char *page_label,
246 EvWindow *new_window;
248 g_return_val_if_fail (uri != NULL, FALSE);
250 new_window = ev_application_get_uri_window (application, uri);
251 if (new_window != NULL) {
252 #ifdef HAVE_GTK_WINDOW_PRESENT_WITH_TIME
253 gtk_window_present_with_time (GTK_WINDOW (new_window),
256 gtk_window_present (GTK_WINDOW (new_window));
261 new_window = ev_application_get_empty_window (application);
263 if (new_window == NULL) {
264 new_window = EV_WINDOW (ev_window_new ());
267 /* We need to load uri before showing the window, so
268 we can restore window size without flickering */
269 ev_window_open_uri (new_window, uri);
271 gtk_widget_show (GTK_WIDGET (new_window));
273 #ifdef HAVE_GTK_WINDOW_PRESENT_WITH_TIME
274 gtk_window_present_with_time (GTK_WINDOW (new_window),
277 gtk_window_present (GTK_WINDOW (new_window));
280 if (page_label != NULL) {
281 ev_window_open_page_label (new_window, page_label);
288 ev_application_open_uri_list (EvApplication *application,
294 for (l = uri_list; l != NULL; l = l->next) {
295 ev_application_open_uri (application, (char *)l->data,
303 ev_application_shutdown (EvApplication *application)
305 if (application->toolbars_model) {
306 g_object_unref (application->toolbars_model);
307 g_free (application->toolbars_file);
308 application->toolbars_model = NULL;
309 application->toolbars_file = NULL;
312 if (application->recent_model) {
313 g_object_unref (application->recent_model);
314 application->recent_model = NULL;
317 g_free (application->last_chooser_uri);
318 g_object_unref (application);
324 ev_application_class_init (EvApplicationClass *ev_application_class)
329 ev_application_init (EvApplication *ev_application)
331 init_session (ev_application);
333 ev_application->toolbars_model = egg_toolbars_model_new ();
335 ev_application->toolbars_file = g_build_filename
336 (ev_dot_dir (), "evince_toolbar.xml", NULL);
338 if (!egg_toolbars_model_load (ev_application->toolbars_model,
339 ev_application->toolbars_file)) {
340 egg_toolbars_model_load (ev_application->toolbars_model,
341 DATADIR"/evince-toolbar.xml");
344 egg_toolbars_model_set_flags (ev_application->toolbars_model, 0,
345 EGG_TB_MODEL_NOT_REMOVABLE);
347 ev_application->recent_model = egg_recent_model_new (EGG_RECENT_MODEL_SORT_MRU);
348 /* FIXME we should add a mime type filter but current eggrecent
349 has only a varargs style api which does not work well when
350 the list of mime types is dynamic */
351 egg_recent_model_set_limit (ev_application->recent_model, 5);
352 egg_recent_model_set_filter_groups (ev_application->recent_model,
357 ev_application_get_windows (EvApplication *application)
359 GList *l, *toplevels;
360 GList *windows = NULL;
362 toplevels = gtk_window_list_toplevels ();
364 for (l = toplevels; l != NULL; l = l->next) {
365 if (EV_IS_WINDOW (l->data)) {
366 windows = g_list_append (windows, l->data);
370 g_list_free (toplevels);
375 EggToolbarsModel *ev_application_get_toolbars_model (EvApplication *application)
377 return application->toolbars_model;
380 EggRecentModel *ev_application_get_recent_model (EvApplication *application)
382 return application->recent_model;
385 void ev_application_save_toolbars_model (EvApplication *application)
387 egg_toolbars_model_save (application->toolbars_model,
388 application->toolbars_file, "1.0");
391 void ev_application_set_chooser_uri (EvApplication *application, gchar *uri)
393 g_free (application->last_chooser_uri);
394 application->last_chooser_uri = g_strdup (uri);
397 const gchar* ev_application_get_chooser_uri (EvApplication *application)
399 return application->last_chooser_uri;