From 2de0ff28dd1ef0851aab377547dad96bd9bf04e0 Mon Sep 17 00:00:00 2001 From: Maciej Zygmanowski Date: Thu, 29 Apr 2021 08:53:12 +0200 Subject: [PATCH] Browser: Save search engine setting to preferences --- Base/home/anon/.config/Browser.ini | 1 + Userland/Applications/Browser/Browser.h | 1 + Userland/Applications/Browser/Tab.cpp | 22 ++++++++++++++-------- Userland/Applications/Browser/main.cpp | 1 + 4 files changed, 17 insertions(+), 8 deletions(-) diff --git a/Base/home/anon/.config/Browser.ini b/Base/home/anon/.config/Browser.ini index f3211ec1d6..ebf0bc207e 100644 --- a/Base/home/anon/.config/Browser.ini +++ b/Base/home/anon/.config/Browser.ini @@ -1,2 +1,3 @@ [Preferences] Home=file:///res/html/misc/welcome.html +SearchEngine= diff --git a/Userland/Applications/Browser/Browser.h b/Userland/Applications/Browser/Browser.h index 7b254f291d..b3152d2881 100644 --- a/Userland/Applications/Browser/Browser.h +++ b/Userland/Applications/Browser/Browser.h @@ -11,5 +11,6 @@ namespace Browser { extern String g_home_url; +extern String g_search_engine; } diff --git a/Userland/Applications/Browser/Tab.cpp b/Userland/Applications/Browser/Tab.cpp index c638ce1a86..7b80ed86ed 100644 --- a/Userland/Applications/Browser/Tab.cpp +++ b/Userland/Applications/Browser/Tab.cpp @@ -14,6 +14,7 @@ #include "WindowActions.h" #include #include +#include #include #include #include @@ -39,12 +40,12 @@ namespace Browser { -static String s_search_engine_format = {}; +String g_search_engine = {}; URL url_from_user_input(const String& input) { - if (input.starts_with("?") && !s_search_engine_format.is_null()) { - return URL(String::formatted(s_search_engine_format, urlencode(input.substring(1)))); + if (input.starts_with("?") && !g_search_engine.is_null()) { + return URL(String::formatted(g_search_engine, urlencode(input.substring(1)))); } auto url = URL(input); @@ -412,18 +413,23 @@ Tab::Tab(Type type) auto add_search_engine = [&](auto& name, auto& url_format) { auto action = GUI::Action::create_checkable( name, [&](auto&) { - dbgln("Setting search engine to {}", url_format); - s_search_engine_format = url_format; + g_search_engine = url_format; + auto m_config = Core::ConfigFile::get_for_app("Browser"); + m_config->write_entry("Preferences", "SearchEngine", 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); + } }; auto disable_search_engine_action = GUI::Action::create_checkable( "Disable", [this](auto&) { - dbgln("Disabling search engine"); - s_search_engine_format = {}; + g_search_engine = {}; + auto m_config = Core::ConfigFile::get_for_app("Browser"); + m_config->write_entry("Preferences", "SearchEngine", g_search_engine); }, this); search_engine_menu.add_action(disable_search_engine_action); @@ -433,7 +439,7 @@ Tab::Tab(Type type) // FIXME: Support adding custom search engines add_search_engine("Bing", "https://www.bing.com/search?q={}"); add_search_engine("DuckDuckGo", "https://duckduckgo.com/?q={}"); - add_search_engine("GitHub", "https://github.com/search=?q={}"); + add_search_engine("GitHub", "https://github.com/search?q={}"); add_search_engine("Google", "https://google.com/search?q={}"); add_search_engine("Yandex", "https://yandex.com/search/?text={}"); diff --git a/Userland/Applications/Browser/main.cpp b/Userland/Applications/Browser/main.cpp index 0c53f64d9c..82ade93158 100644 --- a/Userland/Applications/Browser/main.cpp +++ b/Userland/Applications/Browser/main.cpp @@ -116,6 +116,7 @@ int main(int argc, char** argv) auto m_config = Core::ConfigFile::get_for_app("Browser"); Browser::g_home_url = m_config->read_entry("Preferences", "Home", "about:blank"); + Browser::g_search_engine = m_config->read_entry("Preferences", "SearchEngine", {}); auto ad_filter_list_or_error = Core::File::open(String::formatted("{}/BrowserContentFilters.txt", Core::StandardPaths::config_directory()), Core::IODevice::ReadOnly); if (!ad_filter_list_or_error.is_error()) {