]> www.fi.muni.cz Git - evince.git/blob - shell/ev-sidebar-annotations.c
shell: Use GAppInfo to launch evince-previewer instead of gdk_spawn
[evince.git] / shell / ev-sidebar-annotations.c
1 /* ev-sidebar-annotations.c
2  *  this file is part of evince, a gnome document viewer
3  *
4  * Copyright (C) 2010 Carlos Garcia Campos  <carlosgc@gnome.org>
5  *
6  * Evince is free software; you can redistribute it and/or modify it
7  * under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * Evince is distributed in the hope that it will be useful, but
12  * WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
19  */
20
21 #include "config.h"
22
23 #include <glib/gi18n.h>
24
25 #include "ev-document-annotations.h"
26 #include "ev-sidebar-page.h"
27 #include "ev-sidebar-annotations.h"
28 #include "ev-jobs.h"
29 #include "ev-job-scheduler.h"
30 #include "ev-stock-icons.h"
31
32 enum {
33         PROP_0,
34         PROP_WIDGET
35 };
36
37 enum {
38         COLUMN_MARKUP,
39         COLUMN_ICON,
40         COLUMN_ANNOT_MAPPING,
41         N_COLUMNS
42 };
43
44 enum {
45         ANNOT_ACTIVATED,
46         BEGIN_ANNOT_ADD,
47         ANNOT_ADD_CANCELLED,
48         N_SIGNALS
49 };
50
51 struct _EvSidebarAnnotationsPrivate {
52         EvDocument  *document;
53
54         GtkWidget   *notebook;
55         GtkWidget   *tree_view;
56         GtkWidget   *palette;
57         GtkToolItem *annot_text_item;
58
59         EvJob       *job;
60         guint        selection_changed_id;
61 };
62
63 static void ev_sidebar_annotations_page_iface_init (EvSidebarPageInterface *iface);
64 static void ev_sidebar_annotations_load            (EvSidebarAnnotations   *sidebar_annots);
65
66 static guint signals[N_SIGNALS];
67
68 G_DEFINE_TYPE_EXTENDED (EvSidebarAnnotations,
69                         ev_sidebar_annotations,
70                         GTK_TYPE_VBOX,
71                         0,
72                         G_IMPLEMENT_INTERFACE (EV_TYPE_SIDEBAR_PAGE,
73                                                ev_sidebar_annotations_page_iface_init))
74
75 #define EV_SIDEBAR_ANNOTATIONS_GET_PRIVATE(object) \
76         (G_TYPE_INSTANCE_GET_PRIVATE ((object), EV_TYPE_SIDEBAR_ANNOTATIONS, EvSidebarAnnotationsPrivate))
77
78 static void
79 ev_sidebar_annotations_dispose (GObject *object)
80 {
81         EvSidebarAnnotations *sidebar_annots = EV_SIDEBAR_ANNOTATIONS (object);
82         EvSidebarAnnotationsPrivate *priv = sidebar_annots->priv;
83
84         if (priv->document) {
85                 g_object_unref (priv->document);
86                 priv->document = NULL;
87         }
88
89         G_OBJECT_CLASS (ev_sidebar_annotations_parent_class)->dispose (object);
90 }
91
92 static GtkTreeModel *
93 ev_sidebar_annotations_create_simple_model (const gchar *message)
94 {
95         GtkTreeModel *retval;
96         GtkTreeIter iter;
97         gchar *markup;
98
99         /* Creates a fake model to indicate that we're loading */
100         retval = (GtkTreeModel *)gtk_list_store_new (N_COLUMNS,
101                                                      G_TYPE_STRING,
102                                                      GDK_TYPE_PIXBUF,
103                                                      G_TYPE_POINTER);
104
105         gtk_list_store_append (GTK_LIST_STORE (retval), &iter);
106         markup = g_strdup_printf ("<span size=\"larger\" style=\"italic\">%s</span>",
107                                   message);
108         gtk_list_store_set (GTK_LIST_STORE (retval), &iter,
109                             COLUMN_MARKUP, markup,
110                             -1);
111         g_free (markup);
112
113         return retval;
114 }
115
116 static void
117 ev_sidebar_annotations_add_annots_list (EvSidebarAnnotations *ev_annots)
118 {
119         GtkWidget         *swindow;
120         GtkTreeModel      *loading_model;
121         GtkCellRenderer   *renderer;
122         GtkTreeViewColumn *column;
123         GtkTreeSelection  *selection;
124         GtkWidget         *label;
125
126         swindow = gtk_scrolled_window_new (NULL, NULL);
127         gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (swindow),
128                                         GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
129         gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (swindow),
130                                              GTK_SHADOW_IN);
131
132         /* Create tree view */
133         loading_model = ev_sidebar_annotations_create_simple_model (_("Loading…"));
134         ev_annots->priv->tree_view = gtk_tree_view_new_with_model (loading_model);
135         g_object_unref (loading_model);
136
137         gtk_tree_view_set_headers_visible (GTK_TREE_VIEW (ev_annots->priv->tree_view),
138                                            FALSE);
139         selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (ev_annots->priv->tree_view));
140         gtk_tree_selection_set_mode (selection, GTK_SELECTION_NONE);
141
142         column = gtk_tree_view_column_new ();
143
144         renderer = gtk_cell_renderer_pixbuf_new ();
145         gtk_tree_view_column_pack_start (column, renderer, FALSE);
146         gtk_tree_view_column_set_attributes (column, renderer,
147                                              "pixbuf", COLUMN_ICON,
148                                              NULL);
149
150         renderer = gtk_cell_renderer_text_new ();
151         gtk_tree_view_column_pack_start (column, renderer, TRUE);
152         gtk_tree_view_column_set_attributes (column, renderer,
153                                              "markup", COLUMN_MARKUP,
154                                              NULL);
155         gtk_tree_view_append_column (GTK_TREE_VIEW (ev_annots->priv->tree_view),
156                                      column);
157
158         gtk_container_add (GTK_CONTAINER (swindow), ev_annots->priv->tree_view);
159         gtk_widget_show (ev_annots->priv->tree_view);
160
161         label = gtk_label_new (_("List"));
162         gtk_notebook_append_page (GTK_NOTEBOOK (ev_annots->priv->notebook),
163                                   swindow, label);
164         gtk_widget_show (label);
165
166         gtk_widget_show (swindow);
167 }
168
169 static void
170 ev_sidebar_annotations_text_annot_button_toggled (GtkToggleToolButton  *toolbutton,
171                                                   EvSidebarAnnotations *sidebar_annots)
172 {
173         EvAnnotationType annot_type;
174
175         if (!gtk_toggle_tool_button_get_active (toolbutton)) {
176                 g_signal_emit (sidebar_annots, signals[ANNOT_ADD_CANCELLED], 0, NULL);
177                 return;
178         }
179
180         if (GTK_TOOL_ITEM (toolbutton) == sidebar_annots->priv->annot_text_item)
181                 annot_type = EV_ANNOTATION_TYPE_TEXT;
182         else
183                 annot_type = EV_ANNOTATION_TYPE_UNKNOWN;
184
185         g_signal_emit (sidebar_annots, signals[BEGIN_ANNOT_ADD], 0, annot_type);
186 }
187
188 static void
189 ev_sidebar_annotations_add_annots_palette (EvSidebarAnnotations *ev_annots)
190 {
191         GtkWidget   *swindow;
192         GtkWidget   *group;
193         GtkToolItem *item;
194         GtkWidget   *label;
195
196         swindow = gtk_scrolled_window_new (NULL, NULL);
197         gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (swindow),
198                                         GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
199         gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (swindow),
200                                              GTK_SHADOW_IN);
201
202         ev_annots->priv->palette = gtk_tool_palette_new ();
203         group = gtk_tool_item_group_new (_("Annotations"));
204         gtk_container_add (GTK_CONTAINER (ev_annots->priv->palette), group);
205
206         /* FIXME: use a better icon than EDIT */
207         item = gtk_toggle_tool_button_new ();
208         gtk_tool_button_set_icon_name (GTK_TOOL_BUTTON (item), GTK_STOCK_EDIT);
209         gtk_tool_button_set_label (GTK_TOOL_BUTTON (item), _("Text"));
210         gtk_widget_set_tooltip_text (GTK_WIDGET (item), _("Add text annotation"));
211         ev_annots->priv->annot_text_item = item;
212         g_signal_connect (item, "toggled",
213                           G_CALLBACK (ev_sidebar_annotations_text_annot_button_toggled),
214                           ev_annots);
215         gtk_tool_item_group_insert (GTK_TOOL_ITEM_GROUP (group), item, -1);
216         gtk_widget_show (GTK_WIDGET (item));
217
218         gtk_container_add (GTK_CONTAINER (swindow), ev_annots->priv->palette);
219         gtk_widget_show (ev_annots->priv->palette);
220
221         label = gtk_label_new (_("Add"));
222         gtk_notebook_append_page (GTK_NOTEBOOK (ev_annots->priv->notebook),
223                                   swindow, label);
224         gtk_widget_show (label);
225
226         gtk_widget_show (swindow);
227 }
228
229 static void
230 ev_sidebar_annotations_init (EvSidebarAnnotations *ev_annots)
231 {
232         ev_annots->priv = EV_SIDEBAR_ANNOTATIONS_GET_PRIVATE (ev_annots);
233
234         ev_annots->priv->notebook = gtk_notebook_new ();
235         gtk_notebook_set_show_tabs (GTK_NOTEBOOK (ev_annots->priv->notebook), FALSE);
236         gtk_notebook_set_show_border (GTK_NOTEBOOK (ev_annots->priv->notebook), FALSE);
237         ev_sidebar_annotations_add_annots_list (ev_annots);
238         ev_sidebar_annotations_add_annots_palette (ev_annots);
239         gtk_container_add (GTK_CONTAINER (ev_annots), ev_annots->priv->notebook);
240         gtk_widget_show (ev_annots->priv->notebook);
241 }
242
243 static void
244 ev_sidebar_annotations_get_property (GObject    *object,
245                                      guint       prop_id,
246                                      GValue     *value,
247                                      GParamSpec *pspec)
248 {
249         EvSidebarAnnotations *ev_sidebar_annots;
250
251         ev_sidebar_annots = EV_SIDEBAR_ANNOTATIONS (object);
252
253         switch (prop_id) {
254                 case PROP_WIDGET:
255                         g_value_set_object (value, ev_sidebar_annots->priv->notebook);
256                         break;
257                 default:
258                         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
259                         break;
260         }
261 }
262
263 static void
264 ev_sidebar_annotations_class_init (EvSidebarAnnotationsClass *klass)
265 {
266         GObjectClass *g_object_class = G_OBJECT_CLASS (klass);
267
268         g_object_class->get_property = ev_sidebar_annotations_get_property;
269         g_object_class->dispose = ev_sidebar_annotations_dispose;
270
271         g_type_class_add_private (g_object_class, sizeof (EvSidebarAnnotationsPrivate));
272
273         g_object_class_override_property (g_object_class, PROP_WIDGET, "main-widget");
274
275         signals[ANNOT_ACTIVATED] =
276                 g_signal_new ("annot-activated",
277                               G_TYPE_FROM_CLASS (g_object_class),
278                               G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
279                               G_STRUCT_OFFSET (EvSidebarAnnotationsClass, annot_activated),
280                               NULL, NULL,
281                               g_cclosure_marshal_VOID__POINTER,
282                               G_TYPE_NONE, 1,
283                               G_TYPE_POINTER);
284         signals[BEGIN_ANNOT_ADD] =
285                 g_signal_new ("begin-annot-add",
286                               G_TYPE_FROM_CLASS (g_object_class),
287                               G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
288                               G_STRUCT_OFFSET (EvSidebarAnnotationsClass, begin_annot_add),
289                               NULL, NULL,
290                               g_cclosure_marshal_VOID__ENUM,
291                               G_TYPE_NONE, 1,
292                               EV_TYPE_ANNOTATION_TYPE);
293         signals[ANNOT_ADD_CANCELLED] =
294                 g_signal_new ("annot-add-cancelled",
295                               G_TYPE_FROM_CLASS (g_object_class),
296                               G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
297                               G_STRUCT_OFFSET (EvSidebarAnnotationsClass, annot_add_cancelled),
298                               NULL, NULL,
299                               g_cclosure_marshal_VOID__VOID,
300                               G_TYPE_NONE, 0,
301                               G_TYPE_NONE);
302 }
303
304 GtkWidget *
305 ev_sidebar_annotations_new (void)
306 {
307         return GTK_WIDGET (g_object_new (EV_TYPE_SIDEBAR_ANNOTATIONS, NULL));
308 }
309
310 void
311 ev_sidebar_annotations_annot_added (EvSidebarAnnotations *sidebar_annots,
312                                     EvAnnotation         *annot)
313 {
314         GtkToggleToolButton *toolbutton;
315
316         if (EV_IS_ANNOTATION_TEXT (annot)) {
317                 toolbutton = GTK_TOGGLE_TOOL_BUTTON (sidebar_annots->priv->annot_text_item);
318                 g_signal_handlers_block_by_func (toolbutton,
319                                                  ev_sidebar_annotations_text_annot_button_toggled,
320                                                  sidebar_annots);
321                 gtk_toggle_tool_button_set_active (toolbutton, FALSE);
322                 g_signal_handlers_unblock_by_func (toolbutton,
323                                                    ev_sidebar_annotations_text_annot_button_toggled,
324                                                    sidebar_annots);
325         }
326
327         ev_sidebar_annotations_load (sidebar_annots);
328 }
329
330 static void
331 selection_changed_cb (GtkTreeSelection     *selection,
332                       EvSidebarAnnotations *sidebar_annots)
333 {
334         GtkTreeModel *model;
335         GtkTreeIter   iter;
336
337         if (gtk_tree_selection_get_selected (selection, &model, &iter)) {
338                 EvMapping *mapping = NULL;
339
340                 gtk_tree_model_get (model, &iter,
341                                     COLUMN_ANNOT_MAPPING, &mapping,
342                                     -1);
343                 if (mapping)
344                         g_signal_emit (sidebar_annots, signals[ANNOT_ACTIVATED], 0, mapping);
345         }
346 }
347
348 static void
349 job_finished_callback (EvJobAnnots          *job,
350                        EvSidebarAnnotations *sidebar_annots)
351 {
352         EvSidebarAnnotationsPrivate *priv;
353         GtkTreeStore *model;
354         GtkTreeSelection *selection;
355         GList *l;
356         GdkPixbuf *text_icon = NULL;
357         GdkPixbuf *attachment_icon = NULL;
358
359         priv = sidebar_annots->priv;
360
361         if (!job->annots) {
362                 GtkTreeModel *list;
363
364                 list = ev_sidebar_annotations_create_simple_model (_("Document contains no annotations"));
365                 gtk_tree_view_set_model (GTK_TREE_VIEW (priv->tree_view), list);
366                 g_object_unref (list);
367
368                 g_object_unref (job);
369                 priv->job = NULL;
370
371                 return;
372         }
373
374         selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->tree_view));
375         gtk_tree_selection_set_mode (selection, GTK_SELECTION_SINGLE);
376         if (priv->selection_changed_id == 0) {
377                 priv->selection_changed_id =
378                         g_signal_connect (selection, "changed",
379                                           G_CALLBACK (selection_changed_cb),
380                                           sidebar_annots);
381         }
382
383         model = gtk_tree_store_new (N_COLUMNS,
384                                     G_TYPE_STRING,
385                                     GDK_TYPE_PIXBUF,
386                                     G_TYPE_POINTER);
387
388         for (l = job->annots; l; l = g_list_next (l)) {
389                 EvMappingList *mapping_list;
390                 GList         *ll;
391                 gchar         *page_label;
392                 GtkTreeIter    iter;
393                 gboolean       found = FALSE;
394
395                 mapping_list = (EvMappingList *)l->data;
396                 page_label = g_strdup_printf (_("Page %d"),
397                                               ev_mapping_list_get_page (mapping_list) + 1);
398                 gtk_tree_store_append (model, &iter, NULL);
399                 gtk_tree_store_set (model, &iter,
400                                     COLUMN_MARKUP, page_label,
401                                     -1);
402                 g_free (page_label);
403
404                 for (ll = ev_mapping_list_get_list (mapping_list); ll; ll = g_list_next (ll)) {
405                         EvAnnotation *annot;
406                         const gchar  *label;
407                         const gchar  *modified;
408                         gchar        *markup;
409                         GtkTreeIter   child_iter;
410                         GdkPixbuf    *pixbuf = NULL;
411
412                         annot = ((EvMapping *)(ll->data))->data;
413                         if (!EV_IS_ANNOTATION_MARKUP (annot))
414                                 continue;
415
416                         label = ev_annotation_markup_get_label (EV_ANNOTATION_MARKUP (annot));
417                         modified = ev_annotation_get_modified (annot);
418                         if (modified) {
419                                 markup = g_strdup_printf ("<span weight=\"bold\">%s</span>\n%s",
420                                                           label, modified);
421                         } else {
422                                 markup = g_strdup_printf ("<span weight=\"bold\">%s</span>", label);
423                         }
424
425                         if (EV_IS_ANNOTATION_TEXT (annot)) {
426                                 if (!text_icon) {
427                                         /* FIXME: use a better icon than EDIT */
428                                         text_icon = gtk_widget_render_icon (priv->tree_view,
429                                                                             GTK_STOCK_EDIT,
430                                                                             GTK_ICON_SIZE_BUTTON,
431                                                                             NULL);
432                                 }
433                                 pixbuf = text_icon;
434                         } else if (EV_IS_ANNOTATION_ATTACHMENT (annot)) {
435                                 if (!attachment_icon) {
436                                         attachment_icon = gtk_widget_render_icon (priv->tree_view,
437                                                                                   EV_STOCK_ATTACHMENT,
438                                                                                   GTK_ICON_SIZE_BUTTON,
439                                                                                   NULL);
440                                 }
441                                 pixbuf = attachment_icon;
442                         }
443
444                         gtk_tree_store_append (model, &child_iter, &iter);
445                         gtk_tree_store_set (model, &child_iter,
446                                             COLUMN_MARKUP, markup,
447                                             COLUMN_ICON, pixbuf,
448                                             COLUMN_ANNOT_MAPPING, ll->data,
449                                             -1);
450                         g_free (markup);
451                         found = TRUE;
452                 }
453
454                 if (!found)
455                         gtk_tree_store_remove (model, &iter);
456         }
457
458         gtk_tree_view_set_model (GTK_TREE_VIEW (priv->tree_view),
459                                  GTK_TREE_MODEL (model));
460         g_object_unref (model);
461
462         if (text_icon)
463                 g_object_unref (text_icon);
464         if (attachment_icon)
465                 g_object_unref (attachment_icon);
466
467         g_object_unref (job);
468         priv->job = NULL;
469 }
470
471 static void
472 ev_sidebar_annotations_load (EvSidebarAnnotations *sidebar_annots)
473 {
474         EvSidebarAnnotationsPrivate *priv = sidebar_annots->priv;
475
476         if (priv->job) {
477                 g_signal_handlers_disconnect_by_func (priv->job,
478                                                       job_finished_callback,
479                                                       sidebar_annots);
480                 g_object_unref (priv->job);
481         }
482
483         priv->job = ev_job_annots_new (priv->document);
484         g_signal_connect (priv->job, "finished",
485                           G_CALLBACK (job_finished_callback),
486                           sidebar_annots);
487         /* The priority doesn't matter for this job */
488         ev_job_scheduler_push_job (priv->job, EV_JOB_PRIORITY_NONE);
489 }
490
491 static void
492 ev_sidebar_annotations_document_changed_cb (EvDocumentModel      *model,
493                                             GParamSpec           *pspec,
494                                             EvSidebarAnnotations *sidebar_annots)
495 {
496         EvDocument *document = ev_document_model_get_document (model);
497         EvSidebarAnnotationsPrivate *priv = sidebar_annots->priv;
498         gboolean show_tabs;
499
500         if (!EV_IS_DOCUMENT_ANNOTATIONS (document))
501                 return;
502
503         if (priv->document)
504                 g_object_unref (priv->document);
505         priv->document = g_object_ref (document);
506
507         show_tabs = ev_document_annotations_can_add_annotation (EV_DOCUMENT_ANNOTATIONS (document));
508         gtk_notebook_set_show_tabs (GTK_NOTEBOOK (priv->notebook), show_tabs);
509
510         ev_sidebar_annotations_load (sidebar_annots);
511 }
512
513 /* EvSidebarPageIface */
514 static void
515 ev_sidebar_annotations_set_model (EvSidebarPage   *sidebar_page,
516                                   EvDocumentModel *model)
517 {
518         g_signal_connect (model, "notify::document",
519                           G_CALLBACK (ev_sidebar_annotations_document_changed_cb),
520                           sidebar_page);
521 }
522
523 static gboolean
524 ev_sidebar_annotations_support_document (EvSidebarPage *sidebar_page,
525                                          EvDocument    *document)
526 {
527         return (EV_IS_DOCUMENT_ANNOTATIONS (document));
528 }
529
530 static const gchar *
531 ev_sidebar_annotations_get_label (EvSidebarPage *sidebar_page)
532 {
533         return _("Annotations");
534 }
535
536 static void
537 ev_sidebar_annotations_page_iface_init (EvSidebarPageInterface *iface)
538 {
539         iface->support_document = ev_sidebar_annotations_support_document;
540         iface->set_model = ev_sidebar_annotations_set_model;
541         iface->get_label = ev_sidebar_annotations_get_label;
542 }