[Link Mauve & I] store bookmarks in pubsub if server supports that.
This commit is contained in:
		
							parent
							
								
									66254bc47f
								
							
						
					
					
						commit
						4a0e5bc2b3
					
				
					 3 changed files with 88 additions and 48 deletions
				
			
		|  | @ -1589,38 +1589,47 @@ class Connection(ConnectionHandlers): | ||||||
| 		self.connection.send(iq) | 		self.connection.send(iq) | ||||||
| 
 | 
 | ||||||
| 	def get_bookmarks(self): | 	def get_bookmarks(self): | ||||||
| 		'''Get Bookmarks from storage as described in XEP 0048''' | 		'''Get Bookmarks from storage or PubSub if supported as described in | ||||||
|  | 		XEP 0048''' | ||||||
| 		self.bookmarks = [] #avoid multiple bookmarks when re-connecting | 		self.bookmarks = [] #avoid multiple bookmarks when re-connecting | ||||||
| 		if not self.connection: | 		if not self.connection: | ||||||
| 			return | 			return | ||||||
|  | 		if self.pubsub_supported: | ||||||
|  | 			self.send_pb_retrieve('', 'storage:bookmarks', self._PrivatePubsubCB) | ||||||
|  | 		else: | ||||||
| 			iq = common.xmpp.Iq(typ='get') | 			iq = common.xmpp.Iq(typ='get') | ||||||
| 			iq2 = iq.addChild(name='query', namespace=common.xmpp.NS_PRIVATE) | 			iq2 = iq.addChild(name='query', namespace=common.xmpp.NS_PRIVATE) | ||||||
| 			iq2.addChild(name='storage', namespace='storage:bookmarks') | 			iq2.addChild(name='storage', namespace='storage:bookmarks') | ||||||
| 			self.connection.send(iq) | 			self.connection.send(iq) | ||||||
| 
 | 
 | ||||||
| 	def store_bookmarks(self): | 	def store_bookmarks(self): | ||||||
| 		''' Send bookmarks to the storage namespace ''' | 		''' Send bookmarks to the storage namespace or PubSub if supported''' | ||||||
| 		if not self.connection: | 		if not self.connection: | ||||||
| 			return | 			return | ||||||
| 		iq = common.xmpp.Iq(typ='set') | 		iq = common.xmpp.Node(tag='storage', attrs={'xmlns': 'storage:bookmarks'}) | ||||||
| 		iq2 = iq.addChild(name='query', namespace=common.xmpp.NS_PRIVATE) |  | ||||||
| 		iq3 = iq2.addChild(name='storage', namespace='storage:bookmarks') |  | ||||||
| 		for bm in self.bookmarks: | 		for bm in self.bookmarks: | ||||||
| 			iq4 = iq3.addChild(name = "conference") | 			iq2 = iq.addChild(name = "conference") | ||||||
| 			iq4.setAttr('jid', bm['jid']) | 			iq2.setAttr('jid', bm['jid']) | ||||||
| 			iq4.setAttr('autojoin', bm['autojoin']) | 			iq2.setAttr('autojoin', bm['autojoin']) | ||||||
| 			iq4.setAttr('minimize', bm['minimize']) | 			iq2.setAttr('minimize', bm['minimize']) | ||||||
| 			iq4.setAttr('name', bm['name']) | 			iq2.setAttr('name', bm['name']) | ||||||
| 			# Only add optional elements if not empty | 			# Only add optional elements if not empty | ||||||
| 			# Note: need to handle both None and '' as empty | 			# Note: need to handle both None and '' as empty | ||||||
| 			#   thus shouldn't use "is not None" | 			#   thus shouldn't use "is not None" | ||||||
| 			if bm.get('nick', None): | 			if bm.get('nick', None): | ||||||
| 				iq4.setTagData('nick', bm['nick']) | 				iq2.setTagData('nick', bm['nick']) | ||||||
| 			if bm.get('password', None): | 			if bm.get('password', None): | ||||||
| 				iq4.setTagData('password', bm['password']) | 				iq2.setTagData('password', bm['password']) | ||||||
| 			if bm.get('print_status', None): | 			if bm.get('print_status', None): | ||||||
| 				iq4.setTagData('print_status', bm['print_status']) | 				iq2.setTagData('print_status', bm['print_status']) | ||||||
| 		self.connection.send(iq) | 
 | ||||||
|  | 		if self.pubsub_supported: | ||||||
|  | 			self.send_pb_publish('', 'storage:bookmarks', iq, 'current') | ||||||
|  | 		else: | ||||||
|  | 			iqA = common.xmpp.Iq(typ='set') | ||||||
|  | 			iqB = iqA.addChild(name='query', namespace=common.xmpp.NS_PRIVATE) | ||||||
|  | 			iqB.addChild(node=iq) | ||||||
|  | 			self.connection.send(iqA) | ||||||
| 
 | 
 | ||||||
