/***************************************************************************
*   Copyright (C) 2003-2004 by                                            *
*   Unai Garro (ugarro@users.sourceforge.net)                             *
*   Jason Kivlighn (jkivlighn@gmail.com)                                  *
*                                                                         *
*   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.                                   *
***************************************************************************/

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <unistd.h>
#include <pwd.h>

#include <tqhbox.h>
#include <tqvgroupbox.h>
#include <tqlayout.h>
#include <tqpixmap.h>
#include <tqpushbutton.h>
#include <tqtooltip.h>

#include <tdeconfig.h>
#include <kdebug.h>
#include <tdeapplication.h>
#include <tdestandarddirs.h>
#include <tdelocale.h>
#include <kiconloader.h>
#include <tdefiledialog.h>

#include "setupwizard.h"

SetupWizard::SetupWizard( TQWidget *parent, const char *name, bool modal, WFlags f ) : KWizard( parent, name, modal, f )
{
	welcomePage = new WelcomePage( this );
	addPage( welcomePage, i18n( "Welcome to Krecipes" ) );

	dbTypeSetupPage = new DBTypeSetupPage( this );
	addPage( dbTypeSetupPage, i18n( "Database Type" ) );

	sqliteSetupPage = new SQLiteSetupPage( this );
	addPage( sqliteSetupPage, i18n( "Server Settings" ) );

	permissionsSetupPage = new PermissionsSetupPage( this );
	addPage( permissionsSetupPage, i18n( "Database Permissions" ) );

	pSqlPermissionsSetupPage = new PSqlPermissionsSetupPage( this );
	addPage( pSqlPermissionsSetupPage, i18n( "Database Permissions" ) );

	serverSetupPage = new ServerSetupPage( this );
	addPage( serverSetupPage, i18n( "Server Settings" ) );

	dataInitializePage = new DataInitializePage( this );
	addPage( dataInitializePage, i18n( "Initialize Database" ) );

	savePage = new SavePage( this );
	addPage( savePage, i18n( "Finish & Save Settings" ) );

	setFinishEnabled( savePage, true ); // Enable finish button
	setSizePolicy( TQSizePolicy::MinimumExpanding, TQSizePolicy::MinimumExpanding );

	#if (!HAVE_SQLITE3)
		#if (HAVE_MYSQL)
			showPages( MySQL );
		#else
		#if (HAVE_POSTGRESQL)
			showPages( PostgreSQL );
		#endif
		#endif
	#else
		showPages( SQLite );
	#endif

	connect( finishButton(), TQ_SIGNAL( clicked() ), this, TQ_SLOT( save() ) );
	connect( dbTypeSetupPage, TQ_SIGNAL( showPages( DBType ) ), this, TQ_SLOT( showPages( DBType ) ) );

}


SetupWizard::~SetupWizard()
{
}


void SetupWizard::showPages( DBType type )
{
	switch ( type ) {
	case MySQL:
		setAppropriate( serverSetupPage, true );
		setAppropriate( permissionsSetupPage, true );
		setAppropriate( pSqlPermissionsSetupPage, false );
		setAppropriate( sqliteSetupPage, false );
		break;
	case PostgreSQL:
		setAppropriate( serverSetupPage, true );
		setAppropriate( pSqlPermissionsSetupPage, true );
		setAppropriate( permissionsSetupPage, false );
		setAppropriate( sqliteSetupPage, false );
		break;
	case SQLite:
		setAppropriate( serverSetupPage, false );
		setAppropriate( permissionsSetupPage, false );
		setAppropriate( pSqlPermissionsSetupPage, false );
		setAppropriate( sqliteSetupPage, true );
		break;
	}
}


WelcomePage::WelcomePage( TQWidget *parent ) : TQWidget( parent )
{
	TQGridLayout * layout = new TQGridLayout( this, 1, 1, 0, 0 );
	TQSpacerItem *spacer_top = new TQSpacerItem( 10, 10, TQSizePolicy::Minimum, TQSizePolicy::Fixed );
	layout->addItem( spacer_top, 0, 1 );
	TQSpacerItem *spacer_left = new TQSpacerItem( 10, 10, TQSizePolicy::Fixed, TQSizePolicy::Minimum );
	layout->addItem( spacer_left, 1, 0 );
	TQPixmap logoPixmap ( locate( "data", "krecipes/pics/wizard.png" ) );
	logo = new TQLabel( this );
	logo->setPixmap( logoPixmap );
	logo->setSizePolicy( TQSizePolicy::Fixed, TQSizePolicy::Fixed );
	layout->addWidget( logo, 1, 1, TQt::AlignTop );

	TQSpacerItem *spacer_from_image = new TQSpacerItem( 10, 10, TQSizePolicy::Fixed, TQSizePolicy::Minimum );
	layout->addItem( spacer_from_image, 1, 2 );

	welcomeText = new TQLabel( this );
	welcomeText->setText( i18n( "<b><font size=\"+1\">Thank you very much for choosing Krecipes.</font></b><br>It looks like this is the first time you are using it. This wizard will help you with the initial setup so that you can start using it quickly.<br><br>Welcome, and enjoy cooking!" ) );
	welcomeText->setMinimumWidth( 200 );
	welcomeText->setMaximumWidth( 10000 );
	welcomeText->setSizePolicy( TQSizePolicy::MinimumExpanding, TQSizePolicy::Minimum );
	welcomeText->setAlignment( int( TQLabel::WordBreak | TQLabel::AlignTop ) );
	layout->addWidget( welcomeText, 1, 3 );

}

