Newer
Older
## Copyright (C) 2003-2008 Yann Leboulanger <asterix AT lagaule.org>
## Copyright (C) 2005-2008 Travis Shirk <travis AT pobox.com>
## Nikos Kouremenos <kourem AT gmail.com>
## Copyright (C) 2006 Geobert Quach <geobert AT gmail.com>
## Dimitur Kirov <dkirov AT gmail.com>
## Copyright (C) 2006-2008 Jean-Marie Traissard <jim AT lapin.org>
## Copyright (C) 2007 Julien Pivotto <roidelapluie AT gmail.com>
## Stephan Erb <steve-e AT h3c.de>
## Copyright (C) 2008 Brendan Taylor <whateley AT gmail.com>
## Jonathan Schleifer <js-gajim AT webkeks.org>
## This file is part of Gajim.
##
## Gajim is free software; you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published
## by the Free Software Foundation; version 3 only.
## Gajim is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
## GNU General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with Gajim. If not, see <http://www.gnu.org/licenses/>.

Yann Leboulanger
committed
import gobject
import common

nicfit
committed
import gtkgui_helpers
import message_control
from chat_control import ChatControlBase

nicfit
committed
from common import gajim
####################
'''Class for windows which contain message like things; chats,
groupchats, etc.'''
# DND_TARGETS is the targets needed by drag_source_set and drag_dest_set
DND_TARGETS = [('GAJIM_TAB', 0, 81)]
hid = 0 # drag_data_received handler id

Yann Leboulanger
committed
(
CLOSE_TAB_MIDDLE_CLICK,
CLOSE_ESC,
CLOSE_CLOSE_BUTTON,
CLOSE_COMMAND,
CLOSE_CTRL_KEY
) = range(5)
def __init__(self, acct, type, parent_window=None, parent_paned=None):
# A dictionary of dictionaries
# where _contacts[account][jid] == A MessageControl
# If None, the window is not tied to any specific account
self.account = acct
# If None, the window is not tied to any specific type
self.type = type
# dict { handler id: widget}. Keeps callbacks, which
# lead to cylcular references
self.handlers = {}
# Don't show warning dialogs when we want to delete the window
self.dont_warn_on_delete = False
self.widget_name = 'message_window'
self.xml = gtkgui_helpers.get_glade('%s.glade' % self.widget_name)
self.window = self.xml.get_widget(self.widget_name)
self.notebook = self.xml.get_widget('notebook')
self.parent_paned = None
if parent_window:
orig_window = self.window
self.window = parent_window
self.parent_paned = parent_paned
self.notebook.reparent(self.parent_paned)
self.parent_paned.pack2(self.notebook, resize=True, shrink=True)
orig_window.destroy()
del orig_window

Mateusz Biliński
committed
# NOTE: we use 'connect_after' here because in
# MessageWindowMgr._new_window we register handler that saves window
# state when closing it, and it should be called before
# MessageWindow._on_window_delete, which manually destroys window
# through win.destroy() - this means no additional handlers for
# 'delete-event' are called.
id = self.window.connect_after('delete-event', self._on_window_delete)
self.handlers[id] = self.window
id = self.window.connect('destroy', self._on_window_destroy)
self.handlers[id] = self.window
id = self.window.connect('focus-in-event', self._on_window_focus)
self.handlers[id] = self.window
keys=['<Control>f', '<Control>g', '<Control>h', '<Control>i',
'<Control>l', '<Control>L', '<Control>n', '<Control>t', '<Control>u',
'<Control>v', '<Control>b', '<Control><Shift>Tab', '<Control>Tab',
'<Control>F4', '<Control>w', '<Control>Page_Up', '<Control>Page_Down',
'<Alt>Right', '<Alt>Left', '<Alt>a', '<Alt>c', '<Alt>m', 'Escape'] + \
['<Alt>'+str(i) for i in xrange(10)]
accel_group = gtk.AccelGroup()

