]> www.fi.muni.cz Git - evince.git/blob - shell/main.c
Marco Pesenti Gritti <mpg@redhat.com>
[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 <gtk/gtkmain.h>
28 #include <libgnome/gnome-program.h>
29 #include <libgnomeui/gnome-ui-init.h>
30 #include <libgnomeui/gnome-app-helper.h>
31 #include <libgnomeui/gnome-authentication-manager.h>
32 #include <libgnomevfs/gnome-vfs-utils.h>
33
34 #ifdef ENABLE_DBUS
35 #include <dbus/dbus-glib-bindings.h>
36 #endif
37
38 #include "ev-stock-icons.h"
39 #include "ev-debug.h"
40 #include "ev-job-queue.h"
41 #include "ev-file-helpers.h"
42
43 static char *ev_page_label;
44
45 static struct poptOption popt_options[] =
46 {
47         { "page-label", 'p', POPT_ARG_STRING, &ev_page_label, 0, N_("The page of the document to display."), N_("PAGE")},
48         { NULL, 0, 0, NULL, 0, NULL, NULL }
49 };
50
51 static void
52 load_files (const char **files)
53 {
54         int i;
55
56         if (!files) {
57                 ev_application_open_window (EV_APP, NULL);
58                 return;
59         }
60
61         for (i = 0; files[i]; i++) {
62                 char *uri;
63
64                 uri = gnome_vfs_make_uri_from_shell_arg (files[i]);
65                 ev_application_open_uri (EV_APP, uri, ev_page_label, NULL);             
66                 g_free (uri);
67         }
68 }
69
70 #ifdef ENABLE_DBUS
71 static void
72 load_files_remote (const char **files)
73 {
74         int i;
75         GError *error = NULL;
76         DBusGConnection *connection;
77         DBusGPendingCall *call;
78         DBusGProxy *remote_object;
79
80         connection = dbus_g_bus_get (DBUS_BUS_SESSION, &error);
81         if (connection == NULL) {
82                 g_warning (error->message);
83                 g_error_free (error);
84                 
85                 return;
86         }
87
88         remote_object = dbus_g_proxy_new_for_name (connection,
89                                                    "org.gnome.evince.ApplicationService",
90                                                    "/org/gnome/evince/Evince",
91                                                    "org.gnome.evince.Application");
92         if (!files) {
93                 call = dbus_g_proxy_begin_call (remote_object, "OpenWindow", G_TYPE_INVALID);
94
95                 if (!dbus_g_proxy_end_call (remote_object, call, &error, G_TYPE_INVALID)) {
96                         g_warning (error->message);
97                         g_clear_error (&error);
98                 }
99                 return;
100         }
101
102         for (i = 0; files[i]; i++) {
103                 const char *page_label;
104                 char *uri;
105
106                 uri = gnome_vfs_make_uri_from_shell_arg (files[i]);
107                 page_label = ev_page_label ? ev_page_label : ""; 
108
109                 call = dbus_g_proxy_begin_call (remote_object, "OpenURI",
110                                                 G_TYPE_STRING, uri,
111                                                 G_TYPE_STRING, page_label,
112                                                 G_TYPE_INVALID);
113
114                 if (!dbus_g_proxy_end_call (remote_object, call, &error, G_TYPE_INVALID)) {
115                         g_warning (error->message);
116                         g_clear_error (&error);
117                 }
118                 
119                 g_free (uri);
120         }
121 }
122 #endif /* ENABLE_DBUS */
123
124 int
125 main (int argc, char *argv[])
126 {
127         poptContext context;
128         GValue context_as_value = { 0 };
129         GnomeProgram *program;
130
131 #ifdef ENABLE_NLS
132         /* Initialize the i18n stuff */
133         bindtextdomain(GETTEXT_PACKAGE, GNOMELOCALEDIR);
134         bind_textdomain_codeset(GETTEXT_PACKAGE, "UTF-8");
135         textdomain(GETTEXT_PACKAGE);
136 #endif
137
138         program = gnome_program_init (PACKAGE, VERSION,
139                                       LIBGNOMEUI_MODULE, argc, argv,
140                                       GNOME_PARAM_POPT_TABLE, popt_options,
141                                       GNOME_PARAM_HUMAN_READABLE_NAME, _("Evince"),
142                                       GNOME_PARAM_APP_DATADIR, GNOMEDATADIR,
143                                       NULL);
144         g_object_get_property (G_OBJECT (program),
145                                GNOME_PARAM_POPT_CONTEXT,
146                                g_value_init (&context_as_value, G_TYPE_POINTER));
147         context = g_value_get_pointer (&context_as_value);
148
149
150 #ifdef ENABLE_DBUS
151         if (!ev_application_register_service (EV_APP)) {
152                 load_files_remote (poptGetArgs (context));
153                 g_warning ("Another process was running.");
154                 return 0;
155         } else {
156                 g_warning ("Starting evince process.");
157         }
158 #endif
159
160         gnome_authentication_manager_init ();
161
162         ev_job_queue_init ();
163         g_set_application_name (_("Evince Document Viewer"));
164
165         ev_file_helpers_init ();
166         ev_debug_init ();
167         ev_stock_icons_init ();
168         gtk_window_set_default_icon_name ("postscript-viewer");
169
170         load_files (poptGetArgs (context));
171
172         gtk_main ();
173
174         gnome_accelerators_sync ();
175         poptFreeContext (context);
176         ev_file_helpers_shutdown ();
177 #if ENABLE_METADATA
178         ev_metadata_manager_shutdown ();
179 #endif
180
181         return 0;
182 }