[AutoTimer]Update German localization
[enigma2-plugins.git] / permanentclock / src / plugin.py
1 ##
2 ## Permanent Clock
3 ## by AliAbdul
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
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()
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]
28         gettext.bindtextdomain("enigma2", resolveFilename(SCOPE_LANGUAGE))
29         gettext.textdomain("enigma2")
30         gettext.bindtextdomain("PermanentClock", "%s%s" % (resolveFilename(SCOPE_PLUGINS), "Extensions/PermanentClock/locale/"))
31
32 def _(txt):
33         t = gettext.dgettext("PermanentClock", txt)
34         if t == txt:
35                 t = gettext.gettext(txt)
36         return t
37
38 localeInit()
39 language.addCallback(localeInit)
40
41 ##############################################################################
42
43 SKIN = """
44         <screen position="0,0" size="120,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")
49
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
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"],
96                 {
97                         "left": self.left,
98                         "up": self.up,
99                         "right": self.right,
100                         "down": self.down,
101                         "ok": self.ok,
102                         "back": self.exit
103                 }, -1)
104                 
105                 desktop = getDesktop(0)
106                 self.desktopWidth = desktop.size().width()
107                 self.desktopHeight = desktop.size().height()
108                 
109                 self.moveTimer = eTimer()
110                 self.moveTimer.callback.append(self.movePosition)
111                 self.moveTimer.start(50, 1)
112
113         def movePosition(self):
114                 self.instance.move(ePoint(config.plugins.PermanentClock.position_x.value, config.plugins.PermanentClock.position_y.value))
115                 self.moveTimer.start(50, 1)
116
117         def left(self):
118                 value = config.plugins.PermanentClock.position_x.value
119                 value -= 1
120                 if value < 0:
121                         value = 0
122                 config.plugins.PermanentClock.position_x.value = value
123
124         def up(self):
125                 value = config.plugins.PermanentClock.position_y.value
126                 value -= 1
127                 if value < 0:
128                         value = 0
129                 config.plugins.PermanentClock.position_y.value = value
130
131         def right(self):
132                 value = config.plugins.PermanentClock.position_x.value
133                 value += 1
134                 if value > self.desktopWidth:
135                         value = self.desktopWidth
136                 config.plugins.PermanentClock.position_x.value = value
137
138         def down(self):
139                 value = config.plugins.PermanentClock.position_y.value
140                 value += 1
141                 if value > self.desktopHeight:
142                         value = self.desktopHeight
143                 config.plugins.PermanentClock.position_y.value = value
144
145         def ok(self):
146                 config.plugins.PermanentClock.position_x.save()
147                 config.plugins.PermanentClock.position_y.save()
148                 self.close()
149
150         def exit(self):
151                 config.plugins.PermanentClock.position_x.cancel()
152                 config.plugins.PermanentClock.position_y.cancel()
153                 self.close()
154
155 ##############################################################################
156
157 class PermanentClockMenu(Screen):
158         skin = """
159                 <screen position="center,center" 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 ##############################################################################
194
195 def sessionstart(reason, **kwargs):
196         if reason == 0:
197                 pClock.gotSession(kwargs["session"])
198
199 def startConfig(session, **kwargs):
200         session.open(PermanentClockMenu)
201
202 def main(menuid):
203         if menuid != "system": 
204                 return [ ]
205         return [(_("Permanent Clock"), startConfig, "permanent_clock", None)]
206
207 ##############################################################################
208
209 def Plugins(**kwargs):
210         return [
211                 PluginDescriptor(where=[PluginDescriptor.WHERE_SESSIONSTART], fnc=sessionstart),
212                 PluginDescriptor(name=_("Permanent Clock"), description=_("Shows the clock permanent on the screen"), where=PluginDescriptor.WHERE_MENU, fnc=main)]