summaryrefslogtreecommitdiff
path: root/src/feeds.c
blob: 9770ca0a20caf99c0ab95a2aa6cb6d56c2896cd4 (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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
/* 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 "feeds.h"
#include "bookmarks.h"
#include "gmrequest.h"
#include "visited.h"
#include "lang.h"
#include "app.h"

#include <the_Foundation/file.h>
#include <the_Foundation/hash.h>
#include <the_Foundation/intset.h>
#include <the_Foundation/mutex.h>
#include <the_Foundation/path.h>
#include <the_Foundation/queue.h>
#include <the_Foundation/regexp.h>
#include <the_Foundation/stringset.h>
#include <the_Foundation/thread.h>
#include <SDL_timer.h>
#include <ctype.h>

iDeclareType(Feeds)
iDeclareType(FeedJob)

iDefineTypeConstruction(FeedEntry)

void init_FeedEntry(iFeedEntry *d) {
    iZap(d->posted);
    iZap(d->discovered);
    init_String(&d->url);
    init_String(&d->title);
    d->bookmarkId = 0;
    d->isHeading = iFalse;
}

void deinit_FeedEntry(iFeedEntry *d) {
    deinit_String(&d->title);
    deinit_String(&d->url);
}

const iString *url_FeedEntry(const iFeedEntry *d) {
    return urlFragmentStripped_String(&d->url);
}

iBool isUnread_FeedEntry(const iFeedEntry *d) {
    const size_t fragPos = indexOf_String(&d->url, '#');
    if (fragPos != iInvalidPos) {
        /* Check if the entry is newer than the latest visit. */
        const iTime visTime = urlVisitTime_Visited(visited_App(), url_FeedEntry(d));
        return cmp_Time(&visTime, &d->posted) < 0;
    }
    if (!containsUrl_Visited(visited_App(), &d->url)) {
        return iTrue;
    }
    return iFalse;
}

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

static int requestTimeoutSeconds_FeedJob_ = 10.0f;

struct Impl_FeedJob {
    iString     url;
    uint32_t    bookmarkId;
    iTime       startTime;
    iBool       isFirstUpdate; /* hasn't been checked ever before */
    iBool       checkHeadings;
    iGmRequest *request;
    iPtrArray   results;
};

static void init_FeedJob(iFeedJob *d, const iBookmark *bookmark) {
    initCopy_String(&d->url, &bookmark->url);
    d->bookmarkId = id_Bookmark(bookmark);
    d->request = NULL;
    init_PtrArray(&d->results);
    iZap(d->startTime);
    d->isFirstUpdate = iFalse;
    d->checkHeadings = hasTag_Bookmark(bookmark, headings_BookmarkTag);
}

static void deinit_FeedJob(iFeedJob *d) {
    iRelease(d->request);
    iForEach(PtrArray, i, &d->results) {
        delete_FeedEntry(i.ptr);
    }
    deinit_PtrArray(&d->results);
    deinit_String(&d->url);
}

static iBool isTimedOut_FeedJob_(iFeedJob *d) {
    return elapsedSeconds_Time(&d->startTime) > requestTimeoutSeconds_FeedJob_;
}

iDefineTypeConstructionArgs(FeedJob, (const iBookmark *bm), bm)

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

static const char *feedsFilename_Feeds_         = "feeds.txt";
static const int   updateIntervalSeconds_Feeds_ = 4 * 60 * 60;

struct Impl_Feeds {
    iMutex *  mtx;
    iString   saveDir;
    iIntSet   previouslyCheckedFeeds; /* bookmark IDs */
    iTime     lastRefreshedAt;
    int       refreshTimer;
    iThread * worker;
    iBool     stopWorker;
    iPtrArray jobs; /* pending */
    iSortedArray entries; /* pointers to all discovered feed entries, sorted by entry ID (URL) */
};

static iFeeds feeds_;

#define maxConcurrentRequests_Feeds 4

static void submit_FeedJob_(iFeedJob *d) {
    d->request = new_GmRequest(certs_App());
    setUrl_GmRequest(d->request, &d->url);
    initCurrent_Time(&d->startTime);
    submit_GmRequest(d->request);
}

