From 311ec482feacf9105b9b9a6d4e9a59e093ebf371 Mon Sep 17 00:00:00 2001 From: Denis Fomin Date: Sun, 2 Jan 2011 17:02:46 +0300 Subject: [PATCH] ImagePlugin added --- image/__init__.py | 1 + image/image.py | 116 +++++++++++++++++++++++++++++++++++++++++++++ image/manifest.ini | 10 ++++ 3 files changed, 127 insertions(+) create mode 100644 image/__init__.py create mode 100644 image/image.py create mode 100644 image/manifest.ini diff --git a/image/__init__.py b/image/__init__.py new file mode 100644 index 0000000..dd917e2 --- /dev/null +++ b/image/__init__.py @@ -0,0 +1 @@ +from image import ImagePlugin diff --git a/image/image.py b/image/image.py new file mode 100644 index 0000000..2d8b219 --- /dev/null +++ b/image/image.py @@ -0,0 +1,116 @@ +# -*- coding: utf-8 -*- +## +import gtk +import os +import base64 +import urllib2 + +from plugins import GajimPlugin +from plugins.helpers import log_calls +from common.xmpp.protocol import NS_XHTML_IM +from dialogs import ImageChooserDialog, ErrorDialog + + +class ImagePlugin(GajimPlugin): + @log_calls('ImagePlugin') + def init(self): + 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)} + + @log_calls('ImagePlugin') + def connect_with_chat_control(self, chat_control): + self.chat_control = chat_control + control = Base(self, self.chat_control) + self.controls.append(control) + + @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.gpg_is_active) + if not is_support_xhtml: + text = _('This contact does not support XHTML_IM') + else: + text = _('Send image') + 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('actions_hbox') + self.button = gtk.Button(label=None, stock=None, use_underline=True) + self.button.set_property('relief', gtk.RELIEF_NONE) + self.button.set_property('can-focus', False) + img = gtk.Image() + #img_path = self.plugin.local_file_path('favicon.png') + #pixbuf = gtk.gdk.pixbuf_new_from_file(img_path) + #iconset = gtk.IconSet(pixbuf=pixbuf) + #factory = gtk.IconFactory() + #factory.add('juick', iconset) + #factory.add_default() + img.set_from_stock('gtk-orientation-portrait', gtk.ICON_SIZE_BUTTON) + self.button.set_image(img) + send_button = chat_control.xml.get_object('send_button') + send_button_pos = actions_hbox.child_get_property(send_button, + 'position') + actions_hbox.add_with_properties(self.button, 'position', + send_button_pos - 1, 'expand', False) + id_ = self.button.connect('clicked', self.on_image_button_clicked) + chat_control.handlers[id_] = self.button + self.button.show() + + 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: + img = urllib2.quote(base64.standard_b64encode(open( + path_to_file, "rb").read()), '') + if len(img) > 60000: + invalid_file = True + msg = _('File too big') + 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 = ' img' % ( + 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('actions_hbox') + actions_hbox.remove(self.button) diff --git a/image/manifest.ini b/image/manifest.ini new file mode 100644 index 0000000..f06bc6f --- /dev/null +++ b/image/manifest.ini @@ -0,0 +1,10 @@ +[info] +name: Image +short_name: image +version: 0.1 +description: This plugin is designed to send a small(0 - 40 kb) graphic image to your contact. + Client on the other side must support XEP-0071: XHTML-IM + and maintain the scheme data: URI. +authors: Denis Fomin +homepage: http://trac-plugins.gajim.org/wiki/ImagePlugin +