PermissionsSetupPage::PermissionsSetupPage( TQWidget *parent ) : TQWidget( parent )
{
	TQGridLayout * layout = new TQGridLayout( this, 1, 1, 0, 0 );
	TQSpacerItem *spacer_top = new TQSpacerItem( 10, 10, TQSizePolicy::Minimum, TQSizePolicy::Fixed );
	layout->addItem( spacer_top, 0, 1 );
	TQSpacerItem *spacer_left = new TQSpacerItem( 10, 10, TQSizePolicy::Fixed, TQSizePolicy::Minimum );
	layout->addItem( spacer_left, 1, 0 );


	// Logo
	TQPixmap permissionsSetupPixmap ( locate( "data", "krecipes/pics/dbpermissions.png" ) );
	logo = new TQLabel( this );
	logo->setPixmap( permissionsSetupPixmap );
	logo->setSizePolicy( TQSizePolicy::Fixed, TQSizePolicy::Fixed );
	layout->addMultiCellWidget( logo, 1, 8, 1, 1, TQt::AlignTop );

	// Spacer to separate the logo
	TQSpacerItem *logoSpacer = new TQSpacerItem( 10, 10, TQSizePolicy::Fixed, TQSizePolicy::Minimum );
	layout->addItem( logoSpacer, 1, 2 );


	// Explanation Text
	permissionsText = new TQLabel( this );
	permissionsText->setText( i18n( "This dialog will allow you to specify a MySQL account that has the necessary permissions to access the Krecipes MySQL database.<br><br><b><font size=\"+1\">Most users that use Krecipes and MySQL for the first time can just leave the default parameters and press \'Next\'.</font></b> <br><br>If you set a MySQL root password before, or you have already permissions as normal user, click on the appropriate option. Otherwise the account 'root' will be used, with no password.<br><br>[For security reasons, we strongly encourage you to setup a MySQL root password if you have not done so yet. Just type as root: mysqladmin password <i>your_password</i>]" ) );

	permissionsText->setSizePolicy( TQSizePolicy::MinimumExpanding, TQSizePolicy::Minimum );
	permissionsText->setAlignment( int( TQLabel::WordBreak | TQLabel::AlignTop ) );
	layout->addWidget( permissionsText, 1, 3 );

	// Text spacer
	TQSpacerItem *textSpacer = new TQSpacerItem( 10, 30, TQSizePolicy::Minimum, TQSizePolicy::Fixed );
	layout->addItem( textSpacer, 2, 3 );


	// "The user already has permissions" checkbox
	noSetupCheckBox = new TQCheckBox( i18n( "I have already set the necessary permissions" ), this, "noSetupCheckBox" );
	layout->addWidget( noSetupCheckBox, 3, 3 );

	TQSpacerItem *checkBoxSpacer = new TQSpacerItem( 10, 10, TQSizePolicy::Minimum, TQSizePolicy::Fixed );
	layout->addItem( checkBoxSpacer, 4, 3 );

	// root checkbox
	rootCheckBox = new TQCheckBox( i18n( "I have already set a MySQL root/admin account" ), this, "rootCheckBox" );
	layout->addWidget( rootCheckBox, 5, 3 );

	TQSpacerItem *rootInfoSpacer = new TQSpacerItem( 10, 20, TQSizePolicy::Minimum, TQSizePolicy::Fixed );
	layout->addItem( rootInfoSpacer, 6, 3 );

	// MySQL root/admin info
	TQGroupBox *rootInfoGBox = new TQGroupBox( this, "rootInfoGBox" );
	rootInfoGBox->setTitle( i18n( "MySQL Administrator Account" ) );
	rootInfoGBox->setEnabled( false ); // Disable by default
	rootInfoGBox->setColumns( 2 );
	rootInfoGBox->setInsideSpacing( 10 );
	layout->addWidget( rootInfoGBox, 7, 3 );

	// User Entry
	TQLabel *userLabel = new TQLabel( rootInfoGBox );
	userLabel->setText( i18n( "Username:" ) );
	userEdit = new KLineEdit( rootInfoGBox );
	userEdit->setText( "root" );

	// Password Entry
	TQLabel *passLabel = new TQLabel( rootInfoGBox );
	passLabel->setText( i18n( "Password:" ) );
	passEdit = new KLineEdit( rootInfoGBox );
	passEdit->setEchoMode( TQLineEdit::Password );

	// Bottom spacer
	TQSpacerItem *bottomSpacer = new TQSpacerItem( 10, 20, TQSizePolicy::Minimum, TQSizePolicy::MinimumExpanding );
	layout->addItem( bottomSpacer, 8, 1 );

	// Connect Signals & slots

	connect( rootCheckBox, TQ_SIGNAL( toggled( bool ) ), rootInfoGBox, TQ_SLOT( setEnabled( bool ) ) );
	connect( rootCheckBox, TQ_SIGNAL( toggled( bool ) ), this, TQ_SLOT( rootCheckBoxChanged( bool ) ) );
	connect( noSetupCheckBox, TQ_SIGNAL( toggled( bool ) ), this, TQ_SLOT( noSetupCheckBoxChanged( bool ) ) );
}

