Merge branch 'master' of https://git.maemo.org/projects/erwise
[erwise] / Cl / WWWLibrary / HTHistory.c
1 #include "HTHistory.h"
2
3 #include "tcp.h"                /* for standard io */
4
5 static HTList * history;        /* List of visited anchors */
6
7
8 /*                              Navigation
9 **                              ==========
10 */
11
12 /*              Record the jump to an anchor
13 **              ----------------------------
14 */
15
16 void HTHistory_record
17   ARGS1 (HTAnchor *,destination)
18 {
19   if (destination) {
20     if (! history)
21       history = HTList_new();
22     HTList_addObject (history, destination);
23   }
24 }
25
26 /*              Go back in history (find the last visited node)
27 **              ------------------
28 */
29
30 HTAnchor * HTHistory_backtrack
31   NOARGS  /* FIXME: Should we add a `sticky' option ? */
32 {
33   if (HTHistory_canBacktrack())
34     HTList_removeLastObject (history);
35   return HTList_lastObject (history);  /* is Home if can't backtrack */
36 }
37
38 BOOL HTHistory_canBacktrack
39   NOARGS
40 {
41   return (HTList_objectAt (history, 1) != NULL);
42 }
43
44 /*              Browse through references in the same parent node
45 **              -------------------------------------------------
46 **
47 **      Take the n-th child's link after or before the one we took to get here.
48 **      Positive offset means go towards most recently added children.
49 */
50
51 HTAnchor * HTHistory_moveBy
52  ARGS1 (int,offset)
53 {
54   HTAnchor * last = HTList_objectAt (history, 1);
55   if (! last)
56     return NULL;  /* No last visited node */
57   if (last != (HTAnchor *) last->parent) {  /* Was a child */
58     HTList * kids = last->parent->children;
59     int i = HTList_indexOf (kids, last); 
60     HTAnchor * nextOne = HTList_objectAt (kids, i - offset);
61     if (nextOne) {
62       HTAnchor * destination = HTAnchor_followMainLink (nextOne);
63       if (destination) {
64         HTList_removeLastObject (history);
65         HTList_removeLastObject (history);
66         HTList_addObject (history, nextOne);
67         HTList_addObject (history, destination);
68       }
69       return destination;
70     } else {
71       if (TRACE) printf(
72                 "HTHistory_moveBy: offset by %+d goes out of list %p.\n",
73                 offset, kids);
74       return NULL;
75     }
76   } else {  /* Was a parent */
77     return NULL;  /* FIXME we could possibly follow the next link... */
78   }
79 }
80
81 BOOL HTHistory_canMoveBy
82  ARGS1 (int,offset)
83 {
84   HTAnchor * last = HTList_objectAt (history, 1);
85   if (! last)
86     return NO;  /* No last visited node */
87   if (last != (HTAnchor *) last->parent) {  /* Was a child */
88     HTList * kids = last->parent->children;
89     int i = HTList_indexOf (kids, last); 
90     return (HTList_objectAt (kids, i - offset) != NULL);
91   } else {  /* Was a parent */
92     return NO;  /* FIXME we could possibly follow the next link... */
93   }
94 }
95
96
97 /*                              Retrieval
98 **                              =========
99 */
100
101 /*              Read numbered visited anchor (1 is the oldest)
102 **              ----------------------------
103 */
104
105 HTAnchor * HTHistory_read
106   ARGS1 (int,number)
107 {
108   return HTList_objectAt (history, HTList_count (history) - number);
109 }
110
111
112 /*              Recall numbered visited anchor (1 is the oldest)
113 **              ------------------------------
114 **      This reads the anchor and stores it again in the list, except if last.
115 */
116
117 HTAnchor * HTHistory_recall
118   ARGS1 (int,number)
119 {
120   HTAnchor * destination =
121     HTList_objectAt (history, HTList_count (history) - number);
122   if (destination && destination != HTList_lastObject (history))
123     HTList_addObject (history, destination);
124   return destination;
125 }
126
127 /*              Number of Anchors stored
128 **              ------------------------
129 **
130 **      This is needed in order to check the validity of certain commands
131 **      for menus, etc.
132 (not needed for now. Use canBacktrack, etc.)
133 int HTHistory_count
134   NOARGS
135 {
136   return HTList_count (history);
137 }
138 */
139
140 /*              Change last history entry
141 **              -------------------------
142 **
143 **      Sometimes we load a node by one anchor but leave by a different
144 **      one, and it is the one we left from which we want to remember.
145 */
146
147 void HTHistory_leavingFrom
148   ARGS1 (HTAnchor *,anchor)
149 {
150   if (HTList_removeLastObject (history))
151     HTList_addObject (history, anchor);
152   else
153     if (TRACE) fprintf(stderr, "HTHistory_leavingFrom: empty history !\n");
154 }