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;
51 option_version_cb (const gchar *option_name,
56 g_print ("%s %s\n", _("GNOME Document Viewer"), VERSION);
62 static const GOptionEntry goption_options[] =
64 { "page-label", 'p', 0, G_OPTION_ARG_STRING, &ev_page_label, N_("The page of the document to display."), N_("PAGE")},
65 { "fullscreen", 'f', 0, G_OPTION_ARG_NONE, &fullscreen_mode, N_("Run evince in fullscreen mode"), NULL },
66 { "presentation", 's', 0, G_OPTION_ARG_NONE, &presentation_mode, N_("Run evince in presentation mode"), NULL },
67 { "preview", 'w', 0, G_OPTION_ARG_NONE, &preview_mode, N_("Run evince as a previewer"), NULL },
68 { "find", 'l', 0, G_OPTION_ARG_STRING, &ev_find_string, N_("The word or phrase to find in the document"), N_("STRING")},
69 { "unlink-tempfile", 'u', G_OPTION_FLAG_HIDDEN, G_OPTION_ARG_NONE, &unlink_temp_file, NULL, NULL },
70 { "print-settings", 't', G_OPTION_FLAG_HIDDEN, G_OPTION_ARG_FILENAME, &print_settings, NULL, NULL },
71 { "version", 0, G_OPTION_FLAG_NO_ARG | G_OPTION_FLAG_HIDDEN, G_OPTION_ARG_CALLBACK, option_version_cb, NULL, NULL },
72 { G_OPTION_REMAINING, 0, 0, G_OPTION_ARG_FILENAME_ARRAY, &file_arguments, NULL, N_("[FILE...]") },
77 value_free (GValue *value)
79 g_value_unset (value);
86 * Parses the arguments and creates a #GHashTable with this data.
90 * dislay -> display at the default screen.
91 * screen -> screen number.
92 * page-label -> only if the page label argument has been passed,
93 * the page of the document to display.
94 * mode -> only if the view mode is one of the availables,
96 * unlink-temp-file -> only if the view mode is preview mode and
97 * unlink-temp-file has been passed, unlink-temp-file.
99 * Returns: a pointer into #GHashTable with data from the arguments.
102 arguments_parse (void)
106 EvWindowRunMode mode;
109 const gchar *display_name;
112 args = g_hash_table_new_full (g_str_hash,
114 (GDestroyNotify)g_free,
115 (GDestroyNotify)value_free);
117 screen = gdk_screen_get_default ();
118 display = gdk_screen_get_display (screen);
120 display_name = gdk_display_get_name (display);
121 screen_number = gdk_screen_get_number (screen);
123 value = g_new0 (GValue, 1);
124 g_value_init (value, G_TYPE_STRING);
125 g_value_set_string (value, display_name);
126 g_hash_table_insert (args, g_strdup ("display"), value);
128 value = g_new0 (GValue, 1);
129 g_value_init (value, G_TYPE_INT);
130 g_value_set_int (value, screen_number);
131 g_hash_table_insert (args, g_strdup ("screen"), value);
134 value = g_new0 (GValue, 1);
135 g_value_init (value, G_TYPE_STRING);
136 g_value_set_string (value, ev_page_label);
138 g_hash_table_insert (args, g_strdup ("page-label"), value);
140 g_free (ev_page_label);
141 ev_page_label = NULL;
144 if (ev_find_string) {
145 value = g_new0 (GValue, 1);
146 g_value_init (value, G_TYPE_STRING);
147 g_value_set_string (value, ev_find_string);
149 g_hash_table_insert (args, g_strdup ("find-string"), value);
151 g_free (ev_find_string);
152 ev_page_label = NULL;
156 mode = EV_WINDOW_MODE_FULLSCREEN;
157 else if (presentation_mode)
158 mode = EV_WINDOW_MODE_PRESENTATION;
159 else if (preview_mode)
160 mode = EV_WINDOW_MODE_PREVIEW;
164 value = g_new0 (GValue, 1);
165 g_value_init (value, G_TYPE_UINT);
166 g_value_set_uint (value, mode);
168 g_hash_table_insert (args, g_strdup ("mode"), value);
170 if (mode == EV_WINDOW_MODE_PREVIEW && unlink_temp_file) {
171 value = g_new0 (GValue, 1);
172 g_value_init (value, G_TYPE_BOOLEAN);
173 g_value_set_boolean (value, unlink_temp_file);
175 g_hash_table_insert (args,
176 g_strdup ("unlink-temp-file"),
180 if (mode == EV_WINDOW_MODE_PREVIEW && print_settings) {
181 value = g_new0 (GValue, 1);
182 g_value_init (value, G_TYPE_STRING);
183 g_value_set_string (value, print_settings);
185 g_hash_table_insert (args,
186 g_strdup ("print-settings"),
188 g_free (print_settings);
189 print_settings = NULL;
196 load_files (const char **files,
202 ev_application_open_window (EV_APP, args, GDK_CURRENT_TIME, NULL);
206 for (i = 0; files[i]; i++) {
212 file = g_file_new_for_commandline_arg (files[i]);
213 uri = g_file_get_uri (file);
214 g_object_unref (file);
216 label = strchr (uri, '#');
223 old = g_hash_table_lookup (args, "page-label");
225 new = g_new0 (GValue, 1);
226 g_value_init (new, G_TYPE_STRING);
227 g_value_set_string (new, label);
229 g_hash_table_insert (args, g_strdup ("page-label"), new);
233 ev_application_open_uri (EV_APP, uri, args,
234 GDK_CURRENT_TIME, NULL);
237 g_hash_table_insert (args, g_strdup ("page-label"), old);
245 load_files_remote (const char **files,
249 GError *error = NULL;
250 DBusGConnection *connection;
251 gboolean result = FALSE;
252 DBusGProxy *remote_object;
256 display = gdk_display_get_default ();
257 timestamp = gdk_x11_display_get_user_time (display);
258 connection = dbus_g_bus_get (DBUS_BUS_STARTER, &error);
260 if (connection == NULL) {
261 g_warning ("%s", error->message);
262 g_error_free (error);
267 remote_object = dbus_g_proxy_new_for_name (connection,
268 "org.gnome.evince.ApplicationService",
269 "/org/gnome/evince/Evince",
270 "org.gnome.evince.Application");
272 if (!dbus_g_proxy_call (remote_object, "OpenWindow", &error,
273 dbus_g_type_get_map ("GHashTable", G_TYPE_STRING, G_TYPE_VALUE), args,
274 G_TYPE_UINT, timestamp,
277 g_warning ("%s", error->message);
278 g_clear_error (&error);
279 g_object_unref (remote_object);
280 dbus_g_connection_unref (connection);
284 g_object_unref (remote_object);
285 dbus_g_connection_unref (connection);
290 for (i = 0; files[i]; i++) {
291 const char *page_label;
295 file = g_file_new_for_commandline_arg (files[i]);
296 uri = g_file_get_uri (file);
297 g_object_unref (file);
299 page_label = ev_page_label ? ev_page_label : "";
301 if (!dbus_g_proxy_call (remote_object, "OpenURI", &error,
303 dbus_g_type_get_map ("GHashTable", G_TYPE_STRING, G_TYPE_VALUE), args,
304 G_TYPE_UINT, timestamp,
307 g_warning ("%s", error->message);
308 g_clear_error (&error);
317 g_object_unref (remote_object);
318 dbus_g_connection_unref (connection);
320 gdk_notify_startup_complete ();
324 #endif /* ENABLE_DBUS */
327 main (int argc, char *argv[])
329 GOptionContext *context;
331 GError *error = NULL;
333 /* Init glib threads asap */
334 if (!g_thread_supported ())
335 g_thread_init (NULL);
338 /* Initialize the i18n stuff */
339 bindtextdomain (GETTEXT_PACKAGE, GNOMELOCALEDIR);
340 bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
341 textdomain (GETTEXT_PACKAGE);
344 context = g_option_context_new (N_("GNOME Document Viewer"));
345 g_option_context_set_translation_domain(context, GETTEXT_PACKAGE);
346 g_option_context_add_main_entries (context, goption_options, GETTEXT_PACKAGE);
348 g_option_context_add_group (context, egg_sm_client_get_option_group ());
349 g_option_context_add_group (context, gtk_get_option_group (TRUE));
351 if (!g_option_context_parse (context, &argc, &argv, &error)) {
352 g_printerr ("Cannot parse arguments: %s", error->message);
353 g_error_free (error);
354 g_option_context_free (context);
358 g_option_context_free (context);
360 args = arguments_parse ();
363 if (!ev_application_register_service (EV_APP)) {
364 if (load_files_remote (file_arguments, args)) {
365 g_hash_table_destroy (args);
370 #endif /* ENABLE_DBUS */
373 ev_backends_manager_init ();
375 ev_file_helpers_init ();
376 ev_stock_icons_init ();
378 egg_set_desktop_file (GNOMEDATADIR "/applications/evince.desktop");
380 if (!ev_application_load_session (EV_APP))
381 load_files (file_arguments, args);
382 g_hash_table_destroy (args);
386 ev_file_helpers_shutdown ();
388 ev_backends_manager_shutdown ();
390 ev_debug_shutdown ();