/***************************************************************************
 *   Copyright (C) 2010 by Volker Lanz <vl@fidra.de>                       *
 *                                                                         *
 *   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, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA            *
 ***************************************************************************/

#include "core/storage.h"
#include "gui/mainwindow.h"

#include "veneer/system.h"

#include <QApplication>
#include <QMessageBox>
#include <QDebug>
#include <QTranslator>
#include <QLibraryInfo>
#include <QDir>
#include <QFile>

#include <MarbleDirs.h>

#if defined(HAVE_KDE)
#include <kapplication.h>
#include <kcmdlineargs.h>
#include <kaboutdata.h>

static KAboutData* createWorkoutAboutData()
{
	KAboutData* about = new KAboutData(
		"workout",
		NULL,
		ki18n("Workout"),
		VERSION,
		ki18n("KDE Sports and Activity Tracker"),
		KAboutData::License_GPL,
		ki18n("(c) 2010 Volker Lanz")
	);

	about->addAuthor(ki18n("Volker Lanz"), KLocalizedString(), "vl@fidra.de");
	about->setHomepage("http://www.volker-lanz.de");

	return about;
}
#endif


QString findTranslationDir()
{
	QString rval;
	QStringList dataDirs = Veneer::findDataDirPaths();

	foreach (const QString& dir, dataDirs)
	{
		rval = dir + QDir::separator() + "translations" + QDir::separator();
		if (QFile::exists(rval))
			break;
	}

	return rval;
}

int main(int argc, char* argv[])
{
#if defined(HAVE_KDE)
	KCmdLineArgs::init(argc, argv, createWorkoutAboutData());
	KCmdLineOptions options;
	KCmdLineArgs::addCmdLineOptions(options);

	KApplication app;
	KGlobal::locale()->insertCatalog("workout_qt");
#else
	QApplication app(argc, argv);
	QCoreApplication::setApplicationName("Workout");
	QCoreApplication::setOrganizationName("Fidra");
	QCoreApplication::setOrganizationDomain("fidra.de");

	QTranslator qtTranslator;
	const QString locale = QLocale::system().name();
	const QString qtLocaleName = "qt_" + locale;

	if (qtTranslator.load(qtLocaleName, QLibraryInfo::location(QLibraryInfo::TranslationsPath)))
		qApp->installTranslator(&qtTranslator);
	else
		qDebug() << "loading Qt translations for locale " << qtLocaleName << " from dir " << QLibraryInfo::location(QLibraryInfo::TranslationsPath) << " failed.";

	QTranslator t;
	const QString translationDir = findTranslationDir();
	if (t.load(locale, translationDir))
		qApp->installTranslator(&t);
	else
		qDebug() << "loading app translations for locale " << locale << " from dir " << translationDir << " failed.";

#if defined(Q_OS_WIN)
	Marble::MarbleDirs::setMarbleDataPath(QCoreApplication::applicationDirPath() + "/marble/data");
	Marble::MarbleDirs::setMarblePluginPath(QCoreApplication::applicationDirPath() + "/marble/plugins");
#endif

#endif

	if (!Storage::init())
	{
		QMessageBox::critical(NULL,
			"Failed To Set Up Database",
			"Failed to set up the internal database.<br><br>Please check your installation. The application cannot run without a working database.");
		return -1;
	}

	MainWindow* mainWindow = new MainWindow();
	mainWindow->show();

	return app.exec();
}
