/***************************************************************************
 * midi.cpp: implementation of Tse3Assistant 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 "midi.h"

#include <KConfigGroup>
#include <KLocale>
#include <KMessageBox>
#include <KDebug>

#include <tse3/Error.h>

#include "settings.h"

Tse3Assistant::Tse3Assistant(TSE3::MidiSchedulerFactory& factory, QWidget* parent)
: QWizardPage(parent),
  mMidiport(0),
  mMidiportLabel(0),
  mMidiRefresh(0),
  mMidiButton(0),
  mMidiLayout(0),
  mScheduler(0),
  mFactory(factory)
{
	QStringList headerLabels;
	
	setTitle(i18n("TSE3"));
	setSubTitle(i18n("With this wizard you can configure your midi driver\n(hardware or software with timidity)."));
	
	mMidiport = new QTreeWidget(this);
	
	headerLabels << "Name" << "Port" << "Port Number" << "Is Readable" << "Is Writeable";
	
	mMidiport->setHeaderLabels(headerLabels);
	mMidiportLabel = new QLabel(mMidiport, i18n("MIDI &output port"), this);
	mMidiRefresh = new QPushButton(i18n("&Refresh"), this);
	mMidiButton = new QHBoxLayout();
	mMidiLayout = new QVBoxLayout(this);
	
	fillTse3Box();
	
	connect(mMidiRefresh, SIGNAL(clicked()), SLOT(fillTse3Box()));
	
	mMidiButton->addWidget(mMidiRefresh);
	
	mMidiLayout->addWidget(mMidiportLabel);
	mMidiLayout->addWidget(mMidiport, 1);
	mMidiLayout->addLayout(mMidiButton);
}

void Tse3Assistant::fillTse3Box()
{
	QString temp, read, write;
	QTreeWidgetItem* lastItem;
	 
	mMidiport->clear();
	
	if (mScheduler)
		delete mScheduler;
	
	try {
		mScheduler = mFactory.createScheduler();
	} catch (TSE3::MidiSchedulerError e) {
		QString temp = e.what();
		
		kDebug() << "error while creating the midi scheduler !!\n" << temp << "\n";
		KMessageBox::error(this, i18n("TSE3 error"), i18n("Could not create a MidiScheduler for this platform ") + temp);
		mScheduler = 0;
		
		return ;
	}
	
	mScheduler->portNumbers(mPortNums);
	
	for (uint i = 0; i < mScheduler->numPorts(); i++) {
		QStringList item;
		
		if (mScheduler->portReadable(mPortNums[i]))
			read = "Yes";
		else
			read = "No";
		
		if (mScheduler->portWriteable(mPortNums[i]))
			write = "Yes";
		else
			write = "No";
		
		item << mScheduler->portName(mPortNums[i]) << mScheduler->portType(mPortNums[i]) << temp.setNum(mPortNums[i]) << read << write;
		
		lastItem = new QTreeWidgetItem(mMidiport, item);
		
		if (mPortNums[i] == Settings::getInstance()->midiPortNumber())
			mMidiport->setCurrentItem(lastItem);
	}
}

void Tse3Assistant::writeConfig()
{
	KConfigGroup config = KGlobal::config()->group("MIDI");
	
	if (mMidiport->currentItem()) {
		config.writeEntry("Name", mMidiport->currentItem()->text(0));
		config.writeEntry("Port", mMidiport->currentItem()->text(1));
		config.writeEntry("PortNumber", mMidiport->currentItem()->text(2));
		config.writeEntry("Readable", mMidiport->currentItem()->text(3));
		config.writeEntry("Writeable", mMidiport->currentItem()->text(4));
	}
}

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

#include "midi.moc"