static iBool isSubscribed_(void *context, const iBookmark *bm) {
    iUnused(context);
    static iRegExp *pattern_ = NULL;
    if (!pattern_) {
        pattern_ = new_RegExp("\\bsubscribed\\b", caseSensitive_RegExpOption);
    }
    iRegExpMatch m;
    init_RegExpMatch(&m);
    return matchString_RegExp(pattern_, &bm->tags, &m);
}

static const iPtrArray *listSubscriptions_(void) {
    return list_Bookmarks(bookmarks_App(), NULL, isSubscribed_, NULL);
}

static iFeedJob *startNextJob_Feeds_(iFeeds *d) {
    if (isEmpty_PtrArray(&d->jobs)) {
        return NULL;
    }
    iFeedJob *job;
    take_PtrArray(&d->jobs, 0, (void **) &job);
    submit_FeedJob_(job);
    return job;
}

static iBool isTrimmablePunctuation_(iChar c) {
    if (c == '"') {
        return iFalse; /* Probably quoted text. */
    }
    if (c == '(' || c == '[' || c == '{' || c == '<') {
        return iFalse;
    }
    /* Dashes or punctuation? */
    return c == 0x2013 || c == 0x2014 || (c < 128 && ispunct(c));
}

static void trimTitle_(iString *title) {
    const char *start = constBegin_String(title);
    iConstForEach(String, i, title) {
        start = i.pos;
        if (!isSpace_Char(i.value) && !isTrimmablePunctuation_(i.value)) {
            break;
        }
    }
    remove_Block(&title->chars, 0, start - constBegin_String(title));
}

static void parseResult_FeedJob_(iFeedJob *d) {
    /* TODO: Should tell the user if the request failed. */
    if (isSuccess_GmStatusCode(status_GmRequest(d->request))) {
        iBeginCollect();
        iTime now;
        initCurrent_Time(&now);
        iRegExp *linkPattern =
            new_RegExp("^=>\\s*([^\\s]+)\\s+"
                       "([0-9][0-9][0-9][0-9]-[0-1][0-9]-[0-3][0-9])"
                       "([^0-9].*)",
                       0);
        iString src;
        initBlock_String(&src, body_GmRequest(d->request));
        iRangecc srcLine = iNullRange;
        while (nextSplit_Rangecc(range_String(&src), "\n", &srcLine)) {
            iRangecc line = srcLine;
            trimEnd_Rangecc(&line);
            iRegExpMatch m;
            init_RegExpMatch(&m);
            if (matchRange_RegExp(linkPattern, line, &m)) {
                const iRangecc url   = capturedRange_RegExpMatch(&m, 1);
                const iRangecc date  = capturedRange_RegExpMatch(&m, 2);
                const iRangecc title = capturedRange_RegExpMatch(&m, 3);
                iFeedEntry *   entry = new_FeedEntry();
                entry->discovered = now;
                entry->bookmarkId = d->bookmarkId;
                setRange_String(&entry->url, url);
                set_String(&entry->url, canonicalUrl_String(absoluteUrl_String(url_GmRequest(d->request), &entry->url)));
                setRange_String(&entry->title, title);
                trimTitle_(&entry->title);
                int year, month, day;
                sscanf(date.start, "%04d-%02d-%02d", &year, &month, &day);
                init_Time(
                    &entry->posted,
                    &(iDate){
                        .year = year, .month = month, .day = day, .hour = 12 /* noon UTC */ });
                pushBack_PtrArray(&d->results, entry);
            }
            if (d->checkHeadings) {
                init_RegExpMatch(&m);
                if (startsWith_Rangecc(line, "#")) {
                    while (*line.start == '#' && line.start < line.end) {
                        line.start++;
                    }
                    trimStart_Rangecc(&line);
                    iFeedEntry *entry = new_FeedEntry();
                    entry->isHeading = iTrue;
                    entry->posted = now;
                    if (!d->isFirstUpdate) {
                        entry->discovered = now;
                    }
                    entry->bookmarkId = d->bookmarkId;
                    iString *title = newRange_String(line);
                    set_String(&entry->title, title);
                    set_String(&entry->url, &d->url);
                    appendChar_String(&entry->url, '#');
                    append_String(&entry->url, collect_String(urlEncode_String(title)));
                    set_String(&entry->url, canonicalUrl_String(&entry->url));
                    delete_String(title);
                    pushBack_PtrArray(&d->results, entry);
                }
            }
        }
        deinit_String(&src);
        iRelease(linkPattern);
        iEndCollect();
    }
}

