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