+/* Session */
+gboolean
+ev_application_load_session (EvApplication *application)
+{
+ GKeyFile *state_file;
+ gchar *uri;
+
+#ifdef WITH_SMCLIENT
+ if (egg_sm_client_is_resumed (application->smclient)) {
+ state_file = egg_sm_client_get_state_file (application->smclient);
+ if (!state_file)
+ return FALSE;
+ } else
+#endif /* WITH_SMCLIENT */
+ return FALSE;
+
+ uri = g_key_file_get_string (state_file, "Evince", "uri", NULL);
+ if (!uri)
+ return FALSE;
+
+ ev_application_open_uri_at_dest (application, uri,
+ gdk_screen_get_default (),
+ NULL, 0, NULL,
+ GDK_CURRENT_TIME);
+ g_free (uri);
+ g_key_file_free (state_file);
+
+ return TRUE;
+}
+
+#ifdef WITH_SMCLIENT
+
+static void
+smclient_save_state_cb (EggSMClient *client,
+ GKeyFile *state_file,
+ EvApplication *application)
+{
+ if (!application->uri)
+ return;
+
+ g_key_file_set_string (state_file, "Evince", "uri", application->uri);
+}
+
+static void
+smclient_quit_cb (EggSMClient *client,
+ EvApplication *application)
+{
+ ev_application_shutdown (application);
+}
+#endif /* WITH_SMCLIENT */
+
+static void
+ev_application_init_session (EvApplication *application)
+{
+#ifdef WITH_SMCLIENT
+ application->smclient = egg_sm_client_get ();
+ g_signal_connect (application->smclient, "save_state",
+ G_CALLBACK (smclient_save_state_cb),
+ application);
+ g_signal_connect (application->smclient, "quit",
+ G_CALLBACK (smclient_quit_cb),
+ application);
+#endif
+}
+
+/**
+ * ev_display_open_if_needed:
+ * @name: the name of the display to be open if it's needed.
+ *
+ * Search among all the open displays if any of them have the same name as the
+ * passed name. If the display isn't found it tries the open it.
+ *
+ * Returns: a #GdkDisplay of the display with the passed name.
+ */
+static GdkDisplay *
+ev_display_open_if_needed (const gchar *name)
+{
+ GSList *displays;
+ GSList *l;
+ GdkDisplay *display = NULL;
+
+ displays = gdk_display_manager_list_displays (gdk_display_manager_get ());
+
+ for (l = displays; l != NULL; l = l->next) {
+ const gchar *display_name = gdk_display_get_name ((GdkDisplay *) l->data);
+
+ if (g_ascii_strcasecmp (display_name, name) == 0) {
+ display = l->data;
+ break;
+ }
+ }
+
+ g_slist_free (displays);
+
+ return display != NULL ? display : gdk_display_open (name);
+}
+
+/**
+ * get_screen_from_args:
+ * @args: a #GHashTable with data passed to the application.
+ *
+ * Looks for the screen in the display available in the hash table passed to the
+ * application. If the display isn't opened, it's opened and the #GdkScreen
+ * assigned to the screen in that display returned.
+ *
+ * Returns: the #GdkScreen assigned to the screen on the display indicated by
+ * the data on the #GHashTable.
+ */
+static GdkScreen *
+get_screen_from_args (GHashTable *args)
+{
+ GValue *value = NULL;
+ GdkDisplay *display = NULL;
+ GdkScreen *screen = NULL;
+
+ g_assert (args != NULL);
+
+ value = g_hash_table_lookup (args, "display");
+ if (value) {
+ const gchar *display_name;
+
+ display_name = g_value_get_string (value);
+ display = ev_display_open_if_needed (display_name);
+ }
+
+ value = g_hash_table_lookup (args, "screen");
+ if (value) {
+ gint screen_number;
+
+ screen_number = g_value_get_int (value);
+ screen = gdk_display_get_screen (display, screen_number);
+ }
+
+ return screen;
+}
+
+/**
+ * get_window_run_mode_from_args:
+ * @args: a #GHashTable with data passed to the application.
+ *
+ * It does look if the mode option has been passed from command line, using it
+ * as the window run mode, otherwise the run mode will be the normal mode.
+ *
+ * Returns: The window run mode passed from command line or
+ * EV_WINDOW_MODE_NORMAL in other case.
+ */
+static EvWindowRunMode
+get_window_run_mode_from_args (GHashTable *args)
+{
+ EvWindowRunMode mode = EV_WINDOW_MODE_NORMAL;
+ GValue *value = NULL;
+
+ g_assert (args != NULL);
+
+ value = g_hash_table_lookup (args, "mode");
+ if (value) {
+ mode = g_value_get_uint (value);
+ }
+
+ return mode;
+}
+
+/**
+ * get_destination_from_args:
+ * @args: a #GHashTable with data passed to the application.
+ *
+ * It does look for the page-label argument parsed from the command line and
+ * if it does exist, it returns an #EvLinkDest.
+ *
+ * Returns: An #EvLinkDest to page-label if it has been passed from the command
+ * line, NULL in other case.
+ */
+static EvLinkDest *
+get_destination_from_args (GHashTable *args)
+{
+ EvLinkDest *dest = NULL;
+ GValue *value = NULL;
+
+ g_assert (args != NULL);
+
+ value = g_hash_table_lookup (args, "page-label");
+ if (value) {
+ const gchar *page_label;
+
+ page_label = g_value_get_string (value);
+ dest = ev_link_dest_new_page_label (page_label);
+ }
+
+ return dest;
+}
+
+static const gchar *
+get_find_string_from_args (GHashTable *args)
+{
+ GValue *value = NULL;
+
+ g_assert (args != NULL);
+
+ value = g_hash_table_lookup (args, "find-string");
+
+ return value ? g_value_get_string (value) : NULL;
+}
+
+static void
+value_free (GValue *value)
+{
+ g_value_unset (value);
+ g_free (value);
+}
+
+static GHashTable *
+build_args (GdkScreen *screen,
+ EvLinkDest *dest,
+ EvWindowRunMode mode,
+ const gchar *search_string)
+{
+ GHashTable *args;
+ GValue *value;
+ GdkDisplay *display;
+ const gchar *display_name;
+ gint screen_number;
+
+ args = g_hash_table_new_full (g_str_hash,
+ g_str_equal,
+ (GDestroyNotify)g_free,
+ (GDestroyNotify)value_free);
+
+ /* Display */
+ display = gdk_screen_get_display (screen);
+ display_name = gdk_display_get_name (display);
+ value = g_new0 (GValue, 1);
+ g_value_init (value, G_TYPE_STRING);
+ g_value_set_string (value, display_name);
+ g_hash_table_insert (args, g_strdup ("display"), value);
+
+ /* Screen */
+ screen_number = gdk_screen_get_number (screen);
+ value = g_new0 (GValue, 1);
+ g_value_init (value, G_TYPE_INT);
+ g_value_set_int (value, screen_number);
+ g_hash_table_insert (args, g_strdup ("screen"), value);
+
+ /* Page label */
+ if (dest) {
+ value = g_new0 (GValue, 1);
+ g_value_init (value, G_TYPE_STRING);
+ g_value_set_string (value, ev_link_dest_get_page_label (dest));
+
+ g_hash_table_insert (args, g_strdup ("page-label"), value);
+ }
+
+ /* Find string */
+ if (search_string) {
+ value = g_new0 (GValue, 1);
+ g_value_init (value, G_TYPE_STRING);
+ g_value_set_string (value, search_string);
+
+ g_hash_table_insert (args, g_strdup ("find-string"), value);
+ }
+
+ /* Mode */
+ if (mode != EV_WINDOW_MODE_NORMAL) {
+ value = g_new0 (GValue, 1);
+ g_value_init (value, G_TYPE_UINT);
+ g_value_set_uint (value, mode);
+
+ g_hash_table_insert (args, g_strdup ("mode"), value);
+ }
+
+ return args;
+}
+
+static void
+child_setup (gpointer user_data)
+{
+ gchar *startup_id;
+
+ startup_id = g_strdup_printf ("_TIME%lu",
+ (unsigned long)GPOINTER_TO_INT (user_data));
+ g_setenv ("DESKTOP_STARTUP_ID", startup_id, TRUE);
+ g_free (startup_id);
+}
+
+static void
+ev_spawn (const char *uri,
+ GdkScreen *screen,
+ EvLinkDest *dest,
+ EvWindowRunMode mode,
+ const gchar *search_string,
+ guint timestamp)
+{
+ gchar *argv[6];
+ guint arg = 0;
+ gint i;
+ gboolean res;
+ GError *error = NULL;
+
+#ifdef G_OS_WIN32
+{
+ gchar *dir;
+
+ dir = g_win32_get_package_installation_directory_of_module (NULL);
+ argv[arg++] = g_build_filename (dir, "bin", "evince", NULL);
+ g_free (dir);
+}
+#else
+ argv[arg++] = g_build_filename (BINDIR, "evince", NULL);
+#endif
+
+ /* Page label */
+ if (dest) {
+ const gchar *page_label;
+
+ page_label = ev_link_dest_get_page_label (dest);
+ if (page_label)
+ argv[arg++] = g_strdup_printf ("--page-label=%s", page_label);
+ else
+ argv[arg++] = g_strdup_printf ("--page-label=%d",
+ ev_link_dest_get_page (dest));
+ }
+
+ /* Find string */
+ if (search_string) {
+ argv[arg++] = g_strdup_printf ("--find=%s", search_string);
+ }
+
+ /* Mode */
+ switch (mode) {
+ case EV_WINDOW_MODE_FULLSCREEN:
+ argv[arg++] = g_strdup ("-f");
+ break;
+ case EV_WINDOW_MODE_PRESENTATION:
+ argv[arg++] = g_strdup ("-s");
+ break;
+ default:
+ break;
+ }
+
+ argv[arg++] = (gchar *)uri;
+ argv[arg] = NULL;
+
+ res = gdk_spawn_on_screen (screen, NULL /* wd */, argv, NULL /* env */,
+ 0,
+ child_setup,
+ GINT_TO_POINTER(timestamp),
+ NULL, &error);
+ if (!res) {
+ g_warning ("Error launching evince %s: %s\n", uri, error->message);
+ g_error_free (error);
+ }
+
+ for (i = 0; i < arg - 1; i++) {
+ g_free (argv[i]);
+ }
+}
+
+static GList *
+ev_application_get_windows (EvApplication *application)