use celementtree to parse config,
[enigma2-plugins.git] / multirc / src / plugin.py
1 from Plugins.Plugin import PluginDescriptor
2 from Screens.Screen import Screen
3 from Screens.MessageBox import MessageBox
4 from Components.ConfigList import ConfigListScreen
5 from Components.ActionMap import ActionMap
6 from Components.Label import Label
7 from Components.config import *
8 from os import path
9
10 CONFIGS = [("1", _("layer 1")),
11            ("2", _("layer 2")),
12            ("4", _("layer 3")),
13            ("8", _("layer 4")),
14            ("f", _("any RC"))]
15
16 config.plugins.MultiRC = ConfigSubsection()
17 config.plugins.MultiRC.mask = ConfigSelection(choices = CONFIGS, default = "f")
18
19 # config file for IR mask, default is DM7025 or later, fallback to DM500/600
20 MASK = "/proc/stb/ir/rc/mask0"
21 if not path.exists(MASK):
22         MASK = "/proc/stb/ir/rc/mask"
23
24 class MultiRCSetup(ConfigListScreen, Screen):
25         skin = """
26                 <screen position="170,150" size="420,280" title="Multi RemoteControl" >
27                 <widget name="config" position="10,10" size="400,40" scrollbarMode="showOnDemand" />
28                 <widget name="warning" position="10,50" size="400,220" font="Regular;20" halign="center"/>
29                 </screen>"""
30
31         # most of the following is black magic copied from other plugins.
32         # e2 devs should really make some best practices or wrapper for this!
33         def __init__(self, session, args = None):
34                 Screen.__init__(self, session)
35                 self.list = [getConfigListEntry(_("Listen on Remote Control"), config.plugins.MultiRC.mask)]
36                 ConfigListScreen.__init__(self, self.list)
37                 self["warning"] = Label(_("""WARNING!
38 After changing this and pressing <Ok>, your Remote Control might stop working!
39
40 Information about re-configuring the RC is available at http://www.dream-multimedia-tv.de/board/thread.php?threadid=5613"""))
41                 self["config"].list = self.list
42                 self["config"].setList(self.list)
43                 self["setupActions"] = ActionMap(["SetupActions"],
44                 {
45                         "save": self.ask_save,
46                         "cancel": self.cancel,
47                         "ok": self.ask_save,
48                 }, -2)
49
50         def ask_save(self):
51                 if not set_mask():
52                         self.session.open(MessageBox, text = _("Error writing to %s!") % MASK,
53                                 type = MessageBox.TYPE_WARNING)
54                         return
55                 # mask value 0xf allows all RCs, no need to verify
56                 if config.plugins.MultiRC.mask.value == "f":
57                         self.confirm_save(True)
58                 else:
59                         self.session.openWithCallback(self.confirm_save, MessageBox,
60                                 _("Is the RC still working?"), MessageBox.TYPE_YESNO,
61                                 timeout = 20, default = False)
62
63         def confirm_save(self, confirmed):
64                 if confirmed:
65                         for x in self["config"].list:
66                                 x[1].save()
67                         set_mask()
68                         self.close()
69                 else:
70                         # input failed, reset to any RC
71                         set_mask("f")
72
73         def cancel(self):
74                 for x in self["config"].list:
75                         x[1].cancel()
76                 set_mask()
77                 self.close()
78
79 def set_mask(mask=None):
80         if not mask:
81                 mask = config.plugins.MultiRC.mask.value
82         try:
83                 f = open(MASK, "w")
84                 f.write(mask)
85                 f.close()
86         except Exception, e:
87                 return False
88         return True
89
90 def multirc_setup(session, **kwargs):
91         session.open(MultiRCSetup)
92
93 def multirc_autostart(reason, **kwargs):
94         # on startup, set correct remote mask
95         if reason == 0:
96                 set_mask()
97                 pass
98
99 def Plugins(**kwargs):
100         return [PluginDescriptor(name="Multi RemoteControl",
101                                 description=_("Multi Dreambox RC layer setup"),
102                                 where=PluginDescriptor.WHERE_PLUGINMENU,
103                                 fnc = multirc_setup),
104
105                 PluginDescriptor(where=PluginDescriptor.WHERE_AUTOSTART,
106                                 fnc = multirc_autostart)]
107
108