2 * @file PswGen/GUI/gui.cpp
3 * @brief GUI for the PswGen application
6 * Copyright (c) 2011-2012 Enar Vaikene
8 * This file is part of the eVaf C++ cross-platform application development framework.
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.
16 * Alternatively, this file may be used in accordance with the Commercial License
17 * Agreement provided with the Software.
23 #include "Generator/iGenerator"
24 #include "Storage/iStorage"
26 #include <Common/Globals>
27 #include <Common/iLogger>
28 #include <Common/iRegistry>
29 #include <SdiWindow/iSdiWindow>
35 VER_EXPORT_VERSION_INFO()
36 Q_EXPORT_PLUGIN2(VER_MODULE_NAME_STR
, eVaf::PswGen::GUI::Module
)
39 //-------------------------------------------------------------------
42 using namespace eVaf::PswGen::GUI
;
44 int const Module::DefaultPasswordLength
= 16;
52 setObjectName(QString("%1.%2").arg(VER_MODULE_NAME_STR
).arg(__FUNCTION__
));
54 EVAF_INFO("%s created", qPrintable(objectName()));
59 EVAF_INFO("%s destroyed", qPrintable(objectName()));
62 bool Module::init(QString
const & args
)
66 // Get the iGenerator interface
67 EVAF_TEST_X((mGenerator
= evafQueryInterface
<PswGen::iGenerator
>("iGenerator")), "No iGenerator interface");
69 // Get the iStorage interface (can be null)
70 mStorage
= evafQueryInterface
<PswGen::iStorage
>("iStorage");
72 EVAF_WARNING("No iStorage interface");
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");
78 Gui::Panel
* panel
= new Gui::Panel
;
79 win
->addPanel("PswGen", panel
);
81 QVBoxLayout
* v
= new QVBoxLayout
;
84 QGridLayout
* g
= new QGridLayout
;
86 g
->setColumnStretch(2, 2);
88 QLabel
* l
= new QLabel(tr("Master &password:", VER_MODULE_NAME_STR
));
89 l
->setAlignment(Qt::AlignRight
);
90 g
->addWidget(l
, 0, 0);
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
, 0, 1, 1, 2);
98 l
= new QLabel(tr("Web site or application &name:", VER_MODULE_NAME_STR
));
99 l
->setAlignment(Qt::AlignRight
);
100 g
->addWidget(l
, 1, 0);
102 wName
= new QLineEdit
;
105 QCompleter
* completer
= new QCompleter(wName
);
106 completer
->setModel(mStorage
->autoCompletionModel());
107 completer
->setCompletionMode(QCompleter::InlineCompletion
);
108 wName
->setCompleter(completer
);
110 connect(wName
, SIGNAL(textChanged(QString
)), this, SLOT(textChanged(QString
)));
111 g
->addWidget(wName
, 1, 1, 1, 2);
112 panel
->setFocusProxy(wName
);
114 l
= new QLabel(tr("&Suffix:", VER_MODULE_NAME_STR
));
115 l
->setAlignment(Qt::AlignRight
);
116 g
->addWidget(l
, 2, 0);
118 wSuffix
= new QLineEdit
;
119 l
->setBuddy(wSuffix
);
120 g
->addWidget(wSuffix
, 2, 1, 1, 2);
122 l
= new QLabel(tr("&Length of the password:", VER_MODULE_NAME_STR
));
123 l
->setAlignment(Qt::AlignRight
);
124 g
->addWidget(l
, 3, 0);
126 wLength
= new QSpinBox
;
127 l
->setBuddy(wLength
);
128 wLength
->setRange(0, mGenerator
->maxLength());
129 wLength
->setValue(DefaultPasswordLength
);
130 wLength
->setSpecialValueText(tr("Maximum", VER_MODULE_NAME_STR
));
131 g
->addWidget(wLength
, 3, 1);
133 wAlNum
= new QCheckBox(tr("&Alpha-Numeric only", VER_MODULE_NAME_STR
));
134 g
->addWidget(wAlNum
, 3, 2);
136 l
= new QLabel(tr("Password:"));
137 l
->setAlignment(Qt::AlignRight
);
138 g
->addWidget(l
, 4, 0);
140 wPassword
= new QLineEdit
;
141 wPassword
->setReadOnly(true);
142 g
->addWidget(wPassword
, 4, 1, 1, 2);
146 QHBoxLayout
* h
= new QHBoxLayout
;
150 wGenerate
= new QPushButton(tr("&Generate", VER_MODULE_NAME_STR
));
151 wGenerate
->setDisabled(true);
152 wGenerate
->setDefault(true);
153 connect(wGenerate
, SIGNAL(clicked()), this, SLOT(generateClicked()));
154 h
->addWidget(wGenerate
);
156 wCopy
= new QPushButton(tr("&Copy to Clipboard", VER_MODULE_NAME_STR
));
157 wCopy
->setDisabled(true);
158 connect(wCopy
, SIGNAL(clicked()), this, SLOT(copyClicked()));
161 QAction
* a
= new QAction(panel
);
162 a
->setShortcut(Qt::Key_Return
);
163 connect(a
, SIGNAL(triggered()), this, SLOT(generateClicked()));
166 a
= new QAction(panel
);
167 a
->setShortcut(Qt::Key_Escape
);
168 connect(a
, SIGNAL(triggered()), qApp
, SLOT(quit()));
173 EVAF_INFO("%s initialized", qPrintable(objectName()));
182 EVAF_INFO("%s finalized", qPrintable(objectName()));
185 void Module::textChanged(QString
const &)
187 wGenerate
->setDisabled(wMasterPassword
->text().isEmpty() || wName
->text().isEmpty());
188 if (!wName
->text().isEmpty() && mStorage
) {
189 QExplicitlySharedDataPointer
<PswGen::Storage::Data
> data
= mStorage
->query(wName
->text());
191 wLength
->setValue(data
->length());
192 wSuffix
->setText(data
->suffix());
193 wAlNum
->setChecked(data
->flags() & uint(iGenerator::ALPHANUMERIC
));
198 void Module::generateClicked()
200 if (wMasterPassword
->text().isEmpty() || wName
->text().isEmpty())
203 if (wAlNum
->isChecked())
204 flags
|= iGenerator::ALPHANUMERIC
;
205 wPassword
->setText(mGenerator
->generatePassword(wName
->text() + wSuffix
->text(),
206 wMasterPassword
->text(),
209 wCopy
->setEnabled(true);
211 QExplicitlySharedDataPointer
<PswGen::Storage::Data
> data
= mStorage
->query(wName
->text());
213 data
= new Storage::Data(wName
->text(), wSuffix
->text(), wLength
->value());
216 data
->setSuffix(wSuffix
->text());
217 data
->setLength(wLength
->value());
218 if (wAlNum
->isChecked())
219 data
->setFlags(data
->flags() | iGenerator::ALPHANUMERIC
);
221 data
->setFlags(data
->flags() & ~uint(iGenerator::ALPHANUMERIC
));
223 mStorage
->save(wName
->text(), data
);
227 void Module::copyClicked()
229 QClipboard
* clipboard
= QApplication::clipboard();
231 clipboard
->setText(wPassword
->text());