home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2011 February / maximum-cd-2011-02.iso / DiscContents / digsby_setup85.exe / lib / gui / imwin / messagearea.pyo (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2010-11-24  |  8.3 KB  |  229 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):
  51.         super(MessageArea, self).__init__(parent)
  52.         self.inited = False
  53.         self.header_enabled = header_enabled
  54.         self.Bind(webview.EVT_WEBVIEW_BEFORE_LOAD, self._before_load)
  55.  
  56.     date_context_format = '%A, %B %d, %Y'
  57.     show_fonts = prefprop('appearance.conversations.show_message_fonts', True)
  58.     show_colors = prefprop('appearance.conversations.show_message_colors', True)
  59.     show_emoticons = prefprop('appearance.conversations.emoticons.enabled', True)
  60.     htmlize_links = prefprop('appearance.conversations.htmlize_links', True)
  61.     
  62.     def _before_load(self, e):
  63.         e.Skip()
  64.         if e.NavigationType == webview.WEBVIEW_NAV_LINK_CLICKED:
  65.             url = e.URL
  66.             if url.startswith('digsby://'):
  67.                 urlhandler = urlhandler
  68.                 import common
  69.                 handle_result = urlhandler.handle(url)
  70.                 if handle_result.cancel_navigation:
  71.                     e.Cancel()
  72.                 elif url != handle_result.url:
  73.                     e.SetURL(url)
  74.                 
  75.             
  76.         
  77.  
  78.     
  79.     def init_content(self, theme, chatName = None, buddy = None, show_history = None):
  80.         self.theme = theme
  81.         now = datetime.now()
  82.         self.tstamp = toutc(datetime(now.year, now.month, now.day))
  83.         if self.header_enabled:
  84.             pass
  85.         show_header = pref('appearance.conversations.show_header', False)
  86.         initialContents = theme.initialContents(chatName, buddy, show_header)
  87.         self.SetPageSource(initialContents, theme.baseUrl)
  88.         if show_history is None:
  89.             show_history = True
  90.         
  91.         self.inited = True
  92.  
  93.     
  94.     def show_history(self, buddy):
  95.         num_lines = max(0, pref('conversation_window.num_lines', 5, int))
  96.         if num_lines > 0:
  97.             logsource = buddy
  98.             if pref('conversation_window.merge_metacontact_history', False):
  99.                 profile = profile
  100.                 import common
  101.                 metacontact = profile.metacontacts.forbuddy(buddy)
  102.                 if metacontact:
  103.                     logsource = list(metacontact).pop()
  104.                 
  105.             
  106.             msgobjs = reversed(list(takemany(num_lines, logsource.history)))
  107.             self.replay_messages(msgobjs, buddy)
  108.         
  109.  
  110.     
  111.     def replay_messages(self, msgobjs, buddy, context = True):
  112.         next = False
  113.         oldname = None
  114.         olddirection = None
  115.         num_messages = 0
  116.         skipped_messages = 0
  117.         for msg in msgobjs:
  118.             num_messages += 1
  119.             if msg in quiet_log_messages.messages:
  120.                 skipped_messages += 1
  121.                 continue
  122.             
  123.             name = msg.buddy.name
  124.             direction = msg.type
  125.             if oldname == name:
  126.                 pass
  127.             next = direction == olddirection
  128.             msg.buddy = buddy_lookup(buddy, name)
  129.             msg.content_type = 'text/html'
  130.             self.format_message(direction, msg, next, context = context)
  131.             oldname = name
  132.             olddirection = direction
  133.         
  134.         log.info('replay_messages: %d total messages (%d skipped) for buddy %r', num_messages, skipped_messages, buddy)
  135.  
  136.     
  137.     def show_header(self, show):
  138.         return self.RunScript(self.theme.show_header_script(show))
  139.  
  140.     
  141.     def date_status(self, dt):
  142.         format_dt = fromutc(dt)
  143.         return S(message = format_dt.strftime(self.date_context_format), timestamp = dt, buddy = None, type = None)
  144.  
  145.     
  146.     def format_message(self, messagetype, messageobj, next = False, context = False):
  147.         if messagetype != 'status':
  148.             msgtime = messageobj.timestamp
  149.             if msgtime is None:
  150.                 msgtime = datetime.utcnow()
  151.             
  152.             if should_show_time(self.tstamp, msgtime):
  153.                 self.theme.set_always_show_timestamp(True)
  154.                 
  155.                 try:
  156.                     self.format_message('status', self.date_status(msgtime), next = False, context = context)
  157.                 finally:
  158.                     self.theme.set_always_show_timestamp(False)
  159.  
  160.                 next = False
  161.             
  162.             self.tstamp = msgtime
  163.         
  164.         content_type = getattr(messageobj, 'content_type', 'text/plain')
  165.         if content_type == 'text/plain':
  166.             messageobj.message = messageobj.message.encode('xml')
  167.             messageobj.message = preserve_whitespace(messageobj.message)
  168.             messageobj.content_type = 'text/html'
  169.         
  170.         if self.theme.allow_text_colors:
  171.             pass
  172.         show_colors = self.show_colors
  173.         show_emoticons = None if self.show_emoticons else None
  174.         transforms = dict(emoticons = show_emoticons, links = self.htmlize_links, spaces = True)
  175.         if not getattr(messageobj, 'linkify', True):
  176.             transforms['links'] = False
  177.         
  178.         (stripped, strip_values) = strip(messageobj.message, formatting = not (self.show_fonts), colors = not show_colors, plaintext_transforms = transforms)
  179.         messageobj = messageobj.copy()
  180.         messageobj.message = stripped
  181.         if getattr(messageobj, 'has_autotext', False):
  182.             extra = { }
  183.         else:
  184.             extra = dict(has_autotext = True, autotext = AUTORESP)
  185.         if self.show_colors:
  186.             extra.update(handle_colors(strip_values))
  187.         
  188.         (func, msg) = self.theme.format_message(messagetype, messageobj, next, context, **extra)
  189.         if func:
  190.             script = "%s('%s');" % (func, js_escape(msg))
  191.             self.RunScript(script)
  192.         
  193.  
  194.  
  195.  
  196. def handle_colors(vals):
  197.     bodycolor = None
  198.     if 'bgcolor' in vals:
  199.         bodycolor = vals['bgcolor'][0]
  200.     elif 'back' in vals:
  201.         bodycolor = vals['back'][0]
  202.     
  203.     if bodycolor:
  204.         return {
  205.             'textbackgroundcolor': bodycolor }
  206.     return { }
  207.  
  208.  
  209. def js_escape(msg):
  210.     return msg.replace('\\', '\\\\').replace('\n', '\\\n').replace('\r', '\\\r').replace("'", ''')
  211.  
  212.  
  213. def buddy_lookup(buddy, name):
  214.     protocol = getattr(buddy, 'protocol', None)
  215.     if protocol is not None and hasattr(protocol, 'buddies') and protocol.buddies:
  216.         
  217.         try:
  218.             buddy = buddy.protocol.get_buddy(name)
  219.         except Exception:
  220.             import traceback
  221.             traceback.print_exc_once()
  222.         except:
  223.             None<EXCEPTION MATCH>Exception
  224.         
  225.  
  226.     None<EXCEPTION MATCH>Exception
  227.     return buddy
  228.  
  229.