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 APPLICATION_SERVICE_NAME "org.gnome.evince.ApplicationService"
53 ev_application_register_service (EvApplication *application)
55 static DBusGConnection *connection = NULL;
56 DBusGProxy *driver_proxy;
58 guint request_name_result;
61 g_warning ("Service already registered.");
65 connection = dbus_g_bus_get (DBUS_BUS_STARTER, &err);
66 if (connection == NULL) {
67 g_warning ("Service registration failed.");
73 driver_proxy = dbus_g_proxy_new_for_name (connection,
78 #if DBUS_VERSION >= 60
79 if (!org_freedesktop_DBus_request_name (driver_proxy,
80 APPLICATION_SERVICE_NAME,
81 DBUS_NAME_FLAG_DO_NOT_QUEUE,
82 &request_name_result, &err)) {
83 g_warning ("Service registration failed.");
87 if (!org_freedesktop_DBus_request_name (driver_proxy,
88 APPLICATION_SERVICE_NAME,
89 0, &request_name_result, &err)) {
90 g_warning ("Service registration failed.");
95 g_object_unref (driver_proxy);
97 if (request_name_result == DBUS_REQUEST_NAME_REPLY_EXISTS) {
101 #if DBUS_VERSION == 33
102 dbus_g_object_class_install_info (G_OBJECT_GET_CLASS (application),
103 &dbus_glib_ev_application_object_info);
105 dbus_g_object_type_install_info (EV_TYPE_APPLICATION,
106 &dbus_glib_ev_application_object_info);
109 dbus_g_connection_register_g_object (connection,
110 "/org/gnome/evince/Evince",
111 G_OBJECT (application));
115 #endif /* ENABLE_DBUS */
118 ev_application_get_instance (void)
120 static EvApplication *instance;
123 instance = EV_APPLICATION (g_object_new (EV_TYPE_APPLICATION, NULL));
130 removed_from_session (GnomeClient *client, EvApplication *application)
132 ev_application_shutdown (application);
136 save_session (GnomeClient *client, gint phase, GnomeSaveStyle save_style, gint shutdown,
137 GnomeInteractStyle interact_style, gint fast, EvApplication *application)
143 windows = ev_application_get_windows (application);
144 restart_argv = g_new (char *, g_list_length (windows) + 1);
145 restart_argv[argc++] = g_strdup ("evince");
147 for (l = windows; l != NULL; l = l->next) {
148 EvWindow *window = EV_WINDOW (l->data);
149 restart_argv[argc++] = g_strdup (ev_window_get_uri (window));
152 gnome_client_set_restart_command (client, argc, restart_argv);
154 for (k = 0; k < argc; k++) {
155 g_free (restart_argv[k]);
158 g_list_free (windows);
159 g_free (restart_argv);
165 init_session (EvApplication *application)
169 client = gnome_master_client ();
171 g_signal_connect (client, "save_yourself",
172 G_CALLBACK (save_session), application);
173 g_signal_connect (client, "die",
174 G_CALLBACK (removed_from_session), application);
178 ev_display_open_if_needed (const gchar *name)
182 GdkDisplay *display = NULL;
184 displays = gdk_display_manager_list_displays (gdk_display_manager_get ());
186 for (l = displays; l != NULL; l = l->next) {
187 const gchar *display_name = gdk_display_get_name ((GdkDisplay *) l->data);
189 if (g_ascii_strcasecmp (display_name, name) == 0) {
195 g_slist_free (displays);
197 return display != NULL ? display : gdk_display_open (name);
201 get_screen_from_args (GHashTable *args)
203 GValue *value = NULL;
204 GdkDisplay *display = NULL;
205 GdkScreen *screen = NULL;
207 g_assert (args != NULL);
209 value = g_hash_table_lookup (args, "display");
211 const gchar *display_name;
213 display_name = g_value_get_string (value);
214 display = ev_display_open_if_needed (display_name);
217 value = g_hash_table_lookup (args, "screen");
221 screen_number = g_value_get_int (value);
222 screen = gdk_display_get_screen (display, screen_number);
228 static EvWindowRunMode
229 get_window_run_mode_from_args (GHashTable *args)
231 EvWindowRunMode mode = EV_WINDOW_MODE_NORMAL;
232 GValue *value = NULL;
234 g_assert (args != NULL);
236 value = g_hash_table_lookup (args, "mode");
238 mode = g_value_get_uint (value);
245 get_destination_from_args (GHashTable *args)
247 EvLinkDest *dest = NULL;
248 GValue *value = NULL;
250 g_assert (args != NULL);
252 value = g_hash_table_lookup (args, "page-label");
254 const gchar *page_label;
256 page_label = g_value_get_string (value);
257 dest = ev_link_dest_new_page_label (page_label);
264 get_unlink_temp_file_from_args (GHashTable *args)
266 gboolean unlink_temp_file = FALSE;
267 GValue *value = NULL;
269 g_assert (args != NULL);
271 value = g_hash_table_lookup (args, "unlink-temp-file");
273 unlink_temp_file = g_value_get_boolean (value);
276 return unlink_temp_file;
280 ev_application_open_window (EvApplication *application,
285 GtkWidget *new_window = ev_window_new ();
286 GdkScreen *screen = NULL;
289 screen = get_screen_from_args (args);
293 gtk_window_set_screen (GTK_WINDOW (new_window), screen);
296 gtk_widget_show (new_window);
298 gtk_window_present_with_time (GTK_WINDOW (new_window),
304 ev_application_get_empty_window (EvApplication *application,
307 EvWindow *empty_window = NULL;
308 GList *windows = ev_application_get_windows (application);
311 for (l = windows; l != NULL; l = l->next) {
312 EvWindow *window = EV_WINDOW (l->data);
314 if (ev_window_is_empty (window) &&
315 gtk_window_get_screen (GTK_WINDOW (window)) == screen) {
316 empty_window = window;
321 g_list_free (windows);
327 ev_application_get_uri_window (EvApplication *application, const char *uri)
329 EvWindow *uri_window = NULL;
330 GList *windows = gtk_window_list_toplevels ();
333 g_return_val_if_fail (uri != NULL, NULL);
335 for (l = windows; l != NULL; l = l->next) {
336 if (EV_IS_WINDOW (l->data)) {
337 EvWindow *window = EV_WINDOW (l->data);
338 const char *window_uri = ev_window_get_uri (window);
340 if (window_uri && strcmp (window_uri, uri) == 0 && !ev_window_is_empty (window)) {
347 g_list_free (windows);
353 ev_application_open_uri_at_dest (EvApplication *application,
357 EvWindowRunMode mode,
358 gboolean unlink_temp_file,
361 EvWindow *new_window;
363 g_return_if_fail (uri != NULL);
365 new_window = ev_application_get_uri_window (application, uri);
367 if (new_window == NULL) {
368 new_window = ev_application_get_empty_window (application, screen);
371 if (new_window == NULL) {
372 new_window = EV_WINDOW (ev_window_new ());
376 gtk_window_set_screen (GTK_WINDOW (new_window), screen);
378 /* We need to load uri before showing the window, so
379 we can restore window size without flickering */
380 ev_window_open_uri (new_window, uri, dest, mode, unlink_temp_file);
382 gtk_widget_show (GTK_WIDGET (new_window));
384 gtk_window_present_with_time (GTK_WINDOW (new_window),
389 ev_application_open_uri (EvApplication *application,
395 EvLinkDest *dest = NULL;
396 EvWindowRunMode mode = EV_WINDOW_MODE_NORMAL;
397 gboolean unlink_temp_file = FALSE;
398 GdkScreen *screen = NULL;
401 screen = get_screen_from_args (args);
402 dest = get_destination_from_args (args);
403 mode = get_window_run_mode_from_args (args);
404 unlink_temp_file = (mode == EV_WINDOW_MODE_PREVIEW &&
405 get_unlink_temp_file_from_args (args));
408 ev_application_open_uri_at_dest (application, uri, screen,
409 dest, mode, unlink_temp_file,
413 g_object_unref (dest);
419 ev_application_open_uri_list (EvApplication *application,
426 for (l = uri_list; l != NULL; l = l->next) {
427 ev_application_open_uri_at_dest (application, (char *)l->data,
428 screen, NULL, 0, FALSE,
434 ev_application_shutdown (EvApplication *application)
436 if (application->toolbars_model) {
437 g_object_unref (application->toolbars_model);
438 g_object_unref (application->preview_toolbars_model);
439 g_free (application->toolbars_file);
440 application->toolbars_model = NULL;
441 application->preview_toolbars_model = NULL;
442 application->toolbars_file = NULL;
445 #ifndef HAVE_GTK_RECENT
446 if (application->recent_model) {
447 g_object_unref (application->recent_model);
448 application->recent_model = NULL;
452 g_free (application->last_chooser_uri);
453 g_object_unref (application);
459 ev_application_class_init (EvApplicationClass *ev_application_class)
464 ev_application_init (EvApplication *ev_application)
466 init_session (ev_application);
468 ev_application->toolbars_model = egg_toolbars_model_new ();
470 ev_application->toolbars_file = g_build_filename
471 (ev_dot_dir (), "evince_toolbar.xml", NULL);
473 egg_toolbars_model_load_names (ev_application->toolbars_model,
474 DATADIR "/evince-toolbar.xml");
476 if (!egg_toolbars_model_load_toolbars (ev_application->toolbars_model,
477 ev_application->toolbars_file)) {
478 egg_toolbars_model_load_toolbars (ev_application->toolbars_model,
479 DATADIR"/evince-toolbar.xml");
482 egg_toolbars_model_set_flags (ev_application->toolbars_model, 0,
483 EGG_TB_MODEL_NOT_REMOVABLE);
485 ev_application->preview_toolbars_model = egg_toolbars_model_new ();
487 egg_toolbars_model_load_toolbars (ev_application->preview_toolbars_model,
488 DATADIR"/evince-preview-toolbar.xml");
490 egg_toolbars_model_set_flags (ev_application->preview_toolbars_model, 0,
491 EGG_TB_MODEL_NOT_REMOVABLE);
493 #ifndef HAVE_GTK_RECENT
494 ev_application->recent_model = egg_recent_model_new (EGG_RECENT_MODEL_SORT_MRU);
495 /* FIXME we should add a mime type filter but current eggrecent
496 has only a varargs style api which does not work well when
497 the list of mime types is dynamic */
498 egg_recent_model_set_limit (ev_application->recent_model, 5);
499 egg_recent_model_set_filter_groups (ev_application->recent_model,
501 #endif /* HAVE_GTK_RECENT */
505 ev_application_get_windows (EvApplication *application)
507 GList *l, *toplevels;
508 GList *windows = NULL;
510 toplevels = gtk_window_list_toplevels ();
512 for (l = toplevels; l != NULL; l = l->next) {
513 if (EV_IS_WINDOW (l->data)) {
514 windows = g_list_append (windows, l->data);
518 g_list_free (toplevels);
523 EggToolbarsModel *ev_application_get_toolbars_model (EvApplication *application,
527 application->preview_toolbars_model : application->toolbars_model;
530 #ifndef HAVE_GTK_RECENT
531 EggRecentModel *ev_application_get_recent_model (EvApplication *application)
533 return application->recent_model;
537 void ev_application_save_toolbars_model (EvApplication *application)
539 egg_toolbars_model_save_toolbars (application->toolbars_model,
540 application->toolbars_file, "1.0");
543 void ev_application_set_chooser_uri (EvApplication *application, const gchar *uri)
545 g_free (application->last_chooser_uri);
546 application->last_chooser_uri = g_strdup (uri);
549 const gchar* ev_application_get_chooser_uri (EvApplication *application)
551 return application->last_chooser_uri;