/*
 * This file is part of roccat-tools.
 *
 * roccat-tools 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.
 *
 * roccat-tools 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 roccat-tools. If not, see <http://www.gnu.org/licenses/>.
 */

#include "roccat_notificator.h"
#include "config.h"
#include <libnotify/notify.h>
#include <unistd.h>

static void init_osd(RoccatNotificator *notificator, gchar const *app_name) {
	notificator->osd_usable = FALSE;

	if (!notify_init(app_name)) {
		g_warning("Error initializing notify");
		return;
	}

	notificator->osd_usable = TRUE;

}

static void init_audio(RoccatNotificator *notificator, gchar const *app_name, gchar const *sound_driver) {
	gint retval;
	gchar *pid_string;

	notificator->audio_usable = FALSE;

	/* Creating own context to be independent of sound scheme. */
	retval = ca_context_create(&notificator->context);
	if (retval) {
		notificator->context = NULL;
		g_warning("Error initializing canberra: %s", ca_strerror(retval));
		return;
	}

	/* Setting driver explicit to be independent of warning sound volume */
	retval = ca_context_set_driver(notificator->context, sound_driver);
	if (retval) {
		g_warning("Error setting canberra driver: %s", ca_strerror(retval));
		goto destroy_context;
	}

	pid_string = g_strdup_printf("%lu", (gulong)getpid());
	retval = ca_context_change_props(notificator->context,
			CA_PROP_APPLICATION_NAME, app_name,
			CA_PROP_APPLICATION_VERSION, VERSION_STRING,
			CA_PROP_APPLICATION_PROCESS_ID, pid_string,
			NULL);
	g_free(pid_string);
	if (retval) {
		g_warning("Error setting canberra properties: %s", ca_strerror(retval));
		goto destroy_context;
		return;
	}

	retval = ca_context_open(notificator->context);
	if (retval) {
		g_warning("Error connecting canberra context: %s", ca_strerror(retval));
		goto destroy_context;
	}

	notificator->audio_usable = TRUE;
	return;

destroy_context:
	ca_context_destroy(notificator->context);
	notificator->context = NULL;
	notificator->audio_usable = FALSE;
}

RoccatNotificator *roccat_notificator_init(gchar const *app_name, gchar const *sound_driver) {
	RoccatNotificator *notificator;

	notificator = g_malloc0(sizeof(RoccatNotificator));

	init_osd(notificator, app_name);
	init_audio(notificator, app_name, sound_driver);

	return notificator;
}

void roccat_notificator_deinit(RoccatNotificator *notificator) {
	gint retval;

	if (notificator->osd_usable)
		notify_uninit();

	if (notificator->audio_usable) {
		retval = ca_context_destroy(notificator->context);
		if (retval)
			g_warning("Error deinitializing canberra: %s", ca_strerror(retval));
	}

	g_free(notificator);
}
