/*
 * Copyright 2009-2010  Stefan Gehn <stefan@srcbox.net>
 * 
 * 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.
 * 
 * This program 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 this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <qapplication.h>
#include <qtextcodec.h>
#include "MainWindow.hpp"
#include "EventProducer.hpp"
#include "TouchThread.hpp"

#include <qfile.h>

int main(int argc, char **argv)
{
	QThread *touchThread;
  QApplication app(argc, argv);
	QTextCodec::setCodecForTr(QTextCodec::codecForName("UTF-8"));
  MainWindow mw;
	IrTouch::EventProducer evprod;

	QStringList args = app.arguments();
	if ((args.count() > 1) && QFile::exists(args[1]))
		touchThread = new IrTouch::TouchThread(args[1]);
	else
		touchThread = new IrTouch::TouchThread();

	touchThread->start();
	QObject::connect(touchThread, SIGNAL(dataDecoded(const IrTouch::TouchData)),
			&evprod, SLOT(buildEvents(const IrTouch::TouchData)),
			Qt::QueuedConnection);

	mw.show();
	int ret = app.exec();

	touchThread->quit();
	if (!touchThread->wait(5000))
		qCritical() << "TouchThread still alive, exiting anyway";
	delete touchThread;

	return ret;
}
