summaryrefslogtreecommitdiff
path: root/src/evbp-viewer.c
blob: 1f19f6af08a0a9136881fb7405b52441bf83b793 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
/*
 * Copyright (C) 2012  Dan Nicholson <dbn.lists@gmail.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or (at
 * your option) any later version.

 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.

 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 * 02110-1301 USA.
 */

#include <config.h>
#include "evbp-viewer.h"
#include "evbp-mime.h"
#include "ev-page-action.h"
#include <string.h>
#include <gtk/gtk.h>

G_DEFINE_TYPE(EvbpViewer, evbp_viewer, GTK_TYPE_BOX)

static void
evbp_viewer_zoom_best_fit(GtkToggleAction *action, EvbpViewer *viewer)
{
    ev_document_model_set_sizing_mode(viewer->model,
                                      gtk_toggle_action_get_active(action) ?
                                      EV_SIZING_BEST_FIT : EV_SIZING_FREE);
}

static void
evbp_viewer_zoom_page_width(GtkToggleAction *action, EvbpViewer *viewer)
{
    ev_document_model_set_sizing_mode(viewer->model,
                                      gtk_toggle_action_get_active(action) ?
                                      EV_SIZING_FIT_WIDTH : EV_SIZING_FREE);
}

static void
evbp_viewer_toggle_continuous(GtkToggleAction *action, EvbpViewer *viewer)
{
    ev_document_model_set_continuous(viewer->model,
                                     gtk_toggle_action_get_active(action) ?
                                     TRUE : FALSE);
}

static void
evbp_chooser_response_cb(GtkDialog *dialog, gint response, EvbpViewer *viewer)
{
    if (response == GTK_RESPONSE_ACCEPT) {
        gchar *uri;
        GError *error;

        uri = gtk_file_chooser_get_uri(GTK_FILE_CHOOSER(dialog));
        if (!ev_document_save(viewer->document, uri, &error)) {
            g_warning("Failed to save file %s: %s\n", uri,
                      error->message);
            g_error_free(error);
            error = NULL;
        }
        g_free(uri);
    }

    gtk_widget_destroy(GTK_WIDGET(dialog));
}

static void
evbp_viewer_save_copy(GtkAction *action, EvbpViewer *viewer)
{
    const gchar *title;
    const gchar *download_dir;
    gchar *filename = NULL;
    GtkWindow *parent;
    GtkWidget *chooser;

    /* create a filechooser as a child of the viewer */
    parent = GTK_WINDOW(gtk_widget_get_toplevel(GTK_WIDGET(viewer)));
    chooser = gtk_file_chooser_dialog_new("Save File", parent,
                                          GTK_FILE_CHOOSER_ACTION_SAVE,
                                          GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
                                          GTK_STOCK_SAVE, GTK_RESPONSE_ACCEPT,
                                          NULL);
    gtk_file_chooser_set_do_overwrite_confirmation(GTK_FILE_CHOOSER(chooser),
                                                   TRUE);
    g_signal_connect(G_OBJECT(chooser), "response",
                     G_CALLBACK(evbp_chooser_response_cb), viewer);
    gtk_window_set_modal(GTK_WINDOW(chooser), TRUE);

    /* filter per the current document type */
    ev_document_factory_add_filters(chooser, viewer->document);

    /* Set the initial directory to the xdg download directory. FIXME: This
     * should probably check mozilla's download directory first. */
    if ((download_dir = g_get_user_special_dir(G_USER_DIRECTORY_DOWNLOAD)))
        gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(chooser),
                                            download_dir);

    /* Try to determine an appropriate initial filename. What we get from
     * mozilla is likely a garbage temporary name, so try the document title
     * first. */
    title = ev_document_get_title(viewer->document);
    if (title) {
        EvTypeInfo *typeinfo;
        const gchar *suffix;

        typeinfo = ev_backends_manager_get_document_type_info(viewer->document);
        suffix = evbp_mime_get_suffix(*typeinfo->mime_types);
        filename = g_strdup_printf("%s.%s", title, suffix);
        g_free(typeinfo);
    } else {
        const gchar *uri;
        gchar *path;

        uri = ev_document_get_uri(viewer->document);
        path = g_filename_from_uri(uri, NULL, NULL);
        if (path) {
            filename = g_path_get_basename(path);
            g_free(path);
        }
    }
    if (filename) {
        gtk_file_chooser_set_current_name(GTK_FILE_CHOOSER(chooser),
                                          filename);
        g_free(filename);
    }

    gtk_widget_show(chooser);
}

