Serenity Operating System
1/*
2 * Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
3 *
4 * SPDX-License-Identifier: BSD-2-Clause
5 */
6
7#include "History.h"
8
9namespace Browser {
10
11void History::dump() const
12{
13 dbgln("Dump {} items(s)", m_items.size());
14 int i = 0;
15 for (auto& item : m_items) {
16 dbgln("[{}] {} '{}' {}", i, item.url, item.title, m_current == i ? '*' : ' ');
17 ++i;
18 }
19}
20
21Vector<History::URLTitlePair> History::get_all_history_entries()
22{
23 return m_items;
24}
25
26void History::push(const URL& url, DeprecatedString const& title)
27{
28 if (!m_items.is_empty() && m_items[m_current].url == url)
29 return;
30 m_items.shrink(m_current + 1);
31 m_items.append(URLTitlePair {
32 .url = url,
33 .title = title,
34 });
35 m_current++;
36}
37
38void History::replace_current(const URL& url, DeprecatedString const& title)
39{
40 if (m_current == -1)
41 return;
42
43 m_items.remove(m_current);
44 m_current--;
45 push(url, title);
46}
47
48History::URLTitlePair History::current() const
49{
50 if (m_current == -1)
51 return {};
52 return m_items[m_current];
53}
54
55void History::go_back(int steps)
56{
57 VERIFY(can_go_back(steps));
58 m_current -= steps;
59}
60
61void History::go_forward(int steps)
62{
63 VERIFY(can_go_forward(steps));
64 m_current += steps;
65}
66
67void History::clear()
68{
69 m_items = {};
70 m_current = -1;
71}
72
73void History::update_title(DeprecatedString const& title)
74{
75 if (m_current == -1)
76 return;
77 m_items[m_current].title = title;
78}
79
80Vector<StringView> const History::get_back_title_history()
81{
82 Vector<StringView> back_title_history;
83 for (int i = m_current - 1; i >= 0; i--) {
84 back_title_history.append(m_items[i].title);
85 }
86 return back_title_history;
87}
88
89Vector<StringView> const History::get_forward_title_history()
90{
91 Vector<StringView> forward_title_history;
92 for (int i = m_current + 1; i < static_cast<int>(m_items.size()); i++) {
93 forward_title_history.append(m_items[i].title);
94 }
95 return forward_title_history;
96}
97
98}