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"
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;
52 option_version_cb (const gchar *option_name,
57 g_print ("%s %s\n", _("GNOME Document Viewer"), VERSION);
63 static const GOptionEntry goption_options[] =
65 { "page-label", 'p', 0, G_OPTION_ARG_STRING, &ev_page_label, N_("The page of the document to display."), N_("PAGE")},
66 { "fullscreen", 'f', 0, G_OPTION_ARG_NONE, &fullscreen_mode, N_("Run evince in fullscreen mode"), NULL },
67 { "presentation", 's', 0, G_OPTION_ARG_NONE, &presentation_mode, N_("Run evince in presentation mode"), NULL },
68 { "preview", 'w', 0, G_OPTION_ARG_NONE, &preview_mode, N_("Run evince as a previewer"), NULL },
69 { "find", 'l', 0, G_OPTION_ARG_STRING, &ev_find_string, N_("The word or phrase to find in the document"), N_("STRING")},
70 { "unlink-tempfile", 'u', G_OPTION_FLAG_HIDDEN, G_OPTION_ARG_NONE, &unlink_temp_file, NULL, NULL },
71 { "print-settings", 't', G_OPTION_FLAG_HIDDEN, G_OPTION_ARG_FILENAME, &print_settings, NULL, NULL },
72 { "version", 0, G_OPTION_FLAG_NO_ARG | G_OPTION_FLAG_HIDDEN, G_OPTION_ARG_CALLBACK, option_version_cb, NULL, NULL },
73 { G_OPTION_REMAINING, 0, 0, G_OPTION_ARG_FILENAME_ARRAY, &file_arguments, NULL, N_("[FILE...]") },
78 value_free (GValue *value)
80 g_value_unset (value);
87 * Parses the arguments and creates a #GHashTable with this data.
91 * dislay -> display at the default screen.
92 * screen -> screen number.
93 * page-label -> only if the page label argument has been passed,
94 * the page of the document to display.
95 * mode -> only if the view mode is one of the availables,
97 * unlink-temp-file -> only if the view mode is preview mode and
98 * unlink-temp-file has been passed, unlink-temp-file.
100 * Returns: a pointer into #GHashTable with data from the arguments.
103 arguments_parse (void)
107 EvWindowRunMode mode;
110 const gchar *display_name;
113 args = g_hash_table_new_full (g_str_hash,
115 (GDestroyNotify)g_free,
116 (GDestroyNotify)value_free);
118 screen = gdk_screen_get_default ();
119 display = gdk_screen_get_display (screen);
121 display_name = gdk_display_get_name (display);
122 screen_number = gdk_screen_get_number (screen);
124 value = g_new0 (GValue, 1);
125 g_value_init (value, G_TYPE_STRING);
126 g_value_set_string (value, display_name);
127 g_hash_table_insert (args, g_strdup ("display"), value);
129 value = g_new0 (GValue, 1);
130 g_value_init (value, G_TYPE_INT);
131 g_value_set_int (value, screen_number);
132 g_hash_table_insert (args, g_strdup ("screen"), value);
135 value = g_new0 (GValue, 1);
136 g_value_init (value, G_TYPE_STRING);
137 g_value_set_string (value, ev_page_label);
139 g_hash_table_insert (args, g_strdup ("page-label"), value);
141 g_free (ev_page_label);
142 ev_page_label = NULL;
145 if (ev_find_string) {
146 value = g_new0 (GValue, 1);
147 g_value_init (value, G_TYPE_STRING);
148 g_value_set_string (value, ev_find_string);
150 g_hash_table_insert (args, g_strdup ("find-string"), value);
152 g_free (ev_find_string);
153 ev_page_label = NULL;
157 mode = EV_WINDOW_MODE_FULLSCREEN;
158 else if (presentation_mode)
159 mode = EV_WINDOW_MODE_PRESENTATION;
160 else if (preview_mode)
161 mode = EV_WINDOW_MODE_PREVIEW;
165 value = g_new0 (GValue, 1);
166 g_value_init (value, G_TYPE_UINT);
167 g_value_set_uint (value, mode);
169 g_hash_table_insert (args, g_strdup ("mode"), value);
171 if (mode == EV_WINDOW_MODE_PREVIEW && unlink_temp_file) {
172 value = g_new0 (GValue, 1);
173 g_value_init (value, G_TYPE_BOOLEAN);
174 g_value_set_boolean (value, unlink_temp_file);
176 g_hash_table_insert (args,
177 g_strdup ("unlink-temp-file"),
181 if (mode == EV_WINDOW_MODE_PREVIEW && print_settings) {
182 value = g_new0 (GValue, 1);
183 g_value_init (value, G_TYPE_STRING);
184 g_value_set_string (value, print_settings);
186 g_hash_table_insert (args,
187 g_strdup ("print-settings"),
189 g_free (print_settings);
190 print_settings = NULL;
197 load_files (const char **files,
203 ev_application_open_window (EV_APP, args, GDK_CURRENT_TIME, NULL);
207 for (i = 0; files[i]; i++) {
213 file = g_file_new_for_commandline_arg (files[i]);
214 uri = g_file_get_uri (file);
215 g_object_unref (file);
217 label = strchr (uri, '#');
224 old = g_hash_table_lookup (args, "page-label");
226 new = g_new0 (GValue, 1);
227 g_value_init (new, G_TYPE_STRING);
228 g_value_set_string (new, label);
230 g_hash_table_insert (args, g_strdup ("page-label"), new);
234 ev_application_open_uri (EV_APP, uri, args,
235 GDK_CURRENT_TIME, NULL);
238 g_hash_table_insert (args, g_strdup ("page-label"), old);
246 load_files_remote (const char **files,
250 GError *error = NULL;
251 DBusGConnection *connection;
252 gboolean result = FALSE;
253 DBusGProxy *remote_object;
257 display = gdk_display_get_default ();
258 timestamp = gdk_x11_display_get_user_time (display);
259 connection = dbus_g_bus_get (DBUS_BUS_STARTER, &error);
261 if (connection == NULL) {
262 g_warning ("%s", error->message);
263 g_error_free (error);
268 remote_object = dbus_g_proxy_new_for_name (connection,
269 "org.gnome.evince.ApplicationService",
270 "/org/gnome/evince/Evince",
271 "org.gnome.evince.Application");
273 if (!dbus_g_proxy_call (remote_object, "OpenWindow", &error,
274 dbus_g_type_get_map ("GHashTable", G_TYPE_STRING, G_TYPE_VALUE), args,
275 G_TYPE_UINT, timestamp,
278 g_warning ("%s", error->message);
279 g_clear_error (&error);
280 g_object_unref (remote_object);
281 dbus_g_connection_unref (connection);
285 g_object_unref (remote_object);
286 dbus_g_connection_unref (connection);
291 for (i = 0; files[i]; i++) {
292 const char *page_label;
296 file = g_file_new_for_commandline_arg (files[i]);
297 uri = g_file_get_uri (file);
298 g_object_unref (file);
300 page_label = ev_page_label ? ev_page_label : "";
302 if (!dbus_g_proxy_call (remote_object, "OpenURI", &error,
304 dbus_g_type_get_map ("GHashTable", G_TYPE_STRING, G_TYPE_VALUE), args,
305 G_TYPE_UINT, timestamp,
308 g_warning ("%s", error->message);
309 g_clear_error (&error);
318 g_object_unref (remote_object);
319 dbus_g_connection_unref (connection);
321 gdk_notify_startup_complete ();
325 #endif /* ENABLE_DBUS */
328 main (int argc, char *argv[])
330 GOptionContext *context;
332 GError *error = NULL;
334 /* Init glib threads asap */
335 if (!g_thread_supported ())
336 g_thread_init (NULL);
339 /* Initialize the i18n stuff */
340 bindtextdomain (GETTEXT_PACKAGE, GNOMELOCALEDIR);
341 bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
342 textdomain (GETTEXT_PACKAGE);
345 context = g_option_context_new (N_("GNOME Document Viewer"));
346 g_option_context_set_translation_domain(context, GETTEXT_PACKAGE);
347 g_option_context_add_main_entries (context, goption_options, GETTEXT_PACKAGE);
349 g_option_context_add_group (context, egg_sm_client_get_option_group ());
350 g_option_context_add_group (context, gtk_get_option_group (TRUE));
352 if (!g_option_context_parse (context, &argc, &argv, &error)) {
353 g_printerr ("Cannot parse arguments: %s", error->message);
354 g_error_free (error);
355 g_option_context_free (context);
359 g_option_context_free (context);
361 args = arguments_parse ();
364 if (!ev_application_register_service (EV_APP)) {
365 if (load_files_remote (file_arguments, args)) {
366 g_hash_table_destroy (args);
371 #endif /* ENABLE_DBUS */
376 ev_stock_icons_init ();
378 egg_set_desktop_file (GNOMEDATADIR "/applications/evince.desktop");
380 if (!ev_application_load_session (EV_APP))
381 load_files (file_arguments, args);
382 g_hash_table_destroy (args);