/*
 *  Copyright (C) 2007-2008 CMM Sigma Andrzej Chmielowiec <cmmsigma@cmmsigma.eu>
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License version 3
 *  as published by the Free Software Foundation.
 *
 *  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.
 */

#include <QtGui>
#include <QTextCodec>

#include "MainWindow.h"
#include "CA.h"

QString lang;

int main(int argc, char *argv[])
{
	QTextCodec::setCodecForTr(QTextCodec::codecForName("ISO 8859-2"));
	QApplication app(argc, argv);
	
	QDir::setCurrent(QDir::homePath());

#ifndef STOKEN_SOFT
	if (!QDir(QDir::current().path() + QDir::separator() + QString(".stoken_ca")).exists())
		QDir::current().mkdir(QString(".stoken_ca"));
	
	QDir::setCurrent(QDir::current().path() + QDir::separator() + QString(".stoken_ca"));
#else
	if (!QDir(QDir::current().path() + QDir::separator() + QString(".stoken_softca")).exists())
		QDir::current().mkdir(QString(".stoken_softca"));
	
	QDir::setCurrent(QDir::current().path() + QDir::separator() + QString(".stoken_softca"));
#endif
	
	QFile lang_init("lang");
	if(lang_init.open(QIODevice::ReadOnly))
	{
		lang = (QString)lang_init.readAll();
		lang_init.close();

		if(lang == "pl")
		{
			QTranslator *qtTranslator = new QTranslator(&app);
			qtTranslator->load(":qt_pl");
			app.installTranslator(qtTranslator);

			QTranslator *appTranslator = new QTranslator(&app);
			appTranslator->load(":stoken_ca_pl");
			app.installTranslator(appTranslator);
			
			QTranslator *libCommonTranslator = new QTranslator(&app);
			libCommonTranslator->load(":stoken_libcommon_pl");
			app.installTranslator(libCommonTranslator);
			
			QTranslator *qtNetProtocolsTranslator = new QTranslator(&app);
			qtNetProtocolsTranslator->load(":qtnetprotocols_pl");
			app.installTranslator(qtNetProtocolsTranslator);
		}
	}

	MainWindow mainWindow;
	mainWindow.show();
	
	int result = app.exec();
	
	QFile lang_new("lang");
	lang_new.open(QIODevice::WriteOnly);
	lang_new.write(lang.toAscii());
	lang_new.close();

	return result;
}
