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.
22 #include "Generator/iGenerator"
23 #include "Storage/iStorage"
25 #include <Common/Globals>
26 #include <Common/iLogger>
27 #include <Common/iRegistry>
28 #include <SdiWindow/iSdiWindow>
34 VER_EXPORT_VERSION_INFO()
37 //-------------------------------------------------------------------
40 using namespace eVaf::PswGen::GUI
;
42 int const Module::DefaultPasswordLength
= 16;
50 setObjectName(QString("%1.%2").arg(VER_MODULE_NAME_STR
).arg(__FUNCTION__
));
52 EVAF_INFO("%s created", qPrintable(objectName()));
57 EVAF_INFO("%s destroyed", qPrintable(objectName()));
60 bool Module::init(QString
const & args
)
64 // Get the iGenerator interface
65 EVAF_TEST_X((mGenerator
= evafQueryInterface
<PswGen::iGenerator
>("iGenerator")), "No iGenerator interface");
67 // Get the iStorage interface (can be null)
68 mStorage
= evafQueryInterface
<PswGen::iStorage
>("iStorage");
70 EVAF_WARNING("No iStorage interface");
72 // Get the main window interface and fill it with the widgets
73 SdiWindow::iSdiWindow
* win
= evafQueryInterface
<SdiWindow::iSdiWindow
>("iSdiWindow");
74 EVAF_TEST_X(win
, "No iSdiWindow interface");
76 Gui::Panel
* panel
= new Gui::Panel
;
77 win
->addPanel("PswGen", panel
);
79 QVBoxLayout
* v
= new QVBoxLayout
;
82 QGridLayout
* g
= new QGridLayout
;
84 g
->setColumnStretch(2, 2);
86 QLabel
* l
= new QLabel(tr("Master &password:", VER_MODULE_NAME_STR
));
87 l
->setAlignment(Qt::AlignRight
);
88 g
->addWidget(l
, 0, 0);
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
, 0, 1, 1, 2);
96 l
= new QLabel(tr("Web site or application &name:", VER_MODULE_NAME_STR
));
97 l
->setAlignment(Qt::AlignRight
);
98 g
->addWidget(l
, 1, 0);
100 wName
= new QLineEdit
;
103 QCompleter
* completer
= new QCompleter(wName
);
104 completer
->setModel(mStorage
->autoCompletionModel());
105 completer
->setCompletionMode(QCompleter::InlineCompletion
);
106 wName
->setCompleter(completer
);
108 connect(wName
, SIGNAL(textChanged(QString
)), this, SLOT(textChanged(QString
)));
109 g
->addWidget(wName
, 1, 1, 1, 2);
110 panel
->setFocusProxy(wName
);
112 l
= new QLabel(tr("&Suffix:", VER_MODULE_NAME_STR
));
113 l
->setAlignment(Qt::AlignRight
);
114 g
->addWidget(l
, 2, 0);
116 wSuffix
= new QLineEdit
;
117 l
->setBuddy(wSuffix
);
118 g
->addWidget(wSuffix
, 2, 1, 1, 2);
120 l
= new QLabel(tr("&Length of the password:", VER_MODULE_NAME_STR
));
121 l
->setAlignment(Qt::AlignRight
);
122 g
->addWidget(l
, 3, 0);
124 wLength
= new QSpinBox
;
125 l
->setBuddy(wLength
);
126 wLength
->setRange(0, mGenerator
->maxLength());
127 wLength
->setValue(DefaultPasswordLength
);
128 wLength
->setSpecialValueText(tr("Maximum", VER_MODULE_NAME_STR
));
129 g
->addWidget(wLength
, 3, 1);
131 wAlNum
= new QCheckBox(tr("&Alpha-Numeric only", VER_MODULE_NAME_STR
));
132 g
->addWidget(wAlNum
, 3, 2);
134 l
= new QLabel(tr("Password:"));
135 l
->setAlignment(Qt::AlignRight
);
136 g
->addWidget(l
, 4, 0);
138 wPassword
= new QLineEdit
;
139 wPassword
->setReadOnly(true);
140 g
->addWidget(wPassword
, 4, 1, 1, 2);
144 QHBoxLayout
* h
= new QHBoxLayout
;
148 wGenerate
= new QPushButton(tr("&Generate", VER_MODULE_NAME_STR
));
149 wGenerate
->setDisabled(true);
150 wGenerate
->setDefault(true);
151 connect(wGenerate
, SIGNAL(clicked()), this, SLOT(generateClicked()));
152 h
->addWidget(wGenerate
);
154 wCopy
= new QPushButton(tr("&Copy to Clipboard", VER_MODULE_NAME_STR
));
155 wCopy
->setDisabled(true);
156 connect(wCopy
, SIGNAL(clicked()), this, SLOT(copyClicked()));
159 QAction
* a
= new QAction(panel
);
160 a
->setShortcut(Qt::Key_Return
);
161 connect(a
, SIGNAL(triggered()), this, SLOT(generateClicked()));
164 a
= new QAction(panel
);
165 a
->setShortcut(Qt::Key_Escape
);
166 connect(a
, SIGNAL(triggered()), qApp
, SLOT(quit()));
171 EVAF_INFO("%s initialized", qPrintable(objectName()));
180 EVAF_INFO("%s finalized", qPrintable(objectName()));
183 void Module::textChanged(QString
const &)
185 wGenerate
->setDisabled(wMasterPassword
->text().isEmpty() || wName
->text().isEmpty());
186 if (!wName
->text().isEmpty() && mStorage
) {
187 QExplicitlySharedDataPointer
<PswGen::Storage::Data
> data
= mStorage
->query(wName
->text());
189 wLength
->setValue(data
->length());
190 wSuffix
->setText(data
->suffix());
191 wAlNum
->setChecked(data
->flags() & uint(iGenerator::ALPHANUMERIC
));
196 void Module::generateClicked()
198 if (wMasterPassword
->text().isEmpty() || wName
->text().isEmpty())
201 if (wAlNum
->isChecked())
202 flags
|= iGenerator::ALPHANUMERIC
;
203 wPassword
->setText(mGenerator
->generatePassword(wName
->text() + wSuffix
->text(),
204 wMasterPassword
->text(),
207 wCopy
->setEnabled(true);
209 QExplicitlySharedDataPointer
<PswGen::Storage::Data
> data
= mStorage
->query(wName
->text());
211 data
= new Storage::Data(wName
->text(), wSuffix
->text(), wLength
->value());
214 data
->setSuffix(wSuffix
->text());
215 data
->setLength(wLength
->value());
216 if (wAlNum
->isChecked())
217 data
->setFlags(data
->flags() | iGenerator::ALPHANUMERIC
);
219 data
->setFlags(data
->flags() & ~uint(iGenerator::ALPHANUMERIC
));
221 mStorage
->save(wName
->text(), data
);
225 void Module::copyClicked()
227 QClipboard
* clipboard
= QApplication::clipboard();
229 clipboard
->setText(wPassword
->text());