1 /*
2  * Copyright (C) 2018 Rafael Ostertag
3  *
4  * This file is part of YAPET.
5  *
6  * YAPET is free software: you can redistribute it and/or modify it under the
7  * terms of the GNU General Public License as published by the Free Software
8  * Foundation, either version 3 of the License, or (at your option) any later
9  * version.
10  *
11  * YAPET is distributed in the hope that it will be useful, but WITHOUT ANY
12  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
14  * details.
15  *
16  * You should have received a copy of the GNU General Public License along with
17  * YAPET.  If not, see <http://www.gnu.org/licenses/>.
18  *
19  * Additional permission under GNU GPL version 3 section 7
20  *
21  * If you modify this program, or any covered work, by linking or combining it
22  * with the OpenSSL project's OpenSSL library (or a modified version of that
23  * library), containing parts covered by the terms of the OpenSSL or SSLeay
24  * licenses, Rafael Ostertag grants you additional permission to convey the
25  * resulting work.  Corresponding Source for a non-source form of such a
26  * combination shall include the source code for the parts of OpenSSL used as
27  * well as that of the covered work.
28  */
29 
30 #ifdef HAVE_CONFIG_H
31 #include "config.h"
32 #endif
33 
34 #include "globals.h"
35 #include "intl.h"
36 #include "pwgendialog.h"
37 
38 //
39 // Private
40 //
41 
42 //
43 // Protected
44 //
checkbox_selection_handler(YACURS::Event & _e)45 void PwGenDialog::checkbox_selection_handler(YACURS::Event& _e) {
46     assert(_e == YACURS::EVT_CHECKBOX_SELECTION);
47 
48     YACURS::EventEx<YACURS::CheckBox*>& evt =
49         dynamic_cast<YACURS::EventEx<YACURS::CheckBox*>&>(_e);
50 
51     if (evt.data() == charpools) {
52         YAPET::Globals::config.pwgen_letters.set(
53             charpools->selected(_("Letters")));
54 
55         YAPET::Globals::config.pwgen_digits.set(
56             charpools->selected(_("Digits")));
57         YAPET::Globals::config.pwgen_punct.set(
58             charpools->selected(_("Punctuation")));
59         YAPET::Globals::config.pwgen_special.set(
60             charpools->selected(_("Special")));
61         YAPET::Globals::config.pwgen_other.set(charpools->selected(_("Other")));
62 
63         // make sure at least one character pool is selected, else
64         // setNewPool() will throw an exception.
65         if (YAPET::Globals::config.character_pools() == 0) {
66             YAPET::Globals::config.pwgen_letters.set(true);
67             charpools->set_selection(0);
68         }
69 
70         passwordGenerator.characterPools(
71             YAPET::Globals::config.character_pools());
72         return;
73     }
74 }
75 
button_press_handler(YACURS::Event & _e)76 void PwGenDialog::button_press_handler(YACURS::Event& _e) {
77     Dialog::button_press_handler(_e);
78 
79     YACURS::EventEx<YACURS::Button*>& evt =
80         dynamic_cast<YACURS::EventEx<YACURS::Button*>&>(_e);
81 
82     if (evt.data() == &regenbutton) {
83         // Transfer pwlen to configuration (might have changed)
84 
85         // Configuration takes care of sanitizing number with respect
86         // to min/max value.
87         YAPET::Globals::config.pwgenpwlen.set_str(pwlen.input());
88         // Transfer length back to input, in case sanitation took
89         // place and changed the value
90         pwlen.input(YAPET::Globals::config.pwgenpwlen);
91 
92         auto password{passwordGenerator.generatePassword(
93             YAPET::Globals::config.pwgenpwlen)};
94         genpw.input(reinterpret_cast<const char*>(*password));
95     }
96 }
97 
98 //
99 // Public
100 //
PwGenDialog()101 PwGenDialog::PwGenDialog()
102     : YACURS::Dialog{_("Password Generator"), YACURS::OKCANCEL,
103                      YACURS::AUTOMATIC},
104       passwordGenerator{YAPET::Globals::config.character_pools()},
105       mainpack{},
106       genpwlabel{_("Generated password")},
107       genpw{},
108       pwlenlabel{_("Password length")},
109       pwlen{},
110       regenbutton_spacer{},
111       regenbutton{_("Regenerate")} {
112     // coloring
113     genpwlabel.color(YACURS::DIALOG);
114     pwlenlabel.color(YACURS::DIALOG);
115 
116     genpw.max_input(256);
117     pwlen.max_input(3);
118     pwlen.filter(YACURS::FilterDigit());
119 
120     std::vector<std::string> labels;
121 
122     // If labels are updated, please make sure
123     // PwGenDialog::checkbox_selection_handler() is adjusted
124     // accordingly.
125     labels.push_back(_("Letters"));
126     labels.push_back(_("Digits"));
127     labels.push_back(_("Punctuation"));
128     labels.push_back(_("Special"));
129     labels.push_back(_("Other"));
130     charpools = new YACURS::CheckBox(_("Character Pools"), labels);
131 
132     if (yapet::pwgen::isLetters(YAPET::Globals::config.character_pools()))
133         charpools->set_selection(_("Letters"));
134     if (yapet::pwgen::isDigits(YAPET::Globals::config.character_pools()))
135         charpools->set_selection(_("Digits"));
136     if (yapet::pwgen::isPunct((YAPET::Globals::config.character_pools())))
137         charpools->set_selection(_("Punctuation"));
138     if (yapet::pwgen::isSpecial(YAPET::Globals::config.character_pools()))
139         charpools->set_selection(_("Special"));
140     if (yapet::pwgen::isOther(YAPET::Globals::config.character_pools()))
141         charpools->set_selection(_("Other"));
142 
143     mainpack.add_back(&genpwlabel);
144     mainpack.add_back(&genpw);
145 
146     mainpack.add_back(&pwlenlabel);
147     mainpack.add_back(&pwlen);
148 
149     mainpack.add_back(charpools);
150 
151     widget(&mainpack);
152 
153     // Add regenerate button
154     add_button(&regenbutton_spacer);
155     add_button(&regenbutton);
156 
157     // Initialize values
158     std::ostringstream conv;
159     conv << YAPET::Globals::config.pwgenpwlen.get();
160     pwlen.input(conv.str());
161 
162     auto password{passwordGenerator.generatePassword(
163         YAPET::Globals::config.pwgenpwlen.get())};
164     genpw.input(reinterpret_cast<const char*>(*password));
165 
166     YACURS::EventQueue::connect_event(
167         YACURS::EventConnectorMethod1<PwGenDialog>(
168             YACURS::EVT_CHECKBOX_SELECTION, this,
169             &PwGenDialog::checkbox_selection_handler));
170 }
171 
~PwGenDialog()172 PwGenDialog::~PwGenDialog() {
173     YACURS::EventQueue::disconnect_event(
174         YACURS::EventConnectorMethod1<PwGenDialog>(
175             YACURS::EVT_CHECKBOX_SELECTION, this,
176             &PwGenDialog::checkbox_selection_handler));
177 
178     delete charpools;
179 }
180