1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-06-12 16:02:07 +00:00

BrowserSettings: Port content filters to String

This commit is contained in:
Timothy Flynn 2023-04-21 08:03:50 -04:00 committed by Andreas Kling
parent 5089766af6
commit d1ad513cb1
3 changed files with 45 additions and 36 deletions

View file

@ -55,7 +55,7 @@ ErrorOr<NonnullRefPtr<AutoplaySettingsWidget>> AutoplaySettingsWidget::create()
String text; String text;
if (GUI::InputBox::show(widget->window(), text, "Website:"sv, "Add website to autoplay allowlist"sv, GUI::InputType::NonemptyText) == GUI::Dialog::ExecResult::OK) { if (GUI::InputBox::show(widget->window(), text, "Website:"sv, "Add website to autoplay allowlist"sv, GUI::InputType::NonemptyText) == GUI::Dialog::ExecResult::OK) {
widget->m_allowlist_model->add_domain(text.to_deprecated_string()); widget->m_allowlist_model->add_domain(move(text));
widget->set_modified(true); widget->set_modified(true);
} }
}; };

View file

@ -31,10 +31,16 @@ ErrorOr<void> DomainListModel::load()
auto file = TRY(Core::File::open(TRY(filter_list_file_path()), Core::File::OpenMode::Read)); auto file = TRY(Core::File::open(TRY(filter_list_file_path()), Core::File::OpenMode::Read));
auto content_filter_list = TRY(Core::BufferedFile::create(move(file))); auto content_filter_list = TRY(Core::BufferedFile::create(move(file)));
auto buffer = TRY(ByteBuffer::create_uninitialized(4096)); auto buffer = TRY(ByteBuffer::create_uninitialized(4096));
m_domain_list.clear_with_capacity();
while (TRY(content_filter_list->can_read_line())) { while (TRY(content_filter_list->can_read_line())) {
auto line = TRY(content_filter_list->read_line(buffer)); auto line = TRY(content_filter_list->read_line(buffer));
if (!line.is_empty()) if (line.is_empty())
m_domain_list.append(line); continue;
auto pattern = TRY(String::from_utf8(line));
TRY(m_domain_list.try_append(move(pattern)));
} }
return {}; return {};
@ -55,7 +61,7 @@ ErrorOr<void> DomainListModel::save()
return {}; return {};
} }
void DomainListModel::add_domain(DeprecatedString name) void DomainListModel::add_domain(String name)
{ {
begin_insert_rows({}, m_domain_list.size(), m_domain_list.size()); begin_insert_rows({}, m_domain_list.size(), m_domain_list.size());
m_domain_list.append(move(name)); m_domain_list.append(move(name));
@ -76,32 +82,37 @@ void DomainListModel::delete_domain(size_t index)
void DomainListModel::reset_default_values() void DomainListModel::reset_default_values()
{ {
// FIXME: This probably should not be hardcoded. // FIXME: This probably should not be hardcoded.
m_domain_list = { static constexpr Array default_domain_list {
"207.net", "207.net"sv,
"247realmedia.com", "247realmedia.com"sv,
"2o7.net", "2o7.net"sv,
"adbrite.com", "adbrite.com"sv,
"admob.com", "admob.com"sv,
"adthis.com", "adthis.com"sv,
"advertising.com", "advertising.com"sv,
"aquantive.com", "aquantive.com"sv,
"atwola.com", "atwola.com"sv,
"channelintelligence.com", "channelintelligence.com"sv,
"doubleclick.com", "doubleclick.com"sv,
"doubleclick.net", "doubleclick.net"sv,
"esomniture.com", "esomniture.com"sv,
"google-analytics.com", "google-analytics.com"sv,
"googleadservices.com", "googleadservices.com"sv,
"googlesyndication.com", "googlesyndication.com"sv,
"gravity.com", "gravity.com"sv,
"hitbox.com", "hitbox.com"sv,
"intellitxt.com", "intellitxt.com"sv,
"nielsen-online.com", "nielsen-online.com"sv,
"omniture.com", "omniture.com"sv,
"quantcast.com", "quantcast.com"sv,
"quantserve.com", "quantserve.com"sv,
"scorecardresearch.com", "scorecardresearch.com"sv,
}; };
m_domain_list.clear_with_capacity();
for (auto domain : default_domain_list)
m_domain_list.append(String::from_utf8(domain).release_value_but_fixme_should_propagate_errors());
m_was_modified = true; m_was_modified = true;
did_update(UpdateFlag::InvalidateAllIndices); did_update(UpdateFlag::InvalidateAllIndices);
} }
@ -120,14 +131,13 @@ ContentFilterSettingsWidget::ContentFilterSettingsWidget()
String text; String text;
if (GUI::InputBox::show(window(), text, "Enter domain name"sv, "Add domain to Content Filter"sv, GUI::InputType::NonemptyText) == GUI::Dialog::ExecResult::OK) { if (GUI::InputBox::show(window(), text, "Enter domain name"sv, "Add domain to Content Filter"sv, GUI::InputType::NonemptyText) == GUI::Dialog::ExecResult::OK) {
m_domain_list_model->add_domain(move(text).to_deprecated_string()); m_domain_list_model->add_domain(move(text));
set_modified(true); set_modified(true);
} }
}; };
m_domain_list_model = make_ref_counted<DomainListModel>(); m_domain_list_model = make_ref_counted<DomainListModel>();
// FIXME: Propagate errors m_domain_list_model->load().release_value_but_fixme_should_propagate_errors();
MUST(m_domain_list_model->load());
m_domain_list_view->set_model(m_domain_list_model); m_domain_list_view->set_model(m_domain_list_model);
auto delete_action = GUI::CommonActions::make_delete_action([&](GUI::Action const&) { auto delete_action = GUI::CommonActions::make_delete_action([&](GUI::Action const&) {
@ -147,8 +157,7 @@ ContentFilterSettingsWidget::ContentFilterSettingsWidget()
void ContentFilterSettingsWidget::apply_settings() void ContentFilterSettingsWidget::apply_settings()
{ {
// FIXME: Propagate errors m_domain_list_model->save().release_value_but_fixme_should_propagate_errors();
MUST(m_domain_list_model->save());
Config::write_bool("Browser"sv, "Preferences"sv, "EnableContentFilters"sv, m_enable_content_filtering_checkbox->is_checked()); Config::write_bool("Browser"sv, "Preferences"sv, "EnableContentFilters"sv, m_enable_content_filtering_checkbox->is_checked());
} }

View file

@ -22,12 +22,12 @@ public:
virtual int column_count(GUI::ModelIndex const& = GUI::ModelIndex()) const override { return 1; } virtual int column_count(GUI::ModelIndex const& = GUI::ModelIndex()) const override { return 1; }
virtual GUI::Variant data(GUI::ModelIndex const& index, GUI::ModelRole = GUI::ModelRole::Display) const override { return m_domain_list[index.row()]; } virtual GUI::Variant data(GUI::ModelIndex const& index, GUI::ModelRole = GUI::ModelRole::Display) const override { return m_domain_list[index.row()]; }
void add_domain(DeprecatedString name); void add_domain(String name);
void delete_domain(size_t index); void delete_domain(size_t index);
protected: protected:
bool m_was_modified { false }; bool m_was_modified { false };
Vector<DeprecatedString> m_domain_list; Vector<String> m_domain_list;
}; };
class ContentFilterSettingsWidget : public GUI::SettingsWindow::Tab { class ContentFilterSettingsWidget : public GUI::SettingsWindow::Tab {