mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-09-11 05:25:32 +03:00
Ladybird/Qt: Migrate to LibWebView for search engines
This commit is contained in:
parent
e8d921820a
commit
f8f0c5459d
Notes:
sideshowbarker
2024-07-17 00:27:16 +09:00
Author: https://github.com/trflynn89 Commit: https://github.com/SerenityOS/serenity/commit/f8f0c5459d Pull-request: https://github.com/SerenityOS/serenity/pull/21513 Reviewed-by: https://github.com/AtkinsSJ ✅
@ -30,15 +30,13 @@ LocationEdit::LocationEdit(QWidget* parent)
|
||||
connect(this, &QLineEdit::returnPressed, [&] {
|
||||
clearFocus();
|
||||
|
||||
Optional<String> search_engine_url;
|
||||
if (Settings::the()->enable_search()) {
|
||||
auto search_engine = Settings::the()->search_engine();
|
||||
search_engine_url = MUST(ak_string_from_qstring(search_engine.url));
|
||||
}
|
||||
Optional<StringView> search_engine_url;
|
||||
if (Settings::the()->enable_search())
|
||||
search_engine_url = Settings::the()->search_engine().query_url;
|
||||
|
||||
auto query = MUST(ak_string_from_qstring(text()));
|
||||
|
||||
if (auto url = WebView::sanitize_url(query, search_engine_url.map([](auto& value) { return value.bytes_as_string_view(); })); url.has_value())
|
||||
if (auto url = WebView::sanitize_url(query, search_engine_url); url.has_value())
|
||||
setText(qstring_from_ak_deprecated_string(url->serialize()));
|
||||
});
|
||||
|
||||
|
@ -30,8 +30,20 @@ static QString rebase_default_url_on_serenity_resource_root(StringView default_u
|
||||
}
|
||||
|
||||
Settings::Settings()
|
||||
: m_search_engine(WebView::default_search_engine())
|
||||
{
|
||||
m_qsettings = make<QSettings>("Serenity", "Ladybird", this);
|
||||
|
||||
auto default_search_engine = WebView::default_search_engine();
|
||||
auto default_search_engine_name = qstring_from_ak_deprecated_string(default_search_engine.name);
|
||||
|
||||
auto search_engine_name = m_qsettings->value("search_engine_name", default_search_engine_name).toString();
|
||||
auto search_engine = WebView::find_search_engine(MUST(ak_string_from_qstring(search_engine_name)));
|
||||
|
||||
if (search_engine.has_value())
|
||||
m_search_engine = search_engine.release_value();
|
||||
else
|
||||
set_search_engine(move(default_search_engine));
|
||||
}
|
||||
|
||||
Optional<QPoint> Settings::last_position()
|
||||
@ -66,18 +78,10 @@ void Settings::set_is_maximized(bool is_maximized)
|
||||
m_qsettings->setValue("is_maximized", is_maximized);
|
||||
}
|
||||
|
||||
Settings::EngineProvider Settings::search_engine()
|
||||
void Settings::set_search_engine(WebView::SearchEngine search_engine)
|
||||
{
|
||||
EngineProvider engine_provider;
|
||||
engine_provider.name = m_qsettings->value("search_engine_name", "Google").toString();
|
||||
engine_provider.url = m_qsettings->value("search_engine", "https://www.google.com/search?q={}").toString();
|
||||
return engine_provider;
|
||||
}
|
||||
|
||||
void Settings::set_search_engine(EngineProvider const& engine_provider)
|
||||
{
|
||||
m_qsettings->setValue("search_engine_name", engine_provider.name);
|
||||
m_qsettings->setValue("search_engine", engine_provider.url);
|
||||
m_qsettings->setValue("search_engine_name", qstring_from_ak_deprecated_string(search_engine.name));
|
||||
m_search_engine = move(search_engine);
|
||||
}
|
||||
|
||||
Settings::EngineProvider Settings::autocomplete_engine()
|
||||
|
@ -9,6 +9,7 @@
|
||||
|
||||
#include <AK/DeprecatedString.h>
|
||||
#include <AK/OwnPtr.h>
|
||||
#include <LibWebView/SearchEngine.h>
|
||||
#include <QPoint>
|
||||
#include <QSettings>
|
||||
#include <QSize>
|
||||
@ -38,14 +39,14 @@ public:
|
||||
QString new_tab_page();
|
||||
void set_new_tab_page(QString const& page);
|
||||
|
||||
WebView::SearchEngine search_engine() const { return m_search_engine; }
|
||||
void set_search_engine(WebView::SearchEngine engine);
|
||||
|
||||
struct EngineProvider {
|
||||
QString name;
|
||||
QString url;
|
||||
};
|
||||
|
||||
EngineProvider search_engine();
|
||||
void set_search_engine(EngineProvider const& engine);
|
||||
|
||||
EngineProvider autocomplete_engine();
|
||||
void set_autocomplete_engine(EngineProvider const& engine);
|
||||
|
||||
@ -60,6 +61,7 @@ protected:
|
||||
|
||||
private:
|
||||
OwnPtr<QSettings> m_qsettings;
|
||||
WebView::SearchEngine m_search_engine;
|
||||
};
|
||||
|
||||
}
|
||||
|
@ -9,6 +9,7 @@
|
||||
#include "Settings.h"
|
||||
#include "StringUtils.h"
|
||||
#include <AK/URL.h>
|
||||
#include <LibWebView/SearchEngine.h>
|
||||
#include <QLabel>
|
||||
#include <QMenu>
|
||||
|
||||
@ -23,7 +24,7 @@ SettingsDialog::SettingsDialog(QMainWindow* window)
|
||||
m_enable_search->setChecked(Settings::the()->enable_search());
|
||||
|
||||
m_search_engine_dropdown = make<QToolButton>(this);
|
||||
m_search_engine_dropdown->setText(Settings::the()->search_engine().name);
|
||||
m_search_engine_dropdown->setText(qstring_from_ak_deprecated_string(Settings::the()->search_engine().name));
|
||||
|
||||
m_enable_autocomplete = make<QCheckBox>(this);
|
||||
m_enable_autocomplete->setChecked(Settings::the()->enable_autocomplete());
|
||||
@ -64,18 +65,7 @@ SettingsDialog::SettingsDialog(QMainWindow* window)
|
||||
|
||||
void SettingsDialog::setup_search_engines()
|
||||
{
|
||||
// FIXME: These should be in a config file.
|
||||
Vector<Settings::EngineProvider> search_engines = {
|
||||
{ "Bing", "https://www.bing.com/search?q={}" },
|
||||
{ "Brave", "https://search.brave.com/search?q={}" },
|
||||
{ "DuckDuckGo", "https://duckduckgo.com/?q={}" },
|
||||
{ "GitHub", "https://github.com/search?q={}" },
|
||||
{ "Google", "https://google.com/search?q={}" },
|
||||
{ "Mojeek", "https://www.mojeek.com/search?q={}" },
|
||||
{ "Yahoo", "https://search.yahoo.com/search?p={}" },
|
||||
{ "Yandex", "https://yandex.com/search/?text={}" },
|
||||
};
|
||||
|
||||
// FIXME: These should be centralized in LibWebView.
|
||||
Vector<Settings::EngineProvider> autocomplete_engines = {
|
||||
{ "DuckDuckGo", "https://duckduckgo.com/ac/?q={}" },
|
||||
{ "Google", "https://www.google.com/complete/search?client=chrome&q={}" },
|
||||
@ -83,12 +73,15 @@ void SettingsDialog::setup_search_engines()
|
||||
};
|
||||
|
||||
QMenu* search_engine_menu = new QMenu(this);
|
||||
for (auto& search_engine : search_engines) {
|
||||
QAction* action = new QAction(search_engine.name, this);
|
||||
connect(action, &QAction::triggered, this, [&, search_engine] {
|
||||
for (auto const& search_engine : WebView::search_engines()) {
|
||||
auto search_engine_name = qstring_from_ak_deprecated_string(search_engine.name);
|
||||
QAction* action = new QAction(search_engine_name, this);
|
||||
|
||||
connect(action, &QAction::triggered, this, [&, search_engine_name = std::move(search_engine_name)]() {
|
||||
Settings::the()->set_search_engine(search_engine);
|
||||
m_search_engine_dropdown->setText(search_engine.name);
|
||||
m_search_engine_dropdown->setText(search_engine_name);
|
||||
});
|
||||
|
||||
search_engine_menu->addAction(action);
|
||||
}
|
||||
m_search_engine_dropdown->setMenu(search_engine_menu);
|
||||
|
Loading…
Reference in New Issue
Block a user