/*
vim: ts=4 ft=cpp

Copyright (c) 2002, Mattias Douhan
 All rights reserved.

Redistribution and use in source and binary forms,
with or without modification,
are permitted provided that the following conditions are met:

Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.

Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED.
IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

/************* krdesktop.cc ******************/
#include "krdesktop.moc"
#include <kmessagebox.h>
#include <qwidget.h>
#include <kprocess.h>
#include <qobject.h>
#include <qcheckbox.h>
#include <kmenubar.h>
#include <qpopupmenu.h>
#include <khelpmenu.h>
#include <klineedit.h>
#include <qbuttongroup.h>
#include <qradiobutton.h>
#include <qgroupbox.h>
#include <kstandarddirs.h>
#include <kiconloader.h>
#include <stdlib.h>
#include <qdir.h>
#include <qmap.h>
#include <qcombobox.h>
#include <qdatetime.h>

KRdeskTop::KRdeskTop() 
{
	btnConnect = new QPushButton("Connect", this);
	btnConnect->setGeometry(45,45,65,25);
	btnConnect->show();
	connect(btnConnect, SIGNAL(clicked()), this, SLOT(slotConnect()));
	
	kb_comboBox = new QComboBox( this );
	kb_comboBox->setGeometry(45,170,100,25);
	kb_comboBox->show();
		kb_comboBox->insertItem( "Swedish" );
		kb_comboBox->insertItem( "Finnish" );
		kb_comboBox->insertItem( "US" );
		kb_comboBox->insertItem( "German" );


	kb_map[ "Swedish" ] = "SV";
	kb_map[ "Finnish" ] = "FI";
	kb_map[ "US" ] = "US";
	kb_map[ "German" ] = "DE";

	oldver_kb_map[ "Swedish" ] = "0x41D";
        oldver_kb_map[ "Finnish" ] = "0x40B";
        oldver_kb_map[ "US" ] = "0x409";
        oldver_kb_map[ "German" ] = "0x407";


	grp2 = new QButtonGroup(1, QGroupBox::Horizontal, "rdesktop version", this);
	grp2->setGeometry(45,80,250,75);
	grp2->show();
		rb21 = new QRadioButton( "ver 1.1.0-pl19-8-5 or later", grp2);
		rb21->setChecked ( TRUE );
		rb22 = new QRadioButton( "PRE ver 1.1.0-pl19-8-5", grp2);

	grp3 = new QButtonGroup(1, QGroupBox::Horizontal, "Terminal Server Options", this);
	grp3->setGeometry(350,170,210,280);
	grp3->show();
		NT4 = new QCheckBox("Terminal server runs NT4", grp3);
		fullScreen = new QCheckBox("Full Screen", grp3);
		disEncrypt = new QCheckBox("Disable encryption", grp3);
		fbu = new QCheckBox("Force Bitmap Updates", grp3);
		noMotion = new QCheckBox("Don't send motion events", grp3);
		noLicense = new QCheckBox("Do not request license", grp3);
		userName= new QCheckBox("Administrator login", grp3);
		alttab = new QCheckBox("Obey WM alt+tab", grp3);
		nonpckb = new QCheckBox("Use a non PC keyboard", grp3);
		nobuiltinlic = new QCheckBox("Send --built-in-license", grp3);
		randomhostname = new QCheckBox("Randomize hostname", grp3);

	grp4 = new QButtonGroup(1, QGroupBox::Horizontal, "VNC Options", this);
        grp4->setGeometry(350,80,210,75);
        grp4->show();
		VNCCon = new QCheckBox("Connect using VNC", grp4);

	grp5 = new QButtonGroup(1, QGroupBox::Vertical, "Screen Geometry", this);
	grp5->setGeometry(45,450,515,75);
	grp5->show();
		rb51 = new QRadioButton( "640x480", grp5);
		rb52 = new QRadioButton( "800x600", grp5);
		rb53 = new QRadioButton( "1024x768", grp5);
		rb54 = new QRadioButton( "1280x1024", grp5);


	lineEdit = new KLineEdit("Server", this);
	lineEdit->setGeometry(350,45,100,25);
  	lineEdit->show();

	file = new QPopupMenu();
	file->insertItem("&Connect", this, SLOT(slotConnect()));
	file->insertItem(SmallIcon("exit"),"&Exit", this, SLOT(slotExit()));

	help = new QPopupMenu();
	help->insertItem("&About", this, SLOT(slotHelp()));

	menu = menuBar();
	menu->insertItem("&File", file);

	menutwo = menuBar();
	menutwo->insertItem("&Help", help);

}

void KRdeskTop::closeEvent(QCloseEvent *e)
{
	kapp->beep();
	KMainWindow::closeEvent(e);
}

