From e8d921820a6d08f6bf324db528bc8da0c7e86151 Mon Sep 17 00:00:00 2001 From: Timothy Flynn Date: Thu, 19 Oct 2023 15:52:06 -0400 Subject: [PATCH] Browser+BrowserSettings: Migrate to LibWebView for search engines --- Base/home/anon/.config/SearchEngines.json | 38 ------------ .../Applications/Browser/BrowserWindow.cpp | 51 +++++---------- Userland/Applications/Browser/main.cpp | 3 +- .../BrowserSettings/BrowserSettingsWidget.cpp | 62 ++++++++++++++----- .../BrowserSettings/CMakeLists.txt | 2 +- .../Applications/BrowserSettings/Defaults.h | 1 - 6 files changed, 65 insertions(+), 92 deletions(-) delete mode 100644 Base/home/anon/.config/SearchEngines.json diff --git a/Base/home/anon/.config/SearchEngines.json b/Base/home/anon/.config/SearchEngines.json deleted file mode 100644 index 748cfd95c8..0000000000 --- a/Base/home/anon/.config/SearchEngines.json +++ /dev/null @@ -1,38 +0,0 @@ -[ - { - "title": "Bing", - "url_format": "https://www.bing.com/search?q={}" - }, - { - "title": "Brave", - "url_format": "https://search.brave.com/search?q={}" - }, - { - "title": "DuckDuckGo", - "url_format": "https://duckduckgo.com/?q={}" - }, - { - "title": "FrogFind", - "url_format": "https://frogfind.com/?q={}" - }, - { - "title": "GitHub", - "url_format": "https://github.com/search?q={}" - }, - { - "title": "Google", - "url_format": "https://google.com/search?q={}" - }, - { - "title": "Mojeek", - "url_format": "https://www.mojeek.com/search?q={}" - }, - { - "title": "Wiby", - "url_format": "https://wiby.me/?q={}" - }, - { - "title": "Yandex", - "url_format": "https://yandex.com/search/?text={}" - } -] diff --git a/Userland/Applications/Browser/BrowserWindow.cpp b/Userland/Applications/Browser/BrowserWindow.cpp index a9d0fa7520..db2cfbe493 100644 --- a/Userland/Applications/Browser/BrowserWindow.cpp +++ b/Userland/Applications/Browser/BrowserWindow.cpp @@ -36,6 +36,7 @@ #include #include #include +#include #include #include @@ -49,14 +50,6 @@ static DeprecatedString bookmarks_file_path() return builder.to_deprecated_string(); } -static DeprecatedString search_engines_file_path() -{ - StringBuilder builder; - builder.append(Core::StandardPaths::config_directory()); - builder.append("/SearchEngines.json"sv); - return builder.to_deprecated_string(); -} - BrowserWindow::BrowserWindow(WebView::CookieJar& cookie_jar, Vector const& initial_urls) : m_cookie_jar(cookie_jar) , m_window_actions(*this) @@ -481,36 +474,22 @@ ErrorOr BrowserWindow::load_search_engines(GUI::Menu& settings_menu) m_search_engine_actions.add_action(*m_disable_search_engine_action); m_disable_search_engine_action->set_checked(true); - auto search_engines_file = TRY(Core::File::open(Browser::search_engines_file_path(), Core::File::OpenMode::Read)); - auto file_size = TRY(search_engines_file->size()); - auto buffer = TRY(ByteBuffer::create_uninitialized(file_size)); - if (!search_engines_file->read_until_filled(buffer).is_error()) { - StringView buffer_contents { buffer.bytes() }; - if (auto json = TRY(JsonValue::from_string(buffer_contents)); json.is_array()) { - auto json_array = json.as_array(); - for (auto& json_item : json_array.values()) { - if (!json_item.is_object()) - continue; - auto search_engine = json_item.as_object(); - auto name = search_engine.get_deprecated_string("title"sv).value(); - auto url_format = search_engine.get_deprecated_string("url_format"sv).value(); + for (auto [name, url_format] : WebView::search_engines()) { + auto action = GUI::Action::create_checkable( + name, [&, url_format](auto&) { + g_search_engine = url_format; + Config::write_string("Browser"sv, "Preferences"sv, "SearchEngine"sv, g_search_engine); + }, + this); + search_engine_menu->add_action(action); + m_search_engine_actions.add_action(action); - auto action = GUI::Action::create_checkable( - name, [&, url_format](auto&) { - g_search_engine = url_format; - Config::write_string("Browser"sv, "Preferences"sv, "SearchEngine"sv, g_search_engine); - }, - this); - search_engine_menu->add_action(action); - m_search_engine_actions.add_action(action); - - if (g_search_engine == url_format) { - action->set_checked(true); - search_engine_set = true; - } - action->set_status_tip(TRY(String::from_deprecated_string(url_format))); - } + if (g_search_engine == url_format) { + action->set_checked(true); + search_engine_set = true; } + + action->set_status_tip(TRY(String::from_utf8(url_format))); } auto custom_search_engine_action = GUI::Action::create_checkable("Custom...", [&](auto& action) { diff --git a/Userland/Applications/Browser/main.cpp b/Userland/Applications/Browser/main.cpp index b164d1e069..a42cc1563e 100644 --- a/Userland/Applications/Browser/main.cpp +++ b/Userland/Applications/Browser/main.cpp @@ -27,6 +27,7 @@ #include #include #include +#include #include #include @@ -134,7 +135,7 @@ ErrorOr serenity_main(Main::Arguments arguments) Browser::g_home_url = Config::read_string("Browser"sv, "Preferences"sv, "Home"sv, Browser::default_homepage_url); Browser::g_new_tab_url = Config::read_string("Browser"sv, "Preferences"sv, "NewTab"sv, Browser::default_new_tab_url); - Browser::g_search_engine = Config::read_string("Browser"sv, "Preferences"sv, "SearchEngine"sv, Browser::default_search_engine); + Browser::g_search_engine = Config::read_string("Browser"sv, "Preferences"sv, "SearchEngine"sv, WebView::default_search_engine().query_url); Browser::g_content_filters_enabled = Config::read_bool("Browser"sv, "Preferences"sv, "EnableContentFilters"sv, Browser::default_enable_content_filters); Browser::g_autoplay_allowed_on_all_websites = Config::read_bool("Browser"sv, "Preferences"sv, "AllowAutoplayOnAllWebsites"sv, Browser::default_allow_autoplay_on_all_websites); diff --git a/Userland/Applications/BrowserSettings/BrowserSettingsWidget.cpp b/Userland/Applications/BrowserSettings/BrowserSettingsWidget.cpp index fbd7173b83..4af770026d 100644 --- a/Userland/Applications/BrowserSettings/BrowserSettingsWidget.cpp +++ b/Userland/Applications/BrowserSettings/BrowserSettingsWidget.cpp @@ -9,10 +9,10 @@ #include #include #include -#include #include #include #include +#include struct ColorScheme { DeprecatedString title; @@ -20,7 +20,6 @@ struct ColorScheme { }; class ColorSchemeModel final : public GUI::Model { - public: ColorSchemeModel() { @@ -52,6 +51,49 @@ private: Vector m_color_schemes; }; +class SearchEngineModel final : public GUI::Model { + enum class SearchEngineColumn : int { + Name, + QueryURL, + }; + +public: + SearchEngineModel() + { + m_search_engines.ensure_capacity(WebView::search_engines().size() + 1); + + for (auto const& engine : WebView::search_engines()) + m_search_engines.append(engine); + + m_search_engines.empend("Custom..."sv, StringView {}); + } + + virtual int row_count(GUI::ModelIndex const& = GUI::ModelIndex()) const override { return m_search_engines.size(); } + virtual int column_count(GUI::ModelIndex const& = GUI::ModelIndex()) const override { return 2; } + + virtual GUI::Variant data(GUI::ModelIndex const& index, GUI::ModelRole role) const override + { + if (role == GUI::ModelRole::TextAlignment) + return Gfx::TextAlignment::CenterLeft; + + if (role == GUI::ModelRole::Display) { + switch (static_cast(index.row())) { + case SearchEngineColumn::Name: + return m_search_engines[index.row()].name; + case SearchEngineColumn::QueryURL: + return m_search_engines[index.row()].query_url; + } + + VERIFY_NOT_REACHED(); + } + + return {}; + } + +private: + Vector m_search_engines; +}; + ErrorOr> BrowserSettingsWidget::create() { auto widget = TRY(try_make_ref_counted()); @@ -96,17 +138,7 @@ ErrorOr BrowserSettingsWidget::setup() set_modified(true); }; - Vector search_engine_fields; - search_engine_fields.empend("title", "Title"_string, Gfx::TextAlignment::CenterLeft); - search_engine_fields.empend("url_format", "Url format"_string, Gfx::TextAlignment::CenterLeft); - auto search_engines_model = GUI::JsonArrayModel::create(DeprecatedString::formatted("{}/SearchEngines.json", Core::StandardPaths::config_directory()), move(search_engine_fields)); - search_engines_model->invalidate(); - Vector custom_search_engine; - custom_search_engine.append("Custom..."); - custom_search_engine.append(""); - TRY(search_engines_model->add(move(custom_search_engine))); - - m_search_engine_combobox->set_model(move(search_engines_model)); + m_search_engine_combobox->set_model(adopt_ref(*new SearchEngineModel())); m_search_engine_combobox->set_only_allow_values_from_model(true); m_search_engine_combobox->on_change = [this](AK::DeprecatedString const&, GUI::ModelIndex const& cursor_index) { auto url_format = m_search_engine_combobox->model()->index(cursor_index.row(), 1).data().to_deprecated_string(); @@ -114,7 +146,7 @@ ErrorOr BrowserSettingsWidget::setup() m_custom_search_engine_group->set_enabled(m_is_custom_search_engine); set_modified(true); }; - set_search_engine_url(Config::read_string("Browser"sv, "Preferences"sv, "SearchEngine"sv, Browser::default_search_engine)); + set_search_engine_url(Config::read_string("Browser"sv, "Preferences"sv, "SearchEngine"sv, WebView::default_search_engine().query_url)); m_auto_close_download_windows_checkbox = find_descendant_of_type_named("auto_close_download_windows_checkbox"); m_auto_close_download_windows_checkbox->set_checked(Config::read_bool("Browser"sv, "Preferences"sv, "CloseDownloadWidgetOnFinish"sv, Browser::default_close_download_widget_on_finish), GUI::AllowCallback::No); @@ -217,5 +249,5 @@ void BrowserSettingsWidget::reset_default_values() m_show_bookmarks_bar_checkbox->set_checked(Browser::default_show_bookmarks_bar); set_color_scheme(Browser::default_color_scheme); m_auto_close_download_windows_checkbox->set_checked(Browser::default_close_download_widget_on_finish); - set_search_engine_url(Browser::default_search_engine); + set_search_engine_url(WebView::default_search_engine().query_url); } diff --git a/Userland/Applications/BrowserSettings/CMakeLists.txt b/Userland/Applications/BrowserSettings/CMakeLists.txt index bcfee8aa69..3eee5d93ab 100644 --- a/Userland/Applications/BrowserSettings/CMakeLists.txt +++ b/Userland/Applications/BrowserSettings/CMakeLists.txt @@ -22,4 +22,4 @@ set(GENERATED_SOURCES ) serenity_app(BrowserSettings ICON app-browser) -target_link_libraries(BrowserSettings PRIVATE LibCore LibGfx LibGUI LibConfig LibMain) +target_link_libraries(BrowserSettings PRIVATE LibCore LibGfx LibGUI LibConfig LibMain LibWebView) diff --git a/Userland/Applications/BrowserSettings/Defaults.h b/Userland/Applications/BrowserSettings/Defaults.h index 8e3046ea9c..2b2fb038f1 100644 --- a/Userland/Applications/BrowserSettings/Defaults.h +++ b/Userland/Applications/BrowserSettings/Defaults.h @@ -12,7 +12,6 @@ namespace Browser { static constexpr StringView default_homepage_url = "file:///res/html/misc/welcome.html"sv; static constexpr StringView default_new_tab_url = "file:///res/html/misc/new-tab.html"sv; -static constexpr StringView default_search_engine = "https://www.google.com/search?q={}"sv; static constexpr StringView default_color_scheme = "auto"sv; static constexpr bool default_enable_content_filters = true; static constexpr bool default_show_bookmarks_bar = true;