pep_tune.c
changeset 31 e404cd1c7077
parent 30 a66ed0454ca8
child 35 a77a8e7ab8ae
--- a/pep_tune.c	Mon May 21 03:38:51 2012 +0300
+++ b/pep_tune.c	Mon May 21 19:10:25 2012 +0300
@@ -30,7 +30,7 @@
 #include <mcabber/modules.h>
 
 #include "pep.h"
-#include "tune.h"
+#include "pep_tune.h"
 
 #include "config.h"
 
@@ -41,9 +41,7 @@
 void pep_tune_init   (void);
 void pep_tune_uninit (void);
 
-#define DESCRIPTION ( \
-	"PEP tune event handler\n" \
-	"Optios: " OPT_TUNE_PAUSE_IS_STOP ", " OPT_TUNE_INTERVAL )
+#define DESCRIPTION ( PEP_TUNE_DESCRIPTION )
 
 static const gchar *deps[] = { "pep", NULL };
 
@@ -219,64 +217,7 @@
 	lm_message_unref (request);
 }
 
-void tune_publish (const tune_pair_t *pairs)
-{
-	gboolean           publish    = FALSE;
-	const tune_pair_t *tag;
-	tune_pair_t        new_info[] = {
-		{ "artist", NULL },
-		{ "length", NULL },
-		{ "rating", NULL },
-		{ "source", NULL },
-		{ "title",  NULL },
-		{ "track",  NULL },
-		{ "uri",    NULL },
-		{ NULL,     NULL },
-	};
-
-	// populate new_info with new values
-	for ( tag = pairs; tag -> name; ++ tag )
-		if ( ! g_strcmp0 ( tag -> name, "state" ) ) {
-			if ( ( ! g_strcmp0 ( tag -> value, "stop" ) ) ||
-				 ( ( ! g_strcmp0 ( tag -> value, "pause" ) ) &&
-				   settings_opt_get_int ( OPT_TUNE_PAUSE_IS_STOP ) ) ) {
-				int i;
-				for ( i = 0; i <= MAX_NO; ++ i )
-					new_info [ i ] .value = NULL;
-				break;
-			}
-		} else {
-			int i;
-			for ( i = 0; i <= MAX_NO; ++ i )
-				if ( ! g_strcmp0 ( tag -> name, new_info [ i  ] .name ) )
-					new_info [ i ] .value = tag -> value;
-		}
-
-	{ // check, if it differ from info
-		int i;
-		for (i = 0; i <= MAX_NO; ++i)
-			if (g_strcmp0 (new_info[i].value, info[i].value)) {
-				publish = TRUE;
-				break;
-			}
-	}
-
-	if (publish) {
-
-		{ // copy new values to info
-			int i;
-			for (i = 0; i <= MAX_NO; ++i) {
-				if (info[i].value)
-					g_free (info[i].value);
-				info[i].value = g_strdup (new_info[i].value);
-			}
-		}
-
-		tune_publish_info ();
-	}
-}
-
-gboolean tune_request ( const gchar *to, GError **err )
+gboolean pep_tune_request ( const gchar *to, GError **err )
 {
 	LmMessage *request;
 	LmMessageNode *node;
@@ -367,7 +308,49 @@
 
 static guint tune_htoh ( const gchar *htype, hk_arg_t *args, gpointer udata )
 {
-	tune_publish ( (const tune_pair_t *) args );
+	gboolean        publish = FALSE;
+	gboolean        stop    = FALSE;
+
+	{ // check state
+		const hk_arg_t *arg;
+		for ( arg = args; arg -> name != NULL; ++ arg ) {
+			if ( ! g_strcmp0 ( arg -> name, "state" ) ) {
+				stop = ( ! g_strcmp0 ( arg -> value, "stop" ) ) ||
+					   ( ( ! g_strcmp0 ( arg -> value, "pause" ) ) && settings_opt_get_int ( OPT_TUNE_PAUSE_IS_STOP ) );
+				break;
+			}
+		}
+	}
+
+	if ( stop ) { // ignore info
+		int i;
+		for ( i = 0; i <= MAX_NO; ++ i )
+			if ( info [ i ] .value != NULL ) {
+				g_free ( info [ i ] .value );
+				info [ i ] .value = NULL;
+				publish = TRUE;
+			}
+	} else { // populate info with new values
+		int i;
+		for ( i = 0; i <= MAX_NO; ++ i ) {
+			const hk_arg_t *arg;
+			for ( arg = args; arg -> name != NULL; ++ arg ) {
+				if ( ! g_strcmp0 ( arg -> name, info [ i ] .name ) ) {
+					if ( g_strcmp0 ( arg -> value, info [ i ] .value ) ) {
+						if ( info [ i ] .value )
+							g_free ( info [ i ] .value );
+						info [ i ] .value = g_strdup ( arg -> value );
+						publish = TRUE;
+					}
+					break;
+				}
+			}
+		}
+	}
+
+	if ( publish )
+		tune_publish_info ();
+
 	return HOOK_HANDLER_RESULT_ALLOW_MORE_HANDLERS;
 }