Yann Leboulanger
committed
for key in keys:
keyval, mod = gtk.accelerator_parse(key)
accel_group.connect_group(keyval, mod, gtk.ACCEL_VISIBLE,
self.accel_group_func)
self.window.add_accel_group(accel_group)
# gtk+ doesn't make use of the motion notify on gtkwindow by default
# so this line adds that
self.window.add_events(gtk.gdk.POINTER_MOTION_MASK)
self.alignment = self.xml.get_widget('alignment')
id = self.notebook.connect('switch-page',
self.handlers[id] = self.notebook
id = self.notebook.connect('key-press-event',
# Remove the glade pages
while self.notebook.get_n_pages():
self.notebook.remove_page(0)
# Tab customizations
pref_pos = gajim.config.get('tabs_position')

Yann Leboulanger
committed
if pref_pos == 'bottom':
nb_pos = gtk.POS_BOTTOM
elif pref_pos == 'left':
nb_pos = gtk.POS_LEFT
elif pref_pos == 'right':
nb_pos = gtk.POS_RIGHT
else:
nb_pos = gtk.POS_TOP
self.notebook.set_tab_pos(nb_pos)

Yann Leboulanger
committed
window_mode = gajim.interface.msg_win_mgr.mode
if gajim.config.get('tabs_always_visible') or \
window_mode == MessageWindowMgr.ONE_MSG_WINDOW_ALWAYS_WITH_ROSTER:
self.notebook.set_show_tabs(True)
self.alignment.set_property('top-padding', 2)
else:
self.notebook.set_show_tabs(False)
self.notebook.set_show_border(gajim.config.get('tabs_border'))
# if GTK+ version < 2.10, use OUR way to reorder tabs (set up DnD)
if gtk.pygtk_version < (2, 10, 0) or gtk.gtk_version < (2, 10, 0):
self.hid = self.notebook.connect('drag_data_received',
self.on_tab_label_drag_data_received_cb)
self.handlers[self.hid] = self.notebook
self.notebook.drag_dest_set(gtk.DEST_DEFAULT_ALL, self.DND_TARGETS,
gtk.gdk.ACTION_MOVE)

Yann Leboulanger
committed
def change_account_name(self, old_name, new_name):
if self._controls.has_key(old_name):
self._controls[new_name] = self._controls[old_name]
del self._controls[old_name]

Yann Leboulanger
committed
for ctrl in self.controls():
if ctrl.account == old_name:
ctrl.account = new_name
if self.account == old_name:
self.account = new_name

nicfit
committed
def get_num_controls(self):
n = 0
for dict in self._controls.values():
n += len(dict)

nicfit
committed
return n
def resize(self, width, height):
gtkgui_helpers.resize_window(self.window, width, height)
def _on_window_focus(self, widget, event):
# window received focus, so if we had urgency REMOVE IT
# NOTE: we do not have to read the message (it maybe in a bg tab)
# to remove urgency hint so this functions does that
gtkgui_helpers.set_unset_urgency_hint(self.window, False)
ctrl = self.get_active_control()
if ctrl:
ctrl.set_control_active(True)

nicfit
committed
self.redraw_tab(ctrl, 'active')
else:
# NOTE: we do not send any chatstate to preserve
# inactive, gone, etc.

nicfit
committed
self.redraw_tab(ctrl)
def _on_window_delete(self, win, event):
if self.dont_warn_on_delete:
# Destroy the window
return False
def on_yes(ctrl):
if self.on_delete_ok == 1:
self.dont_warn_on_delete = True
win.destroy()
self.on_delete_ok -= 1
def on_no(ctrl):
return
def on_minimize(ctrl):
ctrl.minimize()
if self.on_delete_ok == 1:
self.dont_warn_on_delete = True
win.destroy()
self.on_delete_ok -= 1
ctrl_to_minimize = []
self.on_delete_ok = self.get_nb_controls()
for ctrl in self.controls():
ctrl.allow_shutdown(self.CLOSE_CLOSE_BUTTON, on_yes, on_no,
on_minimize)
return True # halt the delete for the moment
def _on_window_destroy(self, win):
for ctrl in self.controls():
# Clean up handlers connected to the parent window, this is important since
# self.window may be the RosterWindow
for i in self.handlers.keys():
if self.handlers[i].handler_is_connected(i):
self.handlers[i].disconnect(i)
del self.handlers[i]
del self.handlers
def new_tab(self, control):

Yann Leboulanger
committed
fjid = control.get_full_jid()
if not self._controls.has_key(control.account):
self._controls[control.account] = {}
self._controls[control.account][fjid] = control

nicfit
committed

Yann Leboulanger
committed
if self.get_num_controls() == 2:
# is first conversation_textview scrolled down ?
scrolled = False
first_widget = self.notebook.get_nth_page(0)
ctrl = self._widget_to_control(first_widget)
conv_textview = ctrl.conv_textview
if conv_textview.at_the_end():
scrolled = True

Yann Leboulanger
committed
if scrolled:
gobject.idle_add(conv_textview.scroll_to_end_iter)
# Add notebook page and connect up to the tab's close button
xml = gtkgui_helpers.get_glade('message_window.glade', 'chat_tab_ebox')
tab_label_box = xml.get_widget('chat_tab_ebox')
id = widget.connect('clicked', self._on_close_button_clicked, control)
control.handlers[id] = widget
id = tab_label_box.connect('button-press-event', self.on_tab_eventbox_button_press_event,
control.widget)
control.handlers[id] = tab_label_box
self.notebook.append_page(control.widget, tab_label_box)
# If GTK+ version >= 2.10, use gtk native way to reorder tabs
if gtk.pygtk_version >= (2, 10, 0) and gtk.gtk_version >= (2, 10, 0):
self.notebook.set_tab_reorderable(control.widget, True)
else:
self.setup_tab_dnd(control.widget)

nicfit
committed
self.redraw_tab(control)
if self.parent_paned:
self.notebook.show_all()
else:
self.window.show_all()
# NOTE: we do not call set_control_active(True) since we don't know whether
# the tab is the active one.
self.show_title()
def on_tab_eventbox_button_press_event(self, widget, event, child):
if event.button == 3: # right click
n = self.notebook.page_num(child)
self.notebook.set_current_page(n)
self.popup_menu(event)
elif event.button == 2: # middle click
ctrl = self._widget_to_control(child)

Yann Leboulanger
committed
self.remove_tab(ctrl, self.CLOSE_TAB_MIDDLE_CLICK)
def _on_message_textview_mykeypress_event(self, widget, event_keyval,
# NOTE: handles mykeypress which is custom signal; see message_textview.py
# construct event instance from binding
event = gtk.gdk.Event(gtk.gdk.KEY_PRESS) # it's always a key-press here
event.keyval = event_keyval
event.state = event_keymod
event.time = 0 # assign current time
if event.state & gtk.gdk.CONTROL_MASK:
# Tab switch bindings
if event.keyval == gtk.keysyms.Tab: # CTRL + TAB
elif event.keyval == gtk.keysyms.ISO_Left_Tab: # CTRL + SHIFT + TAB
elif event.keyval == gtk.keysyms.Page_Down: # CTRL + PAGE DOWN
self.notebook.emit('key_press_event', event)
elif event.keyval == gtk.keysyms.Page_Up: # CTRL + PAGE UP
self.notebook.emit('key_press_event', event)

Yann Leboulanger
committed
def accel_group_func(self, accel_group, acceleratable, keyval, modifier):

Yann Leboulanger
committed
st = '1234567890' # alt+1 means the first tab (tab 0)

Yann Leboulanger
committed
control = self.get_active_control()
if not control:
# No more control in this window
return

Yann Leboulanger
committed
# CTRL mask

Yann Leboulanger
committed
if modifier & gtk.gdk.CONTROL_MASK:
if keyval == gtk.keysyms.h: # CTRL + h

Yann Leboulanger
committed
control._on_history_menuitem_activate()

Yann Leboulanger
committed
elif control.type_id == message_control.TYPE_CHAT and \
keyval == gtk.keysyms.f: # CTRL + f
control._on_send_file_menuitem_activate(None)
elif control.type_id == message_control.TYPE_CHAT and \
keyval == gtk.keysyms.g: # CTRL + g
control._on_convert_to_gc_menuitem_activate(None)
elif control.type_id in (message_control.TYPE_CHAT,
message_control.TYPE_PM) and keyval == gtk.keysyms.i: # CTRL + i

Yann Leboulanger
committed
control._on_contact_information_menuitem_activate(None)
elif keyval == gtk.keysyms.l or keyval == gtk.keysyms.L: # CTRL + l|L
control.conv_textview.clear()
elif control.type_id == message_control.TYPE_GC and \
keyval == gtk.keysyms.n: # CTRL + n
control._on_change_nick_menuitem_activate(None)
elif control.type_id == message_control.TYPE_GC and \
keyval == gtk.keysyms.t: # CTRL + t
control._on_change_subject_menuitem_activate(None)
elif keyval == gtk.keysyms.u: # CTRL + u: emacs style clear line
control.clear(control.msg_textview)
elif keyval == gtk.keysyms.v: # CTRL + v: Paste into msg_textview
if not control.msg_textview.is_focus():
control.msg_textview.grab_focus()
# Paste into the msg textview
event = gtk.gdk.Event(gtk.gdk.KEY_PRESS)
event.window = self.window.window
event.time = int(time.time())
event.state = gtk.gdk.CONTROL_MASK
event.keyval = gtk.keysyms.v
control.msg_textview.emit('key_press_event', event)
elif control.type_id == message_control.TYPE_GC and \
keyval == gtk.keysyms.b: # CTRL + b
control._on_bookmark_room_menuitem_activate(None)

Yann Leboulanger
committed
# Tab switch bindings
elif keyval == gtk.keysyms.ISO_Left_Tab: # CTRL + SHIFT + TAB
self.move_to_next_unread_tab(False)
elif keyval == gtk.keysyms.Tab: # CTRL + TAB
self.move_to_next_unread_tab(True)
elif keyval == gtk.keysyms.F4: # CTRL + F4
self.remove_tab(control, self.CLOSE_CTRL_KEY)
elif keyval == gtk.keysyms.w: # CTRL + w
# CTRL + w removes latest word before sursor when User uses emacs

Yann Leboulanger
committed
# theme
if not gtk.settings_get_default().get_property(
'gtk-key-theme-name') == 'Emacs':
self.remove_tab(control, self.CLOSE_CTRL_KEY)
elif keyval in (gtk.keysyms.Page_Up, gtk.keysyms.Page_Down):
# CTRL + PageUp | PageDown
# Create event and send it to notebook
event = gtk.gdk.Event(gtk.gdk.KEY_PRESS)
event.window = self.window.window
event.time = int(time.time())
event.state = gtk.gdk.CONTROL_MASK
event.keyval = int(keyval)
self.notebook.emit('key_press_event', event)

Yann Leboulanger
committed
# MOD1 (ALT) mask
elif modifier & gtk.gdk.MOD1_MASK:
# Tab switch bindings
if keyval == gtk.keysyms.Right: # ALT + RIGHT
new = self.notebook.get_current_page() + 1

Yann Leboulanger
committed
new = 0
self.notebook.set_current_page(new)
elif keyval == gtk.keysyms.Left: # ALT + LEFT
new = self.notebook.get_current_page() - 1
if new < 0:
new = self.notebook.get_n_pages() - 1
self.notebook.set_current_page(new)
elif chr(keyval) in st: # ALT + 1,2,3..
self.notebook.set_current_page(st.index(chr(keyval)))
elif keyval == gtk.keysyms.c: # ALT + C toggles chat buttons
control.chat_buttons_set_visible(not control.hide_chat_buttons)
elif keyval == gtk.keysyms.m: # ALT + M show emoticons menu
control.show_emoticons_menu()
elif keyval == gtk.keysyms.a: # ALT + A show actions menu
control.on_actions_button_clicked(control.actions_button)

Yann Leboulanger
committed
# Close tab bindings
elif keyval == gtk.keysyms.Escape and \
gajim.config.get('escape_key_closes'): # Escape
self.remove_tab(control, self.CLOSE_ESC)

Yann Leboulanger
committed
def _on_close_button_clicked(self, button, control):
'''When close button is pressed: close a tab'''

Yann Leboulanger
committed
self.remove_tab(control, self.CLOSE_CLOSE_BUTTON)

nicfit
committed

nicfit
committed
'''redraw the window's title'''
if not control:
control = self.get_active_control()
if not control:
# No more control in this window
return

nicfit
committed
unread = 0
for ctrl in self.controls():

Yann Leboulanger
committed
if ctrl.type_id == message_control.TYPE_GC and not \
gajim.config.get('notify_on_all_muc_messages') and not \
ctrl.attention_flag:
# count only pm messages
unread += ctrl.get_nb_unread_pm()

Yann Leboulanger
committed
continue

Yann Leboulanger
committed
unread += ctrl.get_nb_unread()

nicfit
committed
if unread > 1:

nicfit
committed
elif unread == 1:

Yann Leboulanger
committed
else:
urgent = False
if control.resource:
name += '/' + control.resource
if window_mode == MessageWindowMgr.ONE_MSG_WINDOW_PERTYPE:
# Show the plural form since number of tabs > 1
elif window_mode == MessageWindowMgr.ONE_MSG_WINDOW_ALWAYS_WITH_ROSTER:
label = None
elif self.get_num_controls() == 1:
label = name
if window_mode == MessageWindowMgr.ONE_MSG_WINDOW_PERACCT:
title = title + ": " + control.account

nicfit
committed

nicfit
committed
if urgent:
gtkgui_helpers.set_unset_urgency_hint(self.window, unread)
else:
gtkgui_helpers.set_unset_urgency_hint(self.window, False)
ctrl_page = self.notebook.page_num(ctrl.widget)
self.notebook.set_current_page(ctrl_page)
def remove_tab(self, ctrl, method, reason = None, force = False):

Yann Leboulanger
committed
'''reason is only for gc (offline status message)
if force is True, do not ask any confirmation'''
def close(ctrl):
if reason is not None: # We are leaving gc with a status message
ctrl.shutdown(reason)
else: # We are leaving gc without status message or it's a chat
ctrl.shutdown()
# Update external state
gajim.events.remove_events(ctrl.account, ctrl.get_full_jid,
types = ['printed_msg', 'chat', 'gc_msg'])
fjid = ctrl.get_full_jid()
jid = gajim.get_jid_without_resource(fjid)
fctrl = self.get_control(fjid, ctrl.account)
bctrl = self.get_control(jid, ctrl.account)
# keep last_message_time around unless this was our last control with
# that jid
if not fctrl and not bctrl:
del gajim.last_message_time[ctrl.account][fjid]
# Disconnect tab DnD only if GTK version < 2.10
if gtk.pygtk_version < (2, 10, 0) or gtk.gtk_version < (2, 10, 0):
self.disconnect_tab_dnd(ctrl.widget)
self.notebook.remove_page(self.notebook.page_num(ctrl.widget))
del self._controls[ctrl.account][fjid]
if len(self._controls[ctrl.account]) == 0:
del self._controls[ctrl.account]

nicfit
committed
def on_no(ctrl):
close(ctrl)
def on_minimize(ctrl):
if method != self.CLOSE_COMMAND:
ctrl.minimize()
self.check_tabs()
return
close(ctrl)
# Shutdown the MessageControl
if force:
close(ctrl)
else:
ctrl.allow_shutdown(method, on_yes, on_no, on_minimize)
def check_tabs(self):
if self.get_num_controls() == 0:

nicfit
committed
# These are not called when the window is destroyed like this, fake it
gajim.interface.msg_win_mgr._on_window_delete(self.window, None)
gajim.interface.msg_win_mgr._on_window_destroy(self.window)
# dnd clean up
self.notebook.drag_dest_unset()
if self.parent_paned:
# Don't close parent window, just remove the child
child = self.parent_paned.get_child2()
self.parent_paned.remove(child)
else:
self.window.destroy()
return # don't show_title, we are dead
elif self.get_num_controls() == 1: # we are going from two tabs to one

Yann Leboulanger
committed
window_mode = gajim.interface.msg_win_mgr.mode
show_tabs_if_one_tab = gajim.config.get('tabs_always_visible') or \
window_mode == MessageWindowMgr.ONE_MSG_WINDOW_ALWAYS_WITH_ROSTER
self.notebook.set_show_tabs(show_tabs_if_one_tab)
if not show_tabs_if_one_tab:
self.alignment.set_property('top-padding', 0)

nicfit
committed
def redraw_tab(self, ctrl, chatstate = None):
hbox = self.notebook.get_tab_label(ctrl.widget).get_children()[0]
status_img = hbox.get_children()[0]
nick_label = hbox.get_children()[1]
# Optionally hide close button
close_button = hbox.get_children()[2]
if gajim.config.get('tabs_close_button'):
close_button.show()
else:
close_button.hide()
# Update nick
(tab_label_str, tab_label_color) = ctrl.get_tab_label(chatstate)

nicfit
committed
nick_label.set_markup(tab_label_str)
if tab_label_color:
nick_label.modify_fg(gtk.STATE_NORMAL, tab_label_color)
nick_label.modify_fg(gtk.STATE_ACTIVE, tab_label_color)
if tab_img:
if tab_img.get_storage_type() == gtk.IMAGE_ANIMATION:
status_img.set_from_animation(tab_img.get_animation())
else:
status_img.set_from_pixbuf(tab_img.get_pixbuf())
def repaint_themed_widgets(self):
'''Repaint controls in the window with theme color'''
# iterate through controls and repaint
for ctrl in self.controls():
for ctrl in self.controls():

nicfit
committed
def get_active_control(self):
notebook = self.notebook
active_widget = notebook.get_nth_page(notebook.get_current_page())

nicfit
committed
def get_active_contact(self):
ctrl = self.get_active_control()
if ctrl:
return ctrl.contact
return None
contact = self.get_active_contact()
if contact:
return contact.jid
return None
def is_active(self):
return self.window.is_active()
def get_origin(self):
return self.window.window.get_origin()
def toggle_emoticons(self):
for ctrl in self.controls():

nicfit
committed
def update_font(self):
for ctrl in self.controls():

nicfit
committed
def update_tags(self):
for ctrl in self.controls():
def get_control(self, key, acct):
'''Return the MessageControl for jid or n, where n is a notebook page index.
When key is an int index acct may be None'''
if isinstance(key, str):
key = unicode(key, 'utf-8')

nicfit
committed
try:
return self._controls[acct][jid]

nicfit
committed
except:
return None
if page_num is None:
page_num = notebook.get_current_page()
nth_child = notebook.get_nth_page(page_num)
return (acct in self._controls and jid in self._controls[acct])
def change_key(self, old_jid, new_jid, acct):
ctrl = self._controls[acct][old_jid]
self._controls[acct][new_jid] = ctrl
del self._controls[acct][old_jid]

Yann Leboulanger
committed
if old_jid in gajim.last_message_time[acct]:
gajim.last_message_time[acct][new_jid] = \
gajim.last_message_time[acct][old_jid]
del gajim.last_message_time[acct][old_jid]
for jid_dict in self._controls.values():
for ctrl in jid_dict.values():
yield ctrl
def get_nb_controls(self):
nb_ctrl = 0
for jid_dict in self._controls.values():
for ctrl in jid_dict.values():
nb_ctrl += 1
return nb_ctrl
def move_to_next_unread_tab(self, forward):
ind = self.notebook.get_current_page()
current = ind
found = False
first_composing_ind = -1 # id of first composing ctrl to switch to
# if no others controls have awaiting events
# loop until finding an unread tab or having done a complete cycle
if forward == True: # look for the first unread tab on the right
ind = ind + 1
if ind >= self.notebook.get_n_pages():
ind = 0
else: # look for the first unread tab on the right
ind = ind - 1
if ind < 0:
ind = self.notebook.get_n_pages() - 1
ctrl = self.get_control(ind, None)

Yann Leboulanger
committed
if ctrl.get_nb_unread() > 0:

Yann Leboulanger
committed
elif gajim.config.get('ctrl_tab_go_to_next_composing') : # Search for a composing contact
contact = ctrl.contact
if first_composing_ind == -1 and contact.chatstate == 'composing':
# If no composing contact found yet, check if this one is composing
first_composing_ind = ind
if ind == current:
break # a complete cycle without finding an unread tab
elif first_composing_ind != -1:
self.notebook.set_current_page(first_composing_ind)
else: # not found and nobody composing
if forward: # CTRL + TAB
if current < (self.notebook.get_n_pages() - 1):
self.notebook.next_page()
else: # traverse for ever (eg. don't stop at last tab)
self.notebook.set_current_page(0)
else: # CTRL + SHIFT + TAB
if current > 0:
self.notebook.prev_page()
else: # traverse for ever (eg. don't stop at first tab)
self.notebook.set_current_page(
self.notebook.get_n_pages() - 1)
def popup_menu(self, event):
menu = self.get_active_control().prepare_context_menu()
# show the menu
menu.popup(None, None, None, event.button, event.time)
menu.show_all()
def _on_notebook_switch_page(self, notebook, page, page_num):
old_no = notebook.get_current_page()
if old_no >= 0:
old_ctrl = self._widget_to_control(notebook.get_nth_page(old_no))
old_ctrl.set_control_active(False)
new_ctrl = self._widget_to_control(notebook.get_nth_page(page_num))
new_ctrl.set_control_active(True)
self.show_title(control = new_ctrl)

nicfit
committed
control = self.get_active_control()
if isinstance(control, ChatControlBase):
control.msg_textview.grab_focus()
def _on_notebook_key_press(self, widget, event):
# Ctrl+PageUP / DOWN has to be handled by notebook
if (event.state & gtk.gdk.CONTROL_MASK and
event.keyval in (gtk.keysyms.Page_Down, gtk.keysyms.Page_Up)):
# when tab itself is selected, make sure <- and -> are allowed for navigating between tabs
if event.keyval in (gtk.keysyms.Left, gtk.keysyms.Right):
return False

Yann Leboulanger
committed
if isinstance(control, ChatControlBase):
# we forwarded it to message textview

Yann Leboulanger
committed
control.msg_textview.emit('key_press_event', event)
control.msg_textview.grab_focus()
def setup_tab_dnd(self, child):
'''Set tab label as drag source and connect the drag_data_get signal'''
tab_label = self.notebook.get_tab_label(child)
tab_label.dnd_handler = tab_label.connect('drag_data_get',
self.on_tab_label_drag_data_get_cb)
self.handlers[tab_label.dnd_handler] = tab_label
tab_label.drag_source_set(gtk.gdk.BUTTON1_MASK, self.DND_TARGETS,
gtk.gdk.ACTION_MOVE)
tab_label.page_num = self.notebook.page_num(child)
def on_tab_label_drag_data_get_cb(self, widget, drag_context, selection,
info, time):
source_page_num = self.find_page_num_according_to_tab_label(widget)
# 8 is the data size for the string
selection.set(selection.target, 8, str(source_page_num))
def on_tab_label_drag_data_received_cb(self, widget, drag_context, x, y,
'''Reorder the tabs according to the drop position'''
source_page_num = int(selection.data)
dest_page_num, to_right = self.get_tab_at_xy(x, y)
source_child = self.notebook.get_nth_page(source_page_num)
if dest_page_num != source_page_num:
self.notebook.reorder_child(source_child, dest_page_num)
def get_tab_at_xy(self, x, y):
'''Thanks to Gaim
Return the tab under xy and
'''
page_num = -1
to_right = False
horiz = self.notebook.get_tab_pos() == gtk.POS_TOP or \
self.notebook.get_tab_pos() == gtk.POS_BOTTOM
for i in xrange(self.notebook.get_n_pages()):
page = self.notebook.get_nth_page(i)
tab = self.notebook.get_tab_label(page)
tab_alloc = tab.get_allocation()
if horiz:
if (x >= tab_alloc.x) and \
(x <= (tab_alloc.x + tab_alloc.width)):
page_num = i
if x >= tab_alloc.x + (tab_alloc.width / 2.0):
to_right = True
break
else:
if (y >= tab_alloc.y) and \
(y <= (tab_alloc.y + tab_alloc.height)):
if y > tab_alloc.y + (tab_alloc.height / 2.0):
to_right = True
break
return (page_num, to_right)
def find_page_num_according_to_tab_label(self, tab_label):
'''Find the page num of the tab label'''
page_num = -1
for i in xrange(self.notebook.get_n_pages()):
page = self.notebook.get_nth_page(i)
tab = self.notebook.get_tab_label(page)
if tab == tab_label:
page_num = i
break
return page_num
def disconnect_tab_dnd(self, child):
'''Clean up DnD signals, source and dest'''
tab_label = self.notebook.get_tab_label(child)
tab_label.drag_source_unset()
tab_label.disconnect(tab_label.dnd_handler)
################################################################################
'''A manager and factory for MessageWindow objects'''
__gsignals__ = {
'window-delete': (gobject.SIGNAL_RUN_LAST, None, (object,)),
}
# These constants map to common.config.opt_one_window_types indices
(
ONE_MSG_WINDOW_NEVER,
ONE_MSG_WINDOW_ALWAYS,
ONE_MSG_WINDOW_PERTYPE,
) = range(5)
# A key constant for the main window in ONE_MSG_WINDOW_ALWAYS mode
MAIN_WIN = 'main'
# A key constant for the main window in ONE_MSG_WINDOW_ALWAYS_WITH_ROSTER mode
ROSTER_MAIN_WIN = 'roster'
''' A dictionary of windows; the key depends on the config:
ONE_MSG_WINDOW_NEVER: The key is the contact JID
ONE_MSG_WINDOW_ALWAYS: The key is MessageWindowMgr.MAIN_WIN
ONE_MSG_WINDOW_ALWAYS_WITH_ROSTER: The key is MessageWindowMgr.MAIN_WIN
ONE_MSG_WINDOW_PERACCT: The key is the account name
ONE_MSG_WINDOW_PERTYPE: The key is a message type constant'''
# Map the mode to a int constant for frequent compares
mode = gajim.config.get('one_message_window')
self.mode = common.config.opt_one_window_types.index(mode)

Yann Leboulanger
committed
self.parent_win = parent_window
self.parent_paned = parent_paned

Yann Leboulanger
committed
def change_account_name(self, old_name, new_name):
for win in self.windows():
win.change_account_name(old_name, new_name)
parent_win = None
parent_paned = None
if self.mode == self.ONE_MSG_WINDOW_ALWAYS_WITH_ROSTER:
parent_win = self.parent_win
parent_paned = self.parent_paned
win = MessageWindow(acct, type, parent_win, parent_paned)
# we track the lifetime of this window
win.window.connect('delete-event', self._on_window_delete)
win.window.connect('destroy', self._on_window_destroy)
return win
for w in self.windows():
if w.window == gtk_win:
return w
return None
for win in self.windows():
if win.has_control(jid, acct):
return win
return self.get_window(jid, acct) is not None
def one_window_opened(self, contact=None, acct=None, type=None):
return \
self._windows[self._mode_to_key(contact, acct, type)] is not None
def _resize_window(self, win, acct, type):
if self.mode in (self.ONE_MSG_WINDOW_ALWAYS,
self.ONE_MSG_WINDOW_ALWAYS_WITH_ROSTER):

Yann Leboulanger
committed
size = (gajim.config.get('msgwin-width'),
gajim.config.get('msgwin-height'))
if self.mode == self.ONE_MSG_WINDOW_ALWAYS_WITH_ROSTER:
parent_size = win.window.get_size()
# Need to add the size of the now visible paned handle, otherwise
# the saved width of the message window decreases by this amount
handle_size = win.parent_paned.style_get_property('handle-size')
size = (parent_size[0] + size[0] + handle_size, size[1])

Yann Leboulanger
committed
size = (gajim.config.get_per('accounts', acct, 'msgwin-width'),
gajim.config.get_per('accounts', acct, 'msgwin-height'))
elif self.mode in (self.ONE_MSG_WINDOW_NEVER, self.ONE_MSG_WINDOW_PERTYPE):

Yann Leboulanger
committed
if type == message_control.TYPE_PM:
type = message_control.TYPE_CHAT
opt_width = type + '-msgwin-width'
opt_height = type + '-msgwin-height'
size = (gajim.config.get(opt_width), gajim.config.get(opt_height))
if win.parent_paned:
win.parent_paned.set_position(parent_size[0])
if (self.mode in [self.ONE_MSG_WINDOW_NEVER,
self.ONE_MSG_WINDOW_ALWAYS_WITH_ROSTER]):
pos = (gajim.config.get('msgwin-x-position'),
gajim.config.get('msgwin-y-position'))
pos = (gajim.config.get_per('accounts', acct, 'msgwin-x-position'),
gajim.config.get_per('accounts', acct, 'msgwin-y-position'))
pos = (gajim.config.get(type + '-msgwin-x-position'),
gajim.config.get(type + '-msgwin-y-position'))
gtkgui_helpers.move_window(win.window, pos[0], pos[1])
def _mode_to_key(self, contact, acct, type, resource = None):

nicfit
committed
key = acct + contact.jid
if resource:
key += '/' + resource
return self.MAIN_WIN
elif self.mode == self.ONE_MSG_WINDOW_ALWAYS_WITH_ROSTER:
return self.ROSTER_MAIN_WIN
def create_window(self, contact, acct, type, resource = None):
win_key = self._mode_to_key(contact, acct, type, resource)
win_role = type
elif self.mode == self.ONE_MSG_WINDOW_NEVER:
win_type = type
elif self.mode == self.ONE_MSG_WINDOW_ALWAYS:
win_role = 'messages'
win = None
try:
except KeyError:
# Position and size window based on saved state and window mode
self._position_window(win, acct, type)