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"
26 #include <glib/gi18n.h>
28 #include <gtk/gtkmain.h>
33 #include <libgnome/gnome-program.h>
34 #include <libgnomeui/gnome-ui-init.h>
35 #include <libgnomeui/gnome-app-helper.h>
36 #include <libgnomeui/gnome-authentication-manager.h>
40 #include <dbus/dbus-glib-bindings.h>
43 #include "ev-stock-icons.h"
44 #include "ev-job-queue.h"
45 #include "ev-file-helpers.h"
46 #include "ev-backends-manager.h"
48 static gchar *ev_page_label;
49 static gchar *ev_find_string;
50 static gboolean preview_mode = FALSE;
51 static gboolean fullscren_mode = FALSE;
52 static gboolean presentation_mode = FALSE;
53 static gboolean unlink_temp_file = FALSE;
54 static gchar *print_settings;
55 static const char **file_arguments = NULL;
57 static const GOptionEntry goption_options[] =
59 { "page-label", 'p', 0, G_OPTION_ARG_STRING, &ev_page_label, N_("The page of the document to display."), N_("PAGE")},
60 { "fullscreen", 'f', 0, G_OPTION_ARG_NONE, &fullscren_mode, N_("Run evince in fullscreen mode"), NULL },
61 { "presentation", 's', 0, G_OPTION_ARG_NONE, &presentation_mode, N_("Run evince in presentation mode"), NULL },
62 { "preview", 'w', 0, G_OPTION_ARG_NONE, &preview_mode, N_("Run evince as a previewer"), NULL },
63 { "find", 'l', 0, G_OPTION_ARG_STRING, &ev_find_string, N_("The word or phrase to find in the document"), N_("STRING")},
64 { "unlink-tempfile", 'u', G_OPTION_FLAG_HIDDEN, G_OPTION_ARG_NONE, &unlink_temp_file, NULL, NULL },
65 { "print-settings", 't', G_OPTION_FLAG_HIDDEN, G_OPTION_ARG_FILENAME, &print_settings, NULL, NULL },
66 { G_OPTION_REMAINING, 0, 0, G_OPTION_ARG_FILENAME_ARRAY, &file_arguments, NULL, N_("[FILE...]") },
71 value_free (GValue *value)
73 g_value_unset (value);
80 * Parses the arguments and creates a #GHashTable with this data.
84 * dislay -> display at the default screen.
85 * screen -> screen number.
86 * page-label -> only if the page label argument has been passed,
87 * the page of the document to display.
88 * mode -> only if the view mode is one of the availables,
90 * unlink-temp-file -> only if the view mode is preview mode and
91 * unlink-temp-file has been passed, unlink-temp-file.
93 * Returns: a pointer into #GHashTable with data from the arguments.
96 arguments_parse (void)
100 EvWindowRunMode mode;
103 const gchar *display_name;
106 args = g_hash_table_new_full (g_str_hash,
108 (GDestroyNotify)g_free,
109 (GDestroyNotify)value_free);
111 screen = gdk_screen_get_default ();
112 display = gdk_screen_get_display (screen);
114 display_name = gdk_display_get_name (display);
115 screen_number = gdk_screen_get_number (screen);
117 value = g_new0 (GValue, 1);
118 g_value_init (value, G_TYPE_STRING);
119 g_value_set_string (value, display_name);
120 g_hash_table_insert (args, g_strdup ("display"), value);
122 value = g_new0 (GValue, 1);
123 g_value_init (value, G_TYPE_INT);
124 g_value_set_int (value, screen_number);
125 g_hash_table_insert (args, g_strdup ("screen"), value);
128 value = g_new0 (GValue, 1);
129 g_value_init (value, G_TYPE_STRING);
130 g_value_set_string (value, ev_page_label);
132 g_hash_table_insert (args, g_strdup ("page-label"), value);
134 g_free (ev_page_label);
135 ev_page_label = NULL;
138 if (ev_find_string) {
139 value = g_new0 (GValue, 1);
140 g_value_init (value, G_TYPE_STRING);
141 g_value_set_string (value, ev_find_string);
143 g_hash_table_insert (args, g_strdup ("find-string"), value);
145 g_free (ev_find_string);
146 ev_page_label = NULL;
150 mode = EV_WINDOW_MODE_FULLSCREEN;
151 else if (presentation_mode)
152 mode = EV_WINDOW_MODE_PRESENTATION;
153 else if (preview_mode)
154 mode = EV_WINDOW_MODE_PREVIEW;
158 value = g_new0 (GValue, 1);
159 g_value_init (value, G_TYPE_UINT);
160 g_value_set_uint (value, mode);
162 g_hash_table_insert (args, g_strdup ("mode"), value);
164 if (mode == EV_WINDOW_MODE_PREVIEW && unlink_temp_file) {
165 value = g_new0 (GValue, 1);
166 g_value_init (value, G_TYPE_BOOLEAN);
167 g_value_set_boolean (value, unlink_temp_file);
169 g_hash_table_insert (args,
170 g_strdup ("unlink-temp-file"),
174 if (mode == EV_WINDOW_MODE_PREVIEW && print_settings) {
175 value = g_new0 (GValue, 1);
176 g_value_init (value, G_TYPE_STRING);
177 g_value_set_string (value, print_settings);
179 g_hash_table_insert (args,
180 g_strdup ("print-settings"),
182 g_free (print_settings);
183 print_settings = NULL;
190 load_files (const char **files,
196 ev_application_open_window (EV_APP, args, GDK_CURRENT_TIME, NULL);
200 for (i = 0; files[i]; i++) {
206 file = g_file_new_for_commandline_arg (files[i]);
207 uri = g_file_get_uri (file);
208 g_object_unref (file);
210 label = strchr (uri, '#');
217 old = g_hash_table_lookup (args, "page-label");
219 new = g_new0 (GValue, 1);
220 g_value_init (new, G_TYPE_STRING);
221 g_value_set_string (new, label);
223 g_hash_table_insert (args, g_strdup ("page-label"), new);
227 ev_application_open_uri (EV_APP, uri, args,
228 GDK_CURRENT_TIME, NULL);
231 g_hash_table_insert (args, g_strdup ("page-label"), old);
239 load_files_remote (const char **files,
243 GError *error = NULL;
244 DBusGConnection *connection;
245 gboolean result = FALSE;
246 DBusGProxy *remote_object;
250 display = gdk_display_get_default ();
251 timestamp = gdk_x11_display_get_user_time (display);
252 connection = dbus_g_bus_get (DBUS_BUS_STARTER, &error);
254 if (connection == NULL) {
255 g_warning ("%s", error->message);
256 g_error_free (error);
261 remote_object = dbus_g_proxy_new_for_name (connection,
262 "org.gnome.evince.ApplicationService",
263 "/org/gnome/evince/Evince",
264 "org.gnome.evince.Application");
266 if (!dbus_g_proxy_call (remote_object, "OpenWindow", &error,
267 dbus_g_type_get_map ("GHashTable", G_TYPE_STRING, G_TYPE_VALUE), args,
268 G_TYPE_UINT, timestamp,
271 g_warning ("%s", error->message);
272 g_clear_error (&error);
273 g_object_unref (remote_object);
274 dbus_g_connection_unref (connection);
278 g_object_unref (remote_object);
279 dbus_g_connection_unref (connection);
284 for (i = 0; files[i]; i++) {
285 const char *page_label;
289 file = g_file_new_for_commandline_arg (files[i]);
290 uri = g_file_get_uri (file);
291 g_object_unref (file);
293 page_label = ev_page_label ? ev_page_label : "";
295 if (!dbus_g_proxy_call (remote_object, "OpenURI", &error,
297 dbus_g_type_get_map ("GHashTable", G_TYPE_STRING, G_TYPE_VALUE), args,
298 G_TYPE_UINT, timestamp,
301 g_warning ("%s", error->message);
302 g_clear_error (&error);
311 g_object_unref (remote_object);
312 dbus_g_connection_unref (connection);
314 gdk_notify_startup_complete ();
318 #endif /* ENABLE_DBUS */
321 main (int argc, char *argv[])
323 gboolean enable_metadata = FALSE;
324 GOptionContext *context;
327 GnomeProgram *program;
329 char *accel_filename;
330 GError *error = NULL;
333 context = g_option_context_new (_("GNOME Document Viewer"));
336 /* Initialize the i18n stuff */
337 bindtextdomain(GETTEXT_PACKAGE, GNOMELOCALEDIR);
338 bind_textdomain_codeset(GETTEXT_PACKAGE, "UTF-8");
339 textdomain(GETTEXT_PACKAGE);
340 g_option_context_add_main_entries (context, goption_options, GETTEXT_PACKAGE);
341 g_option_context_set_translation_domain(context, GETTEXT_PACKAGE);
343 g_option_context_add_main_entries (context, goption_options, NULL);
347 program = gnome_program_init (PACKAGE, VERSION,
348 LIBGNOMEUI_MODULE, argc, argv,
349 GNOME_PARAM_GOPTION_CONTEXT, context,
350 GNOME_PARAM_HUMAN_READABLE_NAME, _("Evince"),
351 GNOME_PARAM_APP_DATADIR, GNOMEDATADIR,
354 g_option_context_add_group (context, gtk_get_option_group (TRUE));
355 if (!g_option_context_parse (context, &argc, &argv, &error)) {
356 g_warning ("Cannot parse arguments: %s", error->message);
357 g_error_free (error);
360 g_option_context_free (context);
362 accel_filename = g_build_filename (ev_dot_dir (), "accels", NULL);
363 gtk_accel_map_load (accel_filename);
366 args = arguments_parse ();
369 if (!ev_application_register_service (EV_APP)) {
370 if (load_files_remote (file_arguments, args)) {
371 g_hash_table_destroy (args);
373 g_object_unref (program);
378 enable_metadata = TRUE;
385 gnome_authentication_manager_init ();
388 ev_backends_manager_init ();
390 if (enable_metadata) {
391 ev_metadata_manager_init ();
394 ev_job_queue_init ();
395 g_set_application_name (_("Evince Document Viewer"));
397 ev_file_helpers_init ();
398 ev_stock_icons_init ();
399 gtk_window_set_default_icon_name ("evince");
401 load_files (file_arguments, args);
402 g_hash_table_destroy (args);
407 gnome_accelerators_sync ();
409 gtk_accel_map_save (accel_filename);
410 g_free (accel_filename);
413 ev_file_helpers_shutdown ();
415 if (enable_metadata) {
416 ev_metadata_manager_shutdown ();
419 ev_backends_manager_shutdown ();
422 g_object_unref (program);