+ ev_application_open_uri_in_window (application, uri, ev_window,
+ screen, dest, mode,
+ search_string,
+ timestamp);
+}
+
+/**
+ * ev_application_open_uri_at_dest:
+ * @application: The instance of the application.
+ * @uri: The uri to be opened.
+ * @screen: Thee screen where the link will be shown.
+ * @dest: The #EvLinkDest of the document.
+ * @mode: The run mode of the window.
+ * @timestamp: Current time value.
+ */
+void
+ev_application_open_uri_at_dest (EvApplication *application,
+ const char *uri,
+ GdkScreen *screen,
+ EvLinkDest *dest,
+ EvWindowRunMode mode,
+ const gchar *search_string,
+ guint timestamp)
+{
+ g_return_if_fail (uri != NULL);
+
+ if (application->uri && strcmp (application->uri, uri) != 0) {
+ /* spawn a new evince process */
+ ev_spawn (uri, screen, dest, mode, search_string, timestamp);
+ return;
+ } else if (!application->uri) {
+ application->uri = g_strdup (uri);
+ }
+
+#ifdef ENABLE_DBUS
+ /* Register the uri or send Reload to
+ * remote instance if already registered
+ */
+ ev_application_register_uri (application, uri, screen, dest, mode, search_string, timestamp);
+#else
+ _ev_application_open_uri_at_dest (application, uri, screen, dest, mode, search_string, timestamp);
+#endif /* ENABLE_DBUS */
+}
+
+/**
+ * ev_application_open_window:
+ * @application: The instance of the application.
+ * @timestamp: Current time value.
+ *
+ * Creates a new window
+ */
+void
+ev_application_open_window (EvApplication *application,
+ GdkScreen *screen,
+ guint32 timestamp)
+{
+ GtkWidget *new_window = ev_window_new ();
+#ifdef GDK_WINDOWING_X11
+ GdkWindow *gdk_window;
+#endif
+
+ if (screen) {
+ ev_stock_icons_set_screen (screen);
+ gtk_window_set_screen (GTK_WINDOW (new_window), screen);
+ }
+
+ if (!gtk_widget_get_realized (new_window))
+ gtk_widget_realize (new_window);
+
+#ifdef GDK_WINDOWING_X11
+ gdk_window = gtk_widget_get_window (GTK_WIDGET (new_window));
+
+ if (timestamp <= 0)
+ timestamp = gdk_x11_get_server_time (gdk_window);
+ gdk_x11_window_set_user_time (gdk_window, timestamp);
+
+ gtk_window_present (GTK_WINDOW (new_window));
+#else
+ gtk_window_present_with_time (GTK_WINDOW (new_window), timestamp);
+#endif /* GDK_WINDOWING_X11 */
+}
+
+#ifdef ENABLE_DBUS
+static void
+method_call_cb (GDBusConnection *connection,
+ const gchar *sender,
+ const gchar *object_path,
+ const gchar *interface_name,
+ const gchar *method_name,
+ GVariant *parameters,
+ GDBusMethodInvocation *invocation,
+ gpointer user_data)
+{
+ EvApplication *application = EV_APPLICATION (user_data);
+ GList *windows, *l;
+ guint timestamp;
+ GVariantIter *iter;
+ const gchar *key;
+ GVariant *value;
+ GdkDisplay *display = NULL;
+ int screen_number = 0;
+ EvLinkDest *dest = NULL;
+ EvWindowRunMode mode = EV_WINDOW_MODE_NORMAL;
+ const gchar *search_string = NULL;
+ GdkScreen *screen = NULL;
+
+ if (g_strcmp0 (method_name, "Reload") == 0) {
+ g_variant_get (parameters, "(a{sv}u)", &iter, ×tamp);
+
+ while (g_variant_iter_loop (iter, "{&sv}", &key, &value)) {
+ if (strcmp (key, "display") == 0 && g_variant_classify (value) == G_VARIANT_CLASS_STRING) {
+ display = ev_display_open_if_needed (g_variant_get_string (value, NULL));
+ } else if (strcmp (key, "screen") == 0 && g_variant_classify (value) == G_VARIANT_CLASS_STRING) {
+ screen_number = g_variant_get_int32 (value);
+ } else if (strcmp (key, "mode") == 0 && g_variant_classify (value) == G_VARIANT_CLASS_UINT32) {
+ mode = g_variant_get_uint32 (value);
+ } else if (strcmp (key, "page-label") == 0 && g_variant_classify (value) == G_VARIANT_CLASS_STRING) {
+ dest = ev_link_dest_new_page_label (g_variant_get_string (value, NULL));
+ } else if (strcmp (key, "find-string") == 0 && g_variant_classify (value) == G_VARIANT_CLASS_STRING) {
+ search_string = g_variant_get_string (value, NULL);
+ }
+ }
+ g_variant_iter_free (iter);
+
+ if (display != NULL &&
+ screen_number >= 0 &&
+ screen_number < gdk_display_get_n_screens (display))
+ screen = gdk_display_get_screen (display, screen_number);
+ else
+ screen = gdk_screen_get_default ();
+
+ windows = ev_application_get_windows (application);
+ for (l = windows; l != NULL; l = g_list_next (l)) {
+ EvWindow *ev_window = EV_WINDOW (l->data);
+
+ ev_application_open_uri_in_window (application, application->uri,
+ ev_window,
+ screen, dest, mode,
+ search_string,
+ timestamp);
+ }
+ g_list_free (windows);
+
+ if (dest)
+ g_object_unref (dest);
+
+ g_dbus_method_invocation_return_value (invocation, g_variant_new ("()"));
+ } else if (g_strcmp0 (method_name, "GetWindowList") == 0) {
+ GList *windows = ev_application_get_windows (application);
+ GVariantBuilder builder;
+ GList *l;
+
+ g_variant_builder_init (&builder, G_VARIANT_TYPE ("(ao)"));
+ g_variant_builder_open (&builder, G_VARIANT_TYPE ("ao"));
+
+ for (l = windows; l; l = g_list_next (l)) {
+ EvWindow *window = (EvWindow *)l->data;
+
+ g_variant_builder_add (&builder, "o", ev_window_get_dbus_object_path (window));
+ }
+
+ g_variant_builder_close (&builder);
+ g_list_free (windows);
+
+ g_dbus_method_invocation_return_value (invocation, g_variant_builder_end (&builder));
+ }
+}
+
+static const char introspection_xml[] =
+ "<node>"
+ "<interface name='org.gnome.evince.Application'>"
+ "<method name='Reload'>"
+ "<arg type='a{sv}' name='args' direction='in'/>"
+ "<arg type='u' name='timestamp' direction='in'/>"
+ "</method>"
+ "<method name='GetWindowList'>"
+ "<arg type='ao' name='window_list' direction='out'/>"
+ "</method>"
+ "</interface>"
+ "</node>";
+
+static const GDBusInterfaceVTable interface_vtable = {
+ method_call_cb,
+ NULL,
+ NULL
+};
+
+static GDBusNodeInfo *introspection_data;
+#endif /* ENABLE_DBUS */
+
+void
+ev_application_open_uri_list (EvApplication *application,
+ GSList *uri_list,
+ GdkScreen *screen,
+ guint timestamp)
+{
+ GSList *l;
+
+ for (l = uri_list; l != NULL; l = l->next) {
+ ev_application_open_uri_at_dest (application, (char *)l->data,
+ screen, NULL, 0, NULL,
+ timestamp);
+ }
+}
+
+static void
+ev_application_accel_map_save (EvApplication *application)
+{
+ gchar *accel_map_file;
+ gchar *tmp_filename;
+ gint fd;
+
+ accel_map_file = g_build_filename (application->dot_dir, "accels", NULL);
+ tmp_filename = g_strdup_printf ("%s.XXXXXX", accel_map_file);
+
+ fd = g_mkstemp (tmp_filename);
+ if (fd == -1) {
+ g_free (accel_map_file);
+ g_free (tmp_filename);
+
+ return;
+ }
+ gtk_accel_map_save_fd (fd);
+ close (fd);
+
+ g_mkdir_with_parents (application->dot_dir, 0700);
+ if (g_rename (tmp_filename, accel_map_file) == -1) {
+ /* FIXME: win32? */
+ g_unlink (tmp_filename);
+ }
+
+ g_free (accel_map_file);
+ g_free (tmp_filename);
+}
+
+static void
+ev_application_accel_map_load (EvApplication *application)
+{
+ gchar *accel_map_file;
+
+ accel_map_file = g_build_filename (application->dot_dir, "accels", NULL);
+ gtk_accel_map_load (accel_map_file);
+ g_free (accel_map_file);
+}
+
+static void
+ev_application_migrate_config_dir (EvApplication *application)
+{
+ const gchar *userdir;
+ gchar *old_dot_dir;
+ gchar *old_accels;
+ GError *error;
+ gint i;
+ gboolean dir_created = FALSE;
+ static const gchar *config_files[] = {
+ "evince_toolbar.xml",
+ "print-settings",
+ NULL
+ };
+
+ userdir = g_getenv ("GNOME22_USER_DIR");
+ if (userdir) {
+ old_dot_dir = g_build_filename (userdir, "evince", NULL);
+ old_accels = g_build_filename (userdir, "accels", "evince", NULL);
+ } else {
+ old_dot_dir = g_build_filename (g_get_home_dir (),
+ ".gnome2",
+ "evince",
+ NULL);
+ old_accels = g_build_filename (g_get_home_dir (),
+ ".gnome2", "accels",
+ "evince", NULL);
+ }
+
+ if (g_file_test (old_dot_dir, G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR)) {
+ for (i = 0; config_files[i]; i++) {
+ gchar *old_filename;
+ gchar *new_filename;
+ GFile *old_file;
+ GFile *new_file;
+
+ old_filename = g_build_filename (old_dot_dir, config_files[i], NULL);
+ if (!g_file_test (old_filename, G_FILE_TEST_EXISTS)) {
+ g_free (old_filename);
+ continue;
+ }
+
+ if (!dir_created) {
+ g_mkdir_with_parents (application->dot_dir, 0700);
+ dir_created = TRUE;
+ }
+
+ new_filename = g_build_filename (application->dot_dir, config_files[i], NULL);
+ old_file = g_file_new_for_path (old_filename);
+ new_file = g_file_new_for_path (new_filename);
+
+ error = NULL;
+ g_file_move (old_file, new_file, 0, NULL, NULL, NULL, &error);
+ if (error) {
+ g_warning ("Error migrating config file %s: %s\n",
+ old_filename, error->message);
+ g_error_free (error);
+ }
+
+ g_free (old_filename);
+ g_free (new_filename);
+ g_object_unref (old_file);
+ g_object_unref (new_file);
+ }
+ }
+
+ g_free (old_dot_dir);
+
+ if (g_file_test (old_accels, G_FILE_TEST_EXISTS)) {
+ gchar *new_accels;
+ GFile *old_accels_file;
+ GFile *new_accels_file;
+
+ if (!dir_created)
+ g_mkdir_with_parents (application->dot_dir, 0700);
+
+ new_accels = g_build_filename (application->dot_dir, "accels", NULL);
+ old_accels_file = g_file_new_for_path (old_accels);
+ new_accels_file = g_file_new_for_path (new_accels);
+
+ error = NULL;
+ g_file_move (old_accels_file, new_accels_file, 0, NULL, NULL, NULL, &error);
+ if (error) {
+ g_warning ("Error migrating accelerator specifications file %s: %s\n",
+ old_accels, error->message);
+ g_error_free (error);
+ }
+
+ g_free (new_accels);
+ g_object_unref (old_accels_file);
+ g_object_unref (new_accels_file);
+ }
+
+ g_free (old_accels);
+}
+
+void
+ev_application_shutdown (EvApplication *application)
+{
+ if (application->uri) {
+#ifdef ENABLE_DBUS
+ ev_application_unregister_uri (application,
+ application->uri);
+#endif
+ g_free (application->uri);
+ application->uri = NULL;
+ }
+
+ ev_application_accel_map_save (application);
+
+ g_object_unref (application->scr_saver);
+ application->scr_saver = NULL;
+
+#ifdef ENABLE_DBUS
+ if (application->keys) {
+ g_object_unref (application->keys);
+ application->keys = NULL;
+ }
+ if (application->registration_id != 0) {
+ g_dbus_connection_unregister_object (application->connection,
+ application->registration_id);
+ application->registration_id = 0;
+ }
+ if (application->connection != NULL) {
+ g_object_unref (application->connection);
+ application->connection = NULL;
+ }
+ if (introspection_data) {
+ g_dbus_node_info_ref (introspection_data);
+ introspection_data = NULL;
+ }
+#endif /* ENABLE_DBUS */
+
+ g_free (application->dot_dir);
+ application->dot_dir = NULL;
+ g_free (application->data_dir);
+ application->data_dir = NULL;
+ g_free (application->filechooser_open_uri);
+ application->filechooser_open_uri = NULL;
+ g_free (application->filechooser_save_uri);
+ application->filechooser_save_uri = NULL;
+
+ g_object_unref (application);
+ instance = NULL;
+
+ gtk_main_quit ();
+}
+
+static void
+ev_application_class_init (EvApplicationClass *ev_application_class)
+{