void PermissionsSetupPage::rootCheckBoxChanged( bool on )
{
	if ( on )
		noSetupCheckBox->setChecked( false ); // exclude mutually the options (both can be unset)
}

bool PermissionsSetupPage::doUserSetup()
{
	return ( !noSetupCheckBox->isChecked() );
}

bool PermissionsSetupPage::useAdmin()
{
	return ( rootCheckBox->isChecked() );
}

void PermissionsSetupPage::getAdmin( TQString &adminName, TQString &adminPass )
{
	adminName = userEdit->text();
	adminPass = passEdit->text();
}

void PermissionsSetupPage::noSetupCheckBoxChanged( bool on )
{
	if ( on )
		rootCheckBox->setChecked( false ); // exclude mutually the options (both can be unset)
}


PSqlPermissionsSetupPage::PSqlPermissionsSetupPage( TQWidget *parent ) : TQWidget( parent )
{
	TQGridLayout * layout = new TQGridLayout( this, 1, 1, 0, 0 );
	TQSpacerItem *spacer_top = new TQSpacerItem( 10, 10, TQSizePolicy::Minimum, TQSizePolicy::Fixed );
	layout->addItem( spacer_top, 0, 1 );
	TQSpacerItem *spacer_left = new TQSpacerItem( 10, 10, TQSizePolicy::Fixed, TQSizePolicy::Minimum );
	layout->addItem( spacer_left, 1, 0 );


	// Logo
	TQPixmap permissionsSetupPixmap ( locate( "data", "krecipes/pics/dbpermissions.png" ) );
	logo = new TQLabel( this );
	logo->setPixmap( permissionsSetupPixmap );
	logo->setSizePolicy( TQSizePolicy::Fixed, TQSizePolicy::Fixed );
	layout->addMultiCellWidget( logo, 1, 8, 1, 1, TQt::AlignTop );

	// Spacer to separate the logo
	TQSpacerItem *logoSpacer = new TQSpacerItem( 10, 10, TQSizePolicy::Fixed, TQSizePolicy::Minimum );
	layout->addItem( logoSpacer, 1, 2 );


	// Explanation Text
	permissionsText = new TQLabel( this );
	permissionsText->setText( i18n( "This dialog will allow you to specify a PostgreSQL account that has the necessary permissions to access the Krecipes PostgreSQL database.  This account may either be a <b>PostgreSQL superuser</b> or have the ability to both <b>create new PostgreSQL users and databases</b>.<br><br>If no superuser or privileged account is given, the account 'postgres' will be attempted, with no password.  If this is insufficient for your PostgreSQL setup, you <b>must</b> select the appropriate option below to enter the information of a privileged PostgreSQL account." ) );

	permissionsText->setSizePolicy( TQSizePolicy::MinimumExpanding, TQSizePolicy::Minimum );
	permissionsText->setAlignment( int( TQLabel::WordBreak | TQLabel::AlignTop ) );
	layout->addWidget( permissionsText, 1, 3 );

	// Text spacer
	TQSpacerItem *textSpacer = new TQSpacerItem( 10, 30, TQSizePolicy::Minimum, TQSizePolicy::Fixed );
	layout->addItem( textSpacer, 2, 3 );


	// "The user already has permissions" checkbox
	noSetupCheckBox = new TQCheckBox( i18n( "I have already set the necessary permissions" ), this, "noSetupCheckBox" );
	layout->addWidget( noSetupCheckBox, 3, 3 );

	TQSpacerItem *checkBoxSpacer = new TQSpacerItem( 10, 10, TQSizePolicy::Minimum, TQSizePolicy::Fixed );
	layout->addItem( checkBoxSpacer, 4, 3 );

	// root checkbox
	rootCheckBox = new TQCheckBox( i18n( "I have already set a superuser or privileged account" ), this, "rootCheckBox" );
	layout->addWidget( rootCheckBox, 5, 3 );

	TQSpacerItem *rootInfoSpacer = new TQSpacerItem( 10, 20, TQSizePolicy::Minimum, TQSizePolicy::Fixed );
	layout->addItem( rootInfoSpacer, 6, 3 );

	// MySQL root/admin info
	TQGroupBox *rootInfoGBox = new TQGroupBox( this, "rootInfoGBox" );
	rootInfoGBox->setTitle( i18n( "PostgreSQL Superuser or Privileged Account" ) );
	rootInfoGBox->setEnabled( false ); // Disable by default
	rootInfoGBox->setColumns( 2 );
	rootInfoGBox->setInsideSpacing( 10 );
	layout->addWidget( rootInfoGBox, 7, 3 );

	// User Entry
	TQLabel *userLabel = new TQLabel( rootInfoGBox );
	userLabel->setText( i18n( "Username:" ) );
	userEdit = new KLineEdit( rootInfoGBox );
	userEdit->setText( "postgres" );

	// Password Entry
	TQLabel *passLabel = new TQLabel( rootInfoGBox );
	passLabel->setText( i18n( "Password:" ) );
	passEdit = new KLineEdit( rootInfoGBox );
	passEdit->setEchoMode( TQLineEdit::Password );

	// Bottom spacer
	TQSpacerItem *bottomSpacer = new TQSpacerItem( 10, 20, TQSizePolicy::Minimum, TQSizePolicy::MinimumExpanding );
	layout->addItem( bottomSpacer, 8, 1 );

	// Connect Signals & slots

	connect( rootCheckBox, TQ_SIGNAL( toggled( bool ) ), rootInfoGBox, TQ_SLOT( setEnabled( bool ) ) );
	connect( rootCheckBox, TQ_SIGNAL( toggled( bool ) ), this, TQ_SLOT( rootCheckBoxChanged( bool ) ) );
	connect( noSetupCheckBox, TQ_SIGNAL( toggled( bool ) ), this, TQ_SLOT( noSetupCheckBoxChanged( bool ) ) );
}

