~ubuntu-branches/ubuntu/oneiric/evince/oneiric-updates

« back to all changes in this revision

Viewing changes to shell/main.c

  • Committer: Bazaar Package Importer
  • Author(s): Robert Ancell
  • Date: 2009-11-05 17:21:47 UTC
  • mto: (1.6.3 upstream)
  • mto: This revision was merged to the branch mainline in revision 96.
  • Revision ID: james.westby@ubuntu.com-20091105172147-ao0nlfb9368zmyhj
Tags: upstream-2.29.1
ImportĀ upstreamĀ versionĀ 2.29.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
26
26
#include <glib/gi18n.h>
27
27
#include <gtk/gtk.h>
28
28
 
29
 
#ifdef ENABLE_DBUS
30
 
#include <gdk/gdkx.h>
31
 
#include <dbus/dbus-glib-bindings.h>
32
 
#endif
33
 
 
34
29
#include "ev-application.h"
35
 
#include "ev-backends-manager.h"
36
30
#include "ev-debug.h"
37
31
#include "ev-init.h"
38
32
#include "ev-file-helpers.h"
39
33
#include "ev-stock-icons.h"
 
34
#include "ev-metadata.h"
40
35
 
41
36
#ifdef WITH_SMCLIENT
42
37
#include "eggsmclient.h"
147
142
}
148
143
 
149
144
static void
150
 
value_free (GValue *value)
151
 
{
152
 
        g_value_unset (value);
153
 
        g_free (value);
154
 
}
155
 
 
156
 
/**
157
 
 * arguments_parse:
158
 
 *
159
 
 * Parses the arguments and creates a #GHashTable with this data.
160
 
 *
161
 
 *  key                 ->  value
162
 
 *
163
 
 *  dislay              ->  display at the default screen.
164
 
 *  screen              ->  screen number.
165
 
 *  page-label          ->  only if the page label argument has been passed,
166
 
 *                          the page of the document to display.
167
 
 *  mode                ->  only if the view mode is one of the availables,
168
 
 *                          the view mode.
169
 
 *
170
 
 * Returns: a pointer into #GHashTable with data from the arguments.
171
 
 */
172
 
static GHashTable *
173
 
arguments_parse (void)
174
 
