/***************************************************************************
 * assistant.cpp: implementation of Assistant class
 *
 * This file is part of KTabEdit, a KDE tabulature editor
 *
 * copyright (C) 2003-2006 the KTabEdit development team <mrgwen@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.
 *
 * See the file COPYING for more information.
 ***************************************************************************/

#include "assistant.h"

#include <KMessageBox>
#include <KConfigGroup>
#include <KGlobal>
#include <KSharedConfig>

#include "introduction.h"
#include "midi.h"
#include "settings.h"
#include "timidity.h"
#include "timiditylauncher.h"

Assistant::Assistant(TSE3::MidiSchedulerFactory& factory, QWidget* parent)
: QWizard(parent),
  mScheduler(0),
  mLauncher(0),
  mFactory(factory)
{
	connect(button(QWizard::NextButton), SIGNAL(clicked()), this, SLOT(next()));
	connect(button(QWizard::BackButton), SIGNAL(clicked()), this, SLOT(back()));
	
	mLauncher = new TimidityLauncher();
	
	setupIntro();
	setupTimidity();
	setupTSE3();
}

Assistant::~Assistant()
{
	writeConfig();
}

void Assistant::back()
{
	switch (currentId()) {
	case Page_Intro:
		break;
	case Page_Timidity:
		killTimidity();
		
		break;
	case Page_Tse3:
	default:
		break;
	}
}

void Assistant::next()
{
	switch (currentId()) {
	case Page_Intro:
		break;
	case Page_Timidity:
		break;
	case Page_Tse3:
		killTimidity();
		
		if (mTimidity->isTimidityWanted()) {
			mLauncher->setProgram(mTimidity->program());
			mLauncher->setArguments(mTimidity->buildCmdLine());
			
			if (mLauncher->launchTimidityByHand() == false)
				KMessageBox::error(this, "KTabEdit", "Could not launch timidity " + mLauncher->errorMsg());
		}
		break;
	default:
		break;
	}
}

void Assistant::restart()
{
}

void Assistant::accept()
{
	mScheduler = mMidi->midiScheduler();
	
	mTimidity->writeConfig();
	mMidi->writeConfig();
	
	QWizard::accept();
}

void Assistant::reject()
{
	mScheduler = mMidi->midiScheduler();
	
	killTimidity();
	
	if (mScheduler)
		delete mScheduler;
	
	mScheduler = 0;
	
	QWizard::reject();
}

TimidityLauncher* Assistant::timidityLauncher() const
{
	return mLauncher;
}

TSE3::MidiScheduler* Assistant::midiScheduler() const
{
	return mScheduler;
}

void Assistant::setupIntro()
{
	setPage(Page_Intro, new Introduction(this));
}

void Assistant::setupTimidity()
{
	mTimidity = new Timidity(this);
	
	setPage(Page_Timidity, mTimidity);
}

void Assistant::setupTSE3()
{
	mMidi = new Tse3Assistant(mFactory, this);
	
	setPage(Page_Tse3, mMidi);
}

void Assistant::killTimidity()
{
	if (mLauncher->state() == QProcess::Running)
		mLauncher->kill();
}

void Assistant::writeConfig()
{
	KConfigGroup config = KGlobal::config()->group("Assistant");
	config.writeEntry("FirstTime", true);
}

#include "assistant.moc"