void PSqlPermissionsSetupPage::rootCheckBoxChanged( bool on )
{
	if ( on )
		noSetupCheckBox->setChecked( false ); // exclude mutually the options (both can be unset)
}

bool PSqlPermissionsSetupPage::doUserSetup()
{
	return ( !noSetupCheckBox->isChecked() );
}

bool PSqlPermissionsSetupPage::useAdmin()
{
	return ( rootCheckBox->isChecked() );
}

void PSqlPermissionsSetupPage::getAdmin( TQString &adminName, TQString &adminPass )
{
	adminName = userEdit->text();
	adminPass = passEdit->text();
}

void PSqlPermissionsSetupPage::noSetupCheckBoxChanged( bool on )
{
	if ( on )
		rootCheckBox->setChecked( false ); // exclude mutually the options (both can be unset)
}


ServerSetupPage::ServerSetupPage( TQWidget *parent ) : TQWidget( parent )
{
	TQGridLayout * layout = new TQGridLayout( this, 1, 1, 0, 0 );
	TQSpacerItem *spacer_top = new TQSpacerItem( 10, 10, TQSizePolicy::Minimum, TQSizePolicy::Fixed );
	layout->addItem( spacer_top, 0, 1 );
	TQSpacerItem *spacer_left = new TQSpacerItem( 10, 10, TQSizePolicy::Fixed, TQSizePolicy::Minimum );
	layout->addItem( spacer_left, 1, 0 );


	// Image

	TQPixmap serverSetupPixmap ( locate( "data", "krecipes/pics/network.png" ) );
	logo = new TQLabel( this );
	logo->setPixmap( serverSetupPixmap );
	logo->setSizePolicy( TQSizePolicy::Fixed, TQSizePolicy::Fixed );
	layout->addMultiCellWidget( logo, 1, 8, 1, 1, TQt::AlignTop );

	TQSpacerItem *spacer_from_image = new TQSpacerItem( 10, 10, TQSizePolicy::Fixed, TQSizePolicy::Minimum );
	layout->addItem( spacer_from_image, 1, 2 );


	// Explanation text
	serverSetupText = new TQLabel( this );
	serverSetupText->setText( i18n( "In this dialog you can adjust the database server settings.<br><br><b>Warning: Passwords are stored in plain text and could potentially be compromised.  We recommend that you create a username and password combination solely for use by Krecipes.</b>" ) );
	serverSetupText->setSizePolicy( TQSizePolicy::MinimumExpanding, TQSizePolicy::Minimum );
	serverSetupText->setAlignment( int( TQLabel::AlignTop | TQLabel::WordBreak ) );
	layout->addWidget( serverSetupText, 1, 3 );

	// Text spacer

	TQSpacerItem* textSpacer = new TQSpacerItem( 10, 30, TQSizePolicy::Minimum, TQSizePolicy::Fixed );
	layout->addItem( textSpacer, 2, 3 );

	// Input Boxes

	TQGroupBox *inputGBox = new TQGroupBox( this, "inputGBox" );
	inputGBox->setFrameStyle( TQFrame::NoFrame );
	inputGBox->setInsideSpacing( 10 );
	inputGBox->setColumns( 2 );
	layout->addWidget( inputGBox, 3, 3 );

	// Username Input

	TQLabel* usernameText = new TQLabel( i18n( "Username:" ), inputGBox );
	usernameText->setFixedSize( TQSize( 100, 20 ) );
	usernameText->setSizePolicy( TQSizePolicy::Fixed, TQSizePolicy::Fixed );

	usernameEdit = new KLineEdit( inputGBox );
	usernameEdit->setFixedSize( TQSize( 120, 20 ) );
	usernameEdit->setSizePolicy( TQSizePolicy::Fixed, TQSizePolicy::Fixed );
	// get username
	uid_t userID;
	TQString username;
	struct passwd *user;
	userID = getuid();
	user = getpwuid ( userID );
	username = user->pw_name;

	usernameEdit->setText( username );


	// Password

	TQLabel* passwordText = new TQLabel( i18n( "Password:" ), inputGBox );
	passwordText->setFixedSize( TQSize( 100, 20 ) );
	passwordText->setSizePolicy( TQSizePolicy::Fixed, TQSizePolicy::Fixed );

	passwordEdit = new KLineEdit( inputGBox );
	passwordEdit->setEchoMode( TQLineEdit::Password );
	passwordEdit->setFixedSize( TQSize( 120, 20 ) );
	passwordEdit->setSizePolicy( TQSizePolicy::Fixed, TQSizePolicy::Fixed );

	// DB Name

	TQLabel* dbNameText = new TQLabel( i18n( "Database name:" ), inputGBox );
	dbNameText->setFixedSize( TQSize( 100, 20 ) );
	dbNameText->setSizePolicy( TQSizePolicy::Fixed, TQSizePolicy::Fixed );

	dbNameEdit = new KLineEdit( inputGBox );
	dbNameEdit->setFixedSize( TQSize( 120, 20 ) );
	dbNameEdit->setSizePolicy( TQSizePolicy::Fixed, TQSizePolicy::Fixed );
	dbNameEdit->setText( "Krecipes" );


	// Spacer from box
	TQSpacerItem* spacerFromBox = new TQSpacerItem( 10, 20, TQSizePolicy::Minimum, TQSizePolicy::Fixed );
	layout->addItem( spacerFromBox, 4, 3 );


	// Remote server checkbox

	remoteServerCheckBox = new TQCheckBox( i18n( "The server is remote" ), this, "remoteServerCheckBox" );
	layout->addWidget( remoteServerCheckBox, 5, 3 );

	// Spacer from CheckBox
	TQSpacerItem* spacerFromCheckBox = new TQSpacerItem( 10, 10, TQSizePolicy::Minimum, TQSizePolicy::Fixed );
	layout->addItem( spacerFromCheckBox, 6, 3 );

	// Server & Client Box
	TQGroupBox *serverSettingsGBox = new TQGroupBox( this, "serverSettingsGBox" );
	serverSettingsGBox->setTitle( i18n( "Server / Client Settings" ) );
	serverSettingsGBox->setEnabled( false ); // Disable by default
	serverSettingsGBox->setInsideSpacing( 10 );
	serverSettingsGBox->setColumns( 2 );
	layout->addWidget( serverSettingsGBox, 7, 3 );


	// Server
	( void ) new TQLabel( i18n( "Server:" ), serverSettingsGBox );
	serverEdit = new KLineEdit( serverSettingsGBox );
	serverEdit->setText( "localhost" );

	// Client
	( void ) new TQLabel( i18n( "Client:" ), serverSettingsGBox );
	clientEdit = new KLineEdit( serverSettingsGBox );
	clientEdit->setText( "localhost" );

	// Bottom Spacers

	TQSpacerItem* bottomSpacer = new TQSpacerItem( 10, 10, TQSizePolicy::Minimum, TQSizePolicy::MinimumExpanding );
	layout->addItem( bottomSpacer, 8, 1 );

	//TQSpacerItem* spacerRight = new TQSpacerItem( 10, 10, TQSizePolicy::MinimumExpanding, TQSizePolicy::Fixed );
	//layout->addItem( spacerRight, 7, 4 );

	// Signals & Slots
	connect( remoteServerCheckBox, TQ_SIGNAL( toggled( bool ) ), serverSettingsGBox, TQ_SLOT( setEnabled( bool ) ) );

}

