Commit d564acac authored by Weblate's avatar Weblate
Browse files

[whiteboard] Port to Py3 / GTK3

parent 72db56fd
from .plugin import WhiteboardPlugin
[info]
name: Whiteboard
short_name: whiteboard
version: 0.3
description: Shows a whiteboard in chat. python-pygoocanvas is required.
authors = Yann Leboulanger <asterix@lagaule.org>
homepage = https://dev.gajim.org/gajim/gajim-plugins/wikis/WhiteboardPlugin
min_gajim_version: 0.16.11
## plugins/whiteboard/plugin.py
##
## Copyright (C) 2009 Jeff Ling <jeff.ummu AT gmail.com>
## Copyright (C) 2010 Yann Leboulanger <asterix AT lagaule.org>
##
## 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/>.
##
'''
Whiteboard plugin.
:author: Yann Leboulanger <asterix@lagaule.org>
:since: 1st November 2010
:copyright: Copyright (2010) Yann Leboulanger <asterix@lagaule.org>
:license: GPL
'''
from gajim import common
from gajim.common import helpers
from gajim.common import app
from gajim.plugins import GajimPlugin
from gajim.plugins.gajimplugin import GajimPluginException
from gajim.plugins.helpers import log_calls, log
from nbxmpp import Message
from gi.repository import Gtk
from gi.repository import GdkPixbuf
from gajim import chat_control
from gajim.common import ged
from gajim.common.jingle_session import JingleSession
from gajim.common.jingle_content import JingleContent
from gajim.common.jingle_transport import JingleTransport, TransportType
from gajim import dialogs
from .whiteboard_widget import Whiteboard, HAS_GOOCANVAS
from gajim.common import caps_cache
NS_JINGLE_XHTML = 'urn:xmpp:tmp:jingle:apps:xhtml'
NS_JINGLE_SXE = 'urn:xmpp:tmp:jingle:transports:sxe'
NS_SXE = 'urn:xmpp:sxe:0'
class WhiteboardPlugin(GajimPlugin):
@log_calls('WhiteboardPlugin')
def init(self):
self.config_dialog = None
self.events_handlers = {
'jingle-request-received': (ged.GUI1, self._nec_jingle_received),
'jingle-connected-received': (ged.GUI1, self._nec_jingle_connected),
'jingle-disconnected-received': (ged.GUI1,
self._nec_jingle_disconnected),
'raw-message-received': (ged.GUI1, self._nec_raw_message),
}
self.gui_extension_points = {
'chat_control_base' : (self.connect_with_chat_control,
self.disconnect_from_chat_control),
'chat_control_base_update_toolbar': (self.update_button_state,
None),
}
self.controls = []
self.sid = None
@log_calls('WhiteboardPlugin')
def _compute_caps_hash(self):
for a in app.connections:
app.caps_hash[a] = caps_cache.compute_caps_hash([
app.gajim_identity], app.gajim_common_features + \
app.gajim_optional_features[a])
# re-send presence with new hash
connected = app.connections[a].connected
if connected > 1 and app.SHOW_LIST[connected] != 'invisible':
app.connections[a].change_status(app.SHOW_LIST[connected],
app.connections[a].status)
@log_calls('WhiteboardPlugin')
def activate(self):
if not HAS_GOOCANVAS:
raise GajimPluginException('python-pygoocanvas is missing!')
if NS_JINGLE_SXE not in app.gajim_common_features:
app.gajim_common_features.append(NS_JINGLE_SXE)
if NS_SXE not in app.gajim_common_features:
app.gajim_common_features.append(NS_SXE)
self._compute_caps_hash()
@log_calls('WhiteboardPlugin')
def deactivate(self):
if NS_JINGLE_SXE in app.gajim_common_features:
app.gajim_common_features.remove(NS_JINGLE_SXE)
if NS_SXE in app.gajim_common_features:
app.gajim_common_features.remove(NS_SXE)
self._compute_caps_hash()
@log_calls('WhiteboardPlugin')
def connect_with_chat_control(self, control):
if isinstance(control, chat_control.ChatControl):
base = Base(self, control)
self.controls.append(base)
@log_calls('WhiteboardPlugin')
def disconnect_from_chat_control(self, chat_control):
for base in self.controls:
base.disconnect_from_chat_control()
self.controls = []
@log_calls('WhiteboardPlugin')
def update_button_state(self, control):
for base in self.controls:
if base.chat_control == control:
if control.contact.supports(NS_JINGLE_SXE) and \
control.contact.supports(NS_SXE):
base.button.set_sensitive(True)
tooltip_text = _('Show whiteboard')
else:
base.button.set_sensitive(False)
tooltip_text = _('Client on the other side '
'does not support the whiteboard')
base.button.set_tooltip_text(tooltip_text)
@log_calls('WhiteboardPlugin')
def show_request_dialog(self, account, fjid, jid, sid, content_types):
def on_ok():
session = app.connections[account].get_jingle_session(fjid, sid)
self.sid = session.sid
print(session.accepted)
if not session.accepted:
session.approve_session()
for content in content_types:
session.approve_content('xhtml')
for _jid in (fjid, jid):
ctrl = app.interface.msg_win_mgr.get_control(_jid, account)
if ctrl:
break
if not ctrl:
# create it
app.interface.new_chat_from_jid(account, jid)
ctrl = app.interface.msg_win_mgr.get_control(jid, account)
session = session.contents[('initiator', 'xhtml')]
ctrl.draw_whiteboard(session)
def on_cancel():
session = app.connections[account].get_jingle_session(fjid, sid)
session.decline_session()
contact = app.contacts.get_first_contact_from_jid(account, jid)
if contact:
name = contact.get_shown_name()
else:
name = jid
pritext = _('Incoming Whiteboard')
sectext = _('%(name)s (%(jid)s) wants to start a whiteboard with '
'you. Do you want to accept?') % {'name': name, 'jid': jid}
dialog = dialogs.NonModalConfirmationDialog(pritext, sectext=sectext,
on_response_ok=on_ok, on_response_cancel=on_cancel)
dialog.popup()
@log_calls('WhiteboardPlugin')
def _nec_jingle_received(self, obj):
if not HAS_GOOCANVAS:
return
content_types = obj.contents.media
if content_types != 'xhtml':
return
self.show_request_dialog(obj.conn.name, obj.fjid, obj.jid, obj.sid,
content_types)
@log_calls('WhiteboardPlugin')
def _nec_jingle_connected(self, obj):
if not HAS_GOOCANVAS:
return
account = obj.conn.name
ctrl = (app.interface.msg_win_mgr.get_control(obj.fjid, account)
or app.interface.msg_win_mgr.get_control(obj.jid, account))
if not ctrl:
return
session = app.connections[obj.conn.name].get_jingle_session(obj.fjid,
obj.sid)
if ('initiator', 'xhtml') not in session.contents:
return
session = session.contents[('initiator', 'xhtml')]
ctrl.draw_whiteboard(session)
@log_calls('WhiteboardPlugin')
def _nec_jingle_disconnected(self, obj):
for base in self.controls:
if base.sid == obj.sid:
base.stop_whiteboard(reason = obj.reason)
@log_calls('WhiteboardPlugin')
def _nec_raw_message(self, obj):
if not HAS_GOOCANVAS:
return
if obj.stanza.getTag('sxe', namespace=NS_SXE):
account = obj.conn.name
try:
fjid = helpers.get_full_jid_from_iq(obj.stanza)
except helpers.InvalidFormat:
obj.conn.dispatch('ERROR', (_('Invalid Jabber ID'),
_('A message from a non-valid JID arrived, it has been '
'ignored.')))
jid = app.get_jid_without_resource(fjid)
ctrl = (app.interface.msg_win_mgr.get_control(fjid, account)
or app.interface.msg_win_mgr.get_control(jid, account))
if not ctrl:
return
sxe = obj.stanza.getTag('sxe')
if not sxe:
return
sid = sxe.getAttr('session')
if (jid, sid) not in obj.conn._sessions:
pass
# newjingle = JingleSession(con=self, weinitiate=False, jid=jid, sid=sid)
# self.addJingle(newjingle)
# we already have such session in dispatcher...
session = obj.conn.get_jingle_session(fjid, sid)
cn = session.contents[('initiator', 'xhtml')]
error = obj.stanza.getTag('error')
if error:
action = 'iq-error'
else:
action = 'edit'
cn.on_stanza(obj.stanza, sxe, error, action)
# def __editCB(self, stanza, content, error, action):
#new_tags = sxe.getTags('new')
#remove_tags = sxe.getTags('remove')
#if new_tags is not None:
## Process new elements
#for tag in new_tags:
#if tag.getAttr('type') == 'element':
#ctrl.whiteboard.recieve_element(tag)
#elif tag.getAttr('type') == 'attr':
#ctrl.whiteboard.recieve_attr(tag)
#ctrl.whiteboard.apply_new()
#if remove_tags is not None:
## Delete rids
#for tag in remove_tags:
#target = tag.getAttr('target')
#ctrl.whiteboard.image.del_rid(target)
# Stop propagating this event, it's handled
return True
class Base(object):
def __init__(self, plugin, chat_control):
self.plugin = plugin
self.chat_control = chat_control
self.chat_control.draw_whiteboard = self.draw_whiteboard
self.contact = self.chat_control.contact
self.account = self.chat_control.account
self.jid = self.contact.get_full_jid()
self.create_buttons()
self.whiteboard = None
self.sid = None
def create_buttons(self):
# create whiteboard button
actions_hbox = self.chat_control.xml.get_object('actions_hbox')
self.button = Gtk.ToggleButton()
self.button.set_property('relief', Gtk.ReliefStyle.NONE)
self.button.set_property('can-focus', False)
img = Gtk.Image()
img_path = self.plugin.local_file_path('whiteboard.png')
pixbuf = GdkPixbuf.Pixbuf.new_from_file(img_path)
iconset = Gtk.IconSet(pixbuf=pixbuf)
factory = Gtk.IconFactory()
factory.add('whiteboard', iconset)
img_path = self.plugin.local_file_path('brush_tool.png')
pixbuf = GdkPixbuf.Pixbuf.new_from_file(img_path)
iconset = Gtk.IconSet(pixbuf=pixbuf)
factory.add('brush_tool', iconset)
img_path = self.plugin.local_file_path('line_tool.png')
pixbuf = GdkPixbuf.Pixbuf.new_from_file(img_path)
iconset = Gtk.IconSet(pixbuf=pixbuf)
factory.add('line_tool', iconset)
img_path = self.plugin.local_file_path('oval_tool.png')
pixbuf = GdkPixbuf.Pixbuf.new_from_file(img_path)
iconset = Gtk.IconSet(pixbuf=pixbuf)
factory.add('oval_tool', iconset)
factory.add_default()
img.set_from_stock('whiteboard', Gtk.IconSize.MENU)
self.button.set_image(img)
send_button = self.chat_control.xml.get_object('send_button')
send_button_pos = actions_hbox.child_get_property(send_button,
'position')
actions_hbox.pack_start(self.button, False, False, 0)
actions_hbox.reorder_child(self.button, send_button_pos - 1)
id_ = self.button.connect('toggled', self.on_whiteboard_button_toggled)
self.chat_control.handlers[id_] = self.button
self.button.show()
def draw_whiteboard(self, content):
hbox = self.chat_control.xml.get_object('chat_control_hbox')
if len(hbox.get_children()) == 1:
self.whiteboard = Whiteboard(self.account, self.contact, content,
self.plugin)
# set minimum size
self.whiteboard.hbox.set_size_request(300, 0)
hbox.pack_start(self.whiteboard.hbox, False, False, 0)
self.whiteboard.hbox.show_all()
self.button.set_active(True)
content.control = self
self.sid = content.session.sid
def on_whiteboard_button_toggled(self, widget):
"""
Popup whiteboard
"""
if widget.get_active():
if not self.whiteboard:
self.start_whiteboard()
else:
self.stop_whiteboard()
def start_whiteboard(self):
conn = app.connections[self.chat_control.account]
jingle = JingleSession(conn, weinitiate=True, jid=self.jid)
self.sid = jingle.sid
conn._sessions[jingle.sid] = jingle
content = JingleWhiteboard(jingle)
content.control = self
jingle.add_content('xhtml', content)
jingle.start_session()
def stop_whiteboard(self, reason=None):
conn = app.connections[self.chat_control.account]
self.sid = None
session = conn.get_jingle_session(self.jid, media='xhtml')
if session:
session.end_session()
self.button.set_active(False)
if reason:
txt = _('Whiteboard stopped: %(reason)s') % {'reason': reason}
self.chat_control.print_conversation(txt, 'info')
if not self.whiteboard:
return
hbox = self.chat_control.xml.get_object('chat_control_hbox')
if self.whiteboard.hbox in hbox.get_children():
if hasattr(self.whiteboard, 'hbox'):
hbox.remove(self.whiteboard.hbox)
self.whiteboard = None
def disconnect_from_chat_control(self):
actions_hbox = self.chat_control.xml.get_object('actions_hbox')
actions_hbox.remove(self.button)
class JingleWhiteboard(JingleContent):
''' Jingle Whiteboard sessions consist of xhtml content'''
def __init__(self, session, transport=None, senders=None):
if not transport:
transport = JingleTransportSXE()
JingleContent.__init__(self, session, transport, senders)
self.media = 'xhtml'
self.negotiated = True # there is nothing to negotiate
self.last_rid = 0
self.callbacks['session-accept'] += [self._sessionAcceptCB]
self.callbacks['session-terminate'] += [self._stop]
self.callbacks['session-terminate-sent'] += [self._stop]
self.callbacks['edit'] = [self._EditCB]
@log_calls('WhiteboardPlugin')
def _EditCB(self, stanza, content, error, action):
new_tags = content.getTags('new')
remove_tags = content.getTags('remove')
if not self.control.whiteboard:
return
if new_tags is not None:
# Process new elements
for tag in new_tags:
if tag.getAttr('type') == 'element':
self.control.whiteboard.recieve_element(tag)
elif tag.getAttr('type') == 'attr':
self.control.whiteboard.recieve_attr(tag)
self.control.whiteboard.apply_new()
if remove_tags is not None:
# Delete rids
for tag in remove_tags:
target = tag.getAttr('target')
self.control.whiteboard.image.del_rid(target)
@log_calls('WhiteboardPlugin')
def _sessionAcceptCB(self, stanza, content, error, action):
log.debug('session accepted')
self.session.connection.dispatch('WHITEBOARD_ACCEPTED',
(self.session.peerjid, self.session.sid))
def generate_rids(self, x):
# generates x number of rids and returns in list
rids = []
for x in range(x):
rids.append(str(self.last_rid))
self.last_rid += 1
return rids
@log_calls('WhiteboardPlugin')
def send_whiteboard_node(self, items, rids):
# takes int rid and dict items and sends it as a node
# sends new item
jid = self.session.peerjid
sid = self.session.sid
message = Message(to=jid)
sxe = message.addChild(name='sxe', attrs={'session': sid},
namespace=NS_SXE)
for x in rids:
if items[x]['type'] == 'element':
parent = x
attrs = {'rid': x,
'name': items[x]['data'][0].getName(),
'type': items[x]['type']}
sxe.addChild(name='new', attrs=attrs)
if items[x]['type'] == 'attr':
attr_name = items[x]['data']
chdata = items[parent]['data'][0].getAttr(attr_name)
attrs = {'rid': x,
'name': attr_name,
'type': items[x]['type'],
'chdata': chdata,
'parent': parent}
sxe.addChild(name='new', attrs=attrs)
self.session.connection.connection.send(message)
@log_calls('WhiteboardPlugin')
def delete_whiteboard_node(self, rids):
message = Message(to=self.session.peerjid)
sxe = message.addChild(name='sxe', attrs={'session': self.session.sid},
namespace=NS_SXE)
for x in rids:
sxe.addChild(name='remove', attrs = {'target': x})
self.session.connection.connection.send(message)
def send_items(self, items, rids):
# recieves dict items and a list of rids of items to send
# TODO: is there a less clumsy way that doesn't involve passing
# whole list
self.send_whiteboard_node(items, rids)
def del_item(self, rids):
self.delete_whiteboard_node(rids)
def encode(self, xml):
# encodes it sendable string
return 'data:text/xml,' + urllib.quote(xml)
def _fill_content(self, content):
content.addChild(NS_JINGLE_XHTML + ' description')
def _stop(self, *things):
pass
def __del__(self):
pass
def get_content(desc):
return JingleWhiteboard
common.jingle_content.contents[NS_JINGLE_XHTML] = get_content
class JingleTransportSXE(JingleTransport):
def __init__(self, node=None):
JingleTransport.__init__(self, TransportType.SOCKS5)
def make_transport(self, candidates=None):
transport = JingleTransport.make_transport(self, candidates)
transport.setNamespace(NS_JINGLE_SXE)
transport.setTagData('host', 'TODO')
return transport
common.jingle_transport.transports[NS_JINGLE_SXE] = JingleTransportSXE
## plugins/whiteboard/whiteboard_widget.py
##
## Copyright (C) 2009 Jeff Ling <jeff.ummu AT gmail.com>
## Copyright (C) 2010-2017 Yann Leboulanger <asterix AT lagaule.org>
##
## 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/>.
##
from gi.repository import Gtk
from gajim import gtkgui_helpers
try:
import gi
gi.require_version('GooCanvas', '2.0')
from gi.repository import GooCanvas
HAS_GOOCANVAS = True
except:
HAS_GOOCANVAS = False
from nbxmpp import Node
from gajim.dialogs import FileChooserDialog
'''
A whiteboard widget made for Gajim.
- Ummu
'''
class Whiteboard(object):
def __init__(self, account, contact, session, plugin):
self.plugin = plugin
file_path = plugin.local_file_path('whiteboard_widget.ui')
xml = Gtk.Builder()
xml.set_translation_domain('gajim_plugins')
xml.add_from_file(file_path)
self.hbox = xml.get_object('whiteboard_hbox')
self.canevas = GooCanvas.Canvas()
self.hbox.pack_start(self.canevas, True, True, 0)
self.hbox.reorder_child(self.canevas, 0)
self.fg_color_select_button = xml.get_object('fg_color_button')
self.root = self.canevas.get_root_item()
self.tool_buttons = []
for tool in ('brush', 'oval', 'line', 'delete'):
self.tool_buttons.append(xml.get_object(tool + '_button'))
xml.get_object('brush_button').set_active(True)
# Events
self.canevas.connect('button-press-event', self.button_press_event)
self.canevas.connect('button-release-event', self.button_release_event)
self.canevas.connect('motion-notify-event', self.motion_notify_event)
self.canevas.connect('item-created', self.item_created)
# Config
self.line_width = 2
xml.get_object('size_scale').set_value(2)
c = self.fg_color_select_button.get_rgba()
self.color = int(c.red*255*256*256*256 + c.green*255*256*256 + \
c.blue*255*256 + 255)
# SVG Storage
self.image = SVGObject(self.root, session)
xml.connect_signals(self)
# Temporary Variables for items
self.item_temp = None
self.item_temp_coords = (0, 0)
self.item_data = None
# Will be {ID: {type:'element', data:[node, goocanvas]}, ID2: {}} instance
self.recieving = {}
def on_tool_button_toggled(self, widget):
for btn in self.tool_buttons:
if btn == widget:
continue
btn.set_active(False)
def on_brush_button_toggled(self, widget):
if widget.get_active():
self.image.draw_tool = 'brush'
self.on_tool_button_toggled(widget)
def on_oval_button_toggled(self, widget):