home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2011 October / maximum-cd-2011-10.iso / DiscContents / digsby_setup.exe / lib / gui / imwin / messagearea.pyo (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2011-06-22  |  8.4 KB  |  230 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyo (Python 2.6)
  3.  
  4. from __future__ import with_statement
  5. from datetime import datetime
  6. from gui.imwin.styles.stripformatting import strip
  7. from gui.browser.webkit import WebKitWindow
  8. from gui.toolbox.scrolling import WheelScrollCtrlZoomMixin, WheelShiftScrollFastMixin, ScrollWinMixin, FrozenLoopScrollMixin
  9. from common import pref, prefprop
  10. from util import Storage as S, takemany, preserve_whitespace, traceguard
  11. from util.primitives.misc import toutc, fromutc
  12. from logging import getLogger
  13. log = getLogger('msgarea')
  14. AUTORESP = _('[Auto-Response] ')
  15.  
  16. class quiet_log_messages(object):
  17.     messages = set()
  18.     
  19.     def __init__(self, messages):
  20.         self._messages = messages
  21.  
  22.     
  23.     def __enter__(self):
  24.         quiet_log_messages.messages = set(self._messages)
  25.  
  26.     
  27.     def __exit__(self, exc_type, exc_val, exc_tb):
  28.         quiet_log_messages.messages = set()
  29.  
  30.  
  31.  
  32. def history_enabled():
  33.     if pref('conversation_window.show_history', True):
  34.         pass
  35.     return pref('log.ims', True)
  36.  
  37.  
  38. def should_show_time(tstamp1, tstamp2):
  39.     return fromutc(tstamp1).date() != fromutc(tstamp2).date()
  40.  
  41.  
  42. try:
  43.     import webview
  44. except:
  45.     import wx.webview as webview
  46.  
  47.  
  48. class MessageArea(FrozenLoopScrollMixin, ScrollWinMixin, WheelShiftScrollFastMixin, WheelScrollCtrlZoomMixin, WebKitWindow):
  49.     
  50.     def __init__(self, parent, header_enabled = True, prevent_align_to_bottom = False):
  51.         super(MessageArea, self).__init__(parent)
  52.         self.inited = False
  53.         self.header_enabled = header_enabled
  54.         self.prevent_align_to_bottom = prevent_align_to_bottom
  55.         self.Bind(webview.EVT_WEBVIEW_BEFORE_LOAD, self._before_load)
  56.  
  57.     date_context_format = '%A, %B %d, %Y'
  58.     show_fonts = prefprop('appearance.conversations.show_message_fonts', True)
  59.     show_colors = prefprop('appearance.conversations.show_message_colors', True)
  60.     show_emoticons = prefprop('appearance.conversations.emoticons.enabled', True)
  61.     htmlize_links = prefprop('appearance.conversations.htmlize_links', True)
  62.     
  63.     def _before_load(self, e):
  64.         e.Skip()
  65.         if e.NavigationType == webview.WEBVIEW_NAV_LINK_CLICKED:
  66.             url = e.URL
  67.             if url.startswith('digsby://'):
  68.                 urlhandler = urlhandler
  69.                 import common
  70.                 handle_result = urlhandler.handle(url)
  71.                 if handle_result.cancel_navigation:
  72.                     e.Cancel()
  73.                 elif url != handle_result.url:
  74.                     e.SetURL(url)
  75.                 
  76.             
  77.         
  78.  
  79.     
  80.     def init_content(self, theme, chatName = None, buddy = None, show_history = None, prevent_align_to_bottom = False):
  81.         self.theme = theme
  82.         now = datetime.now()
  83.         self.tstamp = toutc(datetime(now.year, now.month, now.day))
  84.         if self.header_enabled:
  85.             pass
  86.         show_header = pref('appearance.conversations.show_header', False)
  87.         initialContents = theme.initialContents(chatName, buddy, show_header, prevent_align_to_bottom = prevent_align_to_bottom)
  88.         self.SetPageSource(initialContents, theme.baseUrl)
  89.         if show_history is None:
  90.             show_history = True
  91.         
  92.         self.inited = True
  93.  
  94.     
  95.     def show_history(self, buddy):
  96.         num_lines = max(0, pref('conversation_window.num_lines', 5, int))
  97.         if num_lines > 0:
  98.             logsource = buddy
  99.             if pref('conversation_window.merge_metacontact_history', False):
  100.                 profile = profile
  101.                 import common
  102.                 metacontact = profile.metacontacts.forbuddy(buddy)
  103.                 if metacontact:
  104.                     logsource = list(metacontact).pop()
  105.                 
  106.             
  107.             msgobjs = reversed(list(takemany(num_lines, logsource.history)))
  108.             self.replay_messages(msgobjs, buddy)
  109.         
  110.  
  111.     
  112.     def replay_messages(self, msgobjs, buddy, context = True):
  113.         next = False
  114.         oldname = None
  115.         olddirection = None
  116.         num_messages = 0
  117.         skipped_messages = 0
  118.         for msg in msgobjs:
  119.             num_messages += 1
  120.             if msg in quiet_log_messages.messages:
  121.                 skipped_messages += 1
  122.                 continue
  123.             
  124.             name = msg.buddy.name
  125.             direction = msg.type
  126.             if oldname == name:
  127.                 pass
  128.             next = direction == olddirection
  129.             msg.buddy = buddy_lookup(buddy, name)
  130.             msg.content_type = 'text/html'
  131.             self.format_message(direction, msg, next, context = context)
  132.             oldname = name
  133.             olddirection = direction
  134.         
  135.         log.info('replay_messages: %d total messages (%d skipped) for buddy %r', num_messages, skipped_messages, buddy)
  136.  
  137.     
  138.     def show_header(self, show):
  139.         return self.RunScript(self.theme.show_header_script(show))
  140.  
  141.     
  142.     def date_status(self, dt):
  143.         format_dt = fromutc(dt)
  144.         return S(message = format_dt.strftime(self.date_context_format), timestamp = dt, buddy = None, type = None)
  145.  
  146.     
  147.     def format_message(self, messagetype, messageobj, next = False, context = False):
  148.         if messagetype != 'status':
  149.             msgtime = messageobj.timestamp
  150.             if msgtime is None:
  151.                 msgtime = datetime.utcnow()
  152.             
  153.             if should_show_time(self.tstamp, msgtime):
  154.                 self.theme.set_always_show_timestamp(True)
  155.                 
  156.                 try:
  157.                     self.format_message('status', self.date_status(msgtime), next = False, context = context)
  158.                 finally:
  159.                     self.theme.set_always_show_timestamp(False)
  160.  
  161.                 next = False
  162.             
  163.             self.tstamp = msgtime
  164.         
  165.         content_type = getattr(messageobj, 'content_type', 'text/plain')
  166.         if content_type == 'text/plain':
  167.             messageobj.message = messageobj.message.encode('xml')
  168.             messageobj.message = preserve_whitespace(messageobj.message)
  169.             messageobj.content_type = 'text/html'
  170.         
  171.         if self.theme.allow_text_colors:
  172.             pass
  173.         show_colors = self.show_colors
  174.         show_emoticons = None if self.show_emoticons else None
  175.         transforms = dict(emoticons = show_emoticons, links = self.htmlize_links, spaces = True)
  176.         if not getattr(messageobj, 'linkify', True):
  177.             transforms['links'] = False
  178.         
  179.         (stripped, strip_values) = strip(messageobj.message, formatting = not (self.show_fonts), colors = not show_colors, plaintext_transforms = transforms)
  180.         messageobj = messageobj.copy()
  181.         messageobj.message = stripped
  182.         if getattr(messageobj, 'has_autotext', False):
  183.             extra = { }
  184.         else:
  185.             extra = dict(has_autotext = True, autotext = AUTORESP)
  186.         if self.show_colors:
  187.             extra.update(handle_colors(strip_values))
  188.         
  189.         (func, msg) = self.theme.format_message(messagetype, messageobj, next, context, **extra)
  190.         if func:
  191.             script = "%s('%s');" % (func, js_escape(msg))
  192.             self.RunScript(script)
  193.         
  194.  
  195.  
  196.  
  197. def handle_colors(vals):
  198.     bodycolor = None
  199.     if 'bgcolor' in vals:
  200.         bodycolor = vals['bgcolor'][0]
  201.     elif 'back' in vals:
  202.         bodycolor = vals['back'][0]
  203.     
  204.     if bodycolor:
  205.         return {
  206.             'textbackgroundcolor': bodycolor }
  207.     return { }
  208.  
  209.  
  210. def js_escape(msg):
  211.     return msg.replace('\\', '\\\\').replace('\n', '\\\n').replace('\r', '\\\r').replace("'", ''')
  212.  
  213.  
  214. def buddy_lookup(buddy, name):
  215.     protocol = getattr(buddy, 'protocol', None)
  216.     if protocol is not None and hasattr(protocol, 'buddies') and protocol.buddies:
  217.         
  218.         try:
  219.             buddy = buddy.protocol.get_buddy(name)
  220.         except Exception:
  221.             import traceback
  222.             traceback.print_exc_once()
  223.         except:
  224.             None<EXCEPTION MATCH>Exception
  225.         
  226.  
  227.     None<EXCEPTION MATCH>Exception
  228.     return buddy
  229.  
  230.