From 9b83c74487243f0c4b26b84cf8c0994e4819f0ee Mon Sep 17 00:00:00 2001 From: Yann Leboulanger Date: Fri, 6 Oct 2006 14:52:25 +0000 Subject: [PATCH] move gtkgui_helpers.reduce_chars_newlines to common/helpers.py. It's not GTK related. --- src/chat_control.py | 2 +- src/common/helpers.py | 29 ++++++++++++++++++++++++++++- src/groupchat_control.py | 4 ++-- src/gtkgui_helpers.py | 27 --------------------------- src/roster_window.py | 2 +- src/tooltips.py | 8 ++++---- 6 files changed, 36 insertions(+), 36 deletions(-) diff --git a/src/chat_control.py b/src/chat_control.py index 82e76867e..4b4592f8d 100644 --- a/src/chat_control.py +++ b/src/chat_control.py @@ -1045,7 +1045,7 @@ class ChatControl(ChatControlBase): status = contact.status if status is not None: banner_name_label.set_ellipsize(pango.ELLIPSIZE_END) - status = gtkgui_helpers.reduce_chars_newlines(status, max_lines = 2) + status = helpers.reduce_chars_newlines(status, max_lines = 2) status_escaped = gtkgui_helpers.escape_for_pango_markup(status) font_attrs, font_attrs_small = self.get_font_attrs() diff --git a/src/common/helpers.py b/src/common/helpers.py index 61235c1a0..ed8270f3e 100644 --- a/src/common/helpers.py +++ b/src/common/helpers.py @@ -817,6 +817,33 @@ def get_chat_control(account, contact): return None return gajim.interface.msg_win_mgr.get_control(contact.jid, account) +def reduce_chars_newlines(text, max_chars = 0, max_lines = 0): + '''Cut the chars after 'max_chars' on each line + and show only the first 'max_lines'. + If any of the params is not present (None or 0) the action + on it is not performed''' + + def _cut_if_long(string): + if len(string) > max_chars: + string = string[:max_chars - 3] + '...' + return string + + if isinstance(text, str): + text = text.decode('utf-8') + + if max_lines == 0: + lines = text.split('\n') + else: + lines = text.split('\n', max_lines)[:max_lines] + if max_chars > 0: + if lines: + lines = map(lambda e: _cut_if_long(e), lines) + if lines: + reduced_text = reduce(lambda e, e1: e + '\n' + e1, lines) + else: + reduced_text = '' + return reduced_text + def get_notification_icon_tooltip_text(): text = None unread_chat = gajim.events.get_nb_events(types = ['printed_chat', @@ -868,7 +895,7 @@ def get_notification_icon_tooltip_text(): text = _('Gajim') elif len(accounts) == 1: message = accounts[0]['status_line'] - message = gtkgui_helpers.reduce_chars_newlines(message, 100, 1) + message = reduce_chars_newlines(message, 100, 1) message = gtkgui_helpers.escape_for_pango_markup(message) text = _('Gajim - %s') % message else: diff --git a/src/groupchat_control.py b/src/groupchat_control.py index 980cce016..295f10f99 100644 --- a/src/groupchat_control.py +++ b/src/groupchat_control.py @@ -666,7 +666,7 @@ class GroupchatControl(ChatControlBase): self.subject = subject self.name_label.set_ellipsize(pango.ELLIPSIZE_END) - subject = gtkgui_helpers.reduce_chars_newlines(subject, max_lines = 2) + subject = helpers.reduce_chars_newlines(subject, max_lines = 2) subject = gtkgui_helpers.escape_for_pango_markup(subject) font_attrs, font_attrs_small = self.get_font_attrs() text = '%s' % (font_attrs, self.room_jid) @@ -735,7 +735,7 @@ class GroupchatControl(ChatControlBase): 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) + status = 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) diff --git a/src/gtkgui_helpers.py b/src/gtkgui_helpers.py index 574f95586..e8608c511 100644 --- a/src/gtkgui_helpers.py +++ b/src/gtkgui_helpers.py @@ -169,33 +169,6 @@ def get_default_font(): return None -def reduce_chars_newlines(text, max_chars = 0, max_lines = 0): - '''Cut the chars after 'max_chars' on each line - and show only the first 'max_lines'. - If any of the params is not present (None or 0) the action - on it is not performed''' - - def _cut_if_long(string): - if len(string) > max_chars: - string = string[:max_chars - 3] + '...' - return string - - if isinstance(text, str): - text = text.decode('utf-8') - - if max_lines == 0: - lines = text.split('\n') - else: - lines = text.split('\n', max_lines)[:max_lines] - if max_chars > 0: - if lines: - lines = map(lambda e: _cut_if_long(e), lines) - if lines: - reduced_text = reduce(lambda e, e1: e + '\n' + e1, lines) - else: - reduced_text = '' - return reduced_text - def escape_for_pango_markup(string): # escapes < > & ' " # for pango markup not to break diff --git a/src/roster_window.py b/src/roster_window.py index 07973c545..9fdb261c5 100644 --- a/src/roster_window.py +++ b/src/roster_window.py @@ -514,7 +514,7 @@ class RosterWindow: if contact.status and gajim.config.get('show_status_msgs_in_roster'): status = contact.status.strip() if status != '': - status = gtkgui_helpers.reduce_chars_newlines(status, max_lines = 1) + status = 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.tree, selected, focus) colorstring = "#%04x%04x%04x" % (color.red, color.green, color.blue) diff --git a/src/tooltips.py b/src/tooltips.py index 8beebd843..7fbdf4d2d 100644 --- a/src/tooltips.py +++ b/src/tooltips.py @@ -178,7 +178,7 @@ class StatusTable: if isinstance(status, str): status = unicode(status, encoding='utf-8') # reduce to 100 chars, 1 line - status = gtkgui_helpers.reduce_chars_newlines(status, 100, 1) + status = helpers.reduce_chars_newlines(status, 100, 1) str_status = gtkgui_helpers.escape_for_pango_markup(str_status) status = gtkgui_helpers.escape_for_pango_markup(status) str_status += ' - ' + status + '' @@ -233,7 +233,7 @@ class NotificationAreaTooltip(BaseTooltip, StatusTable): # there are possible pango TBs on 'set_markup' if isinstance(message, str): message = unicode(message, encoding = 'utf-8') - message = gtkgui_helpers.reduce_chars_newlines(message, 100, 1) + message = helpers.reduce_chars_newlines(message, 100, 1) message = gtkgui_helpers.escape_for_pango_markup(message) if gajim.con_types.has_key(acct['name']) and \ gajim.con_types[acct['name']] in ('tls', 'ssl'): @@ -296,7 +296,7 @@ class GCTooltip(BaseTooltip): status = contact.status.strip() if status != '': # escape markup entities - status = gtkgui_helpers.reduce_chars_newlines(status, 100, 5) + status = helpers.reduce_chars_newlines(status, 100, 5) status = '' +\ gtkgui_helpers.escape_for_pango_markup(status) + '' properties.append((status, None)) @@ -479,7 +479,7 @@ class RosterTooltip(NotificationAreaTooltip): if status: # reduce long status # (no more than 100 chars on line and no more than 5 lines) - status = gtkgui_helpers.reduce_chars_newlines(status, 100, 5) + status = helpers.reduce_chars_newlines(status, 100, 5) # escape markup entities. status = gtkgui_helpers.escape_for_pango_markup(status) properties.append(('%s' % status, None))