[Pidgin] DbusHowto modified

Pidgin trac at pidgin.im
Mon Feb 29 03:55:01 EST 2016


Page "DbusHowto" was changed by lew21
Diff URL: <https://developer.pidgin.im/wiki/DbusHowto?action=diff&version=9>
Revision 9
Comment: Update to pydbus and Python 3.
Changes:
-------8<------8<------8<------8<------8<------8<------8<------8<--------
Index: DbusHowto
=========================================================================
--- DbusHowto (version: 8)
+++ DbusHowto (version: 9)
@@ -12,6 +12,17 @@
 
 You do the listening and calling; Pidgin does the emitting and replying.
 
+== Setup ==
+
+To interact with Pidgin you need to get a reference to Pidgin's D-Bus interface.
+
+{{{
+#!python
+from pydbus import SessionBus
+
+bus = SessionBus()
+purple = bus.get("im.pidgin.purple.PurpleService", "/im/pidgin/purple/PurpleObject")
+}}}
 == Listening to signals ==
 
 Many Pidgin events generate corresponding signals, and by listening to them, you can gain a fair idea of what's going on, and react appropriately.
@@ -19,9 +30,7 @@
 To be notified when a signal (e.g. the "received-im-msg" signal) is emitted, you must "register" with D-Bus as a receiver of that signal. You do this with something like this (the code is in Python; consult your language's D-Bus bindings for the equivalent):
 {{{
 #!python
-bus.add_signal_receiver(my_func,
-                        dbus_interface="im.pidgin.purple.PurpleInterface",
-                        signal_name="ReceivedImMsg")
+purple.ReceivedImMsg.connect(my_func)
 }}}
 
 [Note that the signal is called "received-im-msg" in the Pidgin documentation but the D-Bus signal is called "!ReceivedImMsg"; do this simple transformation for all signals you see.]
@@ -29,8 +38,8 @@
 This registers a function, called `my_func`, which will be called when the signal is emitted. Now you look up the documentation to see what parameters come with the signal, and you define `my_func` to work on those parameters. In this case, the [/doxygen/dev2.x.y/html/conversation-signals.html#received-im-msg documentation] tells you that the parameters are (account, sender, message, conversation, flags), so you define `my_func` accordingly:
 {{{
 #!python
-   def my_func(account, sender, message, conversation, flags):
-       print sender, "said:", message
+def my_func(account, sender, message, conversation, flags):
+    print(sender, "said:", message)
 }}}
 
 There is a little more code you'll have to write to set up D-Bus etc. You put everything together in a D-Bus script like this:
@@ -39,19 +48,16 @@
 #!/usr/bin/env python
 
 def my_func(account, sender, message, conversation, flags):
-    print sender, "said:", message
-
-import dbus, gobject
-from dbus.mainloop.glib import DBusGMainLoop
-dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
-bus = dbus.SessionBus()
-
-bus.add_signal_receiver(my_func,
-                        dbus_interface="im.pidgin.purple.PurpleInterface",
-                        signal_name="ReceivedImMsg")
-
-loop = gobject.MainLoop()
-loop.run()
+    print(sender, "said:", message)
+
+from pydbus import SessionBus
+from gi.repository import GObject
+
+bus = SessionBus()
+purple = bus.get("im.pidgin.purple.PurpleService", "/im/pidgin/purple/PurpleObject")
+purple.ReceivedImMsg.connect(my_func)
+
+GObject.MainLoop().run()
 }}}
 
 Obviously,
@@ -141,19 +147,11 @@
 
 Most of Pidgin's functions can be called through the D-Bus interface. The D-Bus functions have similar names as the C functions, and this is not unlike writing actual Pidgin plugins in C or Perl or Tcl instead.
 
-To start calling Pidgin functions, you need to get a reference to Pidgin's D-Bus interface. This is in Python:
-{{{
-#!python
-bus = dbus.SessionBus()
-obj = bus.get_object("im.pidgin.purple.PurpleService", "/im/pidgin/purple/PurpleObject")
-purple = dbus.Interface(obj, "im.pidgin.purple.PurpleInterface")
-}}}
-
-Now you can call Pidgin functions as if they were members of the `purple` object. For example, to send a message to all your IM windows, you can do:
-{{{
-#!python
-for conv in purple.PurpleGetIms():
-    purple.PurpleConvImSend(purple.PurpleConvIm(conv), "Ignore.")
+For example, to send a message to all your IM windows, you can do:
+{{{
+#!python
+for conv in purple.PurpleGetIms()[0]:
+    purple.PurpleConvImSend(purple.PurpleConvIm(conv)[0], "Ignore.")
 }}}
 
 Sometimes things are not direct. To set your status message without changing the status, for example, you need to use five calls:
@@ -161,9 +159,9 @@
 #!python
 def set_message(message):
     # Get current status type (Available/Away/etc.)
-    current = purple.PurpleSavedstatusGetType(purple.PurpleSavedstatusGetCurrent())
+    current = purple.PurpleSavedstatusGetType(purple.PurpleSavedstatusGetCurrent()[0])[0]
     # Create new transient status and activate it
-    status = purple.PurpleSavedstatusNew("", current)
+    status = purple.PurpleSavedstatusNew("", current)[0]
     purple.PurpleSavedstatusSetMessage(status, message)
     purple.PurpleSavedstatusActivate(status)
 }}}
-------8<------8<------8<------8<------8<------8<------8<------8<--------

--
Page URL: <https://developer.pidgin.im/wiki/DbusHowto>
Pidgin <https://pidgin.im>
Pidgin

This is an automated message. Someone added your email address to be
notified of changes on 'DbusHowto' page.
If it was not you, please report to datallah at pidgin.im.


More information about the Wikiedit mailing list