1 | #!/usr/bin/env python
|
---|
2 | # -*- coding: utf-8 -*-
|
---|
3 |
|
---|
4 | """
|
---|
5 | knotify-client sends messages to the KDE knotify daemon.
|
---|
6 | KDE applications can define different types of messages
|
---|
7 | and theses types define different actions
|
---|
8 | (display message, play sound, start program, ...)
|
---|
9 |
|
---|
10 | tested on KDE 4.3.5 (openSUSE 11.2)
|
---|
11 | """
|
---|
12 |
|
---|
13 | # $Id: knotify-client 940 2010-12-09 19:25:47Z joergs $
|
---|
14 |
|
---|
15 |
|
---|
16 | import sys
|
---|
17 | import dbus
|
---|
18 | from os import system
|
---|
19 | from pprint import pprint
|
---|
20 | from time import sleep
|
---|
21 |
|
---|
22 | # since python 2.7
|
---|
23 | #import argparse
|
---|
24 | from optparse import OptionParser
|
---|
25 |
|
---|
26 |
|
---|
27 | parser = OptionParser(usage = "\n %prog [options] 'text message to send to knotify'\n %prog --appevents APPLICATION")
|
---|
28 | #parser.add_argument('message', type=string, nargs='+',
|
---|
29 | # help='message text to send')
|
---|
30 | parser.add_option('--event', dest='event', default="notification",
|
---|
31 | help='event (default: %default). Use --appevents to get list of events')
|
---|
32 | parser.add_option('--app', dest='application', default="kde",
|
---|
33 | help='event from application (default: %default)')
|
---|
34 | parser.add_option('--title', dest='title', default=sys.argv[0],
|
---|
35 | help='text in title of knotify message')
|
---|
36 | parser.add_option('--timeout', dest='timeout', type=int, default=0,
|
---|
37 | help='timeout in seconds. Without timeout, message will stay until it is confirmed, but ' + sys.argv[0] + ' will return immedialy')
|
---|
38 | parser.add_option('--appevents', metavar='APPLICATION', dest='appevents',
|
---|
39 | help='print list of events for application')
|
---|
40 | (options, args) = parser.parse_args()
|
---|
41 |
|
---|
42 | print "options:"
|
---|
43 | pprint(options)
|
---|
44 |
|
---|
45 | if options.appevents:
|
---|
46 | ressource = options.appevents + "/" + options.appevents + ".notifyrc"
|
---|
47 | system("kread-ressource.py --type data " + ressource )
|
---|
48 | exit(0)
|
---|
49 |
|
---|
50 | message = " ".join( args )
|
---|
51 |
|
---|
52 | print "message: ", message
|
---|
53 |
|
---|
54 | knotify = dbus.SessionBus().get_object("org.kde.knotify", "/Notify")
|
---|
55 |
|
---|
56 |
|
---|
57 | # predefined notify settings, see
|
---|
58 | # find /usr/share/kde4/apps -name *.notifyrc
|
---|
59 | # grep -v '[a-z]\[' /usr/share/kde4/apps/kde/kde.notifyrc
|
---|
60 |
|
---|
61 | # interface for knotify,
|
---|
62 | # taken from knotify.h
|
---|
63 | # int event(
|
---|
64 | # const QString &event,
|
---|
65 | # const QString &fromApp,
|
---|
66 | # const QVariantList& contexts,
|
---|
67 | # const QString &title,
|
---|
68 | # const QString &text,
|
---|
69 | # const QByteArray& pixmap,
|
---|
70 | # const QStringList& actions,
|
---|
71 | # int timeout,
|
---|
72 | # qlonglong winId );
|
---|
73 |
|
---|
74 | # specifiing timeout as parameter does not show a effect,
|
---|
75 | # therefore notification is closed manually after timeout
|
---|
76 | id = knotify.event( options.event, options.application, [], options.title, message, [], [], options.timeout, dbus_interface="org.kde.KNotify")
|
---|
77 |
|
---|
78 | if options.timeout > 0:
|
---|
79 | print "Knotify-ID: ", id
|
---|
80 |
|
---|
81 | sleep(options.timeout)
|
---|
82 |
|
---|
83 | # void update(int id, const QString &title, const QString &text, const KNotifyImage& image, const QStringList& actions);
|
---|
84 | #knotify.update( id, "knotify-client", "finished", [], [], dbus_interface="org.kde.KNotify")
|
---|
85 |
|
---|
86 | #sleep( 1 )
|
---|
87 |
|
---|
88 | #for i in range(3):
|
---|
89 | #print i
|
---|
90 | #knotify.update( id, "knotify-client", str(i), [], [], dbus_interface="org.kde.KNotify")
|
---|
91 | ## void reemit(int id, const ContextList& contexts);
|
---|
92 | #knotify.reemit( id, [], dbus_interface="org.kde.KNotify" )
|
---|
93 | #sleep(3)
|
---|
94 |
|
---|
95 | knotify.closeNotification(id, dbus_interface="org.kde.KNotify");
|
---|
96 |
|
---|
97 | exit(0)
|
---|