gajim-plural/src/groupchat_control.py

711 lines
24 KiB
Python
Raw Normal View History

2005-12-31 08:35:14 +01:00
## groupchat_control.py
##
## Copyright (C) 2005-2006 Travis Shirk <travis@pobox.com>
##
## This program 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 2 only.
##
## This program 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.
##
2006-01-06 04:36:07 +01:00
import os
2006-01-05 06:51:28 +01:00
import time
2005-12-31 08:35:14 +01:00
import gtk
import gtk.glade
import pango
import gobject
import gtkgui_helpers
import message_control
2006-01-06 04:36:07 +01:00
import tooltips
import dialogs
import vcard
import chat
import cell_renderer_image
import history_window
import tooltips
from common import gajim
from common import helpers
2005-12-31 08:35:14 +01:00
from common import gajim
2006-01-01 20:40:05 +01:00
from chat_control import ChatControl
2005-12-31 08:35:14 +01:00
from chat_control import ChatControlBase
from conversation_textview import ConversationTextview
from message_textview import MessageTextView
from gettext import ngettext
from common import i18n
_ = i18n._
Q_ = i18n.Q_
APP = i18n.APP
gtk.glade.bindtextdomain(APP, i18n.DIR)
gtk.glade.textdomain(APP)
GTKGUI_GLADE = 'gtkgui.glade'
#(status_image, type, nick, shown_nick)
(
C_IMG, # image to show state (online, new message etc)
C_TYPE, # type of the row ('contact' or 'group')
C_NICK, # contact nickame or group name
C_TEXT, # text shown in the cellrenderer
) = range(4)
2005-12-31 08:35:14 +01:00
2006-01-01 20:40:05 +01:00
class PrivateChatControl(ChatControl):
TYPE_ID = message_control.TYPE_PM
2006-01-01 20:40:05 +01:00
def __init__(self, parent_win, contact, acct):
ChatControl.__init__(self, parent_win, contact, acct)
self.TYPE_ID = 'pm'
2006-01-05 06:51:28 +01:00
self.display_name = _('Private chat')
2006-01-01 20:40:05 +01:00
2005-12-31 08:35:14 +01:00
class GroupchatControl(ChatControlBase):
TYPE_ID = message_control.TYPE_GC
2005-12-31 08:35:14 +01:00
def __init__(self, parent_win, contact, acct):
ChatControlBase.__init__(self, self.TYPE_ID, parent_win,
'muc_child_vbox', _('Group Chat'), contact, acct);
2006-01-05 06:51:28 +01:00
self.room_jid = self.contact.jid
self.nick = contact.name
self.name = self.room_jid.split('@')[0]
2005-12-31 08:35:14 +01:00
self.compact_view_always = gajim.config.get('always_compact_view_gc')
2006-01-06 04:36:07 +01:00
self.gc_refer_to_nick_char = gajim.config.get('gc_refer_to_nick_char')
self._last_selected_contact = None # None or holds jid, account tuple
# alphanum sorted
self.muc_cmds = ['ban', 'chat', 'query', 'clear', 'close', 'compact', 'help', 'invite',
'join', 'kick', 'leave', 'me', 'msg', 'nick', 'part', 'say', 'topic']
2006-01-06 02:54:33 +01:00
# muc attention flag (when we are mentioned in a muc)
# if True, the room has mentioned us
self.attention_flag = False
self.room_creation = time.time()
self.nick_hits = 0
self.cmd_hits = 0
self.last_key_tabs = False
self.subject = ''
self.subject_tooltip = gtk.Tooltips()
2006-01-06 04:36:07 +01:00
self.tooltip = tooltips.GCTooltip()
2006-01-05 06:51:28 +01:00
self.allow_focus_out_line = True
# holds the iter's offset which points to the end of --- line
self.focus_out_end_iter_offset = None
2006-01-01 20:40:05 +01:00
2006-01-02 10:04:30 +01:00
# connect the menuitems to their respective functions
2006-01-05 06:51:28 +01:00
xm = gtk.glade.XML(GTKGUI_GLADE, 'gc_control_popup_menu', APP)
2006-01-02 10:04:30 +01:00
xm.signal_autoconnect(self)
self.gc_popup_menu = xm.get_widget('gc_popup_menu')
2006-01-06 02:54:33 +01:00
self.name_label = self.xml.get_widget('banner_name_label')
2006-01-06 04:36:07 +01:00
# set the position of the current hpaned
self.hpaned_position = gajim.config.get('gc-hpaned-position')
self.hpaned = self.xml.get_widget('hpaned')
self.hpaned.set_position(self.hpaned_position)
2006-01-06 02:54:33 +01:00
list_treeview = self.list_treeview = self.xml.get_widget('list_treeview')
list_treeview.get_selection().connect('changed',
self.on_list_treeview_selection_changed)
list_treeview.connect('style-set', self.on_list_treeview_style_set)
2006-01-06 04:36:07 +01:00
# we want to know when the the widget resizes, because that is
# an indication that the hpaned has moved...
# FIXME: Find a better indicator that the hpaned has moved.
self.list_treeview.connect('size-allocate', self.on_treeview_size_allocate)
#status_image, type, nickname, shown_nick
store = gtk.TreeStore(gtk.Image, str, str, str)
store.set_sort_column_id(C_TEXT, gtk.SORT_ASCENDING)
column = gtk.TreeViewColumn('contacts')
renderer_image = cell_renderer_image.CellRendererImage()
renderer_image.set_property('width', 20)
column.pack_start(renderer_image, expand = False)
column.add_attribute(renderer_image, 'image', 0)
renderer_text = gtk.CellRendererText()
column.pack_start(renderer_text, expand = True)
column.set_attributes(renderer_text, markup = C_TEXT)
column.set_cell_data_func(renderer_image, self.tree_cell_data_func, None)
column.set_cell_data_func(renderer_text, self.tree_cell_data_func, None)
self.list_treeview.append_column(column)
self.list_treeview.set_model(store)
# workaround to avoid gtk arrows to be shown
column = gtk.TreeViewColumn() # 2nd COLUMN
renderer = gtk.CellRendererPixbuf()
column.pack_start(renderer, expand = False)
self.list_treeview.append_column(column)
column.set_visible(False)
self.list_treeview.set_expander_column(column)
# set an empty subject to show the room_jid
self.set_subject('')
self.got_disconnected() #init some variables
self.draw_widgets()
self.conv_textview.grab_focus()
self.widget.show_all()
def tree_cell_data_func(self, column, renderer, model, iter, data=None):
theme = gajim.config.get('roster_theme')
if model.iter_parent(iter):
bgcolor = gajim.config.get_per('themes', theme, 'contactbgcolor')
else: # it is root (eg. group)
bgcolor = gajim.config.get_per('themes', theme, 'groupbgcolor')
if bgcolor:
renderer.set_property('cell-background', bgcolor)
else:
renderer.set_property('cell-background', None)
2006-01-06 02:54:33 +01:00
2006-01-06 04:36:07 +01:00
def on_treeview_size_allocate(self, widget, allocation):
'''The MUC treeview has resized. Move the hpaned in all tabs to match'''
self.hpaned_position = self.hpaned.get_position()
self.hpaned.set_position(self.hpaned_position)
2006-01-06 02:54:33 +01:00
def iter_contact_rows(self):
'''iterate over all contact rows in the tree model'''
model = self.list_treeview.get_model()
role_iter = model.get_iter_root()
while role_iter:
contact_iter = model.iter_children(role_iter)
while contact_iter:
yield model[contact_iter]
contact_iter = model.iter_next(contact_iter)
role_iter = model.iter_next(role_iter)
def on_list_treeview_style_set(self, treeview, style):
'''When style (theme) changes, redraw all contacts'''
# Get the room_jid from treeview
for contact in self.iter_contact_rows():
nick = contact[C_NICK].decode('utf-8')
self.draw_contact(nick)
def on_list_treeview_selection_changed(self, selection):
model, selected_iter = selection.get_selected()
2006-01-06 04:36:07 +01:00
self.draw_contact(self.nick)
2006-01-06 02:54:33 +01:00
if self._last_selected_contact is not None:
self.draw_contact(self._last_selected_contact)
if selected_iter is None:
self._last_selected_contact = None
return
contact = model[selected_iter]
nick = contact[C_NICK].decode('utf-8')
self._last_selected_contact = nick
if contact[C_TYPE] != 'contact':
return
self.draw_contact(nick, selected=True, focus=True)
2006-01-05 06:51:28 +01:00
def get_tab_label(self, chatstate):
2006-01-01 20:40:05 +01:00
'''Markup the label if necessary. Returns a tuple such as:
(new_label_str, color)
either of which can be None
if chatstate is given that means we have HE SENT US a chatstate'''
num_unread = self.nb_unread
has_focus = self.parent_win.window.get_property('has-toplevel-focus')
2006-01-01 20:40:05 +01:00
current_tab = self.parent_win.get_active_control() == self
color = None
theme = gajim.config.get('roster_theme')
if chatstate == 'attention' and (not has_focus or not current_tab):
2006-01-06 02:54:33 +01:00
attention_flag = True
2006-01-01 20:40:05 +01:00
color = gajim.config.get_per('themes', theme,
'state_muc_directed_msg')
elif chatstate:
if chatstate == 'active' or (current_tab and has_focus):
2006-01-06 02:54:33 +01:00
attention_flag = False
2006-01-01 20:40:05 +01:00
color = gajim.config.get_per('themes', theme,
'state_active_color')
elif chatstate == 'newmsg' and (not has_focus or not current_tab) and\
2006-01-06 02:54:33 +01:00
not self.attention_flag:
2006-01-01 20:40:05 +01:00
color = gajim.config.get_per('themes', theme, 'state_muc_msg')
if color:
color = gtk.gdk.colormap_get_system().alloc_color(color)
2006-01-05 06:51:28 +01:00
if self.parent_win.get_active_control() != self:
color = self.lighten_color(color)
2006-01-01 20:40:05 +01:00
2006-01-05 06:51:28 +01:00
label_str = self.name
2006-01-01 20:40:05 +01:00
if num_unread: # if unread, text in the label becomes bold
label_str = '<b>' + str(num_unread) + label_str + '</b>'
return (label_str, color)
2006-01-05 06:51:28 +01:00
def get_tab_image(self):
# Set tab image (always 16x16); unread messages show the 'message' image
img_16 = gajim.interface.roster.get_appropriate_state_images(self.room_jid)
# nb_unread is the number directed messages (msgs that mention our nick)
tab_image = None
if self.nb_unread and gajim.config.get('show_unread_tab_icon'):
tab_image = img_16['message']
else:
tab_image = img_16['muc_active']
return tab_image
2006-01-02 10:04:30 +01:00
def prepare_context_menu(self):
'''sets compact view menuitem active state
sets active and sensitivity state for toggle_gpg_menuitem
and remove possible 'Switch to' menuitems'''
menu = self.gc_popup_menu
childs = menu.get_children()
# compact_view_menuitem
childs[5].set_active(self.compact_view_current_state)
menu = self.remove_possible_switch_to_menuitems(menu)
return menu
2006-01-05 06:51:28 +01:00
def on_message(self, nick, msg, tim):
if not nick:
# message from server
self.print_conversation(msg, tim = tim)
else:
# message from someone
self.print_conversation(msg, nick, tim)
def on_private_message(self, nick, msg, tim):
# Do we have a queue?
fjid = self.room_jid + '/' + nick
qs = gajim.awaiting_events[self.account]
no_queue = True
if qs.has_key(fjid):
no_queue = False
# We print if window is opened
2006-01-06 04:36:07 +01:00
pm_control = gajim.interface.msg_win_mgr.get_control(fjid)
2006-01-05 06:51:28 +01:00
if pm_control:
pm_control.print_conversation(msg, tim = tim)
return
if no_queue:
qs[fjid] = []
qs[fjid].append(('chat', (msg, '', 'incoming', tim, False, '')))
autopopup = gajim.config.get('autopopup')
autopopupaway = gajim.config.get('autopopupaway')
iter = self.get_contact_iter(nick)
path = self.list_treeview.get_model().get_path(iter)
if not autopopup or (not autopopupaway and \
gajim.connections[self.account].connected > 2):
if no_queue: # We didn't have a queue: we change icons
model = self.list_treeview.get_model()
state_images =\
gajim.interface.roster.get_appropriate_state_images(self.room_jid)
image = state_images['message']
model[iter][C_IMG] = image
if gajim.interface.systray_enabled:
gajim.interface.systray.add_jid(fjid, self.account, 'pm')
self.parent_win.show_title()
else:
gc_c = gajim.contacts.get_gc_contact(self.account, self.room_jid, nick)
c = gajim.contacts.contact_from_gc_contact(gc_c)
gajim.interface.roster.new_chat(c, self.account)
2006-01-05 06:51:28 +01:00
# Scroll to line
self.list_treeview.expand_row(path[0:1], False)
self.list_treeview.scroll_to_cell(path)
self.list_treeview.set_cursor(path)
def get_contact_iter(self, nick):
model = self.list_treeview.get_model()
fin = False
role_iter = model.get_iter_root()
if not role_iter:
return None
while not fin:
fin2 = False
user_iter = model.iter_children(role_iter)
if not user_iter:
fin2 = True
while not fin2:
if nick == model[user_iter][C_NICK].decode('utf-8'):
return user_iter
user_iter = model.iter_next(user_iter)
if not user_iter:
fin2 = True
role_iter = model.iter_next(role_iter)
if not role_iter:
fin = True
return None
def print_conversation(self, text, contact = '', tim = None):
'''Print a line in the conversation:
if contact is set: it's a message from someone
if contact is not set: it's a message from the server or help'''
if isinstance(text, str):
text = unicode(text, 'utf-8')
other_tags_for_name = []
other_tags_for_text = []
if contact:
if contact == self.nick: # it's us
kind = 'outgoing'
else:
kind = 'incoming'
# muc-specific chatstate
self.parent_win.redraw_tab(self.contact, 'newmsg')
else:
kind = 'status'
if kind == 'incoming': # it's a message NOT from us
# highlighting and sounds
(highlight, sound) = self.highlighting_for_message(text, tim)
if highlight:
self.redraw_tab(self.contact, 'attention') # muc-specific chatstate
other_tags_for_name.append('bold')
other_tags_for_text.append('marked')
if sound == 'received':
helpers.play_sound('muc_message_received')
elif sound == 'highlight':
helpers.play_sound('muc_message_highlight')
self.check_and_possibly_add_focus_out_line()
ChatControlBase.print_conversation_line(self, text, kind, contact, tim,
other_tags_for_name, [], other_tags_for_text)
def highlighting_for_message(self, text, tim):
'''Returns a 2-Tuple. The first says whether or not to highlight the
text, the second, what sound to play.'''
highlight, sound = (None, None)
# Do we play a sound on every muc message?
if gajim.config.get_per('soundevents', 'muc_message_received', 'enabled'):
if gajim.config.get('notify_on_all_muc_messages'):
sound = 'received'
# Are any of the defined highlighting words in the text?
if self.needs_visual_notification(text):
highlight = True
if gajim.config.get_per('soundevents', 'muc_message_highlight',
'enabled'):
sound = 'highlight'
# Is it a history message? Don't want sound-floods when we join.
if tim != time.localtime():
sound = None
return (highlight, sound)
def check_and_possibly_add_focus_out_line(self):
'''checks and possibly adds focus out line for room_jid if it needs it
and does not already have it as last event. If it goes to add this line
it removes previous line first'''
win = gajim.interface.msg_win_mgr.get_window(self.room_jid)
if self.room_jid == win.get_active_jid() and\
win.window.get_property('has-toplevel-focus'):
# it's the current room and it's the focused window.
# we have full focus (we are reading it!)
return
if not self.allow_focus_out_line:
# if room did not receive focus-in from the last time we added
# --- line then do not readd
return
print_focus_out_line = False
buffer = self.conv_textview.get_buffer()
if self.focus_out_end_iter_offset is None:
# this happens only first time we focus out on this room
print_focus_out_line = True
else:
if self.focus_out_end_iter_offset != buffer.get_end_iter().get_offset():
# this means after last-focus something was printed
# (else end_iter's offset is the same as before)
# only then print ---- line (eg. we avoid printing many following
# ---- lines)
print_focus_out_line = True
if print_focus_out_line and buffer.get_char_count() > 0:
buffer.begin_user_action()
# remove previous focus out line if such focus out line exists
if self.focus_out_end_iter_offset is not None:
end_iter_for_previous_line = buffer.get_iter_at_offset(
self.focus_out_end_iter_offset)
begin_iter_for_previous_line = end_iter_for_previous_line.copy()
begin_iter_for_previous_line.backward_chars(2) # img_char+1 (the '\n')
# remove focus out line
buffer.delete(begin_iter_for_previous_line,
end_iter_for_previous_line)
# add the new focus out line
# FIXME: Why is this loaded from disk everytime
path_to_file = os.path.join(gajim.DATA_DIR, 'pixmaps', 'muc_separator.png')
focus_out_line_pixbuf = gtk.gdk.pixbuf_new_from_file(path_to_file)
end_iter = buffer.get_end_iter()
buffer.insert(end_iter, '\n')
buffer.insert_pixbuf(end_iter, focus_out_line_pixbuf)
end_iter = buffer.get_end_iter()
before_img_iter = end_iter.copy()
before_img_iter.backward_char() # one char back (an image also takes one char)
buffer.apply_tag_by_name('focus-out-line', before_img_iter, end_iter)
#FIXME: remove this workaround when bug is fixed
# c http://bugzilla.gnome.org/show_bug.cgi?id=318569
self.allow_focus_out_line = False
# update the iter we hold to make comparison the next time
self.focus_out_end_iter_offset = buffer.get_end_iter().get_offset()
buffer.end_user_action()
# scroll to the end (via idle in case the scrollbar has appeared)
gobject.idle_add(self.conv_textview.scroll_to_end)
def needs_visual_notification(self, text):
'''checks text to see whether any of the words in (muc_highlight_words
and nick) appear.'''
special_words = gajim.config.get('muc_highlight_words').split(';')
special_words.append(self.nick)
# Strip empties: ''.split(';') == [''] and would highlight everything.
# Also lowercase everything for case insensitive compare.
special_words = [word.lower() for word in special_words if word]
text = text.lower()
text_splitted = text.split()
for word in text_splitted: # get each word of the text
for special_word in special_words:
if word.startswith(special_word):
return True
return False
2006-01-06 02:54:33 +01:00
def set_subject(self, subject):
self.subject= subject
full_subject = None
subject = gtkgui_helpers.reduce_chars_newlines(subject, 0, 2)
subject = gtkgui_helpers.escape_for_pango_markup(subject)
self.name_label.set_markup(
'<span weight="heavy" size="x-large">%s</span>\n%s' % (self.room_jid, subject))
2006-01-06 04:36:07 +01:00
event_box = self.name_label.get_parent()
2006-01-06 02:54:33 +01:00
if subject == '':
subject = _('This room has no subject')
if full_subject is not None:
subject = full_subject # tooltip must always hold ALL the subject
self.subject_tooltip.set_tip(event_box, subject)
def save_var(self):
return {
'nick': self.nick,
'model': self.list_treeview.get_model(),
'subject': self.subject,
}
def load_var(self, room_jid, var):
self.list_treeview.set_model(var['model'])
self.list_treeviewexpand_all()
self.set_subject(var['subject'])
self.subject= var['subject']
if gajim.gc_connected[self.account][room_jid]:
self.got_connected()
def got_connected(self):
gajim.gc_connected[self.account][self.room_jid] = True
self.msg_textview.set_sensitive(True)
self.xml.get_widget('send_button').set_sensitive(True)
def got_disconnected(self):
model = self.list_treeview.get_model()
model.clear()
nick_list = gajim.contacts.get_nick_list(self.account, self.room_jid)
for nick in nick_list:
gc_contact = gajim.contacts.get_gc_contact(self.account, self.room_jid,
nick)
gajim.contacts.remove_gc_contact(self.account, gc_contact)
gajim.gc_connected[self.account][self.room_jid] = False
self.msg_textview.set_sensitive(False)
self.xml.get_widget('send_button').set_sensitive(False)
2006-01-06 04:36:07 +01:00
def draw_widgets(self):
ChatControlBase.draw_widgets(self)
self.draw_roster()
def draw_roster(self):
model = self.list_treeview.get_model()
model.clear()
print "draw_roster"
for nick in gajim.contacts.get_nick_list(self.account, self.room_jid):
gc_contact = gajim.contacts.get_gc_contact(self.account, self.room_jid, nick)
self.add_contact_to_roster(self.room_jid, nick, gc_contact.show,
gc_contact.role, gc_contact.affiliation, gc_contact.status,
gc_contact.jid)
2006-01-06 02:54:33 +01:00
def draw_contact(self, nick, selected=False, focus=False):
2006-01-06 04:36:07 +01:00
iter = self.get_contact_iter(nick)
2006-01-06 02:54:33 +01:00
if not iter:
return
model = self.list_treeview.get_model()
gc_contact = gajim.contacts.get_gc_contact(self.account, self.room_jid, nick)
state_images = gajim.interface.roster.jabber_state_images['16']
if gajim.awaiting_events[self.account].has_key(self.room_jid + '/' + nick):
image = state_images['message']
else:
image = state_images[gc_contact.show]
name = gtkgui_helpers.escape_for_pango_markup(gc_contact.name)
status = gc_contact.status
# add status msg, if not empty, under contact name in the treeview
if status and gajim.config.get('show_status_msgs_in_roster'):
status = status.strip()
if status != '':
status = gtkgui_helpers.reduce_chars_newlines(status, max_lines = 1)
# escape markup entities and make them small italic and fg color
color = gtkgui_helpers._get_fade_color(self.list_treeview,
selected, focus)
colorstring = "#%04x%04x%04x" % (color.red, color.green, color.blue)
name += '\n' '<span size="small" style="italic" foreground="%s">%s</span>'\
% (colorstring, gtkgui_helpers.escape_for_pango_markup(status))
model[iter][C_IMG] = image
model[iter][C_TEXT] = name
2006-01-06 04:36:07 +01:00
def chg_contact_status(self, nick, show, status, role, affiliation, jid, reason, actor,
statusCode, new_nick):
'''When an occupant changes his or her status'''
if show == 'invisible':
return
if not role:
role = 'visitor'
if not affiliation:
affiliation = 'none'
if show in ('offline', 'error'):
if statusCode == '307':
if actor is None: # do not print 'kicked by None'
s = _('%(nick)s has been kicked: %(reason)s') % {
'nick': nick,
'reason': reason }
else:
s = _('%(nick)s has been kicked by %(who)s: %(reason)s') % {
'nick': nick,
'who': actor,
'reason': reason }
self.print_conversation(s)
elif statusCode == '301':
if actor is None: # do not print 'banned by None'
s = _('%(nick)s has been banned: %(reason)s') % {
'nick': nick,
'reason': reason }
else:
s = _('%(nick)s has been banned by %(who)s: %(reason)s') % {
'nick': nick,
'who': actor,
'reason': reason }
self.print_conversation(s, self.room_jid)
elif statusCode == '303': # Someone changed his or her nick
if nick == self.nick: # We changed our nick
self.nick = new_nick
s = _('You are now known as %s') % new_nick
else:
s = _('%s is now known as %s') % (nick, new_nick)
self.print_conversation(s)
if not gajim.awaiting_events[self.account].has_key(self.room_jid + '/' + nick):
self.remove_contact(nick)
else:
c = gajim.contacts.get_gc_contact(self.account, self.room_jid, nick)
c.show = show
c.status = status
if nick == self.nick and statusCode != '303': # We became offline
self.got_disconnected()
else:
iter = self.get_contact_iter(nick)
if not iter:
iter = self.add_contact_to_roster(nick, show, role,
affiliation, status, jid)
else:
actual_role = self.get_role(nick)
if role != actual_role:
self.remove_contact(nick)
self.add_contact_to_roster(nick, show, role,
affiliation, status, jid)
else:
c = gajim.contacts.get_gc_contact(self.account, self.room_jid, nick)
if c.show == show and c.status == status and \
c.affiliation == affiliation: #no change
return
c.show = show
c.affiliation = affiliation
c.status = status
self.draw_contact(nick)
if (time.time() - self.room_creation) > 30 and \
nick != self.nick and statusCode != '303':
if show == 'offline':
st = _('%s has left') % nick
else:
st = _('%s is now %s') % (nick, helpers.get_uf_show(show))
if status:
st += ' (' + status + ')'
self.print_conversation(st)
def add_contact_to_roster(self, nick, show, role, affiliation, status, jid = ''):
model = self.list_treeview.get_model()
role_name = helpers.get_uf_role(role, plural = True)
resource = ''
if jid:
jids = jid.split('/', 1)
j = jids[0]
if len(jids) > 1:
resource = jids[1]
else:
j = ''
name = nick
role_iter = self.get_role_iter(role)
if not role_iter:
role_iter = model.append(None,
(gajim.interface.roster.jabber_state_images['16']['closed'], 'role', role,
'<b>%s</b>' % role_name))
iter = model.append(role_iter, (None, 'contact', nick, name))
if not nick in gajim.contacts.get_nick_list(self.account, self.room_jid):
gc_contact = gajim.contacts.create_gc_contact(room_jid = self.room_jid,
name = nick, show = show, status = status, role = role,
affiliation = affiliation, jid = j, resource = resource)
gajim.contacts.add_gc_contact(self.account, gc_contact)
self.draw_contact(nick)
if nick == self.nick: # we became online
self.got_connected()
self.list_treeview.expand_row((model.get_path(role_iter)), False)
return iter
def get_role_iter(self, role):
model = self.list_treeview.get_model()
fin = False
iter = model.get_iter_root()
if not iter:
return None
while not fin:
role_name = model[iter][C_NICK].decode('utf-8')
if role == role_name:
return iter
iter = model.iter_next(iter)
if not iter:
fin = True
return None
def remove_contact(self, nick):
'''Remove a user from the contacts_list'''
model = self.list_treeview.get_model()
iter = self.get_contact_iter(nick)
if not iter:
return
gc_contact = gajim.contacts.get_gc_contact(self.account, self.room_jid, nick)
if gc_contact:
gajim.contacts.remove_gc_contact(self.account, gc_contact)
parent_iter = model.iter_parent(iter)
model.remove(iter)
if model.iter_n_children(parent_iter) == 0:
model.remove(parent_iter)