1 | /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */ |
---|
2 | /* e-history.c |
---|
3 | * |
---|
4 | * Copyright (C) 2002 Ximian, Inc. |
---|
5 | * |
---|
6 | * This program is free software; you can redistribute it and/or |
---|
7 | * modify it under the terms of version 2 of the GNU General Public |
---|
8 | * License as published by the Free Software Foundation. |
---|
9 | * |
---|
10 | * This program is distributed in the hope that it will be useful, |
---|
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
13 | * General Public License for more details. |
---|
14 | * |
---|
15 | * You should have received a copy of the GNU General Public |
---|
16 | * License along with this program; if not, write to the |
---|
17 | * Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
---|
18 | * Boston, MA 02111-1307, USA. |
---|
19 | * |
---|
20 | * Author: Ettore Perazzoli <ettore@ximian.com> |
---|
21 | */ |
---|
22 | |
---|
23 | #ifdef HAVE_CONFIG_H |
---|
24 | #include <config.h> |
---|
25 | #endif |
---|
26 | |
---|
27 | #include "e-history.h" |
---|
28 | |
---|
29 | #include <gal/util/e-util.h> |
---|
30 | |
---|
31 | |
---|
32 | #define PARENT_TYPE gtk_object_get_type () |
---|
33 | static GtkObjectClass *parent_class = NULL; |
---|
34 | |
---|
35 | struct _EHistoryPrivate { |
---|
36 | EHistoryItemFreeFunc item_free_function; |
---|
37 | |
---|
38 | GList *items; |
---|
39 | GList *current_item; |
---|
40 | }; |
---|
41 | |
---|
42 | |
---|
43 | /* GtkObject methods. */ |
---|
44 | |
---|
45 | static void |
---|
46 | impl_destroy (GtkObject *object) |
---|
47 | { |
---|
48 | EHistory *history; |
---|
49 | EHistoryPrivate *priv; |
---|
50 | GList *p; |
---|
51 | |
---|
52 | history = E_HISTORY (object); |
---|
53 | priv = history->priv; |
---|
54 | |
---|
55 | for (p = priv->items; p != NULL; p = p->next) |
---|
56 | (* priv->item_free_function) (p->data); |
---|
57 | |
---|
58 | g_list_free (priv->items); |
---|
59 | |
---|
60 | g_free (priv); |
---|
61 | |
---|
62 | (* GTK_OBJECT_CLASS (parent_class)->destroy) (object); |
---|
63 | } |
---|
64 | |
---|
65 | |
---|
66 | static void |
---|
67 | class_init (GtkObjectClass *object_class) |
---|
68 | { |
---|
69 | parent_class = gtk_type_class (PARENT_TYPE); |
---|
70 | |
---|
71 | object_class->destroy = impl_destroy; |
---|
72 | } |
---|
73 | |
---|
74 | static void |
---|
75 | init (EHistory *history) |
---|
76 | { |
---|
77 | EHistoryPrivate *priv; |
---|
78 | |
---|
79 | priv = g_new (EHistoryPrivate, 1); |
---|
80 | priv->items = NULL; |
---|
81 | priv->current_item = NULL; |
---|
82 | |
---|
83 | history->priv = priv; |
---|
84 | |
---|
85 | GTK_OBJECT_UNSET_FLAGS (history, GTK_FLOATING); |
---|
86 | } |
---|
87 | |
---|
88 | |
---|
89 | void |
---|
90 | e_history_construct (EHistory *history, |
---|
91 | EHistoryItemFreeFunc item_free_function) |
---|
92 | { |
---|
93 | EHistoryPrivate *priv; |
---|
94 | |
---|
95 | g_return_if_fail (history != NULL); |
---|
96 | g_return_if_fail (E_IS_HISTORY (history)); |
---|
97 | |
---|
98 | priv = history->priv; |
---|
99 | |
---|
100 | priv->item_free_function = item_free_function; |
---|
101 | } |
---|
102 | |
---|
103 | EHistory * |
---|
104 | e_history_new (EHistoryItemFreeFunc item_free_function) |
---|
105 | { |
---|
106 | EHistory *history; |
---|
107 | |
---|
108 | history = gtk_type_new (e_history_get_type ()); |
---|
109 | e_history_construct (history, item_free_function); |
---|
110 | |
---|
111 | return history; |
---|
112 | } |
---|
113 | |
---|
114 | void * |
---|
115 | e_history_prev (EHistory *history) |
---|
116 | { |
---|
117 | EHistoryPrivate *priv; |
---|
118 | |
---|
119 | g_return_val_if_fail (history != NULL, NULL); |
---|
120 | g_return_val_if_fail (E_IS_HISTORY (history), NULL); |
---|
121 | |
---|
122 | priv = history->priv; |
---|
123 | |
---|
124 | if (! e_history_has_prev (history)) |
---|
125 | return NULL; |
---|
126 | |
---|
127 | priv->current_item = priv->current_item->prev; |
---|
128 | return e_history_get_current (history); |
---|
129 | } |
---|
130 | |
---|
131 | gboolean |
---|
132 | e_history_has_prev (EHistory *history) |
---|
133 | { |
---|
134 | EHistoryPrivate *priv; |
---|
135 | |
---|
136 | g_return_val_if_fail (history != NULL, FALSE); |
---|
137 | g_return_val_if_fail (E_IS_HISTORY (history), FALSE); |
---|
138 | |
---|
139 | priv = history->priv; |
---|
140 | |
---|
141 | if (priv->current_item == NULL) |
---|
142 | return FALSE; |
---|
143 | |
---|
144 | if (priv->current_item->prev == NULL) |
---|
145 | return FALSE; |
---|
146 | else |
---|
147 | return TRUE; |
---|
148 | } |
---|
149 | |
---|
150 | void * |
---|
151 | e_history_next (EHistory *history) |
---|
152 | { |
---|
153 | EHistoryPrivate *priv; |
---|
154 | |
---|
155 | g_return_val_if_fail (history != NULL, NULL); |
---|
156 | g_return_val_if_fail (E_IS_HISTORY (history), NULL); |
---|
157 | |
---|
158 | priv = history->priv; |
---|
159 | |
---|
160 | if (! e_history_has_next (history)) |
---|
161 | return NULL; |
---|
162 | |
---|
163 | priv->current_item = priv->current_item->next; |
---|
164 | return e_history_get_current (history); |
---|
165 | } |
---|
166 | |
---|
167 | gboolean |
---|
168 | e_history_has_next (EHistory *history) |
---|
169 | { |
---|
170 | EHistoryPrivate *priv; |
---|
171 | |
---|
172 | g_return_val_if_fail (history != NULL, FALSE); |
---|
173 | g_return_val_if_fail (E_IS_HISTORY (history), FALSE); |
---|
174 | |
---|
175 | priv = history->priv; |
---|
176 | |
---|
177 | if (priv->current_item == NULL) |
---|
178 | return FALSE; |
---|
179 | |
---|
180 | if (priv->current_item->next == NULL) |
---|
181 | return FALSE; |
---|
182 | else |
---|
183 | return TRUE; |
---|
184 | } |
---|
185 | |
---|
186 | void * |
---|
187 | e_history_get_current (EHistory *history) |
---|
188 | { |
---|
189 | EHistoryPrivate *priv; |
---|
190 | |
---|
191 | g_return_val_if_fail (history != NULL, NULL); |
---|
192 | g_return_val_if_fail (E_IS_HISTORY (history), NULL); |
---|
193 | |
---|
194 | priv = history->priv; |
---|
195 | |
---|
196 | if (priv->current_item == NULL) |
---|
197 | return NULL; |
---|
198 | |
---|
199 | return priv->current_item->data; |
---|
200 | } |
---|
201 | |
---|
202 | void |
---|
203 | e_history_add (EHistory *history, |
---|
204 | void *data) |
---|
205 | { |
---|
206 | EHistoryPrivate *priv; |
---|
207 | |
---|
208 | g_return_if_fail (history != NULL); |
---|
209 | g_return_if_fail (E_IS_HISTORY (history)); |
---|
210 | |
---|
211 | priv = history->priv; |
---|
212 | |
---|
213 | if (priv->current_item == NULL) { |
---|
214 | priv->items = g_list_prepend (priv->items, data); |
---|
215 | priv->current_item = priv->items; |
---|
216 | |
---|
217 | return; |
---|
218 | } |
---|
219 | |
---|
220 | if (priv->current_item->next != NULL) { |
---|
221 | GList *p; |
---|
222 | |
---|
223 | for (p = priv->current_item->next; p != NULL; p = p->next) |
---|
224 | (* priv->item_free_function) (p->data); |
---|
225 | |
---|
226 | priv->current_item->next->prev = NULL; |
---|
227 | g_list_free (priv->current_item->next); |
---|
228 | |
---|
229 | priv->current_item->next = NULL; |
---|
230 | } |
---|
231 | |
---|
232 | g_list_append (priv->current_item, data); |
---|
233 | priv->current_item = priv->current_item->next; |
---|
234 | } |
---|
235 | |
---|
236 | void |
---|
237 | e_history_remove_matching (EHistory *history, |
---|
238 | const void *data, |
---|
239 | GCompareFunc compare_func) |
---|
240 | { |
---|
241 | EHistoryPrivate *priv; |
---|
242 | GList *p; |
---|
243 | |
---|
244 | g_return_if_fail (history != NULL); |
---|
245 | g_return_if_fail (E_IS_HISTORY (history)); |
---|
246 | g_return_if_fail (compare_func != NULL); |
---|
247 | |
---|
248 | priv = history->priv; |
---|
249 | |
---|
250 | for (p = priv->items; p != NULL; p = p->next) { |
---|
251 | if ((* compare_func) (data, p->data) == 0) { |
---|
252 | if (priv->items == priv->current_item) |
---|
253 | priv->items = priv->current_item = g_list_remove_link (priv->items, p); |
---|
254 | else |
---|
255 | priv->items = g_list_remove_link (priv->items, p); |
---|
256 | } |
---|
257 | } |
---|
258 | } |
---|
259 | |
---|
260 | |
---|
261 | E_MAKE_TYPE (e_history, "EHistory", EHistory, class_init, init, GTK_TYPE_OBJECT) |
---|