TQString ServerSetupPage::server( void )
{
	return ( serverEdit->text() );
}

TQString ServerSetupPage::user( void )
{
	return ( usernameEdit->text() );
}

TQString ServerSetupPage::password( void )
{
	return ( passwordEdit->text() );
}

TQString ServerSetupPage::dbName( void )
{
	return ( dbNameEdit->text() );
}

void ServerSetupPage::getServerInfo( bool &isRemote, TQString &host, TQString &client, TQString &dbName, TQString &user, TQString &pass, int &port )
{
	isRemote = remoteServerCheckBox->isChecked();
	host = serverEdit->text();
	client = clientEdit->text();
	user = usernameEdit->text();
	pass = passwordEdit->text();
	dbName = dbNameEdit->text();
	port = 0;
}

SQLiteSetupPage::SQLiteSetupPage( TQWidget *parent ) : TQWidget( parent )
{
	TQGridLayout * layout = new TQGridLayout( this, 1, 1, 0, 0 );
	TQSpacerItem *spacer_top = new TQSpacerItem( 10, 10, TQSizePolicy::Minimum, TQSizePolicy::Fixed );
	layout->addItem( spacer_top, 0, 1 );
	TQSpacerItem *spacer_left = new TQSpacerItem( 10, 10, TQSizePolicy::Fixed, TQSizePolicy::Minimum );
	layout->addItem( spacer_left, 1, 0 );


	// Image

	TQPixmap serverSetupPixmap ( locate( "data", "krecipes/pics/network.png" ) );
	logo = new TQLabel( this );
	logo->setPixmap( serverSetupPixmap );
	logo->setSizePolicy( TQSizePolicy::Fixed, TQSizePolicy::Fixed );
	layout->addMultiCellWidget( logo, 1, 4, 1, 1, TQt::AlignTop );

	TQSpacerItem *spacer_from_image = new TQSpacerItem( 10, 10, TQSizePolicy::Fixed, TQSizePolicy::Minimum );
	layout->addItem( spacer_from_image, 1, 2 );


	// Explanation text
	serverSetupText = new TQLabel( this );
	serverSetupText->setText( i18n( "In this dialog you can adjust SQLite settings." ) );
	serverSetupText->setSizePolicy( TQSizePolicy::MinimumExpanding, TQSizePolicy::Minimum );
	serverSetupText->setAlignment( int( TQLabel::AlignTop | TQLabel::AlignJustify ) );
	layout->addWidget( serverSetupText, 1, 3 );

	// Text spacer

	TQSpacerItem* textSpacer = new TQSpacerItem( 10, 30, TQSizePolicy::Minimum, TQSizePolicy::Fixed );
	layout->addItem( textSpacer, 2, 3 );

	// Input Boxes

	TQHBox *hbox = new TQHBox( this );

	( void ) new TQLabel( i18n( "Database file:" ), hbox );

	fileEdit = new KLineEdit( hbox );
	fileEdit->setText( locateLocal ( "appdata", "krecipes.krecdb" ) );
	hbox->setStretchFactor( fileEdit, 2 );

	TDEIconLoader il;
	TQPushButton *file_select = new TQPushButton( il.loadIcon( "document-open", TDEIcon::NoGroup, 16 ), TQString::null, hbox );
	TQToolTip::add
		( file_select, i18n( "Open file dialog" ) );
	file_select->setFixedWidth( 25 );

	layout->addWidget( hbox, 3, 3 );

	// Bottom Spacers

	TQSpacerItem* bottomSpacer = new TQSpacerItem( 10, 10, TQSizePolicy::Minimum, TQSizePolicy::MinimumExpanding );
	layout->addItem( bottomSpacer, 4, 1 );

	connect( file_select, TQ_SIGNAL( clicked() ), TQ_SLOT( selectFile() ) );
}

