]> www.fi.muni.cz Git - evince.git/blob - shell/main.c
Make session manager code compile for w32. These changes have been taken
[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  */
19
20 #include "config.h"
21
22 #include <stdlib.h>
23 #include <string.h>
24
25 #include <glib/gi18n.h>
26 #include <gtk/gtk.h>
27
28 #ifdef ENABLE_DBUS
29 #include <gdk/gdkx.h>
30 #include <dbus/dbus-glib-bindings.h>
31 #endif
32
33 #include "ev-application.h"
34 #include "ev-backends-manager.h"
35 #include "ev-debug.h"
36 #include "ev-init.h"
37 #include "ev-file-helpers.h"
38 #include "ev-stock-icons.h"
39 #include "eggsmclient.h"
40 #ifndef G_OS_WIN32
41 #include "eggdesktopfile.h"
42 #endif
43
44 static gchar   *ev_page_label;
45 static gchar   *ev_find_string;
46 static gboolean preview_mode = FALSE;
47 static gboolean fullscreen_mode = FALSE;
48 static gboolean presentation_mode = FALSE;
49 static gboolean unlink_temp_file = FALSE;
50 static gchar   *print_settings;
51 static const char **file_arguments = NULL;
52
53 static gboolean
54 option_version_cb (const gchar *option_name,
55                    const gchar *value,
56                    gpointer     data,
57                    GError     **error)
58 {
59   g_print ("%s %s\n", _("GNOME Document Viewer"), VERSION);
60
61   exit (0);
62   return FALSE;
63 }
64
65 static const GOptionEntry goption_options[] =
66 {
67         { "page-label", 'p', 0, G_OPTION_ARG_STRING, &ev_page_label, N_("The page of the document to display."), N_("PAGE")},
68         { "fullscreen", 'f', 0, G_OPTION_ARG_NONE, &fullscreen_mode, N_("Run evince in fullscreen mode"), NULL },
69         { "presentation", 's', 0, G_OPTION_ARG_NONE, &presentation_mode, N_("Run evince in presentation mode"), NULL },
70         { "preview", 'w', 0, G_OPTION_ARG_NONE, &preview_mode, N_("Run evince as a previewer"), NULL },
71         { "find", 'l', 0, G_OPTION_ARG_STRING, &ev_find_string, N_("The word or phrase to find in the document"), N_("STRING")},
72         { "unlink-tempfile", 'u', G_OPTION_FLAG_HIDDEN, G_OPTION_ARG_NONE, &unlink_temp_file, NULL, NULL },
73         { "print-settings", 't', G_OPTION_FLAG_HIDDEN, G_OPTION_ARG_FILENAME, &print_settings, NULL, NULL },
74         { "version", 0, G_OPTION_FLAG_NO_ARG | G_OPTION_FLAG_HIDDEN, G_OPTION_ARG_CALLBACK, option_version_cb, NULL, NULL },
75         { G_OPTION_REMAINING, 0, 0, G_OPTION_ARG_FILENAME_ARRAY, &file_arguments, NULL, N_("[FILE...]") },
76         { NULL }
77 };
78
79 static void
80 value_free (GValue *value)
81 {
82         g_value_unset (value);
83         g_free (value);
84 }
85
86 /**
87  * arguments_parse:
88  *
89  * Parses the arguments and creates a #GHashTable with this data.
90  *
91  *  key                 ->  value
92  *
93  *  dislay              ->  display at the default screen.
94  *  screen              ->  screen number.
95  *  page-label          ->  only if the page label argument has been passed,
96  *                          the page of the document to display.
97  *  mode                ->  only if the view mode is one of the availables,
98  *                          the view mode.
99  *  unlink-temp-file    ->  only if the view mode is preview mode and
100  *                          unlink-temp-file has been passed, unlink-temp-file.
101  *
102  * Returns: a pointer into #GHashTable with data from the arguments.
103  */
104 static GHashTable *
105 arguments_parse (void)
106 {
107         GHashTable      *args;
108         GValue          *value;
109         EvWindowRunMode  mode;
110         GdkScreen       *screen;
111         GdkDisplay      *display;
112         const gchar     *display_name;
113         gint             screen_number;
114
115         args = g_hash_table_new_full (g_str_hash,
116                                       g_str_equal,
117                                       (GDestroyNotify)g_free,
118                                       (GDestroyNotify)value_free);
119         
120         screen = gdk_screen_get_default ();
121         display = gdk_screen_get_display (screen);
122
123         display_name = gdk_display_get_name (display);
124         screen_number = gdk_screen_get_number (screen);
125
126         value = g_new0 (GValue, 1);
127         g_value_init (value, G_TYPE_STRING);
128         g_value_set_string (value, display_name);
129         g_hash_table_insert (args, g_strdup ("display"), value);
130
131         value = g_new0 (GValue, 1);
132         g_value_init (value, G_TYPE_INT);
133         g_value_set_int (value, screen_number);
134         g_hash_table_insert (args, g_strdup ("screen"), value);
135
136         if (ev_page_label) {
137                 value = g_new0 (GValue, 1);
138                 g_value_init (value, G_TYPE_STRING);
139                 g_value_set_string (value, ev_page_label);
140
141                 g_hash_table_insert (args, g_strdup ("page-label"), value);
142
143                 g_free (ev_page_label);
144                 ev_page_label = NULL;
145         }
146
147         if (ev_find_string) {
148                 value = g_new0 (GValue, 1);
149                 g_value_init (value, G_TYPE_STRING);
150                 g_value_set_string (value, ev_find_string);
151
152                 g_hash_table_insert (args, g_strdup ("find-string"), value);
153
154                 g_free (ev_find_string);
155                 ev_page_label = NULL;
156         }
157
158         if (fullscreen_mode)
159                 mode = EV_WINDOW_MODE_FULLSCREEN;
160         else if (presentation_mode)
161                 mode = EV_WINDOW_MODE_PRESENTATION;
162         else if (preview_mode)
163                 mode = EV_WINDOW_MODE_PREVIEW;
164         else
165                 return args;
166
167         value = g_new0 (GValue, 1);
168         g_value_init (value, G_TYPE_UINT);
169         g_value_set_uint (value, mode);
170
171         g_hash_table_insert (args, g_strdup ("mode"), value);
172
173         if (mode == EV_WINDOW_MODE_PREVIEW && unlink_temp_file) {
174                 value = g_new0 (GValue, 1);
175                 g_value_init (value, G_TYPE_BOOLEAN);
176                 g_value_set_boolean (value, unlink_temp_file);
177
178                 g_hash_table_insert (args,
179                                      g_strdup ("unlink-temp-file"),
180                                      value);
181         }
182
183         if (mode == EV_WINDOW_MODE_PREVIEW && print_settings) {
184                 value = g_new0 (GValue, 1);
185                 g_value_init (value, G_TYPE_STRING);
186                 g_value_set_string (value, print_settings);
187
188                 g_hash_table_insert (args,
189                                      g_strdup ("print-settings"),
190                                      value);
191                 g_free (print_settings);
192                 print_settings = NULL;
193         }
194
195         return args;
196 }
197
198 static void
199 load_files (const char **files,
200             GHashTable  *args)
201 {
202         int i;
203
204         if (!files) {
205                 ev_application_open_window (EV_APP, args, GDK_CURRENT_TIME, NULL);
206                 return;
207         }
208
209         for (i = 0; files[i]; i++) {
210                 char   *uri;
211                 char   *label;
212                 GValue *old = NULL;
213                 GFile  *file;
214
215                 file = g_file_new_for_commandline_arg (files[i]);
216                 uri = g_file_get_uri (file);
217                 g_object_unref (file);
218                 
219                 label = strchr (uri, '#');
220
221                 if (label) {
222                         GValue *new;
223
224                         *label = 0; label++;
225                         
226                         old = g_hash_table_lookup (args, "page-label");
227                         
228                         new = g_new0 (GValue, 1);
229                         g_value_init (new, G_TYPE_STRING);
230                         g_value_set_string (new, label);
231
232                         g_hash_table_insert (args, g_strdup ("page-label"), new);
233
234                 }
235
236                 ev_application_open_uri (EV_APP, uri, args,
237                                          GDK_CURRENT_TIME, NULL);
238
239                 if (old)
240                         g_hash_table_insert (args, g_strdup ("page-label"), old);
241                 
242                 g_free (uri);
243         }
244 }
245
246 #ifdef ENABLE_DBUS
247 static gboolean
248 load_files_remote (const char **files,
249                    GHashTable  *args)
250 {
251         int i;
252         GError *error = NULL;
253         DBusGConnection *connection;
254         gboolean result = FALSE;
255         DBusGProxy *remote_object;
256         GdkDisplay *display;
257         guint32 timestamp;
258
259         display = gdk_display_get_default ();
260         timestamp = gdk_x11_display_get_user_time (display);
261         connection = dbus_g_bus_get (DBUS_BUS_STARTER, &error);
262
263         if (connection == NULL) {
264                 g_warning ("%s", error->message);
265                 g_error_free (error);   
266
267                 return FALSE;
268         }
269
270         remote_object = dbus_g_proxy_new_for_name (connection,
271                                                    "org.gnome.evince.ApplicationService",
272                                                    "/org/gnome/evince/Evince",
273                                                    "org.gnome.evince.Application");
274         if (!files) {
275                 if (!dbus_g_proxy_call (remote_object, "OpenWindow", &error,
276                                         dbus_g_type_get_map ("GHashTable", G_TYPE_STRING, G_TYPE_VALUE), args,
277                                         G_TYPE_UINT, timestamp,
278                                         G_TYPE_INVALID,
279                                         G_TYPE_INVALID)) {
280                         g_warning ("%s", error->message);
281                         g_clear_error (&error);
282                         g_object_unref (remote_object);
283                         dbus_g_connection_unref (connection);
284                         return FALSE;
285                 }
286
287                 g_object_unref (remote_object);
288                 dbus_g_connection_unref (connection);
289                 
290                 return TRUE;
291         }
292
293         for (i = 0; files[i]; i++) {
294                 const char *page_label;
295                 GFile *file;
296                 char *uri;
297
298                 file = g_file_new_for_commandline_arg (files[i]);
299                 uri = g_file_get_uri (file);
300                 g_object_unref (file);
301
302                 page_label = ev_page_label ? ev_page_label : "";
303
304                 if (!dbus_g_proxy_call (remote_object, "OpenURI", &error,
305                                         G_TYPE_STRING, uri,
306                                         dbus_g_type_get_map ("GHashTable", G_TYPE_STRING, G_TYPE_VALUE), args,
307                                         G_TYPE_UINT, timestamp,
308                                         G_TYPE_INVALID,
309                                         G_TYPE_INVALID)) {
310                         g_warning ("%s", error->message);
311                         g_clear_error (&error);
312                         g_free (uri);
313                         continue;
314                 }
315
316                 g_free (uri);
317                 result = TRUE;
318         }
319
320         g_object_unref (remote_object);
321         dbus_g_connection_unref (connection);
322
323         gdk_notify_startup_complete ();
324
325         return result;
326 }
327 #endif /* ENABLE_DBUS */
328
329 int
330 main (int argc, char *argv[])
331 {
332         GOptionContext *context;
333         GHashTable *args;
334         GError *error = NULL;
335
336         /* Init glib threads asap */
337         if (!g_thread_supported ())
338                 g_thread_init (NULL);
339
340 #ifdef ENABLE_NLS
341         /* Initialize the i18n stuff */
342         bindtextdomain (GETTEXT_PACKAGE, GNOMELOCALEDIR);
343         bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
344         textdomain (GETTEXT_PACKAGE);
345 #endif
346
347         context = g_option_context_new (N_("GNOME Document Viewer"));
348         g_option_context_set_translation_domain(context, GETTEXT_PACKAGE);
349         g_option_context_add_main_entries (context, goption_options, GETTEXT_PACKAGE);
350         
351         g_option_context_add_group (context, egg_sm_client_get_option_group ());
352         g_option_context_add_group (context, gtk_get_option_group (TRUE));
353
354         if (!g_option_context_parse (context, &argc, &argv, &error)) {
355                 g_printerr ("Cannot parse arguments: %s", error->message);
356                 g_error_free (error);
357                 g_option_context_free (context);
358                 
359                 return 1;
360         }
361         g_option_context_free (context);
362
363         args = arguments_parse ();
364
365 #ifdef ENABLE_DBUS
366         if (!ev_application_register_service (EV_APP)) {
367                 if (load_files_remote (file_arguments, args)) {
368                         g_hash_table_destroy (args);
369
370                         return 0;
371                 }
372         }
373 #endif /* ENABLE_DBUS */
374
375         if (!ev_init ())
376                 return 1;
377
378         ev_stock_icons_init ();
379
380 #ifdef G_OS_WIN32
381         /* Manually set name and icon in win32 */
382         g_set_application_name (_("Evince"));
383         gtk_window_set_default_icon_name ("evince");
384 #else
385         egg_set_desktop_file (GNOMEDATADIR "/applications/evince.desktop");
386 #endif /* G_OS_WIN32 */
387
388         if (!ev_application_load_session (EV_APP))
389                 load_files (file_arguments, args);
390         g_hash_table_destroy (args);
391
392         gtk_main ();
393
394         ev_shutdown ();
395
396         return 0;
397 }