aboutsummaryrefslogtreecommitdiff
path: root/src/history.c
blob: ad08611b77b2645849f4c06f29bdb9095b418175 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/*
 * File: history.c
 *
 * Copyright (C) 2001-2007 Jorge Arellano Cid <jcid@dillo.org>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 */

/*
 * Linear history (it also provides indexes for the navigation stack)
 */

#include "list.h"
#include "history.h"


typedef struct {
   DilloUrl *url;
   char *title;
} H_Item;


/* Global history list */
static H_Item *history = NULL;
static int history_size = 0;        /* [1 based] */
static int history_size_max = 16;


/*
 * Add a new H_Item at the end of the history list
 * (taking care of not making a duplicate entry)
 */
int a_History_add_url(DilloUrl *url)
{
   int i, idx;

   for (i = 0; i < history_size; ++i)
      if (!a_Url_cmp(history[i].url, url) &&
          !strcmp(URL_FRAGMENT(history[i].url), URL_FRAGMENT(url)))
         return i;

   idx = history_size;
   a_List_add(history, history_size, history_size_max);
   history[idx].url = a_Url_dup(url);
   history[idx].title = NULL;
   ++history_size;
   return idx;
}

/*
 * Set the page-title for a given URL (by idx)
 * (this is known when the first chunks of HTML data arrive)
 */
int a_History_set_title(int idx, const char *title)
{
   dReturn_val_if_fail(idx >= 0 && idx < history_size, 0);

   dFree(history[idx].title);
   history[idx].title = dStrdup(title);
   return 1;
}

/*
 * Return the DilloUrl field (by index)
 */
DilloUrl *a_History_get_url(int idx)
{
   dReturn_val_if_fail(idx >= 0 && idx < history_size, NULL);

   return history[idx].url;
}

/*
 * Return the title field (by index)
 * ('force' returns URL_STR when there's no title)
 */
const char *a_History_get_title(int idx, int force)
{
   dReturn_val_if_fail(idx >= 0 && idx < history_size, NULL);

   if (history[idx].title)
      return history[idx].title;
   else if (force)
      return URL_STR(history[idx].url);
   else
      return NULL;
}

/*
 * Return the title field (by url)
 * ('force' returns URL_STR when there's no title)
 */
const char *a_History_get_title_by_url(const DilloUrl *url, int force)
{
   int i;

   dReturn_val_if_fail(url != NULL, NULL);

   for (i = 0; i < history_size; ++i)
      if (a_Url_cmp(url, history[i].url) == 0)
         break;

   if (i < history_size && history[i].title)
      return history[i].title;
   else if (force)
      return URL_STR_(url);
   return NULL;
}


/*
 * Free all the memory used by this module
 */
void a_History_free()
{
   int i;

   for (i = 0; i < history_size; ++i) {
      a_Url_free(history[i].url);
      dFree(history[i].title);
   }
   dFree(history);
}