TQString SQLiteSetupPage::dbFile( void ) const
{
	return ( fileEdit->text() );
}

void SQLiteSetupPage::selectFile()
{
	KFileDialog dialog( TQString::null, "*.*|All Files", this, "dialog", true );
	if ( dialog.exec() == TQDialog::Accepted ) {
		fileEdit->setText( dialog.selectedFile() );
	}
}


SavePage::SavePage( TQWidget *parent ) : TQWidget( parent )
{
	TQGridLayout * layout = new TQGridLayout( this, 1, 1, 0, 0 );
	TQSpacerItem *spacer_top = new TQSpacerItem( 10, 10, TQSizePolicy::Minimum, TQSizePolicy::Fixed );
	layout->addItem( spacer_top, 0, 1 );
	TQSpacerItem *spacer_left = new TQSpacerItem( 10, 10, TQSizePolicy::Fixed, TQSizePolicy::Minimum );
	layout->addItem( spacer_left, 1, 0 );

	TQPixmap logoPixmap ( locate( "data", "krecipes/pics/save.png" ) );
	logo = new TQLabel( this );
	logo->setPixmap( logoPixmap );
	logo->setSizePolicy( TQSizePolicy::Fixed, TQSizePolicy::Fixed );
	layout->addWidget( logo, 1, 1, TQt::AlignTop );

	TQSpacerItem *spacer_from_image = new TQSpacerItem( 10, 10, TQSizePolicy::Fixed, TQSizePolicy::Minimum );
	layout->addItem( spacer_from_image, 1, 2 );

	saveText = new TQLabel( this );
	saveText->setText( i18n( "Congratulations; all the necessary configuration setup is done. Press 'Finish' to continue, and enjoy cooking!" ) );
	saveText->setSizePolicy( TQSizePolicy::Minimum, TQSizePolicy::Minimum );

	saveText->setAlignment( int( TQLabel::WordBreak | TQLabel::AlignVCenter ) );
	layout->addWidget( saveText, 1, 3 );


}

