diff src/scrobbler/plugin.c @ 2306:dd78327f5747

keep track of how long song is played
author Tomasz Mon <desowin@gmail.com>
date Sun, 13 Jan 2008 17:33:47 +0100
parents b8da6a0b0da2
children 3f684f1b1840
line wrap: on
line diff
--- a/src/scrobbler/plugin.c	Sat Jan 12 13:09:10 2008 -0600
+++ b/src/scrobbler/plugin.c	Sun Jan 13 17:33:47 2008 +0100
@@ -53,6 +53,7 @@
 
 static GMutex *hs_mutex, *xs_mutex;
 static GCond *hs_cond, *xs_cond;
+guint track_timeout;
 
 static GeneralPlugin scrobbler_gp =
 {
@@ -91,6 +92,14 @@
 	g_cond_signal(xs_cond);
 }
 
+static void aud_hook_playback_end(gpointer aud_hook_data, gpointer user_data)
+{
+    if (track_timeout) {
+        g_source_remove(track_timeout);
+        track_timeout = 0;
+    }
+}
+
 static void init(void)
 {
 	char *username = NULL, *password = NULL;
@@ -166,6 +175,7 @@
 	}
 
 	aud_hook_associate("playback begin", aud_hook_playback_begin, NULL);
+	aud_hook_associate("playback end", aud_hook_playback_end, NULL);
 
 	pdebug("plugin started", DEBUG);
 }
@@ -205,6 +215,7 @@
 	gerpok_sc_cleaner();
 
 	aud_hook_dissociate("playback begin", aud_hook_playback_begin);
+	aud_hook_dissociate("playback end", aud_hook_playback_end);
 }
 
 static void *xs_thread(void *data __attribute__((unused)))
@@ -253,6 +264,8 @@
 				
 				sc_addentry(m_scrobbler, tuple, aud_tuple_get_int(tuple, FIELD_LENGTH, NULL) / 1000);
 				gerpok_sc_addentry(m_scrobbler, tuple, aud_tuple_get_int(tuple, FIELD_LENGTH, NULL) / 1000);
+                                if (!track_timeout)
+                                    track_timeout = g_timeout_add_seconds(1, sc_timeout, NULL);
 			}
 			else
 				pdebug("tuple does not contain an artist or a title, not submitting.", DEBUG);