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_application_open_window (EvApplication *application,
182 GtkWidget *new_window = ev_window_new ();
184 gtk_widget_show (new_window);
186 gtk_window_present_with_time (GTK_WINDOW (new_window),
192 ev_application_get_empty_window (EvApplication *application)
194 EvWindow *empty_window = NULL;
195 GList *windows = ev_application_get_windows (application);
198 for (l = windows; l != NULL; l = l->next) {
199 EvWindow *window = EV_WINDOW (l->data);
201 if (ev_window_is_empty (window)) {
202 empty_window = window;
207 g_list_free (windows);
213 ev_application_get_uri_window (EvApplication *application, const char *uri)
215 EvWindow *uri_window = NULL;
216 GList *windows = gtk_window_list_toplevels ();
219 g_return_val_if_fail (uri != NULL, NULL);
221 for (l = windows; l != NULL; l = l->next) {
222 if (EV_IS_WINDOW (l->data)) {
223 EvWindow *window = EV_WINDOW (l->data);
224 const char *window_uri = ev_window_get_uri (window);
226 if (window_uri && strcmp (window_uri, uri) == 0 && !ev_window_is_empty (window)) {
233 g_list_free (windows);
239 ev_application_open_uri_at_dest (EvApplication *application,
242 EvWindowRunMode mode,
245 EvWindow *new_window;
247 g_return_if_fail (uri != NULL);
249 new_window = ev_application_get_uri_window (application, uri);
250 if (new_window != NULL) {
251 gtk_window_present_with_time (GTK_WINDOW (new_window),
254 ev_window_goto_dest (new_window, dest);
259 new_window = ev_application_get_empty_window (application);
261 if (new_window == NULL) {
262 new_window = EV_WINDOW (ev_window_new ());
265 /* We need to load uri before showing the window, so
266 we can restore window size without flickering */
267 ev_window_open_uri (new_window, uri, dest, mode);
269 gtk_widget_show (GTK_WIDGET (new_window));
271 gtk_window_present_with_time (GTK_WINDOW (new_window),
276 ev_application_open_uri (EvApplication *application,
282 EvLinkDest *dest = NULL;
283 EvWindowRunMode mode = EV_WINDOW_MODE_NORMAL;
286 GValue *value = NULL;
288 value = g_hash_table_lookup (args, "page-label");
290 const gchar *page_label;
292 page_label = g_value_get_string (value);
293 dest = ev_link_dest_new_page_label (page_label);
296 value = g_hash_table_lookup (args, "mode");
298 mode = g_value_get_uint (value);
302 ev_application_open_uri_at_dest (application, uri, dest, mode, timestamp);
305 g_object_unref (dest);
311 ev_application_open_uri_list (EvApplication *application,
317 for (l = uri_list; l != NULL; l = l->next) {
318 ev_application_open_uri (application, (char *)l->data,
319 NULL, timestamp, NULL);
324 ev_application_shutdown (EvApplication *application)
326 if (application->toolbars_model) {
327 g_object_unref (application->toolbars_model);
328 g_free (application->toolbars_file);
329 application->toolbars_model = NULL;
330 application->toolbars_file = NULL;
333 if (application->recent_model) {
334 g_object_unref (application->recent_model);
335 application->recent_model = NULL;
338 g_free (application->last_chooser_uri);
339 g_object_unref (application);
345 ev_application_class_init (EvApplicationClass *ev_application_class)
350 ev_application_init (EvApplication *ev_application)
352 init_session (ev_application);
354 ev_application->toolbars_model = egg_toolbars_model_new ();
356 ev_application->toolbars_file = g_build_filename
357 (ev_dot_dir (), "evince_toolbar.xml", NULL);
359 egg_toolbars_model_load_names (ev_application->toolbars_model,
360 DATADIR "/evince-toolbar.xml");
362 if (!egg_toolbars_model_load_toolbars (ev_application->toolbars_model,
363 ev_application->toolbars_file)) {
364 egg_toolbars_model_load_toolbars (ev_application->toolbars_model,
365 DATADIR"/evince-toolbar.xml");
368 egg_toolbars_model_set_flags (ev_application->toolbars_model, 0,
369 EGG_TB_MODEL_NOT_REMOVABLE);
371 ev_application->recent_model = egg_recent_model_new (EGG_RECENT_MODEL_SORT_MRU);
372 /* FIXME we should add a mime type filter but current eggrecent
373 has only a varargs style api which does not work well when
374 the list of mime types is dynamic */
375 egg_recent_model_set_limit (ev_application->recent_model, 5);
376 egg_recent_model_set_filter_groups (ev_application->recent_model,
381 ev_application_get_windows (EvApplication *application)
383 GList *l, *toplevels;
384 GList *windows = NULL;
386 toplevels = gtk_window_list_toplevels ();
388 for (l = toplevels; l != NULL; l = l->next) {
389 if (EV_IS_WINDOW (l->data)) {
390 windows = g_list_append (windows, l->data);
394 g_list_free (toplevels);
399 EggToolbarsModel *ev_application_get_toolbars_model (EvApplication *application)
401 return application->toolbars_model;
404 EggRecentModel *ev_application_get_recent_model (EvApplication *application)
406 return application->recent_model;
409 void ev_application_save_toolbars_model (EvApplication *application)
411 egg_toolbars_model_save_toolbars (application->toolbars_model,
412 application->toolbars_file, "1.0");
415 void ev_application_set_chooser_uri (EvApplication *application, const gchar *uri)
417 g_free (application->last_chooser_uri);
418 application->last_chooser_uri = g_strdup (uri);
421 const gchar* ev_application_get_chooser_uri (EvApplication *application)
423 return application->last_chooser_uri;