void SetupWizard::save( void )
{
	kdDebug() << "Setting parameters in tdeconfig..." << endl;
	TDEConfig *config = tdeApp->config();

	// Save the database type
	TQString sDBType;

	switch ( dbTypeSetupPage->dbType() ) {
	case MySQL:
		sDBType = "MySQL";
		break;
	case PostgreSQL:
		sDBType = "PostgreSQL";
		break;
	default:
		sDBType = "SQLite";
		break;
	}

	config->setGroup( "DBType" );
	config->writeEntry( "Type", sDBType );
	kdDebug() << "DB type set in tdeconfig was... " << sDBType << endl;
	// Save the server data if needed
	if ( !( dbTypeSetupPage->dbType() == SQLite ) ) {
		config->setGroup( "Server" );
		config->writeEntry( "Host", serverSetupPage->server() );
		config->writeEntry( "Username", serverSetupPage->user() );
		config->writeEntry( "Password", serverSetupPage->password() );
		config->writeEntry( "DBName", serverSetupPage->dbName() );
		kdDebug() << "Finished setting the database parameters for MySQL or PostgreSQL (non SQLite)..." << endl;
	}
	else {
		config->setGroup( "Server" );
		config->writeEntry( "DBFile", sqliteSetupPage->dbFile() );
	}

	// Indicate that settings were already made

	config->setGroup( "Wizard" );
	config->writeEntry( "SystemSetup", true );
	config->writeEntry( "Version", "0.9" );
	kdDebug() << "Setting in tdeconfig the lines to disable wizard startup..." << sDBType << endl;
}

void SetupWizard::getOptions( bool &setupUser, bool &initializeData, bool &doUSDAImport )
{
	setupUser = permissionsSetupPage->doUserSetup() && pSqlPermissionsSetupPage->doUserSetup();
	initializeData = dataInitializePage->doInitialization();
	doUSDAImport = dataInitializePage->doUSDAImport();
}

void SetupWizard::getAdminInfo( bool &enabled, TQString &adminUser, TQString &adminPass, const TQString &dbType )
{
	enabled = permissionsSetupPage->useAdmin() || pSqlPermissionsSetupPage->useAdmin();
	if ( dbType == "MySQL" )
		permissionsSetupPage->getAdmin( adminUser, adminPass );
	else
		pSqlPermissionsSetupPage->getAdmin( adminUser, adminPass );
}

void SetupWizard::getServerInfo( bool &isRemote, TQString &host, TQString &client, TQString &dbName, TQString &user, TQString &pass, int &port )
{
	serverSetupPage->getServerInfo( isRemote, host, client, dbName, user, pass, port );
	if ( dbTypeSetupPage->dbType() == SQLite )
		dbName = sqliteSetupPage->dbFile();
}

DataInitializePage::DataInitializePage( TQWidget *parent ) : TQWidget( parent )
{
	TQGridLayout * layout = new TQGridLayout( this, 1, 1, 0, 0 );
	TQSpacerItem *spacer_top = new TQSpacerItem( 10, 10, TQSizePolicy::Minimum, TQSizePolicy::Fixed );
	layout->addItem( spacer_top, 0, 1 );
	TQSpacerItem *spacer_left = new TQSpacerItem( 10, 10, TQSizePolicy::Fixed, TQSizePolicy::Minimum );
	layout->addItem( spacer_left, 1, 0 );

	// Explanation Text// Widgets
	TQLabel *logo;
	//TQLabel *serverSetupText;
	//KLineEdit *usernameEdit;
	//KLineEdit *passwordEdit;
	//KLineEdit *dbNameEdit;
	initializeText = new TQLabel( this );
	initializeText->setText( i18n( "Krecipes comes with some delicious default recipes and useful data. <br><br>Would you like to initialize your database with those? Note that this will erase all your previous recipes if you have any. " ) );

	initializeText->setSizePolicy( TQSizePolicy::MinimumExpanding, TQSizePolicy::Minimum );
	initializeText->setAlignment( int( TQLabel::WordBreak | TQLabel::AlignTop ) );
	layout->addWidget( initializeText, 1, 3 );

	// Logo
	TQPixmap dataInitializePixmap ( locate( "data", "krecipes/pics/recipes.png" ) );
	logo = new TQLabel( this );
	logo->setPixmap( dataInitializePixmap );
	logo->setSizePolicy( TQSizePolicy::Fixed, TQSizePolicy::Fixed );
	layout->addMultiCellWidget( logo, 1, 8, 1, 1, TQt::AlignTop );

	// Spacer to separate the logo
	TQSpacerItem *logoSpacer = new TQSpacerItem( 10, 10, TQSizePolicy::Fixed, TQSizePolicy::Minimum );
	layout->addItem( logoSpacer, 1, 2 );

	// Initialize data checkbox

	initializeCheckBox = new TQCheckBox( i18n( "Yes please, initialize the database with the examples" ), this, "initializeCheckBox" );
	layout->addWidget( initializeCheckBox, 3, 3 );

	TQSpacerItem *textInfoSpacer = new TQSpacerItem( 0, 50, TQSizePolicy::Minimum, TQSizePolicy::Fixed );
	layout->addItem( textInfoSpacer, 4, 3 );

	USDAImportText = new TQLabel( i18n( "Krecipes can import nutrient data from the USDA's nutrient database for over 400 foods.  A total of 43 food properties are included for each food, such as energy, fat, vitamin C, etc.<br><br>Would you like to import this data now?  Note that this operation is safe to use on an existing database, and no data loss will occur.  This operation may take several minutes." ), this );
	layout->addWidget( USDAImportText, 5, 3 );

	TQSpacerItem *importInfoSpacer = new TQSpacerItem( 0, 50, TQSizePolicy::Minimum, TQSizePolicy::Fixed );
	layout->addItem( importInfoSpacer, 6, 3 );

	USDAImportCheckBox = new TQCheckBox( i18n( "Yes please, load the database with nutrient data for 400+ foods. (Note: English only.)" ), this, "USDAImportCheckBox" );
	layout->addWidget( USDAImportCheckBox, 7, 3 );
}

