KissCount/src/view/UsersDialog.cpp

129 lines
3.5 KiB
C++
Raw Normal View History

2010-07-10 16:34:30 +02:00
/*
Copyright 2010-2011 Grégory Soutadé
2010-07-10 16:34:30 +02:00
This file is part of KissCount.
2010-07-10 16:34:30 +02:00
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.
2010-07-10 16:34:30 +02:00
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.
2010-07-10 16:34:30 +02:00
You should have received a copy of the GNU General Public License
along with KissCount. If not, see <http://www.gnu.org/licenses/>.
2010-07-10 16:34:30 +02:00
*/
#include "UsersDialog.h"
2010-07-04 16:33:25 +02:00
enum {BUTTON_OK_ID=1, BUTTON_CANCEL_ID, BUTTON_NEW_USER_ID};
BEGIN_EVENT_TABLE(UsersDialog, wxDialog)
2010-07-04 16:33:25 +02:00
EVT_BUTTON(BUTTON_OK_ID, UsersDialog::OnOK)
EVT_BUTTON(BUTTON_CANCEL_ID, UsersDialog::OnCancel)
EVT_BUTTON(BUTTON_NEW_USER_ID, UsersDialog::OnNewUser)
END_EVENT_TABLE()
UsersDialog::UsersDialog(KissCount* kiss, wxUI *parent) : wxDialog(&(*parent), -1, _("Users")), _kiss(kiss), _wxUI(parent)
{
wxGridBagSizer *gridBagSizer;
wxStaticText* label;
wxCommandEvent event;
2010-12-11 09:51:44 +01:00
wxBoxSizer *hbox = new wxBoxSizer(wxHORIZONTAL);
gridBagSizer = new wxGridBagSizer(4, 4);
2010-05-15 11:21:42 +02:00
label = new wxStaticText(this, -1, _("User "));
gridBagSizer->Add(label, wxGBPosition(0, 0));
_users = new wxChoice(this, wxID_ANY);
gridBagSizer->Add(_users, wxGBPosition(0, 1));
label = new wxStaticText(this, -1, _("Password "));
gridBagSizer->Add(label, wxGBPosition(1, 0));
_password = new wxTextCtrl(this, wxID_ANY);
gridBagSizer->Add(_password, wxGBPosition(1, 1));
2010-06-27 21:39:49 +02:00
_password->SetWindowStyle(wxTE_PASSWORD);
2010-06-27 21:39:49 +02:00
wxButton* ok = new wxButton(this, BUTTON_OK_ID, _("OK"));
wxButton* cancel = new wxButton(this, BUTTON_CANCEL_ID, _("Cancel"));
wxButton* newUser = new wxButton(this, BUTTON_NEW_USER_ID, _("New User"));
gridBagSizer->Add(ok, wxGBPosition(3, 1));
gridBagSizer->Add(cancel, wxGBPosition(3, 2));
gridBagSizer->Add(newUser, wxGBPosition(3, 3));
2010-06-27 21:39:49 +02:00
std::list<wxString> users_list = _kiss->GetUsers();
2010-06-27 21:39:49 +02:00
for(std::list<wxString>::iterator i = users_list.begin(); i != users_list.end(); i++)
2010-06-27 21:39:49 +02:00
{
_users->Append(*i);
2010-06-27 21:39:49 +02:00
}
_users->Select(0);
2010-12-11 09:51:44 +01:00
hbox->Add(gridBagSizer, 0, wxGROW|wxALL, 10);
SetSizer(hbox);
_users->SetFocus();
2010-12-11 09:51:44 +01:00
Fit();
Center();
2010-07-04 16:33:25 +02:00
if (users_list.size() == 0)
OnNewUser(event);
else
ShowModal();
}
void UsersDialog::OnOK(wxCommandEvent& event)
{
// No users in database
if (!_users->GetStringSelection().Length())
{
return;
}
if (!_kiss->IsValidUser(_users->GetStringSelection(), _password->GetLineText(0)))
{
wxMessageBox(_("Invalid password"), _("Error"), wxICON_ERROR | wxOK);
_password->Clear();
_password->SetFocus();
}
else
{
_kiss->LoadUser(_users->GetStringSelection());
Close();
}
}
void UsersDialog::OnCancel(wxCommandEvent& event)
{
Close();
}
2010-06-27 21:39:49 +02:00
void UsersDialog::OnNewUser(wxCommandEvent& event)
{
wxString name;
wxTextEntryDialog u(this, wxT(""), _("New User"));
2010-06-27 21:39:49 +02:00
if (u.ShowModal() == wxID_CANCEL)
return;
2010-06-27 21:39:49 +02:00
name = u.GetValue();
2010-06-27 21:39:49 +02:00
if (!name.size()) return;
2010-06-27 21:39:49 +02:00
if (_kiss->UserExists(name))
2010-06-27 21:39:49 +02:00
{
wxMessageBox(_("User ") + name + _(" already exists"), _("Error"), wxICON_ERROR | wxOK);
return;
2010-06-27 21:39:49 +02:00
}
_kiss->NewUser(name);
_kiss->LoadUser(name);
2010-06-27 21:39:49 +02:00
Close();
2010-06-27 21:39:49 +02:00
}