| 	def get_annotations(self): | 	def get_annotations(self): | ||||||
| 		'''Get Annonations from storage as described in XEP 0048, and XEP 0145''' | 		'''Get Annonations from storage as described in XEP 0048, and XEP 0145''' | ||||||
|  |  | ||||||
|  | @ -1527,6 +1527,44 @@ class ConnectionHandlers(ConnectionVcard, ConnectionBytestream, ConnectionDisco, | ||||||
| 		if storage: | 		if storage: | ||||||
| 			ns = storage.getNamespace() | 			ns = storage.getNamespace() | ||||||
| 			if ns == 'storage:bookmarks': | 			if ns == 'storage:bookmarks': | ||||||
|  | 				self._parse_bookmarks(storage) | ||||||
|  | 			elif ns == 'gajim:prefs': | ||||||
|  | 				# Preferences data | ||||||
|  | 				# http://www.xmpp.org/extensions/xep-0049.html | ||||||
|  | 				#TODO: implement this | ||||||
|  | 				pass | ||||||
|  | 			elif ns == 'storage:rosternotes': | ||||||
|  | 				# Annotations | ||||||
|  | 				# http://www.xmpp.org/extensions/xep-0145.html | ||||||
|  | 				notes = storage.getTags('note') | ||||||
|  | 				for note in notes: | ||||||
|  | 					try: | ||||||
|  | 						jid = helpers.parse_jid(note.getAttr('jid')) | ||||||
|  | 					except common.helpers.InvalidFormat: | ||||||
|  | 						log.warn('Invalid JID: %s, ignoring it' % note.getAttr('jid')) | ||||||
|  | 						continue | ||||||
|  | 					annotation = note.getData() | ||||||
|  | 					self.annotations[jid] = annotation | ||||||
|  | 
 | ||||||
|  | 	def _PrivatePubsubCB(self, conn, request): | ||||||
|  | 		'''Private data from PubSub''' | ||||||
|  | 		gajim.log.debug('_PrivatePubsubCB') | ||||||
|  | 		pubsub = request.getTag('pubsub') | ||||||
|  | 		if not pubsub: | ||||||
|  | 			return | ||||||
|  | 		items = pubsub.getTag('items') | ||||||
|  | 		if not items: | ||||||
|  | 			return | ||||||
|  | 		item = items.getTag('item') | ||||||
|  | 		if not item: | ||||||
|  | 			return | ||||||
|  | 		storage = item.getTag('storage') | ||||||
|  | 		if storage: | ||||||
|  | 			ns = storage.getNamespace() | ||||||
|  | 			if ns == 'storage:bookmarks': | ||||||
|  | 				self._parse_bookmarks(storage) | ||||||
|  | 
 | ||||||
|  | 	def _parse_bookmarks(self, storage): | ||||||
| 		# Bookmarked URLs and Conferences | 		# Bookmarked URLs and Conferences | ||||||
| 		# http://www.xmpp.org/extensions/xep-0048.html | 		# http://www.xmpp.org/extensions/xep-0048.html | ||||||
| 		confs = storage.getTags('conference') | 		confs = storage.getTags('conference') | ||||||
|  | @ -1555,24 +1593,6 @@ class ConnectionHandlers(ConnectionVcard, ConnectionBytestream, ConnectionDisco, | ||||||
| 			self.bookmarks.append(bm) | 			self.bookmarks.append(bm) | ||||||
| 		self.dispatch('BOOKMARKS', self.bookmarks) | 		self.dispatch('BOOKMARKS', self.bookmarks) | ||||||
| 
 | 
 | ||||||
| 			elif ns == 'gajim:prefs': |  | ||||||
| 				# Preferences data |  | ||||||
| 				# http://www.xmpp.org/extensions/xep-0049.html |  | ||||||
| 				#TODO: implement this |  | ||||||
| 				pass |  | ||||||
| 			elif ns == 'storage:rosternotes': |  | ||||||
| 				# Annotations |  | ||||||
| 				# http://www.xmpp.org/extensions/xep-0145.html |  | ||||||
| 				notes = storage.getTags('note') |  | ||||||
| 				for note in notes: |  | ||||||
| 					try: |  | ||||||
| 						jid = helpers.parse_jid(note.getAttr('jid')) |  | ||||||
| 					except common.helpers.InvalidFormat: |  | ||||||
| 						log.warn('Invalid JID: %s, ignoring it' % note.getAttr('jid')) |  | ||||||
| 						continue |  | ||||||
| 					annotation = note.getData() |  | ||||||
| 					self.annotations[jid] = annotation |  | ||||||
| 
 |  | ||||||
| 	def _rosterSetCB(self, con, iq_obj): | 	def _rosterSetCB(self, con, iq_obj): | ||||||
| 		log.debug('rosterSetCB') | 		log.debug('rosterSetCB') | ||||||
| 		version = iq_obj.getTagAttr('query', 'ver') | 		version = iq_obj.getTagAttr('query', 'ver') | ||||||
|  |  | ||||||
|  | @ -75,6 +75,17 @@ class ConnectionPubSub: | ||||||
| 
 | 
 | ||||||
| 		self.connection.send(query) | 		self.connection.send(query) | ||||||
| 
 | 
 | ||||||
|  | 	def send_pb_retrieve(self, jid, node, cb, *args, **kwargs):  | ||||||
|  | 		'''Get items from a node'''  | ||||||
|  | 		if not self.connection or self.connected < 2:  | ||||||
|  | 			return  | ||||||
|  | 		query = xmpp.Iq('get', to=jid)  | ||||||
|  | 		r = query.addChild('pubsub', namespace=xmpp.NS_PUBSUB)  | ||||||
|  | 		r = r.addChild('items', {'node': node})  | ||||||
|  | 		id_ = self.connection.send(query) | ||||||
|  | 
 | ||||||
|  | 		self.__callbacks[id_]=(cb, args, kwargs) | ||||||
|  | 
 | ||||||
| 	def send_pb_retract(self, jid, node, id_): | 	def send_pb_retract(self, jid, node, id_): | ||||||
| 		'''Delete item from a node''' | 		'''Delete item from a node''' | ||||||
| 		if not self.connection or self.connected < 2: | 		if not self.connection or self.connected < 2: | ||||||
|  |  | ||||||
		Loading…
	
	Add table
		
		Reference in a new issue