]> www.fi.muni.cz Git - evince.git/blob - shell/ev-history.c
Do not call g_type_class_peek_parent twice.
[evince.git] / shell / ev-history.c
1 /*
2  *  Copyright (C) 2005 Marco Pesenti Gritti
3  *
4  *  This program is free software; you can redistribute it and/or modify
5  *  it under the terms of the GNU General Public License as published by
6  *  the Free Software Foundation; either version 2, or (at your option)
7  *  any later version.
8  *
9  *  This program is distributed in the hope that it will be useful,
10  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  *  GNU General Public License for more details.
13  *
14  *  You should have received a copy of the GNU General Public License
15  *  along with this program; if not, write to the Free Software
16  *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17  *
18  */
19
20 #include "config.h"
21
22 #include <glib/gi18n.h>
23
24 #include "ev-history.h"
25
26 struct _EvHistoryPrivate
27 {
28         GList *links;
29         int current_index;
30 };
31
32 enum {
33         PROP_0,
34         PROP_INDEX
35 };
36
37 static void ev_history_init       (EvHistory *history);
38 static void ev_history_class_init (EvHistoryClass *class);
39
40 G_DEFINE_TYPE (EvHistory, ev_history, G_TYPE_OBJECT)
41
42 #define EV_HISTORY_GET_PRIVATE(object)(G_TYPE_INSTANCE_GET_PRIVATE ((object), EV_TYPE_HISTORY, EvHistoryPrivate))
43
44 static void
45 ev_history_init (EvHistory *history)
46 {
47         history->priv = EV_HISTORY_GET_PRIVATE (history);
48
49         history->priv->links = NULL;
50         history->priv->current_index = -1;
51 }
52
53 static void
54 free_links_list (GList *l)
55 {
56         g_list_foreach (l, (GFunc)g_object_unref, NULL);
57         g_list_free (l);
58 }
59
60 static void
61 ev_history_finalize (GObject *object)
62 {
63         EvHistory *history = EV_HISTORY (object);
64
65         free_links_list (history->priv->links);
66
67         G_OBJECT_CLASS (ev_history_parent_class)->finalize (object);
68 }
69
70 static void
71 ev_history_get_property (GObject *object, guint prop_id, GValue *value,
72                          GParamSpec *param_spec)
73 {
74         EvHistory *self;
75
76         self = EV_HISTORY (object);
77
78         switch (prop_id) {
79         case PROP_INDEX:
80                 g_value_set_int (value, self->priv->current_index);
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_history_set_property (GObject *object, guint prop_id, const GValue *value,
92                          GParamSpec *param_spec)
93 {
94         EvHistory *self;
95         
96         self = EV_HISTORY (object);
97         
98         switch (prop_id) {
99         case PROP_INDEX:
100                 ev_history_set_current_index (self, g_value_get_int (value));
101                 break;
102         default:
103                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object,
104                                                    prop_id,
105                                                    param_spec);
106                 break;
107         }
108 }
109
110 static void
111 ev_history_class_init (EvHistoryClass *class)
112 {
113         GObjectClass *object_class = G_OBJECT_CLASS (class);
114
115         object_class->finalize = ev_history_finalize;
116         object_class->set_property = ev_history_set_property;
117         object_class->get_property = ev_history_get_property;
118
119         g_object_class_install_property (object_class,
120                                          PROP_INDEX,
121                                          g_param_spec_int ("index",
122                                                            "Current Index",
123                                                            "The current index",
124                                                             -1,
125                                                             G_MAXINT,
126                                                             0,
127                                                             G_PARAM_READWRITE));
128
129         g_type_class_add_private (object_class, sizeof (EvHistoryPrivate));
130 }
131
132 void
133 ev_history_add_link (EvHistory *history, EvLink *link)
134 {
135         int length;
136
137         g_return_if_fail (EV_IS_HISTORY (history));
138         g_return_if_fail (EV_IS_LINK (link));
139
140         length = g_list_length (history->priv->links);
141         if (history->priv->current_index < length - 1) {
142                 GList *l = g_list_nth (history->priv->links,
143                                        history->priv->current_index + 1);
144                 
145                 if (l->prev) {
146                         l->prev->next = NULL;
147                         free_links_list (l);
148                 } else {
149                         free_links_list (history->priv->links);
150                         history->priv->links = NULL;
151                 }
152         }
153
154         g_object_ref (link);
155         history->priv->links = g_list_append (history->priv->links,
156                                               link);
157
158         length = g_list_length (history->priv->links);
159         history->priv->current_index = length - 1;
160 }
161
162 void
163 ev_history_add_page (EvHistory *history, int page, const gchar *label)
164 {
165         EvLink *link;
166         EvLinkDest *dest;
167         EvLinkAction *action;
168         gchar *title;
169
170         g_return_if_fail (EV_IS_HISTORY (history));
171         
172         title = g_strdup_printf (_("Page: %s"), label);
173
174         dest = ev_link_dest_new_page (page);
175         action = ev_link_action_new_dest (dest);
176         link = ev_link_new (title, action);
177         g_free (title);
178
179         ev_history_add_link (history, link);
180 }
181
182 EvLink *
183 ev_history_get_link_nth (EvHistory *history, int index)
184 {
185         GList *l;
186
187         g_return_val_if_fail (EV_IS_HISTORY (history), NULL);
188
189         l = g_list_nth (history->priv->links, index);
190
191         return EV_LINK (l->data);
192 }
193
194 int
195 ev_history_get_n_links (EvHistory *history)
196 {
197         g_return_val_if_fail (EV_IS_HISTORY (history), -1);
198
199         return g_list_length (history->priv->links);
200 }
201
202 int
203 ev_history_get_current_index (EvHistory *history)
204 {
205         g_return_val_if_fail (EV_IS_HISTORY (history), -1);
206
207         return history->priv->current_index;
208 }
209
210 void
211 ev_history_set_current_index (EvHistory *history, int index)
212 {
213         g_return_if_fail (EV_IS_HISTORY (history));
214
215         history->priv->current_index = index;
216
217         g_object_notify (G_OBJECT (history), "index");
218 }
219
220 EvHistory *
221 ev_history_new (void)
222 {
223         return EV_HISTORY (g_object_new (EV_TYPE_HISTORY, NULL));
224 }