annotate src/history.c @ 0:6ee11bf9e3ea

Initial revision
author jcid
date Sun, 07 Oct 2007 00:36:34 +0200
parents
children 4a5942beffdf
rev   line source
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1 /*
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
2 * File: history.c
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
3 *
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
4 * Copyright (C) 2001, 2002 Jorge Arellano Cid <jcid@dillo.org>
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
5 *
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
6 * This program is free software; you can redistribute it and/or modify
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
7 * it under the terms of the GNU General Public License as published by
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
8 * the Free Software Foundation; either version 3 of the License, or
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
9 * (at your option) any later version.
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
10 */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
11
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
12 /*
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
13 * Linear history (it also provides indexes for the navigation stack)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
14 */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
15
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
16 #include "list.h"
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
17 #include "history.h"
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
18
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
19
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
20 typedef struct {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
21 DilloUrl *url;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
22 char *title;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
23 } H_Item;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
24
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
25
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
26 /* Global history list */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
27 static H_Item *history = NULL;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
28 static int history_size = 0; /* [1 based] */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
29 static int history_size_max = 16;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
30
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
31
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
32 /*
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
33 * Add a new H_Item at the end of the history list
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
34 * (taking care of not making a duplicate entry)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
35 */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
36 int a_History_add_url(DilloUrl *url)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
37 {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
38 int i, idx;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
39
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
40 for (i = 0; i < history_size; ++i)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
41 if (a_Url_cmp(history[i].url, url) == 0)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
42 return i;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
43
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
44 idx = history_size;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
45 a_List_add(history, history_size, history_size_max);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
46 history[idx].url = a_Url_dup(url);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
47 history[idx].title = NULL;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
48 ++history_size;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
49 return idx;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
50 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
51
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
52 /*
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
53 * Set the page-title for a given URL (by idx)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
54 * (this is known when the first chunks of HTML data arrive)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
55 */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
56 int a_History_set_title(int idx, const char *title)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
57 {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
58 dReturn_val_if_fail(idx >= 0 && idx < history_size, 0);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
59
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
60 dFree(history[idx].title);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
61 history[idx].title = dStrdup(title);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
62 return 1;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
63 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
64
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
65 /*
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
66 * Return the DilloUrl camp (by index)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
67 */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
68 DilloUrl *a_History_get_url(int idx)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
69 {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
70 dReturn_val_if_fail(idx >= 0 && idx < history_size, NULL);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
71
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
72 return history[idx].url;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
73 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
74
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
75 /*
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
76 * Return the title camp (by index)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
77 * ('force' returns URL_STR when there's no title)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
78 */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
79 const char *a_History_get_title(int idx, int force)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
80 {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
81 dReturn_val_if_fail(idx >= 0 && idx < history_size, NULL);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
82
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
83 if (history[idx].title)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
84 return history[idx].title;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
85 else if (force)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
86 return URL_STR(history[idx].url);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
87 else
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
88 return NULL;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
89 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
90
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
91 /*
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
92 * Return the title camp (by url)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
93 * ('force' returns URL_STR when there's no title)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
94 */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
95 const char *a_History_get_title_by_url(DilloUrl *url, int force)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
96 {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
97 int i;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
98
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
99 dReturn_val_if_fail(url != NULL, NULL);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
100
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
101 for (i = 0; i < history_size; ++i)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
102 if (a_Url_cmp(url, history[i].url) == 0)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
103 break;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
104
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
105 if (i < history_size && history[i].title)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
106 return history[i].title;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
107 else if (force)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
108 return URL_STR_(url);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
109 return NULL;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
110 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
111
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
112
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
113 /*
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
114 * Free all the memory used by this module
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
115 */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
116 void a_History_free()
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
117 {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
118 int i;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
119
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
120 for (i = 0; i < history_size; ++i) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
121 a_Url_free(history[i].url);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
122 dFree(history[i].title);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
123 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
124 dFree(history);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
125 }