/* This file is part of the Keep project
   Copyright (C) 2005 Jean-Rémy Falleri <jr.falleri@laposte.net>

   Keep 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.

   Keep 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 Keep; if not, write to the
   Free Software Foundation, Inc.,
   51 Franklin Steet, Fifth Floor, Boston, MA 02110-1301, USA.           */

#include "addbackupwizard.h"

#include <tqpushbutton.h>
#include <kiconloader.h>
#include <tqcheckbox.h>
#include <kurlrequester.h>
#include <knuminput.h>
#include <kdebug.h>

AddBackupWizard::AddBackupWizard(TQWidget *parent,const char* name): KWizard( parent, name, true)
{
	initView();
	resize( minimumSizeHint() );

	setNextEnabled(page1,false);
	setNextEnabled(page2,false);
	setFinishEnabled(page3, false);
	
	this->setCaption("Add a backup");

	initConnections();
}

AddBackupWizard::AddBackupWizard(Backup backup,TQWidget *parent,const char* name): KWizard( parent, name, true)
{
	initView();

	this->setCaption("Edit backup");
	
	addBackupWizard1View->source->setURL(backup.source());
	addBackupWizard1View->useIncludeExclude->setChecked(backup.useIncludeExclude());
	addBackupWizard2View->dest->setURL(backup.dest());
	addBackupWizard3View->interval->setValue(backup.interval());
	addBackupWizard3View->deleteAfter->setValue(backup.deleteAfter());
	addBackupWizard3View->neverDelete->setChecked(backup.neverDelete());
	addBackupWizard3View->useCompression->setChecked(backup.useCompression());
	addBackupWizard3View->excludeSpecialFiles->setChecked(backup.excludeSpecialFiles());
	addBackupWizard3View->useAdvancedConfig->setChecked(backup.useAdvancedConfig());

	includeExcludeDialog->setIncludeExcludeList(backup.includeExcludeList());
	advancedBackupConfigDialog->setOptionList(backup.optionList());

	resize( minimumSizeHint() );

	setNextEnabled(page1,true);
	setNextEnabled(page2,true);
	setFinishEnabled(page3, true);

	initConnections();
}

void AddBackupWizard::initView()
{
	advancedBackupConfigDialog = new AdvancedBackupConfigDialog(this);
	includeExcludeDialog = new IncludeExcludeDialog(this);

	TDEIconLoader icons;
	this->setIcon( icons.loadIcon( "wizard",TDEIcon::Small ));

	setupPage1();
	setupPage2();
	setupPage3();
}

void AddBackupWizard::initConnections()
{
	connect( addBackupWizard1View->source, TQ_SIGNAL( textChanged(const TQString&)), this, TQ_SLOT( slotPage1Changed() ) );
	connect( addBackupWizard2View->dest, TQ_SIGNAL( textChanged(const TQString&)), this, TQ_SLOT( slotPage2Changed() ) );
	connect( addBackupWizard3View->advancedConfig, TQ_SIGNAL( clicked()),this,TQ_SLOT(slotShowAdvancedConfig()));
	connect( addBackupWizard1View->includeExclude, TQ_SIGNAL( clicked()),this,TQ_SLOT(slotShowIncludeExclude()));
	connect( finishButton(), TQ_SIGNAL( clicked()), this, TQ_SLOT( slotFinishClicked() ) );
}

void AddBackupWizard::setupPage1()
{
	page1 = new TQHBox( this );
	page1->setSpacing(0);
	page1->setMargin(0);

	addBackupWizard1View = new AddBackupWizard1View(page1,"addBackupWizard1View");
	addBackupWizard1View->source->setMode(KFile::Directory);
	TDEIconLoader *icons = TDEGlobal::iconLoader();
	addBackupWizard1View->includeExclude->setPixmap(icons->loadIcon("configure",TDEIcon::Toolbar,16));

	addPage( page1, "Directory to backup" );
}

void AddBackupWizard::setupPage2()
{
	page2 = new TQHBox( this );
	page2->setSpacing(0);
	page2->setMargin(0);

	addBackupWizard2View = new AddBackupWizard2View(page2,"addBackupWizard2View");
	addBackupWizard2View->dest->setMode(KFile::Directory);

	addPage( page2, "Backup location" );
}

void AddBackupWizard::setupPage3()
{
	page3 = new TQHBox( this );
	page3->setSpacing(0);
	page3->setMargin(0);

	addBackupWizard3View = new AddBackupWizard3View(page3,"addBackupWizard3View");
	TDEIconLoader *icons = TDEGlobal::iconLoader();
	addBackupWizard3View->advancedConfig->setPixmap(icons->loadIcon("configure",TDEIcon::Toolbar,16));

	addPage( page3, "Backup options" );
}

void AddBackupWizard::slotPage1Changed()
{
	setNextEnabled(page1,false);
	setFinishEnabled(page3,false);
	if ( addBackupWizard1View->source->url() != "" )
	{
		setNextEnabled(page1,true);
		if ( addBackupWizard2View->dest->url() != "" )
			setFinishEnabled(page3,true);	
	}
}

void AddBackupWizard::slotPage2Changed()
{
	setNextEnabled(page2,false);
	setFinishEnabled(page3,false);
	if ( addBackupWizard2View->dest->url() != "" )
	{
		setNextEnabled(page2,true);
		if (  addBackupWizard1View->source->url() != "" )
			setFinishEnabled(page3,true);	
	}
}

void AddBackupWizard::slotShowAdvancedConfig()
{
	advancedBackupConfigDialog->show();
}

void AddBackupWizard::slotShowIncludeExclude()
{
	includeExcludeDialog->show();
}

Backup AddBackupWizard::backup()
{
	TQString source = addBackupWizard1View->source->url();
	TQString dest = addBackupWizard2View->dest->url();
	int interval = addBackupWizard3View->interval->value();
	int deleteAfter = addBackupWizard3View->deleteAfter->value();
	bool neverDelete = addBackupWizard3View->neverDelete->isChecked();
	bool useCompression = addBackupWizard3View->useCompression->isChecked();
	bool excludeSpecialFiles = addBackupWizard3View->excludeSpecialFiles->isChecked();
	bool useAdvancedConfig = addBackupWizard3View->useAdvancedConfig->isChecked();
	TQStringList optionList = advancedBackupConfigDialog->optionList();
	bool useIncludeExclude = addBackupWizard1View->useIncludeExclude->isChecked();
	TQStringList includeExcludeList = includeExcludeDialog->includeExcludeList();
	Backup backup(source,dest,interval,deleteAfter,neverDelete,useCompression,excludeSpecialFiles,useAdvancedConfig,optionList,useIncludeExclude,includeExcludeList);
	
	return backup;
}

void AddBackupWizard::slotFinishClicked()
{
	emit backupSetted(backup());
}

#include "addbackupwizard.moc"
