/*******************************************************************************
***  This file is part of Dux Rei Nummariae.                                 ***
***                                                                          ***
***  Copyright (C) 2017, 2018, 2019, 2020, 2021                              ***
***  John Schneiderman <Licensing _AT_ Schneiderman _DOT_ me>                ***
***                                                                          ***
***  This program is free software: you can redistribute it and/or modify it ***
***  under the terms of the GNU Affero General Public License as published   ***
***  by the Free Software Foundation, either version 3 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 Affero General Public License for more details.             ***
***                                                                          ***
***  You should have received a copy of the GNU Affero General Public License***
***  along with this program. If not, see <http://www.gnu.org/licenses/>.    ***
*******************************************************************************/
#include "Application.h"
using drn::desktop::Application;

#include <QDebug>
// using qDebug macros.
#include <QMessageBox>
using ::QMessageBox;

#include <stdexcept>
using std::logic_error;
using std::runtime_error;
using std::exception;

#include <drn/config.h>
// using APPLICATION_LONG_NAME.
// using APPLICATION_URL.
// using FULL_VERSION
// using ORGANIZATION_NAME.


Application::Application(int& argc, char* argv[]) :
	QApplication(argc, argv)
{
	QCoreApplication::setOrganizationName(DuxReiNummariae_ORGANIZATION_NAME);
	QCoreApplication::setOrganizationDomain(DuxReiNummariae_APPLICATION_URL);
	QCoreApplication::setApplicationName(DuxReiNummariae_APPLICATION_LONG_NAME);
	QCoreApplication::setApplicationVersion(DuxReiNummariae_FULL_VERSION);
}

bool Application::notify(QObject* callerObject, QEvent* calledEvent)
try
{
	return QApplication::notify(callerObject, calledEvent);
}
catch(const logic_error& error)
{
	qCritical() << error.what();
	QMessageBox::warning(nullptr, "Development Error", error.what());
	return true;
}
catch(const runtime_error& error)
{
	qCritical() << error.what();
	QMessageBox::warning(nullptr, tr("Budget Application Error"), error.what());
	return true;
}
catch(const exception& error)
{
	QMessageBox::critical(
		nullptr,
		tr("Unexpected Error"),
		tr("The application will now close.\n\n%1").arg(error.what())
	);
	qFatal("%s", error.what());
	return true;
}
catch(...)
{
	QMessageBox::critical(
		nullptr,
		tr("Unknown Fatal Error"),
		tr("The application will close.\n\nAn unexpected error occurred.")
	);
	qFatal("An unexpected error occurred");
	return true;
}
