| 1 | ## message_window.py |
|---|
| 2 | ## |
|---|
| 3 | ## Copyright (C) 2003-2004 Yann Le Boulanger <asterix@lagaule.org> |
|---|
| 4 | ## Vincent Hanquez <tab@snarc.org> |
|---|
| 5 | ## Copyright (C) 2005 Yann Le Boulanger <asterix@lagaule.org> |
|---|
| 6 | ## Vincent Hanquez <tab@snarc.org> |
|---|
| 7 | ## Nikos Kouremenos <kourem@gmail.com> |
|---|
| 8 | ## Dimitur Kirov <dkirov@gmail.com> |
|---|
| 9 | ## Travis Shirk <travis@pobox.com> |
|---|
| 10 | ## Norman Rasmussen <norman@rasmussen.co.za> |
|---|
| 11 | ## Copyright (C) 2006 Travis Shirk <travis@pobox.com> |
|---|
| 12 | ## Copyright (C) 2006 Geobert Quach <geobert@gmail.com> |
|---|
| 13 | ## |
|---|
| 14 | ## This program is free software; you can redistribute it and/or modify |
|---|
| 15 | ## it under the terms of the GNU General Public License as published |
|---|
| 16 | ## by the Free Software Foundation; version 2 only. |
|---|
| 17 | ## |
|---|
| 18 | ## This program is distributed in the hope that it will be useful, |
|---|
| 19 | ## but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 20 | ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 21 | ## GNU General Public License for more details. |
|---|
| 22 | ## |
|---|
| 23 | |
|---|
| 24 | import gtk |
|---|
| 25 | import gobject |
|---|
| 26 | |
|---|
| 27 | import common |
|---|
| 28 | import gtkgui_helpers |
|---|
| 29 | import message_control |
|---|
| 30 | from chat_control import ChatControlBase |
|---|
| 31 | |
|---|
| 32 | from common import gajim |
|---|
| 33 | |
|---|
| 34 | #################### |
|---|
| 35 | |
|---|
| 36 | class MessageWindow: |
|---|
| 37 | '''Class for windows which contain message like things; chats, |
|---|
| 38 | groupchats, etc.''' |
|---|
| 39 | |
|---|
| 40 | # DND_TARGETS is the targets needed by drag_source_set and drag_dest_set |
|---|
| 41 | DND_TARGETS = [('GAJIM_TAB', 0, 81)] |
|---|
| 42 | hid = 0 # drag_data_received handler id |
|---|
| 43 | ( |
|---|
| 44 | CLOSE_TAB_MIDDLE_CLICK, |
|---|
| 45 | CLOSE_ESC, |
|---|
| 46 | CLOSE_CLOSE_BUTTON, |
|---|
| 47 | CLOSE_COMMAND, |
|---|
| 48 | CLOSE_CTRL_KEY |
|---|
| 49 | ) = range(5) |
|---|
| 50 | |
|---|
| 51 | def __init__(self, acct, type): |
|---|
| 52 | # A dictionary of dictionaries where _contacts[account][jid] == A MessageControl |
|---|
| 53 | self._controls = {} |
|---|
| 54 | # If None, the window is not tied to any specific account |
|---|
| 55 | self.account = acct |
|---|
| 56 | # If None, the window is not tied to any specific type |
|---|
| 57 | self.type = type |
|---|
| 58 | # dict { handler id: widget}. Keeps callbacks, which |
|---|
| 59 | # lead to cylcular references |
|---|
| 60 | self.handlers = {} |
|---|
| 61 | |
|---|
| 62 | self.widget_name = 'message_window' |
|---|
| 63 | self.xml = gtkgui_helpers.get_glade('%s.glade' % self.widget_name) |
|---|
| 64 | self.window = self.xml.get_widget(self.widget_name) |
|---|
| 65 | id = self.window.connect('delete-event', self._on_window_delete) |
|---|
| 66 | self.handlers[id] = self.window |
|---|
| 67 | id = self.window.connect('destroy', self._on_window_destroy) |
|---|
| 68 | self.handlers[id] = self.window |
|---|
| 69 | id = self.window.connect('focus-in-event', self._on_window_focus) |
|---|
| 70 | self.handlers[id] = self.window |
|---|
| 71 | |
|---|
| 72 | # gtk+ doesn't make use of the motion notify on gtkwindow by default |
|---|
| 73 | # so this line adds that |
|---|
| 74 | self.window.add_events(gtk.gdk.POINTER_MOTION_MASK) |
|---|
| 75 | self.alignment = self.xml.get_widget('alignment') |
|---|
| 76 | |
|---|
| 77 | self.notebook = self.xml.get_widget('notebook') |
|---|
| 78 | id = self.notebook.connect('switch-page', |
|---|
| 79 | self._on_notebook_switch_page) |
|---|
| 80 | self.handlers[id] = self.notebook |
|---|
| 81 | id = self.notebook.connect('key-press-event', |
|---|
| 82 | self._on_notebook_key_press) |
|---|
| 83 | self.handlers[id] = self.notebook |
|---|
| 84 | |
|---|
| 85 | # Remove the glade pages |
|---|
| 86 | while self.notebook.get_n_pages(): |
|---|
| 87 | self.notebook.remove_page(0) |
|---|
| 88 | # Tab customizations |
|---|
| 89 | pref_pos = gajim.config.get('tabs_position') |
|---|
| 90 | if pref_pos == 'bottom': |
|---|
| 91 | nb_pos = gtk.POS_BOTTOM |
|---|
| 92 | elif pref_pos == 'left': |
|---|
| 93 | nb_pos = gtk.POS_LEFT |
|---|
| 94 | elif pref_pos == 'right': |
|---|
| 95 | nb_pos = gtk.POS_RIGHT |
|---|
| 96 | else: |
|---|
| 97 | nb_pos = gtk.POS_TOP |
|---|
| 98 | self.notebook.set_tab_pos(nb_pos) |
|---|
| 99 | if gajim.config.get('tabs_always_visible'): |
|---|
| 100 | self.notebook.set_show_tabs(True) |
|---|
| 101 | self.alignment.set_property('top-padding', 2) |
|---|
| 102 | else: |
|---|
| 103 | self.notebook.set_show_tabs(False) |
|---|
| 104 | self.notebook.set_show_border(gajim.config.get('tabs_border')) |
|---|
| 105 | |
|---|
| 106 | # set up DnD if GTK+ version < 2.10, use OUR way to reorder tabs |
|---|
| 107 | if gtk.pygtk_version < (2, 10, 0) or gtk.gtk_version < (2, 10, 0): |
|---|
| 108 | self.hid = self.notebook.connect('drag_data_received', |
|---|
| 109 | self.on_tab_label_drag_data_received_cb) |
|---|
| 110 | self.handlers[self.hid] = self.notebook |
|---|
| 111 | self.notebook.drag_dest_set(gtk.DEST_DEFAULT_ALL, self.DND_TARGETS, |
|---|
| 112 | gtk.gdk.ACTION_MOVE) |
|---|
| 113 | |
|---|
| 114 | def change_account_name(self, old_name, new_name): |
|---|
| 115 | if self._controls.has_key(old_name): |
|---|
| 116 | self._controls[new_name] = self._controls[old_name] |
|---|
| 117 | del self._controls[old_name] |
|---|
| 118 | for ctrl in self.controls(): |
|---|
| 119 | if ctrl.account == old_name: |
|---|
| 120 | ctrl.account = new_name |
|---|
| 121 | if self.account == old_name: |
|---|
| 122 | self.account = new_name |
|---|
| 123 | |
|---|
| 124 | def get_num_controls(self): |
|---|
| 125 | n = 0 |
|---|
| 126 | for dict in self._controls.values(): |
|---|
| 127 | n += len(dict) |
|---|
| 128 | return n |
|---|
| 129 | |
|---|
| 130 | def _on_window_focus(self, widget, event): |
|---|
| 131 | # window received focus, so if we had urgency REMOVE IT |
|---|
| 132 | # NOTE: we do not have to read the message (it maybe in a bg tab) |
|---|
| 133 | # to remove urgency hint so this functions does that |
|---|
| 134 | gtkgui_helpers.set_unset_urgency_hint(self.window, False) |
|---|
| 135 | |
|---|
| 136 | ctrl = self.get_active_control() |
|---|
| 137 | if ctrl: |
|---|
| 138 | ctrl.set_control_active(True) |
|---|
| 139 | # Undo "unread" state display, etc. |
|---|
| 140 | if ctrl.type_id == message_control.TYPE_GC: |
|---|
| 141 | self.redraw_tab(ctrl, 'active') |
|---|
| 142 | else: |
|---|
| 143 | # NOTE: we do not send any chatstate to preserve |
|---|
| 144 | # inactive, gone, etc. |
|---|
| 145 | self.redraw_tab(ctrl) |
|---|
| 146 | |
|---|
| 147 | def _on_window_delete(self, win, event): |
|---|
| 148 | # Make sure all controls are okay with being deleted |
|---|
| 149 | for ctrl in self.controls(): |
|---|
| 150 | if not ctrl.allow_shutdown(self.CLOSE_CLOSE_BUTTON): |
|---|
| 151 | return True # halt the delete |
|---|
| 152 | return False |
|---|
| 153 | |
|---|
| 154 | def _on_window_destroy(self, win): |
|---|
| 155 | for ctrl in self.controls(): |
|---|
| 156 | ctrl.shutdown() |
|---|
| 157 | self._controls.clear() |
|---|
| 158 | for i in self.handlers.keys(): |
|---|
| 159 | if self.handlers[i].handler_is_connected(i): |
|---|
| 160 | self.handlers[i].disconnect(i) |
|---|
| 161 | del self.handlers[i] |
|---|
| 162 | del self.handlers |
|---|
| 163 | |
|---|
| 164 | def new_tab(self, control): |
|---|
| 165 | if not self._controls.has_key(control.account): |
|---|
| 166 | self._controls[control.account] = {} |
|---|
| 167 | fjid = control.get_full_jid() |
|---|
| 168 | self._controls[control.account][fjid] = control |
|---|
| 169 | |
|---|
| 170 | if self.get_num_controls() == 2: |
|---|
| 171 | # is first conversation_textview scrolled down ? |
|---|
| 172 | scrolled = False |
|---|
| 173 | first_widget = self.notebook.get_nth_page(0) |
|---|
| 174 | ctrl = self._widget_to_control(first_widget) |
|---|
| 175 | conv_textview = ctrl.conv_textview |
|---|
| 176 | if conv_textview.at_the_end(): |
|---|
| 177 | scrolled = True |
|---|
| 178 | self.notebook.set_show_tabs(True) |
|---|
| 179 | if scrolled: |
|---|
| 180 | gobject.idle_add(conv_textview.scroll_to_end_iter) |
|---|
| 181 | self.alignment.set_property('top-padding', 2) |
|---|
| 182 | |
|---|
| 183 | # Add notebook page and connect up to the tab's close button |
|---|
| 184 | xml = gtkgui_helpers.get_glade('message_window.glade', 'chat_tab_ebox') |
|---|
| 185 | tab_label_box = xml.get_widget('chat_tab_ebox') |
|---|
| 186 | widget = xml.get_widget('tab_close_button') |
|---|
| 187 | id = widget.connect('clicked', self._on_close_button_clicked, control) |
|---|
| 188 | control.handlers[id] = widget |
|---|
| 189 | |
|---|
| 190 | id = tab_label_box.connect('button-press-event', self.on_tab_eventbox_button_press_event, control.widget) |
|---|
| 191 | control.handlers[id] = tab_label_box |
|---|
| 192 | self.notebook.append_page(control.widget, tab_label_box) |
|---|
| 193 | |
|---|
| 194 | # If GTK+ version >= 2.10, use gtk native way to reorder tabs |
|---|
| 195 | if gtk.pygtk_version >= (2, 10, 0) and gtk.gtk_version >= (2, 10, 0): |
|---|
| 196 | self.notebook.set_tab_reorderable(control.widget, True) |
|---|
| 197 | else: |
|---|
| 198 | self.setup_tab_dnd(control.widget) |
|---|
| 199 | |
|---|
| 200 | self.redraw_tab(control) |
|---|
| 201 | self.window.show_all() |
|---|
| 202 | # NOTE: we do not call set_control_active(True) since we don't know whether |
|---|
| 203 | # the tab is the active one. |
|---|
| 204 | self.show_title() |
|---|
| 205 | |
|---|
| 206 | def on_tab_eventbox_button_press_event(self, widget, event, child): |
|---|
| 207 | if event.button == 3: # right click |
|---|
| 208 | n = self.notebook.page_num(child) |
|---|
| 209 | self.notebook.set_current_page(n) |
|---|
| 210 | self.popup_menu(event) |
|---|
| 211 | elif event.button == 2: # middle click |
|---|
| 212 | ctrl = self._widget_to_control(child) |
|---|
| 213 | self.remove_tab(ctrl, self.CLOSE_TAB_MIDDLE_CLICK) |
|---|
| 214 | |
|---|
| 215 | def _on_message_textview_mykeypress_event(self, widget, event_keyval, |
|---|
| 216 | event_keymod): |
|---|
| 217 | # NOTE: handles mykeypress which is custom signal; see message_textview.py |
|---|
| 218 | |
|---|
| 219 | # construct event instance from binding |
|---|
| 220 | event = gtk.gdk.Event(gtk.gdk.KEY_PRESS) # it's always a key-press here |
|---|
| 221 | event.keyval = event_keyval |
|---|
| 222 | event.state = event_keymod |
|---|
| 223 | event.time = 0 # assign current time |
|---|
| 224 | |
|---|
| 225 | if event.state & gtk.gdk.CONTROL_MASK: |
|---|
| 226 | # Tab switch bindings |
|---|
| 227 | if event.keyval == gtk.keysyms.Tab: # CTRL + TAB |
|---|
| 228 | self.move_to_next_unread_tab(True) |
|---|
| 229 | elif event.keyval == gtk.keysyms.ISO_Left_Tab: # CTRL + SHIFT + TAB |
|---|
| 230 | self.move_to_next_unread_tab(False) |
|---|
| 231 | elif event.keyval == gtk.keysyms.Page_Down: # CTRL + PAGE DOWN |
|---|
| 232 | self.notebook.emit('key_press_event', event) |
|---|
| 233 | elif event.keyval == gtk.keysyms.Page_Up: # CTRL + PAGE UP |
|---|
| 234 | self.notebook.emit('key_press_event', event) |
|---|
| 235 | |
|---|
| 236 | def _on_close_button_clicked(self, button, control): |
|---|
| 237 | '''When close button is pressed: close a tab''' |
|---|
| 238 | self.remove_tab(control, self.CLOSE_CLOSE_BUTTON) |
|---|
| 239 | |
|---|
| 240 | def show_title(self, urgent = True, control = None): |
|---|
| 241 | '''redraw the window's title''' |
|---|
| 242 | if not control: |
|---|
| 243 | control = self.get_active_control() |
|---|
| 244 | if not control: |
|---|
| 245 | # No more control in this window |
|---|
| 246 | return |
|---|
| 247 | unread = 0 |
|---|
| 248 | for ctrl in self.controls(): |
|---|
| 249 | if ctrl.type_id == message_control.TYPE_GC and not \ |
|---|
| 250 | gajim.config.get('notify_on_all_muc_messages') and not \ |
|---|
| 251 | ctrl.attention_flag: |
|---|
| 252 | # count only pm messages |
|---|
| 253 | unread += ctrl.get_nb_unread_pm() |
|---|
| 254 | continue |
|---|
| 255 | unread += ctrl.get_nb_unread() |
|---|
| 256 | |
|---|
| 257 | unread_str = '' |
|---|
| 258 | if unread > 1: |
|---|
| 259 | unread_str = '[' + unicode(unread) + '] ' |
|---|
| 260 | elif unread == 1: |
|---|
| 261 | unread_str = '* ' |
|---|
| 262 | else: |
|---|
| 263 | urgent = False |
|---|
| 264 | |
|---|
| 265 | if control.type_id == message_control.TYPE_GC: |
|---|
| 266 | name = control.room_jid.split('@')[0] |
|---|
| 267 | urgent = control.attention_flag |
|---|
| 268 | else: |
|---|
| 269 | name = control.contact.get_shown_name() |
|---|
| 270 | if control.resource: |
|---|
| 271 | name += '/' + control.resource |
|---|
| 272 | |
|---|
| 273 | window_mode = gajim.interface.msg_win_mgr.mode |
|---|
| 274 | |
|---|
| 275 | if self.get_num_controls() == 1: |
|---|
| 276 | label = name |
|---|
| 277 | elif window_mode == MessageWindowMgr.ONE_MSG_WINDOW_PERTYPE: |
|---|
| 278 | # Show the plural form since number of tabs > 1 |
|---|
| 279 | if self.type == 'chat': |
|---|
| 280 | label = _('Chats') |
|---|
| 281 | elif self.type == 'gc': |
|---|
| 282 | label = _('Group Chats') |
|---|
| 283 | else: |
|---|
| 284 | label = _('Private Chats') |
|---|
| 285 | else: |
|---|
| 286 | label = _('Messages') |
|---|
| 287 | title = _('%s - Gajim') % label |
|---|
| 288 | |
|---|
| 289 | if window_mode == MessageWindowMgr.ONE_MSG_WINDOW_PERACCT: |
|---|
| 290 | title = title + ": " + control.account |
|---|
| 291 | |
|---|
| 292 | self.window.set_title(unread_str + title) |
|---|
| 293 | |
|---|
| 294 | if urgent: |
|---|
| 295 | gtkgui_helpers.set_unset_urgency_hint(self.window, unread) |
|---|
| 296 | else: |
|---|
| 297 | gtkgui_helpers.set_unset_urgency_hint(self.window, False) |
|---|
| 298 | |
|---|
| 299 | def set_active_tab(self, jid, acct): |
|---|
| 300 | ctrl = self._controls[acct][jid] |
|---|
| 301 | ctrl_page = self.notebook.page_num(ctrl.widget) |
|---|
| 302 | self.notebook.set_current_page(ctrl_page) |
|---|
| 303 | |
|---|
| 304 | def remove_tab(self, ctrl, method, reason = None, force = False): |
|---|
| 305 | '''reason is only for gc (offline status message) |
|---|
| 306 | if force is True, do not ask any confirmation''' |
|---|
| 307 | # Shutdown the MessageControl |
|---|
| 308 | if not force and not ctrl.allow_shutdown(method): |
|---|
| 309 | return |
|---|
| 310 | if reason is not None: # We are leaving gc with a status message |
|---|
| 311 | ctrl.shutdown(reason) |
|---|
| 312 | else: # We are leaving gc without status message or it's a chat |
|---|
| 313 | ctrl.shutdown() |
|---|
| 314 | |
|---|
| 315 | # Update external state |
|---|
| 316 | gajim.events.remove_events(ctrl.account, ctrl.get_full_jid, |
|---|
| 317 | types = ['printed_msg', 'chat', 'gc_msg']) |
|---|
| 318 | del gajim.last_message_time[ctrl.account][ctrl.get_full_jid()] |
|---|
| 319 | |
|---|
| 320 | # Disconnect tab DnD only if GTK version < 2.10 |
|---|
| 321 | if gtk.pygtk_version < (2, 10, 0) or gtk.gtk_version < (2, 10, 0): |
|---|
| 322 | self.disconnect_tab_dnd(ctrl.widget) |
|---|
| 323 | |
|---|
| 324 | self.notebook.remove_page(self.notebook.page_num(ctrl.widget)) |
|---|
| 325 | |
|---|
| 326 | fjid = ctrl.get_full_jid() |
|---|
| 327 | del self._controls[ctrl.account][fjid] |
|---|
| 328 | if len(self._controls[ctrl.account]) == 0: |
|---|
| 329 | del self._controls[ctrl.account] |
|---|
| 330 | |
|---|
| 331 | if self.get_num_controls() == 0: |
|---|
| 332 | # These are not called when the window is destroyed like this, fake it |
|---|
| 333 | gajim.interface.msg_win_mgr._on_window_delete(self.window, None) |
|---|
| 334 | gajim.interface.msg_win_mgr._on_window_destroy(self.window) |
|---|
| 335 | # dnd clean up |
|---|
| 336 | self.notebook.disconnect(self.hid) |
|---|
| 337 | self.notebook.drag_dest_unset() |
|---|
| 338 | self.window.destroy() |
|---|
| 339 | return # don't show_title, we are dead |
|---|
| 340 | elif self.get_num_controls() == 1: # we are going from two tabs to one |
|---|
| 341 | show_tabs_if_one_tab = gajim.config.get('tabs_always_visible') |
|---|
| 342 | self.notebook.set_show_tabs(show_tabs_if_one_tab) |
|---|
| 343 | if not show_tabs_if_one_tab: |
|---|
| 344 | self.alignment.set_property('top-padding', 0) |
|---|
| 345 | self.show_title() |
|---|
| 346 | |
|---|
| 347 | |
|---|
| 348 | def redraw_tab(self, ctrl, chatstate = None): |
|---|
| 349 | hbox = self.notebook.get_tab_label(ctrl.widget).get_children()[0] |
|---|
| 350 | status_img = hbox.get_children()[0] |
|---|
| 351 | nick_label = hbox.get_children()[1] |
|---|
| 352 | |
|---|
| 353 | # Optionally hide close button |
|---|
| 354 | close_button = hbox.get_children()[2] |
|---|
| 355 | if gajim.config.get('tabs_close_button'): |
|---|
| 356 | close_button.show() |
|---|
| 357 | else: |
|---|
| 358 | close_button.hide() |
|---|
| 359 | |
|---|
| 360 | # Update nick |
|---|
| 361 | nick_label.set_max_width_chars(10) |
|---|
| 362 | (tab_label_str, tab_label_color) = ctrl.get_tab_label(chatstate) |
|---|
| 363 | nick_label.set_markup(tab_label_str) |
|---|
| 364 | if tab_label_color: |
|---|
| 365 | nick_label.modify_fg(gtk.STATE_NORMAL, tab_label_color) |
|---|
| 366 | nick_label.modify_fg(gtk.STATE_ACTIVE, tab_label_color) |
|---|
| 367 | |
|---|
| 368 | tab_img = ctrl.get_tab_image() |
|---|
| 369 | if tab_img: |
|---|
| 370 | if tab_img.get_storage_type() == gtk.IMAGE_ANIMATION: |
|---|
| 371 | status_img.set_from_animation(tab_img.get_animation()) |
|---|
| 372 | else: |
|---|
| 373 | status_img.set_from_pixbuf(tab_img.get_pixbuf()) |
|---|
| 374 | |
|---|
| 375 | def repaint_themed_widgets(self): |
|---|
| 376 | '''Repaint controls in the window with theme color''' |
|---|
| 377 | # iterate through controls and repaint |
|---|
| 378 | for ctrl in self.controls(): |
|---|
| 379 | ctrl.repaint_themed_widgets() |
|---|
| 380 | |
|---|
| 381 | def _widget_to_control(self, widget): |
|---|
| 382 | for ctrl in self.controls(): |
|---|
| 383 | if ctrl.widget == widget: |
|---|
| 384 | return ctrl |
|---|
| 385 | return None |
|---|
| 386 | |
|---|
| 387 | def get_active_control(self): |
|---|
| 388 | notebook = self.notebook |
|---|
| 389 | active_widget = notebook.get_nth_page(notebook.get_current_page()) |
|---|
| 390 | return self._widget_to_control(active_widget) |
|---|
| 391 | |
|---|
| 392 | def get_active_contact(self): |
|---|
| 393 | ctrl = self.get_active_control() |
|---|
| 394 | if ctrl: |
|---|
| 395 | return ctrl.contact |
|---|
| 396 | return None |
|---|
| 397 | |
|---|
| 398 | def get_active_jid(self): |
|---|
| 399 | contact = self.get_active_contact() |
|---|
| 400 | if contact: |
|---|
| 401 | return contact.jid |
|---|
| 402 | return None |
|---|
| 403 | |
|---|
| 404 | def is_active(self): |
|---|
| 405 | return self.window.is_active() |
|---|
| 406 | |
|---|
| 407 | def get_origin(self): |
|---|
| 408 | return self.window.window.get_origin() |
|---|
| 409 | |
|---|
| 410 | def toggle_emoticons(self): |
|---|
| 411 | for ctrl in self.controls(): |
|---|
| 412 | ctrl.toggle_emoticons() |
|---|
| 413 | |
|---|
| 414 | def update_font(self): |
|---|
| 415 | for ctrl in self.controls(): |
|---|
| 416 | ctrl.update_font() |
|---|
| 417 | |
|---|
| 418 | def update_tags(self): |
|---|
| 419 | for ctrl in self.controls(): |
|---|
| 420 | ctrl.update_tags() |
|---|
| 421 | |
|---|
| 422 | def get_control(self, key, acct): |
|---|
| 423 | '''Return the MessageControl for jid or n, where n is a notebook page index. |
|---|
| 424 | When key is an int index acct may be None''' |
|---|
| 425 | if isinstance(key, str): |
|---|
| 426 | key = unicode(key, 'utf-8') |
|---|
| 427 | |
|---|
| 428 | if isinstance(key, unicode): |
|---|
| 429 | jid = key |
|---|
| 430 | try: |
|---|
| 431 | return self._controls[acct][jid] |
|---|
| 432 | except: |
|---|
| 433 | return None |
|---|
| 434 | else: |
|---|
| 435 | page_num = key |
|---|
| 436 | notebook = self.notebook |
|---|
| 437 | if page_num == None: |
|---|
| 438 | page_num = notebook.get_current_page() |
|---|
| 439 | nth_child = notebook.get_nth_page(page_num) |
|---|
| 440 | return self._widget_to_control(nth_child) |
|---|
| 441 | |
|---|
| 442 | def controls(self): |
|---|
| 443 | for ctrl_dict in self._controls.values(): |
|---|
| 444 | for ctrl in ctrl_dict.values(): |
|---|
| 445 | yield ctrl |
|---|
| 446 | |
|---|
| 447 | def move_to_next_unread_tab(self, forward): |
|---|
| 448 | ind = self.notebook.get_current_page() |
|---|
| 449 | current = ind |
|---|
| 450 | found = False |
|---|
| 451 | first_composing_ind = -1 # id of first composing ctrl to switch to |
|---|
| 452 | # if no others controls have awaiting events |
|---|
| 453 | # loop until finding an unread tab or having done a complete cycle |
|---|
| 454 | while True: |
|---|
| 455 | if forward == True: # look for the first unread tab on the right |
|---|
| 456 | ind = ind + 1 |
|---|
| 457 | if ind >= self.notebook.get_n_pages(): |
|---|
| 458 | ind = 0 |
|---|
| 459 | else: # look for the first unread tab on the right |
|---|
| 460 | ind = ind - 1 |
|---|
| 461 | if ind < 0: |
|---|
| 462 | ind = self.notebook.get_n_pages() - 1 |
|---|
| 463 | ctrl = self.get_control(ind, None) |
|---|
| 464 | if ctrl.get_nb_unread() > 0: |
|---|
| 465 | found = True |
|---|
| 466 | break # found |
|---|
| 467 | elif gajim.config.get('ctrl_tab_go_to_next_composing') : # Search for a composing contact |
|---|
| 468 | contact = ctrl.contact |
|---|
| 469 | if first_composing_ind == -1 and contact.chatstate == 'composing': |
|---|
| 470 | # If no composing contact found yet, check if this one is composing |
|---|
| 471 | first_composing_ind = ind |
|---|
| 472 | if ind == current: |
|---|
| 473 | break # a complete cycle without finding an unread tab |
|---|
| 474 | if found: |
|---|
| 475 | self.notebook.set_current_page(ind) |
|---|
| 476 | elif first_composing_ind != -1: |
|---|
| 477 | self.notebook.set_current_page(first_composing_ind) |
|---|
| 478 | else: # not found and nobody composing |
|---|
| 479 | if forward: # CTRL + TAB |
|---|
| 480 | if current < (self.notebook.get_n_pages() - 1): |
|---|
| 481 | self.notebook.next_page() |
|---|
| 482 | else: # traverse for ever (eg. don't stop at last tab) |
|---|
| 483 | self.notebook.set_current_page(0) |
|---|
| 484 | else: # CTRL + SHIFT + TAB |
|---|
| 485 | if current > 0: |
|---|
| 486 | self.notebook.prev_page() |
|---|
| 487 | else: # traverse for ever (eg. don't stop at first tab) |
|---|
| 488 | self.notebook.set_current_page( |
|---|
| 489 | self.notebook.get_n_pages() - 1) |
|---|
| 490 | |
|---|
| 491 | def popup_menu(self, event): |
|---|
| 492 | menu = self.get_active_control().prepare_context_menu() |
|---|
| 493 | # show the menu |
|---|
| 494 | menu.popup(None, None, None, event.button, event.time) |
|---|
| 495 | menu.show_all() |
|---|
| 496 | |
|---|
| 497 | def _on_notebook_switch_page(self, notebook, page, page_num): |
|---|
| 498 | old_no = notebook.get_current_page() |
|---|
| 499 | if old_no >= 0: |
|---|
| 500 | old_ctrl = self._widget_to_control(notebook.get_nth_page(old_no)) |
|---|
| 501 | old_ctrl.set_control_active(False) |
|---|
| 502 | |
|---|
| 503 | new_ctrl = self._widget_to_control(notebook.get_nth_page(page_num)) |
|---|
| 504 | new_ctrl.set_control_active(True) |
|---|
| 505 | self.show_title(control = new_ctrl) |
|---|
| 506 | |
|---|
| 507 | def _on_notebook_key_press(self, widget, event): |
|---|
| 508 | st = '1234567890' # alt+1 means the first tab (tab 0) |
|---|
| 509 | ctrl = self.get_active_control() |
|---|
| 510 | |
|---|
| 511 | # CTRL mask |
|---|
| 512 | if event.state & gtk.gdk.CONTROL_MASK: |
|---|
| 513 | # Tab switch bindings |
|---|
| 514 | if event.keyval == gtk.keysyms.ISO_Left_Tab: # CTRL + SHIFT + TAB |
|---|
| 515 | self.move_to_next_unread_tab(False) |
|---|
| 516 | elif event.keyval == gtk.keysyms.Tab: # CTRL + TAB |
|---|
| 517 | self.move_to_next_unread_tab(True) |
|---|
| 518 | elif event.keyval == gtk.keysyms.F4: # CTRL + F4 |
|---|
| 519 | self.remove_tab(ctrl, self.CLOSE_CTRL_KEY) |
|---|
| 520 | elif event.keyval == gtk.keysyms.w: # CTRL + W |
|---|
| 521 | self.remove_tab(ctrl, self.CLOSE_CTRL_KEY) |
|---|
| 522 | |
|---|
| 523 | # MOD1 (ALT) mask |
|---|
| 524 | elif event.state & gtk.gdk.MOD1_MASK: |
|---|
| 525 | # Tab switch bindings |
|---|
| 526 | if event.keyval == gtk.keysyms.Right: # ALT + RIGHT |
|---|
| 527 | new = self.notebook.get_current_page() + 1 |
|---|
| 528 | if new >= self.notebook.get_n_pages(): |
|---|
| 529 | new = 0 |
|---|
| 530 | self.notebook.set_current_page(new) |
|---|
| 531 | elif event.keyval == gtk.keysyms.Left: # ALT + LEFT |
|---|
| 532 | new = self.notebook.get_current_page() - 1 |
|---|
| 533 | if new < 0: |
|---|
| 534 | new = self.notebook.get_n_pages() - 1 |
|---|
| 535 | self.notebook.set_current_page(new) |
|---|
| 536 | elif event.string and event.string in st and \ |
|---|
| 537 | (event.state & gtk.gdk.MOD1_MASK): # ALT + 1,2,3.. |
|---|
| 538 | self.notebook.set_current_page(st.index(event.string)) |
|---|
| 539 | elif event.keyval == gtk.keysyms.c: # ALT + C toggles chat buttons |
|---|
| 540 | ctrl.chat_buttons_set_visible(not ctrl.hide_chat_buttons_current) |
|---|
| 541 | # Close tab bindings |
|---|
| 542 | elif event.keyval == gtk.keysyms.Escape and \ |
|---|
| 543 | gajim.config.get('escape_key_closes'): # Escape |
|---|
| 544 | self.remove_tab(ctrl, self.CLOSE_ESC) |
|---|
| 545 | else: |
|---|
| 546 | # If the active control has a message_textview pass the event to it |
|---|
| 547 | active_ctrl = self.get_active_control() |
|---|
| 548 | if isinstance(active_ctrl, ChatControlBase): |
|---|
| 549 | active_ctrl.msg_textview.emit('key_press_event', event) |
|---|
| 550 | active_ctrl.msg_textview.grab_focus() |
|---|
| 551 | |
|---|
| 552 | def setup_tab_dnd(self, child): |
|---|
| 553 | '''Set tab label as drag source and connect the drag_data_get signal''' |
|---|
| 554 | tab_label = self.notebook.get_tab_label(child) |
|---|
| 555 | tab_label.dnd_handler = tab_label.connect('drag_data_get', |
|---|
| 556 | self.on_tab_label_drag_data_get_cb) |
|---|
| 557 | self.handlers[tab_label.dnd_handler] = tab_label |
|---|
| 558 | tab_label.drag_source_set(gtk.gdk.BUTTON1_MASK, self.DND_TARGETS, |
|---|
| 559 | gtk.gdk.ACTION_MOVE) |
|---|
| 560 | tab_label.page_num = self.notebook.page_num(child) |
|---|
| 561 | |
|---|
| 562 | def on_tab_label_drag_data_get_cb(self, widget, drag_context, selection, |
|---|
| 563 | info, time): |
|---|
| 564 | source_page_num = self.find_page_num_according_to_tab_label(widget) |
|---|
| 565 | # 8 is the data size for the string |
|---|
| 566 | selection.set(selection.target, 8, str(source_page_num)) |
|---|
| 567 | |
|---|
| 568 | def on_tab_label_drag_data_received_cb(self, widget, drag_context, x, y, |
|---|
| 569 | selection, type, time): |
|---|
| 570 | '''Reorder the tabs according to the drop position''' |
|---|
| 571 | source_page_num = int(selection.data) |
|---|
| 572 | dest_page_num, to_right = self.get_tab_at_xy(x, y) |
|---|
| 573 | source_child = self.notebook.get_nth_page(source_page_num) |
|---|
| 574 | if dest_page_num != source_page_num: |
|---|
| 575 | self.notebook.reorder_child(source_child, dest_page_num) |
|---|
| 576 | |
|---|
| 577 | def get_tab_at_xy(self, x, y): |
|---|
| 578 | '''Thanks to Gaim |
|---|
| 579 | Return the tab under xy and |
|---|
| 580 | if its nearer from left or right side of the tab |
|---|
| 581 | ''' |
|---|
| 582 | page_num = -1 |
|---|
| 583 | to_right = False |
|---|
| 584 | horiz = self.notebook.get_tab_pos() == gtk.POS_TOP or \ |
|---|
| 585 | self.notebook.get_tab_pos() == gtk.POS_BOTTOM |
|---|
| 586 | for i in xrange(self.notebook.get_n_pages()): |
|---|
| 587 | page = self.notebook.get_nth_page(i) |
|---|
| 588 | tab = self.notebook.get_tab_label(page) |
|---|
| 589 | tab_alloc = tab.get_allocation() |
|---|
| 590 | if horiz: |
|---|
| 591 | if (x >= tab_alloc.x) and \ |
|---|
| 592 | (x <= (tab_alloc.x + tab_alloc.width)): |
|---|
| 593 | page_num = i |
|---|
| 594 | if x >= tab_alloc.x + (tab_alloc |
|---|