ladybird/Libraries/LibWebView/SearchEngine.cpp
Timothy Flynn e084a86861 LibWebView+UI: Introduce a persistent settings object
This adds a WebView::Settings class to own persistent browser settings.
In this first pass, it now owns the new tab page URL and search engine
settings.

For simplicitly, we currently use a JSON format for these settings. They
are stored alongside the cookie database. As of this commit, the saved
JSON will have the form:

    {
        "newTabPageURL": "about:blank",
        "searchEngine": {
            "name": "Google"
        }
    }

(The search engine is an object to allow room for a future patch to
implement custom search engine URLs.)

For Qt, this replaces the management of these particular settings in the
Qt settings UI. We will have an internal browser page to control these
settings instead. In the future, we will want to port all settings to
this new class. We will also want to allow UI-specific settings (such as
whether the hamburger menu is displayed in Qt).
2025-03-22 17:27:45 +01:00

75 lines
2.4 KiB
C++

/*
* Copyright (c) 2023, Tim Flynn <trflynn89@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <AK/Find.h>
#include <AK/String.h>
#include <LibWebView/SearchEngine.h>
namespace WebView {
static constexpr auto builtin_search_engines = Array {
SearchEngine { "Bing"sv, "https://www.bing.com/search?q={}"sv },
SearchEngine { "Brave"sv, "https://search.brave.com/search?q={}"sv },
SearchEngine { "DuckDuckGo"sv, "https://duckduckgo.com/?q={}"sv },
SearchEngine { "Ecosia"sv, "https://ecosia.org/search?q={}"sv },
SearchEngine { "Google"sv, "https://www.google.com/search?q={}"sv },
SearchEngine { "Kagi"sv, "https://kagi.com/search?q={}"sv },
SearchEngine { "Mojeek"sv, "https://www.mojeek.com/search?q={}"sv },
SearchEngine { "Startpage"sv, "https://startpage.com/search?q={}"sv },
SearchEngine { "Yahoo"sv, "https://search.yahoo.com/search?p={}"sv },
SearchEngine { "Yandex"sv, "https://yandex.com/search/?text={}"sv },
};
ReadonlySpan<SearchEngine> search_engines()
{
return builtin_search_engines;
}
Optional<SearchEngine const&> find_search_engine_by_name(StringView name)
{
auto it = AK::find_if(builtin_search_engines.begin(), builtin_search_engines.end(),
[&](auto const& engine) {
return engine.name == name;
});
if (it == builtin_search_engines.end())
return {};
return *it;
}
Optional<SearchEngine const&> find_search_engine_by_query_url(StringView query_url)
{
auto it = AK::find_if(builtin_search_engines.begin(), builtin_search_engines.end(),
[&](auto const& engine) {
return engine.query_url == query_url;
});
if (it == builtin_search_engines.end())
return {};
return *it;
}
String format_search_query_for_display(StringView query_url, StringView query)
{
static constexpr auto MAX_SEARCH_STRING_LENGTH = 32;
if (auto search_engine = find_search_engine_by_query_url(query_url); search_engine.has_value()) {
return MUST(String::formatted("Search {} for \"{:.{}}{}\"",
search_engine->name,
query,
MAX_SEARCH_STRING_LENGTH,
query.length() > MAX_SEARCH_STRING_LENGTH ? "..."sv : ""sv));
}
return MUST(String::formatted("Search for \"{:.{}}{}\"",
query,
MAX_SEARCH_STRING_LENGTH,
query.length() > MAX_SEARCH_STRING_LENGTH ? "..."sv : ""sv));
}
}