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