#!/usr/bin/env python

############################################################################
# Config dialog for alarm script
# (c) 2005 Mark Kretschmann <markey@web.de>
#
# Depends on: Python 3, PyTQt
############################################################################
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
############################################################################

from configparser import *
import queue
import os.path
import sys
import threading
from os import *

try:
    from PyTQt.tqt import *
except:
    popen( "kdialog --sorry 'PyTQt (TQt bindings for Python) is required for this script.'" )
    raise


class ConfigDialog( TQDialog ):

    def __init__( self ):
        TQDialog.__init__( self )
        self.setWFlags( TQt.WDestructiveClose )
        self.setCaption( "Alarm Script - Amarok" )

        self.lay = TQHBoxLayout( self )

        self.vbox = TQVBox( self )
        self.lay.addWidget( self.vbox )

        self.htopbox = TQHBox( self.vbox )
        TQLabel( "Alarm time: ", self.htopbox )
        self.timeEdit = TQTimeEdit( self.htopbox )

        self.hbox = TQHBox( self.vbox )

        self.ok = TQPushButton( self.hbox )
        self.ok.setText( "Ok" )

        self.cancel = TQPushButton( self.hbox )
        self.cancel.setText( "Cancel" )
        self.cancel.setDefault( True )

        self.connect( self.ok,     TQ_SIGNAL( "clicked()" ), self.save )
        self.connect( self.cancel, TQ_SIGNAL( "clicked()" ), self, TQ_SLOT( "reject()" ) )

        self.adjustSize()

    def __del__( self ):
        print("ConfigDialog dtor")

    def save( self ):
        wakeTime = str( self.timeEdit.time().toString() )
        print(wakeTime)

        self.file = file( "alarmrc", 'w' )

        self.config = ConfigParser()
        self.config.add_section( "General" )
        self.config.set( "General", "alarmtime", wakeTime)
        self.config.write( self.file )
        self.file.close()

        self.accept()


class Alarm( TQApplication ):

    def __init__( self, args ):
        TQApplication.__init__( self, args )

        self.queue = queue.Queue()
        self.startTimer( 100 )

        self.t = threading.Thread( target = self.readStdin )
        self.t.start()

        self.alarmTimer = TQTimer()
        self.connect( self.alarmTimer, TQ_SIGNAL( "timeout()" ), self.wakeup )

        self.readSettings()

    def __del__( self ):
        print("Alarm dtor")

    def wakeup( self ):
        popen( "dcop amarok player play" )
        self.quit()


    def readSettings( self ):
        config = ConfigParser()
        config.read( "alarmrc" )

        try:
            timestr = config.get( "General", "alarmtime" )
            print("Alarm Time: " + timestr)

            time = TQTime.fromString( timestr )
            secondsleft = TQTime.currentTime().secsTo( time )

            if secondsleft > 0:
                self.alarmTimer.start( secondsleft * 1000, True )
        except:
            pass


############################################################################
# Stdin-Reader Thread
############################################################################

    def readStdin( self ):
        while True:
            line = sys.stdin.readline()

            if line:
                self.queue.put_nowait( line )
            else:
                break


############################################################################
# Command Handling
############################################################################

    def timerEvent( self, event ):
        if not self.queue.empty():
            eventStr = TQString( self.queue.get_nowait() )
            print("[Alarm Script] Received notification: " + str( eventStr ))

            if eventStr.contains( "configure" ):
                self.configure()

    def configure( self ):
        print("Alarm Script: configuration")

        self.dia = ConfigDialog()
        self.dia.show()
        self.connect( self.dia, TQ_SIGNAL( "destroyed()" ), self.readSettings )


############################################################################

def main( args ):
    app = Alarm( args )

    app.exec_loop()

if __name__ == "__main__":
    main( sys.argv )

