summaryrefslogtreecommitdiff
path: root/Userland/Applications/Browser/History.cpp
blob: 52d189fd615e8441ca65ec7b39359a4eef78bd6c (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
/*
 * Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#include "History.h"

namespace Browser {

void History::dump() const
{
    dbgln("Dump {} items(s)", m_items.size());
    int i = 0;
    for (auto& item : m_items) {
        dbgln("[{}] {} '{}' {}", i, item.url, item.title, m_current == i ? '*' : ' ');
        ++i;
    }
}

Vector<History::URLTitlePair> History::get_all_history_entries()
{
    return m_items;
}

void History::push(const URL& url, DeprecatedString const& title)
{
    if (!m_items.is_empty() && m_items[m_current].url == url)
        return;
    m_items.shrink(m_current + 1);
    m_items.append(URLTitlePair {
        .url = url,
        .title = title,
    });
    m_current++;
}

void History::replace_current(const URL& url, DeprecatedString const& title)
{
    if (m_current == -1)
        return;

    m_items.remove(m_current);
    m_current--;
    push(url, title);
}

History::URLTitlePair History::current() const
{
    if (m_current == -1)
        return {};
    return m_items[m_current];
}

void History::go_back(int steps)
{
    VERIFY(can_go_back(steps));
    m_current -= steps;
}

void History::go_forward(int steps)
{
    VERIFY(can_go_forward(steps));
    m_current += steps;
}

void History::clear()
{
    m_items = {};
    m_current = -1;
}

void History::update_title(DeprecatedString const& title)
{
    if (m_current == -1)
        return;
    m_items[m_current].title = title;
}

Vector<StringView> const History::get_back_title_history()
{
    Vector<StringView> back_title_history;
    for (int i = m_current - 1; i >= 0; i--) {
        back_title_history.append(m_items[i].title);
    }
    return back_title_history;
}

Vector<StringView> const History::get_forward_title_history()
{
    Vector<StringView> forward_title_history;
    for (int i = m_current + 1; i < static_cast<int>(m_items.size()); i++) {
        forward_title_history.append(m_items[i].title);
    }
    return forward_title_history;
}

}