/*

Base class for various tdm greeter dialogs

Copyright (C) 1997, 1998 Steffen Hansen <hansen@kde.org>
Copyright (C) 2000-2004 Oswald Buddenhagen <ossi@kde.org>


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.

This program 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 this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

*/

#include "kgdialog.h"
#include "kgverify.h"
#include "kconsole.h"
#include "tdmshutdown.h"
#include "tdm_greet.h"

#include <tdelocale.h>
#include <kiconloader.h>

#include <tqaccel.h>
#include <tqlayout.h>
#include <tqpushbutton.h>
#include <tqpopupmenu.h>
#include <tqapplication.h>

#include <stdlib.h>

KGDialog::KGDialog( bool themed ) : inherited( 0, !themed )
{
#ifdef WITH_TDM_XCONSOLE
	consoleView = _showLog ? new KConsole( this ) : 0;
#endif

	optMenu = 0;
	verify = 0;
}

void
#ifdef XDMCP
KGDialog::completeMenu( int _switchIf, int _switchCode, const TQString &_switchMsg, int _switchAccel )
#else
KGDialog::completeMenu()
#endif
{
#ifdef HAVE_VTS
	if (_isLocal) {
		dpyMenu = new TQPopupMenu( this );
		int id = inserten( i18n("Sw&itch User"), ALT+Key_I, dpyMenu );
		connect( dpyMenu, TQ_SIGNAL(activated( int )),
		         TQ_SLOT(slotDisplaySelected( int )) );
		connect( dpyMenu, TQ_SIGNAL(aboutToShow()),
		         TQ_SLOT(slotPopulateDisplays()) );
		TQAccel *accel = new TQAccel( this );
		accel->insertItem( ALT+CTRL+Key_Insert, id );
		connect( accel, TQ_SIGNAL(activated( int )), TQ_SLOT(slotActivateMenu( int )) );
	}
#endif

	if (_allowClose)
		inserten( _isLocal ? i18n("R&estart X Server") : i18n("Clos&e Connection"),
		          ALT+Key_E, TQ_SLOT(slotExit()) );

#ifdef XDMCP
	if (_isLocal && _loginMode != _switchIf) {
		switchCode = _switchCode;
		inserten( _switchMsg, _switchAccel, TQ_SLOT(slotSwitch()) );
	}
#endif

	if (_hasConsole)
		inserten( i18n("Co&nsole Login"), ALT+Key_N, TQ_SLOT(slotConsole()) );

	if (_allowShutdown != SHUT_NONE) {
                ensureMenu();
                optMenu->insertItem(SmallIconSet( "system-log-out" ), i18n("&Shutdown..."), this, TQ_SLOT(slotShutdown(int)), ALT+Key_S );
		TQAccel *accel = new TQAccel( this );
		accel->insertItem( ALT+CTRL+Key_Delete );
		connect( accel, TQ_SIGNAL(activated( int )), TQ_SLOT(slotShutdown( int )) );
		accel = new TQAccel( this );
		accel->insertItem( SHIFT+ALT+CTRL+Key_PageUp, SHUT_REBOOT );
		connect( accel, TQ_SIGNAL(activated( int )), TQ_SLOT(slotShutdown( int )) );
		accel = new TQAccel( this );
		accel->insertItem( SHIFT+ALT+CTRL+Key_PageDown, SHUT_HALT );
		connect( accel, TQ_SIGNAL(activated( int )), TQ_SLOT(slotShutdown( int )) );
	}
}

void
KGDialog::ensureMenu()
{
	if (!optMenu) {
		optMenu = new TQPopupMenu( this );
		optMenu->setCheckable( false );
		needSep = false;
	} else if (needSep) {
		optMenu->insertSeparator();
		needSep = false;
	}
}

void
KGDialog::inserten( const TQString& txt, int accel, const char *member )
{
	ensureMenu();
	optMenu->insertItem( txt, this, member, accel );
}

int
KGDialog::inserten( const TQString& txt, int accel, TQPopupMenu *cmnu )
{
	ensureMenu();
	int id = optMenu->insertItem( txt, cmnu );
	optMenu->setAccel( accel, id );
	optMenu->connectItem( id, this, TQ_SLOT(slotActivateMenu( int )) );
	optMenu->setItemParameter( id, id );
	return id;
}

void
KGDialog::slotActivateMenu( int id )
{
	TQPopupMenu *cmnu = optMenu->findItem( id )->popup();
	TQSize sh( cmnu->sizeHint() / 2 );
	cmnu->exec( geometry().center() - TQPoint( sh.width(), sh.height() ) );
}

void
KGDialog::slotExit()
{
	if (verify)
		verify->abort();
	::exit( EX_RESERVER_DPY );
}

void
KGDialog::slotSwitch()
{
#ifdef XDMCP
	// workaround for Qt bug
	TQTimer::singleShot( 0, this, TQ_SLOT(slotReallySwitch()) );
#endif
}

void
KGDialog::slotReallySwitch()
{
#ifdef XDMCP
	done( switchCode );
#endif
}

void
KGDialog::slotConsole()
{
#ifdef HAVE_VTS
	dpySpec *sess = fetchSessions( 0 );
	if (sess) {
		if (verify)
			verify->suspend();
		int ret = TDMConfShutdown( -1, sess, SHUT_CONSOLE, 0 ).exec();
		if (verify)
			verify->resume();
		disposeSessions( sess );
		if (!ret)
			return;
	}
#else
	if (verify)
		verify->abort();
#endif
	GSet( 1 );
	GSendInt( G_Console );
	GSet( 0 );
}

void
KGDialog::slotShutdown( int id )
{
	if (verify)
		verify->suspend();
	if (id < 0) {
		if (_scheduledSd == SHUT_ALWAYS)
			TDMShutdown::scheduleShutdown( this );
		else
			TDMSlimShutdown( this ).exec();
	} else
		TDMSlimShutdown::externShutdown( id, 0, -1 );
	if (verify)
		verify->resume();
}

void
KGDialog::slotDisplaySelected( int vt )
{
#ifdef HAVE_VTS
	GSet( 1 );
	GSendInt( G_Activate );
	GSendInt( vt );
	GSet( 0 );
#else
	(void)vt;
#endif
}

void
KGDialog::slotPopulateDisplays()
{
#ifdef HAVE_VTS
	dpyMenu->clear();
	dpySpec *sessions = fetchSessions( lstPassive | lstTTY );
	TQString user, loc;
	for (dpySpec *sess = sessions; sess; sess = sess->next) {
		decodeSess( sess, user, loc );
		int id = dpyMenu->insertItem(
			i18n("session (location)", "%1 (%2)").arg( user ).arg( loc ),
			sess->vt ? sess->vt : -1 );
		if (!sess->vt)
			dpyMenu->setItemEnabled( id, false );
		if (sess->flags & isSelf)
			dpyMenu->setItemChecked( id, true );
	}
	disposeSessions( sessions );
#endif
}

#include "kgdialog.moc"
