]> www.fi.muni.cz Git - evince.git/blob - shell/ev-history.c
Preliminary history implementation
[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 static GObjectClass *parent_class = NULL;
41
42 G_DEFINE_TYPE (EvHistory, ev_history, G_TYPE_OBJECT)
43
44 #define EV_HISTORY_GET_PRIVATE(object)(G_TYPE_INSTANCE_GET_PRIVATE ((object), EV_TYPE_HISTORY, EvHistoryPrivate))
45
46 static void
47 ev_history_init (EvHistory *history)
48 {
49         history->priv = EV_HISTORY_GET_PRIVATE (history);
50
51         history->priv->links = NULL;
52         history->priv->current_index = -1;
53 }
54
55 static void
56 free_links_list (GList *l)
57 {
58         g_list_foreach (l, (GFunc)g_object_unref, NULL);
59         g_list_free (l);
60 }
61
62 static void
63 ev_history_finalize (GObject *object)
64 {
65         EvHistory *history = EV_HISTORY (object);
66
67         free_links_list (history->priv->links);
68
69         parent_class->finalize (object);
70 }
71
72 static void
73 ev_history_get_property (GObject *object, guint prop_id, GValue *value,
74                          GParamSpec *param_spec)
75 {
76         EvHistory *self;
77
78         self = EV_HISTORY (object);
79
80         switch (prop_id) {
81         case PROP_INDEX:
82                 g_value_set_int (value, self->priv->current_index);
83                 break;
84         default:
85                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object,
86                                                    prop_id,
87                                                    param_spec);
88                 break;
89         }
90 }
91
92 static void
93 ev_history_set_property (GObject *object, guint prop_id, const GValue *value,
94                          GParamSpec *param_spec)
95 {
96         EvHistory *self;
97         
98         self = EV_HISTORY (object);
99         
100         switch (prop_id) {
101         case PROP_INDEX:
102                 ev_history_set_current_index (self, g_value_get_int (value));
103                 break;
104         default:
105                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object,
106                                                    prop_id,
107                                                    param_spec);
108                 break;
109         }
110 }
111
112 static void
113 ev_history_class_init (EvHistoryClass *class)
114 {
115         GObjectClass *object_class = G_OBJECT_CLASS (class);
116
117         object_class->finalize = ev_history_finalize;
118         object_class->set_property = ev_history_set_property;
119         object_class->get_property = ev_history_get_property;
120
121         parent_class = g_type_class_peek_parent (class);
122
123         g_object_class_install_property (object_class,
124                                          PROP_INDEX,
125                                          g_param_spec_int ("index",
126                                                            "Current Index",
127                                                            "The current index",
128                                                             -1,
129                                                             G_MAXINT,
130                                                             0,
131                                                             G_PARAM_READWRITE));
132
133         g_type_class_add_private (object_class, sizeof (EvHistoryPrivate));
134 }
135
136 void
137 ev_history_add_link (EvHistory *history, EvLink *link)
138 {
139         int length;
140
141         g_return_if_fail (EV_IS_HISTORY (history));
142         g_return_if_fail (EV_IS_LINK (link));
143
144         length = g_list_length (history->priv->links);
145         if (history->priv->current_index < length - 1) {
146                 GList *l = g_list_nth (history->priv->links,
147                                        history->priv->current_index + 1);
148                 
149                 if (l->prev) {
150                         l->prev->next = NULL;
151                         free_links_list (l);
152                 } else {
153                         free_links_list (history->priv->links);
154                         history->priv->links = NULL;
155                 }
156         }
157
158         g_object_ref (link);
159         history->priv->links = g_list_append (history->priv->links,
160                                               link);
161
162         length = g_list_length (history->priv->links);
163         history->priv->current_index = length - 1;
164 }
165
166 void
167 ev_history_add_page (EvHistory *history, int page)
168 {
169         EvLink *link;
170         EvLinkDest *dest;
171         EvLinkAction *action;
172         gchar *title;
173
174         g_return_if_fail (EV_IS_HISTORY (history));
175         title = g_strdup_printf (_("Page: %d"), page);
176
177         dest = ev_link_dest_new_page (page);
178         action = ev_link_action_new_dest (dest);
179         link = ev_link_new (title, action);
180         g_free (title);
181
182         ev_history_add_link (history, link);
183 }
184
185 EvLink *
186 ev_history_get_link_nth (EvHistory *history, int index)
187 {
188         GList *l;
189
190         g_return_val_if_fail (EV_IS_HISTORY (history), NULL);
191
192         l = g_list_nth (history->priv->links, index);
193
194         return EV_LINK (l->data);
195 }
196
197 int
198 ev_history_get_n_links (EvHistory *history)
199 {
200         g_return_val_if_fail (EV_IS_HISTORY (history), -1);
201
202         return g_list_length (history->priv->links);
203 }
204
205 int
206 ev_history_get_current_index (EvHistory *history)
207 {
208         g_return_val_if_fail (EV_IS_HISTORY (history), -1);
209
210         return history->priv->current_index;
211 }
212
213 void
214 ev_history_set_current_index (EvHistory *history, int index)
215 {
216         g_return_if_fail (EV_IS_HISTORY (history));
217
218         history->priv->current_index = index;
219
220         g_object_notify (G_OBJECT (history), "index");
221 }
222
223 EvHistory *
224 ev_history_new (void)
225 {
226         return EV_HISTORY (g_object_new (EV_TYPE_HISTORY, NULL));
227 }