]> vaikene.ee Git - evaf/blob - src/apps/PswGen/GUI/gui.cpp
Warning fixes and copyright update.
[evaf] / src / apps / PswGen / GUI / gui.cpp
1 /**
2 * @file PswGen/GUI/gui.cpp
3 * @brief GUI for the PswGen application
4 * @author Enar Vaikene
5 *
6 * Copyright (c) 2011-2019 Enar Vaikene
7 *
8 * This file is part of the eVaf C++ cross-platform application development framework.
9 *
10 * This file can be used under the terms of the GNU General Public License
11 * version 3.0 as published by the Free Software Foundation and appearing in
12 * the file LICENSE included in the packaging of this file. Please review the
13 * the following information to ensure the GNU General Public License version
14 * 3.0 requirements will be met: http://www.gnu.org/copyleft/gpl.html.
15 *
16 * Alternatively, this file may be used in accordance with the Commercial License
17 * Agreement provided with the Software.
18 */
19
20 #include "gui.h"
21
22 #include "Generator/iGenerator"
23 #include "Storage/iStorage"
24
25 #include <Common/Globals>
26 #include <Common/iLogger>
27 #include <Common/iRegistry>
28 #include <SdiWindow/iSdiWindow>
29 #include <Gui/Panel>
30
31 #include <QtWidgets>
32
33
34 VER_EXPORT_VERSION_INFO()
35
36
37 //-------------------------------------------------------------------
38
39 using namespace eVaf;
40 using namespace eVaf::PswGen::GUI;
41
42 int const Module::DefaultPasswordLength = 16;
43
44 Module::Module()
45 : Plugins::iPlugin()
46 , mReady(false)
47 , mGenerator(nullptr)
48 , mStorage(nullptr)
49 {
50 setObjectName(QString("%1.%2").arg(VER_MODULE_NAME_STR).arg(__FUNCTION__));
51
52 EVAF_INFO("%s created", qPrintable(objectName()));
53 }
54
55 Module::~Module()
56 {
57 EVAF_INFO("%s destroyed", qPrintable(objectName()));
58 }
59
60 bool Module::init(QString const & args)
61 {
62 Q_UNUSED(args)
63
64 // Get the iGenerator interface
65 EVAF_TEST_X((mGenerator = evafQueryInterface<PswGen::iGenerator>("iGenerator")), "No iGenerator interface")
66
67 // Get the iStorage interface (can be null)
68 mStorage = evafQueryInterface<PswGen::iStorage>("iStorage");
69 if (!mStorage)
70 EVAF_WARNING("No iStorage interface");
71
72 // Get the main window interface and fill it with the widgets
73 SdiWindow::iSdiWindow * win = evafQueryInterface<SdiWindow::iSdiWindow>("iSdiWindow");
74 EVAF_TEST_X(win, "No iSdiWindow interface")
75
76 Gui::Panel * panel = new Gui::Panel;
77 win->addPanel("PswGen", panel);
78
79 QVBoxLayout * v = new QVBoxLayout;
80 panel->setLayout(v);
81
82 QGridLayout * g = new QGridLayout;
83 v->addLayout(g);
84 g->setColumnStretch(2, 2);
85
86 QLabel * l = new QLabel(tr("Master &password:", VER_MODULE_NAME_STR));
87 l->setAlignment(Qt::AlignRight);
88 g->addWidget(l, 0, 0);
89
90 wMasterPassword = new QLineEdit;
91 l->setBuddy(wMasterPassword);
92 connect(wMasterPassword, SIGNAL(textChanged(QString)), this, SLOT(textChanged(QString)));
93 wMasterPassword->setEchoMode(QLineEdit::Password);
94 g->addWidget(wMasterPassword, 0, 1, 1, 2);
95
96 l = new QLabel(tr("Web site or application &name:", VER_MODULE_NAME_STR));
97 l->setAlignment(Qt::AlignRight);
98 g->addWidget(l, 1, 0);
99
100 wName = new QLineEdit;
101 l->setBuddy(wName);
102 if (mStorage) {
103 QCompleter * completer = new QCompleter(wName);
104 completer->setModel(mStorage->autoCompletionModel());
105 completer->setCompletionMode(QCompleter::InlineCompletion);
106 wName->setCompleter(completer);
107 }
108 connect(wName, SIGNAL(textChanged(QString)), this, SLOT(textChanged(QString)));
109 g->addWidget(wName, 1, 1, 1, 2);
110 panel->setFocusProxy(wName);
111
112 l = new QLabel(tr("&Suffix:", VER_MODULE_NAME_STR));
113 l->setAlignment(Qt::AlignRight);
114 g->addWidget(l, 2, 0);
115
116 wSuffix = new QLineEdit;
117 l->setBuddy(wSuffix);
118 g->addWidget(wSuffix, 2, 1, 1, 2);
119
120 l = new QLabel(tr("&Length of the password:", VER_MODULE_NAME_STR));
121 l->setAlignment(Qt::AlignRight);
122 g->addWidget(l, 3, 0);
123
124 wLength = new QSpinBox;
125 l->setBuddy(wLength);
126 wLength->setRange(0, mGenerator->maxLength());
127 wLength->setValue(DefaultPasswordLength);
128 wLength->setSpecialValueText(tr("Maximum", VER_MODULE_NAME_STR));
129 g->addWidget(wLength, 3, 1);
130
131 wAlNum = new QCheckBox(tr("&Alpha-Numeric only", VER_MODULE_NAME_STR));
132 g->addWidget(wAlNum, 3, 2);
133
134 l = new QLabel(tr("Password:"));
135 l->setAlignment(Qt::AlignRight);
136 g->addWidget(l, 4, 0);
137
138 wPassword = new QLineEdit;
139 wPassword->setReadOnly(true);
140 g->addWidget(wPassword, 4, 1, 1, 2);
141
142 v->addStretch();
143
144 QHBoxLayout * h = new QHBoxLayout;
145 h->addStretch();
146 v->addLayout(h);
147
148 wGenerate = new QPushButton(tr("&Generate", VER_MODULE_NAME_STR));
149 wGenerate->setDisabled(true);
150 wGenerate->setDefault(true);
151 connect(wGenerate, SIGNAL(clicked()), this, SLOT(generateClicked()));
152 h->addWidget(wGenerate);
153
154 wCopy = new QPushButton(tr("&Copy to Clipboard", VER_MODULE_NAME_STR));
155 wCopy->setDisabled(true);
156 connect(wCopy, SIGNAL(clicked()), this, SLOT(copyClicked()));
157 h->addWidget(wCopy);
158
159 QAction * a = new QAction(panel);
160 a->setShortcut(Qt::Key_Return);
161 connect(a, SIGNAL(triggered()), this, SLOT(generateClicked()));
162 panel->addAction(a);
163
164 a = new QAction(panel);
165 a->setShortcut(Qt::Key_Escape);
166 connect(a, SIGNAL(triggered()), qApp, SLOT(quit()));
167 panel->addAction(a);
168
169 mReady = true;
170
171 EVAF_INFO("%s initialized", qPrintable(objectName()));
172
173 return true;
174 }
175
176 void Module::done()
177 {
178 mReady = false;
179
180 EVAF_INFO("%s finalized", qPrintable(objectName()));
181 }
182
183 void Module::textChanged(QString const &)
184 {
185 wGenerate->setDisabled(wMasterPassword->text().isEmpty() || wName->text().isEmpty());
186 if (!wName->text().isEmpty() && mStorage) {
187 QExplicitlySharedDataPointer<PswGen::Storage::Data> data = mStorage->query(wName->text());
188 if (data) {
189 wLength->setValue(data->length());
190 wSuffix->setText(data->suffix());
191 wAlNum->setChecked(data->flags() & uint(iGenerator::ALPHANUMERIC));
192 }
193 }
194 }
195
196 void Module::generateClicked()
197 {
198 if (wMasterPassword->text().isEmpty() || wName->text().isEmpty())
199 return;
200 uint flags = 0;
201 if (wAlNum->isChecked())
202 flags |= iGenerator::ALPHANUMERIC;
203 wPassword->setText(mGenerator->generatePassword(wName->text() + wSuffix->text(),
204 wMasterPassword->text(),
205 wLength->value(),
206 flags));
207 wCopy->setEnabled(true);
208 if (mStorage) {
209 QExplicitlySharedDataPointer<PswGen::Storage::Data> data = mStorage->query(wName->text());
210 if (!data) {
211 data = new Storage::Data(wName->text(), wSuffix->text(), wLength->value());
212 }
213 else {
214 data->setSuffix(wSuffix->text());
215 data->setLength(wLength->value());
216 if (wAlNum->isChecked())
217 data->setFlags(data->flags() | iGenerator::ALPHANUMERIC);
218 else
219 data->setFlags(data->flags() & ~uint(iGenerator::ALPHANUMERIC));
220 }
221 mStorage->save(wName->text(), data);
222 }
223 }
224
225 void Module::copyClicked()
226 {
227 QClipboard * clipboard = QApplication::clipboard();
228 if (clipboard)
229 clipboard->setText(wPassword->text());
230 }