forked from SerenityOS/serenity
-
Notifications
You must be signed in to change notification settings - Fork 0
/
StorageWidget.cpp
138 lines (111 loc) Β· 5.52 KB
/
StorageWidget.cpp
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
/*
* Copyright (c) 2022, the SerenityOS developers.
* Copyright (c) 2022, Sam Atkins <[email protected]>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include "StorageWidget.h"
#include "CookiesModel.h"
#include "StorageModel.h"
#include <Applications/Browser/StorageWidgetGML.h>
#include <LibGUI/Menu.h>
#include <LibGUI/TabWidget.h>
#include <LibGUI/TableView.h>
#include <LibWeb/Cookie/Cookie.h>
namespace Browser {
StorageWidget::StorageWidget()
{
load_from_gml(storage_widget_gml).release_value_but_fixme_should_propagate_errors();
auto& tab_widget = *find_descendant_of_type_named<GUI::TabWidget>("tab_widget");
m_cookies_table_view = tab_widget.find_descendant_of_type_named<GUI::TableView>("cookies_tableview");
m_cookies_textbox = tab_widget.find_descendant_of_type_named<GUI::TextBox>("cookies_filter_textbox");
m_cookies_model = adopt_ref(*new CookiesModel());
m_cookies_filtering_model = MUST(GUI::FilteringProxyModel::create(*m_cookies_model));
m_cookies_filtering_model->set_filter_term(""sv);
m_cookies_textbox->on_change = [this] {
m_cookies_filtering_model->set_filter_term(m_cookies_textbox->text());
if (m_cookies_filtering_model->row_count() != 0)
m_cookies_table_view->set_cursor(m_cookies_filtering_model->index(0, 0), GUI::AbstractView::SelectionUpdate::Set);
};
m_cookies_table_view->set_model(m_cookies_filtering_model);
m_cookies_table_view->set_column_headers_visible(true);
m_cookies_table_view->set_alternating_row_colors(true);
auto delete_cookie_action = GUI::Action::create(
"&Delete Cookie", { Key_Delete }, Gfx::Bitmap::load_from_file("/res/icons/16x16/delete.png"sv).release_value_but_fixme_should_propagate_errors(), [&](auto const&) {
auto cookie_index = m_cookies_table_view->selection().first();
delete_cookie(m_cookies_model->take_cookie(cookie_index));
},
m_cookies_table_view);
auto delete_all_cookies_action = GUI::Action::create(
"Delete &All Cookies", [&](auto const&) {
auto cookies = m_cookies_model->take_all_cookies();
for (auto& cookie : cookies)
delete_cookie(move(cookie));
},
m_cookies_table_view);
m_cookies_context_menu = GUI::Menu::construct();
m_cookies_context_menu->add_action(delete_cookie_action);
m_cookies_context_menu->add_action(delete_all_cookies_action);
m_cookies_table_view->on_context_menu_request = [&](auto& index, auto& event) {
if (index.is_valid())
m_cookies_context_menu->popup(event.screen_position());
};
m_local_storage_table_view = tab_widget.find_descendant_of_type_named<GUI::TableView>("local_storage_tableview");
m_local_storage_textbox = tab_widget.find_descendant_of_type_named<GUI::TextBox>("local_storage_filter_textbox");
m_local_storage_model = adopt_ref(*new StorageModel());
m_local_storage_filtering_model = MUST(GUI::FilteringProxyModel::create(*m_local_storage_model));
m_local_storage_filtering_model->set_filter_term(""sv);
m_local_storage_textbox->on_change = [this] {
m_local_storage_filtering_model->set_filter_term(m_local_storage_textbox->text());
if (m_local_storage_filtering_model->row_count() != 0)
m_local_storage_table_view->set_cursor(m_local_storage_filtering_model->index(0, 0), GUI::AbstractView::SelectionUpdate::Set);
};
m_local_storage_table_view->set_model(m_local_storage_filtering_model);
m_local_storage_table_view->set_column_headers_visible(true);
m_local_storage_table_view->set_alternating_row_colors(true);
m_session_storage_table_view = tab_widget.find_descendant_of_type_named<GUI::TableView>("session_storage_tableview");
m_session_storage_textbox = tab_widget.find_descendant_of_type_named<GUI::TextBox>("session_storage_filter_textbox");
m_session_storage_model = adopt_ref(*new StorageModel());
m_session_storage_filtering_model = MUST(GUI::FilteringProxyModel::create(*m_session_storage_model));
m_session_storage_filtering_model->set_filter_term(""sv);
m_session_storage_textbox->on_change = [this] {
m_session_storage_filtering_model->set_filter_term(m_session_storage_textbox->text());
if (m_session_storage_filtering_model->row_count() != 0)
m_session_storage_table_view->set_cursor(m_session_storage_filtering_model->index(0, 0), GUI::AbstractView::SelectionUpdate::Set);
};
m_session_storage_table_view->set_model(m_session_storage_filtering_model);
m_session_storage_table_view->set_column_headers_visible(true);
m_session_storage_table_view->set_alternating_row_colors(true);
}
void StorageWidget::set_cookies_entries(Vector<Web::Cookie::Cookie> entries)
{
m_cookies_model->set_items(entries);
}
void StorageWidget::clear_cookies()
{
m_cookies_model->clear_items();
}
void StorageWidget::set_local_storage_entries(OrderedHashMap<String, String> entries)
{
m_local_storage_model->set_items(move(entries));
}
void StorageWidget::clear_local_storage_entries()
{
m_local_storage_model->clear_items();
}
void StorageWidget::set_session_storage_entries(OrderedHashMap<String, String> entries)
{
m_session_storage_model->set_items(move(entries));
}
void StorageWidget::clear_session_storage_entries()
{
m_session_storage_model->clear_items();
}
void StorageWidget::delete_cookie(Web::Cookie::Cookie cookie)
{
// Delete cookie by making its expiry time in the past.
cookie.expiry_time = UnixDateTime::earliest();
if (on_update_cookie)
on_update_cookie(move(cookie));
}
}