static void
evbp_viewer_open_evince(GtkAction *action, EvbpViewer *viewer)
{
    GAppInfoCreateFlags flags;
    GAppInfo *app;
    GAppLaunchContext *context;
    const gchar *docuri;
    GList *uris = NULL;
    GError *error = NULL;

    /* create the evince app info and launch context */
    flags = G_APP_INFO_CREATE_SUPPORTS_URIS |
        G_APP_INFO_CREATE_SUPPORTS_STARTUP_NOTIFICATION;
    app = g_app_info_create_from_commandline("evince", NULL, flags, &error);
    if (!app) {
        g_warning("Couldn't create application info for evince: %s\n",
                  error->message);
        g_error_free(error);
        return;
    }
    context = g_app_launch_context_new();

    /* launch evince with the current uri */
    docuri = ev_document_get_uri(viewer->document);
    uris = g_list_append(uris, (gpointer)docuri);
    if (!g_app_info_launch_uris(G_APP_INFO(app), uris, NULL, &error)) {
        g_warning("Couldn't launch evince with file %s: %s\n", docuri,
                  error->message);
        g_error_free(error);
    }

    g_list_free(uris);
    g_object_unref(context);
    g_object_unref(app);
}

static void
evbp_print_done_cb(EvPrintOperation *print, GtkPrintOperationResult result,
                   gpointer data)
{
    EvbpViewer *viewer = data;

    /* FIXME: handle storing print settings */
    if (result == GTK_PRINT_OPERATION_RESULT_ERROR) {
        GtkWidget *dialog;
        GError *error = NULL;

        ev_print_operation_get_error(print, &error);
        dialog = gtk_message_dialog_new(GTK_WINDOW(viewer),
                                        GTK_DIALOG_DESTROY_WITH_PARENT,
                                        GTK_MESSAGE_ERROR,
                                        GTK_BUTTONS_CLOSE,
                                        "Failed to print document");
        gtk_message_dialog_format_secondary_text(GTK_MESSAGE_DIALOG(dialog),
                                                 error->message);
        g_error_free(error);
        g_signal_connect(dialog, "response", G_CALLBACK(gtk_widget_destroy),
                         NULL);
        gtk_widget_show(dialog);
    }
}

static void
evbp_viewer_print(GtkAction *action, EvbpViewer *viewer)
{
    EvPrintOperation *print;
    const gchar *title;
    gint current_page;
    GtkWindow *parent;

    print = ev_print_operation_new(viewer->document);
    if (!print) {
        g_warning("Printing is not supported for this document\n");
        return;
    }
    g_signal_connect(print, "done", G_CALLBACK(evbp_print_done_cb), viewer);

    /* setup the print job with a reasonable title if possible */
    title = ev_document_get_title(viewer->document);
    if (title)
        ev_print_operation_set_job_name(print, title);
    current_page = ev_document_model_get_page(viewer->model);
    ev_print_operation_set_current_page(print, current_page);

    parent = GTK_WINDOW(gtk_widget_get_toplevel(GTK_WIDGET(viewer)));
    ev_print_operation_run(print, GTK_WINDOW(parent));
}

static void
evbp_viewer_previous_page(GtkAction *action, EvbpViewer *viewer)
{
    ev_view_previous_page(EV_VIEW(viewer->view));
}

static void
evbp_viewer_next_page(GtkAction *action, EvbpViewer *viewer)
{
    ev_view_next_page(EV_VIEW(viewer->view));
}

