]> www.fi.muni.cz Git - evince.git/blob - shell/ev-history.c
Fix several history bugs
[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  *  $Id$
19  */
20
21 #include "config.h"
22
23 #include <glib/gi18n.h>
24
25 #include "ev-history.h"
26
27 struct _EvHistoryPrivate
28 {
29         GList *links;
30         int current_index;
31 };
32
33 static void ev_history_init       (EvHistory *history);
34 static void ev_history_class_init (EvHistoryClass *class);
35
36 static GObjectClass *parent_class = NULL;
37
38 G_DEFINE_TYPE (EvHistory, ev_history, G_TYPE_OBJECT)
39
40 #define EV_HISTORY_GET_PRIVATE(object)(G_TYPE_INSTANCE_GET_PRIVATE ((object), EV_TYPE_HISTORY, EvHistoryPrivate))
41
42 static void
43 ev_history_init (EvHistory *history)
44 {
45         history->priv = EV_HISTORY_GET_PRIVATE (history);
46
47         history->priv->links = NULL;
48         history->priv->current_index = -1;
49 }
50
51 static void
52 free_links_list (GList *l)
53 {
54         g_list_foreach (l, (GFunc)g_object_unref, NULL);
55         g_list_free (l);
56 }
57
58 static void
59 ev_history_finalize (GObject *object)
60 {
61         EvHistory *history = EV_HISTORY (object);
62
63         free_links_list (history->priv->links);
64
65         parent_class->finalize (object);
66 }
67
68 static void
69 ev_history_class_init (EvHistoryClass *class)
70 {
71         GObjectClass *object_class = G_OBJECT_CLASS (class);
72
73         object_class->finalize = ev_history_finalize;
74
75         parent_class = g_type_class_peek_parent (class);
76
77         g_type_class_add_private (object_class, sizeof (EvHistoryPrivate));
78 }
79
80 void
81 ev_history_add_link (EvHistory *history, EvLink *link)
82 {
83         int length;
84
85         g_return_if_fail (EV_IS_HISTORY (history));
86         g_return_if_fail (EV_IS_LINK (link));
87
88         length = g_list_length (history->priv->links);
89         if (history->priv->current_index < length - 1) {
90                 GList *l = g_list_nth (history->priv->links,
91                                        history->priv->current_index + 1);
92                 
93                 if (l->prev) {
94                         l->prev->next = NULL;
95                         free_links_list (l);
96                 } else {
97                         free_links_list (history->priv->links);
98                         history->priv->links = NULL;
99                 }
100         }
101
102         g_object_ref (link);
103         history->priv->links = g_list_append (history->priv->links,
104                                               link);
105
106         length = g_list_length (history->priv->links);
107         history->priv->current_index = length - 1;
108 }
109
110 void
111 ev_history_add_page (EvHistory *history, int page)
112 {
113         EvLink *link;
114         char *title;
115
116         g_return_if_fail (EV_IS_HISTORY (history));
117
118         title = g_strdup_printf (_("Page %d\n"), page);
119         link = ev_link_new_page (title, page);
120         g_free (title);
121
122         ev_history_add_link (history, link);
123 }
124
125 EvLink *
126 ev_history_get_link_nth (EvHistory *history, int index)
127 {
128         GList *l;
129
130         g_return_val_if_fail (EV_IS_HISTORY (history), NULL);
131
132         l = g_list_nth (history->priv->links, index);
133
134         return EV_LINK (l->data);
135 }
136
137 int
138 ev_history_get_n_links (EvHistory *history)
139 {
140         g_return_val_if_fail (EV_IS_HISTORY (history), -1);
141
142         return g_list_length (history->priv->links);
143 }
144
145 int
146 ev_history_get_current_index (EvHistory *history)
147 {
148         g_return_val_if_fail (EV_IS_HISTORY (history), -1);
149
150         return history->priv->current_index;
151 }
152
153 void
154 ev_history_set_current_index (EvHistory *history, int index)
155 {
156         g_return_if_fail (EV_IS_HISTORY (history));
157
158         history->priv->current_index = index;
159 }
160
161 EvHistory *
162 ev_history_new (void)
163 {
164         return EV_HISTORY (g_object_new (EV_TYPE_HISTORY, NULL));
165 }