From 55a7061ea39c660dbfb7685a5b1413e4b7b3382f Mon Sep 17 00:00:00 2001
From: Piotr Gaczkowski <doomhammerng@gmail.com>
Date: Fri, 10 Aug 2007 20:18:32 +0000
Subject: [PATCH] Better Tune support

---
 TODO.pep                          |  3 ---
 src/common/connection.py          | 10 --------
 src/common/connection_handlers.py | 10 ++++++++
 src/common/pep.py                 |  6 ++++-
 src/roster_window.py              | 39 ++++++++++++++++---------------
 5 files changed, 35 insertions(+), 33 deletions(-)

diff --git a/TODO.pep b/TODO.pep
index 3e6072e986..05c924dbf4 100644
--- a/TODO.pep
+++ b/TODO.pep
@@ -1,11 +1,8 @@
 • configure access model when changing it in the combobox
-• configure tab in preferences for PEP
 • PEP in status change
 
 Tune use cases:
-• on connection of an account set Tune to current track
 • on disconnection of an account set Tune to None
-• on change of a track set Tune to a new value
 
 Tooltips use cases:
 • Show PEP in contact tooltips
diff --git a/src/common/connection.py b/src/common/connection.py
index 89d22c3de7..393a7d741c 100644
--- a/src/common/connection.py
+++ b/src/common/connection.py
@@ -50,11 +50,6 @@ log = logging.getLogger('gajim.c.connection')
 
 import gtkgui_helpers
 
-from common import dbus_support
-if dbus_support.supported:
-	from music_track_listener import MusicTrackListener
-	import dbus
-
 ssl_error = { 
 2: "Unable to get issuer certificate",
 3: "Unable to get certificate CRL",
@@ -532,11 +527,6 @@ class Connection(ConnectionHandlers):
 			if self.on_connect_auth:
 				self.on_connect_auth(con)
 				self.on_connect_auth = None
-			#FIXME: should set the Tune per account only
-			listener = MusicTrackListener.get()
-			track = listener.get_playing_track()
-			gajim.interface.roster._music_track_changed(listener, track)
-#			print "%s - %s" % (track.artist, track.title)
 		else:
 			# Forget password if needed
 			if not gajim.config.get_per('accounts', self.name, 'savepass'):
diff --git a/src/common/connection_handlers.py b/src/common/connection_handlers.py
index 5d95db71e8..1451da0a80 100644
--- a/src/common/connection_handlers.py
+++ b/src/common/connection_handlers.py
@@ -40,6 +40,11 @@ from common.commands import ConnectionCommands
 from common.pubsub import ConnectionPubSub
 from common.caps import ConnectionCaps
 
+from common import dbus_support
+if dbus_support.supported:
+	import dbus
+	from music_track_listener import MusicTrackListener
+
 STATUS_LIST = ['offline', 'connecting', 'online', 'chat', 'away', 'xa', 'dnd',
 	'invisible', 'error']
 # kind of events we can wait for an answer
@@ -816,6 +821,11 @@ class ConnectionDisco:
 					if identity['category'] == 'pubsub' and identity['type'] == \
 					'pep':
 						self.pep_supported = True
+						listener = MusicTrackListener.get()
+						track = listener.get_playing_track()
+						if gajim.config.get('publish_tune'):
+							gajim.interface.roster._music_track_changed(listener,
+									track, self.name)
 						break
 			if features.__contains__(common.xmpp.NS_BYTESTREAM):
 				gajim.proxy65_manager.resolve(jid, self.connection, self.name)
diff --git a/src/common/pep.py b/src/common/pep.py
index 8b49d899ea..9259820f5c 100644
--- a/src/common/pep.py
+++ b/src/common/pep.py
@@ -101,8 +101,11 @@ def user_send_activity(account, activity, subactivity = '', 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):
-	if gajim.config.get('publish_tune') == False:
+	print "Tune to be created"
+	if (gajim.config.get('publish_tune') == False) or \
+	(gajim.connections[account].pep_supported == False):
 		return
+	print "publish_tune == True and pep_supported"
 	item = xmpp.Node('tune', {'xmlns': xmpp.NS_TUNE})
 	if artist != '':
 		i = item.addChild('artist')
@@ -123,3 +126,4 @@ def user_send_tune(account, artist = '', title = '', source = '', track = 0,leng
 		item.addChild(payload=items)
 
 	gajim.connections[account].send_pb_publish('', xmpp.NS_TUNE, item, '0')
+	print "Tune published"
diff --git a/src/roster_window.py b/src/roster_window.py
index 91146bf99e..803c4ff66d 100644
--- a/src/roster_window.py
+++ b/src/roster_window.py
@@ -3623,14 +3623,14 @@ class RosterWindow:
 				listener = MusicTrackListener.get()
 				self._music_track_changed_signal = listener.connect(
 					'music-track-changed', self._music_track_changed)
-				track = listener.get_playing_track()
-				self._music_track_changed(listener, track)
+			track = listener.get_playing_track()
+			self._music_track_changed(listener, track)
 		else:
 			if self._music_track_changed_signal is not None:
 				listener = MusicTrackListener.get()
 				listener.disconnect(self._music_track_changed_signal)
 				self._music_track_changed_signal = None
-				self._music_track_changed(None, None)
+			self._music_track_changed(None, None)
 
 	def _change_awn_icon_status(self, status):
 		if not dbus_support.supported:
@@ -3658,9 +3658,11 @@ class RosterWindow:
 		except Exception, e:
 			pass
 
-	def _music_track_changed(self, unused_listener, music_track_info):
+	def _music_track_changed(self, unused_listener, music_track_info,
+			account=''):
 		from common import pep
-		accounts = gajim.connections.keys()
+		if account == '':
+			accounts = gajim.connections.keys()
 		if music_track_info is None:
 				artist = ''
 				title = ''
@@ -3681,14 +3683,18 @@ class RosterWindow:
 				status_message = '♪ ' + _('"%(title)s" by %(artist)s') % \
 				{'title': music_track_info.title,
 					'artist': music_track_info.artist } + ' ♪'
-		for account in accounts:
-			if not gajim.config.get_per('accounts', account,
-			'sync_with_global_status'):
-				continue
-			#FIXME: updates should arrive, when all accounts are
-			# connected and we know their abilities
-			if not gajim.connections[account].pep_supported:
-				continue
+		print "change (%s - %s) for %s" % (artist, title, account)
+		if account == '':
+			print "Multi accounts"
+			for account in accounts:
+				if not gajim.config.get_per('accounts', account,
+				'sync_with_global_status'):
+					continue
+				if not gajim.connections[account].pep_supported:
+					continue
+				pep.user_send_tune(account, artist, title, source = '')
+		else:
+			print "Single account"
 			pep.user_send_tune(account, artist, title, source = '')
 
 
@@ -5270,12 +5276,7 @@ class RosterWindow:
 		self.tooltip = tooltips.RosterTooltip()
 		self.draw_roster()
 
-		## Music Track notifications
-		## FIXME: we use a timeout because changing status of
-		## accounts has no effect until they are connected.
-		gobject.timeout_add(1000,
-			self.enable_syncing_status_msg_from_current_music_track,
-			gajim.config.get('pub_tune'))
+		self.enable_syncing_status_msg_from_current_music_track(gajim.config.get('publish_tune'))
 
 		if gajim.config.get('show_roster_on_startup'):
 			self.window.show_all()
-- 
GitLab