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.
22 #include "ev-application.h"
23 #include "ev-metadata-manager.h"
25 #include <glib/gi18n.h>
27 #include <gtk/gtkmain.h>
32 #include <libgnome/gnome-program.h>
33 #include <libgnomeui/gnome-ui-init.h>
34 #include <libgnomeui/gnome-app-helper.h>
35 #include <libgnomeui/gnome-authentication-manager.h>
38 #include <libgnomevfs/gnome-vfs-init.h>
39 #include <libgnomevfs/gnome-vfs-utils.h>
42 #include <dbus/dbus-glib-bindings.h>
45 #include "ev-stock-icons.h"
46 #include "ev-job-queue.h"
47 #include "ev-file-helpers.h"
49 static gchar *ev_page_label;
50 static gchar *ev_find_string;
51 static gboolean preview_mode = FALSE;
52 static gboolean fullscren_mode = FALSE;
53 static gboolean presentation_mode = FALSE;
54 static gboolean unlink_temp_file = FALSE;
55 static gchar *print_settings;
56 static const char **file_arguments = NULL;
58 static const GOptionEntry goption_options[] =
60 { "page-label", 'p', 0, G_OPTION_ARG_STRING, &ev_page_label, N_("The page of the document to display."), N_("PAGE")},
61 { "fullscreen", 'f', 0, G_OPTION_ARG_NONE, &fullscren_mode, N_("Run evince in fullscreen mode"), NULL },
62 { "presentation", 's', 0, G_OPTION_ARG_NONE, &presentation_mode, N_("Run evince in presentation mode"), NULL },
63 { "preview", 'w', 0, G_OPTION_ARG_NONE, &preview_mode, N_("Run evince as a previewer"), NULL },
64 { "find", 'l', 0, G_OPTION_ARG_STRING, &ev_find_string, N_("The word or phrase to find in the document"), N_("STRING")},
65 { "unlink-tempfile", 'u', G_OPTION_FLAG_HIDDEN, G_OPTION_ARG_NONE, &unlink_temp_file, NULL, NULL },
66 { "print-settings", 't', G_OPTION_FLAG_HIDDEN, G_OPTION_ARG_FILENAME, &print_settings, NULL, NULL },
67 { G_OPTION_REMAINING, 0, 0, G_OPTION_ARG_FILENAME_ARRAY, &file_arguments, NULL, N_("[FILE...]") },
72 value_free (GValue *value)
74 g_value_unset (value);
81 * Parses the arguments and creates a #GHashTable with this data.
85 * dislay -> display at the default screen.
86 * screen -> screen number.
87 * page-label -> only if the page label argument has been passed,
88 * the page of the document to display.
89 * mode -> only if the view mode is one of the availables,
91 * unlink-temp-file -> only if the view mode is preview mode and
92 * unlink-temp-file has been passed, unlink-temp-file.
94 * Returns: a pointer into #GHashTable with data from the arguments.
97 arguments_parse (void)
101 EvWindowRunMode mode;
104 const gchar *display_name;
107 args = g_hash_table_new_full (g_str_hash,
109 (GDestroyNotify)g_free,
110 (GDestroyNotify)value_free);
112 screen = gdk_screen_get_default ();
113 display = gdk_screen_get_display (screen);
115 display_name = gdk_display_get_name (display);
116 screen_number = gdk_screen_get_number (screen);
118 value = g_new0 (GValue, 1);
119 g_value_init (value, G_TYPE_STRING);
120 g_value_set_string (value, display_name);
121 g_hash_table_insert (args, g_strdup ("display"), value);
123 value = g_new0 (GValue, 1);
124 g_value_init (value, G_TYPE_INT);
125 g_value_set_int (value, screen_number);
126 g_hash_table_insert (args, g_strdup ("screen"), value);
129 value = g_new0 (GValue, 1);
130 g_value_init (value, G_TYPE_STRING);
131 g_value_set_string (value, ev_page_label);
133 g_hash_table_insert (args, g_strdup ("page-label"), value);
135 g_free (ev_page_label);
136 ev_page_label = NULL;
139 if (ev_find_string) {
140 value = g_new0 (GValue, 1);
141 g_value_init (value, G_TYPE_STRING);
142 g_value_set_string (value, ev_find_string);
144 g_hash_table_insert (args, g_strdup ("find-string"), value);
146 g_free (ev_find_string);
147 ev_page_label = NULL;
151 mode = EV_WINDOW_MODE_FULLSCREEN;
152 else if (presentation_mode)
153 mode = EV_WINDOW_MODE_PRESENTATION;
154 else if (preview_mode)
155 mode = EV_WINDOW_MODE_PREVIEW;
159 value = g_new0 (GValue, 1);
160 g_value_init (value, G_TYPE_UINT);
161 g_value_set_uint (value, mode);
163 g_hash_table_insert (args, g_strdup ("mode"), value);
165 if (mode == EV_WINDOW_MODE_PREVIEW && unlink_temp_file) {
166 value = g_new0 (GValue, 1);
167 g_value_init (value, G_TYPE_BOOLEAN);
168 g_value_set_boolean (value, unlink_temp_file);
170 g_hash_table_insert (args,
171 g_strdup ("unlink-temp-file"),
175 if (mode == EV_WINDOW_MODE_PREVIEW && print_settings) {
176 value = g_new0 (GValue, 1);
177 g_value_init (value, G_TYPE_STRING);
178 g_value_set_string (value, print_settings);
180 g_hash_table_insert (args,
181 g_strdup ("print-settings"),
183 g_free (print_settings);
184 print_settings = NULL;
191 load_files (const char **files,
197 ev_application_open_window (EV_APP, args, GDK_CURRENT_TIME, NULL);
201 for (i = 0; files[i]; i++) {
206 uri = gnome_vfs_make_uri_from_shell_arg (files[i]);
208 label = strchr (uri, GNOME_VFS_URI_MAGIC_CHR);
215 old = g_hash_table_lookup (args, "page-label");
217 new = g_new0 (GValue, 1);
218 g_value_init (new, G_TYPE_STRING);
219 g_value_set_string (new, label);
221 g_hash_table_insert (args, g_strdup ("page-label"), new);
225 ev_application_open_uri (EV_APP, uri, args,
226 GDK_CURRENT_TIME, NULL);
229 g_hash_table_insert (args, g_strdup ("page-label"), old);
237 load_files_remote (const char **files,
241 GError *error = NULL;
242 DBusGConnection *connection;
243 gboolean result = FALSE;
244 DBusGProxy *remote_object;
248 display = gdk_display_get_default ();
249 timestamp = gdk_x11_display_get_user_time (display);
250 connection = dbus_g_bus_get (DBUS_BUS_STARTER, &error);
252 if (connection == NULL) {
253 g_warning (error->message);
254 g_error_free (error);
259 remote_object = dbus_g_proxy_new_for_name (connection,
260 "org.gnome.evince.ApplicationService",
261 "/org/gnome/evince/Evince",
262 "org.gnome.evince.Application");
264 if (!dbus_g_proxy_call (remote_object, "OpenWindow", &error,
265 dbus_g_type_get_map ("GHashTable", G_TYPE_STRING, G_TYPE_VALUE), args,
266 G_TYPE_UINT, timestamp,
269 g_warning (error->message);
270 g_clear_error (&error);
271 g_object_unref (remote_object);
272 dbus_g_connection_unref (connection);
276 g_object_unref (remote_object);
277 dbus_g_connection_unref (connection);
282 for (i = 0; files[i]; i++) {
283 const char *page_label;
286 uri = gnome_vfs_make_uri_from_shell_arg (files[i]);
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 (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 gboolean enable_metadata = FALSE;
318 GOptionContext *context;
321 GnomeProgram *program;
323 char *accel_filename;
324 GError *error = NULL;
327 context = g_option_context_new (_("GNOME Document Viewer"));
330 /* Initialize the i18n stuff */
331 bindtextdomain(GETTEXT_PACKAGE, GNOMELOCALEDIR);
332 bind_textdomain_codeset(GETTEXT_PACKAGE, "UTF-8");
333 textdomain(GETTEXT_PACKAGE);
334 g_option_context_add_main_entries (context, goption_options, GETTEXT_PACKAGE);
335 g_option_context_set_translation_domain(context, GETTEXT_PACKAGE);
337 g_option_context_add_main_entries (context, goption_options, NULL);
341 program = gnome_program_init (PACKAGE, VERSION,
342 LIBGNOMEUI_MODULE, argc, argv,
343 GNOME_PARAM_GOPTION_CONTEXT, context,
344 GNOME_PARAM_HUMAN_READABLE_NAME, _("Evince"),
345 GNOME_PARAM_APP_DATADIR, GNOMEDATADIR,
348 g_option_context_add_group (context, gtk_get_option_group (TRUE));
349 if (!g_option_context_parse (context, &argc, &argv, &error)) {
350 g_warning ("Cannot parse arguments: %s", error->message);
351 g_error_free (error);
354 g_option_context_free (context);
358 accel_filename = g_build_filename (ev_dot_dir (), "accels", NULL);
359 gtk_accel_map_load (accel_filename);
362 args = arguments_parse ();
365 if (!ev_application_register_service (EV_APP)) {
366 if (load_files_remote (file_arguments, args)) {
367 g_hash_table_destroy (args);
369 g_object_unref (program);
374 enable_metadata = TRUE;
379 gnome_authentication_manager_init ();
382 if (enable_metadata) {
383 ev_metadata_manager_init ();
386 ev_job_queue_init ();
387 g_set_application_name (_("Evince Document Viewer"));
389 ev_file_helpers_init ();
390 ev_stock_icons_init ();
391 gtk_window_set_default_icon_name ("evince");
393 load_files (file_arguments, args);
394 g_hash_table_destroy (args);
399 gnome_accelerators_sync ();
401 gtk_accel_map_save (accel_filename);
402 g_free (accel_filename);
405 ev_file_helpers_shutdown ();
407 if (enable_metadata) {
408 ev_metadata_manager_shutdown ();
412 g_object_unref (program);