//
// QasTools: Dektop toolset for the Linux sound system ALSA
//
// License: See COPYING file that comes with this source distribution
//
// Author: Sebastian Holtermann <sebholt@xwmw.org>, (C) 2012
//

#include "qastools_config.hpp"
#include "init_globals.hpp"
#include "single_application.hpp"
#include "desktop_items.hpp"
#include "views/view_utility.hpp"

#include <QDesktopWidget>
#include <QString>
#include <QFile>
#include <QFileInfo>
#include <QIcon>

#include <iostream>
#include <memory>


/// @brief The main function
///
int
main (
	int argc,
	char * argv[] )
{
	init_globals();

	// Qt Application
	Single_Application app ( argc, argv );
	app.setOrganizationName ( QString ( PACKAGE_NAME ).toLower() );
	app.setApplicationName ( QString ( PROGRAM_NAME ).toLower() );
	app.setQuitOnLastWindowClosed ( false );
	app.setAttribute ( Qt::AA_DontShowIconsInMenus, false );

	Desktop_Items ditems;

	// Parse remaining command line options
	{
		const int res ( ditems.init_settings ( argc, argv ) );
		if ( res < 0 ) {
			return res;
		}
	}

	// If an other instance is running send a message to it and quit
	if ( ditems.cmd_opts().start_single_instance ) {
		app.set_unique_key ( SINGLE_APPLICATION_KEY );

		if ( app.is_running() ) {
			if ( app.send_message ( ditems.message_to_other_instance() ) ) {
				return 0;
			}
		}
	}

	// Connect single instance signals with desktop item manager
	{
		QObject::connect ( &app, SIGNAL ( sig_message_available ( QString ) ),
			&ditems, SLOT ( parse_message ( QString ) ) );

		QObject::connect ( &app, SIGNAL ( commitDataRequest ( QSessionManager & ) ),
			&ditems, SLOT ( shutdown() ) );

		QObject::connect ( &ditems, SIGNAL ( sig_quit() ),
			&app, SLOT ( quit() ), Qt::QueuedConnection );
	}

	// Load application icon, translators
	::Views::load_application_icon ( &app, "multimedia-volume-control" );
	::Views::load_translators ( &app );

	// Start and restore session (on demand)
	ditems.start ( app.isSessionRestored() );

	return app.exec();
}

