]> vaikene.ee Git - evaf/blob - src/apps/PswGen/GUI/gui.cpp
Modified the PswGen application to follow the tutorial.
[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
25 #include <Common/Globals>
26 #include <Common/iLogger>
27 #include <Common/iRegistry>
28 #include <SdiWindow/iSdiWindow>
29
30 #include <QtGui>
31
32
33 using namespace eVaf;
34
35 VER_EXPORT_VERSION_INFO()
36 Q_EXPORT_PLUGIN2(VER_MODULE_NAME_STR, PswGen::GUI::Module)
37
38
39 //-------------------------------------------------------------------
40
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 {
50 setObjectName(QString("%1.%2").arg(VER_MODULE_NAME_STR).arg(__FUNCTION__));
51
52 EVAF_INFO("%s created", qPrintable(objectName()));
53 }
54
55 Module::~Module()
56 {
57 EVAF_INFO("%s destroyed", qPrintable(objectName()));
58 }
59
60 bool Module::init(QString const & args)
61 {
62 Q_UNUSED(args);
63
64 // Get the iGenerator interface
65 EVAF_TEST_X((mGenerator = evafQueryInterface<PswGen::iGenerator>("iGenerator")), "No iGenerator interface");
66
67 // Get the main window interface and fill it with the widgets
68 SdiWindow::iSdiWindow * win = evafQueryInterface<SdiWindow::iSdiWindow>("iSdiWindow");
69 EVAF_TEST_X(win, "No iSdiWindow interface");
70
71 QVBoxLayout * v = new QVBoxLayout;
72 win->widget()->setLayout(v);
73
74 QGridLayout * g = new QGridLayout;
75 v->addLayout(g);
76 g->setColumnStretch(2, 2);
77
78 QLabel * l = new QLabel(tr("Web site or application &name:", VER_MODULE_NAME_STR));
79 l->setAlignment(Qt::AlignRight);
80 g->addWidget(l, 0, 0);
81
82 wName = new QLineEdit;
83 l->setBuddy(wName);
84 connect(wName, SIGNAL(textChanged(QString)), this, SLOT(textChanged(QString)));
85 g->addWidget(wName, 0, 1, 1, 2);
86 win->widget()->setFocusProxy(wName);
87
88 l = new QLabel(tr("Master &password:", VER_MODULE_NAME_STR));
89 l->setAlignment(Qt::AlignRight);
90 g->addWidget(l, 1, 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, 1, 1, 1, 2);
97
98 l = new QLabel(tr("&Length of the password:", VER_MODULE_NAME_STR));
99 l->setAlignment(Qt::AlignRight);
100 g->addWidget(l, 2, 0);
101
102 wLength = new QSpinBox;
103 l->setBuddy(wLength);
104 wLength->setRange(0, mGenerator->maxLength());
105 wLength->setValue(DefaultPasswordLength);
106 wLength->setSpecialValueText(tr("Maximum", VER_MODULE_NAME_STR));
107 g->addWidget(wLength, 2, 1);
108
109 l = new QLabel(tr("Password:"));
110 l->setAlignment(Qt::AlignRight);
111 g->addWidget(l, 3, 0);
112
113 wPassword = new QLineEdit;
114 wPassword->setReadOnly(true);
115 g->addWidget(wPassword, 3, 1, 1, 2);
116
117 v->addStretch();
118
119 QHBoxLayout * h = new QHBoxLayout;
120 h->addStretch();
121 v->addLayout(h);
122
123 wGenerate = new QPushButton(tr("&Generate", VER_MODULE_NAME_STR));
124 wGenerate->setDisabled(true);
125 wGenerate->setDefault(true);
126 connect(wGenerate, SIGNAL(clicked()), this, SLOT(generateClicked()));
127 h->addWidget(wGenerate);
128
129 wCopy = new QPushButton(tr("&Copy to Clipboard", VER_MODULE_NAME_STR));
130 wCopy->setDisabled(true);
131 connect(wCopy, SIGNAL(clicked()), this, SLOT(copyClicked()));
132 h->addWidget(wCopy);
133
134 QAction * a = new QAction(win->widget());
135 a->setShortcut(Qt::Key_Return);
136 connect(a, SIGNAL(triggered()), this, SLOT(generateClicked()));
137 win->widget()->addAction(a);
138
139 a = new QAction(win->widget());
140 a->setShortcut(Qt::Key_Escape);
141 connect(a, SIGNAL(triggered()), qApp, SLOT(quit()));
142 win->widget()->addAction(a);
143
144 mReady = true;
145
146 EVAF_INFO("%s initialized", qPrintable(objectName()));
147
148 return true;
149 }
150
151 void Module::done()
152 {
153 mReady = false;
154
155 EVAF_INFO("%s finalized", qPrintable(objectName()));
156 }
157
158 void Module::textChanged(QString const &)
159 {
160 wGenerate->setDisabled(wMasterPassword->text().isEmpty() || wName->text().isEmpty());
161 }
162
163 void Module::generateClicked()
164 {
165 if (wMasterPassword->text().isEmpty() || wName->text().isEmpty())
166 return;
167 wPassword->setText(mGenerator->generatePassword(wName->text(), wMasterPassword->text(), wLength->value()));
168 wCopy->setEnabled(true);
169 }
170
171 void Module::copyClicked()
172 {
173 QClipboard * clipboard = QApplication::clipboard();
174 if (clipboard)
175 clipboard->setText(wPassword->text());
176 }