use newer eTimer commands
[enigma2-plugins.git] / permanentclock / src / plugin.py
1 ##\r
2 ## Permanent Clock\r
3 ## by AliAbdul\r
4 ##
5 from Components.ActionMap import ActionMap
6 from Components.config import config, ConfigInteger, ConfigSubsection, ConfigYesNo
7 from Components.Language import language
8 from Components.MenuList import MenuList
9 from enigma import ePoint, eTimer, getDesktop
10 from os import environ\r
11 from Plugins.Plugin import PluginDescriptor
12 from Screens.Screen import Screen
13 from Tools.Directories import resolveFilename, SCOPE_LANGUAGE, SCOPE_PLUGINS
14 import gettext
15
16 ##############################################################################
17
18 config.plugins.PermanentClock = ConfigSubsection()\r
19 config.plugins.PermanentClock.enabled = ConfigYesNo(default=False)
20 config.plugins.PermanentClock.position_x = ConfigInteger(default=590)
21 config.plugins.PermanentClock.position_y = ConfigInteger(default=35)
22
23 ##############################################################################
24
25 def localeInit():
26         lang = language.getLanguage()
27         environ["LANGUAGE"] = lang[:2]\r
28         gettext.bindtextdomain("enigma2", resolveFilename(SCOPE_LANGUAGE))\r
29         gettext.textdomain("enigma2")
30         gettext.bindtextdomain("PermanentClock", "%s%s" % (resolveFilename(SCOPE_PLUGINS), "Extensions/PermanentClock/locale/"))
31
32 def _(txt):\r
33         t = gettext.dgettext("PermanentClock", txt)\r
34         if t == txt:\r
35                 t = gettext.gettext(txt)\r
36         return t
37
38 localeInit()\r
39 language.addCallback(localeInit)
40
41 ##############################################################################
42
43 SKIN = """
44         <screen position="0,0" size="92,30" zPosition="10" backgroundColor="#ff000000" title="%s" flags="wfNoBorder">
45                 <widget source="global.CurrentTime" render="Label" position="1,1" size="120,30" font="Regular;26" valign="center" halign="center" backgroundColor="#ff000000" transparent="1">
46                         <convert type="ClockToText">Default</convert>
47                 </widget>
48         </screen>""" % _("Permanent Clock")\r
49 \r
50 ##############################################################################
51
52 class PermanentClockScreen(Screen):
53         def __init__(self, session):
54                 Screen.__init__(self, session)
55                 self.skin = SKIN
56                 self.onShow.append(self.movePosition)
57
58         def movePosition(self):
59                 if self.instance:
60                         self.instance.move(ePoint(config.plugins.PermanentClock.position_x.value, config.plugins.PermanentClock.position_y.value))
61
62 ##############################################################################
63
64 class PermanentClock():
65         def __init__(self):
66                 self.dialog = None
67
68         def gotSession(self, session):
69                 self.dialog = session.instantiateDialog(PermanentClockScreen)
70                 self.showHide()
71
72         def changeVisibility(self):
73                 if config.plugins.PermanentClock.enabled.value:
74                         config.plugins.PermanentClock.enabled.value = False
75                 else:
76                         config.plugins.PermanentClock.enabled.value = True
77                 config.plugins.PermanentClock.enabled.save()
78                 self.showHide()
79
80         def showHide(self):
81                 if config.plugins.PermanentClock.enabled.value:
82                         self.dialog.show()
83                 else:
84                         self.dialog.hide()
85
86 pClock = PermanentClock()
87 \r
88 ##############################################################################
89
90 class PermanentClockPositioner(Screen):
91         def __init__(self, session):
92                 Screen.__init__(self, session)
93                 self.skin = SKIN
94                 
95                 self["actions"] = ActionMap(["WizardActions"],\r
96                 {\r
97                         "left": self.left,\r
98                         "up": self.up,\r
99                         "right": self.right,\r
100                         "down": self.down,\r
101                         "ok": self.ok,\r
102                         "back": self.exit\r
103                 }, -1)
104                 
105                 desktop = getDesktop(0)
106                 self.desktopWidth = desktop.size().width()
107                 self.desktopHeight = desktop.size().height()
108                 
109                 self.moveTimer = eTimer()\r
110                 self.moveTimer.callback.append(self.movePosition)\r
111                 self.moveTimer.start(50, 1)
112
113         def movePosition(self):\r
114                 self.instance.move(ePoint(config.plugins.PermanentClock.position_x.value, config.plugins.PermanentClock.position_y.value))\r
115                 self.moveTimer.start(50, 1)\r
116 \r
117         def left(self):\r
118                 value = config.plugins.PermanentClock.position_x.value\r
119                 value -= 1
120                 if value < 0:
121                         value = 0
122                 config.plugins.PermanentClock.position_x.value = value\r
123 \r
124         def up(self):\r
125                 value = config.plugins.PermanentClock.position_y.value\r
126                 value -= 1
127                 if value < 0:
128                         value = 0
129                 config.plugins.PermanentClock.position_y.value = value\r
130 \r
131         def right(self):\r
132                 value = config.plugins.PermanentClock.position_x.value\r
133                 value += 1
134                 if value > self.desktopWidth:
135                         value = self.desktopWidth
136                 config.plugins.PermanentClock.position_x.value = value\r
137 \r
138         def down(self):\r
139                 value = config.plugins.PermanentClock.position_y.value\r
140                 value += 1
141                 if value > self.desktopHeight:
142                         value = self.desktopHeight
143                 config.plugins.PermanentClock.position_y.value = value\r
144 \r
145         def ok(self):\r
146                 config.plugins.PermanentClock.position_x.save()\r
147                 config.plugins.PermanentClock.position_y.save()\r
148                 self.close()\r
149 \r
150         def exit(self):
151                 config.plugins.PermanentClock.position_x.cancel()\r
152                 config.plugins.PermanentClock.position_y.cancel()\r
153                 self.close()
154
155 ##############################################################################
156
157 class PermanentClockMenu(Screen):
158         skin = """
159                 <screen position="150,235" size="420,105" title="%s">
160                         <widget name="list" position="10,10" size="400,85" />
161                 </screen>""" % _("Permanent Clock")
162
163         def __init__(self, session):
164                 Screen.__init__(self, session)
165                 self.session = session
166                 self["list"] = MenuList([])
167                 self["actions"] = ActionMap(["OkCancelActions"], {"ok": self.okClicked, "cancel": self.close}, -1)
168                 self.onLayoutFinish.append(self.showMenu)
169
170         def showMenu(self):
171                 list = []
172                 if config.plugins.PermanentClock.enabled.value:
173                         list.append(_("Deactivate permanent clock"))
174                 else:
175                         list.append(_("Activate permanent clock"))
176                 list.append(_("Change permanent clock position"))
177                 self["list"].setList(list)
178
179         def okClicked(self):
180                 sel = self["list"].getCurrent()
181                 if pClock.dialog is None:
182                         pClock.gotSession(self.session)
183                 if sel == _("Deactivate permanent clock") or sel == _("Activate permanent clock"):
184                         pClock.changeVisibility()
185                         self.showMenu()
186                 else:
187                         pClock.dialog.hide()
188                         self.session.openWithCallback(self.positionerCallback, PermanentClockPositioner)
189
190         def positionerCallback(self, callback=None):
191                 pClock.showHide()
192
193 ##############################################################################\r
194 \r
195 def sessionstart(reason, **kwargs):
196         if reason == 0:
197                 pClock.gotSession(kwargs["session"])\r
198 \r
199 def startConfig(session, **kwargs):\r
200         session.open(PermanentClockMenu)
201
202 def main(menuid):
203         if menuid != "system": 
204                 return [ ]
205         return [(_("Permanent Clock"), startConfig, "permanent_clock", None)]\r
206 \r
207 ##############################################################################\r
208 \r
209 def Plugins(**kwargs):\r
210         return [
211                 PluginDescriptor(where=[PluginDescriptor.WHERE_SESSIONSTART], fnc=sessionstart),\r
212                 PluginDescriptor(name=_("Permanent Clock"), description=_("Shows the clock permanent on the screen"), where=PluginDescriptor.WHERE_MENU, fnc=main)]\r