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