def set_avatar(self, mark, nick, pixbuf): buffer_ = mark.get_buffer() end_iter = buffer_.get_iter_at_mark(mark) anchor = buffer_.create_child_anchor(end_iter) img = TextViewImage(anchor, nick) img.set_from_pixbuf(pixbuf) img.show() self.textview.tv.add_child_at_anchor(img, anchor)
def insert_pic_preview(self, mark, special_text, url): pixbuf = self.get_pixbuf_from_url(url, self.plugin.config["PREVIEW_SIZE"]) if pixbuf: # insert image buffer_ = mark.get_buffer() end_iter = buffer_.get_iter_at_mark(mark) anchor = buffer_.create_child_anchor(end_iter) img = TextViewImage(anchor, special_text) img.set_from_pixbuf(pixbuf) img.show() self.textview.tv.add_child_at_anchor(img, anchor)
def add_to_textview(): try: # textview closed in the meantime etc. buffer_ = repl_start.get_buffer() iter_ = buffer_.get_iter_at_mark(repl_start) buffer_.insert(iter_, "\n") anchor = buffer_.create_child_anchor(iter_) # Use url as tooltip for image img = TextViewImage(anchor, url) img.set_from_pixbuf(pixbuf) eb.add(img) eb.show_all() self.textview.tv.add_child_at_anchor(eb, anchor) buffer_.delete(iter_, buffer_.get_iter_at_mark(repl_end)) except: pass return False
def add_to_textview(): try: # textview closed in the meantime etc. buffer_ = repl_start.get_buffer() iter_ = buffer_.get_iter_at_mark(repl_start) buffer_.insert(iter_, "\n") anchor = buffer_.create_child_anchor(iter_) # Use weburl as tooltip for image img = TextViewImage(anchor, weburl) loader = gtk.gdk.PixbufLoader() loader.write(data) loader.close() pixbuf = loader.get_pixbuf() img.set_from_pixbuf(pixbuf) eb.add(img) eb.show_all() textview.add_child_at_anchor(eb, anchor) buffer_.delete(iter_, buffer_.get_iter_at_mark(repl_end)) except: pass return False
def insert_pic_preview(self, mark, special_text, url, tag): pixbuf = self.get_pixbuf_from_url( url, self.plugin.config[ 'PREVIEW_SIZE']) if pixbuf: # insert image buffer_ = mark.get_buffer() end_iter = buffer_.get_iter_at_mark(mark) anchor = buffer_.create_child_anchor(end_iter) img = TextViewImage(anchor, special_text) img.set_from_pixbuf(pixbuf) img.show() self.textview.tv.add_child_at_anchor(img, anchor) buffer_ = mark.get_buffer() end_iter = buffer_.get_iter_at_mark(mark) buffer_.insert_with_tags(end_iter, special_text, tag)
def print_special_text(self, special_text, other_tags, graphics=True, iter__=None): if gajim.interface.pointim_sharp_slash_re.match(special_text): # insert post num #123456// buffer_, iter_, tag = self.get_iter_and_tag('pointim_sharp_slash') buffer_.insert_with_tags(iter__, special_text, tag) self.last_pointim_num = special_text self.textview.plugin_modified = True return if gajim.interface.pointim_nick_re.match(special_text): # insert pointim nick @nickname//// buffer_, iter_, tag = self.get_iter_and_tag('pointim_nick') mark = buffer_.create_mark(None, iter__, True) nick = special_text[1:].rstrip(':') buffer_.insert_with_tags(iter__, special_text, tag) # insert avatars if not self.plugin.config['SHOW_AVATARS']: self.textview.plugin_modified = True return b_nick = buffer_.get_text(buffer_.get_start_iter(), buffer_.get_iter_at_mark(mark),False) if self.plugin.config['ONLY_AUTHOR_AVATAR'] and not \ special_text.endswith(':') and b_nick[-9:] not in ('Subscribed to ' ): self.textview.plugin_modified = True return if self.plugin.config['ONLY_FIRST_AVATAR']: if b_nick[-9:] not in ('Reply by ', 'message from ', 'ended by ', 'Subscribed to '): if b_nick[-2] != gajim.config.get('after_nickname'): self.textview.plugin_modified = True return elif b_nick[-1] == '\n': self.textview.plugin_modified = True return conn = gajim.connections[self.chat_control.account] if not conn.connected: self.textview.plugin_modified = True return # search id in the db query = "select nick, id from person where nick = :nick" self.plugin.cursor.execute(query, {'nick':nick}) db_item = self.plugin.cursor.fetchone() if db_item: # nick in the db pixbuf = self.get_avatar(db_item[1], nick, True) if not pixbuf: self.textview.plugin_modified = True return end_iter = buffer_.get_iter_at_mark(mark) anchor = buffer_.create_child_anchor(end_iter) img = TextViewImage(anchor, nick) img.set_from_pixbuf(pixbuf) img.show() self.textview.tv.add_child_at_anchor(img, anchor) self.textview.plugin_modified = True return else: # nick not in the db gobject.idle_add(self.get_new_avatar, mark, nick) return if gajim.interface.pointim_pic_re.match(special_text) and \ self.plugin.config['SHOW_PREVIEW']: # show pics preview buffer_, iter_, tag = self.get_iter_and_tag('url') mark = buffer_.create_mark(None, iter_, True) buffer_.insert_with_tags(iter_, special_text, tag) uid = special_text.split('/')[-1] url = "http://i.pointim.com/photos-512/%s" % uid gajim.thread_interface(self.insert_pic_preview, [mark, special_text, url]) self.textview.plugin_modified = True return
class Base(object): def __init__(self, plugin, chat_control): self.last_juick_num = '' self.plugin = plugin self.chat_control = chat_control self.textview = self.chat_control.conv_textview self.change_cursor = False id_ = self.textview.tv.connect('button_press_event', self.on_textview_button_press_event, self.textview) chat_control.handlers[id_] = self.textview.tv id_ = self.chat_control.msg_textview.connect('key_press_event', self.mykeypress_event) chat_control.handlers[id_] = self.chat_control.msg_textview self.id_ = self.textview.tv.connect('motion_notify_event', self.on_textview_motion_notify_event) self.chat_control.handlers[self.id_] = self.textview.tv # new buffer tags color = self.plugin.config['LINK_COLOR'] buffer_ = self.textview.tv.get_buffer() self.textview.tagSharpSlash = buffer_.create_tag('sharp_slash') self.textview.tagSharpSlash.set_property('foreground', color) self.textview.tagSharpSlash.set_property('underline', pango.UNDERLINE_SINGLE) id_ = self.textview.tagSharpSlash.connect('event', self.juick_hyperlink_handler, 'sharp_slash') chat_control.handlers[id_] = self.textview.tagSharpSlash self.textview.tagJuickNick = buffer_.create_tag('juick_nick') self.textview.tagJuickNick.set_property('foreground', color) self.textview.tagJuickNick.set_property('underline', pango.UNDERLINE_SINGLE) id_ = self.textview.tagJuickNick.connect('event', self.juick_hyperlink_handler, 'juick_nick') chat_control.handlers[id_] = self.textview.tagJuickNick self.textview.tagJuickPic = buffer_.create_tag('juick_pic') self.create_patterns() self.create_link_menu() self.create_tag_menu() self.create_buttons() def create_patterns(self): self.juick_post_uid = self.juick_nick = '' self.juick_post_re = re.compile(r'#(\d+)') self.juick_post_comment_re = re.compile(r'#(\d+)/(\d+)') sharp_slash = r'#\d+(\/\d+)?' juick_nick = r'@[a-zA-Z0-9_@:\.-]+' juick_pic = r'http://i\.juick\.com/.+/[0-9-]+\.[JPG|jpg]' interface = gajim.interface interface.sharp_slash_re = re.compile(sharp_slash) self.juick_nick_re = interface.juick_nick_re = re.compile(juick_nick) self.juick_pic_re = interface.juick_pic_re = re.compile(juick_pic) juick_pattern = '|' + sharp_slash + '|' + juick_nick + '|' + juick_pic interface.basic_pattern = interface.basic_pattern + juick_pattern interface.emot_and_basic = interface.emot_and_basic + juick_pattern interface._basic_pattern_re = re.compile(interface.basic_pattern, re.IGNORECASE) interface._emot_and_basic_re = re.compile(interface.emot_and_basic, re.IGNORECASE + re.UNICODE) def create_buttons(self): # create juick button actions_hbox = self.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('juick.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('juick', gtk.ICON_SIZE_MENU) self.button.set_image(img) self.button.set_tooltip_text(_('Juick commands')) send_button = self.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_juick_button_clicked) self.chat_control.handlers[id_] = self.button self.button.show() # create juick tag button self.tag_button = gtk.Button(label=None, stock=None, use_underline=True) self.tag_button.set_property('relief', gtk.RELIEF_NONE) self.tag_button.set_property('can-focus', False) img = gtk.Image() img_path = self.plugin.local_file_path('juick_tag_button.png') pixbuf = gtk.gdk.pixbuf_new_from_file(img_path) iconset = gtk.IconSet(pixbuf=pixbuf) factory.add('juick_tag', iconset) factory.add_default() img.set_from_stock('juick_tag', gtk.ICON_SIZE_MENU) self.tag_button.set_image(img) actions_hbox.add_with_properties(self.tag_button, 'position', send_button_pos - 1, 'expand', False) id_ = self.tag_button.connect('clicked', self.on_juick_tag_button_clicked) self.chat_control.handlers[id_] = self.tag_button self.tag_button.set_no_show_all(True) self.tag_button.set_tooltip_text(_('Juick tags')) self.tag_button.set_property('visible', self.plugin.config[ 'SHOW_TAG_BUTTON']) def create_link_menu(self): """ Create juick link context menu """ self.juick_link_menu = gtk.Menu() item = gtk.MenuItem(_('Reply to message')) item.connect('activate', self.on_reply) self.juick_link_menu.append(item) menuitems = ((_('Unsubscribe from comments'), 'U #WORD'), (_('Subscribe to message replies'), 'S #WORD'), (_('Recommend post'), '! #WORD'), (_('Show message with replies'), '#WORD+'), (_('Delete post'), 'D #WORD'),) for menuitem in menuitems: item = gtk.MenuItem(menuitem[0]) item.connect('activate', self.send, menuitem[1]) self.juick_link_menu.append(item) item = gtk.MenuItem(_('Open in browser')) item.connect('activate', self.open_in_browser) self.juick_link_menu.append(item) menuitems = ((_('Show user\'s info'), 'NICK'), (_('Show user\'s info and last 10 messages'), 'NICK+'), (_('Subscribe to user\'s blog'), 'S NICK'), (_('Unsubscribe from user\'s blog'), 'U NICK'), (_('Add/delete user to/from your blacklist'), 'BL NICK'),) for menuitem in menuitems: item = gtk.MenuItem(menuitem[0]) item.connect('activate', self.send, menuitem[1]) self.juick_link_menu.append(item) item = gtk.MenuItem(_('Send personal message')) item.connect('activate', self.on_pm) self.juick_link_menu.append(item) def create_tag_menu(self): """ Create juick tag button menu """ self.menu = gtk.Menu() for num in xrange(1, 11): menuitem = self.plugin.config['MENUITEM' + str(num)] text = self.plugin.config['MENUITEM_TEXT' + str(num)] if not menuitem or not text: continue item = gtk.MenuItem(menuitem) item.connect('activate', self.on_insert, text) self.menu.append(item) self.menu.show_all() def juick_hyperlink_handler(self, texttag, widget, event, iter_, kind): # handle message links( #12345 or #12345/6) and juick nicks if event.type == gtk.gdk.BUTTON_PRESS and event.button == 3: # show popup menu (right mouse button clicked) begin_iter = iter_.copy() # we get the begining of the tag while not begin_iter.begins_tag(texttag): begin_iter.backward_char() end_iter = iter_.copy() # we get the end of the tag while not end_iter.ends_tag(texttag): end_iter.forward_char() buffer_ = self.textview.tv.get_buffer() word = buffer_.get_text(begin_iter, end_iter).decode('utf-8') self.juick_post = word post = self.juick_post_re.search(word) nick = self.juick_nick_re.search(word) if post is None and nick is None: return childs = self.juick_link_menu.get_children() if post: self.juick_post_full = gajim.interface.sharp_slash_re\ .search(word).group(0) self.juick_post_uid = post.group(1) for menuitem in xrange(7): childs[menuitem].show() for menuitem in xrange(7, 13): childs[menuitem].hide() if nick: self.juick_nick = nick.group(0) for menuitem in xrange(7): childs[menuitem].hide() for menuitem in xrange(7, 13): childs[menuitem].show() self.juick_link_menu.popup(None, None, None, event.button, event.time) if event.type == gtk.gdk.BUTTON_PRESS and event.button == 1: # insert message num or nick (left mouse button clicked) begin_iter = iter_.copy() # we get the begining of the tag while not begin_iter.begins_tag(texttag): begin_iter.backward_char() end_iter = iter_.copy() # we get the end of the tag while not end_iter.ends_tag(texttag): end_iter.forward_char() buffer_ = self.textview.tv.get_buffer() word = buffer_.get_text(begin_iter, end_iter).decode('utf-8') if kind == 'sharp_slash': self.on_insert(widget, word) if kind == 'juick_nick': self.on_insert(widget, 'PM %s' % word.rstrip(':')) def print_special_text(self, special_text, other_tags, graphics=True, iter__=None): if gajim.interface.sharp_slash_re.match(special_text): # insert post num #123456// buffer_, iter_, tag = self.get_iter_and_tag('sharp_slash') buffer_.insert_with_tags(iter__, special_text, tag) self.last_juick_num = special_text self.textview.plugin_modified = True return if gajim.interface.juick_nick_re.match(special_text): # insert juick nick @nickname//// buffer_, iter_, tag = self.get_iter_and_tag('juick_nick') mark = buffer_.create_mark(None, iter__, True) nick = special_text[1:].rstrip(':') buffer_.insert_with_tags(iter__, special_text, tag) # insert avatars if not self.plugin.config['SHOW_AVATARS']: self.textview.plugin_modified = True return b_nick = buffer_.get_text(buffer_.get_start_iter(), buffer_.get_iter_at_mark(mark),False) if self.plugin.config['ONLY_AUTHOR_AVATAR'] and not \ special_text.endswith(':') and b_nick[-9:] not in ('Subscribed to ' ): self.textview.plugin_modified = True return if self.plugin.config['ONLY_FIRST_AVATAR']: if b_nick[-9:] not in ('Reply by ', 'message from ', 'ended by ', 'Subscribed to '): if b_nick[-2] != gajim.config.get('after_nickname'): self.textview.plugin_modified = True return elif b_nick[-1] == '\n': self.textview.plugin_modified = True return conn = gajim.connections[self.chat_control.account] if not conn.connected: self.textview.plugin_modified = True return # search id in the db query = "select nick, id from person where nick = :nick" self.plugin.cursor.execute(query, {'nick':nick}) db_item = self.plugin.cursor.fetchone() if db_item: # nick in the db pixbuf = self.get_avatar(db_item[1], nick, True) if not pixbuf: self.textview.plugin_modified = True return gobject.idle_add(self.set_avatar, mark, nick, pixbuf) self.textview.plugin_modified = True return else: # nick not in the db gobject.idle_add(self.get_new_avatar, mark, nick) self.textview.plugin_modified = True return if gajim.interface.juick_pic_re.match(special_text) and \ self.plugin.config['SHOW_PREVIEW']: # show pics preview buffer_, iter_, tag = self.get_iter_and_tag('url') mark = buffer_.create_mark(None, buffer_.get_end_iter(), True) uid = special_text.split('/')[-1] url = "http://i.juick.com/photos-512/%s" % uid gajim.thread_interface(self.insert_pic_preview, [mark, special_text, url, tag]) self.textview.plugin_modified = True return def set_avatar(self, mark, nick, pixbuf): buffer_ = mark.get_buffer() end_iter = buffer_.get_iter_at_mark(mark) anchor = buffer_.create_child_anchor(end_iter) img = TextViewImage(anchor, nick) img.set_from_pixbuf(pixbuf) img.show() self.textview.tv.add_child_at_anchor(img, anchor) def insert_pic_preview(self, mark, special_text, url, tag): pixbuf = self.get_pixbuf_from_url( url, self.plugin.config[ 'PREVIEW_SIZE']) if pixbuf: # insert image buffer_ = mark.get_buffer() end_iter = buffer_.get_iter_at_mark(mark) anchor = buffer_.create_child_anchor(end_iter) img = TextViewImage(anchor, special_text) img.set_from_pixbuf(pixbuf) img.show() self.textview.tv.add_child_at_anchor(img, anchor) buffer_ = mark.get_buffer() end_iter = buffer_.get_iter_at_mark(mark) buffer_.insert_with_tags(end_iter, special_text, tag) def get_iter_and_tag(self, tag_name): buffer_ = self.textview.tv.get_buffer() ttable = buffer_.get_tag_table() tag = ttable.lookup(tag_name) return buffer_, buffer_.get_end_iter(), tag def get_new_avatar(self, mark, nick): try: req = urllib2.Request('http://api.juick.com/users?uname=%s' % nick) response = urllib2.urlopen(req) j = json.load(response) _id = str(j[0]['uid']) except urllib2.HTTPError, e: return pixbuf = self.get_avatar(_id, nick) buffer_ = mark.get_buffer() end_iter = buffer_.get_iter_at_mark(mark) anchor = buffer_.create_child_anchor(end_iter) img = TextViewImage(anchor, nick) img.set_from_pixbuf(pixbuf) img.show() self.textview.tv.add_child_at_anchor(img, anchor)