| 1 | ##Â Â Â conversation_textview.py |
|---|
| 2 | ## |
|---|
| 3 | ## Contributors for this file: |
|---|
| 4 | ##Â Â Â - Yann Le Boulanger <asterix@lagaule.org> |
|---|
| 5 | ##Â Â Â - Nikos Kouremenos <kourem@gmail.com> |
|---|
| 6 | ## |
|---|
| 7 | ## Copyright (C) 2003-2004 Yann Le Boulanger <asterix@lagaule.org> |
|---|
| 8 | ##Â Â Â Â Â Â Â Â Â Â Â Â Â Vincent Hanquez <tab@snarc.org> |
|---|
| 9 | ## Copyright (C) 2005 Yann Le Boulanger <asterix@lagaule.org> |
|---|
| 10 | ##Â Â Â Â Â Â Â Â Â Â Vincent Hanquez <tab@snarc.org> |
|---|
| 11 | ##Â Â Â Â Â Â Â Â Â Â Nikos Kouremenos <nkour@jabber.org> |
|---|
| 12 | ##Â Â Â Â Â Â Â Â Â Â Dimitur Kirov <dkirov@gmail.com> |
|---|
| 13 | ##Â Â Â Â Â Â Â Â Â Â Travis Shirk <travis@pobox.com> |
|---|
| 14 | ##Â Â Â Â Â Â Â Â Â Â Norman Rasmussen <norman@rasmussen.co.za> |
|---|
| 15 | ## |
|---|
| 16 | ## This program is free software; you can redistribute it and/or modify |
|---|
| 17 | ## it under the terms of the GNU General Public License as published |
|---|
| 18 | ## by the Free Software Foundation; version 2 only. |
|---|
| 19 | ## |
|---|
| 20 | ## This program is distributed in the hope that it will be useful, |
|---|
| 21 | ## but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 22 | ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 23 | ## GNU General Public License for more details. |
|---|
| 24 | ## |
|---|
| 25 | |
|---|
| 26 | import gtk |
|---|
| 27 | import gtk.glade |
|---|
| 28 | import pango |
|---|
| 29 | import gobject |
|---|
| 30 | import time |
|---|
| 31 | import sys |
|---|
| 32 | import tooltips |
|---|
| 33 | import dialogs |
|---|
| 34 | import locale |
|---|
| 35 | |
|---|
| 36 | import gtkgui_helpers |
|---|
| 37 | from common import gajim |
|---|
| 38 | from common import helpers |
|---|
| 39 | from common import i18n |
|---|
| 40 | from calendar import timegm |
|---|
| 41 | |
|---|
| 42 | _ =Â i18n._ |
|---|
| 43 | APP =Â i18n.APP |
|---|
| 44 | gtk.glade.bindtextdomain(APP, i18n.DIR) |
|---|
| 45 | gtk.glade.textdomain(APP) |
|---|
| 46 | |
|---|
| 47 | class ConversationTextview: |
|---|
| 48 | Â Â Â Â '''Class for the conversation textview (where user reads already said messages) |
|---|
| 49 | Â Â Â Â for chat/groupchat windows''' |
|---|
| 50 |     def __init__(self, account): |
|---|
| 51 | Â Â Â Â Â Â Â Â # no need to inherit TextView, use it as property is safer |
|---|
| 52 | Â Â Â Â Â Â Â Â self.tv =Â gtk.TextView() |
|---|
| 53 | |
|---|
| 54 | Â Â Â Â Â Â Â Â # set properties |
|---|
| 55 | Â Â Â Â Â Â Â Â self.tv.set_border_width(1) |
|---|
| 56 | Â Â Â Â Â Â Â Â self.tv.set_accepts_tab(True) |
|---|
| 57 | Â Â Â Â Â Â Â Â self.tv.set_editable(False) |
|---|
| 58 | Â Â Â Â Â Â Â Â self.tv.set_cursor_visible(False) |
|---|
| 59 | Â Â Â Â Â Â Â Â self.tv.set_wrap_mode(gtk.WRAP_WORD) |
|---|
| 60 | Â Â Â Â Â Â Â Â self.tv.set_left_margin(2) |
|---|
| 61 | Â Â Â Â Â Â Â Â self.tv.set_right_margin(2) |
|---|
| 62 | Â Â Â Â Â Â Â Â self.handlers =Â {} |
|---|
| 63 | |
|---|
| 64 | Â Â Â Â Â Â Â Â # connect signals |
|---|
| 65 |         id = self.tv.connect('motion_notify_event', self.on_textview_motion_notify_event) |
|---|
| 66 | Â Â Â Â Â Â Â Â self.handlers[id]Â =Â self.tv |
|---|
| 67 |         id = self.tv.connect('populate_popup', self.on_textview_populate_popup) |
|---|
| 68 | Â Â Â Â Â Â Â Â self.handlers[id]Â =Â self.tv |
|---|
| 69 |         id = self.tv.connect('button_press_event', self.on_textview_button_press_event) |
|---|
| 70 | Â Â Â Â Â Â Â Â self.handlers[id]Â =Â self.tv |
|---|
| 71 | |
|---|
| 72 | Â Â Â Â Â Â Â Â self.account =Â account |
|---|
| 73 | Â Â Â Â Â Â Â Â self.change_cursor =Â None |
|---|
| 74 | Â Â Â Â Â Â Â Â self.last_time_printout =Â 0 |
|---|
| 75 | |
|---|
| 76 | Â Â Â Â Â Â Â Â font =Â pango.FontDescription(gajim.config.get('conversation_font')) |
|---|
| 77 | Â Â Â Â Â Â Â Â self.tv.modify_font(font) |
|---|
| 78 |         buffer = self.tv.get_buffer() |
|---|
| 79 | Â Â Â Â Â Â Â Â end_iter =Â buffer.get_end_iter() |
|---|
| 80 |         buffer.create_mark('end', end_iter, False) |
|---|
| 81 | |
|---|
| 82 | Â Â Â Â Â Â Â Â self.tagIn =Â buffer.create_tag('incoming') |
|---|
| 83 | Â Â Â Â Â Â Â Â color =Â gajim.config.get('inmsgcolor') |
|---|
| 84 |         self.tagIn.set_property('foreground', color) |
|---|
| 85 | Â Â Â Â Â Â Â Â self.tagOut =Â buffer.create_tag('outgoing') |
|---|
| 86 | Â Â Â Â Â Â Â Â color =Â gajim.config.get('outmsgcolor') |
|---|
| 87 |         self.tagOut.set_property('foreground', color) |
|---|
| 88 | Â Â Â Â Â Â Â Â self.tagStatus =Â buffer.create_tag('status') |
|---|
| 89 | Â Â Â Â Â Â Â Â color =Â gajim.config.get('statusmsgcolor') |
|---|
| 90 |         self.tagStatus.set_property('foreground', color) |
|---|
| 91 | |
|---|
| 92 | Â Â Â Â Â Â Â Â tag =Â buffer.create_tag('marked') |
|---|
| 93 | Â Â Â Â Â Â Â Â color =Â gajim.config.get('markedmsgcolor') |
|---|
| 94 |         tag.set_property('foreground', color) |
|---|
| 95 |         tag.set_property('weight', pango.WEIGHT_BOLD) |
|---|
| 96 | |
|---|
| 97 | Â Â Â Â Â Â Â Â tag =Â buffer.create_tag('time_sometimes') |
|---|
| 98 |         tag.set_property('foreground', 'grey') |
|---|
| 99 |         tag.set_property('scale', pango.SCALE_SMALL) |
|---|
| 100 |         tag.set_property('justification', gtk.JUSTIFY_CENTER) |
|---|
| 101 | |
|---|
| 102 | Â Â Â Â Â Â Â Â tag =Â buffer.create_tag('small') |
|---|
| 103 |         tag.set_property('scale', pango.SCALE_SMALL) |
|---|
| 104 | |
|---|
| 105 | Â Â Â Â Â Â Â Â tag =Â buffer.create_tag('restored_message') |
|---|
| 106 | Â Â Â Â Â Â Â Â color =Â gajim.config.get('restored_messages_color') |
|---|
| 107 |         tag.set_property('foreground', color) |
|---|
| 108 | |
|---|
| 109 | Â Â Â Â Â Â Â Â tag =Â buffer.create_tag('url') |
|---|
| 110 | Â Â Â Â Â Â Â Â color =Â gajim.config.get('urlmsgcolor') |
|---|
| 111 |         tag.set_property('foreground', color) |
|---|
| 112 |         tag.set_property('underline', pango.UNDERLINE_SINGLE) |
|---|
| 113 |         id = tag.connect('event', self.hyperlink_handler, 'url') |
|---|
| 114 | Â Â Â Â Â Â Â Â self.handlers[id]Â =Â tag |
|---|
| 115 | |
|---|
| 116 | Â Â Â Â Â Â Â Â tag =Â buffer.create_tag('mail') |
|---|
| 117 |         tag.set_property('foreground', color) |
|---|
| 118 |         tag.set_property('underline', pango.UNDERLINE_SINGLE) |
|---|
| 119 |         id = tag.connect('event', self.hyperlink_handler, 'mail') |
|---|
| 120 | Â Â Â Â Â Â Â Â self.handlers[id]Â =Â tag |
|---|
| 121 | |
|---|
| 122 | Â Â Â Â Â Â Â Â tag =Â buffer.create_tag('bold') |
|---|
| 123 |         tag.set_property('weight', pango.WEIGHT_BOLD) |
|---|
| 124 | |
|---|
| 125 | Â Â Â Â Â Â Â Â tag =Â buffer.create_tag('italic') |
|---|
| 126 |         tag.set_property('style', pango.STYLE_ITALIC) |
|---|
| 127 | |
|---|
| 128 | Â Â Â Â Â Â Â Â tag =Â buffer.create_tag('underline') |
|---|
| 129 |         tag.set_property('underline', pango.UNDERLINE_SINGLE) |
|---|
| 130 | |
|---|
| 131 |         buffer.create_tag('focus-out-line', justification = gtk.JUSTIFY_CENTER) |
|---|
| 132 | |
|---|
| 133 | Â Â Â Â Â Â Â Â self.line_tooltip =Â tooltips.BaseTooltip() |
|---|
| 134 | |
|---|
| 135 |     def del_handlers(self): |
|---|
| 136 |         for i in self.handlers.keys(): |
|---|
| 137 |             if self.handlers[i].handler_is_connected(i): |
|---|
| 138 | Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â self.handlers[i].disconnect(i) |
|---|
| 139 |         del self.handlers |
|---|
| 140 | Â Â Â Â Â Â Â Â self.tv.destroy() |
|---|
| 141 | Â Â Â Â Â Â Â Â #TODO |
|---|
| 142 | Â Â Â Â Â Â Â Â # self.line_tooltip.destroy() |
|---|
| 143 | Â Â Â Â |
|---|
| 144 |     def update_tags(self): |
|---|
| 145 |         self.tagIn.set_property('foreground', gajim.config.get('inmsgcolor')) |
|---|
| 146 |         self.tagOut.set_property('foreground', gajim.config.get('outmsgcolor')) |
|---|
| 147 | Â Â Â Â Â Â Â Â self.tagStatus.set_property('foreground', |
|---|
| 148 | Â Â Â Â Â Â Â Â Â Â Â Â gajim.config.get('statusmsgcolor')) |
|---|
| 149 | |
|---|
| 150 |     def at_the_end(self): |
|---|
| 151 |         buffer = self.tv.get_buffer() |
|---|
| 152 | Â Â Â Â Â Â Â Â end_iter =Â buffer.get_end_iter() |
|---|
| 153 | Â Â Â Â Â Â Â Â end_rect =Â self.tv.get_iter_location(end_iter) |
|---|
| 154 | Â Â Â Â Â Â Â Â visible_rect =Â self.tv.get_visible_rect() |
|---|
| 155 |         if end_rect.y <= (visible_rect.y + visible_rect.height): |
|---|
| 156 |             return True |
|---|
| 157 |         return False |
|---|
| 158 | |
|---|
| 159 |     def scroll_to_end(self): |
|---|
| 160 | Â Â Â Â Â Â Â Â parent =Â self.tv.get_parent() |
|---|
| 161 |         buffer = self.tv.get_buffer() |
|---|
| 162 | Â Â Â Â Â Â Â Â end_mark =Â buffer.get_mark('end') |
|---|
| 163 |         if not end_mark: |
|---|
| 164 |             return False |
|---|
| 165 |         self.tv.scroll_to_mark(end_mark, 0, True, 0, 1) |
|---|
| 166 | Â Â Â Â Â Â Â Â adjustment =Â parent.get_hadjustment() |
|---|
| 167 | Â Â Â Â Â Â Â Â adjustment.set_value(0) |
|---|
| 168 |         return False # when called in an idle_add, just do it once |
|---|
| 169 | |
|---|
| 170 |     def bring_scroll_to_end(self, diff_y = 0): |
|---|
| 171 | Â Â Â Â Â Â Â Â ''' scrolls to the end of textview if end is not visible ''' |
|---|
| 172 |         buffer = self.tv.get_buffer() |
|---|
| 173 | Â Â Â Â Â Â Â Â end_iter =Â buffer.get_end_iter() |
|---|
| 174 | Â Â Â Â Â Â Â Â end_rect =Â self.tv.get_iter_location(end_iter) |
|---|
| 175 | Â Â Â Â Â Â Â Â visible_rect =Â self.tv.get_visible_rect() |
|---|
| 176 | Â Â Â Â Â Â Â Â # scroll only if expected end is not visible |
|---|
| 177 |         if end_rect.y >= (visible_rect.y + visible_rect.height + diff_y): |
|---|
| 178 | Â Â Â Â Â Â Â Â Â Â Â Â gobject.idle_add(self.scroll_to_end_iter) |
|---|
| 179 | |
|---|
| 180 |     def scroll_to_end_iter(self): |
|---|
| 181 |         buffer = self.tv.get_buffer() |
|---|
| 182 | Â Â Â Â Â Â Â Â end_iter =Â buffer.get_end_iter() |
|---|
| 183 |         self.tv.scroll_to_iter(end_iter, 0, False, 1, 1) |
|---|
| 184 |         return False # when called in an idle_add, just do it once |
|---|
| 185 | |
|---|
| 186 |     def show_line_tooltip(self): |
|---|
| 187 | Â Â Â Â Â Â Â Â pointer =Â self.tv.get_pointer() |
|---|
| 188 |         x, y = self.tv.window_to_buffer_coords(gtk.TEXT_WINDOW_TEXT, pointer[0], |
|---|
| 189 | Â Â Â Â Â Â Â Â Â Â Â Â pointer[1]) |
|---|
| 190 |         tags = self.tv.get_iter_at_location(x, y).get_tags() |
|---|
| 191 | Â Â Â Â Â Â Â Â tag_table =Â self.tv.get_buffer().get_tag_table() |
|---|
| 192 | Â Â Â Â Â Â Â Â over_line =Â False |
|---|
| 193 |         for tag in tags: |
|---|
| 194 |             if tag == tag_table.lookup('focus-out-line'): |
|---|
| 195 | Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â over_line =Â True |
|---|
| 196 | Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â break |
|---|
| 197 |         if over_line and not self.line_tooltip.win: |
|---|
| 198 | Â Â Â Â Â Â Â Â Â Â Â Â # check if the current pointer is still over the line |
|---|
| 199 | Â Â Â Â Â Â Â Â Â Â Â Â position =Â self.tv.window.get_origin() |
|---|
| 200 | Â Â Â Â Â Â Â Â Â Â Â Â win =Â self.tv.get_toplevel() |
|---|
| 201 | Â Â Â Â Â Â Â Â Â Â Â Â self.line_tooltip.show_tooltip(_('Text below this line is what has ' |
|---|
| 202 |             'been said since the last time you paid attention to this group chat'), 8, position[1] + pointer[1]) |
|---|
| 203 | |
|---|
| 204 |     def on_textview_motion_notify_event(self, widget, event): |
|---|
| 205 | Â Â Â Â Â Â Â Â '''change the cursor to a hand when we are over a mail or an url''' |
|---|
| 206 |         pointer_x, pointer_y, spam = self.tv.window.get_pointer() |
|---|
| 207 |         x, y = self.tv.window_to_buffer_coords(gtk.TEXT_WINDOW_TEXT, pointer_x, |
|---|
| 208 | Â Â Â Â Â Â Â Â Â Â Â Â pointer_y) |
|---|
| 209 |         tags = self.tv.get_iter_at_location(x, y).get_tags() |
|---|
| 210 |         if self.change_cursor: |
|---|
| 211 | Â Â Â Â Â Â Â Â Â Â Â Â self.tv.get_window(gtk.TEXT_WINDOW_TEXT).set_cursor( |
|---|
| 212 | Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â gtk.gdk.Cursor(gtk.gdk.XTERM)) |
|---|
| 213 | Â Â Â Â Â Â Â Â Â Â Â Â self.change_cursor =Â None |
|---|
| 214 | Â Â Â Â Â Â Â Â tag_table =Â self.tv.get_buffer().get_tag_table() |
|---|
| 215 | Â Â Â Â Â Â Â Â over_line =Â False |
|---|
| 216 |         for tag in tags: |
|---|
| 217 |             if tag in (tag_table.lookup('url'), tag_table.lookup('mail')): |
|---|
| 218 | Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â self.tv.get_window(gtk.TEXT_WINDOW_TEXT).set_cursor( |
|---|
| 219 | Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â gtk.gdk.Cursor(gtk.gdk.HAND2)) |
|---|
| 220 | Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â self.change_cursor =Â tag |
|---|
| 221 |             elif tag == tag_table.lookup('focus-out-line'): |
|---|
| 222 | Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â over_line =Â True |
|---|
| 223 | |
|---|
| 224 |         if self.line_tooltip.timeout != 0: |
|---|
| 225 | Â Â Â Â Â Â Â Â Â Â Â Â # Check if we should hide the line tooltip |
|---|
| 226 |             if not over_line: |
|---|
| 227 | Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â self.line_tooltip.hide_tooltip() |
|---|
| 228 |         if over_line and not self.line_tooltip.win: |
|---|
| 229 | Â Â Â Â Â Â Â Â Â Â Â Â self.line_tooltip.timeout =Â gobject.timeout_add(500, |
|---|
| 230 | Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â self.show_line_tooltip) |
|---|
| 231 | Â Â Â Â Â Â Â Â Â Â Â Â self.tv.get_window(gtk.TEXT_WINDOW_TEXT).set_cursor( |
|---|
| 232 | Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â gtk.gdk.Cursor(gtk.gdk.LEFT_PTR)) |
|---|
| 233 | Â Â Â Â Â Â Â Â Â Â Â Â self.change_cursor =Â tag |
|---|
| 234 | |
|---|
| 235 |     def clear(self, tv = None): |
|---|
| 236 | Â Â Â Â Â Â Â Â '''clear text in the textview''' |
|---|
| 237 |         buffer = self.tv.get_buffer() |
|---|
| 238 |         start, end = buffer.get_bounds() |
|---|
| 239 |         buffer.delete(start, end) |
|---|
| 240 | |
|---|
| 241 |     def visit_url_from_menuitem(self, widget, link): |
|---|
| 242 | Â Â Â Â Â Â Â Â '''basically it filters out the widget instance''' |
|---|
| 243 |         helpers.launch_browser_mailer('url', link) |
|---|
| 244 | |
|---|
| 245 |     def on_textview_populate_popup(self, textview, menu): |
|---|
| 246 | Â Â Â Â Â Â Â Â '''we override the default context menu and we prepend Clear |
|---|
| 247 | Â Â Â Â Â Â Â Â and if we have sth selected we show a submenu with actions on the phrase |
|---|
| 248 | Â Â Â Â Â Â Â Â (see on_conversation_textview_button_press_event)''' |
|---|
| 249 | Â Â Â Â Â Â Â Â item =Â gtk.SeparatorMenuItem() |
|---|
| 250 | Â Â Â Â Â Â Â Â menu.prepend(item) |
|---|
| 251 | Â Â Â Â Â Â Â Â item =Â gtk.ImageMenuItem(gtk.STOCK_CLEAR) |
|---|
| 252 | Â Â Â Â Â Â Â Â menu.prepend(item) |
|---|
| 253 |         id = item.connect('activate', self.clear) |
|---|
| 254 | Â Â Â Â Â Â Â Â self.handlers[id]Â =Â item |
|---|
| 255 |         if self.selected_phrase: |
|---|
| 256 | Â Â Â Â Â Â Â Â Â Â Â Â s =Â self.selected_phrase |
|---|
| 257 |             if len(s) > 25: |
|---|
| 258 | Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â s =Â s[:21]Â +Â '...' |
|---|
| 259 | Â Â Â Â Â Â Â Â Â Â Â Â item =Â gtk.MenuItem(_('Actions for "%s"')Â %Â s) |
|---|
| 260 | Â Â Â Â Â Â Â Â Â Â Â Â menu.prepend(item) |
|---|
| 261 | Â Â Â Â Â Â Â Â Â Â Â Â submenu =Â gtk.Menu() |
|---|
| 262 | Â Â Â Â Â Â Â Â Â Â Â Â item.set_submenu(submenu) |
|---|
| 263 | |
|---|
| 264 | Â Â Â Â Â Â Â Â Â Â Â Â always_use_en =Â gajim.config.get('always_english_wikipedia') |
|---|
| 265 |             if always_use_en: |
|---|
| 266 | Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â link =Â 'http://en.wikipedia.org/wiki/Special:Search?search=%s'\ |
|---|
| 267 | Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â %Â self.selected_phrase |
|---|
| 268 | Â Â Â Â Â Â Â Â Â Â Â Â else: |
|---|
| 269 | Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â link =Â 'http://%s.wikipedia.org/wiki/Special:Search?search=%s'\ |
|---|
| 270 |                     % (gajim.LANG, self.selected_phrase) |
|---|
| 271 | Â Â Â Â Â Â Â Â Â Â Â Â item =Â gtk.MenuItem(_('Read _Wikipedia Article')) |
|---|
| 272 |             id = item.connect('activate', self.visit_url_from_menuitem, link) |
|---|
| 273 | Â Â Â Â Â Â Â Â Â Â Â Â self.handlers[id]Â =Â item |
|---|
| 274 | Â Â Â Â Â Â Â Â Â Â Â Â submenu.append(item) |
|---|
| 275 | |
|---|
| 276 | Â Â Â Â Â Â Â Â Â Â Â Â item =Â gtk.MenuItem(_('Look it up in _Dictionary')) |
|---|
| 277 | Â Â Â Â Â Â Â Â Â Â Â Â dict_link =Â gajim.config.get('dictionary_url') |
|---|
| 278 |             if dict_link == 'WIKTIONARY': |
|---|
| 279 | Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â # special link (yeah undocumented but default) |
|---|
| 280 | Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â always_use_en =Â gajim.config.get('always_english_wiktionary') |
|---|
| 281 |                 if always_use_en: |
|---|
| 282 | Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â link =Â 'http://en.wiktionary.org/wiki/Special:Search?search=%s'\ |
|---|
| 283 | Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â %Â self.selected_phrase |
|---|
| 284 | Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â else: |
|---|
| 285 | Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â link =Â 'http://%s.wiktionary.org/wiki/Special:Search?search=%s'\ |
|---|
| 286 |                         % (gajim.LANG, self.selected_phrase) |
|---|
| 287 |                 id = item.connect('activate', self.visit_url_from_menuitem, link) |
|---|
| 288 | Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â self.handlers[id]Â =Â item |
|---|
| 289 | Â Â Â Â Â Â Â Â Â Â Â Â else: |
|---|
| 290 |                 if dict_link.find('%s') == -1: |
|---|
| 291 | Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â #we must have %s in the url if not WIKTIONARY |
|---|
| 292 | Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â item =Â gtk.MenuItem(_('Dictionary URL is missing an "%s" and it is not WIKTIONARY')) |
|---|
| 293 |                     item.set_property('sensitive', False) |
|---|
| 294 | Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â else: |
|---|
| 295 | Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â link =Â dict_link %Â self.selected_phrase |
|---|
| 296 |                     id = item.connect('activate', self.visit_url_from_menuitem, link) |
|---|
| 297 | Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â self.handlers[id]Â =Â item |
|---|
| 298 | Â Â Â Â Â Â Â Â Â Â Â Â submenu.append(item) |
|---|
| 299 | |
|---|
| 300 | |
|---|
| 301 | Â Â Â Â Â Â Â Â Â Â Â Â search_link =Â gajim.config.get('search_engine') |
|---|
| 302 |             if search_link.find('%s') == -1: |
|---|
| 303 | Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â #we must have %s in the url |
|---|
| 304 | Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â item =Â gtk.MenuItem(_('Web Search URL is missing an "%s"')) |
|---|
| 305 |                 item.set_property('sensitive', False) |
|---|
| 306 | Â Â Â Â Â Â Â Â Â Â Â Â else: |
|---|
| 307 | Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â item =Â gtk.MenuItem(_('Web _Search for it')) |
|---|
| 308 | Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â link =Â search_link %Â self.selected_phrase |
|---|
| 309 |                 id = item.connect('activate', self.visit_url_from_menuitem, link) |
|---|
| 310 | Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â self.handlers[id]Â =Â item |
|---|
| 311 | Â Â Â Â Â Â Â Â Â Â Â Â submenu.append(item) |
|---|
| 312 | |
|---|
| 313 | Â Â Â Â Â Â Â Â menu.show_all() |
|---|
| 314 | |
|---|
| 315 |     def on_textview_button_press_event(self, widget, event): |
|---|
| 316 | Â Â Â Â Â Â Â Â # If we clicked on a taged text do NOT open the standard popup menu |
|---|
| 317 | Â Â Â Â Â Â Â Â # if normal text check if we have sth selected |
|---|
| 318 | |
|---|
| 319 | Â Â Â Â Â Â Â Â self.selected_phrase =Â '' |
|---|
| 320 | |
|---|
| 321 |         if event.button != 3: # if not right click |
|---|
| 322 |             return False |
|---|
| 323 | |
|---|
| 324 | Â Â Â Â Â Â Â Â win =Â self.tv.get_window(gtk.TEXT_WINDOW_TEXT) |
|---|
| 325 |         x, y = self.tv.window_to_buffer_coords(gtk.TEXT_WINDOW_TEXT, |
|---|
| 326 |             int(event.x), int(event.y)) |
|---|
| 327 |         iter = self.tv.get_iter_at_location(x, y) |
|---|
| 328 | Â Â Â Â Â Â Â Â tags =Â iter.get_tags() |
|---|
| 329 | |
|---|
| 330 | |
|---|
| 331 |         if tags: # we clicked on sth special (it can be status message too) |
|---|
| 332 |             for tag in tags: |
|---|
| 333 | Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â tag_name =Â tag.get_property('name') |
|---|
| 334 |                 if 'url' in tag_name or 'mail' in tag_name: |
|---|
| 335 |                     return True # we block normal context menu |
|---|
| 336 | |
|---|
| 337 | Â Â Â Â Â Â Â Â # we check if sth was selected and if it was we assign |
|---|
| 338 | Â Â Â Â Â Â Â Â # selected_phrase variable |
|---|
| 339 | Â Â Â Â Â Â Â Â # so on_conversation_textview_populate_popup can use it |
|---|
| 340 |         buffer = self.tv.get_buffer() |
|---|
| 341 | Â Â Â Â Â Â Â Â return_val =Â buffer.get_selection_bounds() |
|---|
| 342 |         if return_val: # if sth was selected when we right-clicked |
|---|
| 343 | Â Â Â Â Â Â Â Â Â Â Â Â # get the selected text |
|---|
| 344 |             start_sel, finish_sel = return_val[0], return_val[1] |
|---|
| 345 |             self.selected_phrase = buffer.get_text(start_sel, finish_sel).decode('utf-8') |
|---|
| 346 | |
|---|
| 347 |     def on_open_link_activate(self, widget, kind, text): |
|---|
| 348 |         helpers.launch_browser_mailer(kind, text) |
|---|
| 349 | |
|---|
| 350 |     def on_copy_link_activate(self, widget, text): |
|---|
| 351 | Â Â Â Â Â Â Â Â clip =Â gtk.clipboard_get() |
|---|
| 352 | Â Â Â Â Â Â Â Â clip.set_text(text) |
|---|
| 353 | |
|---|
| 354 |     def on_start_chat_activate(self, widget, jid): |
|---|
| 355 |         gajim.interface.roster.new_chat_from_jid(self.account, jid) |
|---|
| 356 | |
|---|
| 357 |     def on_join_group_chat_menuitem_activate(self, widget, jid): |
|---|
| 358 |         room, server = jid.split('@') |
|---|
| 359 |         if gajim.interface.instances[self.account].has_key('join_gc'): |
|---|
| 360 | Â Â Â Â Â Â Â Â Â Â Â Â instance =Â gajim.interface.instances[self.account]['join_gc'] |
|---|
| 361 | Â Â Â Â Â Â Â Â Â Â Â Â instance.xml.get_widget('server_entry').set_text(server) |
|---|
| 362 | Â Â Â Â Â Â Â Â Â Â Â Â instance.xml.get_widget('room_entry').set_text(room) |
|---|
| 363 | Â Â Â Â Â Â Â Â Â Â Â Â gajim.interface.instances[self.account]['join_gc'].window.present() |
|---|
| 364 | Â Â Â Â Â Â Â Â else: |
|---|
| 365 | Â Â Â Â Â Â Â Â Â Â Â Â try: |
|---|
| 366 | Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â gajim.interface.instances[self.account]['join_gc']Â =Â \ |
|---|
| 367 |                 dialogs.JoinGroupchatWindow(self.account, server, room) |
|---|
| 368 |             except RuntimeError: |
|---|
| 369 | Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â pass |
|---|
| 370 | |
|---|
| 371 |     def on_add_to_roster_activate(self, widget, jid): |
|---|
| 372 |         dialogs.AddNewContactWindow(self.account, jid) |
|---|
| 373 | |
|---|
| 374 |     def make_link_menu(self, event, kind, text): |
|---|
| 375 | Â Â Â Â Â Â Â Â xml =Â gtkgui_helpers.get_glade('chat_context_menu.glade') |
|---|
| 376 | Â Â Â Â Â Â Â Â menu =Â xml.get_widget('chat_context_menu') |
|---|
| 377 | Â Â Â Â Â Â Â Â childs =Â menu.get_children() |
|---|
| 378 |         if kind == 'url': |
|---|
| 379 |             id = childs[0].connect('activate', self.on_copy_link_activate, text) |
|---|
| 380 | Â Â Â Â Â Â Â Â Â Â Â Â self.handlers[id]Â =Â childs[0] |
|---|
| 381 |             id = childs[1].connect('activate', self.on_open_link_activate, kind, text) |
|---|
| 382 | Â Â Â Â Â Â Â Â Â Â Â Â self.handlers[id]Â =Â childs[1] |
|---|
| 383 | Â Â Â Â Â Â Â Â Â Â Â Â childs[2].hide()Â # copy mail address |
|---|
| 384 | Â Â Â Â Â Â Â Â Â Â Â Â childs[3].hide()Â # open mail composer |
|---|
| 385 | Â Â Â Â Â Â Â Â Â Â Â Â childs[4].hide()Â # jid section separator |
|---|
| 386 | Â Â Â Â Â Â Â Â Â Â Â Â childs[5].hide()Â # start chat |
|---|
| 387 | Â Â Â Â Â Â Â Â Â Â Â Â childs[6].hide()Â # join group chat |
|---|
| 388 | Â Â Â Â Â Â Â Â Â Â Â Â childs[7].hide()Â # add to roster |
|---|
| 389 | Â Â Â Â Â Â Â Â else:Â # It's a mail or a JID |
|---|
| 390 |             id = childs[2].connect('activate', self.on_copy_link_activate, text) |
|---|
| 391 | Â Â Â Â Â Â Â Â Â Â Â Â self.handlers[id]Â =Â childs[2] |
|---|
| 392 |             id = childs[3].connect('activate', self.on_open_link_activate, kind, text) |
|---|
| 393 | Â Â Â Â Â Â Â Â Â Â Â Â self.handlers[id]Â =Â childs[3] |
|---|
| 394 |             id = childs[5].connect('activate', self.on_start_chat_activate, text) |
|---|
| 395 | Â Â Â Â Â Â Â Â Â Â Â Â self.handlers[id]Â =Â childs[5] |
|---|
| 396 |             id = childs[6].connect('activate', |
|---|
| 397 |                 self.on_join_group_chat_menuitem_activate, text) |
|---|
| 398 | Â Â Â Â Â Â Â Â Â Â Â Â self.handlers[id]Â =Â childs[6] |
|---|
| 399 | |
|---|
| 400 | Â Â Â Â Â Â Â Â Â Â Â Â allow_add =Â False |
|---|
| 401 |             c = gajim.contacts.get_first_contact_from_jid(self.account, text) |
|---|
| 402 |             if c and not gajim.contacts.is_pm_from_contact(self.account, c): |
|---|
| 403 |                 if _('Not in Roster') in c.groups: |
|---|
| 404 | Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â allow_add =Â True |
|---|
| 405 | Â Â Â Â Â Â Â Â Â Â Â Â else:Â # he or she's not at all in the account contacts |
|---|
| 406 | Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â allow_add =Â True |
|---|
| 407 | |
|---|
| 408 |             if allow_add: |
|---|
| 409 |                 id = childs[7].connect('activate', self.on_add_to_roster_activate, text) |
|---|
| 410 | Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â self.handlers[id]Â =Â childs[7] |
|---|
| 411 | Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â childs[7].show()Â # show add to roster menuitem |
|---|
| 412 | Â Â Â Â Â Â Â Â Â Â Â Â else: |
|---|
| 413 | Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â childs[7].hide()Â # hide add to roster menuitem |
|---|
| 414 | |
|---|
| 415 | Â Â Â Â Â Â Â Â Â Â Â Â childs[0].hide()Â # copy link location |
|---|
| 416 | Â Â Â Â Â Â Â Â Â Â Â Â childs[1].hide()Â # open link in browser |
|---|
| 417 | |
|---|
| 418 |         menu.popup(None, None, None, event.button, event.time) |
|---|
| 419 | |
|---|
| 420 |     def hyperlink_handler(self, texttag, widget, event, iter, kind): |
|---|
| 421 |         if event.type == gtk.gdk.BUTTON_PRESS: |
|---|
| 422 | Â Â Â Â Â Â Â Â Â Â Â Â begin_iter =Â iter.copy() |
|---|
| 423 | Â Â Â Â Â Â Â Â Â Â Â Â # we get the begining of the tag |
|---|
| 424 |             while not begin_iter.begins_tag(texttag): |
|---|
| 425 | Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â begin_iter.backward_char() |
|---|
| 426 | Â Â Â Â Â Â Â Â Â Â Â Â end_iter =Â iter.copy() |
|---|
| 427 | Â Â Â Â Â Â Â Â Â Â Â Â # we get the end of the tag |
|---|
| 428 |             while not end_iter.ends_tag(texttag): |
|---|
| 429 | Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â end_iter.forward_char() |
|---|
| 430 |             word = self.tv.get_buffer().get_text(begin_iter, end_iter).decode('utf-8') |
|---|
| 431 |             if event.button == 3: # right click |
|---|
| 432 |                 self.make_link_menu(event, kind, word) |
|---|
| 433 | Â Â Â Â Â Â Â Â Â Â Â Â else: |
|---|
| 434 | Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â # we launch the correct application |
|---|
| 435 |                 helpers.launch_browser_mailer(kind, word) |
|---|
| 436 | |
|---|
| 437 |     def detect_and_print_special_text(self, otext, other_tags): |
|---|
| 438 | Â Â Â Â Â Â Â Â '''detects special text (emots & links & formatting) |
|---|
| 439 | Â Â Â Â Â Â Â Â prints normal text before any special text it founts, |
|---|
| 440 | Â Â Â Â Â Â Â Â then print special text (that happens many times until |
|---|
| 441 | Â Â Â Â Â Â Â Â last special text is printed) and then returns the index |
|---|
| 442 | Â Â Â Â Â Â Â Â after *last* special text, so we can print it in |
|---|
| 443 | Â Â Â Â Â Â Â Â print_conversation_line()''' |
|---|
| 444 | |
|---|
| 445 |         buffer = self.tv.get_buffer() |
|---|
| 446 | |
|---|
| 447 | Â Â Â Â Â Â Â Â start =Â 0 |
|---|
| 448 | Â Â Â Â Â Â Â Â end =Â 0 |
|---|
| 449 | Â Â Â Â Â Â Â Â index =Â 0 |
|---|
| 450 | |
|---|
| 451 | Â Â Â Â Â Â Â Â # basic: links + mail + formatting is always checked (we like that) |
|---|
| 452 |         if gajim.config.get('emoticons_theme'): # search for emoticons & urls |
|---|
| 453 | Â Â Â Â Â Â Â Â Â Â Â Â iterator =Â gajim.interface.emot_and_basic_re.finditer(otext) |
|---|
| 454 | Â Â Â Â Â Â Â Â else:Â # search for just urls + mail + formatting |
|---|
| 455 | Â Â Â Â Â Â Â Â Â Â Â Â iterator =Â gajim.interface.basic_pattern_re.finditer(otext) |
|---|
| 456 |         for match in iterator: |
|---|
| 457 |             start, end = match.span() |
|---|
| 458 | Â Â Â Â Â Â Â Â Â Â Â Â special_text =Â otext[start:end] |
|---|
| 459 |             if start != 0: |
|---|
| 460 | Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â text_before_special_text =Â otext[index:start] |
|---|
| 461 | Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â end_iter =Â buffer.get_end_iter() |
|---|
| 462 | Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â # we insert normal text |
|---|
| 463 | Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â buffer.insert_with_tags_by_name(end_iter, |
|---|
| 464 |                     text_before_special_text, *other_tags) |
|---|
| 465 | Â Â Â Â Â Â Â Â Â Â Â Â index =Â end # update index |
|---|
| 466 | |
|---|
| 467 | Â Â Â Â Â Â Â Â Â Â Â Â # now print it |
|---|
| 468 |             self.print_special_text(special_text, other_tags) |
|---|
| 469 | |
|---|
| 470 |         return index # the position after *last* special text |
|---|
| 471 | |
|---|
| 472 |     def print_special_text(self, special_text, other_tags): |
|---|
| 473 | Â Â Â Â Â Â Â Â '''is called by detect_and_print_special_text and prints |
|---|
| 474 | Â Â Â Â Â Â Â Â special text (emots, links, formatting)''' |
|---|
| 475 | Â Â Â Â Â Â Â Â tags =Â [] |
|---|
| 476 | Â Â Â Â Â Â Â Â use_other_tags =Â True |
|---|
| 477 | Â Â Â Â Â Â Â Â show_ascii_formatting_chars =Â \ |
|---|
| 478 | Â Â Â Â Â Â Â Â Â Â Â Â gajim.config.get('show_ascii_formatting_chars') |
|---|
| 479 |         buffer = self.tv.get_buffer() |
|---|
| 480 | |
|---|
| 481 | Â Â Â Â Â Â Â Â possible_emot_ascii_caps =Â special_text.upper()Â # emoticons keys are CAPS |
|---|
| 482 |         if gajim.config.get('emoticons_theme') and \ |
|---|
| 483 |         possible_emot_ascii_caps in gajim.interface.emoticons.keys(): |
|---|
| 484 | Â Â Â Â Â Â Â Â Â Â Â Â #it's an emoticon |
|---|
| 485 | Â Â Â Â Â Â Â Â Â Â Â Â emot_ascii =Â possible_emot_ascii_caps |
|---|
| 486 | Â Â Â Â Â Â Â Â Â Â Â Â end_iter =Â buffer.get_end_iter() |
|---|
| 487 | Â Â Â Â Â Â Â Â Â Â Â Â anchor =Â buffer.create_child_anchor(end_iter) |
|---|
| 488 | Â Â Â Â Â Â Â Â Â Â Â Â img =Â gtk.Image() |
|---|
| 489 | Â Â Â Â Â Â Â Â Â Â Â Â img.set_from_file(gajim.interface.emoticons[emot_ascii]) |
|---|
| 490 | Â Â Â Â Â Â Â Â Â Â Â Â img.show() |
|---|
| 491 | Â Â Â Â Â Â Â Â Â Â Â Â #add with possible animation |
|---|
| 492 |             self.tv.add_child_at_anchor(img, anchor) |
|---|
| 493 |         elif special_text.startswith('http://') or \ |
|---|
| 494 |             special_text.startswith('www.') or \ |
|---|
| 495 |             special_text.startswith('ftp://') or \ |
|---|
| 496 |             special_text.startswith('ftp.') or \ |
|---|
| 497 |             special_text.startswith('https://') or \ |
|---|
| 498 |             special_text.startswith('gopher://') or \ |
|---|
| 499 |             special_text.startswith('news://') or \ |
|---|
| 500 |             special_text.startswith('ed2k://') or \ |
|---|
| 501 |             special_text.startswith('irc://') or \ |
|---|
| 502 |             special_text.startswith('sip:') or \ |
|---|
| 503 | Â Â Â Â Â Â Â Â Â Â Â Â special_text.startswith('magnet:'): |
|---|
| 504 | Â Â Â Â Â Â Â Â Â Â Â Â #it's a url |
|---|
| 505 | Â Â Â Â Â Â Â Â Â Â Â Â tags.append('url') |
|---|
| 506 | Â Â Â Â Â Â Â Â Â Â Â Â use_other_tags =Â False |
|---|
| 507 |         elif special_text.startswith('mailto:'): |
|---|
| 508 | Â Â Â Â Â Â Â Â Â Â Â Â #it's a mail |
|---|
| 509 | Â Â Â Â Â Â Â Â Â Â Â Â tags.append('mail') |
|---|
| 510 | Â Â Â Â Â Â Â Â Â Â Â Â use_other_tags =Â False |
|---|
| 511 |         elif gajim.interface.sth_at_sth_dot_sth_re.match(special_text): |
|---|
| 512 | Â Â Â Â Â Â Â Â Â Â Â Â #it's a mail |
|---|
| 513 | Â Â Â Â Â Â Â Â Â Â Â Â tags.append('mail') |
|---|
| 514 | Â Â Â Â Â Â Â Â Â Â Â Â use_other_tags =Â False |
|---|
| 515 |         elif special_text.startswith('*'): # it's a bold text |
|---|
| 516 | Â Â Â Â Â Â Â Â Â Â Â Â tags.append('bold') |
|---|
| 517 |             if special_text[1] == '/' and special_text[-2] == '/' and len(special_text) > 4: # it's also italic |
|---|
| 518 | Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â tags.append('italic') |
|---|
| 519 |                 if not show_ascii_formatting_chars: |
|---|
| 520 | Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â special_text =Â special_text[2:-2]Â # remove */ /* |
|---|
| 521 |             elif special_text[1] == '_' and special_text[-2] == '_' and len(special_text) > 4: # it's also underlined |
|---|
| 522 | Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â tags.append('underline') |
|---|
| 523 |                 if not show_ascii_formatting_chars: |
|---|
| 524 | Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â special_text =Â special_text[2:-2]Â # remove *_ _* |
|---|
| 525 | Â Â Â Â Â Â Â Â Â Â Â Â else: |
|---|
| 526 |                 if not show_ascii_formatting_chars: |
|---|
| 527 | Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â special_text =Â special_text[1:-1]Â # remove * * |
|---|
| 528 |         elif special_text.startswith('/'): # it's an italic text |
|---|
| 529 | Â Â Â Â Â Â Â Â Â Â Â Â tags.append('italic') |
|---|
| 530 |             if special_text[1] == '*' and special_text[-2] == '*' and len(special_text) > 4: # it's also bold |
|---|
| 531 | Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â tags.append('bold') |
|---|
| 532 |                 if not show_ascii_formatting_chars: |
|---|
| 533 | Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â special_text =Â special_text[2:-2]Â # remove /* */ |
|---|
| 534 |             elif special_text[1] == '_' and special_text[-2] == '_' and len(special_text) > 4: # it's also underlined |
|---|
| 535 | Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â tags.append('underline') |
|---|
| 536 |                 if not show_ascii_formatting_chars: |
|---|
| 537 | Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â special_text =Â special_text[2:-2]Â # remove /_ _/ |
|---|
| 538 | Â Â Â Â Â Â Â Â Â Â Â Â else: |
|---|
| 539 |                 if not show_ascii_formatting_chars: |
|---|
| 540 | Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â special_text =Â special_text[1:-1]Â # remove / / |
|---|
| 541 |         elif special_text.startswith('_'): # it's an underlined text |
|---|
| 542 | Â Â Â Â Â Â Â Â Â Â Â Â tags.append('underline') |
|---|
| 543 |             if special_text[1] == '*' and special_text[-2] == '*' and len(special_text) > 4: # it's also bold |
|---|
| 544 | Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â tags.append('bold') |
|---|
| 545 |                 if not show_ascii_formatting_chars: |
|---|
| 546 | Â Â |
|---|