static void save_Feeds_(iFeeds *d) {
    iFile *f = new_File(collect_String(concatCStr_Path(&d->saveDir, feedsFilename_Feeds_)));
    if (open_File(f, write_FileMode | text_FileMode)) {
        lock_Mutex(d->mtx);
        iString *str = new_String();
        format_String(str, "%llu\n# Feeds\n", integralSeconds_Time(&d->lastRefreshedAt));
        write_File(f, utf8_String(str));
        /* Index of feeds for IDs. */ {
            iConstForEach(PtrArray, i, listSubscriptions_()) {
                const iBookmark *bm = i.ptr;
                format_String(str, "%08x %s\n", id_Bookmark(bm), cstr_String(&bm->url));
                write_File(f, utf8_String(str));
            }
        }
        writeData_File(f, "# Entries\n", 10);
        iTime now;
        initCurrent_Time(&now);
        iConstForEach(Array, i, &d->entries.values) {
            const iFeedEntry *entry = *(const iFeedEntry **) i.value;
            /* Heading entries are kept as long as they are present in the source. */
            if (!entry->isHeading && isValid_Time(&entry->discovered) &&
                secondsSince_Time(&now, &entry->discovered) > maxAge_Visited) {
                continue; /* Forget entries discovered long ago. */
            }
            format_String(str, "%x\n%llu\n%llu\n%s\n%s\n",
                          entry->bookmarkId,
                          integralSeconds_Time(&entry->posted),
                          integralSeconds_Time(&entry->discovered),
                          cstr_String(&entry->url),
                          cstr_String(&entry->title));
            write_File(f, utf8_String(str));
        }
        delete_String(str);
        close_File(f);
        unlock_Mutex(d->mtx);
    }
    iRelease(f);
}

static iBool isHeadingEntry_FeedEntry_(const iFeedEntry *d) {
    return contains_String(&d->url, '#');
}

static iStringSet *listHeadingEntriesFrom_Feeds_(const iFeeds *d, uint32_t sourceId) {
    iStringSet *set = new_StringSet();
    iConstForEach(Array, i, &d->entries.values) {
        const iFeedEntry *entry = *(const iFeedEntry **) i.value;
        if (entry->bookmarkId == sourceId) {
            insert_StringSet(set, &entry->url);
        }
    }
    return set;
}

