diff options
author | Sam Atkins <atkinssj@serenityos.org> | 2021-11-25 12:26:54 +0000 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2021-11-26 22:14:56 +0100 |
commit | 11466c53161145c0d648e540d5c65fdabe0b5d52 (patch) | |
tree | 319af3a8d94170f942fdcefd116036cc863dfbbd /Userland/Applications | |
parent | e9270487549a9403189e2bbc2d50ba8c3dd8a50c (diff) | |
download | serenity-11466c53161145c0d648e540d5c65fdabe0b5d52.zip |
BrowserSettings: Add setting for search engine
Diffstat (limited to 'Userland/Applications')
4 files changed, 152 insertions, 0 deletions
diff --git a/Userland/Applications/BrowserSettings/BrowserSettingsWidget.cpp b/Userland/Applications/BrowserSettings/BrowserSettingsWidget.cpp index 36ce71a148..d451552798 100644 --- a/Userland/Applications/BrowserSettings/BrowserSettingsWidget.cpp +++ b/Userland/Applications/BrowserSettings/BrowserSettingsWidget.cpp @@ -7,6 +7,7 @@ #include "BrowserSettingsWidget.h" #include <Applications/BrowserSettings/BrowserSettingsWidgetGML.h> #include <LibConfig/Client.h> +#include <LibGUI/JsonArrayModel.h> BrowserSettingsWidget::BrowserSettingsWidget() { @@ -18,6 +19,66 @@ BrowserSettingsWidget::BrowserSettingsWidget() m_show_bookmarks_bar_checkbox = find_descendant_of_type_named<GUI::CheckBox>("show_bookmarks_bar_checkbox"); m_show_bookmarks_bar_checkbox->set_checked(Config::read_bool("Browser", "Preferences", "ShowBookmarksBar", true), GUI::AllowCallback::No); + m_enable_search_engine_checkbox = find_descendant_of_type_named<GUI::CheckBox>("enable_search_engine_checkbox"); + m_search_engine_combobox_group = find_descendant_of_type_named<GUI::Widget>("search_engine_combobox_group"); + m_search_engine_combobox = find_descendant_of_type_named<GUI::ComboBox>("search_engine_combobox"); + m_custom_search_engine_group = find_descendant_of_type_named<GUI::Widget>("custom_search_engine_group"); + m_custom_search_engine_textbox = find_descendant_of_type_named<GUI::TextBox>("custom_search_engine_textbox"); + + m_enable_search_engine_checkbox->on_checked = [this](bool checked) { + m_search_engine_combobox_group->set_enabled(checked); + m_custom_search_engine_group->set_enabled(checked && m_is_custom_search_engine); + }; + + Vector<GUI::JsonArrayModel::FieldSpec> search_engine_fields; + search_engine_fields.empend("title", "Title", Gfx::TextAlignment::CenterLeft); + search_engine_fields.empend("url_format", "Url format", Gfx::TextAlignment::CenterLeft); + auto search_engines_model = GUI::JsonArrayModel::create(String::formatted("{}/SearchEngines.json", Core::StandardPaths::config_directory()), move(search_engine_fields)); + search_engines_model->invalidate(); + Vector<JsonValue> custom_search_engine; + custom_search_engine.append("Custom..."); + custom_search_engine.append(""); + search_engines_model->add(move(custom_search_engine)); + + m_search_engine_combobox->set_model(move(search_engines_model)); + m_search_engine_combobox->set_only_allow_values_from_model(true); + m_search_engine_combobox->on_change = [this](AK::String const&, GUI::ModelIndex const& cursor_index) { + auto url_format = m_search_engine_combobox->model()->index(cursor_index.row(), 1).data().to_string(); + m_is_custom_search_engine = url_format.is_empty(); + m_custom_search_engine_group->set_enabled(m_is_custom_search_engine); + }; + + auto current_search_engine_url = Config::read_string("Browser", "Preferences", "SearchEngine", {}); + if (current_search_engine_url.is_empty()) { + m_enable_search_engine_checkbox->set_checked(false); + m_search_engine_combobox_group->set_enabled(false); + m_custom_search_engine_group->set_enabled(false); + m_search_engine_combobox->set_selected_index(0); + } else { + m_enable_search_engine_checkbox->set_checked(true); + m_search_engine_combobox_group->set_enabled(true); + + bool found_url = false; + for (int item_index = 0; item_index < m_search_engine_combobox->model()->row_count(); ++item_index) { + auto url_format = m_search_engine_combobox->model()->index(item_index, 1).data().to_string(); + if (url_format == current_search_engine_url) { + m_search_engine_combobox->set_selected_index(item_index); + found_url = true; + break; + } + } + + if (!found_url) { + m_is_custom_search_engine = true; + m_custom_search_engine_textbox->set_text(current_search_engine_url); + // We assume that "Custom" is the last item + m_search_engine_combobox->set_selected_index(m_search_engine_combobox->model()->row_count() - 1); + m_custom_search_engine_group->set_enabled(true); + } else { + m_custom_search_engine_group->set_enabled(false); + } + } + m_auto_close_download_windows_checkbox = find_descendant_of_type_named<GUI::CheckBox>("auto_close_download_windows_checkbox"); m_auto_close_download_windows_checkbox->set_checked(Config::read_bool("Browser", "Preferences", "CloseDownloadWidgetOnFinish", false), GUI::AllowCallback::No); } @@ -33,5 +94,15 @@ void BrowserSettingsWidget::apply_settings() Config::write_bool("Browser", "Preferences", "ShowBookmarksBar", m_show_bookmarks_bar_checkbox->is_checked()); + if (!m_enable_search_engine_checkbox->is_checked()) { + Config::write_string("Browser", "Preferences", "SearchEngine", {}); + } else if (m_is_custom_search_engine) { + Config::write_string("Browser", "Preferences", "SearchEngine", m_custom_search_engine_textbox->text()); + } else { + auto selected_index = m_search_engine_combobox->selected_index(); + auto url = m_search_engine_combobox->model()->index(selected_index, 1).data().to_string(); + Config::write_string("Browser", "Preferences", "SearchEngine", url); + } + Config::write_bool("Browser", "Preferences", "CloseDownloadWidgetOnFinish", m_auto_close_download_windows_checkbox->is_checked()); } diff --git a/Userland/Applications/BrowserSettings/BrowserSettingsWidget.gml b/Userland/Applications/BrowserSettings/BrowserSettingsWidget.gml index 945af63495..88a8b8708b 100644 --- a/Userland/Applications/BrowserSettings/BrowserSettingsWidget.gml +++ b/Userland/Applications/BrowserSettings/BrowserSettingsWidget.gml @@ -70,6 +70,78 @@ } @GUI::GroupBox { + title: "Search Engine" + fixed_height: 140 + + layout: @GUI::VerticalBoxLayout { + margins: [16, 8, 8] + spacing: 2 + } + + @GUI::Widget { + layout: @GUI::HorizontalBoxLayout { + spacing: 16 + } + + @GUI::Label { + fixed_width: 32 + fixed_height: 32 + name: "search_engine_image_label" + } + + @GUI::CheckBox { + text: "Search using '?' in the URL box" + name: "enable_search_engine_checkbox" + } + } + + @GUI::Widget { + layout: @GUI::HorizontalBoxLayout { + spacing: 16 + } + + name: "search_engine_combobox_group" + + @GUI::Widget { + fixed_width: 32 + } + + @GUI::Label { + text: "Search engine:" + text_alignment: "CenterLeft" + fixed_width: 110 + } + + @GUI::ComboBox { + name: "search_engine_combobox" + } + } + + @GUI::Widget { + layout: @GUI::HorizontalBoxLayout { + spacing: 16 + } + + name: "custom_search_engine_group" + + @GUI::Widget { + fixed_width: 32 + } + + @GUI::Label { + text: "Enter URL template:" + text_alignment: "CenterLeft" + fixed_width: 110 + } + + @GUI::TextBox { + name: "custom_search_engine_textbox" + placeholder: "https://host/search?q={}" + } + } + } + + @GUI::GroupBox { title: "Downloads" fixed_height: 70 diff --git a/Userland/Applications/BrowserSettings/BrowserSettingsWidget.h b/Userland/Applications/BrowserSettings/BrowserSettingsWidget.h index cd0ec39983..34725778c7 100644 --- a/Userland/Applications/BrowserSettings/BrowserSettingsWidget.h +++ b/Userland/Applications/BrowserSettings/BrowserSettingsWidget.h @@ -7,6 +7,7 @@ #pragma once #include <LibGUI/CheckBox.h> +#include <LibGUI/ComboBox.h> #include <LibGUI/SettingsWindow.h> #include <LibGUI/TextBox.h> @@ -23,4 +24,11 @@ private: RefPtr<GUI::TextBox> m_homepage_url_textbox; RefPtr<GUI::CheckBox> m_show_bookmarks_bar_checkbox; RefPtr<GUI::CheckBox> m_auto_close_download_windows_checkbox; + + bool m_is_custom_search_engine { false }; + RefPtr<GUI::CheckBox> m_enable_search_engine_checkbox; + RefPtr<GUI::Widget> m_search_engine_combobox_group; + RefPtr<GUI::ComboBox> m_search_engine_combobox; + RefPtr<GUI::Widget> m_custom_search_engine_group; + RefPtr<GUI::TextBox> m_custom_search_engine_textbox; }; diff --git a/Userland/Applications/BrowserSettings/main.cpp b/Userland/Applications/BrowserSettings/main.cpp index 0192da727a..fd06a6fe34 100644 --- a/Userland/Applications/BrowserSettings/main.cpp +++ b/Userland/Applications/BrowserSettings/main.cpp @@ -19,6 +19,7 @@ ErrorOr<int> serenity_main(Main::Arguments arguments) Config::pledge_domains("Browser"); TRY(Core::System::unveil("/res", "r")); + TRY(Core::System::unveil("/home", "r")); TRY(Core::System::unveil(nullptr, nullptr)); auto app_icon = GUI::Icon::default_icon("app-browser"); |