[941] | 1 | #!/usr/bin/env python
|
---|
| 2 | # -*- coding: utf-8 -*-
|
---|
| 3 |
|
---|
| 4 | usage = """Usage:
|
---|
| 5 | knotify-recipient.py
|
---|
| 6 |
|
---|
| 7 | listens to knotify signals and prints them
|
---|
| 8 | """
|
---|
| 9 |
|
---|
[943] | 10 | # see
|
---|
| 11 | # http://cgit.freedesktop.org/dbus/dbus-python/tree/examples/example-signal-emitter.py
|
---|
| 12 | # http://dbus.freedesktop.org/doc/dbus-python/doc/tutorial.html
|
---|
| 13 |
|
---|
[941] | 14 | import sys
|
---|
| 15 | import traceback
|
---|
| 16 |
|
---|
| 17 | import gobject
|
---|
| 18 |
|
---|
| 19 | import dbus
|
---|
| 20 | import dbus.mainloop.glib
|
---|
| 21 | #import dbus.mainloop.qt
|
---|
| 22 |
|
---|
| 23 | def handle_reply(msg):
|
---|
| 24 | print msg
|
---|
| 25 |
|
---|
| 26 | def handle_error(e):
|
---|
| 27 | print str(e)
|
---|
| 28 |
|
---|
| 29 | def signal_handler_notificationClosed(id):
|
---|
| 30 | print "handler_notificationClosed ", str(id)
|
---|
| 31 |
|
---|
| 32 | def signal_handler_actionInvoked(id,action):
|
---|
| 33 | print "handler_actionInvoked: ", str(id), ", ", str(action)
|
---|
| 34 |
|
---|
| 35 |
|
---|
| 36 | #def catchall_signal_handler(*args, **kwargs):
|
---|
| 37 | #print ("Caught signal (in catchall handler) "
|
---|
| 38 | #+ kwargs['dbus_interface'] + "." + kwargs['member'])
|
---|
| 39 | #for arg in args:
|
---|
| 40 | #print " " + str(arg)
|
---|
| 41 |
|
---|
| 42 | def catchall_hello_signals_handler(hello_string):
|
---|
| 43 | print "Received a hello signal and it says ", hello_string
|
---|
| 44 |
|
---|
| 45 | #def catchall_testservice_interface_handler(hello_string, dbus_message):
|
---|
| 46 | #print "com.example.TestService interface says " + hello_string + " when it sent signal " + dbus_message.get_member()
|
---|
| 47 |
|
---|
| 48 |
|
---|
| 49 | if __name__ == '__main__':
|
---|
| 50 | dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
|
---|
| 51 | #dbus.mainloop.qt.DBusQtMainLoop(set_as_default=True)
|
---|
| 52 |
|
---|
| 53 | bus = dbus.SessionBus()
|
---|
| 54 | try:
|
---|
| 55 | object = bus.get_object("org.kde.knotify", "/Notify")
|
---|
| 56 |
|
---|
| 57 | #object.connect_to_signal("notificationClosed", hello_signal_handler, dbus_interface="org.kde.knotify", arg0="Hello")
|
---|
| 58 | object.connect_to_signal("notificationClosed", signal_handler_notificationClosed, dbus_interface="org.kde.KNotify")
|
---|
| 59 | object.connect_to_signal("actionInvoked", signal_handler_actionInvoked, dbus_interface="org.kde.KNotify")
|
---|
| 60 | except dbus.DBusException:
|
---|
| 61 | traceback.print_exc()
|
---|
| 62 | print usage
|
---|
| 63 | sys.exit(1)
|
---|
| 64 |
|
---|
| 65 | #lets make a catchall
|
---|
| 66 | #bus.add_signal_receiver(catchall_signal_handler, interface_keyword='dbus_interface', member_keyword='member')
|
---|
| 67 |
|
---|
| 68 | bus.add_signal_receiver(catchall_hello_signals_handler, dbus_interface = "org.kde.KNotify", signal_name = "notificationClosed")
|
---|
| 69 |
|
---|
| 70 | #bus.add_signal_receiver(catchall_testservice_interface_handler, dbus_interface = "com.example.TestService", message_keyword='dbus_message')
|
---|
| 71 |
|
---|
| 72 | loop = gobject.MainLoop()
|
---|
| 73 | loop.run()
|
---|