static iBool updateEntries_Feeds_(iFeeds *d, iBool isHeadings, uint32_t sourceId,
                                  iPtrArray *incoming) {
    /* Entries are removed from `incoming` if they are added to the Feeds entries array.
       Anything remaining in `incoming` will be deleted afterwards. */
    iBool gotNew = iFalse;
    lock_Mutex(d->mtx);
    iTime now;
    initCurrent_Time(&now);
    if (isHeadings) {
//        printf("Updating sourceID %d...\n", sourceId);
        iStringSet *known = listHeadingEntriesFrom_Feeds_(d, sourceId);
//        puts("  Known URLs:");
//        iConstForEach(StringSet, ss, known) {
//            printf("   {%s}\n", cstr_String(ss.value));
//        }
        iStringSet *presentInSource = new_StringSet();
        /* Look for unknown entries. */
        iForEach(PtrArray, i, incoming) {
            iFeedEntry *entry = i.ptr;
            insert_StringSet(presentInSource, &entry->url);
            if (!contains_StringSet(known, &entry->url)) {
//                printf("  {%s} is new\n", cstr_String(&entry->url));
                insert_SortedArray(&d->entries, &entry);
                gotNew = iTrue;
                remove_PtrArrayIterator(&i);
            }
        }
//        puts("  URLs present in source:");
//        iConstForEach(StringSet, ps, presentInSource) {
//            printf("    {%s}\n", cstr_String(ps.value));
//        }
//        puts("  URLs to purge:");
        /* All known entries that are no longer present in source must be deleted. */
        iForEach(Array, e, &d->entries.values) {
            iFeedEntry *entry = *(iFeedEntry **) e.value;
            if (entry->bookmarkId == sourceId &&
                !contains_StringSet(presentInSource, &entry->url)) {
//                printf("    {%s}\n", cstr_String(&entry->url));
                delete_FeedEntry(entry);
                remove_ArrayIterator(&e);
            }
        }
//        puts("Done.");
        iRelease(presentInSource);
        iRelease(known);
    }
    else {
        iForEach(PtrArray, i, incoming) {
            iFeedEntry *entry = i.ptr;
            /* Disregard old incoming entries. */
            if (secondsSince_Time(&now, &entry->posted) >= maxAge_Visited) {
                /* We don't remember this far back, so the unread status of the entry would
                   be incorrect. */
                continue;
            }
            size_t pos;
            if (locate_SortedArray(&d->entries, &entry, &pos)) {
                iFeedEntry *existing = *(iFeedEntry **) at_SortedArray(&d->entries, pos);
                iAssert(!isHeadingEntry_FeedEntry_(existing));
                iAssert(!isHeadingEntry_FeedEntry_(entry));
                /* Already known, but update it, maybe the time and label have changed. */
                iBool changed = iFalse;
                iDate newDate;
                iDate oldDate;
                init_Date(&newDate, &entry->posted);
                init_Date(&oldDate, &existing->posted);
                if (!equalCase_String(&existing->title, &entry->title) ||
                    (newDate.year != oldDate.year || newDate.month != oldDate.month ||
                     newDate.day != oldDate.day)) {
                    changed = iTrue;
                }
                set_String(&existing->title, &entry->title);
                existing->posted = entry->posted;
                delete_FeedEntry(entry);
                if (changed) {
                    /* TODO: better to use a new flag for read feed entries? */
                    removeUrl_Visited(visited_App(), &existing->url);
                    gotNew = iTrue;
                }
            }
            else {
                insert_SortedArray(&d->entries, &entry);
                gotNew = iTrue;
            }
            remove_PtrArrayIterator(&i);
        }
    }
    unlock_Mutex(d->mtx);
    return gotNew;
}

static iThreadResult fetch_Feeds_(iThread *thread) {
    iFeeds *d = &feeds_;
    iUnused(thread);
    iFeedJob *work[maxConcurrentRequests_Feeds]; /* We'll do a couple of concurrent requests. */
    iZap(work);
    iBool gotNew = iFalse;
    postCommand_App("feeds.update.started");
    while (!d->stopWorker) {
        /* Start new jobs. */
        iForIndices(i, work) {
            if (!work[i]) {
                work[i] = startNextJob_Feeds_(d);
            }
        }
        sleep_Thread(0.5); /* TODO: wait on a Condition so we can exit quickly */
        if (d->stopWorker) break;
        size_t ongoing = 0;
        iForIndices(i, work) {
            if (work[i]) {
                if (isFinished_GmRequest(work[i]->request)) {
                    /* TODO: Handle redirects. Need to resubmit the job with new URL. */
                    parseResult_FeedJob_(work[i]);
                    gotNew |= updateEntries_Feeds_(
                        d, work[i]->checkHeadings, work[i]->bookmarkId, &work[i]->results);
                    delete_FeedJob(work[i]);
                    work[i] = NULL;
                }
                else if (isTimedOut_FeedJob_(work[i])) {
                    /* Maybe we'll get it next time! */
                    delete_FeedJob(work[i]);
                    work[i] = NULL;
                }
                else {
                    ongoing++;
                }
                /* TODO: abort job if it takes too long (> 15 seconds?) */
            }
        }
        /* Stop if everything has finished. */
        if (ongoing == 0 && isEmpty_PtrArray(&d->jobs)) {
            break;
        }
    }
    initCurrent_Time(&d->lastRefreshedAt);
    save_Feeds_(d);
    postCommandf_App("feeds.update.finished arg:%d unread:%zu", gotNew ? 1 : 0,
                     numUnread_Feeds());
    return 0;
}

