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>
39 #include <dbus/dbus-glib-bindings.h>
42 #include "ev-stock-icons.h"
43 #include "ev-job-queue.h"
44 #include "ev-file-helpers.h"
45 #include "ev-backends-manager.h"
47 static gchar *ev_page_label;
48 static gchar *ev_find_string;
49 static gboolean preview_mode = FALSE;
50 static gboolean fullscren_mode = FALSE;
51 static gboolean presentation_mode = FALSE;
52 static gboolean unlink_temp_file = FALSE;
53 static gchar *print_settings;
54 static const char **file_arguments = NULL;
56 static const GOptionEntry goption_options[] =
58 { "page-label", 'p', 0, G_OPTION_ARG_STRING, &ev_page_label, N_("The page of the document to display."), N_("PAGE")},
59 { "fullscreen", 'f', 0, G_OPTION_ARG_NONE, &fullscren_mode, N_("Run evince in fullscreen mode"), NULL },
60 { "presentation", 's', 0, G_OPTION_ARG_NONE, &presentation_mode, N_("Run evince in presentation mode"), NULL },
61 { "preview", 'w', 0, G_OPTION_ARG_NONE, &preview_mode, N_("Run evince as a previewer"), NULL },
62 { "find", 'l', 0, G_OPTION_ARG_STRING, &ev_find_string, N_("The word or phrase to find in the document"), N_("STRING")},
63 { "unlink-tempfile", 'u', G_OPTION_FLAG_HIDDEN, G_OPTION_ARG_NONE, &unlink_temp_file, NULL, NULL },
64 { "print-settings", 't', G_OPTION_FLAG_HIDDEN, G_OPTION_ARG_FILENAME, &print_settings, NULL, NULL },
65 { G_OPTION_REMAINING, 0, 0, G_OPTION_ARG_FILENAME_ARRAY, &file_arguments, NULL, N_("[FILE...]") },
70 value_free (GValue *value)
72 g_value_unset (value);
79 * Parses the arguments and creates a #GHashTable with this data.
83 * dislay -> display at the default screen.
84 * screen -> screen number.
85 * page-label -> only if the page label argument has been passed,
86 * the page of the document to display.
87 * mode -> only if the view mode is one of the availables,
89 * unlink-temp-file -> only if the view mode is preview mode and
90 * unlink-temp-file has been passed, unlink-temp-file.
92 * Returns: a pointer into #GHashTable with data from the arguments.
95 arguments_parse (void)
102 const gchar *display_name;
105 args = g_hash_table_new_full (g_str_hash,
107 (GDestroyNotify)g_free,
108 (GDestroyNotify)value_free);
110 screen = gdk_screen_get_default ();
111 display = gdk_screen_get_display (screen);
113 display_name = gdk_display_get_name (display);
114 screen_number = gdk_screen_get_number (screen);
116 value = g_new0 (GValue, 1);
117 g_value_init (value, G_TYPE_STRING);
118 g_value_set_string (value, display_name);
119 g_hash_table_insert (args, g_strdup ("display"), value);
121 value = g_new0 (GValue, 1);
122 g_value_init (value, G_TYPE_INT);
123 g_value_set_int (value, screen_number);
124 g_hash_table_insert (args, g_strdup ("screen"), value);
127 value = g_new0 (GValue, 1);
128 g_value_init (value, G_TYPE_STRING);
129 g_value_set_string (value, ev_page_label);
131 g_hash_table_insert (args, g_strdup ("page-label"), value);
133 g_free (ev_page_label);
134 ev_page_label = NULL;
137 if (ev_find_string) {
138 value = g_new0 (GValue, 1);
139 g_value_init (value, G_TYPE_STRING);
140 g_value_set_string (value, ev_find_string);
142 g_hash_table_insert (args, g_strdup ("find-string"), value);
144 g_free (ev_find_string);
145 ev_page_label = NULL;
149 mode = EV_WINDOW_MODE_FULLSCREEN;
150 else if (presentation_mode)
151 mode = EV_WINDOW_MODE_PRESENTATION;
152 else if (preview_mode)
153 mode = EV_WINDOW_MODE_PREVIEW;
157 value = g_new0 (GValue, 1);
158 g_value_init (value, G_TYPE_UINT);
159 g_value_set_uint (value, mode);
161 g_hash_table_insert (args, g_strdup ("mode"), value);
163 if (mode == EV_WINDOW_MODE_PREVIEW && unlink_temp_file) {
164 value = g_new0 (GValue, 1);
165 g_value_init (value, G_TYPE_BOOLEAN);
166 g_value_set_boolean (value, unlink_temp_file);
168 g_hash_table_insert (args,
169 g_strdup ("unlink-temp-file"),
173 if (mode == EV_WINDOW_MODE_PREVIEW && print_settings) {
174 value = g_new0 (GValue, 1);
175 g_value_init (value, G_TYPE_STRING);
176 g_value_set_string (value, print_settings);
178 g_hash_table_insert (args,
179 g_strdup ("print-settings"),
181 g_free (print_settings);
182 print_settings = NULL;
189 load_files (const char **files,
195 ev_application_open_window (EV_APP, args, GDK_CURRENT_TIME, NULL);
199 for (i = 0; files[i]; i++) {
205 file = g_file_new_for_commandline_arg (files[i]);
206 uri = g_file_get_uri (file);
207 g_object_unref (file);
209 label = strchr (uri, '#');
216 old = g_hash_table_lookup (args, "page-label");
218 new = g_new0 (GValue, 1);
219 g_value_init (new, G_TYPE_STRING);
220 g_value_set_string (new, label);
222 g_hash_table_insert (args, g_strdup ("page-label"), new);
226 ev_application_open_uri (EV_APP, uri, args,
227 GDK_CURRENT_TIME, NULL);
230 g_hash_table_insert (args, g_strdup ("page-label"), old);
238 load_files_remote (const char **files,
242 GError *error = NULL;
243 DBusGConnection *connection;
244 gboolean result = FALSE;
245 DBusGProxy *remote_object;
249 display = gdk_display_get_default ();
250 timestamp = gdk_x11_display_get_user_time (display);
251 connection = dbus_g_bus_get (DBUS_BUS_STARTER, &error);
253 if (connection == NULL) {
254 g_warning (error->message);
255 g_error_free (error);
260 remote_object = dbus_g_proxy_new_for_name (connection,
261 "org.gnome.evince.ApplicationService",
262 "/org/gnome/evince/Evince",
263 "org.gnome.evince.Application");
265 if (!dbus_g_proxy_call (remote_object, "OpenWindow", &error,
266 dbus_g_type_get_map ("GHashTable", G_TYPE_STRING, G_TYPE_VALUE), args,
267 G_TYPE_UINT, timestamp,
270 g_warning (error->message);
271 g_clear_error (&error);
272 g_object_unref (remote_object);
273 dbus_g_connection_unref (connection);
277 g_object_unref (remote_object);
278 dbus_g_connection_unref (connection);
283 for (i = 0; files[i]; i++) {
284 const char *page_label;
288 file = g_file_new_for_commandline_arg (files[i]);
289 uri = g_file_get_uri (file);
290 g_object_unref (file);
292 page_label = ev_page_label ? ev_page_label : "";
294 if (!dbus_g_proxy_call (remote_object, "OpenURI", &error,
296 dbus_g_type_get_map ("GHashTable", G_TYPE_STRING, G_TYPE_VALUE), args,
297 G_TYPE_UINT, timestamp,
300 g_warning (error->message);
301 g_clear_error (&error);
310 g_object_unref (remote_object);
311 dbus_g_connection_unref (connection);
313 gdk_notify_startup_complete ();
317 #endif /* ENABLE_DBUS */
320 main (int argc, char *argv[])
322 gboolean enable_metadata = FALSE;
323 GOptionContext *context;
326 GnomeProgram *program;
328 char *accel_filename;
329 GError *error = NULL;
332 context = g_option_context_new (_("GNOME Document Viewer"));
335 /* Initialize the i18n stuff */
336 bindtextdomain(GETTEXT_PACKAGE, GNOMELOCALEDIR);
337 bind_textdomain_codeset(GETTEXT_PACKAGE, "UTF-8");
338 textdomain(GETTEXT_PACKAGE);
339 g_option_context_add_main_entries (context, goption_options, GETTEXT_PACKAGE);
340 g_option_context_set_translation_domain(context, GETTEXT_PACKAGE);
342 g_option_context_add_main_entries (context, goption_options, NULL);
346 program = gnome_program_init (PACKAGE, VERSION,
347 LIBGNOMEUI_MODULE, argc, argv,
348 GNOME_PARAM_GOPTION_CONTEXT, context,
349 GNOME_PARAM_HUMAN_READABLE_NAME, _("Evince"),
350 GNOME_PARAM_APP_DATADIR, GNOMEDATADIR,
353 g_option_context_add_group (context, gtk_get_option_group (TRUE));
354 if (!g_option_context_parse (context, &argc, &argv, &error)) {
355 g_warning ("Cannot parse arguments: %s", error->message);
356 g_error_free (error);
359 g_option_context_free (context);
361 accel_filename = g_build_filename (ev_dot_dir (), "accels", NULL);
362 gtk_accel_map_load (accel_filename);
365 args = arguments_parse ();
368 if (!ev_application_register_service (EV_APP)) {
369 if (load_files_remote (file_arguments, args)) {
370 g_hash_table_destroy (args);
372 g_object_unref (program);
377 enable_metadata = TRUE;
382 gnome_authentication_manager_init ();
385 ev_backends_manager_init ();
387 if (enable_metadata) {
388 ev_metadata_manager_init ();
391 ev_job_queue_init ();
392 g_set_application_name (_("Evince Document Viewer"));
394 ev_file_helpers_init ();
395 ev_stock_icons_init ();
396 gtk_window_set_default_icon_name ("evince");
398 load_files (file_arguments, args);
399 g_hash_table_destroy (args);
404 gnome_accelerators_sync ();
406 gtk_accel_map_save (accel_filename);
407 g_free (accel_filename);
410 ev_file_helpers_shutdown ();
412 if (enable_metadata) {
413 ev_metadata_manager_shutdown ();
416 ev_backends_manager_shutdown ();
419 g_object_unref (program);