]> www.fi.muni.cz Git - evince.git/blob - libdocument/ev-link.c
[pdf] Implement EvDocumentText interface
[evince.git] / libdocument / ev-link.c
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8; c-indent-level: 8 -*- */
2 /* this file is part of evince, a gnome document viewer
3  *
4  *  Copyright (C) 2005 Red Hat, Inc.
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19  */
20
21 #include <config.h>
22 #include "ev-link.h"
23
24 enum {
25         PROP_0,
26         PROP_TITLE,
27         PROP_ACTION
28 };
29
30 struct _EvLink {
31         GObject base_instance;
32         EvLinkPrivate *priv;
33 };
34
35 struct _EvLinkClass {
36         GObjectClass base_class;
37 };
38
39 struct _EvLinkPrivate {
40         gchar        *title;
41         EvLinkAction *action;
42 };
43
44 G_DEFINE_TYPE (EvLink, ev_link, G_TYPE_OBJECT)
45
46 #define EV_LINK_GET_PRIVATE(object) \
47         (G_TYPE_INSTANCE_GET_PRIVATE ((object), EV_TYPE_LINK, EvLinkPrivate))
48
49 const gchar *
50 ev_link_get_title (EvLink *self)
51 {
52         g_return_val_if_fail (EV_IS_LINK (self), NULL);
53         
54         return self->priv->title;
55 }
56
57 EvLinkAction *
58 ev_link_get_action (EvLink *self)
59 {
60         g_return_val_if_fail (EV_IS_LINK (self), NULL);
61         
62         return self->priv->action;
63 }
64
65 static void
66 ev_link_get_property (GObject    *object,
67                       guint       prop_id,
68                       GValue     *value,
69                       GParamSpec *param_spec)
70 {
71         EvLink *self;
72
73         self = EV_LINK (object);
74
75         switch (prop_id) {
76                 case PROP_TITLE:
77                         g_value_set_string (value, self->priv->title);
78                         break;
79                 case PROP_ACTION:
80                         g_value_set_pointer (value, self->priv->action);
81                         break;
82                 default:
83                         G_OBJECT_WARN_INVALID_PROPERTY_ID (object,
84                                                            prop_id,
85                                                            param_spec);
86                         break;
87         }
88 }
89
90 static void
91 ev_link_set_property (GObject      *object,
92                       guint         prop_id,
93                       const GValue *value,
94                       GParamSpec   *param_spec)
95 {
96         EvLink *self = EV_LINK (object);
97         
98         switch (prop_id) {
99                 case PROP_TITLE:
100                         self->priv->title = g_value_dup_string (value); 
101                         break;
102                 case PROP_ACTION:
103                         self->priv->action = g_value_get_pointer (value);
104                         break;
105                 default:
106                         G_OBJECT_WARN_INVALID_PROPERTY_ID (object,
107                                                            prop_id,
108                                                            param_spec);
109                         break;
110         }
111 }
112
113 static void
114 ev_link_finalize (GObject *object)
115 {
116         EvLinkPrivate *priv;
117
118         priv = EV_LINK (object)->priv;
119
120         if (priv->title) {
121                 g_free (priv->title);
122                 priv->title = NULL;
123         }
124
125         if (priv->action) {
126                 g_object_unref (priv->action);
127                 priv->action = NULL;
128         }
129
130         G_OBJECT_CLASS (ev_link_parent_class)->finalize (object);
131 }
132
133 static void
134 ev_link_init (EvLink *ev_link)
135 {
136         ev_link->priv = EV_LINK_GET_PRIVATE (ev_link);
137
138         ev_link->priv->title = NULL;
139         ev_link->priv->action = NULL;
140 }
141
142 static void
143 ev_link_class_init (EvLinkClass *ev_window_class)
144 {
145         GObjectClass *g_object_class;
146
147         g_object_class = G_OBJECT_CLASS (ev_window_class);
148
149         g_object_class->set_property = ev_link_set_property;
150         g_object_class->get_property = ev_link_get_property;
151
152         g_object_class->finalize = ev_link_finalize;
153
154         g_type_class_add_private (g_object_class, sizeof (EvLinkPrivate));
155
156         g_object_class_install_property (g_object_class,
157                                          PROP_TITLE,
158                                          g_param_spec_string ("title",
159                                                               "Link Title",
160                                                               "The link title",
161                                                               NULL,
162                                                               G_PARAM_READWRITE |
163                                                               G_PARAM_CONSTRUCT_ONLY));
164         g_object_class_install_property (g_object_class,
165                                          PROP_ACTION,
166                                          g_param_spec_pointer ("action",
167                                                                "Link Action",
168                                                                "The link action",
169                                                                G_PARAM_READWRITE |
170                                                                G_PARAM_CONSTRUCT_ONLY));
171 }
172
173 EvLink *
174 ev_link_new (const char   *title,
175              EvLinkAction *action)
176 {
177         return EV_LINK (g_object_new (EV_TYPE_LINK,
178                                       "title", title,
179                                       "action", action,
180                                       NULL));
181 }
182
183 gint
184 ev_link_get_page (EvLink *link)
185 {
186         EvLinkAction *action;
187         EvLinkDest *dest;
188
189         action = ev_link_get_action (link);
190         if (!action)
191                 return -1;
192
193         if (ev_link_action_get_action_type (action) !=
194             EV_LINK_ACTION_TYPE_GOTO_DEST)
195                 return -1;
196
197         dest = ev_link_action_get_dest (action);
198         if (dest)
199                 return ev_link_dest_get_page (dest);
200                 
201         return -1;
202 }