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