2 * Copyright (C) 2004 Marco Pesenti Gritti
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2, or (at your option)
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
25 #include <glib/gi18n.h>
30 #include <dbus/dbus-glib-bindings.h>
33 #include "ev-application.h"
34 #include "ev-backends-manager.h"
37 #include "ev-file-helpers.h"
38 #include "ev-stock-icons.h"
39 #include "eggsmclient.h"
41 #include "eggdesktopfile.h"
44 static gchar *ev_page_label;
45 static gchar *ev_find_string;
46 static gboolean preview_mode = FALSE;
47 static gboolean fullscreen_mode = FALSE;
48 static gboolean presentation_mode = FALSE;
49 static gboolean unlink_temp_file = FALSE;
50 static gchar *print_settings;
51 static const char **file_arguments = NULL;
54 option_version_cb (const gchar *option_name,
59 g_print ("%s %s\n", _("GNOME Document Viewer"), VERSION);
65 static const GOptionEntry goption_options[] =
67 { "page-label", 'p', 0, G_OPTION_ARG_STRING, &ev_page_label, N_("The page of the document to display."), N_("PAGE")},
68 { "fullscreen", 'f', 0, G_OPTION_ARG_NONE, &fullscreen_mode, N_("Run evince in fullscreen mode"), NULL },
69 { "presentation", 's', 0, G_OPTION_ARG_NONE, &presentation_mode, N_("Run evince in presentation mode"), NULL },
70 { "preview", 'w', 0, G_OPTION_ARG_NONE, &preview_mode, N_("Run evince as a previewer"), NULL },
71 { "find", 'l', 0, G_OPTION_ARG_STRING, &ev_find_string, N_("The word or phrase to find in the document"), N_("STRING")},
72 { "unlink-tempfile", 'u', G_OPTION_FLAG_HIDDEN, G_OPTION_ARG_NONE, &unlink_temp_file, NULL, NULL },
73 { "print-settings", 't', G_OPTION_FLAG_HIDDEN, G_OPTION_ARG_FILENAME, &print_settings, NULL, NULL },
74 { "version", 0, G_OPTION_FLAG_NO_ARG | G_OPTION_FLAG_HIDDEN, G_OPTION_ARG_CALLBACK, option_version_cb, NULL, NULL },
75 { G_OPTION_REMAINING, 0, 0, G_OPTION_ARG_FILENAME_ARRAY, &file_arguments, NULL, N_("[FILE...]") },
80 launch_previewer (void)
86 gboolean retval = FALSE;
89 /* Rebuild the command line, ignoring options
90 * not supported by the previewer and taking only
91 * the first path given
93 cmd_str = g_string_new ("evince-previewer");
98 quoted = g_shell_quote (print_settings);
99 g_string_append_printf (cmd_str, " --print-settings %s", quoted);
103 if (unlink_temp_file)
104 g_string_append (cmd_str, " --unlink-tempfile");
106 if (file_arguments) {
109 quoted = g_shell_quote (file_arguments[0]);
110 g_string_append_printf (cmd_str, " %s", quoted);
114 cmd = g_string_free (cmd_str, FALSE);
115 g_shell_parse_argv (cmd, &argc, &argv, &error);
119 retval = gdk_spawn_on_screen (gdk_screen_get_default (),
128 g_warning ("Error launching previewer: %s\n", error->message);
129 g_error_free (error);
136 value_free (GValue *value)
138 g_value_unset (value);
145 * Parses the arguments and creates a #GHashTable with this data.
149 * dislay -> display at the default screen.
150 * screen -> screen number.
151 * page-label -> only if the page label argument has been passed,
152 * the page of the document to display.
153 * mode -> only if the view mode is one of the availables,
156 * Returns: a pointer into #GHashTable with data from the arguments.
159 arguments_parse (void)
163 EvWindowRunMode mode;
166 const gchar *display_name;
169 args = g_hash_table_new_full (g_str_hash,
171 (GDestroyNotify)g_free,
172 (GDestroyNotify)value_free);
174 screen = gdk_screen_get_default ();
175 display = gdk_screen_get_display (screen);
177 display_name = gdk_display_get_name (display);
178 screen_number = gdk_screen_get_number (screen);
180 value = g_new0 (GValue, 1);
181 g_value_init (value, G_TYPE_STRING);
182 g_value_set_string (value, display_name);
183 g_hash_table_insert (args, g_strdup ("display"), value);
185 value = g_new0 (GValue, 1);
186 g_value_init (value, G_TYPE_INT);
187 g_value_set_int (value, screen_number);
188 g_hash_table_insert (args, g_strdup ("screen"), value);
191 value = g_new0 (GValue, 1);
192 g_value_init (value, G_TYPE_STRING);
193 g_value_set_string (value, ev_page_label);
195 g_hash_table_insert (args, g_strdup ("page-label"), value);
197 g_free (ev_page_label);
198 ev_page_label = NULL;
201 if (ev_find_string) {
202 value = g_new0 (GValue, 1);
203 g_value_init (value, G_TYPE_STRING);
204 g_value_set_string (value, ev_find_string);
206 g_hash_table_insert (args, g_strdup ("find-string"), value);
208 g_free (ev_find_string);
209 ev_page_label = NULL;
213 mode = EV_WINDOW_MODE_FULLSCREEN;
214 else if (presentation_mode)
215 mode = EV_WINDOW_MODE_PRESENTATION;
219 value = g_new0 (GValue, 1);
220 g_value_init (value, G_TYPE_UINT);
221 g_value_set_uint (value, mode);
223 g_hash_table_insert (args, g_strdup ("mode"), value);
229 load_files (const char **files,
235 ev_application_open_window (EV_APP, args, GDK_CURRENT_TIME, NULL);
239 for (i = 0; files[i]; i++) {
245 file = g_file_new_for_commandline_arg (files[i]);
246 uri = g_file_get_uri (file);
247 g_object_unref (file);
249 label = strchr (uri, '#');
256 old = g_hash_table_lookup (args, "page-label");
258 new = g_new0 (GValue, 1);
259 g_value_init (new, G_TYPE_STRING);
260 g_value_set_string (new, label);
262 g_hash_table_insert (args, g_strdup ("page-label"), new);
266 ev_application_open_uri (EV_APP, uri, args,
267 GDK_CURRENT_TIME, NULL);
270 g_hash_table_insert (args, g_strdup ("page-label"), old);
278 load_files_remote (const char **files,
282 GError *error = NULL;
283 DBusGConnection *connection;
284 gboolean result = FALSE;
285 DBusGProxy *remote_object;
289 display = gdk_display_get_default ();
290 timestamp = gdk_x11_display_get_user_time (display);
291 connection = dbus_g_bus_get (DBUS_BUS_STARTER, &error);
293 if (connection == NULL) {
294 g_warning ("%s", error->message);
295 g_error_free (error);
300 remote_object = dbus_g_proxy_new_for_name (connection,
301 "org.gnome.evince.ApplicationService",
302 "/org/gnome/evince/Evince",
303 "org.gnome.evince.Application");
305 if (!dbus_g_proxy_call (remote_object, "OpenWindow", &error,
306 dbus_g_type_get_map ("GHashTable", G_TYPE_STRING, G_TYPE_VALUE), args,
307 G_TYPE_UINT, timestamp,
310 g_warning ("%s", error->message);
311 g_clear_error (&error);
312 g_object_unref (remote_object);
313 dbus_g_connection_unref (connection);
317 g_object_unref (remote_object);
318 dbus_g_connection_unref (connection);
323 for (i = 0; files[i]; i++) {
324 const char *page_label;
328 file = g_file_new_for_commandline_arg (files[i]);
329 uri = g_file_get_uri (file);
330 g_object_unref (file);
332 page_label = ev_page_label ? ev_page_label : "";
334 if (!dbus_g_proxy_call (remote_object, "OpenURI", &error,
336 dbus_g_type_get_map ("GHashTable", G_TYPE_STRING, G_TYPE_VALUE), args,
337 G_TYPE_UINT, timestamp,
340 g_warning ("%s", error->message);
341 g_clear_error (&error);
350 g_object_unref (remote_object);
351 dbus_g_connection_unref (connection);
353 gdk_notify_startup_complete ();
357 #endif /* ENABLE_DBUS */
360 main (int argc, char *argv[])
362 GOptionContext *context;
364 GError *error = NULL;
366 /* Init glib threads asap */
367 if (!g_thread_supported ())
368 g_thread_init (NULL);
371 /* Initialize the i18n stuff */
372 bindtextdomain (GETTEXT_PACKAGE, GNOMELOCALEDIR);
373 bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
374 textdomain (GETTEXT_PACKAGE);
377 context = g_option_context_new (N_("GNOME Document Viewer"));
378 g_option_context_set_translation_domain(context, GETTEXT_PACKAGE);
379 g_option_context_add_main_entries (context, goption_options, GETTEXT_PACKAGE);
381 g_option_context_add_group (context, egg_sm_client_get_option_group ());
382 g_option_context_add_group (context, gtk_get_option_group (TRUE));
384 if (!g_option_context_parse (context, &argc, &argv, &error)) {
385 g_printerr ("Cannot parse arguments: %s", error->message);
386 g_error_free (error);
387 g_option_context_free (context);
391 g_option_context_free (context);
396 retval = launch_previewer ();
398 return retval ? 0 : 1;
401 args = arguments_parse ();
404 if (!ev_application_register_service (EV_APP)) {
405 if (load_files_remote (file_arguments, args)) {
406 g_hash_table_destroy (args);
411 #endif /* ENABLE_DBUS */
416 ev_stock_icons_init ();
419 /* Manually set name and icon in win32 */
420 g_set_application_name (_("Evince"));
421 gtk_window_set_default_icon_name ("evince");
423 egg_set_desktop_file (GNOMEDATADIR "/applications/evince.desktop");
424 #endif /* G_OS_WIN32 */
426 if (!ev_application_load_session (EV_APP))
427 load_files (file_arguments, args);
428 g_hash_table_destroy (args);
433 ev_stock_icons_shutdown ();