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