static void
sizing_mode_handler(GObject *gobject, GParamSpec *pspec, gpointer user_data)
{
    EvDocumentModel *model = EV_DOCUMENT_MODEL(gobject);
    EvbpViewer *viewer = EVBP_VIEWER(user_data);
    EvSizingMode mode;
    GtkAction *action;

    /* set toggle states based on model */
    mode = ev_document_model_get_sizing_mode(model);

    action = gtk_action_group_get_action(viewer->action_group,
                                         "ViewBestFit");
    g_signal_handlers_block_by_func(action,
                                    G_CALLBACK(evbp_viewer_zoom_best_fit),
                                    viewer);
    gtk_toggle_action_set_active(GTK_TOGGLE_ACTION(action),
                                 mode == EV_SIZING_BEST_FIT);
    g_signal_handlers_unblock_by_func(action,
                                      G_CALLBACK(evbp_viewer_zoom_best_fit),
                                      viewer);

    action = gtk_action_group_get_action(viewer->action_group,
                                         "ViewPageWidth");
    g_signal_handlers_block_by_func(action,
                                    G_CALLBACK(evbp_viewer_zoom_page_width),
                                    viewer);
    gtk_toggle_action_set_active(GTK_TOGGLE_ACTION(action),
                                 mode == EV_SIZING_FIT_WIDTH);
    g_signal_handlers_unblock_by_func(action,
                                      G_CALLBACK(evbp_viewer_zoom_page_width),
                                      viewer);
}

static void
continuous_handler(GObject *gobject, GParamSpec *pspec, gpointer user_data)
{
    EvDocumentModel *model = EV_DOCUMENT_MODEL(gobject);
    EvbpViewer *viewer = EVBP_VIEWER(user_data);
    GtkAction *action;

    /* set toggle state based on model */
    action = gtk_action_group_get_action(viewer->action_group,
                                         "ViewContinuous");
    g_signal_handlers_block_by_func(action,
                                    G_CALLBACK(evbp_viewer_toggle_continuous),
                                    viewer);
    gtk_toggle_action_set_active(GTK_TOGGLE_ACTION(action),
                                 ev_document_model_get_continuous(model));
    g_signal_handlers_unblock_by_func(action,
                                      G_CALLBACK(evbp_viewer_toggle_continuous),
                                      viewer);
}

static void
page_action_link_activated(GtkAction *action, GObject *obj, gpointer user_data)
{
    EvLink *link = EV_LINK(obj);
    EvbpViewer *viewer = EVBP_VIEWER(user_data);

    ev_view_handle_link(EV_VIEW(viewer->view), link);
    gtk_widget_grab_focus(viewer->view);
}

static void
focus_page_selector(GtkAction *action, EvbpViewer *viewer)
{
    GtkAction *page_action;

    page_action = gtk_action_group_get_action(viewer->action_group,
                                              "PageSelector");
    ev_page_action_grab_focus(EV_PAGE_ACTION(page_action));
}

static gboolean
view_focus_changed(GtkWidget *widget, GdkEvent *event, gpointer user_data)
{
    GdkEventFocus *focus = (GdkEventFocus *)event;
    EvbpViewer *viewer = EVBP_VIEWER(user_data);

    if (viewer->accel_group)
        gtk_action_group_set_sensitive(viewer->accel_group, focus->in);

    return FALSE;
}

static void
evbp_viewer_dispose(GObject *object)
{
    EvbpViewer *viewer = EVBP_VIEWER(object);

    if (viewer->document) {
        g_object_unref(viewer->document);
        viewer->document = NULL;
    }
    if (viewer->model) {
        g_object_unref(viewer->model);
        viewer->model = NULL;
    }
    if (viewer->action_group) {
        g_object_unref(viewer->action_group);
        viewer->action_group = NULL;
    }
    if (viewer->accel_group) {
        g_object_unref(viewer->accel_group);
        viewer->accel_group = NULL;
    }
    if (viewer->ui_manager) {
        g_object_unref(viewer->ui_manager);
        viewer->ui_manager = NULL;
    }

    G_OBJECT_CLASS(evbp_viewer_parent_class)->dispose(object);
}

static void
evbp_viewer_hierarchy_changed(GtkWidget *widget, GtkWidget *previous_toplevel)
{
    EvbpViewer *viewer = EVBP_VIEWER(widget);
    GtkWidget *toplevel;
    GtkAccelGroup *accel_group;

    toplevel = gtk_widget_get_toplevel(widget);
    if (gtk_widget_is_toplevel(toplevel)) {
        accel_group = gtk_ui_manager_get_accel_group(viewer->ui_manager);
        gtk_window_add_accel_group(GTK_WINDOW(toplevel), accel_group);
    }
}

