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-metadata-manager.h"
38 #include "ev-stock-icons.h"
39 #include "eggsmclient.h"
40 #include "eggdesktopfile.h"
42 static gchar *ev_page_label;
43 static gchar *ev_find_string;
44 static gboolean preview_mode = FALSE;
45 static gboolean fullscreen_mode = FALSE;
46 static gboolean presentation_mode = FALSE;
47 static gboolean unlink_temp_file = FALSE;
48 static gchar *print_settings;
49 static const char **file_arguments = NULL;
51 static const GOptionEntry goption_options[] =
53 { "page-label", 'p', 0, G_OPTION_ARG_STRING, &ev_page_label, N_("The page of the document to display."), N_("PAGE")},
54 { "fullscreen", 'f', 0, G_OPTION_ARG_NONE, &fullscreen_mode, N_("Run evince in fullscreen mode"), NULL },
55 { "presentation", 's', 0, G_OPTION_ARG_NONE, &presentation_mode, N_("Run evince in presentation mode"), NULL },
56 { "preview", 'w', 0, G_OPTION_ARG_NONE, &preview_mode, N_("Run evince as a previewer"), NULL },
57 { "find", 'l', 0, G_OPTION_ARG_STRING, &ev_find_string, N_("The word or phrase to find in the document"), N_("STRING")},
58 { "unlink-tempfile", 'u', G_OPTION_FLAG_HIDDEN, G_OPTION_ARG_NONE, &unlink_temp_file, NULL, NULL },
59 { "print-settings", 't', G_OPTION_FLAG_HIDDEN, G_OPTION_ARG_FILENAME, &print_settings, NULL, NULL },
60 { G_OPTION_REMAINING, 0, 0, G_OPTION_ARG_FILENAME_ARRAY, &file_arguments, NULL, N_("[FILE...]") },
65 value_free (GValue *value)
67 g_value_unset (value);
74 * Parses the arguments and creates a #GHashTable with this data.
78 * dislay -> display at the default screen.
79 * screen -> screen number.
80 * page-label -> only if the page label argument has been passed,
81 * the page of the document to display.
82 * mode -> only if the view mode is one of the availables,
84 * unlink-temp-file -> only if the view mode is preview mode and
85 * unlink-temp-file has been passed, unlink-temp-file.
87 * Returns: a pointer into #GHashTable with data from the arguments.
90 arguments_parse (void)
97 const gchar *display_name;
100 args = g_hash_table_new_full (g_str_hash,
102 (GDestroyNotify)g_free,
103 (GDestroyNotify)value_free);
105 screen = gdk_screen_get_default ();
106 display = gdk_screen_get_display (screen);
108 display_name = gdk_display_get_name (display);
109 screen_number = gdk_screen_get_number (screen);
111 value = g_new0 (GValue, 1);
112 g_value_init (value, G_TYPE_STRING);
113 g_value_set_string (value, display_name);
114 g_hash_table_insert (args, g_strdup ("display"), value);
116 value = g_new0 (GValue, 1);
117 g_value_init (value, G_TYPE_INT);
118 g_value_set_int (value, screen_number);
119 g_hash_table_insert (args, g_strdup ("screen"), value);
122 value = g_new0 (GValue, 1);
123 g_value_init (value, G_TYPE_STRING);
124 g_value_set_string (value, ev_page_label);
126 g_hash_table_insert (args, g_strdup ("page-label"), value);
128 g_free (ev_page_label);
129 ev_page_label = NULL;
132 if (ev_find_string) {
133 value = g_new0 (GValue, 1);
134 g_value_init (value, G_TYPE_STRING);
135 g_value_set_string (value, ev_find_string);
137 g_hash_table_insert (args, g_strdup ("find-string"), value);
139 g_free (ev_find_string);
140 ev_page_label = NULL;
144 mode = EV_WINDOW_MODE_FULLSCREEN;
145 else if (presentation_mode)
146 mode = EV_WINDOW_MODE_PRESENTATION;
147 else if (preview_mode)
148 mode = EV_WINDOW_MODE_PREVIEW;
152 value = g_new0 (GValue, 1);
153 g_value_init (value, G_TYPE_UINT);
154 g_value_set_uint (value, mode);
156 g_hash_table_insert (args, g_strdup ("mode"), value);
158 if (mode == EV_WINDOW_MODE_PREVIEW && unlink_temp_file) {
159 value = g_new0 (GValue, 1);
160 g_value_init (value, G_TYPE_BOOLEAN);
161 g_value_set_boolean (value, unlink_temp_file);
163 g_hash_table_insert (args,
164 g_strdup ("unlink-temp-file"),
168 if (mode == EV_WINDOW_MODE_PREVIEW && print_settings) {
169 value = g_new0 (GValue, 1);
170 g_value_init (value, G_TYPE_STRING);
171 g_value_set_string (value, print_settings);
173 g_hash_table_insert (args,
174 g_strdup ("print-settings"),
176 g_free (print_settings);
177 print_settings = NULL;
184 load_files (const char **files,
190 ev_application_open_window (EV_APP, args, GDK_CURRENT_TIME, NULL);
194 for (i = 0; files[i]; i++) {
200 file = g_file_new_for_commandline_arg (files[i]);
201 uri = g_file_get_uri (file);
202 g_object_unref (file);
204 label = strchr (uri, '#');
211 old = g_hash_table_lookup (args, "page-label");
213 new = g_new0 (GValue, 1);
214 g_value_init (new, G_TYPE_STRING);
215 g_value_set_string (new, label);
217 g_hash_table_insert (args, g_strdup ("page-label"), new);
221 ev_application_open_uri (EV_APP, uri, args,
222 GDK_CURRENT_TIME, NULL);
225 g_hash_table_insert (args, g_strdup ("page-label"), old);
233 load_files_remote (const char **files,
237 GError *error = NULL;
238 DBusGConnection *connection;
239 gboolean result = FALSE;
240 DBusGProxy *remote_object;
244 display = gdk_display_get_default ();
245 timestamp = gdk_x11_display_get_user_time (display);
246 connection = dbus_g_bus_get (DBUS_BUS_STARTER, &error);
248 if (connection == NULL) {
249 g_warning ("%s", error->message);
250 g_error_free (error);
255 remote_object = dbus_g_proxy_new_for_name (connection,
256 "org.gnome.evince.ApplicationService",
257 "/org/gnome/evince/Evince",
258 "org.gnome.evince.Application");
260 if (!dbus_g_proxy_call (remote_object, "OpenWindow", &error,
261 dbus_g_type_get_map ("GHashTable", G_TYPE_STRING, G_TYPE_VALUE), args,
262 G_TYPE_UINT, timestamp,
265 g_warning ("%s", error->message);
266 g_clear_error (&error);
267 g_object_unref (remote_object);
268 dbus_g_connection_unref (connection);
272 g_object_unref (remote_object);
273 dbus_g_connection_unref (connection);
278 for (i = 0; files[i]; i++) {
279 const char *page_label;
283 file = g_file_new_for_commandline_arg (files[i]);
284 uri = g_file_get_uri (file);
285 g_object_unref (file);
287 page_label = ev_page_label ? ev_page_label : "";
289 if (!dbus_g_proxy_call (remote_object, "OpenURI", &error,
291 dbus_g_type_get_map ("GHashTable", G_TYPE_STRING, G_TYPE_VALUE), args,
292 G_TYPE_UINT, timestamp,
295 g_warning ("%s", error->message);
296 g_clear_error (&error);
305 g_object_unref (remote_object);
306 dbus_g_connection_unref (connection);
308 gdk_notify_startup_complete ();
312 #endif /* ENABLE_DBUS */
315 main (int argc, char *argv[])
317 GOptionContext *context;
319 gboolean enable_metadata = FALSE;
320 GError *error = NULL;
322 /* Init glib threads asap */
323 if (!g_thread_supported ())
324 g_thread_init (NULL);
326 context = g_option_context_new (_("GNOME Document Viewer"));
329 /* Initialize the i18n stuff */
330 bindtextdomain (GETTEXT_PACKAGE, GNOMELOCALEDIR);
331 bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
332 textdomain (GETTEXT_PACKAGE);
333 g_option_context_add_main_entries (context, goption_options, GETTEXT_PACKAGE);
334 g_option_context_set_translation_domain(context, GETTEXT_PACKAGE);
336 g_option_context_add_main_entries (context, goption_options, NULL);
337 #endif /* ENABLE_NLS */
339 g_option_context_add_group (context, egg_sm_client_get_option_group ());
340 g_option_context_add_group (context, gtk_get_option_group (TRUE));
342 gtk_init (&argc, &argv);
344 if (!g_option_context_parse (context, &argc, &argv, &error)) {
345 g_warning ("Cannot parse arguments: %s", error->message);
346 g_error_free (error);
347 g_option_context_free (context);
351 g_option_context_free (context);
353 args = arguments_parse ();
356 if (!ev_application_register_service (EV_APP)) {
357 if (load_files_remote (file_arguments, args)) {
358 g_hash_table_destroy (args);
363 enable_metadata = TRUE;
365 #endif /* ENABLE_DBUS */
368 ev_backends_manager_init ();
370 if (enable_metadata) {
371 ev_metadata_manager_init ();
374 ev_file_helpers_init ();
375 ev_stock_icons_init ();
377 egg_set_desktop_file (GNOMEDATADIR "/applications/evince.desktop");
379 if (!ev_application_load_session (EV_APP))
380 load_files (file_arguments, args);
381 g_hash_table_destroy (args);
385 ev_file_helpers_shutdown ();
387 if (enable_metadata) {
388 ev_metadata_manager_shutdown ();
391 ev_backends_manager_shutdown ();
393 ev_debug_shutdown ();