home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2012 January / maximum-cd-2012-01.iso / DiscContents / digsby_setup.exe / lib / hub.pyo (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2011-10-05  |  11.3 KB  |  279 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyo (Python 2.6)
  3.  
  4. from __future__ import with_statement
  5. import wx
  6. from gui.authorizationdialog import AuthorizationDialog
  7. from logging import getLogger
  8. log = getLogger('hub')
  9. info = log.info
  10. from util.singletonmixin import Singleton
  11. from util.primitives.error_handling import traceguard
  12. from util.primitives.funcs import Delegate, get
  13. from common import profile, fire, pref
  14. from cStringIO import StringIO
  15. from PIL import Image
  16. import sys
  17. from common.protocolmeta import protocols
  18. PROMOTE_STRING = '<br><br>I use <a href="http://www.digsby.com">digsby</a>!'
  19.  
  20. class Hub(Singleton):
  21.     
  22.     def __init__(self):
  23.         Singleton.__init__(self)
  24.         self._locale_obj = None
  25.         self.getting_profile_for = None
  26.  
  27.     
  28.     def signoff(self):
  29.         pass
  30.  
  31.     
  32.     def filter_message(self, mobj = None, *a, **k):
  33.         if mobj is None:
  34.             return None
  35.         conv = mobj.conversation
  36.         conn = conv.protocol
  37.         if not mobj.buddy:
  38.             pass
  39.         buddy = mobj.conversation.buddy
  40.         if conn.allow_message(buddy, mobj) is False:
  41.             log.debug('Message from %r is being ignored', buddy)
  42.             log.debug_s('The message was %r', mobj)
  43.             return Delegate.VETO
  44.  
  45.     
  46.     def launchurl(self, url):
  47.         wx.LaunchDefaultBrowser(url)
  48.  
  49.     
  50.     def windowparent(self):
  51.         wins = wx.GetTopLevelWindows()
  52.         if wins:
  53.             return wins[0]
  54.  
  55.     
  56.     def get_file(self, msg = 'Choose a file'):
  57.         filediag = wx.FileDialog(self.windowparent(), msg)
  58.         if filediag.ShowModal() == wx.ID_OK:
  59.             return filediag.GetPath()
  60.  
  61.     
  62.     def get_dir(self, msg = 'Choose a directory'):
  63.         dirdiag = wx.DirDialog(self.windowparent(), msg)
  64.         if dirdiag.ShowModal() == wx.ID_OK:
  65.             return dirdiag.GetPath()
  66.  
  67.     
  68.     def on_conversation(self, convo, quiet = False):
  69.         log.critical('on_conversation is deprecated and does nothing')
  70.  
  71.     
  72.     def send_message(self, buddy, message):
  73.         buddy.protocol.send_message(buddy = buddy.name, msg = message)
  74.  
  75.     
  76.     def user_message(self, message, title = ''):
  77.         wx.CallAfter(wx.MessageBox, message, title)
  78.  
  79.     
  80.     def on_error(self, e):
  81.         import traceback
  82.         log.error(traceback.format_exc())
  83.         title = get(e, 'header', 'Error:')
  84.         msg = get(e, 'major', '%s: %s' % (type(e).__name__, str(e)))
  85.         details = get(e, 'minor', '')
  86.         close = (_('Close'), (lambda : pass))
  87.         fire('error', title = title, msg = msg, details = details, sticky = True, popupid = Exception, buttons = (close,), update = 'replace')
  88.  
  89.     
  90.     def call_later(self, c, *a, **k):
  91.         c(*a, **k)
  92.  
  93.     
  94.     def on_file_request(self, protocol, xferinfo):
  95.         type = xferinfo.type
  96.         if xferinfo not in profile.xfers:
  97.             if pref('filetransfer.auto_accept_from_blist', default = False) and profile.blist.on_buddylist(xferinfo.buddy):
  98.                 profile.xfers.insert(0, xferinfo)
  99.                 wx.CallAfter(xferinfo.save)
  100.             else:
  101.                 xferinfo.state = xferinfo.states.WAITING_FOR_YOU
  102.                 notifies = fire('filetransfer.request', buddy = xferinfo.buddy, target = xferinfo)
  103.                 xferinfo.notifications = notifies
  104.                 profile.xfers.insert(0, xferinfo)
  105.         
  106.  
  107.     
  108.     def on_direct_connect(self, dc):
  109.         caption = _('%s DirectIM') % dc.protocol.name.capitalize()
  110.         msg = _('%s wants to directly connect with you. (Your IP address will be revealed.)' % dc.buddy.name)
  111.         None if self.popup(msg, caption) else dc.decline()
  112.  
  113.     
  114.     def on_invite(self, protocol, buddy, room_name, message = '', on_yes = None, on_no = None):
  115.         if not pref('messaging.groupchat.enabled', False):
  116.             log.warning('groupchat pref is off, ignoring chat invite')
  117.             maybe_safe_call(on_no)
  118.             return None
  119.         message = pref('messaging.groupchat.enabled', False) if message else _('\n\nWould you like to join?')
  120.         buddy_name = getattr(buddy, 'name', unicode(buddy))
  121.         if buddy is not None:
  122.             msg = _('%s has invited you to a group chat%s') % (buddy_name, message)
  123.         else:
  124.             msg = _('You have been invited to a group chat.%s') % message
  125.         
  126.         def cb(join):
  127.             if join:
  128.                 None if on_yes is not None else protocol.join_chat_room(room_name)
  129.             else:
  130.                 maybe_safe_call(on_no)
  131.  
  132.         res = fire('chatinvite.received', buddy = buddy, minor = msg, buttons = [
  133.             ((_('Join'),), (lambda : on_yes())),
  134.             ((_('Ignore'),), (lambda : maybe_safe_call(on_no)))])
  135.         
  136.         def after():
  137.             if res.had_reaction('Popup'):
  138.                 return None
  139.             SimpleMessageDialog = SimpleMessageDialog
  140.             import gui.toolbox
  141.             protocol_name = protocol.account.protocol_info().name
  142.             title = protocol_name + _(' Invite')
  143.             diag = SimpleMessageDialog(None, title = title, message = msg.strip(), ok_caption = _('Join Chat'), icon = protocol.serviceicon.Resized(32), cancel_caption = _('Ignore Invite'), wrap = 450)
  144.             diag.OnTop = True
  145.             diag.ShowWithCallback(cb)
  146.  
  147.         after = (None, None, (None, None, None, None), wx.CallAfter)(after)
  148.  
  149.     
  150.     def authorize_buddy(self, protocol, buddy, message = '', username_added = None, callback = None):
  151.         message = message.strip()
  152.         if message:
  153.             message = '\n\n"%s"' % message
  154.         
  155.         if not getattr(buddy, 'name', None):
  156.             pass
  157.         buddy = buddy
  158.         if callback is None:
  159.             callback = protocol.authorize_buddy
  160.         
  161.         if username_added is None:
  162.             username_added = protocol.username
  163.         
  164.         if buddy != protocol.self_buddy.name:
  165.             diag_message = _(u'Allow {buddy} to add you ({you}) as a buddy on {protocol}?').format(buddy = buddy, you = username_added, protocol = protocols[protocol.service].name)
  166.             diag_message += message
  167.             ad = AuthorizationDialog(protocol, buddy, diag_message, username_added, callback)
  168.             ad.Show(True)
  169.         else:
  170.             callback(buddy, True, username_added)
  171.  
  172.     
  173.     def on_mail(self, protocol, inbox_count, others_count = None):
  174.         log.info('%s has %s new mail messages', protocol.username, inbox_count)
  175.         if others_count:
  176.             log.info('%s has %s new OTHER mail messages', protocol.username, others_count)
  177.         
  178.  
  179.     
  180.     def send_typing_status(self, buddy, status):
  181.         buddy.protocol.send_typing_status(buddy.name, status)
  182.  
  183.     
  184.     def set_buddy_icon(self, wximage):
  185.         img = wximage.PIL
  186.         (w, h) = img.size
  187.         max = profile.MAX_ICON_SIZE
  188.         if w > max or h > max:
  189.             img = img.Resized(max)
  190.         
  191.         imgFile = StringIO()
  192.         img.save(imgFile, 'PNG', optimize = True)
  193.         self.set_buddy_icon_file(imgFile.getvalue())
  194.  
  195.     
  196.     def set_buddy_icon_file(self, bytes):
  197.         if hasattr(bytes, 'read'):
  198.             bytes = bytes.read()
  199.         
  200.         if not isinstance(bytes, str):
  201.             raise TypeError
  202.         isinstance(bytes, str)
  203.         maxsz = profile.MAX_ICON_SIZE
  204.         maxbytes = MAX_BLOB_SIZE
  205.         import digsby.abstract_blob
  206.         nextsize = maxsz
  207.         tries = 0
  208.         while len(bytes) > maxbytes and tries < 10:
  209.             log.warning('image (%dx%d) is larger than %d bytes, have to resize', nextsize, nextsize, maxbytes)
  210.             img = Image.open(StringIO(bytes)).Resized(nextsize)
  211.             newimg = StringIO()
  212.             img.save(newimg, 'PNG', optimize = True)
  213.             bytes = newimg.getvalue()
  214.             nextsize = max(20, nextsize - 10)
  215.             tries += 1
  216.         log.info('setting %d bytes of icon data (max is %d): %s', len(bytes), maxbytes, bytes[:5])
  217.         profile.save_blob('icon', bytes)
  218.         for acct in profile.account_manager.connected_accounts:
  219.             traceguard.__enter__()
  220.             
  221.             try:
  222.                 acct.connection.set_and_size_icon(bytes)
  223.             finally:
  224.                 pass
  225.  
  226.         
  227.  
  228.     
  229.     def get_locale(self):
  230.         if not self._locale_obj:
  231.             self._locale_obj = [
  232.                 wx.Locale(wx.LANGUAGE_DEFAULT).GetCanonicalName(),
  233.                 sys.getfilesystemencoding()]
  234.         
  235.         return self._locale_obj
  236.  
  237.     
  238.     def get_lang_country(self):
  239.         lang_country = self.get_locale()[0]
  240.         (lang, country) = lang_country.split('_')
  241.         return (lang, country.lower().split('@')[0])
  242.  
  243.     
  244.     def get_country(self):
  245.         return self.get_lang_country()[1]
  246.  
  247.     country = property(get_country)
  248.     
  249.     def get_encoding(self):
  250.         return self.get_locale()[1]
  251.  
  252.     
  253.     def ProcessEvent(self, e):
  254.         print 'ProcessEvent', e
  255.  
  256.     
  257.     def get_language(self):
  258.         return self.get_lang_country()[0]
  259.  
  260.     language = property(get_language)
  261.     
  262.     def getThreadsafeInstance(cls):
  263.         ThreadsafeGUIProxy = ThreadsafeGUIProxy
  264.         import events
  265.         return ThreadsafeGUIProxy(cls.getInstance())
  266.  
  267.     getThreadsafeInstance = classmethod(getThreadsafeInstance)
  268.  
  269. get_instance = Hub.getInstance
  270.  
  271. def diskspace_check(size):
  272.     return True
  273.  
  274.  
  275. def maybe_safe_call(cb):
  276.     v = None
  277.     return v
  278.  
  279.