Serenity Operating System
1/*
2 * Copyright (c) 2021, Andreas Kling <kling@serenityos.org>
3 * Copyright (c) 2022, the SerenityOS developers.
4 *
5 * SPDX-License-Identifier: BSD-2-Clause
6 */
7
8#include <LibGUI/Button.h>
9#include <LibGUI/Label.h>
10#include <LibGUI/PasswordInputDialog.h>
11#include <LibGUI/PasswordInputDialogGML.h>
12#include <LibGUI/TextBox.h>
13
14namespace GUI {
15
16PasswordInputDialog::PasswordInputDialog(Window* parent_window, DeprecatedString title, DeprecatedString server, DeprecatedString username)
17 : Dialog(parent_window)
18{
19 if (parent_window)
20 set_icon(parent_window->icon());
21 set_resizable(false);
22 resize(340, 122);
23 set_title(move(title));
24
25 auto widget = set_main_widget<Widget>().release_value_but_fixme_should_propagate_errors();
26 widget->load_from_gml(password_input_dialog_gml).release_value_but_fixme_should_propagate_errors();
27
28 auto& key_icon_label = *widget->find_descendant_of_type_named<GUI::Label>("key_icon_label");
29
30 key_icon_label.set_icon(Gfx::Bitmap::load_from_file("/res/icons/32x32/key.png"sv).release_value_but_fixme_should_propagate_errors());
31
32 auto& server_label = *widget->find_descendant_of_type_named<GUI::Label>("server_label");
33 server_label.set_text(move(server));
34
35 auto& username_label = *widget->find_descendant_of_type_named<GUI::Label>("username_label");
36 username_label.set_text(move(username));
37
38 auto& password_box = *widget->find_descendant_of_type_named<GUI::PasswordBox>("password_box");
39
40 auto& ok_button = *widget->find_descendant_of_type_named<GUI::Button>("ok_button");
41 ok_button.on_click = [&](auto) {
42 dbgln("GUI::PasswordInputDialog: OK button clicked");
43 m_password = password_box.text();
44 done(ExecResult::OK);
45 };
46
47 auto& cancel_button = *widget->find_descendant_of_type_named<GUI::Button>("cancel_button");
48 cancel_button.on_click = [this](auto) {
49 dbgln("GUI::PasswordInputDialog: Cancel button clicked");
50 done(ExecResult::Cancel);
51 };
52
53 password_box.on_return_pressed = [&] {
54 ok_button.click();
55 };
56 password_box.on_escape_pressed = [&] {
57 cancel_button.click();
58 };
59 password_box.set_focus(true);
60}
61
62Dialog::ExecResult PasswordInputDialog::show(Window* parent_window, DeprecatedString& text_value, DeprecatedString title, DeprecatedString server, DeprecatedString username)
63{
64 auto box = PasswordInputDialog::construct(parent_window, move(title), move(server), move(username));
65 auto result = box->exec();
66 text_value = box->m_password;
67 return result;
68}
69
70}