/*
 * 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 <libnotify/notify.h>

RoccatNotificator *roccat_notificator_init(gchar const *app_name) {
	RoccatNotificator *notificator;
	gint retval;

	notificator = g_malloc0(sizeof(RoccatNotificator));

	if (!notify_init(app_name))
		g_warning("Error initializing notify");
	else
		notificator->osd_usable = TRUE;

	/*
	 * Creating own context to be independent of sound scheme.
	 * Being dependent of warning sound volume unless I set driver explicit.
	 */
	retval = ca_context_create(&notificator->context);
	if (!retval) {
		retval = ca_context_change_props(notificator->context,
				CA_PROP_APPLICATION_NAME, app_name,
				NULL);
		if (!retval)
			notificator->audio_usable = TRUE;
		else {
			ca_context_destroy(notificator->context);
			notificator->context = NULL;
			g_warning("Error initializing canberra: %s", ca_strerror(retval));
		}
	} else
		g_warning("Error initializing canberra: %s", ca_strerror(retval));

	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);
}
