/*
 * This file is part of DesktopBSD, see the README file.
 *
 * Authors: Reinhard Pointner
 *			 Peter Hofer <ph@desktopbsd.net>
 *			 (C) 2004, 2005
 *
 * Copyright: BSD, see the COPYING file included in this distribution
 *
 */

/* Users */
#include <user.h>
#include <users.h>
#include <frmchangepasswd.h>

/* DesktopBSD */
#include <dbsdglobal.h>
#include <dialogs.h>

/* Qt */
#include <qvalidator.h>

void frmAddUser::initGraphics() {
	DBSDIconLoader *iloader = DBSDGlobal::getIconLoader();
	pixIcon->setPixmap(iloader->loadIcon("personal", DBSDIconLoader::Size32x32));
	btnAdd->setIconSet(iloader->loadIcon("button_ok", DBSDIconLoader::Button));
	btnCancel->setIconSet(iloader->loadIcon("button_cancel", DBSDIconLoader::Button));
}


void frmAddUser::init() {
	initGraphics();

	QRegExp userExpr("[A-Za-z0-9_]+");
	QValidator* userVal = new QRegExpValidator(userExpr, this);
	editUsername->setValidator(userVal);

	QRegExp commExpr("(\\w|\\s)+");
	QValidator* commVal = new QRegExpValidator(commExpr, this);
	editComment->setValidator(commVal);
}


void  frmAddUser::verify() {
	QString username = editUsername->text();
	QString fullname = editComment->text();

	if (!username.isEmpty()) {
		User	*user;
		if (Users::addUser(username, fullname, QStringList(), "/usr/local/bin/bash")  &&
				  ((user = Users::getUser(username)) != NULL))
		{
			if(DBSDGlobal::isNativeDesktopBSD())
				user->setGroupMemberships("user");

			frmChangePasswd dlg(this);
			dlg.setUser(*user);
			dlg.setCancelEnabled(false);
			dlg.exec();
			accept();
		} else
			Dialogs::warnDlg(tr("An error occured when adding the user."));
	} else
		Dialogs::infoDlg(tr("Please enter a valid user name."));
}


QString frmAddUser::getUsername() {
	return editUsername->text();
}
