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