KissCount/src/view/UsersDialog.cpp

132 lines
3.1 KiB
C++
Raw Normal View History

2011-09-04 11:21:30 +02:00
/*
2012-02-01 11:02:54 +01:00
Copyright 2010-2012 Grégory Soutadé
2011-09-04 11:21:30 +02:00
This file is part of KissCount.
KissCount 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 3 of the License, or
(at your option) any later version.
KissCount 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 KissCount. If not, see <http://www.gnu.org/licenses/>.
*/
#include <list>
#include <algorithm>
#include <functional>
#include <QLabel>
#include <QGridLayout>
#include <QPushButton>
#include <QInputDialog>
#include <QMessageBox>
#include "UsersDialog.hpp"
UsersDialog::UsersDialog(KissCount* kiss, wxUI *parent) : QDialog(0, Qt::Dialog), _kiss(kiss), _wxUI(parent)
{
QGridLayout* gridLayout;
setWindowTitle(_("Users"));
setModal(true);
gridLayout = new QGridLayout(this);
gridLayout->addWidget(new QLabel(_("User "), this), 0, 0);
_users = new QComboBox(this);
gridLayout->addWidget(_users, 0, 1);
gridLayout->addWidget(new QLabel(_("Password "), this), 1, 0);
_password = new QLineEdit(this);
_password->setEchoMode(QLineEdit::Password);
gridLayout->addWidget(_password, 1, 1);
QPushButton* ok = new QPushButton(_("OK"), this);
QPushButton* cancel = new QPushButton(_("Cancel"), this);
QPushButton* newUser = new QPushButton(_("New User"), this);
gridLayout->addWidget(ok, 3, 1);
gridLayout->addWidget(cancel, 3, 2);
gridLayout->addWidget(newUser, 3, 3);
connect(ok, SIGNAL(clicked()), this, SLOT(OnOK()));
connect(cancel, SIGNAL(clicked()), this, SLOT(OnCancel()));
connect(newUser, SIGNAL(clicked()), this, SLOT(OnNewUser()));
std::list<QString> users_list = _kiss->GetUsers();
for(std::list<QString>::iterator i = users_list.begin(); i != users_list.end(); i++)
{
_users->addItem(*i);
}
_users->setFocus();
if (users_list.size() == 0)
{
2011-09-04 11:21:30 +02:00
OnNewUser();
throw ExceptionNewUser();
}
2011-09-04 11:21:30 +02:00
else
show();
}
void UsersDialog::OnEnter()
{
OnOK();
}
void UsersDialog::OnOK()
{
QString curUser = _users->currentText();
// No users in database
if (!curUser.size())
return;
if (!_kiss->IsValidUser(curUser, _password->text()))
{
QMessageBox::critical(0, _("Error"), _("Invalid password"));
_password->clear();
_password->setFocus();
}
else
{
_kiss->LoadUser(curUser);
close();
}
}
void UsersDialog::OnCancel()
{
close();
}
void UsersDialog::OnNewUser()
{
QString name;
bool ok;
name = QInputDialog::getText (0, _("New User"), "", QLineEdit::Normal, "", &ok);
if (!ok)
return;
if (!name.size()) return;
if (_kiss->UserExists(name))
{
QMessageBox::critical(0, _("Error"), _("User ") + name + _(" already exists"));
return;
}
_kiss->NewUser(name);
_kiss->LoadUser(name);
close();
}