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