Supporters update.
[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
22 #include "download_certificate_dialog.h"
23 #include "credentials_download_certificate_panel.h"
24 #include "password_entry.h"
25 #include "wx_util.h"
26
27
28 using std::string;
29 using std::function;
30 using boost::optional;
31
32
33 CredentialsDownloadCertificatePanel::CredentialsDownloadCertificatePanel (
34                 DownloadCertificateDialog* dialog,
35                 function<optional<string> ()> get_username,
36                 function<void (string)> set_username,
37                 function<void ()> unset_username,
38                 function<optional<string> ()> get_password,
39                 function<void (string)> set_password,
40                 function<void ()> unset_password
41                 )
42         : DownloadCertificatePanel (dialog)
43         , _get_username (get_username)
44         , _set_username (set_username)
45         , _unset_username (unset_username)
46         , _get_password (get_password)
47         , _set_password (set_password)
48         , _unset_password (unset_password)
49 {
50         add_label_to_sizer (_table, this, _("User name"), true, 0, wxALIGN_CENTER_VERTICAL);
51         _username = new wxTextCtrl (this, wxID_ANY, std_to_wx(_get_username().get_value_or("")), wxDefaultPosition, wxSize(300, -1));
52         _table->Add (_username, 1, wxEXPAND);
53
54         add_label_to_sizer (_table, this, _("Password"), true, 0, wxALIGN_CENTER_VERTICAL);
55         _password = new PasswordEntry (this);
56         _password->set (_get_password().get_value_or(""));
57         _table->Add (_password->get_panel(), 1, wxEXPAND);
58
59         _username->Bind (wxEVT_TEXT, boost::bind(&CredentialsDownloadCertificatePanel::username_changed, this));
60         _password->Changed.connect (boost::bind(&CredentialsDownloadCertificatePanel::password_changed, this));
61
62         _overall_sizer->Layout ();
63         _overall_sizer->SetSizeHints (this);
64 }
65
66 bool
67 CredentialsDownloadCertificatePanel::ready_to_download () const
68 {
69         return DownloadCertificatePanel::ready_to_download() && static_cast<bool>(_get_username()) && _get_username().get() != "" && static_cast<bool>(_get_password()) && _get_password().get() != "";
70 }
71
72 void
73 CredentialsDownloadCertificatePanel::username_changed ()
74 {
75         wxString const s = _username->GetValue();
76         if (!s.IsEmpty()) {
77                 _set_username (wx_to_std(s));
78         } else {
79                 _unset_username ();
80         }
81         _dialog->setup_sensitivity ();
82 }
83
84 void
85 CredentialsDownloadCertificatePanel::password_changed ()
86 {
87         string const s = _password->get();
88         if (!s.empty()) {
89                 _set_password (s);
90         } else {
91                 _unset_password ();
92         }
93         _dialog->setup_sensitivity ();
94 }
95