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 value_free (GValue *value)
82 g_value_unset (value);
89 * Parses the arguments and creates a #GHashTable with this data.
93 * dislay -> display at the default screen.
94 * screen -> screen number.
95 * page-label -> only if the page label argument has been passed,
96 * the page of the document to display.
97 * mode -> only if the view mode is one of the availables,
99 * unlink-temp-file -> only if the view mode is preview mode and
100 * unlink-temp-file has been passed, unlink-temp-file.
102 * Returns: a pointer into #GHashTable with data from the arguments.
105 arguments_parse (void)
109 EvWindowRunMode mode;
112 const gchar *display_name;
115 args = g_hash_table_new_full (g_str_hash,
117 (GDestroyNotify)g_free,
118 (GDestroyNotify)value_free);
120 screen = gdk_screen_get_default ();
121 display = gdk_screen_get_display (screen);
123 display_name = gdk_display_get_name (display);
124 screen_number = gdk_screen_get_number (screen);
126 value = g_new0 (GValue, 1);
127 g_value_init (value, G_TYPE_STRING);
128 g_value_set_string (value, display_name);
129 g_hash_table_insert (args, g_strdup ("display"), value);
131 value = g_new0 (GValue, 1);
132 g_value_init (value, G_TYPE_INT);
133 g_value_set_int (value, screen_number);
134 g_hash_table_insert (args, g_strdup ("screen"), value);
137 value = g_new0 (GValue, 1);
138 g_value_init (value, G_TYPE_STRING);
139 g_value_set_string (value, ev_page_label);
141 g_hash_table_insert (args, g_strdup ("page-label"), value);
143 g_free (ev_page_label);
144 ev_page_label = NULL;
147 if (ev_find_string) {
148 value = g_new0 (GValue, 1);
149 g_value_init (value, G_TYPE_STRING);
150 g_value_set_string (value, ev_find_string);
152 g_hash_table_insert (args, g_strdup ("find-string"), value);
154 g_free (ev_find_string);
155 ev_page_label = NULL;
159 mode = EV_WINDOW_MODE_FULLSCREEN;
160 else if (presentation_mode)
161 mode = EV_WINDOW_MODE_PRESENTATION;
162 else if (preview_mode)
163 mode = EV_WINDOW_MODE_PREVIEW;
167 value = g_new0 (GValue, 1);
168 g_value_init (value, G_TYPE_UINT);
169 g_value_set_uint (value, mode);
171 g_hash_table_insert (args, g_strdup ("mode"), value);
173 if (mode == EV_WINDOW_MODE_PREVIEW && unlink_temp_file) {
174 value = g_new0 (GValue, 1);
175 g_value_init (value, G_TYPE_BOOLEAN);
176 g_value_set_boolean (value, unlink_temp_file);
178 g_hash_table_insert (args,
179 g_strdup ("unlink-temp-file"),
183 if (mode == EV_WINDOW_MODE_PREVIEW && print_settings) {
184 value = g_new0 (GValue, 1);
185 g_value_init (value, G_TYPE_STRING);
186 g_value_set_string (value, print_settings);
188 g_hash_table_insert (args,
189 g_strdup ("print-settings"),
191 g_free (print_settings);
192 print_settings = NULL;
199 load_files (const char **files,
205 ev_application_open_window (EV_APP, args, GDK_CURRENT_TIME, NULL);
209 for (i = 0; files[i]; i++) {
215 file = g_file_new_for_commandline_arg (files[i]);
216 uri = g_file_get_uri (file);
217 g_object_unref (file);
219 label = strchr (uri, '#');
226 old = g_hash_table_lookup (args, "page-label");
228 new = g_new0 (GValue, 1);
229 g_value_init (new, G_TYPE_STRING);
230 g_value_set_string (new, label);
232 g_hash_table_insert (args, g_strdup ("page-label"), new);
236 ev_application_open_uri (EV_APP, uri, args,
237 GDK_CURRENT_TIME, NULL);
240 g_hash_table_insert (args, g_strdup ("page-label"), old);
248 load_files_remote (const char **files,
252 GError *error = NULL;
253 DBusGConnection *connection;
254 gboolean result = FALSE;
255 DBusGProxy *remote_object;
259 display = gdk_display_get_default ();
260 timestamp = gdk_x11_display_get_user_time (display);
261 connection = dbus_g_bus_get (DBUS_BUS_STARTER, &error);
263 if (connection == NULL) {
264 g_warning ("%s", error->message);
265 g_error_free (error);
270 remote_object = dbus_g_proxy_new_for_name (connection,
271 "org.gnome.evince.ApplicationService",
272 "/org/gnome/evince/Evince",
273 "org.gnome.evince.Application");
275 if (!dbus_g_proxy_call (remote_object, "OpenWindow", &error,
276 dbus_g_type_get_map ("GHashTable", G_TYPE_STRING, G_TYPE_VALUE), args,
277 G_TYPE_UINT, timestamp,
280 g_warning ("%s", error->message);
281 g_clear_error (&error);
282 g_object_unref (remote_object);
283 dbus_g_connection_unref (connection);
287 g_object_unref (remote_object);
288 dbus_g_connection_unref (connection);
293 for (i = 0; files[i]; i++) {
294 const char *page_label;
298 file = g_file_new_for_commandline_arg (files[i]);
299 uri = g_file_get_uri (file);
300 g_object_unref (file);
302 page_label = ev_page_label ? ev_page_label : "";
304 if (!dbus_g_proxy_call (remote_object, "OpenURI", &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);
320 g_object_unref (remote_object);
321 dbus_g_connection_unref (connection);
323 gdk_notify_startup_complete ();
327 #endif /* ENABLE_DBUS */
330 main (int argc, char *argv[])
332 GOptionContext *context;
334 GError *error = NULL;
336 /* Init glib threads asap */
337 if (!g_thread_supported ())
338 g_thread_init (NULL);
341 /* Initialize the i18n stuff */
342 bindtextdomain (GETTEXT_PACKAGE, GNOMELOCALEDIR);
343 bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
344 textdomain (GETTEXT_PACKAGE);
347 context = g_option_context_new (N_("GNOME Document Viewer"));
348 g_option_context_set_translation_domain(context, GETTEXT_PACKAGE);
349 g_option_context_add_main_entries (context, goption_options, GETTEXT_PACKAGE);
351 g_option_context_add_group (context, egg_sm_client_get_option_group ());
352 g_option_context_add_group (context, gtk_get_option_group (TRUE));
354 if (!g_option_context_parse (context, &argc, &argv, &error)) {
355 g_printerr ("Cannot parse arguments: %s", error->message);
356 g_error_free (error);
357 g_option_context_free (context);
361 g_option_context_free (context);
363 args = arguments_parse ();
366 if (!ev_application_register_service (EV_APP)) {
367 if (load_files_remote (file_arguments, args)) {
368 g_hash_table_destroy (args);
373 #endif /* ENABLE_DBUS */
378 ev_stock_icons_init ();
381 /* Manually set name and icon in win32 */
382 g_set_application_name (_("Evince"));
383 gtk_window_set_default_icon_name ("evince");
385 egg_set_desktop_file (GNOMEDATADIR "/applications/evince.desktop");
386 #endif /* G_OS_WIN32 */
388 if (!ev_application_load_session (EV_APP))
389 load_files (file_arguments, args);
390 g_hash_table_destroy (args);