GnuCash  2.6.99
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
gnc-html-history.c
1 /********************************************************************
2  * gnc-html-history.c -- keep a HTML history *
3  * Copyright (C) 2000 Bill Gribble <[email protected]> *
4  * *
5  * This program is free software; you can redistribute it and/or *
6  * modify it under the terms of the GNU General Public License as *
7  * published by the Free Software Foundation; either version 2 of *
8  * the License, or (at your option) any later version. *
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 *
13  * GNU General Public License for more details. *
14  * *
15  * You should have received a copy of the GNU General Public License*
16  * along with this program; if not, contact: *
17  * *
18  * Free Software Foundation Voice: +1-617-542-5942 *
19  * 51 Franklin Street, Fifth Floor Fax: +1-617-542-2652 *
20  * Boston, MA 02110-1301, USA [email protected] *
21  ********************************************************************/
22 
23 #include "config.h"
24 
25 #include <gtk/gtk.h>
26 #include <string.h>
27 
28 #include "gnc-html-history.h"
29 
31 {
32  GList * nodes;
33  GList * current_node;
34  GList * last_node;
35 
36  /* call this whenever a node is destroyed */
37  gnc_html_history_destroy_cb destroy_cb;
38  gpointer destroy_cb_data;
39 };
40 
41 /********************************************************************
42  * gnc_html_history_new
43  ********************************************************************/
44 
46 gnc_html_history_new(void)
47 {
48  gnc_html_history * hist = g_new0(gnc_html_history, 1);
49  hist->nodes = NULL;
50  hist->current_node = NULL;
51  hist->last_node = NULL;
52  return hist;
53 }
54 
55 
56 
57 /********************************************************************
58  * gnc_html_history_destroy
59  ********************************************************************/
60 
61 void
62 gnc_html_history_destroy(gnc_html_history * hist)
63 {
64  GList * n;
65 
66  for (n = hist->nodes; n ; n = n->next)
67  {
68  if (hist->destroy_cb)
69  {
70  (hist->destroy_cb)((gnc_html_history_node *)n->data,
71  hist->destroy_cb_data);
72  }
73  gnc_html_history_node_destroy((gnc_html_history_node *)n->data);
74  }
75  g_list_free(hist->nodes);
76 
77  hist->nodes = NULL;
78  hist->current_node = NULL;
79  hist->last_node = NULL;
80  g_free(hist);
81 }
82 
83 /********************************************************************
84  * gnc_html_history_set_node_destroy_cb
85  ********************************************************************/
86 
87 void
88 gnc_html_history_set_node_destroy_cb(gnc_html_history * hist,
89  gnc_html_history_destroy_cb cb,
90  gpointer cb_data)
91 {
92  hist->destroy_cb = cb;
93  hist->destroy_cb_data = cb_data;
94 }
95 
96 static int
97 g_strcmp(char * a, char * b)
98 {
99  if (!a && b)
100  {
101  return 1;
102  }
103  else if (a && !b)
104  {
105  return -1;
106  }
107  else if (!a && !b)
108  {
109  return 0;
110  }
111  else
112  {
113  return strcmp(a, b);
114  }
115 
116 }
117 
118 
119 /********************************************************************
120  * gnc_html_history_append
121  ********************************************************************/
122 void
123 gnc_html_history_append(gnc_html_history * hist,
124  gnc_html_history_node * node)
125 {
126  GList * n;
128 
129  if (hist->current_node)
130  {
131  hn = hist->current_node->data;
132  if ((hn->type == node->type) &&
133  !g_strcmp(hn->location, node->location) &&
134  !g_strcmp(hn->label, node->label))
135  {
136  if (hist->destroy_cb)
137  {
138  (hist->destroy_cb)(hn, hist->destroy_cb_data);
139  }
140  gnc_html_history_node_destroy(node);
141  return;
142  }
143 
144  /* blow away the history after this point, if there is one */
145  for (n = hist->current_node->next; n; n = n->next)
146  {
147  if (hist->destroy_cb)
148  {
149  (hist->destroy_cb)((gnc_html_history_node *)n->data,
150  hist->destroy_cb_data);
151  }
152  gnc_html_history_node_destroy((gnc_html_history_node *)n->data);
153  }
154  g_list_free(hist->current_node->next);
155  hist->current_node->next = NULL;
156  hist->last_node = hist->current_node;
157  }
158 
159  n = g_list_alloc();
160  n->data = (gpointer) node;
161  n->next = NULL;
162  n->prev = NULL;
163 
164  if (hist->nodes && hist->last_node)
165  {
166  n->prev = hist->last_node; /* back pointer */
167  hist->last_node->next = n; /* add n to the list */
168  hist->last_node = n; /* n is last */
169  hist->current_node = n;
170  }
171  else
172  {
173  /* this is the first entry in the list */
174  if (hist->nodes)
175  {
176  g_print ("???? hist->nodes non-NULL, but no last_node \n");
177  }
178  hist->nodes = n;
179  hist->last_node = n;
180  hist->current_node = n;
181  }
182 }
183 
184 
185 /********************************************************************
186  * gnc_html_history_get_current
187  ********************************************************************/
188 
190 gnc_html_history_get_current(gnc_html_history * hist)
191 {
192  if (!hist || !(hist->current_node)) return NULL;
193 
194  return hist->current_node->data;
195 }
196 
197 
198 /********************************************************************
199  * gnc_html_history_forward
200  ********************************************************************/
201 
203 gnc_html_history_forward(gnc_html_history * hist)
204 {
205  if (!hist || !(hist->current_node))
206  {
207  return NULL;
208  }
209 
210  if (hist->current_node->next)
211  {
212  hist->current_node = hist->current_node->next;
213  }
214 
215  return hist->current_node->data;
216 }
217 
218 
219 /********************************************************************
220  * gnc_html_history_back
221  ********************************************************************/
222 
224 gnc_html_history_back(gnc_html_history * hist)
225 {
226 
227  if (!hist || !(hist->current_node))
228  {
229  return NULL;
230  }
231 
232  if (hist->current_node->prev)
233  {
234  hist->current_node = hist->current_node->prev;
235  }
236 
237  return hist->current_node->data;
238 }
239 
240 
241 /********************************************************************
242  * gnc_html_history_back_p
243  * is it possible to go back?
244  ********************************************************************/
245 
246 int
247 gnc_html_history_back_p(gnc_html_history * hist)
248 {
249  if (hist && hist->current_node && hist->current_node->prev)
250  {
251  return TRUE;
252  }
253  else
254  {
255  return FALSE;
256  }
257 }
258 
259 
260 /********************************************************************
261  * gnc_html_history_forward_p
262  * is it possible to go forward?
263  ********************************************************************/
264 
265 int
266 gnc_html_history_forward_p(gnc_html_history * hist)
267 {
268  if (hist && hist->current_node && hist->current_node->next)
269  {
270  return TRUE;
271  }
272  else
273  {
274  return FALSE;
275  }
276 }
277 
278 
279 /********************************************************************
280  * gnc_html_history_node_new
281  ********************************************************************/
282 
284 gnc_html_history_node_new(URLType type, const gchar * location,
285  const gchar * label)
286 {
288 
289  rv->type = g_strdup(type);
290  rv->location = g_strdup(location);
291  rv->label = g_strdup(label);
292  return rv;
293 }
294 
295 
296 /********************************************************************
297  * gnc_html_history_node_destroy
298  ********************************************************************/
299 
300 void
301 gnc_html_history_node_destroy(gnc_html_history_node * node)
302 {
303 
304  /* free the url resources and cached text */
305  g_free(node->type);
306  g_free(node->location);
307  g_free(node->label);
308 
309  node->location = NULL;
310  node->label = NULL;
311 
312  g_free(node);
313 }