1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
|
#!/usr/bin/env python
############################################################################
# Config dialog for alarm script
# (c) 2005 Mark Kretschmann <[email protected]>
#
# Depends on: Python 2.2, PyQt
############################################################################
#
# 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 qt import *
except:
popen( "kdialog --sorry 'PyQt (Qt bindings for Python) is required for this script.'" )
raise
class ConfigDialog( QDialog ):
def __init__( self ):
QDialog.__init__( self )
self.setWFlags( Qt.WDestructiveClose )
self.setCaption( "Alarm Script - Amarok" )
self.lay = QHBoxLayout( self )
self.vbox = QVBox( self )
self.lay.addWidget( self.vbox )
self.htopbox = QHBox( self.vbox )
QLabel( "Alarm time: ", self.htopbox )
self.timeEdit = QTimeEdit( self.htopbox )
self.hbox = QHBox( self.vbox )
self.ok = QPushButton( self.hbox )
self.ok.setText( "Ok" )
self.cancel = QPushButton( self.hbox )
self.cancel.setText( "Cancel" )
self.cancel.setDefault( True )
self.connect( self.ok, SIGNAL( "clicked()" ), self.save )
self.connect( self.cancel, SIGNAL( "clicked()" ), self, 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( QApplication ):
def __init__( self, args ):
QApplication.__init__( self, args )
self.queue = Queue.Queue()
self.startTimer( 100 )
self.t = threading.Thread( target = self.readStdin )
self.t.start()
self.alarmTimer = QTimer()
self.connect( self.alarmTimer, 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 = QTime.fromString( timestr )
secondsleft = QTime.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():
string = QString( self.queue.get_nowait() )
print "[Alarm Script] Received notification: " + str( string )
if string.contains( "configure" ):
self.configure()
def configure( self ):
print "Alarm Script: configuration"
self.dia = ConfigDialog()
self.dia.show()
self.connect( self.dia, SIGNAL( "destroyed()" ), self.readSettings )
############################################################################
def main( args ):
app = Alarm( args )
app.exec_loop()
if __name__ == "__main__":
main( sys.argv )
|