This forum has been archived. All content is frozen. Please use KDE Discuss instead.

Notification and actions via python/dbus

Tags: None
(comma "," separated)
vkrot
Registered Member
Posts
3
Karma
0
Hi all,

I'm using a tiny script to show notifications:
Code: Select all
#! /usr/bin/python
import sys, dbus
knotify = dbus.SessionBus().get_object("org.kde.knotify", "/Notify")
try:    title, text = sys.argv[1:3]
except: print 'Usage: knotify.py title text'; sys.exit(1)
knotify.event("warning", 1, [], title, text, [], ['Accept'], 0, 0, dbus_interface="org.kde.KNotify")

It shows a message and a button 'Accept'. But how do I bind some action to button? What is the signature of actions/variants parameters to execute some external program or call another dbus object?

Thank you in advance.
User avatar
bcooksley
Administrator
Posts
19765
Karma
87
OS
I suggest running the program "dbus-monitor" and seeing if KNotify emits any signals or other bits of notable information to inform you of the acceptance.

Note that you will get quite a bit of output, as quite a few KDE services use D-Bus..


KDE Sysadmin
[img]content/bcooksley_sig.png[/img]
vkrot
Registered Member
Posts
3
Karma
0
I've solved the issue partly.
According to this documentation (http://www.galago-project.org/specs/not ... /x408.html) when button is clicked org.freedesktop.Notifications.ActionInvoked signal is sent. It contains the id of notification and action_id (matches the sequential order of button that was clicked). It works as expected, but I have no clue how to get the notification id when I send initial call to KNotify.
User avatar
einar
Administrator
Posts
3402
Karma
7
OS
Isn't "python-notify" enough for your use case? Mind you, I just used it for simple notifiations without actions, so I'm not sure if it is useful for you.


"Violence is the last refuge of the incompetent."
Image
Plasma FAQ maintainer - Plasma programming with Python
User avatar
bcooksley
Administrator
Posts
19765
Karma
87
OS
The notification ID is likely the integer which is returned to your application when it makes the initial call to KNotify to display the notification.


KDE Sysadmin
[img]content/bcooksley_sig.png[/img]
vkrot
Registered Member
Posts
3
Karma
0
bcooksley wrote:The notification ID is likely the integer which is returned to your application when it makes the initial call to KNotify to display the notification.

Unfortunately not, they are different numbers.
Any ideas, how to match them?
User avatar
bcooksley
Administrator
Posts
19765
Karma
87
OS
Are you specifying any value other than "0" for "replaces_id" in your initial call to KNotify?
Please note that if you are relying on the Galago signals, you should use the other parts of the Galago interface as well for consistency.


KDE Sysadmin
[img]content/bcooksley_sig.png[/img]
User avatar
realnot
Registered Member
Posts
1
Karma
0
OS
Hi, i'm new in KDE and this is my first post, so hi to all :)

I'm using the same script to show notification and i'd like to notify all my cron job, like this:

Code: Select all
#!/usr/bin/python
# Filename: gentoo_sync.py
import subprocess
import knotify

def sync():
    knotify.notify("Synchronization", "syncing the gentoo repository")
    subprocess.call(["emerge", "--sync"])

if __name__ == '__main__':
    sync()


This is the script to notify each event:

Code: Select all
#!/usr/bin/python
# Filename: knotify.py
import sys, dbus

def notify(title, text):
    knotify = dbus.SessionBus().get_object("org.kde.knotify", "/Notify")
    knotify.event("warning", "kde", [], title, text, [], [], 0, 0,
        dbus_interface="org.kde.KNotify")
   
if __name__ == '__main__':
    notify(title, text)


The problem is that some cron job or script are executed as root (sure, they need the privileges) and should be notified to my user session "realnot". What i got is this error:

venus scripts # /home/realnot/workspace/tools/gentoo_sync.py prova prova
Traceback (most recent call last):
File "/home/realnot/workspace/tools/gentoo_sync.py", line 12, in <module>
sync()
File "/home/realnot/workspace/tools/gentoo_sync.py", line 8, in sync
knotify.notify("Synchronization", "syncing the gentoo repository")
File "/home/realnot/workspace/tools/knotify.py", line 9, in notify
knotify = dbus.SessionBus().get_object("org.kde.knotify", "/Notify")
File "/usr/lib64/python3.2/site-packages/dbus/_dbus.py", line 211, in __new__
mainloop=mainloop)
File "/usr/lib64/python3.2/site-packages/dbus/_dbus.py", line 100, in __new__
bus = BusConnection.__new__(subclass, bus_type, mainloop=mainloop)
File "/usr/lib64/python3.2/site-packages/dbus/bus.py", line 122, in __new__
bus = cls._new_for_bus(address_or_type, mainloop=mainloop)
dbus.exceptions.DBusException: org.freedesktop.DBus.Error.NoReply: Did not receive a reply. Possible causes include: the remote application did not send a reply, the message bus security policy blocked the reply, the reply timeout expired, or the network connection was broken.


I found this post http://stackoverflow.com/questions/1517 ... b-on-linux but i don't know if it can help me, if it's safe and how to use in my scripts. Any suggestions? Thanks in advance!
User avatar
bcooksley
Administrator
Posts
19765
Karma
87
OS
I would recommend you try sourcing the files below ~user/.dbus/session-bus/ before trying to run your script. These should contain all the parameters necessary for root to access your user session bus. You can verify that this has worked by running "qdbus".

You may need to run the following before they will work, after sourcing the files however:
Code: Select all
export DBUS_SESSION_BUS_ADDRESS DBUS_SESSION_BUS_PID DBUS_SESSION_BUS_WINDOWID


KDE Sysadmin
[img]content/bcooksley_sig.png[/img]


Bookmarks



Who is online

Registered users: Bing [Bot], Google [Bot], kde-naveen, Sogou [Bot], Yahoo [Bot]