+ for (i = 0; i < arg - 1; i++) {
+ g_free (argv[i]);
+ }
+}
+
+static GList *
+ev_application_get_windows (EvApplication *application)
+{
+ GList *l, *toplevels;
+ GList *windows = NULL;
+
+ toplevels = gtk_window_list_toplevels ();
+
+ for (l = toplevels; l != NULL; l = l->next) {
+ if (EV_IS_WINDOW (l->data)) {
+ windows = g_list_append (windows, l->data);
+ }
+ }
+
+ g_list_free (toplevels);
+
+ return windows;
+}
+
+static EvWindow *
+ev_application_get_empty_window (EvApplication *application,
+ GdkScreen *screen)
+{
+ EvWindow *empty_window = NULL;
+ GList *windows = ev_application_get_windows (application);
+ GList *l;
+
+ for (l = windows; l != NULL; l = l->next) {
+ EvWindow *window = EV_WINDOW (l->data);
+
+ if (ev_window_is_empty (window) &&
+ gtk_window_get_screen (GTK_WINDOW (window)) == screen) {
+ empty_window = window;
+ break;
+ }
+ }
+
+ g_list_free (windows);
+
+ return empty_window;
+}
+
+
+#ifdef ENABLE_DBUS
+static gboolean
+ev_application_register_uri (EvApplication *application,
+ const gchar *uri,
+ GHashTable *args,
+ guint timestamp)
+{
+ DBusGProxy *proxy;
+ gchar *owner;
+ gboolean retval = TRUE;
+ GError *error = NULL;
+
+ if (!application->connection)
+ return TRUE;
+
+ proxy = dbus_g_proxy_new_for_name (application->connection,
+ "org.gnome.evince.Daemon",
+ "/org/gnome/evince/Daemon",
+ "org.gnome.evince.Daemon");
+ if (!dbus_g_proxy_call (proxy, "RegisterDocument", &error,
+ G_TYPE_STRING, uri,
+ G_TYPE_INVALID,
+ G_TYPE_STRING, &owner,
+ G_TYPE_INVALID)) {
+ g_warning ("Error registering document: %s\n", error->message);
+ g_error_free (error);
+ g_object_unref (proxy);
+
+ return TRUE;
+ }
+ g_object_unref (proxy);
+
+ if (*owner == ':') {
+ /* Already registered */
+ proxy = dbus_g_proxy_new_for_name_owner (application->connection,
+ owner,
+ APPLICATION_DBUS_OBJECT_PATH,
+ APPLICATION_DBUS_INTERFACE,
+ &error);
+ if (proxy) {
+ if (!dbus_g_proxy_call (proxy, "OpenURI", &error,
+ G_TYPE_STRING, uri,
+ dbus_g_type_get_map ("GHashTable", G_TYPE_STRING, G_TYPE_VALUE), args,
+ G_TYPE_UINT, timestamp,
+ G_TYPE_INVALID,
+ G_TYPE_INVALID)) {
+ g_warning ("%s", error->message);
+ g_error_free (error);
+ }
+ g_object_unref (proxy);
+ } else {
+ g_warning ("Error creating proxy: %s\n", error->message);
+ g_error_free (error);
+ }
+
+ /* Do not continue opening this document */
+ retval = FALSE;
+ }
+
+ g_free (owner);
+
+ return retval;
+}
+
+static void
+ev_application_unregister_uri (EvApplication *application,
+ const gchar *uri)
+{
+ DBusGProxy *proxy;
+ GError *error = NULL;
+
+ if (!application->connection)
+ return;
+
+ proxy = dbus_g_proxy_new_for_name (application->connection,
+ "org.gnome.evince.Daemon",
+ "/org/gnome/evince/Daemon",
+ "org.gnome.evince.Daemon");
+ if (!dbus_g_proxy_call (proxy, "UnregisterDocument", &error,
+ G_TYPE_STRING, uri,
+ G_TYPE_INVALID,
+ G_TYPE_INVALID)) {
+ g_warning ("Error unregistering document: %s\n", error->message);
+ g_error_free (error);
+ }
+
+ g_object_unref (proxy);
+}
+#endif /* ENABLE_DBUS */
+
+static void
+ev_application_open_uri_in_window (EvApplication *application,
+ const char *uri,
+ EvWindow *ev_window,
+ GdkScreen *screen,
+ EvLinkDest *dest,
+ EvWindowRunMode mode,
+ const gchar *search_string,
+ guint timestamp)
+{
+ if (screen) {
+ ev_stock_icons_set_screen (screen);
+ gtk_window_set_screen (GTK_WINDOW (ev_window), screen);
+ }
+
+ /* We need to load uri before showing the window, so
+ we can restore window size without flickering */
+ ev_window_open_uri (ev_window, uri, dest, mode, search_string);
+
+ if (!GTK_WIDGET_REALIZED (GTK_WIDGET (ev_window)))
+ gtk_widget_realize (GTK_WIDGET (ev_window));
+
+#ifdef GDK_WINDOWING_X11
+ if (timestamp <= 0)
+ timestamp = gdk_x11_get_server_time (GTK_WIDGET (ev_window)->window);
+ gdk_x11_window_set_user_time (GTK_WIDGET (ev_window)->window, timestamp);
+
+ ev_document_fc_mutex_lock ();
+ gtk_window_present (GTK_WINDOW (ev_window));
+ ev_document_fc_mutex_unlock ();
+#else
+ ev_document_fc_mutex_lock ();
+ gtk_window_present_with_time (GTK_WINDOW (ev_window), timestamp);
+ ev_document_fc_mutex_unlock ();
+#endif /* GDK_WINDOWING_X11 */
+}
+
+/**
+ * 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)
+{
+ EvWindow *ev_window;
+
+ 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;