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

  1. # Source Generated with Decompyle++
  2. # File: in.pyo (Python 2.6)
  3.  
  4. import config
  5. import wx
  6. from wx import Point
  7. from gui.taskbar import DigsbyTaskBarIcon
  8. import common.actions as actions
  9. from common import pref
  10. from gui.toolbox import draw_tiny_text, Monitor, GetDoubleClickTime
  11. from util import try_this
  12. import social
  13. from traceback import print_exc
  14. from operator import itemgetter
  15. import common
  16. import protocols
  17.  
  18. class ITrayIconProvider(protocols.Interface):
  19.     
  20.     def tray_icon_class():
  21.         pass
  22.  
  23.  
  24.  
  25. class AccountTrayIconProvider(object):
  26.     protocols.advise(instancesProvide = [
  27.         ITrayIconProvider], asAdapterForTypes = [
  28.         common.AccountBase])
  29.     
  30.     def __init__(self, subject):
  31.         self.subject = subject
  32.  
  33.     
  34.     def tray_icon_class(self):
  35.         EmailAccount = EmailAccount
  36.         import common.emailaccount
  37.         MySpace = MyspaceAccount
  38.         import myspace.MyspaceAccount
  39.         acct = self.subject
  40.         if isinstance(acct, EmailAccount):
  41.             return EmailTrayIcon
  42.         if isinstance(acct, MySpace):
  43.             return MyspaceTrayIcon
  44.         if hasattr(acct, 'tray_icon_class'):
  45.             return acct.tray_icon_class()
  46.         if isinstance(acct, social.network):
  47.             return SocialAccountTrayIcon
  48.  
  49.  
  50.  
  51. def should_grey(acct):
  52.     return not isinstance(acct, social.network)
  53.  
  54. baseAccountTrayClass = DigsbyTaskBarIcon
  55. if config.platform == 'mac':
  56.     from gui.native.mac import macmenuicon
  57.     baseAccountTrayClass = macmenuicon.MenuBarIconDelegate
  58.  
  59.  
  60. class AccountTrayIcon(baseAccountTrayClass):
  61.     
  62.     def create(cls, acct, infobox):
  63.         trayClass = ITrayIconProvider(acct).tray_icon_class()
  64.         if config.platform == 'mac':
  65.             object = trayClass.alloc().init()
  66.             object.initWithAccount(acct, infobox)
  67.             return object
  68.         return trayClass(acct, infobox)
  69.  
  70.     create = classmethod(create)
  71.     
  72.     def __init__(self, acct, infobox = None):
  73.         self.acct = acct
  74.         self.infobox = infobox
  75.         UMenu = UMenu
  76.         import gui.uberwidgets.umenu
  77.         self._menu = UMenu(wx.FindWindowByName('Buddy List'), onshow = self.update_menu)
  78.         trayid = hash('accounttrayicon_' + acct.protocol + '_' + acct.name)
  79.         super(AccountTrayIcon, self).__init__(acct.icon, menu = self._menu, id = trayid)
  80.         self.Bind(wx.EVT_TASKBAR_LEFT_DOWN, self.on_click)
  81.         self.Bind(wx.EVT_TASKBAR_LEFT_DCLICK, self.on_double_click)
  82.         self.register_observers(acct, self.on_account_updated)
  83.         self.on_account_updated()
  84.  
  85.     
  86.     def update_menu(self, event = None):
  87.         self._menu.RemoveAllItems()
  88.         actions.menu(wx.FindWindowByName('Buddy List'), self.acct, cls = type(self.acct), menu = self._menu)
  89.  
  90.     
  91.     def on_click(self, e = None):
  92.         
  93.         try:
  94.             dclick_timer = self.dclick_timer
  95.         except AttributeError:
  96.             dclick_timer = self.dclick_timer = wx.PyTimer((lambda : pass))
  97.  
  98.         if dclick_timer.IsRunning():
  99.             self.on_double_click()
  100.             dclick_timer.Stop()
  101.         else:
  102.             dclick_timer.StartOneShot(GetDoubleClickTime())
  103.             self.show_infobox()
  104.         e.Skip()
  105.  
  106.     
  107.     def on_double_click(self, e = None):
  108.         if self.infobox.IsShown():
  109.             self.infobox.Hide()
  110.         
  111.         url = self.acct.DefaultAction()
  112.         if url is not None:
  113.             wx.LaunchDefaultBrowser(url)
  114.         
  115.  
  116.     
  117.     def show_infobox(self):
  118.         if not self.infobox:
  119.             return None
  120.         info = self.infobox
  121.         if info.IsShown() and getattr(info, 'account', None) is self.acct:
  122.             info.Hide()
  123.         else:
  124.             pt = self.get_infobox_tray_position()
  125.             info.ShowFromTray(pt, self.acct)
  126.             wx.CallAfter(info.do_focus)
  127.  
  128.     
  129.     def get_infobox_tray_position(self):
  130.         
  131.         try:
  132.             import cgui
  133.             r = cgui.GetTrayRect()
  134.             pt = Point(r.Right - r.Width / 2, r.Bottom - r.Height / 2)
  135.             display = Monitor.GetFromPoint(pt, find_near = True)
  136.             rect = display.GetClientArea()
  137.             distances = []
  138.             for p in ('TopLeft', 'TopRight', 'BottomLeft', 'BottomRight'):
  139.                 corner = getattr(rect, p)
  140.                 distances.append((corner, corner.DistanceTo(pt), p))
  141.             
  142.             distances.sort(key = itemgetter(1))
  143.             (corner, distance, name) = distances[0]
  144.             return corner
  145.         except Exception:
  146.             print_exc()
  147.             return Monitor.GetFromPointer().ClientArea.BottomRight
  148.  
  149.  
  150.     
  151.     def Destroy(self):
  152.         self._destroyed = True
  153.         if not config.platform == 'mac':
  154.             self.unregister_observers(self.acct, self.on_account_updated)
  155.         
  156.         return super(AccountTrayIcon, self).Destroy()
  157.  
  158.     
  159.     def count_string(self):
  160.         acct = self.acct
  161.         if acct.offline_reason != acct.Reasons.NONE:
  162.             count = 'X'
  163.         else:
  164.             count = getattr(acct, 'count', 0)
  165.         return count
  166.  
  167.     count_string = property(count_string)
  168.     
  169.     def on_account_updated(self, obj = None, attr = None, old = None, new = None):
  170.         obj_or_event = obj
  171.         if not self or getattr(self, '_destroyed', False):
  172.             return None
  173.         acct = self.acct
  174.         count = self.count_string
  175.  
  176.     
  177.     def should_show_count(self):
  178.         return pref('trayicons.email.show_count', True)
  179.  
  180.     
  181.     def Tooltip(self):
  182.         return ''
  183.  
  184.     Tooltip = property(Tooltip)
  185.  
  186.  
  187. class UpdateMixinAccountTrayIcon(AccountTrayIcon):
  188.     
  189.     def register_observers(self, acct, callback):
  190.         acct.add_observer(callback, 'count', 'state')
  191.  
  192.     
  193.     def unregister_observers(self, acct, callback):
  194.         acct.remove_observer(callback, 'count', 'state')
  195.  
  196.  
  197.  
  198. class EmailTrayIcon(UpdateMixinAccountTrayIcon):
  199.     
  200.     def update_menu(self, event = None):
  201.         EmailAccount = EmailAccount
  202.         import common.emailaccount
  203.         self._menu.RemoveAllItems()
  204.         actions.menu(wx.FindWindowByName('Buddy List'), self.acct, cls = EmailAccount, search_bases = False, menu = self._menu)
  205.  
  206.     
  207.     def Tooltip(self):
  208.         c = self.count_string
  209.         if c == 'X':
  210.             return _('%s (%s)') % (self.acct.offline_reason, self.acct.email_address)
  211.         
  212.         try:
  213.             count = int(c)
  214.         except ValueError:
  215.             c == 'X'
  216.             c == 'X'
  217.             count = 0
  218.         except:
  219.             c == 'X'
  220.  
  221.         return (c == 'X') % (_('%s unread message%s (%s)'), c if count != 1 else '', self.acct.email_address)
  222.  
  223.     Tooltip = property(Tooltip)
  224.  
  225.  
  226. class SocialAccountTrayIcon(UpdateMixinAccountTrayIcon):
  227.     
  228.     def Tooltip(self):
  229.         c = self.count_string
  230.         if c == 'X':
  231.             return _('%s (%s)' % (self.acct.offline_reason, self.acct.name))
  232.         return (c == 'X', try_this)((lambda : _('%s new alerts (%s)') % (c, self.acct.name)), '')
  233.  
  234.     Tooltip = property(Tooltip)
  235.  
  236.  
  237. class MyspaceTrayIcon(SocialAccountTrayIcon):
  238.     
  239.     def register_observers(self, acct, callback):
  240.         SocialAccountTrayIcon.register_observers(self, acct, callback)
  241.         acct.add_observer(callback, 'alerts')
  242.  
  243.     
  244.     def unregister_observers(self, acct, callback):
  245.         SocialAccountTrayIcon.unregister_observers(self, acct, callback)
  246.         acct.remove_observer(callback, 'alerts')
  247.  
  248.  
  249.