/***************************************************************************
                                trayicon.cpp
                             -------------------
    begin                : Tue Dec 11 2001
    copyright            : (C) 2001-2002 by Tim Jansen
    email                : tim@tjansen.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 "trayicon.h"
#include <tqtooltip.h>
#include <kstdaction.h>
#include <tdeapplication.h>
#include <tdelocale.h>
#include <kdialog.h>
#include <tdeglobal.h>
#include <kiconloader.h>
#include <tdepopupmenu.h>

KPassivePopup2::KPassivePopup2(TQWidget *parent) :
   KPassivePopup(parent){
}

void KPassivePopup2::hideEvent( TQHideEvent *e )
{
    KPassivePopup::hideEvent(e);
    emit hidden();
}

KPassivePopup2 *KPassivePopup2::message( const TQString &caption, const TQString &text,
				         const TQPixmap &icon,
				         TQWidget *parent)
{
    KPassivePopup2 *pop = new KPassivePopup2( parent);
    pop->setView( caption, text, icon );
    pop->show();

    return pop;
}


TrayIcon::TrayIcon(KDialog *d, Configuration *c) :
	KSystemTray(0, "krfb trayicon"),
	configuration(c),
	aboutDialog(d),
	actionCollection(this),
	quitting(false)
{
	TDEIconLoader *loader = TDEGlobal::iconLoader();
	trayIconOpen = loader->loadIcon("eyes-open24", TDEIcon::User);
	trayIconClosed = loader->loadIcon("eyes-closed24", TDEIcon::User);
	setPixmap(trayIconClosed);
	TQToolTip::add(this, i18n("Desktop Sharing - connecting"));

	manageInvitationsAction = new TDEAction(i18n("Manage &Invitations"), TQString(),
					      0, this, TQT_SIGNAL(showManageInvitations()),
					      &actionCollection);
	manageInvitationsAction->plug(contextMenu());

	contextMenu()->insertSeparator();

	enableControlAction = new TDEToggleAction(i18n("Enable Remote Control"));
	enableControlAction->setCheckedState(i18n("Disable Remote Control"));
	enableControlAction->plug(contextMenu());
	enableControlAction->setEnabled(false);
	connect(enableControlAction, TQT_SIGNAL(toggled(bool)), TQT_SIGNAL(enableDesktopControl(bool)));

	contextMenu()->insertSeparator();

	aboutAction = KStdAction::aboutApp(this, TQT_SLOT(showAbout()), &actionCollection);
	aboutAction->plug(contextMenu());

	show();
}

TrayIcon::~TrayIcon(){
}

void TrayIcon::showAbout() {
	aboutDialog->show();
}

void TrayIcon::prepareQuit() {
        quitting = true;
}



void TrayIcon::showConnectedMessage(TQString host) {

        setPixmap(trayIconOpen);
        KPassivePopup2::message(i18n("Desktop Sharing"),
				i18n("The remote user has been authenticated and is now connected."),
				trayIconOpen,
				this);
	TQToolTip::add(this, i18n("Desktop Sharing - connected with %1").arg(host));
}

void TrayIcon::showDisconnectedMessage() {
        if (quitting)
                return;

	TQToolTip::add(this, i18n("Desktop Sharing - disconnected"));
        setPixmap(trayIconClosed);
        KPassivePopup2 *p = KPassivePopup2::message(i18n("Desktop Sharing"),
						    i18n("The remote user has closed the connection."),
						    trayIconClosed,
						    this);
	connect(p, TQT_SIGNAL(hidden()), this, TQT_SIGNAL(diconnectedMessageDisplayed()));
}

void TrayIcon::setDesktopControlSetting(bool b) {
	enableControlAction->setEnabled(true);
	enableControlAction->setChecked(b);
}

void TrayIcon::mousePressEvent(TQMouseEvent *e)
{
        if (!rect().contains(e->pos()))
                return;

	if (e->button() == TQt::LeftButton) {
	        contextMenuAboutToShow(contextMenu());
		contextMenu()->popup(e->globalPos());
	}
	else
	        KSystemTray::mousePressEvent(e);
}

#include "trayicon.moc"
