/***************************************************************************
 * optionsmidi.cpp: implementation of OptionsMidi class
 *
 * This file is part of KTabEdit, a KDE tabulature editor
 *
 * copyright (C) 2003-2006 the KTabEdit development team <mrgwen@gmail.com>
 * copyright (C) 2002-2003 the KGuitar 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 "optionsmidi.h"

#include <QMessageBox>

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

#include <tse3/Error.h>

#include "settings.h"

OptionsMidi::OptionsMidi(TSE3::MidiSchedulerFactory& midiFactory, TSE3::MidiScheduler** scheduler, KConfig* conf, QWidget* parent)
: OptionsPage(conf, parent),
  mMidiPort(this),
  //mMidiPortLabel(&mMidiPort, i18n("MIDI &output port"), this),
  mMidiPortLabel(i18n("MIDI &output port"), this),
  mMidiRefresh(i18n("&Refresh"), this),
  mMidiTimidity(i18n("&Launch Timidity"), this),
  //mMidiLayout(this, 10, 5),
  mMidiLayout(this),
  mMidiButton(this),
  mFactory(midiFactory),
  mScheduler(scheduler),
  mTimidity(false)
{
	QStringList headerLabels;
	
	headerLabels << "Name" << "Port" << "Port Number" << "Is Readable" << "Is Writeable";
	
	mMidiPort.setHeaderLabels(headerLabels);
	
	fillMidiBox();
	
	connect(&mMidiRefresh, SIGNAL(clicked()), SLOT(fillMidiBox()));
	connect(&mMidiTimidity, SIGNAL(clicked()), SLOT(launchTimidity()));
	
	mMidiButton.addWidget(&mMidiRefresh);
	
	// Set widget layout
	//
	mMidiLayout.addWidget(&mMidiPortLabel);
	mMidiLayout.addWidget(&mMidiPort, 1);
	mMidiLayout.addLayout(&mMidiButton);
	mMidiLayout.addWidget(&mMidiTimidity);
	mMidiLayout.activate();
	
	mMidiTimidity.setChecked(Settings::getInstance()->midiTimidity());
}

void OptionsMidi::fillMidiBox()
{
	QString temp, read, write;
	QTreeWidgetItem* lastItem;
	
	mMidiPort.clear();
	
	// The scheduler is already configured so deleted it
	//
	if (*mScheduler)
		delete *mScheduler ;
	
	// Try to create a new scheduler
	//
	try {
		*mScheduler = mFactory.createScheduler();
	} catch (TSE3::MidiSchedulerError e) {
		QString temp = e.what();
		
		kDebug() << "error while creating the midi scheduler !!\n" << temp << "\n";
		KMessageBox::error(0, "Couldn't 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);
		
		// Select the current midi card
		//
		if (mPortNums[i] == Settings::getInstance()->midiPortNumber()) {
			mMidiPort.setCurrentItem(lastItem);
		}
	}
}

void OptionsMidi::launchTimidity()
{
	if (mMidiTimidity.isChecked() == false) {
		mTimidity = false;
		return ;
	}
	
	if (KMessageBox::questionYesNo(this, "KTabEdit",
			i18n("Restart KTabEdit and try to launch the timidity sequencer with alsa emulation? (It must be compiled with alsa support.)"))
		 == QMessageBox::Yes) {
		mTimidity = true;
		return ;
	}
	
	mTimidity = false;
	mMidiTimidity.setChecked(false);
}

void OptionsMidi::defaultBtnClicked()
{
}

void OptionsMidi::applyBtnClicked()
{
/*	config->setGroup("MIDI");
	config->writeEntry("Timidity", mTimidity);
	
	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));
	}*/
}

#include "optionsmidi.moc"