static void
evbp_viewer_class_init(EvbpViewerClass *class)
{
    GObjectClass *object_class = G_OBJECT_CLASS(class);
    GtkWidgetClass *widget_class = GTK_WIDGET_CLASS(class);

    object_class->dispose = evbp_viewer_dispose;
    widget_class->hierarchy_changed = evbp_viewer_hierarchy_changed;
}

static const GtkActionEntry action_entries[] = {
    { "SaveCopy", GTK_STOCK_SAVE_AS,
      "_Save Copy", "<control>s",
      "Save a copy of the document",
      G_CALLBACK(evbp_viewer_save_copy) },
    { "OpenInEvince", "evince",
      "_Open in Evince", NULL,
      "Open the document in the Evince viewer",
      G_CALLBACK(evbp_viewer_open_evince) },
    { "Print", GTK_STOCK_PRINT,
      "_Print", "<control>p",
      "Print the document",
      G_CALLBACK(evbp_viewer_print) },
    { "GoPreviousPage", GTK_STOCK_GO_UP,
      "_Previous Page", "<control>Page_Up",
      "Go to the previous page",
      G_CALLBACK(evbp_viewer_previous_page) },
    { "GoNextPage", GTK_STOCK_GO_DOWN,
      "_Next Page", "<control>Page_Down",
      "Go to the next page",
      G_CALLBACK(evbp_viewer_next_page) }
};

static const GtkToggleActionEntry toggle_action_entries[] = {
    { "ViewBestFit", EV_STOCK_ZOOM_PAGE,
      "_Best Fit", NULL,
      "Make the document fill the window",
      G_CALLBACK(evbp_viewer_zoom_best_fit), FALSE },
    { "ViewPageWidth", EV_STOCK_ZOOM_WIDTH,
      "Fit Page _Width", NULL,
      "Make the document fill the window width",
      G_CALLBACK(evbp_viewer_zoom_page_width), FALSE },
    { "ViewContinuous", EV_STOCK_VIEW_CONTINUOUS,
      "_Continuous", NULL,
      "Show the entire document",
      G_CALLBACK(evbp_viewer_toggle_continuous), FALSE },
};

static const GtkActionEntry accel_entries[] = {
    { "FocusPageSelector", NULL, "", "<control>l", NULL,
      G_CALLBACK(focus_page_selector) },
};

static const gchar ui_string[] =
    "<ui>\n"
    "  <toolbar name=\"ViewerToolbar\">\n"
    "    <toolitem name=\"SaveCopy\" action =\"SaveCopy\"/>\n"
    "    <toolitem name=\"OpenInEvince\" action =\"OpenInEvince\"/>\n"
    "    <toolitem name=\"Print\" action=\"Print\"/>\n"
    "    <separator/>\n"
    "    <toolitem name=\"GoPreviousPage\" action=\"GoPreviousPage\"/>\n"
    "    <toolitem name=\"GoNextPage\" action=\"GoNextPage\"/>\n"
    "    <separator/>\n"
    "    <toolitem name=\"PageSelector\" action=\"PageSelector\"/>\n"
    "    <separator/>\n"
    "    <toolitem name=\"ViewBestFit\" action=\"ViewBestFit\"/>\n"
    "    <toolitem name=\"ViewPageWidth\" action=\"ViewPageWidth\"/>\n"
    "    <toolitem name=\"ViewContinuous\" action=\"ViewContinuous\"/>\n"
    "  </toolbar>\n"
    "  <accelerator name=\"FocusPageSelector\" action=\"FocusPageSelector\"/>\n"
    "</ui>\n";