static iBool startWorker_Feeds_(iFeeds *d) {
    if (d->worker) {
        return iFalse; /* Refresh is already ongoing. */
    }
    /* Queue up all the subscriptions for the worker. */
    iConstForEach(PtrArray, i, listSubscriptions_()) {
        const iBookmark *bm = i.ptr;
        iFeedJob *job = new_FeedJob(bm);
        if (!contains_IntSet(&d->previouslyCheckedFeeds, id_Bookmark(bm))) {
            job->isFirstUpdate = iTrue;
//            printf("first check of %x: %s\n", id_Bookmark(bm), cstr_String(&bm->title));
//            fflush(stdout);
            insert_IntSet(&d->previouslyCheckedFeeds, id_Bookmark(bm));
        }
        pushBack_PtrArray(&d->jobs, job);
    }
    if (!isEmpty_Array(&d->jobs)) {
        d->worker = new_Thread(fetch_Feeds_);
        d->stopWorker = iFalse;
        start_Thread(d->worker);
        return iTrue;
    }
    return iFalse;
}

static uint32_t refresh_Feeds_(uint32_t interval, void *data) {
    /* Called in the SDL timer thread, so let's start a worker thread for running the update. */
    startWorker_Feeds_(&feeds_);
    return 1000 * updateIntervalSeconds_Feeds_;
}

static void stopWorker_Feeds_(iFeeds *d) {
    if (d->worker) {
        d->stopWorker = iTrue;
        join_Thread(d->worker);
        iReleasePtr(&d->worker);
    }
    /* Clear remaining jobs. */
    iForEach(PtrArray, i, &d->jobs) {
        delete_FeedJob(i.ptr);
    }
    clear_PtrArray(&d->jobs);
}

static int cmp_FeedEntryPtr_(const void *a, const void *b) {
    const iFeedEntry * const *elem[2] = { a, b };
    const int cmp = cmpString_String(&(*elem[0])->url, &(*elem[1])->url);
    if (cmp == 0) {
        /* The same URL can be coming from different feeds. */
        return iCmp((*elem[0])->bookmarkId, (*elem[1])->bookmarkId);
    }
    return cmp;
}

iDeclareType(FeedHashNode)

struct Impl_FeedHashNode {
    iHashNode node;
    uint32_t  bookmarkId;
};

