]> vaikene.ee Git - evaf/blob - src/apps/PswGen/GUI/gui.cpp
1ced109fa27cb869ec691d7568d3c0378dd8cb79
[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 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("&Length of the password:", VER_MODULE_NAME_STR));
115 l->setAlignment(Qt::AlignRight);
116 g->addWidget(l, 2, 0);
117
118 wLength = new QSpinBox;
119 l->setBuddy(wLength);
120 wLength->setRange(0, mGenerator->maxLength());
121 wLength->setValue(DefaultPasswordLength);
122 wLength->setSpecialValueText(tr("Maximum", VER_MODULE_NAME_STR));
123 g->addWidget(wLength, 2, 1);
124
125 l = new QLabel(tr("Password:"));
126 l->setAlignment(Qt::AlignRight);
127 g->addWidget(l, 3, 0);
128
129 wPassword = new QLineEdit;
130 wPassword->setReadOnly(true);
131 g->addWidget(wPassword, 3, 1, 1, 2);
132
133 v->addStretch();
134
135 QHBoxLayout * h = new QHBoxLayout;
136 h->addStretch();
137 v->addLayout(h);
138
139 wGenerate = new QPushButton(tr("&Generate", VER_MODULE_NAME_STR));
140 wGenerate->setDisabled(true);
141 wGenerate->setDefault(true);
142 connect(wGenerate, SIGNAL(clicked()), this, SLOT(generateClicked()));
143 h->addWidget(wGenerate);
144
145 wCopy = new QPushButton(tr("&Copy to Clipboard", VER_MODULE_NAME_STR));
146 wCopy->setDisabled(true);
147 connect(wCopy, SIGNAL(clicked()), this, SLOT(copyClicked()));
148 h->addWidget(wCopy);
149
150 QAction * a = new QAction(panel);
151 a->setShortcut(Qt::Key_Return);
152 connect(a, SIGNAL(triggered()), this, SLOT(generateClicked()));
153 panel->addAction(a);
154
155 a = new QAction(panel);
156 a->setShortcut(Qt::Key_Escape);
157 connect(a, SIGNAL(triggered()), qApp, SLOT(quit()));
158 panel->addAction(a);
159
160 mReady = true;
161
162 EVAF_INFO("%s initialized", qPrintable(objectName()));
163
164 return true;
165 }
166
167 void Module::done()
168 {
169 mReady = false;
170
171 EVAF_INFO("%s finalized", qPrintable(objectName()));
172 }
173
174 void Module::textChanged(QString const &)
175 {
176 wGenerate->setDisabled(wMasterPassword->text().isEmpty() || wName->text().isEmpty());
177 if (!wName->text().isEmpty() && mStorage) {
178 QExplicitlySharedDataPointer<PswGen::Storage::Data> data = mStorage->query(wName->text());
179 if (data)
180 wLength->setValue(data->length());
181 }
182 }
183
184 void Module::generateClicked()
185 {
186 if (wMasterPassword->text().isEmpty() || wName->text().isEmpty())
187 return;
188 wPassword->setText(mGenerator->generatePassword(wName->text(), wMasterPassword->text(), wLength->value()));
189 wCopy->setEnabled(true);
190 if (mStorage) {
191 QExplicitlySharedDataPointer<PswGen::Storage::Data> data = mStorage->query(wName->text());
192 if (!data)
193 data = new Storage::Data(wName->text(), wLength->value());
194 else
195 data->setLength(wLength->value());
196 mStorage->save(wName->text(), data);
197 }
198 }
199
200 void Module::copyClicked()
201 {
202 QClipboard * clipboard = QApplication::clipboard();
203 if (clipboard)
204 clipboard->setText(wPassword->text());
205 }