1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
|
# -*- coding: utf-8 -*-
##
from gi.repository import Gtk
from gi.repository import Gdk
from gi.repository import GObject
import os
import base64
import urllib
from gajim import chat_control
from gajim.plugins import GajimPlugin
from gajim.plugins.helpers import log_calls
from gajim.dialogs import ImageChooserDialog, ErrorDialog
NS_XHTML_IM = 'http://jabber.org/protocol/xhtml-im' # XEP-0071
class ImagePlugin(GajimPlugin):
@log_calls('ImagePlugin')
def init(self):
self.description = _('This plugin is designed to send '
'a small(0 - 40 kb) graphic image to your contact.\n'
'Client on the other side must support XEP-0071: XHTML-IM'
' and maintain the scheme data: URI.\n'
'Psi+ and Jabbim supported this.')
self.config_dialog = None # ImagePluginConfigDialog(self)
self.controls = []
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.first_run = True
@log_calls('ImagePlugin')
def connect_with_chat_control(self, control):
if not isinstance(control, chat_control.ChatControl):
return
self.chat_control = control
base = Base(self, self.chat_control)
self.controls.append(base)
@log_calls('ImagePlugin')
def disconnect_from_chat_control(self, chat_control):
for control in self.controls:
control.disconnect_from_chat_control()
self.controls = []
@log_calls('ImagePlugin')
def update_button_state(self, chat_control):
for base in self.controls:
if base.chat_control == chat_control:
is_support_xhtml = chat_control.contact.supports(NS_XHTML_IM)
base.button.set_sensitive(is_support_xhtml and not \
chat_control.encryption)
if not is_support_xhtml:
text = _('This contact does not support XHTML_IM')
else:
text = _('Send image (Alt+L)')
base.button.set_tooltip_text(text)
class Base(object):
def __init__(self, plugin, chat_control):
self.plugin = plugin
self.chat_control = chat_control
actions_hbox = chat_control.xml.get_object('hbox')
self.button = Gtk.Button(label=None, stock=None, use_underline=True)
self.button.get_style_context().add_class(
'chatcontrol-actionbar-button')
self.button.set_property('relief', Gtk.ReliefStyle.NONE)
self.button.set_property('can-focus', False)
img = Gtk.Image()
img.set_from_icon_name('image-x-generic-symbolic.symbolic',
Gtk.IconSize.MENU)
self.button.set_image(img)
self.button.set_tooltip_text('Send image (Alt+L)')
ag = Gtk.accel_groups_from_object(self.chat_control.parent_win.window)[0]
self.button.add_accelerator('activate', ag, Gdk.KEY_L,
Gdk.ModifierType.MOD1_MASK, Gtk.AccelFlags.VISIBLE)
actions_hbox.pack_start(self.button, False, False , 0)
actions_hbox.reorder_child(self.button,
len(actions_hbox.get_children()) - 2)
id_ = self.button.connect('clicked', self.on_image_button_clicked)
self.button.show()
def _on_message_textview_key_press_event(self, widget, event):
if event.get_state() & Gdk.ModifierType.MOD1_MASK and \
event.keyval == Gdk.KEY_l:
if not self.chat_control.contact.supports(NS_XHTML_IM):
from dialogs import WarningDialog
WarningDialog('Warning',
_('This contact does not support XHTML_IM'),
self.chat_control.parent_win.window)
return True
self.on_image_button_clicked(widget)
return True
def on_image_button_clicked(self, widget):
def on_ok(widget, path_to_file):
filesize = os.path.getsize(path_to_file) # in bytes
invalid_file = False
msg = ''
if os.path.isfile(path_to_file):
stat = os.stat(path_to_file)
if stat[6] == 0:
invalid_file = True
msg = _('File is empty')
else:
invalid_file = True
msg = _('File does not exist')
if filesize < 60000:
file_ = open(path_to_file, "rb")
img = urllib.parse.quote(base64.standard_b64encode(
file_.read()), '')
if len(img) > 60000:
invalid_file = True
msg = _('File too big')
file_.close()
else:
invalid_file = True
msg = _('File too big')
if invalid_file:
ErrorDialog(_('Could not load image'), msg)
return
dlg.destroy()
msg = 'HTML image'
extension = os.path.splitext(os.path.split(path_to_file)[1])[1] \
.lower()[1:]
xhtml = '<body><br/> <img alt="img" src="data:image/%s;base64,%s"/> \
</body>' % (extension, img)
self.chat_control.send_message(message=msg, xhtml=xhtml)
self.chat_control.msg_textview.grab_focus()
dlg = ImageChooserDialog(on_response_ok=on_ok, on_response_cancel=None)
def disconnect_from_chat_control(self):
actions_hbox = self.chat_control.xml.get_object('hbox')
actions_hbox.remove(self.button)
|