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>
36 VER_EXPORT_VERSION_INFO()
37 Q_EXPORT_PLUGIN2(VER_MODULE_NAME_STR
, PswGen::GUI::Module
)
40 //-------------------------------------------------------------------
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 QVBoxLayout
* v
= new QVBoxLayout
;
79 win
->widget()->setLayout(v
);
81 QGridLayout
* g
= new QGridLayout
;
83 g
->setColumnStretch(2, 2);
85 QLabel
* l
= new QLabel(tr("Web site or application &name:", VER_MODULE_NAME_STR
));
86 l
->setAlignment(Qt::AlignRight
);
87 g
->addWidget(l
, 0, 0);
89 wName
= new QLineEdit
;
92 QCompleter
* completer
= new QCompleter(wName
);
93 completer
->setModel(mStorage
->autoCompletionModel());
94 completer
->setCompletionMode(QCompleter::InlineCompletion
);
95 wName
->setCompleter(completer
);
97 connect(wName
, SIGNAL(textChanged(QString
)), this, SLOT(textChanged(QString
)));
98 g
->addWidget(wName
, 0, 1, 1, 2);
99 win
->widget()->setFocusProxy(wName
);
101 l
= new QLabel(tr("Master &password:", VER_MODULE_NAME_STR
));
102 l
->setAlignment(Qt::AlignRight
);
103 g
->addWidget(l
, 1, 0);
105 wMasterPassword
= new QLineEdit
;
106 l
->setBuddy(wMasterPassword
);
107 connect(wMasterPassword
, SIGNAL(textChanged(QString
)), this, SLOT(textChanged(QString
)));
108 wMasterPassword
->setEchoMode(QLineEdit::Password
);
109 g
->addWidget(wMasterPassword
, 1, 1, 1, 2);
111 l
= new QLabel(tr("&Length of the password:", VER_MODULE_NAME_STR
));
112 l
->setAlignment(Qt::AlignRight
);
113 g
->addWidget(l
, 2, 0);
115 wLength
= new QSpinBox
;
116 l
->setBuddy(wLength
);
117 wLength
->setRange(0, mGenerator
->maxLength());
118 wLength
->setValue(DefaultPasswordLength
);
119 wLength
->setSpecialValueText(tr("Maximum", VER_MODULE_NAME_STR
));
120 g
->addWidget(wLength
, 2, 1);
122 l
= new QLabel(tr("Password:"));
123 l
->setAlignment(Qt::AlignRight
);
124 g
->addWidget(l
, 3, 0);
126 wPassword
= new QLineEdit
;
127 wPassword
->setReadOnly(true);
128 g
->addWidget(wPassword
, 3, 1, 1, 2);
132 QHBoxLayout
* h
= new QHBoxLayout
;
136 wGenerate
= new QPushButton(tr("&Generate", VER_MODULE_NAME_STR
));
137 wGenerate
->setDisabled(true);
138 wGenerate
->setDefault(true);
139 connect(wGenerate
, SIGNAL(clicked()), this, SLOT(generateClicked()));
140 h
->addWidget(wGenerate
);
142 wCopy
= new QPushButton(tr("&Copy to Clipboard", VER_MODULE_NAME_STR
));
143 wCopy
->setDisabled(true);
144 connect(wCopy
, SIGNAL(clicked()), this, SLOT(copyClicked()));
147 QAction
* a
= new QAction(win
->widget());
148 a
->setShortcut(Qt::Key_Return
);
149 connect(a
, SIGNAL(triggered()), this, SLOT(generateClicked()));
150 win
->widget()->addAction(a
);
152 a
= new QAction(win
->widget());
153 a
->setShortcut(Qt::Key_Escape
);
154 connect(a
, SIGNAL(triggered()), qApp
, SLOT(quit()));
155 win
->widget()->addAction(a
);
159 EVAF_INFO("%s initialized", qPrintable(objectName()));
168 EVAF_INFO("%s finalized", qPrintable(objectName()));
171 void Module::textChanged(QString
const &)
173 wGenerate
->setDisabled(wMasterPassword
->text().isEmpty() || wName
->text().isEmpty());
174 if (!wName
->text().isEmpty() && mStorage
) {
175 QExplicitlySharedDataPointer
<PswGen::Storage::Data
> data
= mStorage
->query(wName
->text());
177 wLength
->setValue(data
->length());
181 void Module::generateClicked()
183 if (wMasterPassword
->text().isEmpty() || wName
->text().isEmpty())
185 wPassword
->setText(mGenerator
->generatePassword(wName
->text(), wMasterPassword
->text(), wLength
->value()));
186 wCopy
->setEnabled(true);
188 QExplicitlySharedDataPointer
<PswGen::Storage::Data
> data
= mStorage
->query(wName
->text());
190 data
= new Storage::Data(wLength
->value());
192 data
->setLength(wLength
->value());
193 mStorage
->save(wName
->text(), data
);
197 void Module::copyClicked()
199 QClipboard
* clipboard
= QApplication::clipboard();
201 clipboard
->setText(wPassword
->text());