From 19c5c70f805182c15392979eb8b5e12625c4ddb6 Mon Sep 17 00:00:00 2001
From: Piotr Gaczkowski <doomhammerng@gmail.com>
Date: Sun, 1 Apr 2007 09:02:04 +0000
Subject: [PATCH] Initial User Tune support

---
 src/common/contacts.py |  1 +
 src/common/pep.py      | 50 +++++++++++++++++++++++++++++++++++++++++-
 src/roster_window.py   | 22 ++++++++++++++-----
 src/tooltips.py        | 18 +++++++++++++++
 4 files changed, 85 insertions(+), 6 deletions(-)

diff --git a/src/common/contacts.py b/src/common/contacts.py
index 9bad04f83..ee0bc9f35 100644
--- a/src/common/contacts.py
+++ b/src/common/contacts.py
@@ -29,6 +29,7 @@ class Contact:
 		# FIXME
 		self.mood = dict()
 		self.activity = dict()
+		self.tune = dict()
 		self.sub = sub
 		self.ask = ask
 		self.resource = resource
diff --git a/src/common/pep.py b/src/common/pep.py
index 8762b8602..9b65781a1 100644
--- a/src/common/pep.py
+++ b/src/common/pep.py
@@ -18,7 +18,33 @@ def user_mood(items, name, jid):
 						contact.mood['text'] = ch.getData()
 
 def user_tune(items, name, jid):
-	pass
+	(user, resource) = gajim.get_room_and_nick_from_fjid(jid)
+	contacts = gajim.contacts.get_contact(name, user, resource=resource)
+	for item in items.getTags('item'):
+		child = item.getTag('tune')
+		if child is not None:
+			for contact in contacts:
+				if contact.tune.has_key('artist'):
+					del contact.tune['artist']
+				if contact.tune.has_key('title'):
+					del contact.tune['title']
+				if contact.tune.has_key('source'):
+					del contact.tune['source']
+				if contact.tune.has_key('track'):
+					del contact.tune['track']
+				if contact.tune.has_key('length'):
+					del contact.tune['length']
+				for ch in child.getChildren():
+					if ch.getName() == 'artist':
+						contact.tune['artist'] = ch.getData()
+					elif ch.getName() == 'title':
+						contact.tune['title'] = ch.getData()
+					elif ch.getName() == 'source':
+						contact.tune['source'] = ch.getData()
+					elif ch.getName() == 'track':
+						contact.tune['track'] = ch.getData()
+					elif ch.getName() == 'length':
+						contact.tune['length'] = ch.getData()
 
 def user_geoloc(items, name, jid):
 	pass
@@ -63,3 +89,25 @@ def user_send_activity(account, activity, subactivity = '', message = ''):
 		i.addData(message)
 
 	gajim.connections[account].send_pb_publish('', xmpp.NS_ACTIVITY, item, '0')
+
+def user_send_tune(account, artist = '', title = '', source = '', track = 0,length = 0, items = None):
+	item = xmpp.Node('tune', {'xmlns': xmpp.NS_TUNE})
+	if artist != '':
+		i = item.addChild('artist')
+		i.addData(artist)
+	if title != '':
+		i = item.addChild('title')
+		i.addData(title)
+	if source != '':
+		i = item.addChild('source')
+		i.addData(source)
+	if track != 0:
+		i = item.addChild('track')
+		i.addData(track)
+	if length != 0:
+		i = item.addChild('length')
+		i.addData(length)
+	if items is not None:
+		item.addChild(payload=items)
+
+	gajim.connections[account].send_pb_publish('', xmpp.NS_TUNE, item, '0')
diff --git a/src/roster_window.py b/src/roster_window.py
index f94fb59df..8771f40fc 100644
--- a/src/roster_window.py
+++ b/src/roster_window.py
@@ -2984,14 +2984,25 @@ class RosterWindow:
 				self._music_track_changed(None, None)
 	
 	def _music_track_changed(self, unused_listener, music_track_info):
+		from common import pep
 		accounts = gajim.connections.keys()
 		if music_track_info is None:
-			status_message = ''
+				artist = ''
+				title = ''
+				source = ''
+				track = ''
+				length = ''
 		else:
 			if hasattr(music_track_info, 'paused') and \
 			music_track_info.paused == 0:
-				status_message = ''
+				artist = ''
+				title = ''
+				source = ''
+				track = ''
+				length = ''
 			else:
+				artist = music_track_info.artist
+				title = music_track_info.title
 				status_message = '♪ ' + _('"%(title)s" by %(artist)s') % \
 				{'title': music_track_info.title,
 					'artist': music_track_info.artist } + ' ♪'
@@ -2999,10 +3010,11 @@ class RosterWindow:
 			if not gajim.config.get_per('accounts', account,
 			'sync_with_global_status'):
 				continue
-			if not gajim.connections[account].connected:
+			#FIXME: updates should arrive, when all accounts are
+			# connected and we know their abilities
+			if not gajim.connections[account].pep_supported:
 				continue
-			current_show = gajim.SHOW_LIST[gajim.connections[account].connected]
-			self.send_status(account, current_show, status_message)
+			pep.user_send_tune(account, artist, title, source = '')
 
 
 	def update_status_combobox(self):
diff --git a/src/tooltips.py b/src/tooltips.py
index 3bc9ad5db..b5d992111 100644
--- a/src/tooltips.py
+++ b/src/tooltips.py
@@ -1,3 +1,4 @@
+# -*- coding: utf-8 -*-
 ##	tooltips.py
 ##
 ## Copyright (C) 2005-2006 Dimitur Kirov <dkirov@gmail.com>
@@ -497,6 +498,23 @@ class RosterTooltip(NotificationAreaTooltip):
 						activity_string += ' (%s)' % activity_text
 					properties.append((activity_string, None))
 
+				if contact.tune.has_key('artist') or contact.tune.has_key('title'):
+					if contact.tune.has_key('artist'):
+						artist = contact.tune['artist'].strip()
+					else:
+						artist = _('Unknown Artist')
+					if contact.tune.has_key('title'):
+						title = contact.tune['title'].strip()
+					else:
+						title = _('Unknown Title')
+					if contact.tune.has_key('source'):
+						source = contact.tune['source'].strip()
+					else:
+						source = _('Unknown Source')
+					tune_string = '♪ ' + _('<b>"%(title)s"</b> by <i>%(artist)s</i>\nfrom <i>%(source)s</i>' %\
+							{'title': title, 'artist': artist, 'source': source}) + ' ♪'
+					properties.append((tune_string, None))
+
 				if contact.status:
 					status = contact.status.strip()
 					if status: