staging.inyokaproject.org

blinklight Empathy bzw. Telepathy bzw. dbus

Status: Ungelöst | Ubuntu-Version: Ubuntu 9.10 (Karmic Koala)
Antworten |

kingpin-

Anmeldungsdatum:
9. April 2006

Beiträge: Zähle...

Ich habe das pidgin Pugin blinklight (ehemals thinklight) immer zu schätzen gewusst. Da der Trend ja zu Empathy geht wollte ich das blinklight portieren (zumindest die Funktionalität). Der erste Teil (das Licht blinken zu lassen) war auch sehr einfach. Leider bietet Empathy von Hause aus nicht die Funktion "führe den Befahl aus wenn ein Nachricht eintrifft". Es sollte aber eigentlich kein Problem sein das nach zurüsten den Empathy nutzt Telepathy und Telepathy ist ja irgendwie eine riesige D-BUS API. Da sollte die simple Funktion Message angekommen ja zu finden sein, nur habe ich es nicht geschafft in dem Wust der Telepathy API die richtige Methode zu finden.

Also was ich möchte ist ein kleines Skript das über D-BUS horcht ob neue Nachrichten über Telepathy ankommen und dann ein Befehl in der Shell ausführt. Eigentlich kann das nicht mehr als 5 Zeilen Programmcode sein. Daher wäre ich sehr dankbar wenn jemand einen Hinweis dazu hat.

barcc

Avatar von barcc

Anmeldungsdatum:
13. Juli 2007

Beiträge: 696

Hier meine Lösung:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#!/usr/bin/python
#-*- coding:utf-8 -*-

import sys
import subprocess

import gobject
import dbus
import dbus.service
import dbus.mainloop.glib


class TI_MainLoop (gobject.MainLoop):
    def __init__(self, dbus_interface, args, placeholder):
        gobject.MainLoop.__init__(self)
        self.dbus_interface = dbus_interface
        self.args = args
        self.placeholder = placeholder
        self.dbus_interface.connect_to_signal('IndicatorModified', self.on_IndicatorModified)
    
    def on_IndicatorModified(self, ind_id, ind_prop):
        #print 'on_IndicatorModified:', ind_id, ind_prop
        if ind_prop == 'body':
            #print self.args
            if self.placeholder is not None:
                message = self.dbus_interface.GetIndicatorProperty(ind_id, ind_prop)
                args = [(message if p == self.placeholder else p) for p in self.args]
            else:
                args = self.args
            #print args
            subprocess.Popen(args)
        

def main(args):
    #print args
    if len(args) <= 1:
        print 'usage:'
        print '    %s [-m placeholder] program [arg] ...' % (__file__)
        return 1
        
    if args[1] == '-m':
        if len(args) <= 2:
            return 1
        placeholder = args[2]
        args = args[3:]
    else:
        placeholder = None
        args = args[1:]
    
    dbus_mainloop = dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
    dbus.set_default_main_loop(dbus_mainloop)
    
    session_bus = dbus.SessionBus()
    bus_name = "org.freedesktop.Telepathy.Client.Empathy"
    dbus.validate_bus_name(bus_name)
    dbusobj = session_bus.get_object(bus_name,'/org/freedesktop/indicate')
    
    #mainloop = gobject.MainLoop()
    dbus_interface = dbus.Interface(dbusobj, dbus_interface='org.freedesktop.indicator')
    mainloop = TI_MainLoop(dbus_interface, args, placeholder)
    
    try:
        mainloop.run()
    except KeyboardInterrupt:
        print
        return 1
    return 0


if __name__ == '__main__':
    sys.exit(main(sys.argv))

Das Script muss nach Empathy gestartet werden mit dem Programmnamen und evtl Programmparametern:

./telepathy_on_indicator.py echo 'Nachricht'

oder mit der Option -m:

./telepathy_on_indicator.py -m _ echo 'MESSAGE:' _ 'END OF TRANSMISSION'

Dabei wird der Parameter _ durch den Nachrichtentext ersetzt

Beenden kann man das Skript mit 'Strg+C'.

kingpin-

(Themenstarter)

Anmeldungsdatum:
9. April 2006

Beiträge: 8

Hi ich hab den Code eben mal (unter Lucid) probiert und bei mir zeigt er keine Reaktion. Nicht mal irgendeine Art von Fehlermeldung. Kannst du es nochmal bei dir unter Lucid probieren ?

Viele Grüße Nico

barcc

Avatar von barcc

Anmeldungsdatum:
13. Juli 2007

Beiträge: 696

Version für Lucid:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#!/usr/bin/python
#-*- coding:utf-8 -*-

import sys
import subprocess

import gobject
import dbus
import dbus.service
import dbus.mainloop.glib


class TI_MainLoop (gobject.MainLoop):
    def __init__(self, dbus_interface, args, placeholder):
        gobject.MainLoop.__init__(self)
        self.dbus_interface = dbus_interface
        self.args = args
        self.placeholder = placeholder
        self.dbus_interface.connect_to_signal('IndicatorModified', self.on_IndicatorModified)
    
    def on_IndicatorModified(self, ind_id, ind_prop):
        #print 'on_IndicatorModified:', ind_id, ind_prop
        if ind_prop == 'body':
            #print self.args
            if self.placeholder is not None:
                message = self.dbus_interface.GetIndicatorProperty(ind_id, ind_prop)
                args = [(message if p == self.placeholder else p) for p in self.args]
            else:
                args = self.args
            #print args
            subprocess.Popen(args)
        

def main(args):
    #print args
    if len(args) <= 1:
        print 'usage:'
        print '    %s [-m placeholder] program [arg] ...' % (__file__)
        return 1
        
    if args[1] == '-m':
        if len(args) <= 2:
            return 1
        placeholder = args[2]
        args = args[3:]
    else:
        placeholder = None
        args = args[1:]
    
    dbus_mainloop = dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
    dbus.set_default_main_loop(dbus_mainloop)
    
    session_bus = dbus.SessionBus()
    bus_name = "org.freedesktop.Telepathy.Client.Empathy"
    dbus.validate_bus_name(bus_name)
    dbusobj = session_bus.get_object(bus_name,'/org/ayatana/indicate')
    
    dbus_interface = dbus.Interface(dbusobj, dbus_interface='org.ayatana.indicate')
    mainloop = TI_MainLoop(dbus_interface, args, placeholder)
    
    try:
        mainloop.run()
    except KeyboardInterrupt:
        print
        return 1
    return 0


if __name__ == '__main__':
    sys.exit(main(sys.argv))
Antworten |