"""Simple test app that hangs when bonobo.main is run in its own
thread.  To reproduce the hang:

1) Run this application in an xterm
2) Run a gnome-terminal
3) Click on the file menu of the gnome-terminal

The desktop will hang every time.
"""

import threading
import gobject
import bonobo
import ORBit

gobject.threads_init()

ORBit.CORBA.ORB_init(orb_id="orbit-io-thread")

ORBit.load_typelib("Accessibility")

import Accessibility
import Accessibility__POA

class EventListener(Accessibility__POA.EventListener):
    def __init__(self, registry, eventType):
        self._default_POA().the_POAManager.activate()
        registry.registerGlobalEventListener(self._this(),
                                             eventType)

    def ref(self): pass

    def unref(self): pass

    def queryInterface(self, repo_id):
	thiz = None
        if repo_id == "IDL:Accessibility/EventListener:1.0":
            thiz = self._this()
	return thiz

    def notifyEvent(self, event):
        print "notifyEvent", event.type
        return True

def shutdownOnSignal(signum=None, frame=None):
    bonobo.main_quit()
    
class BonoboMainThread(threading.Thread):
    def run(self):
        print "BONOBO MAIN THREAD RUN CALLED"
        bonobo.main()
        print "BONOBO MAIN THREAD FINISHING"
    
if __name__ == "__main__":
    import signal
    signal.signal(signal.SIGHUP, shutdownOnSignal)
    signal.signal(signal.SIGINT, shutdownOnSignal)
    signal.signal(signal.SIGTERM, shutdownOnSignal)
    signal.signal(signal.SIGQUIT, shutdownOnSignal)

    listener = EventListener(
        bonobo.get_object("OAFIID:Accessibility_Registry:1.0",
                          "Accessibility/Registry"),
        "focus:")
    if True:
        BonoboMainThread().start()
    else:
        BonoboMainThread().run()
    