bool DataInitializePage::doInitialization( void )
{
	return ( initializeCheckBox->isChecked() );
}

bool DataInitializePage::doUSDAImport( void )
{
	return ( USDAImportCheckBox->isChecked() );
}

DBTypeSetupPage::DBTypeSetupPage( TQWidget *parent ) : TQWidget( parent )
{
	TQGridLayout * layout = new TQGridLayout( this, 1, 1, 0, 0 );
	TQSpacerItem *spacer_top = new TQSpacerItem( 10, 10, TQSizePolicy::Minimum, TQSizePolicy::Fixed );
	layout->addItem( spacer_top, 0, 1 );
	TQSpacerItem *spacer_left = new TQSpacerItem( 10, 10, TQSizePolicy::Fixed, TQSizePolicy::Minimum );
	layout->addItem( spacer_left, 1, 0 );


	// Image

	TQPixmap serverSetupPixmap ( locate( "data", "krecipes/pics/network.png" ) );
	logo = new TQLabel( this );
	logo->setPixmap( serverSetupPixmap );
	logo->setSizePolicy( TQSizePolicy::Fixed, TQSizePolicy::Fixed );
	layout->addMultiCellWidget( logo, 1, 4, 1, 1, TQt::AlignTop );

	TQSpacerItem *spacer_from_image = new TQSpacerItem( 10, 10, TQSizePolicy::Fixed, TQSizePolicy::Minimum );
	layout->addItem( spacer_from_image, 1, 2 );


	// Explanation text
	dbTypeSetupText = new TQLabel( this );
	dbTypeSetupText->setText( i18n( "Choose the type of database that you want to use. Most users will want to choose a simple local database here. However, you can also use remote servers by means of a MySQL or PostgreSQL database." ) );
	dbTypeSetupText->setSizePolicy( TQSizePolicy::MinimumExpanding, TQSizePolicy::Minimum );
	dbTypeSetupText->setAlignment( int( TQLabel::AlignTop | TQLabel::WordBreak ) );
	layout->addWidget( dbTypeSetupText, 1, 3 );

	// Text spacer

	TQSpacerItem* textSpacer = new TQSpacerItem( 10, 30, TQSizePolicy::Minimum, TQSizePolicy::Fixed );
	layout->addItem( textSpacer, 2, 3 );



	// Database type choice
	bg = new TQVButtonGroup( this );
	layout->addWidget( bg, 3, 3 );

	liteCheckBox = new TQRadioButton( i18n( "Simple Local File (SQLite)" ), bg, "liteCheckBox" );
	mysqlCheckBox = new TQRadioButton( i18n( "Local or Remote MySQL Database" ), bg, "liteCheckBox" );
	psqlCheckBox = new TQRadioButton( i18n( "Local or Remote PostgreSQL Database" ), bg, "psqlCheckBox" );
	bg->setButton( 0 ); // By default, SQLite

#if (!HAVE_MYSQL)
	mysqlCheckBox->setEnabled( false );
#endif

#if (!HAVE_POSTGRESQL)
	psqlCheckBox->setEnabled( false );
#endif

#if (!HAVE_SQLITE3)
	liteCheckBox->setEnabled( false );
#if (HAVE_MYSQL)

	bg->setButton( 1 ); // Otherwise by default liteCheckBox is checked even if it's disabled
#else
	#if (HAVE_POSTGRESQL)

	bg->setButton( 2 );
#endif
	#endif
#endif


	TQSpacerItem *spacer_bottom = new TQSpacerItem( 10, 10, TQSizePolicy::Fixed, TQSizePolicy::MinimumExpanding );
	layout->addItem( spacer_bottom, 4, 3 );

	connect( bg, TQ_SIGNAL( clicked( int ) ), this, TQ_SLOT( setPages( int ) ) );

}

int DBTypeSetupPage::dbType( void )
{
	int id = bg->id( bg->selected() );

	switch ( id ) {
	case 1:
		return ( MySQL ); // MySQL (note index=0,1....)
	case 2:
		return ( PostgreSQL );
	default:
		return ( SQLite );
	}
}

/*
** hides/shows pages given the radio button state
*/

void DBTypeSetupPage::setPages( int rb )
{
	switch ( rb ) {
	case 1:
		emit showPages( MySQL );
		break;
	case 2:
		emit showPages( PostgreSQL );
		break;
	default:
		emit showPages( SQLite );
		break;
	}
}

#include "setupwizard.moc"
