summaryrefslogtreecommitdiff
path: root/src/visited.c
blob: 5d8a56766f3ae4e026e9c339cbaa71f8934b7989 (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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
/* Copyright 2020 Jaakko Keränen <jaakko.keranen@iki.fi>

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

1. Redistributions of source code must retain the above copyright notice, this
   list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
   this list of conditions and the following disclaimer in the documentation
   and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */

#include "visited.h"
#include "app.h"

#include <the_Foundation/file.h>
#include <the_Foundation/mutex.h>
#include <the_Foundation/path.h>
#include <the_Foundation/ptrarray.h>
#include <the_Foundation/sortedarray.h>

static const size_t maxAgeVisited_Visited_ = 2 * 3600 * 24 * 30; /* two months */

void init_VisitedUrl(iVisitedUrl *d) {
    initCurrent_Time(&d->when);
    init_String(&d->url);
    d->flags = 0;
}

void deinit_VisitedUrl(iVisitedUrl *d) {
    deinit_String(&d->url);
}

static int cmpUrl_VisitedUrl_(const void *a, const void *b) {
    return cmpString_String(&((const iVisitedUrl *) a)->url, &((const iVisitedUrl *) b)->url);
}

static int cmpNewer_VisitedUrl_(const void *insert, const void *existing) {
    return seconds_Time(&((const iVisitedUrl *) insert  )->when) >
           seconds_Time(&((const iVisitedUrl *) existing)->when);
}

/*----------------------------------------------------------------------------------------------*/

struct Impl_Visited {
    iMutex *mtx;
    iSortedArray visited;
};

iDefineTypeConstruction(Visited)

void init_Visited(iVisited *d) {
    d->mtx = new_Mutex();
    init_SortedArray(&d->visited, sizeof(iVisitedUrl), cmpUrl_VisitedUrl_);
}

void deinit_Visited(iVisited *d) {
    iGuardMutex(d->mtx, {
        clear_Visited(d);
        deinit_SortedArray(&d->visited);
    });
    delete_Mutex(d->mtx);
}

void save_Visited(const iVisited *d, const char *dirPath) {
    iString *line = new_String();
    iFile *f = newCStr_File(concatPath_CStr(dirPath, "visited.txt"));
    if (open_File(f, writeOnly_FileMode | text_FileMode)) {
        lock_Mutex(d->mtx);
        iConstForEach(Array, i, &d->visited.values) {
            const iVisitedUrl *item = i.value;
            iDate date;
            init_Date(&date, &item->when);
            format_String(line,
                          "%04d-%02d-%02dT%02d:%02d:%02d %04x %s\n",
                          date.year,
                          date.month,
                          date.day,
                          date.hour,
                          date.minute,
                          date.second,
                          item->flags,
                          cstr_String(&item->url));
            writeData_File(f, cstr_String(line), size_String(line));
        }
        unlock_Mutex(d->mtx);
    }
    iRelease(f);
    delete_String(line);
}

void load_Visited(iVisited *d, const char *dirPath) {
    iFile *f = newCStr_File(concatPath_CStr(dirPath, "visited.txt"));
    if (open_File(f, readOnly_FileMode | text_FileMode)) {
        lock_Mutex(d->mtx);
        const iRangecc src  = range_Block(collect_Block(readAll_File(f)));
        iRangecc       line = iNullRange;
        iTime          now;
        initCurrent_Time(&now);
        while (nextSplit_Rangecc(src, "\n", &line)) {
            if (size_Range(&line) < 22) continue;
            int y, m, D, H, M, S;
            sscanf(line.start, "%04d-%02d-%02dT%02d:%02d:%02d ", &y, &m, &D, &H, &M, &S);
            if (!y) break;
            iVisitedUrl item;
            const char *urlStart = line.start + 20;
            if (*urlStart == '0' && size_Range(&line) >= 25) {
                item.flags = strtoul(line.start + 20, NULL, 16);
                urlStart += 5;
            }
            init_VisitedUrl(&item);
            init_Time(
                &item.when,
                &(iDate){ .year = y, .month = m, .day = D, .hour = H, .minute = M, .second = S });
            if (secondsSince_Time(&now, &item.when) > maxAgeVisited_Visited_) {
                continue; /* Too old. */
            }
            initRange_String(&item.url, (iRangecc){ urlStart, line.end });
            insert_SortedArray(&d->visited, &item);
        }
        unlock_Mutex(d->mtx);
    }
    iRelease(f);
}

void clear_Visited(iVisited *d) {
    lock_Mutex(d->mtx);
    iForEach(Array, v, &d->visited.values) {
        deinit_VisitedUrl(v.value);
    }
    clear_SortedArray(&d->visited);
    unlock_Mutex(d->mtx);
}

static size_t find_Visited_(const iVisited *d, const iString *url) {
    iVisitedUrl visit;
    init_VisitedUrl(&visit);
    set_String(&visit.url, url);
    size_t pos = iInvalidPos;
    iGuardMutex(d->mtx, {
        locate_SortedArray(&d->visited, &visit, &pos);
        deinit_VisitedUrl(&visit);
    });
    return pos;
}

void visitUrl_Visited(iVisited *d, const iString *url, uint16_t visitFlags) {
    if (isEmpty_String(url)) return;
    iVisitedUrl visit;
    init_VisitedUrl(&visit);
    visit.flags = visitFlags;
    set_String(&visit.url, url);
    size_t pos;
    lock_Mutex(d->mtx);
    if (locate_SortedArray(&d->visited, &visit, &pos)) {
        iVisitedUrl *old = at_SortedArray(&d->visited, pos);
        if (cmpNewer_VisitedUrl_(&visit, old)) {
            old->when = visit.when;
            old->flags = visitFlags;
            unlock_Mutex(d->mtx);
            deinit_VisitedUrl(&visit);
            return;
        }
    }
    insert_SortedArray(&d->visited, &visit);
    unlock_Mutex(d->mtx);
}

void removeUrl_Visited(iVisited *d, const iString *url) {
    iGuardMutex(d->mtx, {
        size_t pos = find_Visited_(d, url);
        if (pos != iInvalidPos) {
            deinit_VisitedUrl(at_SortedArray(&d->visited, pos));
            remove_Array(&d->visited.values, pos);
        }
    });
}

iTime urlVisitTime_Visited(const iVisited *d, const iString *url) {
    iVisitedUrl item;
    size_t pos;
    iZap(item);
    initCopy_String(&item.url, url);
    lock_Mutex(d->mtx);
    if (locate_SortedArray(&d->visited, &item, &pos)) {
        item.when = ((const iVisitedUrl *) constAt_SortedArray(&d->visited, pos))->when;
    }
    unlock_Mutex(d->mtx);
    deinit_String(&item.url);
    return item.when;
}

iBool containsUrl_Visited(const iVisited *d, const iString *url) {
    const iTime time = urlVisitTime_Visited(d, url);
    return isValid_Time(&time);
}

static int cmpWhenDescending_VisitedUrlPtr_(const void *a, const void *b) {
    const iVisitedUrl *s = *(const void **) a, *t = *(const void **) b;
    return -cmp_Time(&s->when, &t->when);
}

const iArray *list_Visited(const iVisited *d, size_t count) {
    iPtrArray *urls = collectNew_PtrArray();
    iGuardMutex(d->mtx, {
        iConstForEach(Array, i, &d->visited.values) {
            const iVisitedUrl *vis = i.value;
            if (~vis->flags & transient_VisitedUrlFlag) {
                pushBack_PtrArray(urls, vis);
            }
        }
    });
    sort_Array(urls, cmpWhenDescending_VisitedUrlPtr_);
    if (count > 0 && size_Array(urls) > count) {
        resize_Array(urls, count);
    }
    return urls;
}