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

Control KNotify4 using DBus

Tags: None
(comma "," separated)
User avatar
Znurre
Registered Member
Posts
3
Karma
0
OS

Control KNotify4 using DBus

Sun Jan 04, 2009 4:17 pm
Hello!
I have been googling whole day for a solution - but did not find anyone that managed to solve it already.
I am creating a bash script, and I would like it to show notifications using KNotify4.
The problem is that all my attempts to control it using DBus are failing.

Code: Select all
qdbus org.kde.VisualNotifications /VisualNotifications Notify 'test', 0, 'test', 'computer', 'Some summary here', 'Content'

Code: Select all
Invalid number of parameters


If I try to write arguments for all properties I will get this error message:

Code: Select all
Sorry, can't pass arg of type 'QVariantMap'.


If I use QDbusViewer to launch the method everything works fine, and I do not need to pass all arguments to the method either.
Any clues?

I am using KDE 4.2 svn trunk from yesterday

Last edited by Znurre on Sun Jan 04, 2009 4:19 pm, edited 1 time in total.
User avatar
bcooksley
Administrator
Posts
19765
Karma
87
OS

RE: Control KNotify4 using DBus

Mon Jan 05, 2009 6:26 am
Unlike in the C++ API, qdbus wants you to use spaces to seperate arguments. You could try without the commas. I think it also demands all of the available variables be supplied ( try setting them to 0 if you don't want to provide them )

Last edited by bcooksley on Mon Jan 05, 2009 6:28 am, edited 1 time in total.


KDE Sysadmin
[img]content/bcooksley_sig.png[/img]
User avatar
Znurre
Registered Member
Posts
3
Karma
0
OS

RE: Control KNotify4 using DBus

Mon Jan 05, 2009 11:23 am
bcooksley wrote:Unlike in the C++ API, qdbus wants you to use spaces to seperate arguments. You could try without the commas. I think it also demands all of the available variables be supplied ( try setting them to 0 if you don't want to provide them )


Thanks alot for taking your time, however it still does not work.
Code: Select all
qdbus org.kde.VisualNotifications /VisualNotifications Notify 'test' 0 'test' 'computer' 'Some summary here' 'Content' 0 0 0

Code: Select all
Sorry, can't pass arg of type 'QVariantMap'.


The qdbus command tells me this is how to invoke the method:
Code: Select all
method uint org.kde.VisualNotifications.Notify(QString app_name, uint replaces_id, QString event_id, QString app_icon, QString summary, QString body, QStringList actions, QVariantMap hints, int timeout)

But how can I create a QVariantMap or a QStringList using bash? Or even simulate this.... Maybe it is not possible to call Notify using qdbus.

Last edited by Znurre on Mon Jan 05, 2009 11:26 am, edited 1 time in total.
User avatar
msoeken
Mentor
Posts
300
Karma
4
OS

RE: Control KNotify4 using DBus

Mon Jan 05, 2009 11:43 am
I took a look into the sourcecode of qdbus and it seems that you cannot pass a QVariantMap. So perhaps you have to write a little wrapper program in C++ or python, which receives the parameters you need and call Notify via dbus.

Last edited by msoeken on Fri Jan 16, 2009 6:49 am, edited 1 time in total.


Image
[size=x-small]code | [url=cia.vc/stats/author/msoeken]cia.vc[/url] | [url=kde.org/support]donating KDE[/url] | [url=tinyurl.com/cto4ns]wishlist[/url][/size]
User avatar
Znurre
Registered Member
Posts
3
Karma
0
OS

RE: Control KNotify4 using DBus

Mon Jan 05, 2009 1:06 pm
msoeken wrote:I took a look into the sourcecode of [b]qdbus[b] and it seems that you cannot pass a QVariantMap. So perhaps you have to write a little wrapper program in C++ or python, which receives the parameters you need and call Notify via dbus.


Thanks for looking into it.
I will write a wrapper in C++ but perhaps something similar should be shipped by default with KDE4, kinda like notify-send for libnotify?
User avatar
msoeken
Mentor
Posts
300
Karma
4
OS

RE: Control KNotify4 using DBus

Mon Jan 05, 2009 1:30 pm
Znurre wrote:
msoeken wrote:I took a look into the sourcecode of qdbus and it seems that you cannot pass a QVariantMap. So perhaps you have to write a little wrapper program in C++ or python, which receives the parameters you need and call Notify via dbus.


Thanks for looking into it.
I will write a wrapper in C++ but perhaps something similar should be shipped by default with KDE4, kinda like notify-send for libnotify?


If you write your wrapper for common use and if it is not too specific to a concrete task, you can commit it to svn (in /trunk/playground). This could be the first version of a notify-send then.

Last edited by msoeken on Fri Jan 16, 2009 6:49 am, edited 1 time in total.


Image
[size=x-small]code | [url=cia.vc/stats/author/msoeken]cia.vc[/url] | [url=kde.org/support]donating KDE[/url] | [url=tinyurl.com/cto4ns]wishlist[/url][/size]
User avatar
anda_skoa
KDE Developer
Posts
783
Karma
4
OS

RE: Control KNotify4 using DBus

Mon Jan 05, 2009 2:21 pm
Another option might be to use kdialog's passivepopup option.

Cheers,
_


anda_skoa, proud to be a member of KDE forums since 2008-Oct.
arizonagroovejet
Registered Member
Posts
12
Karma
0

RE: Control KNotify4 using DBus

Thu Jan 15, 2009 8:36 pm
If you do write a c++ wrapper suitable for common use and are willing to share it I would love a copy. I lack the knowledge to write such a thing myself.
I have a couple of bash scripts which notify users of various things (like having gone over their quota on an NFS mounted home dir) and currently under KDE 3 I'm using kdialog which works but is a bit crude. I was really hoping that in KDE 4.2+ I would be able to make use of the system tray notifications. I'm very disappointed to find out that it's not possible. So close, yet so far...

I'd also be grateful if someone could point me to somewhere which explains what all the options available on dbus calls such as org.kde.VisualNotifications /Notify actually do. Stuff like body (Qstring) and Summary (QString) are easy to figure out, but what the heck are the actions and hints values for? Whatever I enter in qdbusviewer gets ignored and I've not found any enlightenment via Google.
User avatar
jpetso
Registered Member
Posts
4
Karma
0

RE: Control KNotify4 using DBus

Sun Jan 18, 2009 5:58 pm
arizonagroovejet wrote:I'd also be grateful if someone could point me to somewhere which explains what all the options available on dbus calls such as org.kde.VisualNotifications /Notify actually do. Stuff like body (Qstring) and Summary (QString) are easy to figure out, but what the heck are the actions and hints values for?


Actions, no idea (haven't been able to figure out where this is actually used, the code is very distributed over SVN). Hints is unused though, says the source. Kinda mean that just this parameter prevents the qdbus invocation.
User avatar
jpetso
Registered Member
Posts
4
Karma
0

RE: Control KNotify4 using DBus

Sun Jan 18, 2009 9:22 pm
jpetso wrote:Actions, no idea (...)


Ok, actions comes from the fd.o galago (notification) specification and enables buttons to appear on the spec as far as I can see. Nothing that one would need for plain "passive popup" purposes.
User avatar
jpetso
Registered Member
Posts
4
Karma
0

RE: Control KNotify4 using DBus

Mon Jan 19, 2009 12:19 am
I changed kdialog so that a standard call of
Code: Select all
kdialog --passive-popup  --title 

(title and timeout are optional) makes use of those VisualNotifications if they are available. The release team allowed me to backport it to 4.2 too, so there you go.

Last edited by jpetso on Mon Jan 19, 2009 12:20 am, edited 1 time in total.
arizonagroovejet
Registered Member
Posts
12
Karma
0

RE: Control KNotify4 using DBus

Tue Feb 03, 2009 5:40 pm
jpetso wrote:I changed kdialog so that a standard call of
Code: Select all
kdialog --passive-popup  --title 

(title and timeout are optional) makes use of those VisualNotifications if they are available. The release team allowed me to backport it to 4.2 too, so there you go.


That is very neat. Not as good as being able to do what I and the OP wanted to, but still a welcome feature. Good work!
peaches
Registered Member
Posts
31
Karma
1

RE: Control KNotify4 using DBus

Tue Apr 28, 2009 5:36 pm
any notify-send in the works? kdialog doesn't set icon :(
User avatar
Rettich
Registered Member
Posts
123
Karma
0
OS

RE: Control KNotify4 using DBus

Mon May 18, 2009 8:27 pm
I needed such a script. Wasn't that difficult to write it in python. Its not complete but I think its not very hard to modify it for your own needs:

Code: Select all
#!/usr/bin/python
# -*- coding: utf-8 -*-

# Default Configuration (will be overridden by cmd line arguments)
APPLICATION   = ''      # name of the calling application (will be shown if now summery is available)
SUMMERY      = ''
BODY      = ''
ICON      = 'kmail'
TIMEOUT      = 2000      # 0 means, never hide the message; value greater than 0 is the time in ms the message is shown

import sys
import getopt
import dbus

def main():
   dbus = DBus()
   args = CmdLine(sys.argv[1:])
   dbus.notify(app_name=args.getApplication(), app_icon=ICON, summery=args.getSummery(), body=args.getBody(), timeout=TIMEOUT)

class CmdLine():
   # TODO: support for more options

   def __init__(self, argv):
      try:
         opts, args = getopt.getopt(argv, 'hs:', ['help', 'summery=', 'app='])
      except getopt.GetoptError:
         usage()
         sys.exit(2)

      for opt, arg in opts:
         if opt in ('-h', '--help'):
            usage()
            sys.exit()
         elif opt in ('-s', '--summery'):
            self.__summery = arg
         elif opt == '--app':
            self.__app = arg

      if len(args) > 0:
         self.__body = ' '.join(args)

   def getApplication(self):
      try:
         return self.__app
      except AttributeError:
         return APPLICATION

   def getSummery(self):
      try:
         return self.__summery
      except AttributeError:
         return SUMMERY

   def getBody(self):
      try:
         return self.__body
      except AttributeError:
         return BODY

def usage():
   """print usage message"""
   # TODO print more useful help message
   print 'Usage: notify.py [--app=] [-s|--summery=] []'

class DBus():
   """Wrapper class for notify daemon dbus interface"""

   def __init__(self):
      self.__notify = dbus.SessionBus().get_object('org.kde.VisualNotifications', '/VisualNotifications')
   
   def notify(self, app_name='', replaces_id=dbus.UInt32(), event_id='', app_icon='', summery='', body='', actions=dbus.Array(signature='s'), hints=dbus.Dictionary(signature='sv'), timeout=0):
      self.__notify.Notify(app_name, replaces_id, event_id, app_icon, summery, body, actions, hints, timeout)

   def closeNotification(self, event_id):
      self.__notify.CloseNotification(event_id)

if __name__ == "__main__":
   main();


Murphy's Law is recursive. Washing your car to make it rain doesn't work.
seastland
Registered Member
Posts
2
Karma
0
OS

Re: Control KNotify4 using DBus

Thu Jan 28, 2010 2:04 am
To dig up an old thread, I have been looking for a such a script, found the one in the post above, did some Googling about Python, and modified it to this:

Code: Select all
#!/usr/bin/python
# -*- coding: utf-8 -*-
# originally script found at viewtopic.php?f=66&t=23580

import dbus
from optparse import OptionParser

def main():
   parser = OptionParser(usage="%prog [options] <summary> [body]", version="%prog 0.1")
   parser.add_option("-a", "--app", dest="app_name", help="Specifies the application name.", default="notify-dbus.py")
   parser.add_option("-i", "--icon", dest="icon", help="Specifies an icon filename or stock icon to display.", default="")
   parser.add_option("-t", "--expire-timeout", type="int", dest="timeout", help="Specifies the timeout in milliseconds at which to expire the notification.", default=0)
   (options, args) = parser.parse_args()
   if len(args) < 1:
      parser.error("No summary specified.")
   if len(args) > 1:
       BODY = args[1]
   else:
       BODY = ""
   dbus = DBus()
   dbus.notify(app_name=options.app_name, app_icon=options.icon, summary=args[0], body=BODY, timeout=options.timeout)

class DBus():
   """Wrapper class for notify daemon dbus interface"""

   def __init__(self):
      self.__notify = dbus.SessionBus().get_object("org.freedesktop.Notifications", "/org/freedesktop/Notifications")
      self.__iface = dbus.Interface(self.__notify, "org.freedesktop.Notifications")
   
   def notify(self, app_name='', replaces_id=dbus.UInt32(), app_icon='', summary='', body='', actions=dbus.Array(signature="s"), hints=dbus.Dictionary(signature="sv"), timeout=0):
      self.__iface.Notify(app_name, replaces_id, app_icon, summary, body, actions, hints, timeout)

if __name__ == "__main__":
   main();


Bookmarks



Who is online

Registered users: Bing [Bot], claydoh, Google [Bot], rblackwell, Yahoo [Bot]