home *** CD-ROM | disk | FTP | other *** search
- # Source Generated with Decompyle++
- # File: in.pyo (Python 2.6)
-
- from __future__ import with_statement
- import hooks
- import wx
- from wx import GetTopLevelWindows, GetTopLevelParent, WXK_CONTROL, GetKeyState, CallLater
- FindFocus = wx.Window.FindFocus
- import threading
- import gui.native.helpers as helpers
- from util import strip_html2, odict, traceguard
- from util.primitives.funcs import Delegate
- from util.primitives.fmtstr import fmtstr
- from util.lrucache import lru_cache
- from common.notifications import fire
- from common.sms import normalize_sms as smsize
- from common.sms import validate_sms
- from common import profile, pref
- from gui.imwin.imwin_gui import ImWinPanel
- from traceback import print_exc
- from logging import getLogger
- log = getLogger('imhub')
- LOG = log.debug
- from gui.uberwidgets.formattedinput import get_default_format
- strip_html2 = lru_cache(80)(strip_html2)
-
- def is_system_message(messageobj):
- if messageobj is not None:
- pass
- return messageobj.buddy is None
-
-
- def is_announcement(message):
- buddy = getattr(message, 'buddy', None)
- if buddy is not None and buddy.service == 'digsby':
- pass
- return buddy.name == 'digsby.org'
-
-
- def on_status(status, on_done = None):
- for imwin in ImWinPanel.all():
- if imwin.Buddy == status.buddy:
- wx.CallAfter(imwin.show_status, status, on_done)
- break
- continue
-
-
-
- def show_announce_window(message):
- AnnounceWindow = AnnounceWindow
- import gui.imwin.imwin_gui
- announce = AnnounceWindow(wx.FindWindowByName('Buddy List'))
- announce.message(message)
- wx.CallAfter(announce.Show)
-
-
- def show_announce_window_After(message):
- wx.CallAfter(show_announce_window, message)
-
- hooks.register('digsby.server.announcement', show_announce_window_After)
-
- def pre_message_hook(message):
- if is_announcement(message):
- if profile.account_manager.all_accounts:
- show_announce_window(message)
-
- return False
-
- im_show = Delegate()
-
- def frame_show(frame, show, no_activate = False):
- if show and getattr(frame, '_starting_minimized', False):
- frame.Iconize(True)
- frame._starting_minimized = False
- elif no_activate:
- frame.ShowNoActivate(show)
- else:
- frame.Show(show)
-
-
- def on_message(messageobj = None, convo = None, raisenow = False, meta = None, mode = 'im', firenots = True):
- global im_show, im_show, im_show, im_show, im_show, im_show, im_show
- thread_check()
- ImFrame = ImFrame
- import gui.imwin.imtabs
- convo = None if messageobj is not None else convo
- sms = None if messageobj is not None else False
- if not sms:
- pass
- sms = getattr(convo.buddy, 'sms', False)
- if pre_message_hook(messageobj) is False:
- return None
- system_message = is_system_message(messageobj)
- (win, isnew) = None if not raisenow else pre_message_hook(messageobj) is False(window_for, convo = 'meta', meta = 'sms', sms = 'system_message', system_message = 'focus' if raisenow else None, mode = mode)
- if win is None:
- return None
- if firenots:
- fire_notifications(messageobj, win, isnew, mode)
-
- flashnow = True
- frame = GetTopLevelParent(win)
- focusedImFrame = isinstance(focused_top(), ImFrame)
- if isnew:
- if raisenow and not focusedImFrame:
- (im_show,) += (lambda : log.info('calling frame.Show on frame at %r', frame.Rect))
- (im_show,) += (lambda : (wx.CallAfter,)((lambda : frame_show(frame, True)))
- )
- elif not frame.IsShown():
- (im_show,) += (lambda : log.info('calling frame.ShowNoActivate on frame at %r', frame.Rect))
- (im_show,) += (lambda : (wx.CallAfter,)((lambda : frame_show(frame, True, no_activate = True)))
- )
-
-
- if not focusedImFrame:
- if (raisenow or isnew) and 'stealfocus' == new_action():
- (im_show,) += (lambda : log.info('raising existing IM frame at %r', frame.Rect))
- (None, im_show) += (lambda : raise_imwin(frame, win))
- elif flashnow and messageobj is not None and not win.IsActive():
- bud = messageobj.buddy
- if bud is not None and bud is not messageobj.conversation.self_buddy:
- (im_show,) += (lambda : wx.CallAfter(win.Notify))
-
-
- if not pref('fullscreen.hide_convos', True) and helpers.FullscreenApp():
- im_show.call_and_clear()
- else:
- log.info('im_hub.on_message ignoring for now because of fullscreen (delegate is %d messages long)', len(im_show))
- helpers.FullscreenAppLog()
- win.message(messageobj, convo, mode, meta)
- return win
-
-
- def focused_top():
- focused = FindFocus()
- if focused is not None:
- return focused.Top
-
-
- def raise_imwin(frame, imwin):
- log.info('raise_imwin: %r %r', frame, imwin)
- if frame.IsIconized():
- frame.Iconize(False)
- event = wx.IconizeEvent(frame.Id, False)
- frame.AddPendingEvent(event)
-
- frame.ReallyRaise()
- tab = imwin.Tab
- if tab is not None:
- tab.SetActive(True)
-
-
-
- def open(idstr):
- contact = profile.blist.contact_for_idstr(idstr)
- if contact is not None:
- return begin_conversation(contact)
-
-
- def begin_conversation(contact, mode = 'im', forceproto = False):
- thread_check()
- MetaContact = MetaContact
- OfflineBuddy = OfflineBuddy
- import contacts.metacontacts
- if isinstance(contact, OfflineBuddy):
- log.info('cannot open an IM window for OfflineBuddy %r', contact)
- return None
- meta = isinstance(contact, OfflineBuddy) if isinstance(contact, MetaContact) else None
- if forceproto:
- proto = contact.protocol
- else:
- (contact, proto) = profile.blist.get_from(contact)
- if proto is None:
- log.info('cannot open an IM window for %r, no compatible protocols?', contact)
- return None
- convo = proto.convo_for(contact)
- if contact.sms and not profile.blist.on_buddylist(contact):
- mode = 'sms'
-
- pop_any_hidden(contact)
- return on_message(convo = convo, raisenow = True, meta = meta, mode = mode)
-
-
- def window_for(convo, meta = None, sms = False, system_message = False, focus = None, mode = 'im'):
- (win, meta) = find_window_for(convo, meta, sms, system_message)
- if win is not None:
- return (win, False)
- if sms and not profile.blist.on_buddylist(convo.buddy):
- convo = window_for_sms(convo)
-
- if system_message:
- return (None, None)
- win = create_imwin(convo, meta, sms, focus, mode)
- return (win, True)
-
-
- def find_window_for(convo, meta = None, sms = False, system_message = False):
- thread_check()
- if meta is None:
- metas = None if not convo.ischat else []
- if metas:
- meta = list(metas)[0]
-
- else:
- metas = [
- meta]
- return (search_for_buddy(metas, convo, sms), meta)
-
-
- def search_for_buddy(metas, convo, sms):
- for win in ImWinPanel.all():
- traceguard.__enter__()
-
- try:
- c = win.Conversation
- if c is convo:
- LOG('direct conversation object match: win: %r, convo: %r', win, convo)
- return win
- if convo.ischat != win.convo.ischat:
- continue
-
- winbud = win.Buddy
- if winbud is None:
- continue
-
- for meta in metas:
- for contact in meta:
- if winbud == contact:
- LOG('matched %r with %r', winbud, contact)
- return win
-
- for contact in meta:
- if winbud.name == contact.name and winbud.protocol.name == contact.protocol.name:
- LOG('loosely matched %r with %r', winbud, contact)
- return win
-
-
- if winbud.info_key == convo.buddy.info_key:
- return win
- finally:
- pass
-
-
-
-
- def window_for_sms(convo):
- log.info('window_for_sms: %r', convo)
- thread_check()
- buddy_sms = smsize(convo.buddy.name)
- keys = []
- for infokey, infodict in profile.blist.info.iteritems():
-
- try:
- sms_numbers = infodict['sms']
- except KeyError:
- continue
-
- for s in list(sms_numbers):
-
- try:
- sms = smsize(s)
- except ValueError:
- log.critical("invalid SMS number in infodict[%r]['sms']: %r", infokey, s)
- sms_numbers.remove(s)
- continue
-
- if buddy_sms == sms:
- keys += [
- infokey]
- continue
-
-
- if not keys:
- log.info('no matching sms numbers found')
- return convo
- conn = convo.protocol
- for key in keys:
- if key.startswith('Metacontact #'):
- continue
-
- (buddyname, proto) = info_key_tuple(key)
- if conn.protocol == proto and conn.has_buddy(buddyname):
- return conn.convo_for(conn.get_buddy(buddyname))
-
- return convo
-
-
- def new_action():
- return pref('conversation_window.new_action')
-
-
- def create_imwin(convo, meta, sms, focus = None, mode = 'im'):
- global im_show, im_show
- thread_check()
- ImFrame = ImFrame
- import gui.imwin.imtabs
- f = None
- hooks.notify('imwin.created')
- focus = None if focus is None else focus
- if pref('messaging.tabs.ctrl_new_window', True):
- pass
- ctrlDown = GetKeyState(WXK_CONTROL)
- if not ctrlDown and pref('messaging.tabs.enabled', True):
- for win in GetTopLevelWindows():
- if isinstance(win, ImFrame):
- f = win
- if f.IsActive():
- focus = False
-
- break
- continue
-
-
- if isinstance(focused_top(), ImFrame):
- focus = False
-
- if getattr(wx.Window.FindFocus(), 'click_raises_imwin', False) and wx.LeftDown():
- focus = True
-
- if f is None:
- if pref('messaging.tabs.enabled', True):
- id = ''
- elif meta is not None:
- pass
-
- id = convo.buddy.idstr()
- f = ImFrame(startMinimized = not focus, posId = id)
-
- w = ImWinPanel(f)
- if convo is not None:
- w.set_conversation(convo, meta)
-
- if focus:
- (None, im_show) += (lambda : raise_imwin(f, w))
- (im_show,) += (lambda : w.FocusTextCtrl())
-
- tab = f.AddTab(w, focus = focus)
- if hasattr(tab, 'OnActive'):
- tab.OnActive += w.FocusTextCtrl
-
- hooks.notify('digsby.statistics.imwin.imwin_created')
- return w
-
-
- def fire_notifications(msg, win, isnew, mode, hidden = False):
- if msg is None or msg.buddy is None:
- return []
- convo = msg.conversation
- bud = msg.buddy
-
- def stop_notify(win = (None, msg.buddy is None, win)):
- if win:
-
- try:
- win.Unnotify()
- except wx.PyDeadObjectError:
- pass
-
- if not (win.Top.AnyNotified) and pref('conversation_window.notify_flash'):
- win.Top.StopFlashing()
-
-
- if hidden and pref('messaging.popups.close_dismisses_hidden', False):
- _remove_hidden_message(bud, msg)
-
-
- if msg.get('content_type', 'text/html') in ('text/html', 'text/xhtml'):
-
- try:
- popup_message = strip_html2(msg.message).decode('xml')
- except Exception:
- print_exc()
- popup_message = msg.message
- except:
- None<EXCEPTION MATCH>Exception
-
-
- None<EXCEPTION MATCH>Exception
- popup_message = msg.message
- fire_opts = dict(buddy = bud, onuserclose = stop_notify)
- if convo is not None:
- pass
- ischat = convo.ischat
- if ischat:
- skin = skin
- import gui
- fire_opts.update(header = _('Group Chat (%d)') % convo.chat_member_count, msg = _('%s: %s') % (bud.alias, popup_message), icon = skin.get('ActionsBar.Icons.RoomList', None), popupid = 'chat!!!%r!!!%r' % (convo.protocol.name, convo.chat_room_name))
- elif bud is not None:
- pass
-
- fire_opts.update(msg = popup_message, icon = convo.icon, popupid = msg.buddy.idstr())
- if bud is not None:
-
- def click_popup(text):
- if pop_any_hidden(bud):
- return None
- if convo.ischat:
- on_message(convo = convo, raisenow = True)
- else:
- begin_conversation(bud)
- if win:
-
- try:
- val = win.input_area.Value
- except wx.PyDeadObjectError:
- pass
-
- if not val:
- win.input_area.Value = text
- wx.CallAfter(win.TextCtrl.SetInsertionPointEnd)
-
-
-
- fire_opts.update(onclick = click_popup)
-
- notification = _get_notification_types(bud, convo, win, hidden, isnew)
-
- def send_from_popup(text, options, convo = convo, win = win, opts = (fire_opts.copy(),)):
- if not text:
- return None
- CallLater(200, stop_notify)
- convo.send_message(fmtstr.singleformat(text, format = _get_format(win)))
- if not wx.GetKeyState(wx.WXK_CONTROL):
- return '> ' + text
-
- fire_opts['input'] = send_from_popup
- return fire(notification, **fire_opts)
-
-
- def _get_notification_types(bud, convo, win, hidden, isnew):
- if bud is convo.self_buddy:
- notification = 'message.sent'
- elif hidden:
- notification = [
- 'message.received.hidden']
- if isnew:
- notification.append('message.received.initial')
-
- elif isnew:
- notification = 'message.received.initial'
- elif not win and not win.IsActive() and not wx.GetApp().IsActive() or win.Mode not in ('im', 'sms'):
- notification = 'message.received.background'
- else:
- notification = 'message.received'
- return notification
-
-
- def _get_format(win):
- format = None
- if win:
-
- try:
- format = win.input_area.Format
- except wx.PyDeadObjectError:
- pass
- except Exception:
- print_exc()
- except:
- None<EXCEPTION MATCH>wx.PyDeadObjectError
-
-
- None<EXCEPTION MATCH>wx.PyDeadObjectError
- if format is None:
- format = get_default_format()
-
- return format
-
-
- def show_info(buddy):
- begin_conversation(buddy, mode = 'info')
-
-
- def thread_check():
- if threading.currentThread().getName() != 'MainThread':
- raise Exception('imhub methods must be called on the main GUI thread')
- threading.currentThread().getName() != 'MainThread'
-
-
- def info_key_tuple(info_key):
- i = info_key.find('_')
- if i == -1:
- pass
-
- return (info_key[:i], info_key[i + 1:])
-
- hidden_windows = odict()
-
- def hidden_count():
- return len(hidden_windows)
-
-
- def hidden_convo_contacts():
- return hidden_windows.keys()
-
-
- def hide_message(messageobj, meta, sms, system_message):
- if messageobj is None:
- return (False, False)
- convo = messageobj.conversation
- if new_action() != 'hide':
- return (False, False)
- (win, meta) = find_window_for(convo, meta, sms, system_message)
- if win is not None:
- return (False, False)
- if not meta:
- pass
- ident = convo.buddy.info_key
- _notify_hidden()
- return (True, isnew)
-
-
- def pop_all_hidden():
- for contact in list(hidden_windows.keys()):
- pop_any_hidden(contact)
-
-
-
- def pop_any_hidden(contact, notify = True):
- quiet_log_messages = quiet_log_messages
- import gui.imwin.messagearea
- all_messages = get_any_hidden(contact)
- if not all_messages:
- return None
- quiet_log_messages(all_messages).__enter__()
-
- try:
- for messageobj in all_messages:
- traceguard.__enter__()
-
- try:
- on_message(messageobj, raisenow = True, firenots = False)
- finally:
- pass
-
- finally:
- pass
-
-
-
- def get_any_hidden(contact, pop = True):
- keys = hidden_windows.keys()
- if not keys:
- return []
- contacts = set()
- if not isinstance(contact, basestring):
- contacts.update((lambda .0: for m in .0:
- m.info_key)(profile.metacontacts.forbuddy(contact)))
-
- contact = getattr(contact, 'info_key', contact)
- contacts.add(contact)
- all_messages = []
- for message_list in hidden_windows.values():
- all_messages.extend(message_list)
-
- messages = []
- for c in keys:
- if c in contacts:
- if pop:
- msgs = hidden_windows.pop(c, [])
- else:
- msgs = hidden_windows.get(c, [])
- messages.extend(msgs)
- continue
-
- return messages
-
-
- def _remove_hidden_message(contact, message):
- contact = getattr(contact, 'info_key', contact)
-
- try:
- messages = hidden_windows[contact]
- except KeyError:
- return False
-
-
- try:
- messages.remove(message)
- except ValueError:
- return False
-
- if len(messages) == 0:
- hidden_windows.pop(contact)
- _notify_hidden()
-
- return True
-
-
- def _notify_hidden():
- hooks.notify('digsby.im.message_hidden', hidden_windows)
-
-