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"
36 #include "ev-file-helpers.h"
37 #include "ev-stock-icons.h"
38 #include "eggsmclient.h"
39 #include "eggdesktopfile.h"
41 static gchar *ev_page_label;
42 static gchar *ev_find_string;
43 static gboolean preview_mode = FALSE;
44 static gboolean fullscreen_mode = FALSE;
45 static gboolean presentation_mode = FALSE;
46 static gboolean unlink_temp_file = FALSE;
47 static gchar *print_settings;
48 static const char **file_arguments = NULL;
50 static const GOptionEntry goption_options[] =
52 { "page-label", 'p', 0, G_OPTION_ARG_STRING, &ev_page_label, N_("The page of the document to display."), N_("PAGE")},
53 { "fullscreen", 'f', 0, G_OPTION_ARG_NONE, &fullscreen_mode, N_("Run evince in fullscreen mode"), NULL },
54 { "presentation", 's', 0, G_OPTION_ARG_NONE, &presentation_mode, N_("Run evince in presentation mode"), NULL },
55 { "preview", 'w', 0, G_OPTION_ARG_NONE, &preview_mode, N_("Run evince as a previewer"), NULL },
56 { "find", 'l', 0, G_OPTION_ARG_STRING, &ev_find_string, N_("The word or phrase to find in the document"), N_("STRING")},
57 { "unlink-tempfile", 'u', G_OPTION_FLAG_HIDDEN, G_OPTION_ARG_NONE, &unlink_temp_file, NULL, NULL },
58 { "print-settings", 't', G_OPTION_FLAG_HIDDEN, G_OPTION_ARG_FILENAME, &print_settings, NULL, NULL },
59 { G_OPTION_REMAINING, 0, 0, G_OPTION_ARG_FILENAME_ARRAY, &file_arguments, NULL, N_("[FILE...]") },
64 value_free (GValue *value)
66 g_value_unset (value);
73 * Parses the arguments and creates a #GHashTable with this data.
77 * dislay -> display at the default screen.
78 * screen -> screen number.
79 * page-label -> only if the page label argument has been passed,
80 * the page of the document to display.
81 * mode -> only if the view mode is one of the availables,
83 * unlink-temp-file -> only if the view mode is preview mode and
84 * unlink-temp-file has been passed, unlink-temp-file.
86 * Returns: a pointer into #GHashTable with data from the arguments.
89 arguments_parse (void)
96 const gchar *display_name;
99 args = g_hash_table_new_full (g_str_hash,
101 (GDestroyNotify)g_free,
102 (GDestroyNotify)value_free);
104 screen = gdk_screen_get_default ();
105 display = gdk_screen_get_display (screen);
107 display_name = gdk_display_get_name (display);
108 screen_number = gdk_screen_get_number (screen);
110 value = g_new0 (GValue, 1);
111 g_value_init (value, G_TYPE_STRING);
112 g_value_set_string (value, display_name);
113 g_hash_table_insert (args, g_strdup ("display"), value);
115 value = g_new0 (GValue, 1);
116 g_value_init (value, G_TYPE_INT);
117 g_value_set_int (value, screen_number);
118 g_hash_table_insert (args, g_strdup ("screen"), value);
121 value = g_new0 (GValue, 1);
122 g_value_init (value, G_TYPE_STRING);
123 g_value_set_string (value, ev_page_label);
125 g_hash_table_insert (args, g_strdup ("page-label"), value);
127 g_free (ev_page_label);
128 ev_page_label = NULL;
131 if (ev_find_string) {
132 value = g_new0 (GValue, 1);
133 g_value_init (value, G_TYPE_STRING);
134 g_value_set_string (value, ev_find_string);
136 g_hash_table_insert (args, g_strdup ("find-string"), value);
138 g_free (ev_find_string);
139 ev_page_label = NULL;
143 mode = EV_WINDOW_MODE_FULLSCREEN;
144 else if (presentation_mode)
145 mode = EV_WINDOW_MODE_PRESENTATION;
146 else if (preview_mode)
147 mode = EV_WINDOW_MODE_PREVIEW;
151 value = g_new0 (GValue, 1);
152 g_value_init (value, G_TYPE_UINT);
153 g_value_set_uint (value, mode);
155 g_hash_table_insert (args, g_strdup ("mode"), value);
157 if (mode == EV_WINDOW_MODE_PREVIEW && unlink_temp_file) {
158 value = g_new0 (GValue, 1);
159 g_value_init (value, G_TYPE_BOOLEAN);
160 g_value_set_boolean (value, unlink_temp_file);
162 g_hash_table_insert (args,
163 g_strdup ("unlink-temp-file"),
167 if (mode == EV_WINDOW_MODE_PREVIEW && print_settings) {
168 value = g_new0 (GValue, 1);
169 g_value_init (value, G_TYPE_STRING);
170 g_value_set_string (value, print_settings);
172 g_hash_table_insert (args,
173 g_strdup ("print-settings"),
175 g_free (print_settings);
176 print_settings = NULL;
183 load_files (const char **files,
189 ev_application_open_window (EV_APP, args, GDK_CURRENT_TIME, NULL);
193 for (i = 0; files[i]; i++) {
199 file = g_file_new_for_commandline_arg (files[i]);
200 uri = g_file_get_uri (file);
201 g_object_unref (file);
203 label = strchr (uri, '#');
210 old = g_hash_table_lookup (args, "page-label");
212 new = g_new0 (GValue, 1);
213 g_value_init (new, G_TYPE_STRING);
214 g_value_set_string (new, label);
216 g_hash_table_insert (args, g_strdup ("page-label"), new);
220 ev_application_open_uri (EV_APP, uri, args,
221 GDK_CURRENT_TIME, NULL);
224 g_hash_table_insert (args, g_strdup ("page-label"), old);
232 load_files_remote (const char **files,
236 GError *error = NULL;
237 DBusGConnection *connection;
238 gboolean result = FALSE;
239 DBusGProxy *remote_object;
243 display = gdk_display_get_default ();
244 timestamp = gdk_x11_display_get_user_time (display);
245 connection = dbus_g_bus_get (DBUS_BUS_STARTER, &error);
247 if (connection == NULL) {
248 g_warning ("%s", error->message);
249 g_error_free (error);
254 remote_object = dbus_g_proxy_new_for_name (connection,
255 "org.gnome.evince.ApplicationService",
256 "/org/gnome/evince/Evince",
257 "org.gnome.evince.Application");
259 if (!dbus_g_proxy_call (remote_object, "OpenWindow", &error,
260 dbus_g_type_get_map ("GHashTable", G_TYPE_STRING, G_TYPE_VALUE), args,
261 G_TYPE_UINT, timestamp,
264 g_warning ("%s", error->message);
265 g_clear_error (&error);
266 g_object_unref (remote_object);
267 dbus_g_connection_unref (connection);
271 g_object_unref (remote_object);
272 dbus_g_connection_unref (connection);
277 for (i = 0; files[i]; i++) {
278 const char *page_label;
282 file = g_file_new_for_commandline_arg (files[i]);
283 uri = g_file_get_uri (file);
284 g_object_unref (file);
286 page_label = ev_page_label ? ev_page_label : "";
288 if (!dbus_g_proxy_call (remote_object, "OpenURI", &error,
290 dbus_g_type_get_map ("GHashTable", G_TYPE_STRING, G_TYPE_VALUE), args,
291 G_TYPE_UINT, timestamp,
294 g_warning ("%s", error->message);
295 g_clear_error (&error);
304 g_object_unref (remote_object);
305 dbus_g_connection_unref (connection);
307 gdk_notify_startup_complete ();
311 #endif /* ENABLE_DBUS */
314 main (int argc, char *argv[])
316 GOptionContext *context;
318 GError *error = NULL;
320 /* Init glib threads asap */
321 if (!g_thread_supported ())
322 g_thread_init (NULL);
325 /* Initialize the i18n stuff */
326 bindtextdomain (GETTEXT_PACKAGE, GNOMELOCALEDIR);
327 bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
328 textdomain (GETTEXT_PACKAGE);
331 context = g_option_context_new (N_("GNOME Document Viewer"));
332 g_option_context_set_translation_domain(context, GETTEXT_PACKAGE);
333 g_option_context_add_main_entries (context, goption_options, GETTEXT_PACKAGE);
335 g_option_context_add_group (context, egg_sm_client_get_option_group ());
336 g_option_context_add_group (context, gtk_get_option_group (TRUE));
338 if (!g_option_context_parse (context, &argc, &argv, &error)) {
339 g_printerr ("Cannot parse arguments: %s", error->message);
340 g_error_free (error);
341 g_option_context_free (context);
345 g_option_context_free (context);
347 args = arguments_parse ();
350 if (!ev_application_register_service (EV_APP)) {
351 if (load_files_remote (file_arguments, args)) {
352 g_hash_table_destroy (args);
357 #endif /* ENABLE_DBUS */
360 ev_backends_manager_init ();
362 ev_file_helpers_init ();
363 ev_stock_icons_init ();
365 egg_set_desktop_file (GNOMEDATADIR "/applications/evince.desktop");
367 if (!ev_application_load_session (EV_APP))
368 load_files (file_arguments, args);
369 g_hash_table_destroy (args);
373 ev_file_helpers_shutdown ();
375 ev_backends_manager_shutdown ();
377 ev_debug_shutdown ();