void KRdeskTop::slotHelp()
{
	KMessageBox::messageBox((QWidget *)0L, KMessageBox::Information,
		"KRdeskTop, by Matt Douhan");
}


void KRdeskTop::slotConnect()
{
	// Check if rdesktop is available
	if (KStandardDirs::findExe(QString::fromLatin1("rdesktop")).isEmpty())
	{
		KMessageBox::messageBox((QWidget *)0L, KMessageBox::Information,
			"rdesktop cannot be found!, make sure it is installed and located"
			"in your $PATH, usually it should reside in /usr/local/bin");
		return;
	}

	// Check if VNC is available
        if (KStandardDirs::findExe(QString::fromLatin1("vncviewer")).isEmpty())
        {
                KMessageBox::messageBox((QWidget *)0L, KMessageBox::Information,
                        "vncviewer cannot be found!, make sure it is installed and located"
                        "in your $PATH, usually it should reside in /usr/X11R6/bin");
                return;
        }


	if (lineEdit->text() == "") 
	{
		KMessageBox::messageBox((QWidget *)0L, KMessageBox::Information,
			"Server name cannot be empty!");
		return;
	}

	if (lineEdit->text() == "Server")
	{
		int cont;
	
		cont = KMessageBox::questionYesNo(this, (
			"Do you really want to connect to a terminal"
			"server/VNC Server called *server*?"
			));

		if( cont != KMessageBox::Yes )
			return;
	}

	if (VNCCon->isChecked())
		{
			if (lineEdit->text().contains(":"))
			{
			}
			else
			{
			KMessageBox::messageBox((QWidget *)0L, KMessageBox::Information,
                        "When using VNC you must specify the DISPLAY as well as the servername or IP");
			return;
			}
		}

	if (VNCCon->isChecked())
                {
                        if (QFile::exists(QDir::homeDirPath() + "/.vnc/passwd"))
                        {
                        }
                        else
                        {
                        KMessageBox::messageBox((QWidget *)0L, KMessageBox::Information,
                        "When using VNC you must have a ~/.vnc/passwd file, create this file using vncpasswd");
                        return;
                        }
                }



	QString s; KProcess *p; QString keyboardLayout; QDateTime var_randomhostname;
	s = lineEdit->text();
	p = new KProcess;
	keyboardLayout = kb_comboBox->currentText();
	var_randomhostname = QDateTime::currentDateTime();
	var_randomhostname.toString("hh:mm:ss:zzz");

	connect( p, SIGNAL( processExited( KProcess * ) ), this, SLOT( slotCheck( KProcess * ) ) );

	if (VNCCon->isChecked())
                {
		*p << "vncviewer";
		}
		else 
		{
		*p << "rdesktop";
		}

if (VNCCon->isChecked())
 {
 }
else
 {


	*p << "-k";


	if ( rb21->isChecked()) *p << kb_map[keyboardLayout]; 
	else *p << oldver_kb_map[keyboardLayout];


	if (rb51->isChecked())
		*p << "-g" << "640x480+0+0";
	if (rb52->isChecked())
		*p << "-g" << "800x600+0+0";
	if (rb53->isChecked())
		*p << "-g" << "1024x768+0+0";
	if (rb54->isChecked())
		*p << "-g" << "1280x1024+0+0";
	

 }

	if (userName->isChecked())
		*p << "-u" << "Administrator";
	if (alttab->isChecked())
		*p << "-K";
	if (fullScreen->isChecked())
		*p << "-F";
	if (disEncrypt->isChecked())
		*p << "-e";
	if (fbu->isChecked())
		*p << "--no-bitmapcache";
	if (noMotion->isChecked())
		*p << "--no-motionevents";
	if (noLicense->isChecked())
		*p << "-l";
	if (nonpckb->isChecked())
		*p << "-N";
	if (nobuiltinlic->isChecked())
		*p << "--built-in-license";
	if (NT4->isChecked())
		*p << "-4";
	if (randomhostname->isChecked())
		*p << "-n" << "var_randomhostname";
	if (VNCCon->isChecked())
                {
		*p << "-passwd" << QDir::homeDirPath() + "/.vnc/passwd" << s ;
		}
		else
		{
		*p << s ;
		}

	p->start();

}

void KRdeskTop::slotCheck(KProcess *p)
{
        if (p->exitStatus() != 0)
        {
           KMessageBox::messageBox((QWidget *)0L, KMessageBox::Information,
                 "Connection failed!"
                 " is the server name or IP address correct? or if not VNC did you select the wrong version of rdesktop?");
           return;
        }
}

void KRdeskTop::slotExit()
{
	close();
}
