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