+/**
+ * @file PswGen/GUI/gui.cpp
+ * @brief GUI for the PswGen application
+ * @author Enar Vaikene
+ *
+ * Copyright (c) 2011 Enar Vaikene
+ *
+ * This file is part of the eVaf C++ cross-platform application development framework.
+ *
+ * This file can be used under the terms of the GNU General Public License
+ * version 3.0 as published by the Free Software Foundation and appearing in
+ * the file LICENSE included in the packaging of this file. Please review the
+ * the following information to ensure the GNU General Public License version
+ * 3.0 requirements will be met: http://www.gnu.org/copyleft/gpl.html.
+ *
+ * Alternatively, this file may be used in accordance with the Commercial License
+ * Agreement provided with the Software.
+ */
+
+#include "gui.h"
+#include "version.h"
+
+#include "Generator/iGenerator"
+
+#include <Common/Globals>
+#include <Common/iLogger>
+#include <Common/iRegistry>
+#include <SdiWindow/iSdiWindow>
+
+#include <QtGui>
+
+
+using namespace eVaf;
+
+VER_EXPORT_VERSION_INFO()
+Q_EXPORT_PLUGIN2(VER_MODULE_NAME_STR, PswGen::GUI::Module)
+
+
+//-------------------------------------------------------------------
+
+using namespace eVaf::PswGen::GUI;
+
+Module::Module()
+ : Plugins::iPlugin()
+ , mReady(false)
+ , mGenerator(0)
+{
+ setObjectName(QString("%1.%2").arg(VER_MODULE_NAME_STR).arg(__FUNCTION__));
+
+ EVAF_INFO("%s created", qPrintable(objectName()));
+}
+
+Module::~Module()
+{
+ EVAF_INFO("%s destroyed", qPrintable(objectName()));
+}
+
+bool Module::init(QString const & args)
+{
+ Q_UNUSED(args);
+
+ // Get the iGenerator interface
+ EVAF_TEST_X((mGenerator = evafQueryInterface<PswGen::iGenerator>("iGenerator")), "No iGenerator interface");
+
+ // Get the main window interface and fill it with the widgets
+ SdiWindow::iSdiWindow * win = evafQueryInterface<SdiWindow::iSdiWindow>("iSdiWindow");
+ EVAF_TEST_X(win, "No iSdiWindow interface");
+
+ QVBoxLayout * v = new QVBoxLayout;
+ win->widget()->setLayout(v);
+
+ QGridLayout * g = new QGridLayout;
+ v->addLayout(g);
+ g->setColumnStretch(2, 2);
+
+ QLabel * l = new QLabel(tr("Web site or application &name:", VER_MODULE_NAME_STR));
+ l->setAlignment(Qt::AlignRight);
+ g->addWidget(l, 0, 0);
+
+ wName = new QLineEdit;
+ l->setBuddy(wName);
+ connect(wName, SIGNAL(textChanged(QString)), this, SLOT(textChanged(QString)));
+ g->addWidget(wName, 0, 1, 1, 2);
+ win->widget()->setFocusProxy(wName);
+
+ l = new QLabel(tr("Master &password:", VER_MODULE_NAME_STR));
+ l->setAlignment(Qt::AlignRight);
+ g->addWidget(l, 1, 0);
+
+ wMasterPassword = new QLineEdit;
+ l->setBuddy(wMasterPassword);
+ connect(wMasterPassword, SIGNAL(textChanged(QString)), this, SLOT(textChanged(QString)));
+ wMasterPassword->setEchoMode(QLineEdit::Password);
+ g->addWidget(wMasterPassword, 1, 1, 1, 2);
+
+ l = new QLabel(tr("&Length of the password:", VER_MODULE_NAME_STR));
+ l->setAlignment(Qt::AlignRight);
+ g->addWidget(l, 2, 0);
+
+ wLength = new QSpinBox;
+ l->setBuddy(wLength);
+ wLength->setRange(0, mGenerator->maxLength());
+ wLength->setValue(PswGen::iGenerator::DEFAULT_LENGTH);
+ wLength->setSpecialValueText(tr("Maximum", VER_MODULE_NAME_STR));
+ g->addWidget(wLength, 2, 1);
+
+ l = new QLabel(tr("Password:"));
+ l->setAlignment(Qt::AlignRight);
+ g->addWidget(l, 3, 0);
+
+ wPassword = new QLineEdit;
+ wPassword->setReadOnly(true);
+ g->addWidget(wPassword, 3, 1, 1, 2);
+
+ v->addStretch();
+
+ QHBoxLayout * h = new QHBoxLayout;
+ h->addStretch();
+ v->addLayout(h);
+
+ wGenerate = new QPushButton(tr("&Generate", VER_MODULE_NAME_STR));
+ wGenerate->setDisabled(true);
+ wGenerate->setDefault(true);
+ connect(wGenerate, SIGNAL(clicked()), this, SLOT(generateClicked()));
+ h->addWidget(wGenerate);
+
+ wCopy = new QPushButton(tr("&Copy to Clipboard", VER_MODULE_NAME_STR));
+ wCopy->setDisabled(true);
+ connect(wCopy, SIGNAL(clicked()), this, SLOT(copyClicked()));
+ h->addWidget(wCopy);
+
+ QAction * a = new QAction(win->widget());
+ a->setShortcut(Qt::Key_Return);
+ connect(a, SIGNAL(triggered()), this, SLOT(generateClicked()));
+ win->widget()->addAction(a);
+
+ a = new QAction(win->widget());
+ a->setShortcut(Qt::Key_Escape);
+ connect(a, SIGNAL(triggered()), qApp, SLOT(quit()));
+ win->widget()->addAction(a);
+
+ mReady = true;
+
+ EVAF_INFO("%s initialized", qPrintable(objectName()));
+
+ return true;
+}
+
+void Module::done()
+{
+ mReady = false;
+
+ EVAF_INFO("%s finalized", qPrintable(objectName()));
+}
+
+void Module::textChanged(QString const &)
+{
+ wGenerate->setDisabled(wMasterPassword->text().isEmpty() || wName->text().isEmpty());
+}
+
+void Module::generateClicked()
+{
+ if (wMasterPassword->text().isEmpty() || wName->text().isEmpty())
+ return;
+ wPassword->setText(mGenerator->generatePassword(wName->text().toLatin1().constData(), wMasterPassword->text().toLatin1().constData(), wLength->value()));
+ wCopy->setEnabled(true);
+}
+
+void Module::copyClicked()
+{
+ QClipboard * clipboard = QApplication::clipboard();
+ if (clipboard)
+ clipboard->setText(wPassword->text());
+}