home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2009 June / maximum-cd-2009-06.iso / DiscContents / digsby_setup.exe / lib / gui / imwin / messagearea.pyo (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2009-02-26  |  6.3 KB  |  174 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyo (Python 2.5)
  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 common import pref, prefprop
  9. from util import Storage as S, takemany, toutc, fromutc, preserve_whitespace
  10. from logging import getLogger
  11. log = getLogger('msgarea')
  12. AUTORESP = _('[Auto-Response] ')
  13.  
  14. def history_enabled():
  15.     if pref('conversation_window.show_history', True):
  16.         pass
  17.     return pref('log.ims', True)
  18.  
  19.  
  20. def should_show_time(tstamp1, tstamp2):
  21.     return fromutc(tstamp1).date() != fromutc(tstamp2).date()
  22.  
  23.  
  24. try:
  25.     import webview
  26. except:
  27.     import wx.webview as webview
  28.  
  29.  
  30. class MessageArea(WebKitWindow):
  31.     
  32.     def __init__(self, parent, header_enabled = True):
  33.         WebKitWindow.__init__(self, parent)
  34.         self.inited = False
  35.         self.header_enabled = True
  36.         self.Bind(webview.EVT_WEBVIEW_BEFORE_LOAD, self._before_load)
  37.  
  38.     date_context_format = '%A, %B %d, %Y'
  39.     show_fonts = prefprop('appearance.conversations.show_message_fonts', True)
  40.     show_colors = prefprop('appearance.conversations.show_message_colors', True)
  41.     show_emoticons = prefprop('appearance.conversations.emoticons.enabled', True)
  42.     htmlize_links = prefprop('appearance.conversations.htmlize_links', True)
  43.     
  44.     def _before_load(self, e):
  45.         e.Skip()
  46.         if e.NavigationType == webview.WEBVIEW_NAV_LINK_CLICKED:
  47.             url = e.URL
  48.             if url.startswith('digsby://'):
  49.                 urlhandler = urlhandler
  50.                 import common
  51.                 handle_result = urlhandler.handle(url)
  52.                 if handle_result.cancel_navigation:
  53.                     e.Cancel()
  54.                 elif url != handle_result.url:
  55.                     e.SetURL(url)
  56.                 
  57.             
  58.         
  59.  
  60.     
  61.     def init_content(self, theme, chatName = None, buddy = None, show_history = True):
  62.         self.theme = theme
  63.         now = datetime.now()
  64.         self.tstamp = toutc(datetime(now.year, now.month, now.day))
  65.         if self.header_enabled:
  66.             pass
  67.         show_header = pref('appearance.conversations.show_header', False)
  68.         initialContents = theme.initialContents(chatName, buddy, show_header)
  69.         self.SetPageSource(initialContents, theme.baseUrl)
  70.         if show_history and history_enabled() and buddy is not None:
  71.             self.show_history(buddy)
  72.         
  73.         self.inited = True
  74.  
  75.     
  76.     def show_history(self, buddy):
  77.         num_lines = max(0, pref('conversation_window.num_lines', 5, int))
  78.         if num_lines > 0:
  79.             msgobjs = reversed(list(takemany(num_lines, buddy.history)))
  80.             self.replay_messages(msgobjs, buddy)
  81.         
  82.  
  83.     
  84.     def replay_messages(self, msgobjs, buddy, context = True):
  85.         next = False
  86.         oldname = None
  87.         olddirection = None
  88.         for msg in msgobjs:
  89.             name = msg.buddy.name
  90.             direction = msg.type
  91.             if oldname == name:
  92.                 pass
  93.             next = direction == olddirection
  94.             msg.buddy = buddy.protocol.get_buddy(name)
  95.             msg.content_type = 'text/html'
  96.             self.format_message(direction, msg, next, context = context)
  97.             oldname = name
  98.             olddirection = direction
  99.         
  100.  
  101.     
  102.     def show_header(self, show):
  103.         return self.RunScript(self.theme.show_header_script(show))
  104.  
  105.     
  106.     def date_status(self, dt):
  107.         format_dt = fromutc(dt)
  108.         return S(message = format_dt.strftime(self.date_context_format), timestamp = dt, buddy = None, type = None)
  109.  
  110.     
  111.     def format_message(self, messagetype, messageobj, next = False, context = False):
  112.         if messagetype != 'status':
  113.             msgtime = messageobj.timestamp
  114.             if msgtime is None:
  115.                 msgtime = datetime.utcnow()
  116.             
  117.             if should_show_time(self.tstamp, msgtime):
  118.                 self.theme.set_always_show_timestamp(True)
  119.                 
  120.                 try:
  121.                     self.format_message('status', self.date_status(msgtime), next = False, context = context)
  122.                 finally:
  123.                     self.theme.set_always_show_timestamp(False)
  124.  
  125.                 next = False
  126.             
  127.             self.tstamp = msgtime
  128.         
  129.         content_type = getattr(messageobj, 'content_type', 'text/plain')
  130.         if content_type == 'text/plain':
  131.             messageobj.message = messageobj.message.encode('xml')
  132.             messageobj.message = preserve_whitespace(messageobj.message)
  133.         
  134.         if self.theme.allow_text_colors:
  135.             pass
  136.         show_colors = self.show_colors
  137.         show_emoticons = None if self.show_emoticons else None
  138.         transforms = dict(emoticons = show_emoticons, links = self.htmlize_links, spaces = True)
  139.         (stripped, strip_values) = strip(messageobj.message, formatting = not (self.show_fonts), colors = not show_colors, plaintext_transforms = transforms)
  140.         messageobj = messageobj.copy()
  141.         messageobj.message = stripped
  142.         if getattr(messageobj, 'has_autotext', False):
  143.             extra = { }
  144.         else:
  145.             extra = dict(has_autotext = True, autotext = AUTORESP)
  146.         if self.show_colors:
  147.             extra.update(handle_colors(strip_values))
  148.         
  149.         (func, msg) = self.theme.format_message(messagetype, messageobj, next, context, **extra)
  150.         if func:
  151.             script = "%s('%s');" % (func, js_escape(msg))
  152.             self.RunScript(script)
  153.         
  154.  
  155.  
  156.  
  157. def handle_colors(vals):
  158.     bodycolor = None
  159.     if 'bgcolor' in vals:
  160.         bodycolor = vals['bgcolor'][0]
  161.     elif 'back' in vals:
  162.         bodycolor = vals['back'][0]
  163.     
  164.     if bodycolor:
  165.         return {
  166.             'textbackgroundcolor': bodycolor }
  167.     
  168.     return { }
  169.  
  170.  
  171. def js_escape(msg):
  172.     return msg.replace('\\', '\\\\').replace('\n', '\\\n').replace('\r', '\\\r').replace("'", ''')
  173.  
  174.