2 * @file PswGen/GUI/gui.cpp
3 * @brief GUI for the PswGen application
6 * Copyright (c) 2011 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>
34 VER_EXPORT_VERSION_INFO()
35 Q_EXPORT_PLUGIN2(VER_MODULE_NAME_STR
, eVaf::PswGen::GUI::Module
)
38 //-------------------------------------------------------------------
41 using namespace eVaf::PswGen::GUI
;
43 int const Module::DefaultPasswordLength
= 16;
51 setObjectName(QString("%1.%2").arg(VER_MODULE_NAME_STR
).arg(__FUNCTION__
));
53 EVAF_INFO("%s created", qPrintable(objectName()));
58 EVAF_INFO("%s destroyed", qPrintable(objectName()));
61 bool Module::init(QString
const & args
)
65 // Get the iGenerator interface
66 EVAF_TEST_X((mGenerator
= evafQueryInterface
<PswGen::iGenerator
>("iGenerator")), "No iGenerator interface");
68 // Get the iStorage interface (can be null)
69 mStorage
= evafQueryInterface
<PswGen::iStorage
>("iStorage");
71 EVAF_WARNING("No iStorage interface");
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");
77 QWidget
* masterWidget
= new QWidget
;
78 win
->addWidget(masterWidget
);
80 QVBoxLayout
* v
= new QVBoxLayout
;
81 masterWidget
->setLayout(v
);
83 QGridLayout
* g
= new QGridLayout
;
85 g
->setColumnStretch(2, 2);
87 QLabel
* l
= new QLabel(tr("Master &password:", VER_MODULE_NAME_STR
));
88 l
->setAlignment(Qt::AlignRight
);
89 g
->addWidget(l
, 0, 0);
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);
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);
101 wName
= new QLineEdit
;
104 QCompleter
* completer
= new QCompleter(wName
);
105 completer
->setModel(mStorage
->autoCompletionModel());
106 completer
->setCompletionMode(QCompleter::InlineCompletion
);
107 wName
->setCompleter(completer
);
109 connect(wName
, SIGNAL(textChanged(QString
)), this, SLOT(textChanged(QString
)));
110 g
->addWidget(wName
, 1, 1, 1, 2);
111 masterWidget
->setFocusProxy(wName
);
113 l
= new QLabel(tr("&Length of the password:", VER_MODULE_NAME_STR
));
114 l
->setAlignment(Qt::AlignRight
);
115 g
->addWidget(l
, 2, 0);
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);
124 l
= new QLabel(tr("Password:"));
125 l
->setAlignment(Qt::AlignRight
);
126 g
->addWidget(l
, 3, 0);
128 wPassword
= new QLineEdit
;
129 wPassword
->setReadOnly(true);
130 g
->addWidget(wPassword
, 3, 1, 1, 2);
134 QHBoxLayout
* h
= new QHBoxLayout
;
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
);
144 wCopy
= new QPushButton(tr("&Copy to Clipboard", VER_MODULE_NAME_STR
));
145 wCopy
->setDisabled(true);
146 connect(wCopy
, SIGNAL(clicked()), this, SLOT(copyClicked()));
149 QAction
* a
= new QAction(masterWidget
);
150 a
->setShortcut(Qt::Key_Return
);
151 connect(a
, SIGNAL(triggered()), this, SLOT(generateClicked()));
152 masterWidget
->addAction(a
);
154 a
= new QAction(masterWidget
);
155 a
->setShortcut(Qt::Key_Escape
);
156 connect(a
, SIGNAL(triggered()), qApp
, SLOT(quit()));
157 masterWidget
->addAction(a
);
161 EVAF_INFO("%s initialized", qPrintable(objectName()));
170 EVAF_INFO("%s finalized", qPrintable(objectName()));
173 void Module::textChanged(QString
const &)
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());
179 wLength
->setValue(data
->length());
183 void Module::generateClicked()
185 if (wMasterPassword
->text().isEmpty() || wName
->text().isEmpty())
187 wPassword
->setText(mGenerator
->generatePassword(wName
->text(), wMasterPassword
->text(), wLength
->value()));
188 wCopy
->setEnabled(true);
190 QExplicitlySharedDataPointer
<PswGen::Storage::Data
> data
= mStorage
->query(wName
->text());
192 data
= new Storage::Data(wName
->text(), wLength
->value());
194 data
->setLength(wLength
->value());
195 mStorage
->save(wName
->text(), data
);
199 void Module::copyClicked()
201 QClipboard
* clipboard
= QApplication::clipboard();
203 clipboard
->setText(wPassword
->text());