Extract common code out into kdm_for_screen()
[dcpomatic.git] / src / wx / credentials_download_certificate_panel.cc
1 /*
2     Copyright (C) 2020 Carl Hetherington <cth@carlh.net>
3
4     This file is part of DCP-o-matic.
5
6     DCP-o-matic is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10
11     DCP-o-matic is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with DCP-o-matic.  If not, see <http://www.gnu.org/licenses/>.
18
19 */
20
21 #include "download_certificate_dialog.h"
22 #include "credentials_download_certificate_panel.h"
23 #include "password_entry.h"
24 #include "wx_util.h"
25
26 using std::string;
27 using boost::function;
28 using boost::optional;
29
30 CredentialsDownloadCertificatePanel::CredentialsDownloadCertificatePanel (
31                 DownloadCertificateDialog* dialog,
32                 function<optional<string> ()> get_username,
33                 function<void (string)> set_username,
34                 function<void ()> unset_username,
35                 function<optional<string> ()> get_password,
36                 function<void (string)> set_password,
37                 function<void ()> unset_password
38                 )
39         : DownloadCertificatePanel (dialog)
40         , _get_username (get_username)
41         , _set_username (set_username)
42         , _unset_username (unset_username)
43         , _get_password (get_password)
44         , _set_password (set_password)
45         , _unset_password (unset_password)
46 {
47         add_label_to_sizer (_table, this, _("User name"), true);
48         _username = new wxTextCtrl (this, wxID_ANY, std_to_wx(_get_username().get_value_or("")), wxDefaultPosition, wxSize(300, -1));
49         _table->Add (_username, 1, wxEXPAND);
50
51         add_label_to_sizer (_table, this, _("Password"), true);
52         _password = new PasswordEntry (this);
53         _password->set (_get_password().get_value_or(""));
54         _table->Add (_password->get_panel(), 1, wxEXPAND);
55
56         _username->Bind (wxEVT_TEXT, boost::bind(&CredentialsDownloadCertificatePanel::username_changed, this));
57         _password->Changed.connect (boost::bind(&CredentialsDownloadCertificatePanel::password_changed, this));
58
59         _overall_sizer->Layout ();
60         _overall_sizer->SetSizeHints (this);
61 }
62
63 bool
64 CredentialsDownloadCertificatePanel::ready_to_download () const
65 {
66         return DownloadCertificatePanel::ready_to_download() && static_cast<bool>(_get_username()) && _get_username().get() != "" && static_cast<bool>(_get_password()) && _get_password().get() != "";
67 }
68
69 void
70 CredentialsDownloadCertificatePanel::username_changed ()
71 {
72         wxString const s = _username->GetValue();
73         if (!s.IsEmpty()) {
74                 _set_username (wx_to_std(s));
75         } else {
76                 _unset_username ();
77         }
78         _dialog->setup_sensitivity ();
79 }
80
81 void
82 CredentialsDownloadCertificatePanel::password_changed ()
83 {
84         string const s = _password->get();
85         if (!s.empty()) {
86                 _set_password (s);
87         } else {
88                 _unset_password ();
89         }
90         _dialog->setup_sensitivity ();
91 }
92