static void load_Feeds_(iFeeds *d) {
    /* TODO: If there are lots of entries, it would make sense to load async. */
    iFile *f = new_File(collect_String(concatCStr_Path(&d->saveDir, feedsFilename_Feeds_)));
    if (open_File(f, read_FileMode | text_FileMode)) {
        iBlock * src     = readAll_File(f);
        iRangecc line    = iNullRange;
        int      section = 0;
        iHash *  feeds   = new_Hash(); /* mapping from IDs to feed URLs */
        while (nextSplit_Rangecc(range_Block(src), "\n", &line)) {
            if (equal_Rangecc(line, "# Feeds")) {
                section = 1;
                continue;
            }
            else if (equal_Rangecc(line, "# Entries")) {
                section = 2;
                continue;
            }
            switch (section) {
                case 0: {
                    unsigned long long ts = 0;
                    sscanf(line.start, "%llu", &ts);
                    d->lastRefreshedAt.ts.tv_sec = ts;
                    break;
                }
                case 1: {
                    if (size_Range(&line) > 8) {
                        uint32_t id = 0;
                        sscanf(line.start, "%08x", &id);
                        iString *feedUrl =
                            collect_String(newRange_String((iRangecc){ line.start + 9, line.end }));
                        const uint32_t bookmarkId = findUrl_Bookmarks(bookmarks_App(), feedUrl);
                        if (bookmarkId) {
                            iFeedHashNode *node = iMalloc(FeedHashNode);
                            node->node.key      = id;
                            node->bookmarkId    = bookmarkId;
                            insert_Hash(feeds, &node->node);
                            insert_IntSet(&d->previouslyCheckedFeeds, bookmarkId);
                        }
                    }
                    break;
                }
                case 2: {
                    /* TODO: Cleanup needed...
                       All right, this could maybe use a bit more robust, structured format.
                       The code below is messy. */
                    const uint32_t feedId = strtoul(line.start, NULL, 16);
                    if (!nextSplit_Rangecc(range_Block(src), "\n", &line)) {
                        goto aborted;
                    }
                    const unsigned long long posted = strtoull(line.start, NULL, 10);
                    if (posted == 0) {
                        goto aborted;
                    }
                    if (!nextSplit_Rangecc(range_Block(src), "\n", &line)) {
                        goto aborted;
                    }
                    char *endp = NULL;
                    const unsigned long long discovered = strtoull(line.start, &endp, 10);
                    if (endp != line.end) {
                        goto aborted;
                    }
                    if (!nextSplit_Rangecc(range_Block(src), "\n", &line)) {
                        goto aborted;
                    }
                    const iRangecc urlRange = line;
                    if (!nextSplit_Rangecc(range_Block(src), "\n", &line)) {
                        goto aborted;
                    }
                    const iRangecc titleRange = line;
                    iString *url   = newRange_String(urlRange);
                    iString *title = newRange_String(titleRange);
                    /* Look it up in the hash. */
                    const iFeedHashNode *node = (iFeedHashNode *) value_Hash(feeds, feedId);
                    if (node) {
                        iFeedEntry *entry = new_FeedEntry();
                        entry->bookmarkId           = node->bookmarkId;
                        entry->posted.ts.tv_sec     = posted;
                        entry->discovered.ts.tv_sec = discovered;
                        set_String(&entry->url, url);
                        stripDefaultUrlPort_String(&entry->url);
                        set_String(&entry->url, canonicalUrl_String(&entry->url));
                        set_String(&entry->title, title);
                        entry->isHeading = isHeadingEntry_FeedEntry_(entry);
                        if (entry->isHeading) {
                            printf("[Feeds] src:%d url:{%s}\n", entry->bookmarkId,
                                   cstr_String(&entry->url));
                        }
                        insert_SortedArray(&d->entries, &entry);
                    }
                    delete_String(title);
                    delete_String(url);
                    break;
                }
            }
        }
    aborted:
        /* Cleanup. */
        delete_Block(src);
        iForEach(Hash, i, feeds) {
            free(i.value);
        }
        delete_Hash(feeds);
    }
    iRelease(f);
}

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

void init_Feeds(const char *saveDir) {
    iFeeds *d = &feeds_;
    d->mtx = new_Mutex();
    initCStr_String(&d->saveDir, saveDir);
    init_IntSet(&d->previouslyCheckedFeeds);
    iZap(d->lastRefreshedAt);
    d->worker = NULL;
    init_PtrArray(&d->jobs);
    init_SortedArray(&d->entries, sizeof(iFeedEntry *), cmp_FeedEntryPtr_);
    load_Feeds_(d);
    /* Update feeds if it has been a while. */
    int intervalSec = updateIntervalSeconds_Feeds_;
    if (isValid_Time(&d->lastRefreshedAt)) {
        const double elapsed = elapsedSeconds_Time(&d->lastRefreshedAt);
        intervalSec = iMax(1, updateIntervalSeconds_Feeds_ - elapsed);
    }
    d->refreshTimer = SDL_AddTimer(1000 * intervalSec, refresh_Feeds_, NULL);
}

void deinit_Feeds(void) {
    iFeeds *d = &feeds_;
    SDL_RemoveTimer(d->refreshTimer);
    stopWorker_Feeds_(d);
    iAssert(isEmpty_PtrArray(&d->jobs));
    deinit_PtrArray(&d->jobs);
    deinit_String(&d->saveDir);
    delete_Mutex(d->mtx);
    iForEach(Array, i, &d->entries.values) {
        iFeedEntry **entry = i.value;
        delete_FeedEntry(*entry);
    }
    deinit_IntSet(&d->previouslyCheckedFeeds);
    deinit_SortedArray(&d->entries);
}

void refresh_Feeds(void) {
    startWorker_Feeds_(&feeds_);
}

void refreshFinished_Feeds(void) {
    stopWorker_Feeds_(&feeds_);
}

