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 QVBoxLayout
* v
= new QVBoxLayout
;
78 win
->widget()->setLayout(v
);
80 QGridLayout
* g
= new QGridLayout
;
82 g
->setColumnStretch(2, 2);
84 QLabel
* l
= new QLabel(tr("Master &password:", VER_MODULE_NAME_STR
));
85 l
->setAlignment(Qt::AlignRight
);
86 g
->addWidget(l
, 0, 0);
88 wMasterPassword
= new QLineEdit
;
89 l
->setBuddy(wMasterPassword
);
90 connect(wMasterPassword
, SIGNAL(textChanged(QString
)), this, SLOT(textChanged(QString
)));
91 wMasterPassword
->setEchoMode(QLineEdit::Password
);
92 g
->addWidget(wMasterPassword
, 0, 1, 1, 2);
94 l
= new QLabel(tr("Web site or application &name:", VER_MODULE_NAME_STR
));
95 l
->setAlignment(Qt::AlignRight
);
96 g
->addWidget(l
, 1, 0);
98 wName
= new QLineEdit
;
101 QCompleter
* completer
= new QCompleter(wName
);
102 completer
->setModel(mStorage
->autoCompletionModel());
103 completer
->setCompletionMode(QCompleter::InlineCompletion
);
104 wName
->setCompleter(completer
);
106 connect(wName
, SIGNAL(textChanged(QString
)), this, SLOT(textChanged(QString
)));
107 g
->addWidget(wName
, 1, 1, 1, 2);
108 win
->widget()->setFocusProxy(wName
);
110 l
= new QLabel(tr("&Length of the password:", VER_MODULE_NAME_STR
));
111 l
->setAlignment(Qt::AlignRight
);
112 g
->addWidget(l
, 2, 0);
114 wLength
= new QSpinBox
;
115 l
->setBuddy(wLength
);
116 wLength
->setRange(0, mGenerator
->maxLength());
117 wLength
->setValue(DefaultPasswordLength
);
118 wLength
->setSpecialValueText(tr("Maximum", VER_MODULE_NAME_STR
));
119 g
->addWidget(wLength
, 2, 1);
121 l
= new QLabel(tr("Password:"));
122 l
->setAlignment(Qt::AlignRight
);
123 g
->addWidget(l
, 3, 0);
125 wPassword
= new QLineEdit
;
126 wPassword
->setReadOnly(true);
127 g
->addWidget(wPassword
, 3, 1, 1, 2);
131 QHBoxLayout
* h
= new QHBoxLayout
;
135 wGenerate
= new QPushButton(tr("&Generate", VER_MODULE_NAME_STR
));
136 wGenerate
->setDisabled(true);
137 wGenerate
->setDefault(true);
138 connect(wGenerate
, SIGNAL(clicked()), this, SLOT(generateClicked()));
139 h
->addWidget(wGenerate
);
141 wCopy
= new QPushButton(tr("&Copy to Clipboard", VER_MODULE_NAME_STR
));
142 wCopy
->setDisabled(true);
143 connect(wCopy
, SIGNAL(clicked()), this, SLOT(copyClicked()));
146 QAction
* a
= new QAction(win
->widget());
147 a
->setShortcut(Qt::Key_Return
);
148 connect(a
, SIGNAL(triggered()), this, SLOT(generateClicked()));
149 win
->widget()->addAction(a
);
151 a
= new QAction(win
->widget());
152 a
->setShortcut(Qt::Key_Escape
);
153 connect(a
, SIGNAL(triggered()), qApp
, SLOT(quit()));
154 win
->widget()->addAction(a
);
158 EVAF_INFO("%s initialized", qPrintable(objectName()));
167 EVAF_INFO("%s finalized", qPrintable(objectName()));
170 void Module::textChanged(QString
const &)
172 wGenerate
->setDisabled(wMasterPassword
->text().isEmpty() || wName
->text().isEmpty());
173 if (!wName
->text().isEmpty() && mStorage
) {
174 QExplicitlySharedDataPointer
<PswGen::Storage::Data
> data
= mStorage
->query(wName
->text());
176 wLength
->setValue(data
->length());
180 void Module::generateClicked()
182 if (wMasterPassword
->text().isEmpty() || wName
->text().isEmpty())
184 wPassword
->setText(mGenerator
->generatePassword(wName
->text(), wMasterPassword
->text(), wLength
->value()));
185 wCopy
->setEnabled(true);
187 QExplicitlySharedDataPointer
<PswGen::Storage::Data
> data
= mStorage
->query(wName
->text());
189 data
= new Storage::Data(wName
->text(), wLength
->value());
191 data
->setLength(wLength
->value());
192 mStorage
->save(wName
->text(), data
);
196 void Module::copyClicked()
198 QClipboard
* clipboard
= QApplication::clipboard();
200 clipboard
->setText(wPassword
->text());