/*
 * passdialog.cpp
 * Copyright (C) 2009-2010 Virnik, 2011 Khryukin Evgeny
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 */

#include <QCheckBox>
#include <QLabel>
#include <QLayout>
#include <QLineEdit>
#include <QPushButton>
#include <QHBoxLayout>
#include <QVBoxLayout>

#include "passdialog.h"

/**  \brief PassDialog is used to display a password prompt for Jabber authentication.  Note that several return values need to be stored in prepared locations so the contents WILL be changed.  New variables are needed  for this purpose when called.
 * \param jid JID representing the connection displayed to the user to identify the password
 * \param _savepasswd the state of save password checkbox -- initially matches the value supplied
 * \param _passwd the password string supplied by the user
 */
PassDialog::PassDialog(QWidget *parent, const QString& jid, bool *_savePassword, QString *_password)
	: QDialog(parent)
{    
	setWindowTitle(tr("Wrong Account Password"));

	savePassword = _savePassword;
	password = _password;

	QVBoxLayout *mainLayout = new QVBoxLayout(this);
	mainLayout->setMargin(6);
	mainLayout->setSpacing(6);
	QHBoxLayout *botLayout = new QHBoxLayout();

	le_password = new QLineEdit();
	le_password->setEchoMode(QLineEdit::Password);

	cb_savePassword = new QCheckBox(tr("Save Password"));
	cb_savePassword->setChecked(*savePassword); //set the initial state of the checkbox to match supplied value
	botLayout->addWidget(cb_savePassword);
	botLayout->addStretch(1);

	QPushButton *pb_ok = new QPushButton(tr("OK"));
	pb_ok->setDefault(true);
	botLayout->addWidget(pb_ok);

	QPushButton *pb_cancel = new QPushButton(tr("Cancel"));
	botLayout->addWidget(pb_cancel);

	mainLayout->addWidget(new QLabel(tr("Please enter your password for %1:").arg(jid)));
	mainLayout->addWidget(le_password);
	mainLayout->addLayout(botLayout);

	connect(pb_cancel, SIGNAL(clicked()), this, SLOT(reject()));
	connect(pb_ok, SIGNAL(clicked()), this, SLOT(okSelected()));
}

/** Called when the user selects the Ok button */
void PassDialog::okSelected()
{
	*password = le_password->text();
	*savePassword = cb_savePassword->isChecked();
	accept();
}