{
175
 
        GHashTable      *args;
176
 
        GValue          *value;
177
 
        EvWindowRunMode  mode;
178
 
        GdkScreen       *screen;
179
 
        GdkDisplay      *display;
180
 
        const gchar     *display_name;
181
 
        gint             screen_number;
182
 
 
183
 
        args = g_hash_table_new_full (g_str_hash,
184
 
                                      g_str_equal,
185
 
                                      (GDestroyNotify)g_free,
186
 
                                      (GDestroyNotify)value_free);
187
 
        
188
 
        screen = gdk_screen_get_default ();
189
 
        display = gdk_screen_get_display (screen);
190
 
 
191
 
        display_name = gdk_display_get_name (display);
192
 
        screen_number = gdk_screen_get_number (screen);
193
 
 
194
 
        value = g_new0 (GValue, 1);
195
 
        g_value_init (value, G_TYPE_STRING);
196
 
        g_value_set_string (value, display_name);
197
 
        g_hash_table_insert (args, g_strdup ("display"), value);
198
 
 
199
 
        value = g_new0 (GValue, 1);
200
 
        g_value_init (value, G_TYPE_INT);
201
 
        g_value_set_int (value, screen_number);
202
 
        g_hash_table_insert (args, g_strdup ("screen"), value);
203
 
 
204
 
        if (ev_page_label) {
205
 
                value = g_new0 (GValue, 1);
206
 
                g_value_init (value, G_TYPE_STRING);
207
 
                g_value_set_string (value, ev_page_label);
208
 
 
209
 
                g_hash_table_insert (args, g_strdup ("page-label"), value);
210
 
 
211
 
                g_free (ev_page_label);
212
 
                ev_page_label = NULL;
213
 
        }
214
 
 
215
 
        if (ev_find_string) {
216
 
                value = g_new0 (GValue, 1);
217
 
                g_value_init (value, G_TYPE_STRING);
218
 
                g_value_set_string (value, ev_find_string);
219
 
 
220
 
                g_hash_table_insert (args, g_strdup ("find-string"), value);
221
 
 
222
 
                g_free (ev_find_string);
223
 
                ev_page_label = NULL;
224
 
        }
 
145
load_files (const char **files)
 
146
{
 
147
        GdkScreen       *screen = gdk_screen_get_default ();
 
148
        EvWindowRunMode  mode = EV_WINDOW_MODE_NORMAL;
 
149
        gint             i;
 
150
        EvLinkDest      *global_dest = NULL;
 
151
 
 
152
        if (!files) {
 
153
                if (!ev_application_has_window (EV_APP))
 
154
                        ev_application_open_window (EV_APP, screen, GDK_CURRENT_TIME);
 
155
                return;
 
156
        }
 
157
 
 
158
        if (ev_page_label)
 
159
                global_dest = ev_link_dest_new_page_label (ev_page_label);
225
160
 
226
161
        if (fullscreen_mode)
227
162
                mode = EV_WINDOW_MODE_FULLSCREEN;
228
163
        else if (presentation_mode)
229
164
                mode = EV_WINDOW_MODE_PRESENTATION;
230
 
        else
231
 
                return args;
232
 
 
233
 
        value = g_new0 (GValue, 1);
234
 
        g_value_init (value, G_TYPE_UINT);
235
 
        g_value_set_uint (value, mode);
236
 
 
237
 
        g_hash_table_insert (args, g_strdup ("mode"), value);
238
 
 
239
 
        return args;
240
 
}
241
 
 
242
 
static gint
243
 
find_window_list (EvWindow    *window,
244
 
                  const gchar *uri)
245
 
{
246
 
        return g_ascii_strcasecmp (uri, ev_window_get_uri (window));
247
 
}
248
 
 
249
 
static void
250
 
load_files (const char **files,
251
 
            GHashTable  *args)
252
 
{
253
 
        int    i;
254
 
        GList *windows;
255
 
 
256
 
        windows = ev_application_get_windows (EV_APP);
257
 
 
258
 
        if (!files) {
259
 
                if (!windows)
260
 
                        ev_application_open_window (EV_APP, args, GDK_CURRENT_TIME, NULL);
261
 
                else
262
 
                        g_list_free (windows);
263
 
                return;
264
 
        }
265
165
 
266
166
        for (i = 0; files[i]; i++) {
267
 
                char   *uri;
268
 
                char   *label;
269
 
                GValue *old = NULL;
270
 
                GFile  *file;
 
167
                gchar       *uri;
 
168
                gchar       *label;
 
169
                GFile       *file;
 
170
                EvLinkDest  *dest = NULL;
 
171
                const gchar *app_uri;
271
172
 
272
173
                file = g_file_new_for_commandline_arg (files[i]);
273
174
                uri = g_file_get_uri (file);
274
175
                g_object_unref (file);
275
176
 
276
 
                if (g_list_find_custom (windows, uri, (GCompareFunc) find_window_list)) {
 
177
                app_uri = ev_application_get_uri (EV_APP);
 
178
                if (app_uri && strcmp (app_uri, uri) == 0) {
277
179
                        g_free (uri);
278
180
                        continue;
279
181
                }
280
182
 
281
183
                label = strchr (uri, '#');
282
 
 
283
184
                if (label) {
284
 
                        GValue *new;
285
 
 
286
 
                        *label = 0; label++;
287
 
                        
288
 
                        old = g_hash_table_lookup (args, "page-label");
289
 
                        
290
 
                        new = g_new0 (GValue, 1);
291
 
                        g_value_init (new, G_TYPE_STRING);
292
 
                        g_value_set_string (new, label);
293
 
 
294
 
                        g_hash_table_insert (args, g_strdup ("page-label"), new);
295
 
 
296
 
                }
297
 
 
298
 
                ev_application_open_uri (EV_APP, uri, args,
299
 
                                         GDK_CURRENT_TIME, NULL);
300
 
 
301
 
                if (old)
302
 
                        g_hash_table_insert (args, g_strdup ("page-label"), old);
303
 
                
304
 
                g_free (uri);
305
 
        }
306
 
 
307
 
        g_list_free (windows);
308
 
}
309
 
 
310
 
#ifdef ENABLE_DBUS
311
 
static gboolean
312
 
load_files_remote (const char **files,
313
 
                   GHashTable  *args)
314
 
{
315
 
        int i;
316
 
        GError *error = NULL;
317
 
        DBusGConnection *connection;
318
 
        gboolean result = FALSE;
319
 
        DBusGProxy *remote_object;
320
 
        GdkDisplay *display;
321
 
        guint32 timestamp;
322
 
 
323
 
        display = gdk_display_get_default ();
324
 
        timestamp = gdk_x11_display_get_user_time (display);
325
 
        connection = dbus_g_bus_get (DBUS_BUS_STARTER, &error);
326
 
 
327
 
        if (connection == NULL) {
328
 
                g_warning ("%s", error->message);
329
 
                g_error_free (error);   
330
 
 
331
 
                return FALSE;
332
 
        }
333
 
 
334
 
        remote_object = dbus_g_proxy_new_for_name (connection,
335
 
                                                   "org.gnome.evince.ApplicationService",
336
 
                                                   "/org/gnome/evince/Evince",
337
 
                                                   "org.gnome.evince.Application");
338
 
        if (!files) {
339
 
                if (!dbus_g_proxy_call (remote_object, "OpenWindow", &error,
340
 
                                        dbus_g_type_get_map ("GHashTable", G_TYPE_STRING, G_TYPE_VALUE), args,
341
 
                                        G_TYPE_UINT, timestamp,
342
 
                                        G_TYPE_INVALID,
343
 
                                        G_TYPE_INVALID)) {
344
 
                        g_warning ("%s", error->message);
345
 
                        g_clear_error (&error);
346
 
                        g_object_unref (remote_object);
347
 
                        dbus_g_connection_unref (connection);
348
 
                        return FALSE;
349
 
                }
350
 
 
351
 
                g_object_unref (remote_object);
352
 
                dbus_g_connection_unref (connection);
353
 
                
354
 
                return TRUE;
355
 
        }
356
 
 
357
 
        for (i = 0; files[i]; i++) {
358
 
                const char *page_label;
359
 
                GFile *file;
360
 
                char *uri;
361
 
 
362
 
                file = g_file_new_for_commandline_arg (files[i]);
363
 
                uri = g_file_get_uri (file);
364
 
                g_object_unref (file);
365
 
 
366
 
                page_label = ev_page_label ? ev_page_label : "";
367
 
 
368
 
                if (!dbus_g_proxy_call (remote_object, "OpenURI", &error,
369
 
                                        G_TYPE_STRING, uri,
370
 
                                        dbus_g_type_get_map ("GHashTable", G_TYPE_STRING, G_TYPE_VALUE), args,
371
 
                                        G_TYPE_UINT, timestamp,
372
 
                                        G_TYPE_INVALID,
373
 
                                        G_TYPE_INVALID)) {
374
 
                        g_warning ("%s", error->message);
375
 
                        g_clear_error (&error);
376
 
                        g_free (uri);
377
 
                        continue;
378
 
                }
379
 
 
380
 
                g_free (uri);
381
 
                result = TRUE;
382
 
        }
383
 
 
384
 
        g_object_unref (remote_object);
385
 
        dbus_g_connection_unref (connection);
386
 
 
387
 
        gdk_notify_startup_complete ();
388
 
 
389
 
        return result;
390
 
}
391
 
#endif /* ENABLE_DBUS */
 
185
                        *label = 0;
 
186
                        label++;
 
187
                        dest = ev_link_dest_new_page_label (label);
 
188
                } else if (global_dest) {
 
189
                        dest = g_object_ref (global_dest);
 
190
                }
 
191
 
 
192
                ev_application_open_uri_at_dest (EV_APP, uri, screen, dest,
 
193
                                                 mode, ev_find_string,
 
194
                                                 GDK_CURRENT_TIME);
 
195
 
 
196
                if (dest)
 
197
                        g_object_unref (dest);
 
198
                g_free (uri);
 
199
        }
 
