view Plugins/General/notify/notify.c @ 1499:959ebef04f70 trunk

[svn] - Don't show "(null)" information.
author nhjm449
date Fri, 04 Aug 2006 02:46:05 -0700
parents 01353675606b
children bf9374436d1d
line wrap: on
line source

/*
 * libnotify plugin for audacious 1.1+
 * http://nenolod.net/code/audacious-notify
 *
 * See `COPYING' in the directory root for license details.
 */
#include <glib.h>
#include <glib/gi18n.h>

#include <libnotify/notify.h>
#include "audacious/plugin.h"
#include "audacious/playlist.h"

#include "config.h"

static void init(void);
static void cleanup(void);
static gboolean watchdog_func(gpointer unused);
static gint timeout_tag = 0;

static gint notify_playlist_pos = -1;
static gchar *previous_title = NULL;

/* our API. */
static void do_notification(gchar *summary, gchar *message, gchar *icon_uri);

GeneralPlugin notify_gp =
{
        NULL,                   /* handle */
        NULL,                   /* filename */
        -1,                     /* xmms_session */
        NULL,                   /* Description */
        init,
        NULL,
        NULL,
        cleanup,
};

GeneralPlugin *get_gplugin_info(void)
{
	notify_gp.description = g_strdup_printf(_("libnotify Plugin %s"), PACKAGE_VERSION);
	return &notify_gp;
}

static void init(void) 
{
	timeout_tag = gtk_timeout_add(100, watchdog_func, NULL);
}

static void cleanup(void)
{
	gtk_timeout_remove(timeout_tag);

	if (previous_title != NULL)
	{
		g_free(previous_title);
		previous_title = NULL;
	}
}

static gboolean watchdog_func(gpointer unused)
{
	gint pos = playlist_get_position();
	gchar *title = playlist_get_songtitle(pos);

	if (pos != notify_playlist_pos ||
		(title != NULL && previous_title != NULL &&
		 g_strcasecmp(title, previous_title)))
	{
		gchar *tmpbuf;
		TitleInput *tuple;

		tuple = playlist_get_tuple(pos);

		if (tuple == NULL)
			return TRUE;

		tmpbuf = g_strdup_printf("<b>%s</b>\n<i>%s</i>\n%s",
			(tuple->performer ? tuple->performer : "Unknown Artist"),
			(tuple->album_name ? tuple->album_name : "Unknown Album"),
			tuple->track_name);

		do_notification("Audacious", tmpbuf, DATA_DIR "/pixmaps/audacious.png");
		g_free(tmpbuf);
	}

	notify_playlist_pos = pos;

	if (previous_title != NULL)
	{
		g_free(previous_title);
		previous_title = NULL;
	}

	previous_title = g_strdup(title);

	return TRUE;
}

static void do_notification(gchar *summary, gchar *message, gchar *icon_uri)
{
	NotifyNotification *n;  
	gint ret;

	/* Initialise libnotify if we haven't done so yet. */
	notify_init(PACKAGE);

	n = notify_notification_new(summary, 
 	                            message,
        	                    NULL, NULL);

	/* make sure we have a notify object before continuing,
	 * for paranoia's sake anyhow. -nenolod
	 */
	if (n == NULL)
		return;

	notify_notification_set_timeout(n, 5000);	/* todo: configurable? */

	if (icon_uri != NULL)
	{
		GdkPixbuf *gp = gdk_pixbuf_new_from_file(icon_uri, NULL);

		if (gp != NULL)
		{
			notify_notification_set_icon_from_pixbuf(n, GDK_PIXBUF(gp));
			g_object_unref(G_OBJECT(gp));
		}
	}

	ret = notify_notification_show(n, NULL);

#if 0
	/* rainy day: handle this condition below -nenolod */
	if (ret == 0)
	{
		/* do something */
	}
#endif

	g_object_unref(G_OBJECT(n));
}