/*****************************************************************************
 *
 *  main.cpp -- Create and open the main program window.
 * 
 *  Copyright (C) 2010 Starbuck <starbuck@labdv.com>
 *  All rights reserved.
 *
 *  This file is part of the Main module of the DVDx 4 application.
 *
 *  GNU Lesser General Public License Usage
 *  This file may be used under the terms of the GNU Lesser General
 *  Public License version 2.1 as published by the Free Software
 *  Foundation and appearing in the file LICENSE.LGPL included in the
 *  packaging of this file.  Please review the following information to
 *  ensure the GNU Lesser General Public License version 2.1 requirements
 *  will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
 *
 *  GNU General Public License Usage
 *  Alternatively, this file may be used under the terms of the GNU
 *  General Public License version 3.0 as published by the Free Software
 *  Foundation and appearing in the file LICENSE.GPL included in the
 *  packaging of this file.  Please review the following information to
 *  ensure the GNU General Public License version 3.0 requirements will be
 *  met: http://www.gnu.org/copyleft/gpl.html.
 *
 *
 *****************************************************************************/

#include "application.h"
#include "console.h"
#include "core.h"
#include "../encoder/ffmpeg.h"
#include "../encoder/ffprobe.h"
#include "../encoder/mencoder.h"
#include "../gui4/mainwindow.h"
#include "../gui4/macros.h"
#include <QApplication>
#include <QFont>
#include <QFontDatabase>
#include <QMainWindow>
#include <QObject>
#include <QString>

Console		*console;  // main GUI console started in main window
FFmpeg		*ffmpeg;
FFprobe		*ffprobe;
MEncoder	*mencoder;

int			keepSystemAwake(0);
MainWindow	*mainWin(0);

int main(int argc, char *argv[])
{
	// So-called POSIX-compliant Mac OS X bug
#ifdef Q_OS_MAC
	::signal(SIGPIPE, SIG_IGN);
#endif

#ifdef QT_NO_DEBUG
	QString appName = QObject::tr("%1 Open Edition").arg(APPTITLE);
#else
	QString appName = QObject::tr("%1 Open Edition (Debug Build)").arg(APPTITLE);		
#endif

	QApplication app(argc, argv);

	Q_INIT_RESOURCE(gui4);

	app.setApplicationName(appName);
	app.setOrganizationDomain("DVDx");
	app.setOrganizationName("labDV.com");
	app.setWindowIcon(QIcon(":/gui4/images/dvdx4icon.svg"));

#ifdef Q_OS_LINUX
	// Kinf of Qt 4.7.4 Ubuntu 11.10 compliance issue (Helvetia font not available)
	QFontDatabase appFontDatabase;
	QFont appFont(QApplication::font());
	qDebug() << appFont << appFontDatabase.families();
	if (!appFontDatabase.families().contains(appFont.family()))
	{
		QString newFamily;
		if (appFontDatabase.families().contains("Helvetica"))
			newFamily = "Helvetica";
		if (appFontDatabase.families().contains("Ubuntu"))
			newFamily = "Ubuntu";
		else
			newFamily = "Fixed";

		appFont.setFamily(newFamily);
		QApplication::setFont(appFont);
	}
#endif

	int returnCodeFromEventLoop;
	do
	{
		Core::start();  // initialize global encoders and analyzers
		
		mainWin = 0;

		mainWin = new MainWindow;	
		mainWin->initialize();
		mainWin->setWindowTitle(appName);
		mainWin->show();

		returnCodeFromEventLoop = app.exec();

		Core::stop();

		if (returnCodeFromEventLoop == RESTART_CODE)
		{
			if (mainWin) mainWin->close();

			// no need to delete mainSmartWin or mainWin, I guess Qt destroys all windows when closing
		}
	}
	while(returnCodeFromEventLoop == RESTART_CODE);
	
	return returnCodeFromEventLoop;
}
