| 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | |
|---|
| 14 | |
|---|
| 15 | |
|---|
| 16 | |
|---|
| 17 | |
|---|
| 18 | |
|---|
| 19 | |
|---|
| 20 | |
|---|
| 21 | |
|---|
| 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 | |
|---|
| 41 | DND_TARGETS = [('GAJIM_TAB', 0, 81)] |
|---|
| 42 | hid = 0 |
|---|
| 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 | |
|---|
| 53 | self._controls = {} |
|---|
| 54 | |
|---|
| 55 | self.account = acct |
|---|
| 56 | |
|---|
| 57 | self.type = type |
|---|
| 58 | |
|---|
| 59 | |
|---|
| 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 | |
|---|
| 73 | |
|---|
| 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 | |
|---|
| 86 | while self.notebook.get_n_pages(): |
|---|
| 87 | self.notebook.remove_page(0) |
|---|
| 88 | |
|---|
| 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 | |
|---|
| 107 | self.hid = self.notebook.connect('drag_data_received', |
|---|
| 108 | self.on_tab_label_drag_data_received_cb) |
|---|
| 109 | self.handlers[self.hid] = self.notebook |
|---|
| 110 | |
|---|
| 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 | |
|---|
| 132 | |
|---|
| 133 | |
|---|
| 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 | |
|---|
| 140 | if ctrl.type_id == message_control.TYPE_GC: |
|---|
| 141 | self.redraw_tab(ctrl, 'active') |
|---|
| 142 | else: |
|---|
| 143 | |
|---|
| 144 | |
|---|
| 145 | self.redraw_tab(ctrl) |
|---|
| 146 | |
|---|
| 147 | def _on_window_delete(self, win, event): |
|---|
| 148 | |
|---|
| 149 | for ctrl in self.controls(): |
|---|
| 150 | if not ctrl.allow_shutdown(self.CLOSE_CLOSE_BUTTON): |
|---|
| 151 | return True |
|---|
| 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 | |
|---|
| 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 | |
|---|
| 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 | self.setup_tab_dnd(control.widget) |
|---|
| 195 | |
|---|
| 196 | self.redraw_tab(control) |
|---|
| 197 | self.window.show_all() |
|---|
| 198 | |
|---|
| 199 | |
|---|
| 200 | self.show_title() |
|---|
| 201 | |
|---|
| 202 | def on_tab_eventbox_button_press_event(self, widget, event, child): |
|---|
| 203 | if event.button == 3: |
|---|
| 204 | n = self.notebook.page_num(child) |
|---|
| 205 | self.notebook.set_current_page(n) |
|---|
| 206 | self.popup_menu(event) |
|---|
| 207 | elif event.button == 2: |
|---|
| 208 | ctrl = self._widget_to_control(child) |
|---|
| 209 | self.remove_tab(ctrl, self.CLOSE_TAB_MIDDLE_CLICK) |
|---|
| 210 | |
|---|
| 211 | def _on_message_textview_mykeypress_event(self, widget, event_keyval, |
|---|
| 212 | event_keymod): |
|---|
| 213 | |
|---|
| 214 | |
|---|
| 215 | |
|---|
| 216 | event = gtk.gdk.Event(gtk.gdk.KEY_PRESS) |
|---|
| 217 | event.keyval = event_keyval |
|---|
| 218 | event.state = event_keymod |
|---|
| 219 | event.time = 0 |
|---|
| 220 | |
|---|
| 221 | if event.state & gtk.gdk.CONTROL_MASK: |
|---|
| 222 | |
|---|
| 223 | if event.keyval == gtk.keysyms.Tab: |
|---|
| 224 | self.move_to_next_unread_tab(True) |
|---|
| 225 | elif event.keyval == gtk.keysyms.ISO_Left_Tab: |
|---|
| 226 | self.move_to_next_unread_tab(False) |
|---|
| 227 | elif event.keyval == gtk.keysyms.Page_Down: |
|---|
| 228 | self.notebook.emit('key_press_event', event) |
|---|
| 229 | elif event.keyval == gtk.keysyms.Page_Up: |
|---|
| 230 | self.notebook.emit('key_press_event', event) |
|---|
| 231 | |
|---|
| 232 | def _on_close_button_clicked(self, button, control): |
|---|
| 233 | '''When close button is pressed: close a tab''' |
|---|
| 234 | self.remove_tab(control, self.CLOSE_CLOSE_BUTTON) |
|---|
| 235 | |
|---|
| 236 | def show_title(self, urgent = True, control = None): |
|---|
| 237 | '''redraw the window's title''' |
|---|
| 238 | unread = 0 |
|---|
| 239 | for ctrl in self.controls(): |
|---|
| 240 | if ctrl.type_id == message_control.TYPE_GC and not \ |
|---|
| 241 | gajim.config.get('notify_on_all_muc_messages') and not \ |
|---|
| 242 | ctrl.attention_flag: |
|---|
| 243 | |
|---|
| 244 | unread += ctrl.get_nb_unread_pm() |
|---|
| 245 | continue |
|---|
| 246 | unread += ctrl.get_nb_unread() |
|---|
| 247 | |
|---|
| 248 | unread_str = '' |
|---|
| 249 | if unread > 1: |
|---|
| 250 | unread_str = '[' + unicode(unread) + '] ' |
|---|
| 251 | elif unread == 1: |
|---|
| 252 | unread_str = '* ' |
|---|
| 253 | else: |
|---|
| 254 | urgent = False |
|---|
| 255 | |
|---|
| 256 | if not control: |
|---|
| 257 | control = self.get_active_control() |
|---|
| 258 | if control.type_id == message_control.TYPE_GC: |
|---|
| 259 | name = control.room_jid.split('@')[0] |
|---|
| 260 | urgent = control.attention_flag |
|---|
| 261 | else: |
|---|
| 262 | name = control.contact.get_shown_name() |
|---|
| 263 | if control.resource: |
|---|
| 264 | name += '/' + control.resource |
|---|
| 265 | |
|---|
| 266 | window_mode = gajim.interface.msg_win_mgr.mode |
|---|
| 267 | |
|---|
| 268 | if self.get_num_controls() == 1: |
|---|
| 269 | label = name |
|---|
| 270 | elif window_mode == MessageWindowMgr.ONE_MSG_WINDOW_PERTYPE: |
|---|
| 271 | |
|---|
| 272 | if self.type == 'chat': |
|---|
| 273 | label = _('Chats') |
|---|
| 274 | elif self.type == 'gc': |
|---|
| 275 | label = _('Group Chats') |
|---|
| 276 | else: |
|---|
| 277 | label = _('Private Chats') |
|---|
| 278 | else: |
|---|
| 279 | label = _('Messages') |
|---|
| 280 | title = _('%s - Gajim') % label |
|---|
| 281 | |
|---|
| 282 | if window_mode == MessageWindowMgr.ONE_MSG_WINDOW_PERACCT: |
|---|
| 283 | title = title + ": " + control.account |
|---|
| 284 | |
|---|
| 285 | self.window.set_title(unread_str + title) |
|---|
| 286 | |
|---|
| 287 | if urgent: |
|---|
| 288 | gtkgui_helpers.set_unset_urgency_hint(self.window, unread) |
|---|
| 289 | else: |
|---|
| 290 | gtkgui_helpers.set_unset_urgency_hint(self.window, False) |
|---|
| 291 | |
|---|
| 292 | def set_active_tab(self, jid, acct): |
|---|
| 293 | ctrl = self._controls[acct][jid] |
|---|
| 294 | ctrl_page = self.notebook.page_num(ctrl.widget) |
|---|
| 295 | self.notebook.set_current_page(ctrl_page) |
|---|
| 296 | |
|---|
| 297 | def remove_tab(self, ctrl, method, reason = None, force = False): |
|---|
| 298 | '''reason is only for gc (offline status message) |
|---|
| 299 | if force is True, do not ask any confirmation''' |
|---|
| 300 | |
|---|
| 301 | if not force and not ctrl.allow_shutdown(method): |
|---|
| 302 | return |
|---|
| 303 | if reason is not None: |
|---|
| 304 | ctrl.shutdown(reason) |
|---|
| 305 | else: |
|---|
| 306 | ctrl.shutdown() |
|---|
| 307 | |
|---|
| 308 | |
|---|
| 309 | gajim.events.remove_events(ctrl.account, ctrl.get_full_jid, |
|---|
| 310 | types = ['printed_msg', 'chat', 'gc_msg']) |
|---|
| 311 | del gajim.last_message_time[ctrl.account][ctrl.get_full_jid()] |
|---|
| 312 | |
|---|
| 313 | self.disconnect_tab_dnd(ctrl.widget) |
|---|
| 314 | self.notebook.remove_page(self.notebook.page_num(ctrl.widget)) |
|---|
| 315 | |
|---|
| 316 | fjid = ctrl.get_full_jid() |
|---|
| 317 | del self._controls[ctrl.account][fjid] |
|---|
| 318 | if len(self._controls[ctrl.account]) == 0: |
|---|
| 319 | del self._controls[ctrl.account] |
|---|
| 320 | |
|---|
| 321 | if self.get_num_controls() == 0: |
|---|
| 322 | |
|---|
| 323 | gajim.interface.msg_win_mgr._on_window_delete(self.window, None) |
|---|
| 324 | gajim.interface.msg_win_mgr._on_window_destroy(self.window) |
|---|
| 325 | |
|---|
| 326 | self.notebook.disconnect(self.hid) |
|---|
| 327 | self.notebook.drag_dest_unset() |
|---|
| 328 | self.window.destroy() |
|---|
| 329 | return |
|---|
| 330 | elif self.get_num_controls() == 1: |
|---|
| 331 | show_tabs_if_one_tab = gajim.config.get('tabs_always_visible') |
|---|
| 332 | self.notebook.set_show_tabs(show_tabs_if_one_tab) |
|---|
| 333 | if not show_tabs_if_one_tab: |
|---|
| 334 | self.alignment.set_property('top-padding', 0) |
|---|
| 335 | self.show_title() |
|---|
| 336 | |
|---|
| 337 | |
|---|
| 338 | def redraw_tab(self, ctrl, chatstate = None): |
|---|
| 339 | hbox = self.notebook.get_tab_label(ctrl.widget).get_children()[0] |
|---|
| 340 | status_img = hbox.get_children()[0] |
|---|
| 341 | nick_label = hbox.get_children()[1] |
|---|
| 342 | |
|---|
| 343 | |
|---|
| 344 | close_button = hbox.get_children()[2] |
|---|
| 345 | if gajim.config.get('tabs_close_button'): |
|---|
| 346 | close_button.show() |
|---|
| 347 | else: |
|---|
| 348 | close_button.hide() |
|---|
| 349 | |
|---|
| 350 | |
|---|
| 351 | nick_label.set_max_width_chars(10) |
|---|
| 352 | (tab_label_str, tab_label_color) = ctrl.get_tab_label(chatstate) |
|---|
| 353 | nick_label.set_markup(tab_label_str) |
|---|
| 354 | if tab_label_color: |
|---|
| 355 | nick_label.modify_fg(gtk.STATE_NORMAL, tab_label_color) |
|---|
| 356 | nick_label.modify_fg(gtk.STATE_ACTIVE, tab_label_color) |
|---|
| 357 | |
|---|
| 358 | tab_img = ctrl.get_tab_image() |
|---|
| 359 | if tab_img: |
|---|
| 360 | if tab_img.get_storage_type() == gtk.IMAGE_ANIMATION: |
|---|
| 361 | status_img.set_from_animation(tab_img.get_animation()) |
|---|
| 362 | else: |
|---|
| 363 | status_img.set_from_pixbuf(tab_img.get_pixbuf()) |
|---|
| 364 | |
|---|
| 365 | def repaint_themed_widgets(self): |
|---|
| 366 | '''Repaint controls in the window with theme color''' |
|---|
| 367 | |
|---|
| 368 | for ctrl in self.controls(): |
|---|
| 369 | ctrl.repaint_themed_widgets() |
|---|
| 370 | |
|---|
| 371 | def _widget_to_control(self, widget): |
|---|
| 372 | for ctrl in self.controls(): |
|---|
| 373 | if ctrl.widget == widget: |
|---|
| 374 | return ctrl |
|---|
| 375 | return None |
|---|
| 376 | |
|---|
| 377 | def get_active_control(self): |
|---|
| 378 | notebook = self.notebook |
|---|
| 379 | active_widget = notebook.get_nth_page(notebook.get_current_page()) |
|---|
| 380 | return self._widget_to_control(active_widget) |
|---|
| 381 | |
|---|
| 382 | def get_active_contact(self): |
|---|
| 383 | ctrl = self.get_active_control() |
|---|
| 384 | if ctrl: |
|---|
| 385 | return ctrl.contact |
|---|
| 386 | return None |
|---|
| 387 | |
|---|
| 388 | def get_active_jid(self): |
|---|
| 389 | contact = self.get_active_contact() |
|---|
| 390 | if contact: |
|---|
| 391 | return contact.jid |
|---|
| 392 | return None |
|---|
| 393 | |
|---|
| 394 | def is_active(self): |
|---|
| 395 | return self.window.is_active() |
|---|
| 396 | |
|---|
| 397 | def get_origin(self): |
|---|
| 398 | return self.window.window.get_origin() |
|---|
| 399 | |
|---|
| 400 | def toggle_emoticons(self): |
|---|
| 401 | for ctrl in self.controls(): |
|---|
| 402 | ctrl.toggle_emoticons() |
|---|
| 403 | |
|---|
| 404 | def update_font(self): |
|---|
| 405 | for ctrl in self.controls(): |
|---|
| 406 | ctrl.update_font() |
|---|
| 407 | |
|---|
| 408 | def update_tags(self): |
|---|
| 409 | for ctrl in self.controls(): |
|---|
| 410 | ctrl.update_tags() |
|---|
| 411 | |
|---|
| 412 | def get_control(self, key, acct): |
|---|
| 413 | '''Return the MessageControl for jid or n, where n is a notebook page index. |
|---|
| 414 | When key is an int index acct may be None''' |
|---|
| 415 | if isinstance(key, str): |
|---|
| 416 | key = unicode(key, 'utf-8') |
|---|
| 417 | |
|---|
| 418 | if isinstance(key, unicode): |
|---|
| 419 | jid = key |
|---|
| 420 | try: |
|---|
| 421 | return self._controls[acct][jid] |
|---|
| 422 | except: |
|---|
| 423 | return None |
|---|
| 424 | else: |
|---|
| 425 | page_num = key |
|---|
| 426 | notebook = self.notebook |
|---|
| 427 | if page_num == None: |
|---|
| 428 | page_num = notebook.get_current_page() |
|---|
| 429 | nth_child = notebook.get_nth_page(page_num) |
|---|
| 430 | return self._widget_to_control(nth_child) |
|---|
| 431 | |
|---|
| 432 | def controls(self): |
|---|
| 433 | for ctrl_dict in self._controls.values(): |
|---|
| 434 | for ctrl in ctrl_dict.values(): |
|---|
| 435 | yield ctrl |
|---|
| 436 | |
|---|
| 437 | def move_to_next_unread_tab(self, forward): |
|---|
| 438 | ind = self.notebook.get_current_page() |
|---|
| 439 | current = ind |
|---|
| 440 | found = False |
|---|
| 441 | first_composing_ind = -1 |
|---|
| 442 | |
|---|
| 443 | |
|---|
| 444 | while True: |
|---|
| 445 | if forward == True: |
|---|
| 446 | ind = ind + 1 |
|---|
| 447 | if ind >= self.notebook.get_n_pages(): |
|---|
| 448 | ind = 0 |
|---|
| 449 | else: |
|---|
| 450 | ind = ind - 1 |
|---|
| 451 | if ind < 0: |
|---|
| 452 | ind = self.notebook.get_n_pages() - 1 |
|---|
| 453 | ctrl = self.get_control(ind, None) |
|---|
| 454 | if ctrl.get_nb_unread() > 0: |
|---|
| 455 | found = True |
|---|
| 456 | break |
|---|
| 457 | elif gajim.config.get('ctrl_tab_go_to_next_composing') : |
|---|
| 458 | contact = ctrl.contact |
|---|
| 459 | if first_composing_ind == -1 and contact.chatstate == 'composing': |
|---|
| 460 | |
|---|
| 461 | first_composing_ind = ind |
|---|
| 462 | if ind == current: |
|---|
| 463 | break |
|---|
| 464 | if found: |
|---|
| 465 | self.notebook.set_current_page(ind) |
|---|
| 466 | elif first_composing_ind != -1: |
|---|
| 467 | self.notebook.set_current_page(first_composing_ind) |
|---|
| 468 | else: |
|---|
| 469 | if forward: |
|---|
| 470 | if current < (self.notebook.get_n_pages() - 1): |
|---|
| 471 | self.notebook.next_page() |
|---|
| 472 | else: |
|---|
| 473 | self.notebook.set_current_page(0) |
|---|
| 474 | else: |
|---|
| 475 | if current > 0: |
|---|
| 476 | self.notebook.prev_page() |
|---|
| 477 | else: |
|---|
| 478 | self.notebook.set_current_page( |
|---|
| 479 | self.notebook.get_n_pages() - 1) |
|---|
| 480 | |
|---|
| 481 | def popup_menu(self, event): |
|---|
| 482 | menu = self.get_active_control().prepare_context_menu() |
|---|
| 483 | |
|---|
| 484 | menu.popup(None, None, None, event.button, event.time) |
|---|
| 485 | menu.show_all() |
|---|
| 486 | |
|---|
| 487 | def _on_notebook_switch_page(self, notebook, page, page_num): |
|---|
| 488 | old_no = notebook.get_current_page() |
|---|
| 489 | if old_no >= 0: |
|---|
| 490 | old_ctrl = self._widget_to_control(notebook.get_nth_page(old_no)) |
|---|
| 491 | old_ctrl.set_control_active(False) |
|---|
| 492 | |
|---|
| 493 | new_ctrl = self._widget_to_control(notebook.get_nth_page(page_num)) |
|---|
| 494 | new_ctrl.set_control_active(True) |
|---|
| 495 | self.show_title(control = new_ctrl) |
|---|
| 496 | |
|---|
| 497 | def _on_notebook_key_press(self, widget, event): |
|---|
| 498 | st = '1234567890' |
|---|
| 499 | ctrl = self.get_active_control() |
|---|
| 500 | |
|---|
| 501 | |
|---|
| 502 | if event.state & gtk.gdk.CONTROL_MASK: |
|---|
| 503 | |
|---|
| 504 | if event.keyval == gtk.keysyms.ISO_Left_Tab: |
|---|
| 505 | self.move_to_next_unread_tab(False) |
|---|
| 506 | elif event.keyval == gtk.keysyms.Tab: |
|---|
| 507 | self.move_to_next_unread_tab(True) |
|---|
| 508 | elif event.keyval == gtk.keysyms.F4: |
|---|
| 509 | self.remove_tab(ctrl, self.CLOSE_CTRL_KEY) |
|---|
| 510 | elif event.keyval == gtk.keysyms.w: |
|---|
| 511 | self.remove_tab(ctrl, self.CLOSE_CTRL_KEY) |
|---|
| 512 | |
|---|
| 513 | |
|---|
| 514 | elif event.state & gtk.gdk.MOD1_MASK: |
|---|
| 515 | |
|---|
| 516 | if event.keyval == gtk.keysyms.Right: |
|---|
| 517 | new = self.notebook.get_current_page() + 1 |
|---|
| 518 | if new >= self.notebook.get_n_pages(): |
|---|
| 519 | new = 0 |
|---|
| 520 | self.notebook.set_current_page(new) |
|---|
| 521 | elif event.keyval == gtk.keysyms.Left: |
|---|
| 522 | new = self.notebook.get_current_page() - 1 |
|---|
| 523 | if new < 0: |
|---|
| 524 | new = self.notebook.get_n_pages() - 1 |
|---|
| 525 | self.notebook.set_current_page(new) |
|---|
| 526 | elif event.string and event.string in st and \ |
|---|
| 527 | (event.state & gtk.gdk.MOD1_MASK): |
|---|
| 528 | self.notebook.set_current_page(st.index(event.string)) |
|---|
| 529 | elif event.keyval == gtk.keysyms.c: |
|---|
| 530 | ctrl.chat_buttons_set_visible(not ctrl.hide_chat_buttons_current) |
|---|
| 531 | |
|---|
| 532 | elif event.keyval == gtk.keysyms.Escape and \ |
|---|
| 533 | gajim.config.get('escape_key_closes'): |
|---|
| 534 | self.remove_tab(ctrl, self.CLOSE_ESC) |
|---|
| 535 | else: |
|---|
| 536 | |
|---|
| 537 | active_ctrl = self.get_active_control() |
|---|
| 538 | if isinstance(active_ctrl, ChatControlBase): |
|---|
| 539 | active_ctrl.msg_textview.emit('key_press_event', event) |
|---|
| 540 | active_ctrl.msg_textview.grab_focus() |
|---|
| 541 | |
|---|
| 542 | def setup_tab_dnd(self, child): |
|---|
| 543 | '''Set tab label as drag source and connect the drag_data_get signal''' |
|---|
| 544 | tab_label = self.notebook.get_tab_label(child) |
|---|
| 545 | tab_label.dnd_handler = tab_label.connect('drag_data_get', |
|---|
| 546 | self.on_tab_label_drag_data_get_cb) |
|---|
| 547 | self.handlers[tab_label.dnd_handler] = tab_label |
|---|
| 548 | tab_label.drag_source_set(gtk.gdk.BUTTON1_MASK, self.DND_TARGETS, |
|---|
| 549 | gtk.gdk.ACTION_MOVE) |
|---|
| 550 | tab_label.page_num = self.notebook.page_num(child) |
|---|
| 551 | |
|---|
| 552 | def on_tab_label_drag_data_get_cb(self, widget, drag_context, selection, |
|---|
| 553 | info, time): |
|---|
| 554 | source_page_num = self.find_page_num_according_to_tab_label(widget) |
|---|
| 555 | |
|---|
| 556 | selection.set(selection.target, 8, str(source_page_num)) |
|---|
| 557 | |
|---|
| 558 | def on_tab_label_drag_data_received_cb(self, widget, drag_context, x, y, |
|---|
| 559 | selection, type, time): |
|---|
| 560 | '''Reorder the tabs according to the drop position''' |
|---|
| 561 | source_page_num = int(selection.data) |
|---|
| 562 | dest_page_num, to_right = self.get_tab_at_xy(x, y) |
|---|
| 563 | source_child = self.notebook.get_nth_page(source_page_num) |
|---|
| 564 | if dest_page_num != source_page_num: |
|---|
| 565 | self.notebook.reorder_child(source_child, dest_page_num) |
|---|
| 566 | |
|---|
| 567 | def get_tab_at_xy(self, x, y): |
|---|
| 568 | '''Thanks to Gaim |
|---|
| 569 | Return the tab under xy and |
|---|
| 570 | if its nearer from left or right side of the tab |
|---|
| 571 | ''' |
|---|
| 572 | page_num = -1 |
|---|
| 573 | to_right = False |
|---|
| 574 | horiz = self.notebook.get_tab_pos() == gtk.POS_TOP or \ |
|---|
| 575 | self.notebook.get_tab_pos() == gtk.POS_BOTTOM |
|---|
| 576 | for i in xrange(self.notebook.get_n_pages()): |
|---|
| 577 | page = self.notebook.get_nth_page(i) |
|---|
| 578 | tab = self.notebook.get_tab_label(page) |
|---|
| 579 | tab_alloc = tab.get_allocation() |
|---|
| 580 | if horiz: |
|---|
| 581 | if (x >= tab_alloc.x) and \ |
|---|
| 582 | (x <= (tab_alloc.x + tab_alloc.width)): |
|---|
| 583 | page_num = i |
|---|
| 584 | if x >= tab_alloc.x + (tab_alloc.width / 2.0): |
|---|
| 585 | to_right = True |
|---|
| 586 | break |
|---|
| 587 | else: |
|---|
| 588 | if (y >= tab_alloc.y) and \ |
|---|
| 589 | (y <= (tab_alloc.y + tab_alloc.height)): |
|---|
| 590 | page_num = i |
|---|
| 591 | |
|---|
| 592 | if y > tab_alloc.y + (tab_alloc.height / 2.0): |
|---|
| 593 | to_right = True |
|---|
| 594 | break |
|---|
| 595 | return (page_num, to_right) |
|---|
| 596 | |
|---|
| 597 | def find_page_num_according_to_tab_label(self, tab_label): |
|---|
| 598 | '''Find the page num of the tab label''' |
|---|
| 599 | page_num = -1 |
|---|
| 600 | for i in xrange(self.notebook.get_n_pages()): |
|---|
| 601 | page = self.notebook.get_nth_page(i) |
|---|
| 602 | tab = self.notebook.get_tab_label(page) |
|---|
| 603 | if tab == tab_label: |
|---|
| 604 | page_num = i |
|---|
| 605 | break |
|---|
| 606 | return page_num |
|---|
| 607 | |
|---|
| 608 | def disconnect_tab_dnd(self, child): |
|---|
| 609 | '''Clean up DnD signals, source and dest''' |
|---|
| 610 | tab_label = self.notebook.get_tab_label(child) |
|---|
| 611 | tab_label.drag_source_unset() |
|---|
| 612 | tab_label.disconnect(tab_label.dnd_handler) |
|---|
| 613 | |
|---|
| 614 | |
|---|
| 615 | class MessageWindowMgr: |
|---|
| 616 | '''A manager and factory for MessageWindow objects''' |
|---|
| 617 | |
|---|
| 618 | |
|---|
| 619 | ( |
|---|
| 620 | ONE_MSG_WINDOW_NEVER, |
|---|
| 621 | ONE_MSG_WINDOW_ALWAYS, |
|---|
| 622 | ONE_MSG_WINDOW_PERACCT, |
|---|
| 623 | ONE_MSG_WINDOW_PERTYPE |
|---|
| 624 | ) = range(4) |
|---|
| 625 | |
|---|
| 626 | MAIN_WIN = 'main' |
|---|
| 627 | |
|---|
| 628 | def __init__(self): |
|---|
| 629 | ''' A dictionary of windows; the key depends on the config: |
|---|
| 630 | ONE_MSG_WINDOW_NEVER: The key is the contact JID |
|---|
| 631 | ONE_MSG_WINDOW_ALWAYS: The key is MessageWindowMgr.MAIN_WIN |
|---|
| 632 | ONE_MSG_WINDOW_PERACCT: The key is the account name |
|---|
| 633 | ONE_MSG_WINDOW_PERTYPE: The key is a message type constant''' |
|---|
| 634 | self._windows = {} |
|---|
| 635 | |
|---|
| 636 | mode = gajim.config.get('one_message_window') |
|---|
| 637 | self.mode = common.config.opt_one_window_types.index(mode) |
|---|
| 638 | |
|---|
| 639 | def change_account_name(self, old_name, new_name): |
|---|
| 640 | for win in self.windows(): |
|---|
| 641 | win.change_account_name(old_name, new_name) |
|---|
| 642 | |
|---|
| 643 | def _new_window(self, acct, type): |
|---|
| 644 | win = MessageWindow(acct, type) |
|---|
| 645 | |
|---|
| 646 | win.window.connect('delete-event', self._on_window_delete) |
|---|
| 647 | win.window.connect('destroy', self._on_window_destroy) |
|---|
| 648 | return win |
|---|
| 649 | |
|---|
| 650 | def _gtk_win_to_msg_win(self, gtk_win): |
|---|
| 651 | for w in self.windows(): |
|---|
| 652 | if w.window == gtk_win: |
|---|
| 653 | return w |
|---|
| 654 | return None |
|---|
| 655 | |
|---|
| 656 | def get_window(self, jid, acct): |
|---|
| 657 | for win in self.windows(): |
|---|
| 658 | if win.get_control(jid, acct): |
|---|
| 659 | return win |
|---|
| 660 | return None |
|---|
| 661 | |
|---|
| 662 | def has_window(self, jid, acct): |
|---|
| 663 | return self.get_window(jid, acct) != None |
|---|
| 664 | |
|---|
| 665 | def one_window_opened(self, contact, acct, type): |
|---|
| 666 | try: |
|---|
| 667 | return self._windows[self._mode_to_key(contact, acct, type)] != None |
|---|
| 668 | except KeyError: |
|---|
| 669 | return False |
|---|
| 670 | |
|---|
| 671 | def _resize_window(self, win, acct, type): |
|---|
| 672 | '''Resizes window according to config settings''' |
|---|
| 673 | if not gajim.config.get('saveposition'): |
|---|
| 674 | return |
|---|
| 675 | |
|---|
| 676 | if self.mode == self.ONE_MSG_WINDOW_ALWAYS: |
|---|
| 677 | size = (gajim.config.get('msgwin-width'), |
|---|
| 678 | gajim.config.get('msgwin-height')) |
|---|
| 679 | elif self.mode == self.ONE_MSG_WINDOW_PERACCT: |
|---|
| 680 | size = (gajim.config.get_per('accounts', acct, 'msgwin-width'), |
|---|
| 681 | gajim.config.get_per('accounts', acct, 'msgwin-height')) |
|---|
| 682 | elif self.mode in (self.ONE_MSG_WINDOW_NEVER, self.ONE_MSG_WINDOW_PERTYPE): |
|---|
| 683 | if type == message_control.TYPE_PM: |
|---|
| 684 | type = message_control.TYPE_CHAT |
|---|
| 685 | opt_width = type + '-msgwin-width' |
|---|
| 686 | opt_height = type + '-msgwin-height' |
|---|
| 687 | size = (gajim.config.get(opt_width), gajim.config.get(opt_height)) |
|---|
| 688 | else: |
|---|
| 689 | return |
|---|
| 690 | |
|---|
| 691 | gtkgui_helpers.resize_window(win.window, size[0], size[1]) |
|---|
| 692 | |
|---|
| 693 | def _position_window(self, win, acct, type): |
|---|
| 694 | '''Moves window according to config settings''' |
|---|
| 695 | if not gajim.config.get('saveposition') or\ |
|---|
| 696 | self.mode == self.ONE_MSG_WINDOW_NEVER: |
|---|
| 697 | return |
|---|
| 698 | |
|---|
| 699 | if self.mode == self.ONE_MSG_WINDOW_ALWAYS: |
|---|
| 700 | pos = (gajim.config.get('msgwin-x-position'), |
|---|
| 701 | gajim.config.get('msgwin-y-position')) |
|---|
| 702 | elif self.mode == self.ONE_MSG_WINDOW_PERACCT: |
|---|
| 703 | pos = (gajim.config.get_per('accounts', acct, 'msgwin-x-position'), |
|---|
| 704 | gajim.config.get_per('accounts', acct, 'msgwin-y-position')) |
|---|
| 705 | elif self.mode == self.ONE_MSG_WINDOW_PERTYPE: |
|---|
| 706 | pos = (gajim.config.get(type + '-msgwin-x-position'), |
|---|
| 707 | gajim.config.get(type + '-msgwin-y-position')) |
|---|
| 708 | else: |
|---|
| 709 | return |
|---|
| 710 | |
|---|
| 711 | gtkgui_helpers.move_window(win.window, pos[0], pos[1]) |
|---|
| 712 | |
|---|
| 713 | def _mode_to_key(self, contact, acct, type, resource = None): |
|---|
| 714 | if self.mode == self.ONE_MSG_WINDOW_NEVER: |
|---|
| 715 | key = acct + contact.jid |
|---|
| 716 | if resource: |
|---|
| 717 | key += '/' + resource |
|---|
| 718 | elif self.mode == self.ONE_MSG_WINDOW_ALWAYS: |
|---|
| 719 | key = self.MAIN_WIN |
|---|
| 720 | elif self.mode == self.ONE_MSG_WINDOW_PERACCT: |
|---|
| 721 | key = acct |
|---|
| 722 | elif self.mode == self.ONE_MSG_WINDOW_PERTYPE: |
|---|
| 723 | key = type |
|---|
| 724 | return key |
|---|
| 725 | |
|---|
| 726 | def create_window(self, contact, acct, type, resource = None): |
|---|
| 727 | key = None |
|---|
| 728 | win_acct = None |
|---|
| 729 | win_type = None |
|---|
| 730 | win_role = 'messages' |
|---|
| 731 | |
|---|
| 732 | key = self._mode_to_key(contact, acct, type, resource) |
|---|
| 733 | if self.mode == self.ONE_MSG_WINDOW_PERACCT: |
|---|
| 734 | win_acct = acct |
|---|
| 735 | win_role = acct |
|---|
| 736 | elif self.mode == self.ONE_MSG_WINDOW_PERTYPE: |
|---|
| 737 | win_type = type |
|---|
| 738 | win_role = type |
|---|
| 739 | elif self.mode == self.ONE_MSG_WINDOW_NEVER: |
|---|
| 740 | win_type = type |
|---|
| 741 | win_role = contact.jid |
|---|
| 742 | |
|---|
| 743 | win = None |
|---|
| 744 | try: |
|---|
| 745 | win = self._windows[key] |
|---|
| 746 | except KeyError: |
|---|
| 747 | win = self._new_window(win_acct, win_type) |
|---|
| 748 | |
|---|
| 749 | win.window.set_role(win_role) |
|---|
| 750 | |
|---|
| 751 | |
|---|
| 752 | if not self.one_window_opened(contact, acct, type): |
|---|
| 753 | self._position_window(win, acct, type) |
|---|
| 754 | self._resize_window(win, acct, type) |
|---|
| 755 | |
|---|
| 756 | self._windows[key] = win |
|---|
| 757 | return win |
|---|
| 758 | |
|---|
| 759 | def _on_window_delete(self, win, event): |
|---|
| 760 | self.save_state(self._gtk_win_to_msg_win(win)) |
|---|
| 761 | gajim.interface.save_config() |
|---|
| 762 | return False |
|---|
| 763 | |
|---|
| 764 | def _on_window_destroy(self, win): |
|---|
| 765 | for k in self._windows.keys(): |
|---|
| 766 | if self._windows[k].window == win: |
|---|
| 767 | del self._windows[k] |
|---|
| 768 | return |
|---|
| 769 | |
|---|
| 770 | def get_control(self, jid, acct): |
|---|
| 771 | '''Amongst all windows, return the MessageControl for jid''' |
|---|
| 772 | win = self.get_window(jid, acct) |
|---|
| 773 | if win: |
|---|
| 774 | return win.get_control(jid, acct) |
|---|
| 775 | return None |
|---|
| 776 | |
|---|
| 777 | def get_controls(self, type = None, acct = None): |
|---|
| 778 | ctrls = [] |
|---|
| 779 | for c in self.controls(): |
|---|
| 780 | if acct and c.account != acct: |
|---|
| 781 | continue |
|---|
| 782 | if not type or c.type_id == type: |
|---|
| 783 | ctrls.append(c) |
|---|
| 784 | return ctrls |
|---|
| 785 | |
|---|
| 786 | def windows(self): |
|---|
| 787 | for w in self._windows.values(): |
|---|
| 788 | yield w |
|---|
| 789 | |
|---|
| 790 | def controls(self): |
|---|
| 791 | for w in self._windows.values(): |
|---|
| 792 | for c in w.controls(): |
|---|
| 793 | yield c |
|---|
| 794 | |
|---|
| 795 | def shutdown(self): |
|---|
| 796 | for w in self.windows(): |
|---|
| 797 | self.save_state(w) |
|---|
| 798 | w.window.hide() |
|---|
| 799 | w.window.destroy() |
|---|
| 800 | gajim.interface.save_config() |
|---|
| 801 | |
|---|
| 802 | def save_state(self, msg_win): |
|---|
| 803 | if not gajim.config.get('saveposition'): |
|---|
| 804 | return |
|---|
| 805 | |
|---|
| 806 | |
|---|
| 807 | pos_x_key = 'msgwin-x-position' |
|---|
| 808 | pos_y_key = 'msgwin-y-position' |
|---|
| 809 | size_width_key = 'msgwin-width' |
|---|
| 810 | size_height_key = 'msgwin-height' |
|---|
| 811 | |
|---|
| 812 | acct = None |
|---|
| 813 | x, y = msg_win.window.get_position() |
|---|
| 814 | width, height = msg_win.window.get_size() |
|---|
| 815 | |
|---|
| 816 | |
|---|