200
}
392
201
 
393
202
int
394
203
main (int argc, char *argv[])
395
204
{
396
205
        GOptionContext *context;
397
 
        GHashTable *args;
398
 
        GError *error = NULL;
 
206
        GError         *error = NULL;
399
207
 
400
208
#ifdef G_OS_WIN32
401
209
 
444
252
        g_option_context_add_group (context, gtk_get_option_group (TRUE));
445
253
 
446
254
        if (!g_option_context_parse (context, &argc, &argv, &error)) {
447
 
                g_printerr ("Cannot parse arguments: %s", error->message);
 
255
                g_printerr ("Cannot parse arguments: %s\n", error->message);
448
256
                g_error_free (error);
449
257
                g_option_context_free (context);
450
 
                
 
258
 
451
259
                return 1;
452
260
        }
453
261
        g_option_context_free (context);
460
268
                return retval ? 0 : 1;
461
269
        }
462
270
 
463
 
        args = arguments_parse ();
464
 
 
465
 
#ifdef ENABLE_DBUS
466
 
        if (!ev_application_register_service (EV_APP)) {
467
 
                if (load_files_remote (file_arguments, args)) {
468
 
                        g_hash_table_destroy (args);
469
 
 
470
 
                        return 0;
471
 
                }
472
 
        }
473
 
#endif /* ENABLE_DBUS */
474
 
        
475
271
        if (!ev_init ())
476
272
                return 1;
477
273
 
485
281
        gtk_window_set_default_icon_name ("evince");
486
282
#endif /* WITH_SMCLIENT && GDK_WINDOWING_X11 */
487
283
 
488
 
        ev_application_load_session (EV_APP, file_arguments);
489
 
        load_files (file_arguments, args);
490
 
        g_hash_table_destroy (args);
491
 
 
492
 
        /* Change directory so we don't prevent unmounting in case the initial cwd
493
 
         * is on an external device (see bug #575436)
494
 
         */
495
 
        g_chdir (g_get_home_dir ());    
496
 
 
497
 
        gtk_main ();
 
284
        ev_application_load_session (EV_APP);
 
285
        load_files (file_arguments);
 
286
        if (ev_application_has_window (EV_APP)) {
 
287
                /* Change directory so we don't prevent unmounting in case the initial cwd
 
288
                 * is on an external device (see bug #575436)
 
289
                 */
 
290
                g_chdir (g_get_home_dir ());
 
291
 
 
292
                gtk_main ();
 
293
        }
498
294
 
499
295
        ev_shutdown ();
500
296
        ev_stock_icons_shutdown ();