From 2452706b909364178655c6c918a0348fb4298fb2 Mon Sep 17 00:00:00 2001 From: mathieui Date: Sat, 20 Dec 2014 23:13:26 +0100 Subject: Add a deterministic_nick_colors option (default: true) --- src/config.py | 1 + src/tabs/muctab.py | 19 +++++++++++++++++-- src/user.py | 14 ++++++++++++-- 3 files changed, 30 insertions(+), 4 deletions(-) (limited to 'src') diff --git a/src/config.py b/src/config.py index 86aae8ef..339e8a85 100644 --- a/src/config.py +++ b/src/config.py @@ -42,6 +42,7 @@ DEFAULT_CONFIG = { 'custom_host': '', 'custom_port': '', 'default_nick': '', + 'deterministic_nick_colors': True, 'display_activity_notifications': False, 'display_gaming_notifications': False, 'display_mood_notifications': False, diff --git a/src/tabs/muctab.py b/src/tabs/muctab.py index 369ec3e5..72478bd4 100644 --- a/src/tabs/muctab.py +++ b/src/tabs/muctab.py @@ -401,6 +401,19 @@ class MucTab(ChatTab): /recolor [random] Re-assign color to the participants of the room """ + deterministic = config.get_by_tabname('deterministic_nick_colors', self.name) + if deterministic: + for user in self.users: + if user.nick == self.own_nick: + continue + user.set_deterministic_color() + if args[0] == 'random': + self.core.information(_('"random" was provided, but poezio is ' + 'configured to use deterministic colors'), + 'Warning') + self.user_win.refresh(self.users) + self.input.refresh() + return compare_users = lambda x: x.last_talked users = list(self.users) sorted_users = sorted(users, key=compare_users, reverse=True) @@ -995,12 +1008,13 @@ class MucTab(ChatTab): role = presence['muc']['role'] jid = presence['muc']['jid'] typ = presence['type'] + deterministic = config.get_by_tabname('deterministic_nick_colors', self.name) if not self.joined: # user in the room BEFORE us. # ignore redondant presence message, see bug #1509 if (from_nick not in [user.nick for user in self.users] and typ != "unavailable"): new_user = User(from_nick, affiliation, show, - status, role, jid) + status, role, jid, deterministic) self.users.append(new_user) self.core.events.trigger('muc_join', presence, self) if '110' in status_codes or self.own_nick == from_nick: @@ -1136,8 +1150,9 @@ class MucTab(ChatTab): """ When a new user joins the groupchat """ + deterministic = config.get_by_tabname('deterministic_nick_colors', self.name) user = User(from_nick, affiliation, - show, status, role, jid) + show, status, role, jid, deterministic) self.users.append(user) hide_exit_join = config.get_by_tabname('hide_exit_join', self.general_jid) diff --git a/src/user.py b/src/user.py index 0d29569f..8b4ad94b 100644 --- a/src/user.py +++ b/src/user.py @@ -12,6 +12,7 @@ An user is a MUC participant, not a roster contact (see contact.py) from random import choice from datetime import timedelta, datetime +from hashlib import md5 from theming import get_theme @@ -27,14 +28,23 @@ class User(object): """ keep trace of an user in a Room """ - def __init__(self, nick, affiliation, show, status, role, jid): + def __init__(self, nick, affiliation, show, status, role, jid, deterministic=True): self.last_talked = datetime(1, 1, 1) # The oldest possible time self.update(affiliation, show, status, role) self.change_nick(nick) - self.color = choice(get_theme().LIST_COLOR_NICKNAMES) + if deterministic: + self.set_deterministic_color() + else: + self.color = choice(get_theme().LIST_COLOR_NICKNAMES) self.jid = jid self.chatstate = None + def set_deterministic_color(self): + theme = get_theme() + mod = len(theme.LIST_COLOR_NICKNAMES) + nick_pos = int(md5(self.nick.encode('utf-8')).hexdigest(), 16) % mod + self.color = theme.LIST_COLOR_NICKNAMES[nick_pos] + def update(self, affiliation, show, status, role): self.affiliation = affiliation self.show = show -- cgit v1.2.3