static void
evbp_viewer_init(EvbpViewer *viewer)
{
    GError *error = NULL;
    GtkWidget *toolbar;
    GtkAction *action;
    GList *actions;

    gtk_orientable_set_orientation(GTK_ORIENTABLE(viewer),
                                   GTK_ORIENTATION_VERTICAL);

    viewer->model = ev_document_model_new();

    viewer->action_group = gtk_action_group_new("ViewerActions");
    gtk_action_group_add_actions(viewer->action_group, action_entries,
                                 G_N_ELEMENTS(action_entries), viewer);
    gtk_action_group_add_toggle_actions(viewer->action_group,
                                        toggle_action_entries,
                                        G_N_ELEMENTS(toggle_action_entries),
                                        viewer);
    action = g_object_new(EV_TYPE_PAGE_ACTION,
                          "name", "PageSelector",
                          "label", "Page",
                          "tooltip", "Select Page",
                          "icon-name", "text-x-generic",
                          "visible-overflown", FALSE,
                          NULL);
    ev_page_action_set_model(EV_PAGE_ACTION(action), viewer->model);
    g_signal_connect(action, "activate-link",
                     G_CALLBACK(page_action_link_activated), viewer);
    gtk_action_group_add_action(viewer->action_group, action);
    g_object_unref(action);
    gtk_action_group_set_sensitive(viewer->action_group, FALSE);

    viewer->accel_group = gtk_action_group_new("ViewerAccelerators");
    gtk_action_group_add_actions(viewer->accel_group, accel_entries,
                                 G_N_ELEMENTS(accel_entries), viewer);
    gtk_action_group_set_sensitive(viewer->accel_group, FALSE);

    viewer->ui_manager = gtk_ui_manager_new();
    gtk_ui_manager_insert_action_group(viewer->ui_manager,
                                       viewer->action_group, 0);
    gtk_ui_manager_insert_action_group(viewer->ui_manager,
                                       viewer->accel_group, 1);
    if (!gtk_ui_manager_add_ui_from_string(viewer->ui_manager, ui_string,
                                           sizeof(ui_string) - 1, &error)) {
        g_warning("Failed to load ui from string: %s", error->message);
        g_error_free(error);
        error = NULL;
    }

    toolbar = gtk_ui_manager_get_widget(viewer->ui_manager, "/ViewerToolbar");
    gtk_box_pack_start(GTK_BOX(viewer), toolbar, FALSE, FALSE, 0);
    gtk_widget_show(toolbar);

    /* connect the accelerators since GtkUIManager only does that for menus */
    gtk_ui_manager_ensure_update(viewer->ui_manager);
    actions = gtk_action_group_list_actions(viewer->action_group);
    g_list_foreach(actions, (GFunc)gtk_action_connect_accelerator, NULL);
    g_list_free(actions);

    /* wire up toolbar callbacks */
    sizing_mode_handler(G_OBJECT(viewer->model), NULL, viewer);
    continuous_handler(G_OBJECT(viewer->model), NULL, viewer);
    g_signal_connect(viewer->model, "notify::sizing-mode",
                     G_CALLBACK(sizing_mode_handler), viewer);
    g_signal_connect(viewer->model, "notify::continuous",
                     G_CALLBACK(continuous_handler), viewer);

    viewer->scroll = gtk_scrolled_window_new(NULL, NULL);
    gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(viewer->scroll),
                                   GTK_POLICY_AUTOMATIC,
                                   GTK_POLICY_AUTOMATIC);
    gtk_widget_show(viewer->scroll);

    viewer->view = ev_view_new();
    g_signal_connect(viewer->view, "focus-in-event",
                     G_CALLBACK(view_focus_changed), viewer);
    g_signal_connect(viewer->view, "focus-out-event",
                     G_CALLBACK(view_focus_changed), viewer);
    ev_view_set_model(EV_VIEW(viewer->view), viewer->model);
    ev_view_set_loading(EV_VIEW(viewer->view), TRUE);
    gtk_widget_show(viewer->view);

    gtk_container_add(GTK_CONTAINER(viewer->scroll), viewer->view);
    gtk_box_pack_end(GTK_BOX(viewer), viewer->scroll, TRUE, TRUE, 0);
}

GtkWidget *
evbp_viewer_new(void)
{
    return g_object_new(EVBP_TYPE_VIEWER, NULL);
}

gboolean
evbp_viewer_load_uri(EvbpViewer *viewer, const gchar *uri, GError **error)
{
    if (viewer->document)
        g_object_unref(viewer->document);

    viewer->document = ev_document_factory_get_document(uri, error);
    g_return_val_if_fail(viewer->document, FALSE);

    g_debug("loading document \"%s\"", uri);
    ev_document_model_set_document(viewer->model, viewer->document);
    ev_view_set_loading(EV_VIEW(viewer->view), FALSE);
    gtk_action_group_set_sensitive(viewer->action_group, TRUE);

    return TRUE;
}