#!/usr/bin/python3

################################################################################
##3456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789
##      10        20        30        40        50        60        70        80
##
## Info:
##    Example of how to use libnotify correctly
##
## Run:
##    chmod +x sd-example-morning-alarm.py
##    ./sd-example-morning-alarm.py
##
## Copyright 2013 Canonical Ltd.
##
## Author:
##    Mirco "MacSlow" Mueller <mirco.mueller@canonical.com>
##
## This program is free software: you can redistribute it and/or modify it
## under the terms of the GNU General Public License version 3, as published
## by the Free Software Foundation.
##
## This program is distributed in the hope that it will be useful, but
## WITHOUT ANY WARRANTY; without even the implied warranties of
## MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
## PURPOSE.  See the GNU General Public License for more details.
##
## You should have received a copy of the GNU General Public License along
## with this program.  If not, see <http://www.gnu.org/licenses/>.
##
################################################################################

import os
import signal
import sys
import time
import notify2
import dbus
from dbus.mainloop.glib import DBusGMainLoop
from gi.repository import GLib
import example

def action_snooze (notification, action):
    if action == "snooze":
        print("You want to snooze some more you lazy bastard!")
    else:
        print("That should not have happened (action_snooze)!")

def action_ok (notification, action):
    if action == "ok":
        print("Getting up is the right thing to do... good for you!")
    else:
        print("That should not have happened (action_ok)!")

def pushNotification (title, body, icon):
    n = notify2.Notification (title, body, icon);

    # NOTE: the order in which actions are added is important... positive
    # always comes first!
    n.add_action ("ok",     "Ok",     action_ok);
    n.add_action ("snooze", "Snooze", action_snooze);

    # indicate to the notification-daemon, that we want to use snap-decisions
    n.set_hint_string ("x-lomiri-snap-decisions", "true");
    n.set_hint_string ("x-lomiri-non-shaped-icon", "true");
    n.set_hint_string ("x-lomiri-private-affirmative-tint", "true");

    n.show ()
    return n

if __name__ == '__main__':
    dbus_loop = DBusGMainLoop ()
    if not notify2.init ("sd-example-morning-alarm", mainloop=dbus_loop):
        sys.exit (1)

    # call this so we can safely use capabilities dictionary later
    example.initCaps ()

    # show what's supported
    example.printCaps ()

    # be nice and check for required capabilities
    if not example.capabilities['x-lomiri-snap-decisions']:
        sys.exit (2)

    loop = GLib.MainLoop ()

    # since we are using snap-decision, this will be called
    # only once the user has acknowledged the decision or
    # a long time-out
    def quit_app (sig, stack):
        loop.quit ()
    signal.signal (signal.SIGUSR1, quit_app)

    n = pushNotification ("Morning alarm",
                          "It's 6:30... time to get up!",
                          "alarm-clock")
    n.connect ("closed", example.closedHandler)

    loop.run ()
