]> vaikene.ee Git - evaf/blob - src/apps/PswGen/GUI/gui.cpp
Merged the Common::iEnv interface into Common::iApp
[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 using namespace eVaf;
35
36 VER_EXPORT_VERSION_INFO()
37 Q_EXPORT_PLUGIN2(VER_MODULE_NAME_STR, PswGen::GUI::Module)
38
39
40 //-------------------------------------------------------------------
41
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 QVBoxLayout * v = new QVBoxLayout;
79 win->widget()->setLayout(v);
80
81 QGridLayout * g = new QGridLayout;
82 v->addLayout(g);
83 g->setColumnStretch(2, 2);
84
85 QLabel * l = new QLabel(tr("Web site or application &name:", VER_MODULE_NAME_STR));
86 l->setAlignment(Qt::AlignRight);
87 g->addWidget(l, 0, 0);
88
89 wName = new QLineEdit;
90 l->setBuddy(wName);
91 if (mStorage) {
92 QCompleter * completer = new QCompleter(wName);
93 completer->setModel(mStorage->autoCompletionModel());
94 completer->setCompletionMode(QCompleter::InlineCompletion);
95 wName->setCompleter(completer);
96 }
97 connect(wName, SIGNAL(textChanged(QString)), this, SLOT(textChanged(QString)));
98 g->addWidget(wName, 0, 1, 1, 2);
99 win->widget()->setFocusProxy(wName);
100
101 l = new QLabel(tr("Master &password:", VER_MODULE_NAME_STR));
102 l->setAlignment(Qt::AlignRight);
103 g->addWidget(l, 1, 0);
104
105 wMasterPassword = new QLineEdit;
106 l->setBuddy(wMasterPassword);
107 connect(wMasterPassword, SIGNAL(textChanged(QString)), this, SLOT(textChanged(QString)));
108 wMasterPassword->setEchoMode(QLineEdit::Password);
109 g->addWidget(wMasterPassword, 1, 1, 1, 2);
110
111 l = new QLabel(tr("&Length of the password:", VER_MODULE_NAME_STR));
112 l->setAlignment(Qt::AlignRight);
113 g->addWidget(l, 2, 0);
114
115 wLength = new QSpinBox;
116 l->setBuddy(wLength);
117 wLength->setRange(0, mGenerator->maxLength());
118 wLength->setValue(DefaultPasswordLength);
119 wLength->setSpecialValueText(tr("Maximum", VER_MODULE_NAME_STR));
120 g->addWidget(wLength, 2, 1);
121
122 l = new QLabel(tr("Password:"));
123 l->setAlignment(Qt::AlignRight);
124 g->addWidget(l, 3, 0);
125
126 wPassword = new QLineEdit;
127 wPassword->setReadOnly(true);
128 g->addWidget(wPassword, 3, 1, 1, 2);
129
130 v->addStretch();
131
132 QHBoxLayout * h = new QHBoxLayout;
133 h->addStretch();
134 v->addLayout(h);
135
136 wGenerate = new QPushButton(tr("&Generate", VER_MODULE_NAME_STR));
137 wGenerate->setDisabled(true);
138 wGenerate->setDefault(true);
139 connect(wGenerate, SIGNAL(clicked()), this, SLOT(generateClicked()));
140 h->addWidget(wGenerate);
141
142 wCopy = new QPushButton(tr("&Copy to Clipboard", VER_MODULE_NAME_STR));
143 wCopy->setDisabled(true);
144 connect(wCopy, SIGNAL(clicked()), this, SLOT(copyClicked()));
145 h->addWidget(wCopy);
146
147 QAction * a = new QAction(win->widget());
148 a->setShortcut(Qt::Key_Return);
149 connect(a, SIGNAL(triggered()), this, SLOT(generateClicked()));
150 win->widget()->addAction(a);
151
152 a = new QAction(win->widget());
153 a->setShortcut(Qt::Key_Escape);
154 connect(a, SIGNAL(triggered()), qApp, SLOT(quit()));
155 win->widget()->addAction(a);
156
157 mReady = true;
158
159 EVAF_INFO("%s initialized", qPrintable(objectName()));
160
161 return true;
162 }
163
164 void Module::done()
165 {
166 mReady = false;
167
168 EVAF_INFO("%s finalized", qPrintable(objectName()));
169 }
170
171 void Module::textChanged(QString const &)
172 {
173 wGenerate->setDisabled(wMasterPassword->text().isEmpty() || wName->text().isEmpty());
174 if (!wName->text().isEmpty() && mStorage) {
175 QExplicitlySharedDataPointer<PswGen::Storage::Data> data = mStorage->query(wName->text());
176 if (data)
177 wLength->setValue(data->length());
178 }
179 }
180
181 void Module::generateClicked()
182 {
183 if (wMasterPassword->text().isEmpty() || wName->text().isEmpty())
184 return;
185 wPassword->setText(mGenerator->generatePassword(wName->text(), wMasterPassword->text(), wLength->value()));
186 wCopy->setEnabled(true);
187 if (mStorage) {
188 QExplicitlySharedDataPointer<PswGen::Storage::Data> data = mStorage->query(wName->text());
189 if (!data)
190 data = new Storage::Data(wLength->value());
191 else
192 data->setLength(wLength->value());
193 mStorage->save(wName->text(), data);
194 }
195 }
196
197 void Module::copyClicked()
198 {
199 QClipboard * clipboard = QApplication::clipboard();
200 if (clipboard)
201 clipboard->setText(wPassword->text());
202 }