Shahin
Shahin

Reputation: 1475

Using DBus in python program

Question1:

I tried to make an script to speak with Pidgins DBus. My script is something like this now:

#!/usr/bin/env python

import dbus, gobject
from dbus.mainloop.glib import DBusGMainLoop

class DBus_Answer():
    def __init__(self, text):
        dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
        bus = dbus.SessionBus()
        self.answer = text

        bus.add_signal_receiver(self.my_func,
                                dbus_interface="im.pidgin.purple.PurpleInterface",
                                signal_name="ReceivedImMsg")
        loop = gobject.MainLoop()
        loop.run()

    def my_func(self, account, sender, message, conversation, flags):
        print sender, "said:", message
        bus = dbus.SessionBus()
        obj = bus.get_object("im.pidgin.purple.PurpleService", "/im/pidgin/purple/PurpleObject")
        purple = dbus.Interface(obj, "im.pidgin.purple.PurpleInterface")
        purple.PurpleConvImSend(purple.PurpleConvIm(conversation), self.answer)

run = DBus_Answer("My message!")

This works fine. but my original program is using PyQt4 and I want to use QDBus to achieve this point. I searched a lot but I didn't find any useful documentation about this topic.

Question2: I read somewhere that python 3 does not support DBus, is it true? what would it use instead of that?

Thank you all.

Upvotes: 1

Views: 1417

Answers (1)

Shahin
Shahin

Reputation: 1475

I searched more and find some solutions. now my code is like this and work great ;-):

#!/usr/bin/env python

import sys
import dbus
from PyQt4.QtGui import QApplication
from dbus.mainloop.qt import DBusQtMainLoop

class DBus_Answer():
    def __init__(self, text):
        self.answer = text
        bus_loop = DBusQtMainLoop(set_as_default=True)
        self.bus = dbus.SessionBus()
        self.bus.add_signal_receiver(self.my_func,
                                     dbus_interface="im.pidgin.purple.PurpleInterface",
                                     signal_name="ReceivedImMsg")

    def my_func(self, account, sender, message, conversation, flags):
        obj = self.bus.get_object("im.pidgin.purple.PurpleService", "/im/pidgin/purple/PurpleObject")
        purple = dbus.Interface(obj, "im.pidgin.purple.PurpleInterface")
        purple.PurpleConvImSend(purple.PurpleConvIm(conversation), self.answer)

app = QApplication(sys.argv)
run = DBus_Answer("Slam")
app.exec_()

Upvotes: 1

Related Questions