/*
 * 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_osd_note.h"
#include "config.h"
#include <libnotify/notify.h>

struct _RoccatOsdNote {
	NotifyNotification *note;
};

RoccatOsdNote *roccat_osd_note_new(RoccatNotificator *notificator, gint timeout) {
	RoccatOsdNote *note;
	note = g_malloc0(sizeof(RoccatOsdNote));
	if (notificator->osd_usable) {
#ifdef HAVE_NOTIFY_0_7
		note->note = notify_notification_new("dummy", NULL, NULL);
#else
		note->note = notify_notification_new("dummy", NULL, NULL, NULL);
#endif
		notify_notification_set_timeout(note->note, timeout);
	}
	return note;
}

void roccat_osd_note_free(RoccatOsdNote *note) {
	if (note->note)
		g_object_unref(G_OBJECT(note->note));
	g_free(note);
}

gboolean roccat_osd_note_update_v(RoccatOsdNote *note, gchar const *format, va_list args) {
	gchar *summary;
	gboolean result;
	GError *error = NULL;

	if (!note->note)
		return TRUE;

	summary = g_strdup_vprintf(format, args);

	result = notify_notification_update(note->note, summary, NULL, "roccat");
	if (!result)
		goto free_summary;
	result = notify_notification_show(note->note, &error);
	if (!result)
		goto free_summary;
	g_free(summary);

	return TRUE;
free_summary:
	g_free(summary);
	return FALSE;
}

gboolean roccat_osd_note_update(RoccatOsdNote *note, gchar const *format, ...) {
	va_list args;
	gboolean retval;

	if (!note->note)
		return TRUE;

	va_start(args, format);
	retval = roccat_osd_note_update_v(note, format, args);
	va_end(args);

	return retval;
}

void roccat_osd_note_close(RoccatOsdNote *note) {
	GError *error = NULL;

	if (!note->note)
		return;

	notify_notification_close(note->note, &error);
	g_clear_error(&error);
}
