/**
 * The XSupplicant User Interface is Copyright 2007, 2008, 2009 Nortel Networks.
 * Nortel Networks provides the XSupplicant User Interface under dual license terms.
 *
 *   For open source projects, if you are developing and distributing open source 
 *   projects under the GPL License, then you are free to use the XSupplicant User 
 *   Interface under the GPL version 2 license.
 *
 *  --- GPL Version 2 License ---
 *
 *  This program is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU General Public License, Version 2
 *  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, Version 2 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., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.  
 *  You may also find the license at the following link
 *  http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt .
 *
 *
 *   For commercial enterprises, OEMs, ISVs and VARs, if you want to distribute or 
 *   incorporate the XSupplicant User Interface with your products and do not license
 *   and distribute your source code for those products under the GPL, please contact
 *   Nortel Networks for an OEM Commercial License.
 **/

#include "stdafx.h"

extern "C" {
#include "libcrashdump/crash_handler.h"
#include "libcrashdump/crashdump.h"
};

#include "TrayApp.h"
#include "FormLoader.h"
#include "buildnum.h"

int main(int argc, char *argv[])
{
	QT_REQUIRE_VERSION(argc, argv, "4.2.2")

	QApplication app(argc, argv);

	// If you want to force the UI to run in a specific language, uncomment this, modify it and comment out the block below it.
/*
  QTranslator translator;
  translator.load("XSupplicantUI_es");
  app.installTranslator(&translator);
  */

	// If you want to force the UI to run in a specific language, comment this, and uncomment the block above.
	QString locale = QLocale::system().name();

	QTranslator translator;
	translator.load(QString("XSupplicantUI_") + locale);
	app.installTranslator(&translator);

	QPixmap *p;
	QFont f;
	unsigned char timeout = 0;

#ifdef WINDOWS
	crashdump_init("\\xsupuicrashdmp-" BUILDNUM ".zip");

	// Install the crash handler so that we can generate minidumps if we fail for some reason.
	crash_handler_install("\\xsupui-" BUILDNUM ".dmp");
	crashdump_add_file("\\xsupui-" BUILDNUM ".dmp", 1);

	// A second file is implicitly generated by the crash dumper, so we need to add that to the
	// list too.
	crashdump_add_file("\\xsupui-" BUILDNUM ".dmp.log", 1);
#else
#warning Need to implement crash dump file handlingfor this platform.
#endif				// WINDOWS

	f.setPointSize(10);
	app.setFont(f);

	// Wait up to 60 seconds for the system tray to become available after
	// the UI has been started.
	while ((!QSystemTrayIcon::isSystemTrayAvailable()) && (timeout < 60))
	  {
	    timeout++;
#ifdef WINDOWS
	    Sleep(1000);
#else
	    sleep(1);
#endif
	  }

	if (!QSystemTrayIcon::isSystemTrayAvailable()) {
		QMessageBox::critical(0, QObject::tr("System Tray"),
				      QObject::
				      tr("Unable to detect any system tray "
					 "on this system."));
		return 1;
	}

	p = FormLoader::loadicon("prod_color.png");
	QIcon icon((*p));
	app.setWindowIcon(icon);

	delete p;

	app.setQuitOnLastWindowClosed(false);

	TrayApp *pTrayApp = new TrayApp(app);
	if (pTrayApp->init(argc)) {
		return app.exec();
	}

	return -1;
}
