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]
Home=file:///res/html/misc/welcome.html
SearchEngine=

View file

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

View file

@ -14,6 +14,7 @@
#include "WindowActions.h"
#include <AK/StringBuilder.h>
#include <Applications/Browser/TabGML.h>
#include <LibCore/ConfigFile.h>
#include <LibGUI/Action.h>
#include <LibGUI/Application.h>
#include <LibGUI/BoxLayout.h>
@ -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={}");

View file

@ -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()) {