/***************************************************************************
                          dchubprofile.cpp  -  description
                             -------------------
    begin                : Fre Sep 20 2002
    copyright            : (C) 2002 by Mathias Küster
    email                : mathen@users.berlios.de
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   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.                                   *
 *                                                                         *
 ***************************************************************************/

#include <qpushbutton.h>
#include <qcombobox.h>
#include <qcheckbox.h>
#include <qinputdialog.h>
#include <qmessagebox.h>

#include "dcconfig.h"

#include <dclib/core/cstringlist.h>

#include "dchubprofile.h"

/** */
DCHubProfile::DCHubProfile(QWidget *parent, const char *name ) : QDialog(parent,name)
{
	setupUi(this);
	
	InitDocument();
}

/** */
DCHubProfile::~DCHubProfile()
{
}

/** */
void DCHubProfile::InitDocument()
{
	CStringList list;
	DCConfigHubProfile * pConfigHubProfile = 0;

	ComboBox_PROFILE->clear();

 	g_pConfig->GetHubProfileList( &list );

	while ( list.Next((CObject*&)pConfigHubProfile) )
	{
		if ( pConfigHubProfile->m_sName != "" )
		{
			ComboBox_PROFILE->insertItem(pConfigHubProfile->m_sName.Data());
		}
	}

	connect( PushButton_OK, SIGNAL(clicked()), this, SLOT(slotClickedOK()) );
	connect( PushButton_CANCEL, SIGNAL(clicked()), this, SLOT(reject()) );
	connect( ComboBox_PROFILE, SIGNAL(activated( const QString & )), this, SLOT(slotProfileActiveted( const QString & )) );
	connect( PushButton_DELETE, SIGNAL(clicked()), this, SLOT(slotProfileDelete()) );

	slotProfileActiveted("");
}

/** */
void DCHubProfile::SaveProfile( QString profilename )
{
	DCConfigHubProfile pConfigHubProfile;

	if ( profilename != "" )
	{
		pConfigHubProfile.m_sName        = profilename.toAscii().data();
		pConfigHubProfile.m_sNick        = LineEdit_NICK->text().replace( " ", "\xa0").toAscii().data();
		pConfigHubProfile.m_sPassword    = LineEdit_PASSWORD->text().toAscii().data();
		pConfigHubProfile.m_bAutoConnect = CheckBox_AUTOCONNECT->isChecked();
		pConfigHubProfile.m_bTag         = CheckBox_DESCRIPTIONTAG->isChecked();
		pConfigHubProfile.m_bExtHubCount = CheckBox_EXT_HUB_COUNT->isChecked();
		pConfigHubProfile.m_sComment     = LineEdit_PROF_DESCRIPTION->text().toAscii().data();
		pConfigHubProfile.m_bComment     = CheckBox_DESCRIPTION->isChecked();
		pConfigHubProfile.m_sEMail       = LineEdit_EMAIL->text().toAscii().data();
		pConfigHubProfile.m_bEMail       = CheckBox_EMAIL->isChecked();
		pConfigHubProfile.m_bSSL         = CheckBox_SSL->isChecked();
		pConfigHubProfile.m_sSuppressedNicks = LineEdit_SUPPRESS_NICKS->text().toAscii().data();
		
		g_pConfig->AddHubProfile( &pConfigHubProfile );
	}

	g_pConfig->SaveHubProfile();
}

/** */
void DCHubProfile::slotClickedOK()
{
	SaveProfile(ComboBox_PROFILE->currentText());

	accept();
}

