]> www.fi.muni.cz Git - evince.git/blob - shell/main.c
1f3da854cc555a96c66bfd484f40020edf01d1ce
[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 const 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                 char *label;
69
70                 uri = gnome_vfs_make_uri_from_shell_arg (files[i]);
71                 
72                 label = strchr (uri, GNOME_VFS_URI_MAGIC_CHR);
73                 
74                 if (label) {
75                         *label = 0; label++;
76                         ev_application_open_uri (EV_APP, uri, label,
77                                                  GDK_CURRENT_TIME, NULL);
78                 } else {        
79                         ev_application_open_uri (EV_APP, uri, ev_page_label,
80                                                  GDK_CURRENT_TIME, NULL);
81                 }
82                 g_free (uri);
83         }
84 }
85
86 #ifdef ENABLE_DBUS
87
88 static gboolean
89 load_files_remote (const char **files)
90 {
91         int i;
92         GError *error = NULL;
93         DBusGConnection *connection;
94         gboolean result = FALSE;
95 #if DBUS_VERSION < 35
96         DBusGPendingCall *call;
97 #endif
98         DBusGProxy *remote_object;
99         GdkDisplay *display;
100         guint32 timestamp;
101
102         display = gdk_display_get_default();
103         timestamp = gdk_x11_display_get_user_time (display);
104         connection = dbus_g_bus_get (DBUS_BUS_STARTER, &error);
105
106         if (connection == NULL) {
107                 g_warning (error->message);
108                 g_error_free (error);   
109
110                 return FALSE;
111         }
112
113         remote_object = dbus_g_proxy_new_for_name (connection,
114                                                    "org.gnome.evince.ApplicationService",
115                                                    "/org/gnome/evince/Evince",
116                                                    "org.gnome.evince.Application");
117         if (!files) {
118 #if DBUS_VERSION <= 33
119                 call = dbus_g_proxy_begin_call (remote_object, "OpenWindow",
120                                                 DBUS_TYPE_UINT32, &timestamp,
121                                                 DBUS_TYPE_INVALID);
122
123                 if (!dbus_g_proxy_end_call (remote_object, call, &error, DBUS_TYPE_INVALID)) {
124                         g_warning (error->message);
125                         g_clear_error (&error);
126                         g_object_unref (remote_object);
127                         dbus_g_connection_unref (connection);
128                         return FALSE;
129                 }
130 #elif DBUS_VERSION == 34
131                 call = dbus_g_proxy_begin_call (remote_object, "OpenWindow",
132                                                 G_TYPE_UINT, timestamp,
133                                                 G_TYPE_INVALID);
134
135                 if (!dbus_g_proxy_end_call (remote_object, call, &error, G_TYPE_INVALID)) {
136                         g_warning (error->message);
137                         g_clear_error (&error);
138                         g_object_unref (remote_object);
139                         dbus_g_connection_unref (connection);
140                         return FALSE;
141                 }
142 #else
143                 if (!dbus_g_proxy_call (remote_object, "OpenWindow", &error,
144                                         G_TYPE_UINT, timestamp,
145                                         G_TYPE_INVALID,
146                                         G_TYPE_INVALID)) {
147                         g_warning (error->message);
148                         g_clear_error (&error);
149                         g_object_unref (remote_object);
150                         dbus_g_connection_unref (connection);
151                         return FALSE;
152                 }
153 #endif
154                 g_object_unref (remote_object);
155                 dbus_g_connection_unref (connection);
156                 
157                 return TRUE;
158         }
159
160         for (i = 0; files[i]; i++) {
161                 const char *page_label;
162                 char *uri;
163
164                 uri = gnome_vfs_make_uri_from_shell_arg (files[i]);
165                 page_label = ev_page_label ? ev_page_label : "";
166 #if DBUS_VERSION <= 33
167                 call = dbus_g_proxy_begin_call (remote_object, "OpenURI",
168                                                 DBUS_TYPE_STRING, &uri,
169                                                 DBUS_TYPE_STRING, &page_label,
170                                                 DBUS_TYPE_UINT32, &timestamp,
171                                                 DBUS_TYPE_INVALID);
172
173                 if (!dbus_g_proxy_end_call (remote_object, call, &error, DBUS_TYPE_INVALID)) {
174                         g_warning (error->message);
175                         g_clear_error (&error);
176                         g_free (uri);
177                         continue;
178                 }
179 #elif DBUS_VERSION == 34
180                 call = dbus_g_proxy_begin_call (remote_object, "OpenURI",
181                                                 G_TYPE_STRING, uri,
182                                                 G_TYPE_STRING, page_label,
183                                                 G_TYPE_UINT, timestamp,
184                                                 G_TYPE_INVALID);
185
186                 if (!dbus_g_proxy_end_call (remote_object, call, &error, G_TYPE_INVALID)) {
187                         g_warning (error->message);
188                         g_clear_error (&error);
189                         g_free (uri);
190                         continue;
191                 }
192 #else
193                 if (!dbus_g_proxy_call (remote_object, "OpenURI", &error,
194                                         G_TYPE_STRING, uri,
195                                         G_TYPE_STRING, page_label,
196                                         G_TYPE_UINT, timestamp,
197                                         G_TYPE_INVALID,
198                                         G_TYPE_INVALID)) {
199                         g_warning (error->message);
200                         g_clear_error (&error);
201                         g_free (uri);
202                         continue;
203                 }
204 #endif
205                 g_free (uri);
206                 result = TRUE;
207         }
208
209         g_object_unref (remote_object);
210         dbus_g_connection_unref (connection);
211
212         gdk_notify_startup_complete ();
213
214         return result;
215 }
216 #endif /* ENABLE_DBUS */
217
218 int
219 main (int argc, char *argv[])
220 {
221         gboolean enable_metadata = FALSE;
222         GOptionContext *context;
223         GnomeProgram *program;
224
225         context = g_option_context_new (_("GNOME Document Viewer"));
226
227 #ifdef ENABLE_NLS
228         /* Initialize the i18n stuff */
229         bindtextdomain(GETTEXT_PACKAGE, GNOMELOCALEDIR);
230         bind_textdomain_codeset(GETTEXT_PACKAGE, "UTF-8");
231         textdomain(GETTEXT_PACKAGE);
232         g_option_context_add_main_entries (context, goption_options, GETTEXT_PACKAGE);
233 #else
234         g_option_context_add_main_entries (context, goption_options, NULL);
235 #endif
236
237         program = gnome_program_init (PACKAGE, VERSION,
238                                       LIBGNOMEUI_MODULE, argc, argv,
239                                       GNOME_PARAM_GOPTION_CONTEXT, context,
240                                       GNOME_PARAM_HUMAN_READABLE_NAME, _("Evince"),
241                                       GNOME_PARAM_APP_DATADIR, GNOMEDATADIR,
242                                       NULL);
243
244 #ifdef ENABLE_DBUS
245         if (!ev_application_register_service (EV_APP)) {
246                 if (load_files_remote (file_arguments)) {
247                         return 0;
248                 }
249         } else {
250                 enable_metadata = TRUE;
251         }
252 #endif
253         
254         gdk_threads_init ();
255         gnome_authentication_manager_init ();
256
257         if (enable_metadata) {
258                 ev_metadata_manager_init ();
259         }
260
261         ev_job_queue_init ();
262         g_set_application_name (_("Evince Document Viewer"));
263
264         ev_file_helpers_init ();
265         ev_debug_init ();
266         ev_stock_icons_init ();
267         gtk_window_set_default_icon_name ("evince");
268
269         load_files (file_arguments);
270
271         gtk_main ();
272
273         gnome_accelerators_sync ();
274         ev_file_helpers_shutdown ();
275
276         if (enable_metadata) {
277                 ev_metadata_manager_shutdown ();
278         }
279         g_object_unref(program);
280
281         return 0;
282 }