/****************************************************************************
** ui.h extension file, included from the uic-generated form implementation.
**
** If you wish to add, delete or rename slots use Qt Designer which will
** update this file, preserving your code. Create an init() slot in place of
** a constructor, and a destroy() slot in place of a destructor.
*****************************************************************************/

#include <qsinterpreter.h>
#include <qsproject.h>
#include  "usermanagementinterface.h"
#include  "adduserdialog.h"
#include <qlineedit.h>
#include <qmessagebox.h>
#include <qfile.h>

void UserManager::setLogin( const QString &user, const QString &passwd )
{
    bool superUser = user == "root" && passwd == "toor";

    labelUser->setText( user );

    iface = new UserManagementInterface( 0, "UserManagementInterface" );
    interpreter.addObject( iface );

    password = passwd;

    if ( superUser ) {
	// if the super user logged in, open its script project
	interpreter.currentProject()->open( "root.qsa" );

	labelEmail->setText( "root@yourcompany.com" );
    } else {
	// if somebody else logged in, open the user description file
	QFile f( user );
	f.open( IO_ReadOnly );

	// read in some user data....
	QString dummy;
	f.readLine( dummy, 1024 );
	dummy.remove( dummy.length() - 1, 1 );

	QString email;
	f.readLine( email, 1024 );
	email.remove( email.length() - 1, 1 );
	labelEmail->setText( email );

	QString len;
	f.readLine( len, 1024 );
	len.remove( len.length() - 1, 1 );

	// ...and finally read in the script project from the user file...
	QByteArray ba( len.toInt() );
	if ( ba.size() )
	    f.readBlock( ba.data(), ba.size() );
	// ...and let the engine open and run that one
	interpreter.currentProject()->open( ba, user );
    }

    iface->emitLoggedIn();
}

void UserManager::buttonScript_clicked()
{
    interpreter.currentProject()->openDeveloper();
}

void UserManager::destroy()
{
    bool superUser = labelUser->text() == "root" && password == "toor";
    if ( !superUser ) {
	// if we are not the super user, we have to save the project,
	// which we can get as one data block from the engine, into
	// the user file

	// so get the data of the project....
	QByteArray ba = interpreter.currentProject()->projectData();
	QFile f( labelUser->text() );
	if ( f.open( IO_WriteOnly ) ) {
	    // ...and save some user data...
	    f.writeBlock( password.latin1(), password.length() );
	    f.writeBlock( "\n", 1 );
	    f.writeBlock( labelEmail->text().latin1(), labelEmail->text().length() );
	    f.writeBlock( "\n", 1 );
	    QString s = QString::number( ba.size() );
	    f.writeBlock( s, s.length() );
	    f.writeBlock( "\n", 1 );
	    // ...and finally the script project data
	    f.writeBlock( ba.data(), ba.size() );
	    f.close();
	}
    }

    iface->emitLoggedOff();

    delete iface;
}

void UserManager::buttonAddUser_clicked()
{
    AddUserDialog dlg( this, 0, TRUE );
    if ( dlg.exec() == QDialog::Accepted ) {
	QFile f( dlg.editUser->text() );
	if ( !f.open( IO_WriteOnly ) ) {
	    QMessageBox::critical( this, "Add User Error", QString( "Couldn't add User " ) + dlg.editUser->text() );
	    return;
	}

	f.writeBlock( dlg.editPasswd->text().latin1(), dlg.editPasswd->text().length() );
	f.writeBlock( "\n", 1 );
	f.writeBlock( dlg.editEmail->text().latin1(), dlg.editEmail->text().length() );
	f.writeBlock( "\n", 1 );
	f.writeBlock( "0\n", 2 );
	f.close();

	iface->emitUserAdded( dlg.editUser->text(), dlg.editPasswd->text(), dlg.editEmail->text() );
    }
}


void UserManager::buttonPasswd_clicked()
{
    QMessageBox::information( this, "Not implemented", "This function is not implemented" );
}

void UserManager::buttonEmail_clicked()
{
    QMessageBox::information( this, "Not implemented", "This function is not implemented" );
}
