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