[image] Add compatibility for Gajim 1.1.0

This commit is contained in:
Philipp Hörist
2018-08-19 17:39:29 +02:00
parent b1fc21a33c
commit fa41251561

View File

@@ -1,18 +1,24 @@
# -*- coding: utf-8 -*-
##
from gi.repository import Gtk
from gi.repository import Gdk
from gi.repository import GObject
import os import os
import base64 import base64
import urllib import urllib
from gi.repository import Gtk
from gi.repository import Gdk
from gajim import chat_control from gajim import chat_control
from gajim.plugins import GajimPlugin from gajim.plugins import GajimPlugin
from gajim.plugins.helpers import log_calls from gajim.plugins.helpers import log_calls
from gajim.dialogs import ImageChooserDialog, ErrorDialog from gajim.dialogs import ErrorDialog
NS_XHTML_IM = 'http://jabber.org/protocol/xhtml-im' # XEP-0071 try:
from gajim.gtk.filechoosers import FileChooserDialog
NEW_FILECHOOSER = True
except ImportError:
from gajim.dialogs import ImageChooserDialog
NEW_FILECHOOSER = False
NS_XHTML_IM = 'http://jabber.org/protocol/xhtml-im'
class ImagePlugin(GajimPlugin): class ImagePlugin(GajimPlugin):
@@ -99,12 +105,41 @@ class Base(object):
return True return True
def on_image_button_clicked(self, widget): def on_image_button_clicked(self, widget):
if NEW_FILECHOOSER:
self._new_filechooser()
else:
self._old_filechooser(widget)
def _new_filechooser(self):
def on_ok(filename):
image = self._check_file(filename)
if image is None:
return
self._send(image, filename)
FileChooserDialog(on_ok,
select_multiple=False,
transient_for=self.chat_control.parent_win.window)
def _old_filechooser(self, widget):
def on_ok(widget, path_to_file): def on_ok(widget, path_to_file):
filesize = os.path.getsize(path_to_file) # in bytes image = self._check_file(path_to_file)
if image is None:
return
dlg.destroy()
self._send(image, path_to_file)
dlg = ImageChooserDialog(on_response_ok=on_ok, on_response_cancel=None)
def _check_file(self, filename):
filesize = os.path.getsize(filename) # in bytes
invalid_file = False invalid_file = False
msg = '' msg = ''
if os.path.isfile(path_to_file): if os.path.isfile(filename):
stat = os.stat(path_to_file) stat = os.stat(filename)
if stat[6] == 0: if stat[6] == 0:
invalid_file = True invalid_file = True
msg = _('File is empty') msg = _('File is empty')
@@ -112,7 +147,7 @@ class Base(object):
invalid_file = True invalid_file = True
msg = _('File does not exist') msg = _('File does not exist')
if filesize < 60000: if filesize < 60000:
file_ = open(path_to_file, "rb") file_ = open(filename, "rb")
img = urllib.parse.quote(base64.standard_b64encode( img = urllib.parse.quote(base64.standard_b64encode(
file_.read()), '') file_.read()), '')
if len(img) > 60000: if len(img) > 60000:
@@ -125,18 +160,17 @@ class Base(object):
if invalid_file: if invalid_file:
ErrorDialog(_('Could not load image'), msg) ErrorDialog(_('Could not load image'), msg)
return return
return img
dlg.destroy() def _send(self, image, filename):
msg = 'HTML image' msg = 'HTML image'
extension = os.path.splitext(os.path.split(path_to_file)[1])[1] \ extension = os.path.splitext(os.path.split(filename)[1])[1] \
.lower()[1:] .lower()[1:]
xhtml = '<body><br/> <img alt="img" src="data:image/%s;base64,%s"/> \ xhtml = '<body><br/> <img alt="img" src="data:image/%s;base64,%s"/> \
</body>' % (extension, img) </body>' % (extension, image)
self.chat_control.send_message(message=msg, xhtml=xhtml) self.chat_control.send_message(message=msg, xhtml=xhtml)
self.chat_control.msg_textview.grab_focus() self.chat_control.msg_textview.grab_focus()
dlg = ImageChooserDialog(on_response_ok=on_ok, on_response_cancel=None)
def disconnect_from_chat_control(self): def disconnect_from_chat_control(self):
actions_hbox = self.chat_control.xml.get_object('hbox') actions_hbox = self.chat_control.xml.get_object('hbox')
actions_hbox.remove(self.button) actions_hbox.remove(self.button)