2018-07-04 23:23:28 +02:00
|
|
|
# This file is part of Gajim.
|
|
|
|
#
|
|
|
|
# Gajim is free software; you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published
|
|
|
|
# by the Free Software Foundation; version 3 only.
|
|
|
|
#
|
|
|
|
# Gajim is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with Gajim. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
# XEP-0118: User Tune
|
|
|
|
|
2018-09-13 23:56:12 +02:00
|
|
|
from typing import Any
|
|
|
|
from typing import List # pylint: disable=unused-import
|
|
|
|
from typing import Dict
|
|
|
|
from typing import Optional
|
|
|
|
from typing import Tuple
|
|
|
|
|
2018-07-04 23:23:28 +02:00
|
|
|
import logging
|
|
|
|
|
|
|
|
import nbxmpp
|
|
|
|
from gi.repository import GLib
|
|
|
|
|
2018-09-13 23:56:12 +02:00
|
|
|
from gajim.common.i18n import _
|
2018-07-04 23:23:28 +02:00
|
|
|
from gajim.common.const import PEPEventType
|
|
|
|
from gajim.common.exceptions import StanzaMalformed
|
|
|
|
from gajim.common.modules.pep import AbstractPEPModule, AbstractPEPData
|
2018-09-13 23:56:12 +02:00
|
|
|
from gajim.common.types import UserTuneDataT
|
2018-07-04 23:23:28 +02:00
|
|
|
|
|
|
|
log = logging.getLogger('gajim.c.m.user_tune')
|
|
|
|
|
|
|
|
|
|
|
|
class UserTuneData(AbstractPEPData):
|
|
|
|
|
|
|
|
type_ = PEPEventType.TUNE
|
|
|
|
|
2018-09-13 23:56:12 +02:00
|
|
|
def as_markup_text(self) -> str:
|
|
|
|
if self.data is None:
|
|
|
|
return ''
|
|
|
|
|
2018-09-12 00:01:54 +02:00
|
|
|
tune = self.data
|
2018-07-04 23:23:28 +02:00
|
|
|
|
|
|
|
artist = tune.get('artist', _('Unknown Artist'))
|
|
|
|
artist = GLib.markup_escape_text(artist)
|
|
|
|
|
|
|
|
title = tune.get('title', _('Unknown Title'))
|
|
|
|
title = GLib.markup_escape_text(title)
|
|
|
|
|
|
|
|
source = tune.get('source', _('Unknown Source'))
|
|
|
|
source = GLib.markup_escape_text(source)
|
|
|
|
|
|
|
|
tune_string = _('<b>"%(title)s"</b> by <i>%(artist)s</i>\n'
|
|
|
|
'from <i>%(source)s</i>') % {'title': title,
|
|
|
|
'artist': artist,
|
|
|
|
'source': source}
|
|
|
|
return tune_string
|
|
|
|
|
|
|
|
|
|
|
|
class UserTune(AbstractPEPModule):
|
|
|
|
|
|
|
|
name = 'tune'
|
|
|
|
namespace = nbxmpp.NS_TUNE
|
|
|
|
pep_class = UserTuneData
|
|
|
|
store_publish = True
|
|
|
|
_log = log
|
|
|
|
|
2018-09-13 23:56:12 +02:00
|
|
|
def _extract_info(self, item: nbxmpp.Node) -> Optional[Dict[str, str]]:
|
2018-07-04 23:23:28 +02:00
|
|
|
tune_dict = {}
|
|
|
|
tune_tag = item.getTag('tune', namespace=self.namespace)
|
|
|
|
if tune_tag is None:
|
2018-07-05 18:14:36 +02:00
|
|
|
raise StanzaMalformed('No tune node')
|
2018-07-04 23:23:28 +02:00
|
|
|
|
|
|
|
for child in tune_tag.getChildren():
|
|
|
|
name = child.getName().strip()
|
|
|
|
data = child.getData().strip()
|
|
|
|
if child.getName() in ['artist', 'title', 'source',
|
|
|
|
'track', 'length']:
|
|
|
|
tune_dict[name] = data
|
|
|
|
|
|
|
|
return tune_dict or None
|
|
|
|
|
2018-09-13 23:56:12 +02:00
|
|
|
def _build_node(self, data: UserTuneDataT) -> nbxmpp.Node:
|
2018-07-04 23:23:28 +02:00
|
|
|
item = nbxmpp.Node('tune', {'xmlns': nbxmpp.NS_TUNE})
|
2018-07-05 19:21:58 +02:00
|
|
|
if data is None:
|
|
|
|
return item
|
|
|
|
artist, title, source, track, length = data
|
2018-07-04 23:23:28 +02:00
|
|
|
if artist:
|
|
|
|
item.addChild('artist', payload=artist)
|
|
|
|
if title:
|
|
|
|
item.addChild('title', payload=title)
|
|
|
|
if source:
|
|
|
|
item.addChild('source', payload=source)
|
|
|
|
if track:
|
|
|
|
item.addChild('track', payload=track)
|
|
|
|
if length:
|
|
|
|
item.addChild('length', payload=length)
|
|
|
|
return item
|
2018-07-07 13:52:44 +02:00
|
|
|
|
|
|
|
|
2018-09-13 23:56:12 +02:00
|
|
|
def get_instance(*args: Any, **kwargs: Any) -> Tuple[UserTune, str]:
|
2018-07-07 13:52:44 +02:00
|
|
|
return UserTune(*args, **kwargs), 'UserTune'
|