MailSettingsWidget.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. /*
  2. * Copyright (c) 2021, the SerenityOS developers.
  3. * Copyright (c) 2021, Sam Atkins <atkinssj@serenityos.org>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include "MailSettingsWidget.h"
  8. #include <Applications/MailSettings/MailSettingsWidgetGML.h>
  9. #include <LibConfig/Client.h>
  10. #include <LibGUI/BoxLayout.h>
  11. #include <LibGUI/CheckBox.h>
  12. #include <LibGUI/ComboBox.h>
  13. #include <LibGUI/ItemListModel.h>
  14. #include <LibGUI/Label.h>
  15. #include <LibGUI/TextBox.h>
  16. void MailSettingsWidget::reset_default_values()
  17. {
  18. m_server_inputbox->set_text(""sv);
  19. m_port_combobox->set_text("993"sv);
  20. m_tls_checkbox->set_checked(true);
  21. m_email_inputbox->set_text(""sv);
  22. }
  23. void MailSettingsWidget::apply_settings()
  24. {
  25. m_server = m_server_inputbox->get_text();
  26. m_port = m_port_combobox->text();
  27. m_tls = m_tls_checkbox->is_checked();
  28. m_email = m_email_inputbox->get_text();
  29. Config::write_string("Mail"sv, "Connection"sv, "Server"sv, m_server);
  30. Config::write_string("Mail"sv, "Connection"sv, "Port"sv, m_port);
  31. Config::write_bool("Mail"sv, "Connection"sv, "TLS"sv, m_tls);
  32. Config::write_string("Mail"sv, "User"sv, "Username"sv, m_email);
  33. }
  34. ErrorOr<NonnullRefPtr<MailSettingsWidget>> MailSettingsWidget::try_create()
  35. {
  36. auto widget = TRY(adopt_nonnull_ref_or_enomem(new (nothrow) MailSettingsWidget()));
  37. TRY(widget->setup());
  38. return widget;
  39. }
  40. ErrorOr<void> MailSettingsWidget::setup()
  41. {
  42. // Common port values for email fetching
  43. m_common_ports.append("143");
  44. m_common_ports.append("993");
  45. TRY(load_from_gml(mail_settings_widget_gml));
  46. m_server_inputbox = *find_descendant_of_type_named<GUI::TextBox>("server_input");
  47. m_server_inputbox->set_text(Config::read_string("Mail"sv, "Connection"sv, "Server"sv, ""sv));
  48. m_server_inputbox->on_change = [&]() {
  49. set_modified(true);
  50. };
  51. m_port_combobox = *find_descendant_of_type_named<GUI::ComboBox>("port_input");
  52. m_port_combobox->set_text(Config::read_string("Mail"sv, "Connection"sv, "Port"sv, "993"sv));
  53. m_port_combobox->set_only_allow_values_from_model(false);
  54. m_port_combobox->set_model(*GUI::ItemListModel<DeprecatedString>::create(m_common_ports));
  55. m_port_combobox->on_change = [&](auto, auto) {
  56. set_modified(true);
  57. };
  58. m_tls_checkbox = *find_descendant_of_type_named<GUI::CheckBox>("tls_input");
  59. m_tls_checkbox->set_checked(Config::read_bool("Mail"sv, "Connection"sv, "TLS"sv, true));
  60. m_tls_checkbox->on_checked = [&](auto) {
  61. set_modified(true);
  62. };
  63. m_email_inputbox = *find_descendant_of_type_named<GUI::TextBox>("email_input");
  64. m_email_inputbox->set_text(Config::read_string("Mail"sv, "User"sv, "Username"sv, ""sv));
  65. m_email_inputbox->on_change = [&]() {
  66. set_modified(true);
  67. };
  68. return {};
  69. }