+static gboolean
+launch_previewer (void)
+{
+ GString *cmd_str;
+ gchar *cmd;
+ gint argc;
+ gchar **argv;
+ gboolean retval = FALSE;
+ GError *error = NULL;
+
+ /* Rebuild the command line, ignoring options
+ * not supported by the previewer and taking only
+ * the first path given
+ */
+ cmd_str = g_string_new ("evince-previewer");
+
+ if (print_settings) {
+ gchar *quoted;
+
+ quoted = g_shell_quote (print_settings);
+ g_string_append_printf (cmd_str, " --print-settings %s", quoted);
+ g_free (quoted);
+ }
+
+ if (unlink_temp_file)
+ g_string_append (cmd_str, " --unlink-tempfile");
+
+ if (file_arguments) {
+ gchar *quoted;
+
+ quoted = g_shell_quote (file_arguments[0]);
+ g_string_append_printf (cmd_str, " %s", quoted);
+ g_free (quoted);
+ }
+
+ cmd = g_string_free (cmd_str, FALSE);
+ g_shell_parse_argv (cmd, &argc, &argv, &error);
+ g_free (cmd);
+
+ if (!error) {
+ retval = gdk_spawn_on_screen (gdk_screen_get_default (),
+ NULL, argv, NULL,
+ G_SPAWN_SEARCH_PATH,
+ NULL, NULL, NULL,
+ &error);
+ g_strfreev (argv);
+ }
+
+ if (error) {
+ g_warning ("Error launching previewer: %s\n", error->message);
+ g_error_free (error);
+ }
+
+ return retval;
+}
+
+static gchar *
+get_label_from_filename (const gchar *filename)
+{
+ GFile *file;
+ gchar *label;
+ gboolean exists;
+
+ label = g_strrstr (filename, "#");
+ if (!label)
+ return NULL;
+
+ /* Filename contains a #, check
+ * whether it's part of the path
+ * or a label
+ */
+ file = g_file_new_for_commandline_arg (filename);
+ exists = g_file_query_exists (file, NULL);
+ g_object_unref (file);
+
+ return exists ? NULL : label;
+}
+
+static void
+load_files (const char **files)
+{
+ GdkScreen *screen = gdk_screen_get_default ();
+ EvWindowRunMode mode = EV_WINDOW_MODE_NORMAL;
+ gint i;
+ EvLinkDest *global_dest = NULL;
+
+ if (!files) {
+ if (!ev_application_has_window (EV_APP))
+ ev_application_open_window (EV_APP, screen, GDK_CURRENT_TIME);
+ return;
+ }
+
+ if (ev_page_label)
+ global_dest = ev_link_dest_new_page_label (ev_page_label);
+ else if (ev_page_index)
+ global_dest = ev_link_dest_new_page (MAX (0, ev_page_index - 1));
+
+ if (fullscreen_mode)
+ mode = EV_WINDOW_MODE_FULLSCREEN;
+ else if (presentation_mode)
+ mode = EV_WINDOW_MODE_PRESENTATION;
+
+ for (i = 0; files[i]; i++) {
+ const gchar *filename;
+ gchar *uri;
+ gchar *label;
+ GFile *file;
+ EvLinkDest *dest = NULL;
+ const gchar *app_uri;
+
+ filename = files[i];
+ label = get_label_from_filename (filename);
+ if (label) {
+ *label = 0;
+ label++;
+ dest = ev_link_dest_new_page_label (label);
+ } else if (global_dest) {
+ dest = g_object_ref (global_dest);
+ }
+
+ file = g_file_new_for_commandline_arg (filename);
+ uri = g_file_get_uri (file);
+ g_object_unref (file);
+
+ app_uri = ev_application_get_uri (EV_APP);
+ if (app_uri && strcmp (app_uri, uri) == 0) {
+ g_free (uri);
+ continue;
+ }
+
+
+
+ ev_application_open_uri_at_dest (EV_APP, uri, screen, dest,
+ mode, ev_find_string,
+ GDK_CURRENT_TIME);
+
+ if (dest)
+ g_object_unref (dest);
+ g_free (uri);
+ }
+}
+