/** */
void DCHubProfile::slotProfileActiveted( const QString & /*s*/ )
{
	DCConfigHubProfile pConfigHubProfile;

	// save old profile
	if ( m_sOldName != "" )
	{
		// check for changes
		if ( g_pConfig->GetHubProfile( m_sOldName.toAscii().data(), &pConfigHubProfile ) == TRUE )
		{
			if ( (pConfigHubProfile.m_sNick != LineEdit_NICK->text().replace( " ", "\xa0").toAscii().data()) ||
			     (pConfigHubProfile.m_sPassword != LineEdit_PASSWORD->text().toAscii().data()) ||
			     (pConfigHubProfile.m_bAutoConnect != CheckBox_AUTOCONNECT->isChecked()) ||
			     (pConfigHubProfile.m_bTag != CheckBox_DESCRIPTIONTAG->isChecked()) ||
			     (pConfigHubProfile.m_bExtHubCount != CheckBox_EXT_HUB_COUNT->isChecked()) ||
			     (pConfigHubProfile.m_sEMail != LineEdit_EMAIL->text().toAscii().data()) ||
			     (pConfigHubProfile.m_sComment != LineEdit_PROF_DESCRIPTION->text().toAscii().data()) ||
			     (pConfigHubProfile.m_bComment != CheckBox_DESCRIPTION->isChecked()) ||
			     (pConfigHubProfile.m_bEMail != CheckBox_EMAIL->isChecked()) ||
			     (pConfigHubProfile.m_bSSL   != CheckBox_SSL->isChecked()) ||
			     (pConfigHubProfile.m_sSuppressedNicks != LineEdit_SUPPRESS_NICKS->text().toAscii().data()) )
			{
				// ask user to save changes
				switch( QMessageBox::warning( this, tr("Save profile"),
	        			tr("Do you want to save your changes?"),
	        			tr("Save"),
        				tr("Cancel"), 0, 0, 1 ) )
				{
			    		case 1: // The user clicked the Quit or pressed Escape
        					break;
					default:
						SaveProfile(m_sOldName);
						break;
				}
			}
		}
	}

	// set new profile name
	m_sOldName = ComboBox_PROFILE->currentText();

	// reset values
	LineEdit_NICK->setText("");
	LineEdit_PASSWORD->setText("");
	CheckBox_AUTOCONNECT->setChecked(FALSE);
	CheckBox_DESCRIPTIONTAG->setChecked(FALSE);
	CheckBox_EXT_HUB_COUNT->setChecked(FALSE);
	LineEdit_EMAIL->setText("");
	LineEdit_PROF_DESCRIPTION->setText("");
	LineEdit_SUPPRESS_NICKS->setText("Hub");
	
	if ( m_sOldName != "" )
	{
		if ( g_pConfig->GetHubProfile( m_sOldName.toAscii().data(), &pConfigHubProfile ) == TRUE )
		{
			LineEdit_NICK->setText( pConfigHubProfile.m_sNick.Data() );
			LineEdit_PASSWORD->setText( pConfigHubProfile.m_sPassword.Data() );
			CheckBox_AUTOCONNECT->setChecked( pConfigHubProfile.m_bAutoConnect );
			CheckBox_DESCRIPTIONTAG->setChecked( pConfigHubProfile.m_bTag );
			CheckBox_EXT_HUB_COUNT->setChecked( pConfigHubProfile.m_bExtHubCount );
			LineEdit_EMAIL->setText( pConfigHubProfile.m_sEMail.Data() );
			CheckBox_EMAIL->setChecked( pConfigHubProfile.m_bEMail );
			LineEdit_PROF_DESCRIPTION->setText( pConfigHubProfile.m_sComment.Data() );
			CheckBox_DESCRIPTION->setChecked( pConfigHubProfile.m_bComment );
			CheckBox_SSL->setChecked(pConfigHubProfile.m_bSSL);
			LineEdit_SUPPRESS_NICKS->setText( pConfigHubProfile.m_sSuppressedNicks.Data() );
		}
	}
}

/** Deletes the selected profile, asking for confirmation */
void DCHubProfile::slotProfileDelete()
{
	// return value for confirmation dialog
	int ret = 0;
	
	// get currently selected profile name
	CString profname = ComboBox_PROFILE->currentText().toAscii().data();
	
	// ask for confirmation
	ret = QMessageBox::question(this,
				    "Are you sure?",
				    QString("Delete profile for ") + QString(profname.Data()),
				    QMessageBox::Yes,
				    QMessageBox::No
				    );
	
	if (ret == QMessageBox::Yes)
	{
		// remove profile from list
		ComboBox_PROFILE->removeItem( ComboBox_PROFILE->currentItem() );
		this->slotProfileActiveted("");
		
		// delete profile
		g_pConfig->DelHubProfile(profname);
		
		// save changes (saving is also done after adding a profile)
		g_pConfig->SaveHubProfile();
	}
}
