]> www.fi.muni.cz Git - evince.git/blob - shell/main.c
Port to GOption command line parsing. Fix for the bug #327518
[evince.git] / shell / main.c
1 /*
2  *  Copyright (C) 2004 Marco Pesenti Gritti
3  *
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)
7  *  any later version.
8  *
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.
13  *
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.
17  *
18  *  $Id$
19  */
20
21 #include "config.h"
22
23 #include "ev-application.h"
24 #include "ev-metadata-manager.h"
25
26 #include <glib/gi18n.h>
27 #include <gdk/gdkx.h>
28 #include <gtk/gtkmain.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <libgnome/gnome-program.h>
32 #include <libgnomeui/gnome-ui-init.h>
33 #include <libgnomeui/gnome-app-helper.h>
34 #include <libgnomeui/gnome-authentication-manager.h>
35 #include <libgnomevfs/gnome-vfs-utils.h>
36
37 #ifdef ENABLE_DBUS
38 #include <dbus/dbus-glib-bindings.h>
39 #endif
40
41 #include "ev-stock-icons.h"
42 #include "ev-debug.h"
43 #include "ev-job-queue.h"
44 #include "ev-file-helpers.h"
45
46 static char *ev_page_label;
47 static char **file_arguments = NULL;
48
49 static const GOptionEntry goption_options[] =
50 {
51         { "page-label", 'p', 0, G_OPTION_ARG_STRING, &ev_page_label, N_("The page of the document to display."), N_("PAGE")},
52         { G_OPTION_REMAINING, 0, 0, G_OPTION_ARG_FILENAME_ARRAY, &file_arguments, NULL, N_("[FILE...]") },
53         { NULL }
54 };
55
56 static void
57 load_files (const char **files)
58 {
59         int i;
60
61         if (!files) {
62                 ev_application_open_window (EV_APP, GDK_CURRENT_TIME, NULL);
63                 return;
64         }
65
66         for (i = 0; files[i]; i++) {
67                 char *uri;
68
69                 uri = gnome_vfs_make_uri_from_shell_arg (files[i]);
70                 ev_application_open_uri (EV_APP, uri, ev_page_label,
71                                          GDK_CURRENT_TIME, NULL);
72                 g_free (uri);
73         }
74 }
75
76 #ifdef ENABLE_DBUS
77
78 static gboolean
79 load_files_remote (const char **files)
80 {
81         int i;
82         GError *error = NULL;
83         DBusGConnection *connection;
84         gboolean result = FALSE;
85 #if DBUS_VERSION < 35
86         DBusGPendingCall *call;
87 #endif
88         DBusGProxy *remote_object;
89         GdkDisplay *display;
90         guint32 timestamp;
91
92         display = gdk_display_get_default();
93         timestamp = gdk_x11_display_get_user_time (display);
94         connection = dbus_g_bus_get (DBUS_BUS_STARTER, &error);
95
96         if (connection == NULL) {
97                 g_warning (error->message);
98                 g_error_free (error);   
99
100                 return FALSE;
101         }
102
103         remote_object = dbus_g_proxy_new_for_name (connection,
104                                                    "org.gnome.evince.ApplicationService",
105                                                    "/org/gnome/evince/Evince",
106                                                    "org.gnome.evince.Application");
107         if (!files) {
108 #if DBUS_VERSION <= 33
109                 call = dbus_g_proxy_begin_call (remote_object, "OpenWindow",
110                                                 DBUS_TYPE_UINT32, &timestamp,
111                                                 DBUS_TYPE_INVALID);
112
113                 if (!dbus_g_proxy_end_call (remote_object, call, &error, DBUS_TYPE_INVALID)) {
114                         g_warning (error->message);
115                         g_clear_error (&error);
116                         g_object_unref (remote_object);
117                         dbus_g_connection_unref (connection);
118                         return FALSE;
119                 }
120 #elif DBUS_VERSION == 34
121                 call = dbus_g_proxy_begin_call (remote_object, "OpenWindow",
122                                                 G_TYPE_UINT, timestamp,
123                                                 G_TYPE_INVALID);
124
125                 if (!dbus_g_proxy_end_call (remote_object, call, &error, G_TYPE_INVALID)) {
126                         g_warning (error->message);
127                         g_clear_error (&error);
128                         g_object_unref (remote_object);
129                         dbus_g_connection_unref (connection);
130                         return FALSE;
131                 }
132 #else
133                 if (!dbus_g_proxy_call (remote_object, "OpenWindow", &error,
134                                         G_TYPE_UINT, timestamp,
135                                         G_TYPE_INVALID,
136                                         G_TYPE_INVALID)) {
137                         g_warning (error->message);
138                         g_clear_error (&error);
139                         g_object_unref (remote_object);
140                         dbus_g_connection_unref (connection);
141                         return FALSE;
142                 }
143 #endif
144                 g_object_unref (remote_object);
145                 dbus_g_connection_unref (connection);
146                 
147                 return TRUE;
148         }
149
150         for (i = 0; files[i]; i++) {
151                 const char *page_label;
152                 char *uri;
153
154                 uri = gnome_vfs_make_uri_from_shell_arg (files[i]);
155                 page_label = ev_page_label ? ev_page_label : "";
156 #if DBUS_VERSION <= 33
157                 call = dbus_g_proxy_begin_call (remote_object, "OpenURI",
158                                                 DBUS_TYPE_STRING, &uri,
159                                                 DBUS_TYPE_STRING, &page_label,
160                                                 DBUS_TYPE_UINT32, &timestamp,
161                                                 DBUS_TYPE_INVALID);
162
163                 if (!dbus_g_proxy_end_call (remote_object, call, &error, DBUS_TYPE_INVALID)) {
164                         g_warning (error->message);
165                         g_clear_error (&error);
166                         g_free (uri);
167                         continue;
168                 }
169 #elif DBUS_VERSION == 34
170                 call = dbus_g_proxy_begin_call (remote_object, "OpenURI",
171                                                 G_TYPE_STRING, uri,
172                                                 G_TYPE_STRING, page_label,
173                                                 G_TYPE_UINT, timestamp,
174                                                 G_TYPE_INVALID);
175
176                 if (!dbus_g_proxy_end_call (remote_object, call, &error, G_TYPE_INVALID)) {
177                         g_warning (error->message);
178                         g_clear_error (&error);
179                         g_free (uri);
180                         continue;
181                 }
182 #else
183                 if (!dbus_g_proxy_call (remote_object, "OpenURI", &error,
184                                         G_TYPE_STRING, uri,
185                                         G_TYPE_STRING, page_label,
186                                         G_TYPE_UINT, timestamp,
187                                         G_TYPE_INVALID,
188                                         G_TYPE_INVALID)) {
189                         g_warning (error->message);
190                         g_clear_error (&error);
191                         g_free (uri);
192                         continue;
193                 }
194 #endif
195                 g_free (uri);
196                 result = TRUE;
197         }
198
199         g_object_unref (remote_object);
200         dbus_g_connection_unref (connection);
201
202         gdk_notify_startup_complete ();
203
204         return result;
205 }
206 #endif /* ENABLE_DBUS */
207
208 int
209 main (int argc, char *argv[])
210 {
211         gboolean enable_metadata = FALSE;
212         GOptionContext *context;
213         GnomeProgram *program;
214
215         context = g_option_context_new (_("GNOME Document Viewer"));
216
217 #ifdef ENABLE_NLS
218         /* Initialize the i18n stuff */
219         bindtextdomain(GETTEXT_PACKAGE, GNOMELOCALEDIR);
220         bind_textdomain_codeset(GETTEXT_PACKAGE, "UTF-8");
221         textdomain(GETTEXT_PACKAGE);
222         g_option_context_add_main_entries (context, goption_options, GETTEXT_PACKAGE);
223 #else
224         g_option_context_add_main_entries (context, goption_options, NULL);
225 #endif
226
227         program = gnome_program_init (PACKAGE, VERSION,
228                                       LIBGNOMEUI_MODULE, argc, argv,
229                                       GNOME_PARAM_GOPTION_CONTEXT, context,
230                                       GNOME_PARAM_HUMAN_READABLE_NAME, _("Evince"),
231                                       GNOME_PARAM_APP_DATADIR, GNOMEDATADIR,
232                                       NULL);
233
234 #ifdef ENABLE_DBUS
235         if (!ev_application_register_service (EV_APP)) {
236                 if (load_files_remote (file_arguments)) {
237                         return 0;
238                 }
239         } else {
240                 enable_metadata = TRUE;
241         }
242 #endif
243
244         gnome_authentication_manager_init ();
245
246
247         if (enable_metadata) {
248                 ev_metadata_manager_init ();
249         }
250
251         ev_job_queue_init ();
252         g_set_application_name (_("Evince Document Viewer"));
253
254         ev_file_helpers_init ();
255         ev_debug_init ();
256         ev_stock_icons_init ();
257         gtk_window_set_default_icon_name ("evince");
258
259         load_files (file_arguments);
260
261         gtk_main ();
262
263         gnome_accelerators_sync ();
264         ev_file_helpers_shutdown ();
265
266         if (enable_metadata) {
267                 ev_metadata_manager_shutdown ();
268         }
269         g_object_unref(program);
270
271         return 0;
272 }