]> www.fi.muni.cz Git - evince.git/blob - backend/ev-link.c
7e6eb52251c42dfbaaa53024210ddf29fa3e71c4
[evince.git] / backend / 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
19  */
20
21 #ifdef HAVE_CONFIG_H
22 #include "config.h"
23 #endif
24
25 #include "ev-link.h"
26
27 enum {
28         PROP_0,
29         PROP_TITLE,
30         PROP_TYPE,
31         PROP_PAGE,
32         PROP_URI
33 };
34
35
36 struct _EvLink {
37         GObject base_instance;
38         EvLinkPrivate *priv;
39 };
40
41 struct _EvLinkClass {
42         GObjectClass base_class;
43 };
44
45 struct _EvLinkPrivate {
46         char *title;
47         char *uri;
48         EvLinkType type;
49         int page;
50 };
51
52 G_DEFINE_TYPE (EvLink, ev_link, G_TYPE_OBJECT)
53
54 #define EV_LINK_GET_PRIVATE(object) \
55         (G_TYPE_INSTANCE_GET_PRIVATE ((object), EV_TYPE_LINK, EvLinkPrivate))
56
57 GType
58 ev_link_type_get_type (void)
59 {
60         static GType type = 0;
61
62         if (G_UNLIKELY (type == 0)) {
63                 static const GEnumValue values[] = {
64                         { EV_LINK_TYPE_TITLE, "EV_LINK_TYPE_TITLE", "title" },
65                         { EV_LINK_TYPE_PAGE, "EV_LINK_TYPE_PAGE", "page" },
66                         { EV_LINK_TYPE_EXTERNAL_URI, "EV_LINK_TYPE_EXTERNAL_URI", "external" },
67                         { 0, NULL, NULL }
68                 };
69
70                 type = g_enum_register_static ("EvLinkType", values);
71         }
72
73         return type;
74 }
75
76 const char *
77 ev_link_get_title (EvLink *self)
78 {
79         g_return_val_if_fail (EV_IS_LINK (self), NULL);
80         
81         return self->priv->title;
82 }
83
84 void
85 ev_link_set_title (EvLink* self, const char *title)
86 {
87         g_assert (EV_IS_LINK (self));
88         g_assert (title != NULL);
89
90         if (self->priv->title != NULL) {
91                 g_free (self->priv->title);
92         }
93
94         self->priv->title = g_strdup (title);
95
96         g_object_notify (G_OBJECT (self), "title");
97 }
98
99 const char *
100 ev_link_get_uri (EvLink *self)
101 {
102         g_return_val_if_fail (EV_IS_LINK (self), NULL);
103         
104         return self->priv->uri;
105 }
106
107 void
108 ev_link_set_uri (EvLink* self, const char *uri)
109 {
110         g_assert (EV_IS_LINK (self));
111         g_assert (uri != NULL);
112
113         if (self->priv->uri != NULL) {
114                 g_free (self->priv->uri);
115         }
116
117         self->priv->uri = g_strdup (uri);
118
119         g_object_notify (G_OBJECT (self), "uri");
120 }
121
122 EvLinkType
123 ev_link_get_link_type (EvLink *self)
124 {
125         g_return_val_if_fail (EV_IS_LINK (self), 0);
126         
127         return self->priv->type;
128 }
129
130 void
131 ev_link_set_link_type (EvLink* self, EvLinkType type)
132 {
133         g_assert (EV_IS_LINK (self));
134
135         self->priv->type = type;
136
137         g_object_notify (G_OBJECT (self), "type");
138 }
139
140 int
141 ev_link_get_page (EvLink *self)
142 {
143         g_return_val_if_fail (EV_IS_LINK (self), 0);
144         
145         return self->priv->page;
146 }
147
148 void
149 ev_link_set_page (EvLink* self, int page)
150 {
151         g_assert (EV_IS_LINK (self));
152
153         self->priv->page = page;
154
155         g_object_notify (G_OBJECT (self), "page");
156 }
157
158 static void
159 ev_link_get_property (GObject *object, guint prop_id, GValue *value,
160                       GParamSpec *param_spec)
161 {
162         EvLink *self;
163
164         self = EV_LINK (object);
165
166         switch (prop_id) {
167         case PROP_TITLE:
168                 g_value_set_string (value, self->priv->title);
169                 break;
170         case PROP_URI:
171                 g_value_set_string (value, self->priv->uri);
172                 break;
173         case PROP_TYPE:
174                 g_value_set_enum (value, self->priv->type);
175                 break;
176         case PROP_PAGE:
177                 g_value_set_int (value, self->priv->page);
178                 break;
179         default:
180                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object,
181                                                    prop_id,
182                                                    param_spec);
183                 break;
184         }
185 }
186
187 static void
188 ev_link_set_property (GObject *object, guint prop_id, const GValue *value,
189                       GParamSpec *param_spec)
190 {
191         EvLink *self;
192         
193         self = EV_LINK (object);
194         
195         switch (prop_id) {
196         case PROP_TITLE:
197                 ev_link_set_title (self, g_value_get_string (value));
198                 break;
199         case PROP_URI:
200                 ev_link_set_uri (self, g_value_get_string (value));
201                 break;
202         case PROP_TYPE:
203                 ev_link_set_link_type (self, g_value_get_enum (value));
204                 break;
205         case PROP_PAGE:
206                 ev_link_set_page (self, g_value_get_int (value));
207                 break;
208         default:
209                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object,
210                                                    prop_id,
211                                                    param_spec);
212                 break;
213         }
214 }
215
216 static void
217 ev_window_dispose (GObject *object)
218 {
219         EvLinkPrivate *priv;
220
221         g_return_if_fail (EV_IS_LINK (object));
222
223         priv = EV_LINK (object)->priv;
224
225         if (priv->title) {
226                 g_free (priv->title);
227                 priv->title = NULL;
228         }
229
230         G_OBJECT_CLASS (ev_link_parent_class)->dispose (object);
231 }
232
233 static void
234 ev_link_init (EvLink *ev_link)
235 {
236         ev_link->priv = EV_LINK_GET_PRIVATE (ev_link);
237
238         ev_link->priv->type = EV_LINK_TYPE_TITLE;
239 }
240
241 static void
242 ev_link_class_init (EvLinkClass *ev_window_class)
243 {
244         GObjectClass *g_object_class;
245
246         g_object_class = G_OBJECT_CLASS (ev_window_class);
247         g_object_class->dispose = ev_window_dispose;
248         g_object_class->set_property = ev_link_set_property;
249         g_object_class->get_property = ev_link_get_property;
250
251         g_type_class_add_private (g_object_class, sizeof (EvLinkPrivate));
252
253         g_object_class_install_property (g_object_class,
254                                          PROP_TITLE,
255                                          g_param_spec_string ("title",
256                                                               "Link Title",
257                                                               "The link title",
258                                                               NULL,
259                                                               G_PARAM_READWRITE));
260
261         g_object_class_install_property (g_object_class,
262                                          PROP_URI,
263                                          g_param_spec_string ("uri",
264                                                               "Link URI",
265                                                               "The link URI",
266                                                               NULL,
267                                                               G_PARAM_READWRITE));
268
269         g_object_class_install_property (g_object_class,
270                                          PROP_TYPE,
271                                          g_param_spec_enum  ("type",
272                                                              "Link Type",
273                                                              "The link type",
274                                                              EV_TYPE_LINK_TYPE,
275                                                              EV_LINK_TYPE_TITLE,
276                                                              G_PARAM_READWRITE));
277
278         g_object_class_install_property (g_object_class,
279                                          PROP_PAGE,
280                                          g_param_spec_int ("page",
281                                                            "Link Page",
282                                                            "The link page",
283                                                             0,
284                                                             G_MAXINT,
285                                                             0,
286                                                             G_PARAM_READWRITE));
287 }
288
289 EvLink *
290 ev_link_new_title (const char *title)
291 {
292         return EV_LINK (g_object_new (EV_TYPE_LINK,
293                                       "title", title,
294                                       "type", EV_LINK_TYPE_TITLE,
295                                       NULL));
296 }
297
298 EvLink *
299 ev_link_new_page (const char *title, int page)
300 {
301         return EV_LINK (g_object_new (EV_TYPE_LINK,
302                                       "title", title,
303                                       "page", page,
304                                       "type", EV_LINK_TYPE_PAGE,
305                                       NULL));
306 }
307
308 EvLink *
309 ev_link_new_external (const char *title, const char *uri)
310 {
311         return EV_LINK (g_object_new (EV_TYPE_LINK,
312                                       "title", title,
313                                       "uri", uri,
314                                       "type", EV_LINK_TYPE_EXTERNAL_URI,
315                                       NULL));
316 }