void removeEntries_Feeds(uint32_t feedBookmarkId) {
    iFeeds *d = &feeds_;
    iForEach(Array, i, &d->entries.values) {
        iFeedEntry **entry = i.value;
        if ((*entry)->bookmarkId == feedBookmarkId) {
            delete_FeedEntry(*entry);
            remove_ArrayIterator(&i);
        }
    }
}

static int cmpTimeDescending_FeedEntryPtr_(const void *a, const void *b) {
    const iFeedEntry * const *e1 = a, * const *e2 = b;
    const int cmpPosted = -cmp_Time(&(*e1)->posted, &(*e2)->posted);
    if (cmpPosted) return cmpPosted;
    /* Posting timestamps may only be accurate to a day, so also sort by discovery time. */
    return -cmp_Time(&(*e1)->discovered, &(*e2)->discovered);
}

const iPtrArray *listEntries_Feeds(void) {
    iFeeds *d = &feeds_;
    lock_Mutex(d->mtx);
    /* The worker will never delete feed entries so we can use the same ones. Just make a copy
       of the array in case the worker modifies it. */
    iPtrArray *list = collect_PtrArray(copy_Array(&d->entries.values));
    unlock_Mutex(d->mtx);
    sort_Array(list, cmpTimeDescending_FeedEntryPtr_);
    return list;
}

size_t numSubscribed_Feeds(void) {
    return size_PtrArray(listSubscriptions_());
}

size_t numUnread_Feeds(void) {
    size_t count = 0;
    size_t max = 100; /* match the number of items shown in the sidebar */
    iConstForEach(PtrArray, i, listEntries_Feeds()) {
        if (!max--) break;
        const iFeedEntry *entry = i.ptr;
        if (isValid_Time(&entry->discovered) && isUnread_FeedEntry(i.ptr)) {
            count++;
        }
    }
    return count;
}

const iString *entryListPage_Feeds(void) {
    iFeeds *d = &feeds_;
    iString *src = collectNew_String();
    setCStr_String(src, translateCStr_Lang("# ${feeds.list.title}\n\n"));
    lock_Mutex(d->mtx);
    const iPtrArray *subs = listSubscriptions_();
    const int elapsed = elapsedSeconds_Time(&d->lastRefreshedAt) / 60;
    appendFormat_String(
        src,
        formatCStrs_Lang("feeds.list.counts.n", size_PtrArray(subs)),
        formatCStrs_Lang("feeds.list.entrycount.n", size_SortedArray(&d->entries)));
    if (isValid_Time(&d->lastRefreshedAt)) {
        if (elapsed == 0) {
            appendCStr_String(src, translateCStr_Lang("\n${feeds.list.refreshtime.now}\n"));
        }
        else {
            appendFormat_String(src,
                                translateCStr_Lang("\n${feeds.list.refreshtime}\n"),
                                elapsed < 60     ? formatCStr_Lang("minutes.ago.n", elapsed)
                                : elapsed < 1440 ? formatCStr_Lang("hours.ago.n", elapsed / 60)
                                                 : formatCStr_Lang("days.ago.n", elapsed / 1440));
        }
    }
    iDate on;
    iZap(on);
    iConstForEach(PtrArray, i, listEntries_Feeds()) {
        const iFeedEntry *entry = i.ptr;
        if (isHidden_FeedEntry(entry)) {
            continue; /* A hidden entry. */
        }
        iDate entryDate;
        init_Date(&entryDate, &entry->posted);
        if (on.year != entryDate.year || on.month != entryDate.month || on.day != entryDate.day) {
            appendFormat_String(
                src, "## %s\n", cstrCollect_String(format_Date(&entryDate, "%Y-%m-%d")));
            on = entryDate;
        }
        const iBookmark *bm = get_Bookmarks(bookmarks_App(), entry->bookmarkId);
        if (bm) {
            appendFormat_String(src,
                                "=> %s %s - %s\n",
                                cstr_String(&entry->url),
                                cstr_String(&bm->title),
                                cstr_String(&entry->title));
        }
    }
    unlock_Mutex(d->mtx);
    return src;
}