1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 05:07:34 +00:00

Browser: Save search engine setting to preferences

This commit is contained in:
Maciej Zygmanowski 2021-04-29 08:53:12 +02:00 committed by Andreas Kling
parent c3cf739b94
commit 2de0ff28dd
4 changed files with 17 additions and 8 deletions

View file

@ -1,2 +1,3 @@
[Preferences] [Preferences]
Home=file:///res/html/misc/welcome.html Home=file:///res/html/misc/welcome.html
SearchEngine=

View file

@ -11,5 +11,6 @@
namespace Browser { namespace Browser {
extern String g_home_url; extern String g_home_url;
extern String g_search_engine;
} }

View file

@ -14,6 +14,7 @@
#include "WindowActions.h" #include "WindowActions.h"
#include <AK/StringBuilder.h> #include <AK/StringBuilder.h>
#include <Applications/Browser/TabGML.h> #include <Applications/Browser/TabGML.h>
#include <LibCore/ConfigFile.h>
#include <LibGUI/Action.h> #include <LibGUI/Action.h>
#include <LibGUI/Application.h> #include <LibGUI/Application.h>
#include <LibGUI/BoxLayout.h> #include <LibGUI/BoxLayout.h>
@ -39,12 +40,12 @@
namespace Browser { namespace Browser {
static String s_search_engine_format = {}; String g_search_engine = {};
URL url_from_user_input(const String& input) URL url_from_user_input(const String& input)
{ {
if (input.starts_with("?") && !s_search_engine_format.is_null()) { if (input.starts_with("?") && !g_search_engine.is_null()) {
return URL(String::formatted(s_search_engine_format, urlencode(input.substring(1)))); return URL(String::formatted(g_search_engine, urlencode(input.substring(1))));
} }
auto url = URL(input); auto url = URL(input);
@ -412,18 +413,23 @@ Tab::Tab(Type type)
auto add_search_engine = [&](auto& name, auto& url_format) { auto add_search_engine = [&](auto& name, auto& url_format) {
auto action = GUI::Action::create_checkable( auto action = GUI::Action::create_checkable(
name, [&](auto&) { name, [&](auto&) {
dbgln("Setting search engine to {}", url_format); g_search_engine = url_format;
s_search_engine_format = url_format; auto m_config = Core::ConfigFile::get_for_app("Browser");
m_config->write_entry("Preferences", "SearchEngine", g_search_engine);
}, },
this); this);
search_engine_menu.add_action(action); search_engine_menu.add_action(action);
m_search_engine_actions.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( auto disable_search_engine_action = GUI::Action::create_checkable(
"Disable", [this](auto&) { "Disable", [this](auto&) {
dbgln("Disabling search engine"); g_search_engine = {};
s_search_engine_format = {}; auto m_config = Core::ConfigFile::get_for_app("Browser");
m_config->write_entry("Preferences", "SearchEngine", g_search_engine);
}, },
this); this);
search_engine_menu.add_action(disable_search_engine_action); search_engine_menu.add_action(disable_search_engine_action);
@ -433,7 +439,7 @@ Tab::Tab(Type type)
// FIXME: Support adding custom search engines // FIXME: Support adding custom search engines
add_search_engine("Bing", "https://www.bing.com/search?q={}"); add_search_engine("Bing", "https://www.bing.com/search?q={}");
add_search_engine("DuckDuckGo", "https://duckduckgo.com/?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("Google", "https://google.com/search?q={}");
add_search_engine("Yandex", "https://yandex.com/search/?text={}"); add_search_engine("Yandex", "https://yandex.com/search/?text={}");

View file

@ -116,6 +116,7 @@ int main(int argc, char** argv)
auto m_config = Core::ConfigFile::get_for_app("Browser"); auto m_config = Core::ConfigFile::get_for_app("Browser");
Browser::g_home_url = m_config->read_entry("Preferences", "Home", "about:blank"); 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); 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()) { if (!ad_filter_list_or_error.is_error()) {