#!/usr/bin/env python
# -*- coding: utf-8 -*-

import sys
import dbus
from pprint import pprint
from time import sleep

# since python 2.7
#import argparse 
from optparse import OptionParser

parser = OptionParser()
#parser.add_argument('message', type=string, nargs='+',
#                   help='message text to send')
parser.add_option('--event', dest='event', default="notification",
                   help='event')
parser.add_option('--app', dest='app', default="kde",
                   help='fromApp')
parser.add_option('--title', dest='title', default=sys.argv[0],
                   help='event')
parser.add_option('--timeout', dest='timeout', type=int, default=0,
                   help='timeout in seconds. Without timeout, message will stay until it is confirmed, but ' + sys.argv[0] + ' will return immedialy')
(options, args) = parser.parse_args()

print "options:"
pprint(options)

message = " ".join( args )

print "message: ", message

knotify = dbus.SessionBus().get_object("org.kde.knotify", "/Notify")


# predefined notify settings, see
# find /usr/share/kde4/apps -name *.notifyrc
# grep -v '[a-z]\[' /usr/share/kde4/apps/kde/kde.notifyrc

# interface for knotify,
# taken from knotify.h
# int event(
#       const QString &event, 
#       const QString &fromApp, 
#       const QVariantList& contexts,
#       const QString &title, 
#       const QString &text, 
#       const QByteArray& pixmap, 
#       const QStringList& actions, 
#       int timeout,
#       qlonglong winId ); 

# specifiing timeout as parameter does not show a effect,
# therefore notification is closed manually after timeout
id = knotify.event( options.event, options.app, [], options.title, message, [], [], options.timeout, dbus_interface="org.kde.KNotify")

if options.timeout > 0:
    print "Knotify-ID: ", id

    sleep(options.timeout)

    # void update(int id, const QString &title, const QString &text, const KNotifyImage& image,  const QStringList& actions);
    #knotify.update( id, "knotify-client", "finished", [], [], dbus_interface="org.kde.KNotify")

    #sleep( 1 )

    #for i in range(3):
        #print i
        #knotify.update( id, "knotify-client", str(i), [], [], dbus_interface="org.kde.KNotify")
        ## void reemit(int id, const ContextList& contexts);
        #knotify.reemit( id, [], dbus_interface="org.kde.KNotify" )
        #sleep(3)

    knotify.closeNotification(id, dbus_interface="org.kde.KNotify");

exit(0)
