Bladeren bron

Browser+BrowserSettings: Migrate to LibWebView for search engines

Timothy Flynn 1 jaar geleden
bovenliggende
commit
e8d921820a

+ 0 - 38
Base/home/anon/.config/SearchEngines.json

@@ -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={}"
-    }
-]

+ 16 - 37
Userland/Applications/Browser/BrowserWindow.cpp

@@ -36,6 +36,7 @@
 #include <LibWeb/Loader/ResourceLoader.h>
 #include <LibWebView/CookieJar.h>
 #include <LibWebView/OutOfProcessWebView.h>
+#include <LibWebView/SearchEngine.h>
 #include <LibWebView/UserAgent.h>
 #include <LibWebView/WebContentClient.h>
 
@@ -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<URL> const& initial_urls)
     : m_cookie_jar(cookie_jar)
     , m_window_actions(*this)
@@ -481,36 +474,22 @@ ErrorOr<void> 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();
-
-                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)));
-            }
+    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);
+
+        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) {

+ 2 - 1
Userland/Applications/Browser/main.cpp

@@ -27,6 +27,7 @@
 #include <LibWebView/Database.h>
 #include <LibWebView/OutOfProcessWebView.h>
 #include <LibWebView/RequestServerAdapter.h>
+#include <LibWebView/SearchEngine.h>
 #include <LibWebView/URL.h>
 #include <unistd.h>
 
@@ -134,7 +135,7 @@ ErrorOr<int> 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);
 

+ 47 - 15
Userland/Applications/BrowserSettings/BrowserSettingsWidget.cpp

@@ -9,10 +9,10 @@
 #include <Applications/BrowserSettings/BrowserSettingsWidgetGML.h>
 #include <Applications/BrowserSettings/Defaults.h>
 #include <LibConfig/Client.h>
-#include <LibGUI/JsonArrayModel.h>
 #include <LibGUI/Label.h>
 #include <LibGUI/MessageBox.h>
 #include <LibGUI/Model.h>
+#include <LibWebView/SearchEngine.h>
 
 struct ColorScheme {
     DeprecatedString title;
@@ -20,7 +20,6 @@ struct ColorScheme {
 };
 
 class ColorSchemeModel final : public GUI::Model {
-
 public:
     ColorSchemeModel()
     {
@@ -52,6 +51,49 @@ private:
     Vector<ColorScheme> 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<SearchEngineColumn>(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<WebView::SearchEngine> m_search_engines;
+};
+
 ErrorOr<NonnullRefPtr<BrowserSettingsWidget>> BrowserSettingsWidget::create()
 {
     auto widget = TRY(try_make_ref_counted<BrowserSettingsWidget>());
@@ -96,17 +138,7 @@ ErrorOr<void> BrowserSettingsWidget::setup()
         set_modified(true);
     };
 
-    Vector<GUI::JsonArrayModel::FieldSpec> 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<JsonValue> 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<void> 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<GUI::CheckBox>("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);
 }

+ 1 - 1
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)

+ 0 - 1
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;