SettingsDialog.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /*
  2. * Copyright (c) 2022, Filiph Sandström <filiph.sandstrom@filfatstudios.com>
  3. * Copyright (c) 2023, Cameron Youell <cameronyouell@gmail.com>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include "SettingsDialog.h"
  8. #include "Settings.h"
  9. #include "StringUtils.h"
  10. #include <LibURL/URL.h>
  11. #include <LibWebView/SearchEngine.h>
  12. #include <QLabel>
  13. #include <QMenu>
  14. namespace Ladybird {
  15. SettingsDialog::SettingsDialog(QMainWindow* window)
  16. : QDialog(window)
  17. , m_window(window)
  18. {
  19. m_layout = new QFormLayout(this);
  20. m_enable_search = new QCheckBox(this);
  21. m_enable_search->setChecked(Settings::the()->enable_search());
  22. m_search_engine_dropdown = new QPushButton(this);
  23. m_search_engine_dropdown->setText(qstring_from_ak_string(Settings::the()->search_engine().name));
  24. m_search_engine_dropdown->setMaximumWidth(200);
  25. m_enable_autocomplete = new QCheckBox(this);
  26. m_enable_autocomplete->setChecked(Settings::the()->enable_autocomplete());
  27. m_autocomplete_engine_dropdown = new QPushButton(this);
  28. m_autocomplete_engine_dropdown->setText(Settings::the()->autocomplete_engine().name);
  29. m_autocomplete_engine_dropdown->setMaximumWidth(200);
  30. m_new_tab_page = new QLineEdit(this);
  31. m_new_tab_page->setText(Settings::the()->new_tab_page());
  32. QObject::connect(m_new_tab_page, &QLineEdit::textChanged, this, [this] {
  33. auto url_string = ak_string_from_qstring(m_new_tab_page->text());
  34. m_new_tab_page->setStyleSheet(URL::URL(url_string).is_valid() ? "" : "border: 1px solid red;");
  35. });
  36. QObject::connect(m_new_tab_page, &QLineEdit::editingFinished, this, [this] {
  37. auto url_string = ak_string_from_qstring(m_new_tab_page->text());
  38. if (URL::URL(url_string).is_valid())
  39. Settings::the()->set_new_tab_page(m_new_tab_page->text());
  40. });
  41. QObject::connect(m_new_tab_page, &QLineEdit::returnPressed, this, [this] {
  42. close();
  43. });
  44. setup_search_engines();
  45. m_layout->addRow(new QLabel("Page on New Tab", this), m_new_tab_page);
  46. m_layout->addRow(new QLabel("Enable Search", this), m_enable_search);
  47. m_layout->addRow(new QLabel("Search Engine", this), m_search_engine_dropdown);
  48. m_layout->addRow(new QLabel("Enable Autocomplete", this), m_enable_autocomplete);
  49. m_layout->addRow(new QLabel("Autocomplete Engine", this), m_autocomplete_engine_dropdown);
  50. setWindowTitle("Settings");
  51. setLayout(m_layout);
  52. resize(600, 250);
  53. }
  54. void SettingsDialog::setup_search_engines()
  55. {
  56. // FIXME: These should be centralized in LibWebView.
  57. Vector<Settings::EngineProvider> autocomplete_engines = {
  58. { "DuckDuckGo", "https://duckduckgo.com/ac/?q={}" },
  59. { "Google", "https://www.google.com/complete/search?client=chrome&q={}" },
  60. { "Yahoo", "https://search.yahoo.com/sugg/gossip/gossip-us-ura/?output=sd1&command={}" },
  61. };
  62. QMenu* search_engine_menu = new QMenu(this);
  63. for (auto const& search_engine : WebView::search_engines()) {
  64. auto search_engine_name = qstring_from_ak_string(search_engine.name);
  65. QAction* action = new QAction(search_engine_name, this);
  66. connect(action, &QAction::triggered, this, [&, search_engine_name = std::move(search_engine_name)]() {
  67. Settings::the()->set_search_engine(search_engine);
  68. m_search_engine_dropdown->setText(search_engine_name);
  69. });
  70. search_engine_menu->addAction(action);
  71. }
  72. m_search_engine_dropdown->setMenu(search_engine_menu);
  73. m_search_engine_dropdown->setEnabled(Settings::the()->enable_search());
  74. QMenu* autocomplete_engine_menu = new QMenu(this);
  75. for (auto& autocomplete_engine : autocomplete_engines) {
  76. QAction* action = new QAction(autocomplete_engine.name, this);
  77. connect(action, &QAction::triggered, this, [&, autocomplete_engine] {
  78. Settings::the()->set_autocomplete_engine(autocomplete_engine);
  79. m_autocomplete_engine_dropdown->setText(autocomplete_engine.name);
  80. });
  81. autocomplete_engine_menu->addAction(action);
  82. }
  83. m_autocomplete_engine_dropdown->setMenu(autocomplete_engine_menu);
  84. m_autocomplete_engine_dropdown->setEnabled(Settings::the()->enable_autocomplete());
  85. connect(m_enable_search, &QCheckBox::stateChanged, this, [&](int state) {
  86. Settings::the()->set_enable_search(state == Qt::Checked);
  87. m_search_engine_dropdown->setEnabled(state == Qt::Checked);
  88. });
  89. connect(m_enable_autocomplete, &QCheckBox::stateChanged, this, [&](int state) {
  90. Settings::the()->set_enable_autocomplete(state == Qt::Checked);
  91. m_autocomplete_engine_dropdown->setEnabled(state == Qt::Checked);
  92. });
  93. }
  94. }