1.4.2: Changes from Enigma integrated:
[enigma2-plugins.git] / setpasswd / src / plugin.py
1 from enigma import eConsoleAppContainer
2
3 from Screens.Screen import Screen
4 from Screens.MessageBox import MessageBox
5 from Screens.VirtualKeyBoard import VirtualKeyBoard
6
7 from Screens.Setup import SetupSummary
8 from Components.ConfigList import ConfigList
9 from Components.config import config, getConfigListEntry, ConfigSelection, ConfigSubsection, ConfigText
10
11 from Components.ActionMap import ActionMap
12 from Components.Label import Label
13 from Components.Pixmap import Pixmap
14 from Components.Sources.StaticText import StaticText
15 from Components.Sources.List import List
16 from Plugins.Plugin import PluginDescriptor
17
18 from __init__ import _
19
20 import string
21 import sys 
22 import time
23 from random import Random 
24
25 title=_("Change Root Password")
26
27 class ChangePasswdScreen(Screen):
28         skin = """
29                 <screen position="65,160" size="585,250" title="%s" >
30                 <widget name="passwd" position="10,10" size="565,200" scrollbarMode="showOnDemand" />
31                 <ePixmap pixmap="skin_default/div-h.png" position="10,205" size="565,2" transparent="1" alphatest="on" />
32                 <ePixmap pixmap="skin_default/buttons/red.png" position="5,210" size="140,40" alphatest="on" />
33                 <ePixmap pixmap="skin_default/buttons/green.png" position="150,210" size="140,40" alphatest="on" />
34                 <ePixmap pixmap="skin_default/buttons/yellow.png" position="295,210" size="140,40" alphatest="on" />
35                 <ePixmap pixmap="skin_default/buttons/blue.png" position="440,210" size="140,40" alphatest="on" />
36                 <widget source="key_red" render="Label" position="5,210" zPosition="1" size="140,40" font="Regular;17" halign="center" valign="center" backgroundColor="#9f1313" transparent="1" />
37                 <widget source="key_green" render="Label" position="150,210" zPosition="1" size="140,40" font="Regular;17" halign="center" valign="center" backgroundColor="#1f771f" transparent="1" />
38                 <widget source="key_yellow" render="Label" position="295,210" zPosition="1" size="140,40" font="Regular;17" halign="center" valign="center" backgroundColor="#1f771f" transparent="1" />
39                 <widget source="key_blue" render="Label" position="440,210" zPosition="1" size="140,40" font="Regular;17" halign="center" valign="center" backgroundColor="#1f771f" transparent="1" />
40         </screen>""" % title
41
42         def __init__(self, session, args = 0):
43                 Screen.__init__(self, session)
44                 self.skin = ChangePasswdScreen.skin
45
46                 self.user="root"
47                 self.output_line = ""
48                 self.list = []
49                 
50                 self["passwd"] = ConfigList(self.list)
51                 self["key_red"] = StaticText(_("Cancel"))
52                 self["key_green"] = StaticText(_("Set Password"))
53                 self["key_yellow"] = StaticText(_("new Random"))
54                 self["key_blue"] = StaticText(_("virt. Keyboard"))
55
56                 self["actions"] = ActionMap(["OkCancelActions", "ColorActions"],
57                                 {
58                                                 "red": self.close,
59                                                 "green": self.SetPasswd,
60                                                 "yellow": self.newRandom,
61                                                 "blue": self.bluePressed,
62                                                 "cancel": self.close
63                                 }, -1)
64         
65                 self.buildList(self.GeneratePassword())
66
67         def newRandom(self):
68                 self.buildList(self.GeneratePassword())
69         
70         def buildList(self, password):
71                 self.password=password
72                 self.list = []
73                 self.list.append(getConfigListEntry(_('Enter new Password'), ConfigText(default = self.password, fixed_size = False)))
74                 self["passwd"].setList(self.list)
75                 
76         def GeneratePassword(self): 
77                 passwdChars = string.letters + string.digits
78                 passwdLength = 8
79                 return ''.join(Random().sample(passwdChars, passwdLength)) 
80
81         def SetPasswd(self):
82                 print "Changing password for %s to %s" % (self.user,self.password) 
83                 self.container = eConsoleAppContainer()
84                 self.container.appClosed.append(self.runFinished)
85                 self.container.dataAvail.append(self.dataAvail)
86                 retval = self.container.execute("passwd %s" % self.user)
87                 if retval==0:
88                         message=_("Sucessfully changed password for root user to: ") + self.password
89                 else:
90                         message=_("Unable to change/reset password for root user")
91                 self.session.open(MessageBox, message , MessageBox.TYPE_ERROR)
92
93         def dataAvail(self,data):
94                 self.output_line += data
95                 while True:
96                         i = self.output_line.find('\n')
97                         if i == -1:
98                                 break
99                         self.processOutputLine(self.output_line[:i+1])
100                         self.output_line = self.output_line[i+1:]
101
102         def processOutputLine(self,line):
103                 if line.find('password: '):
104                         self.container.write("%s\n"%self.password)
105
106         def runFinished(self,retval):
107                 del self.container.dataAvail[:]
108                 del self.container.appClosed[:]
109                 del self.container
110                 self.close()
111                 
112         def bluePressed(self):
113                 self.session.openWithCallback(self.VirtualKeyBoardTextEntry, VirtualKeyBoard, title = (_("Enter your password here:")), text = self.password)
114         
115         def VirtualKeyBoardTextEntry(self, callback = None):
116                 if callback is not None and len(callback):
117                         self.buildList(callback)
118
119 def startChange(menuid):
120         if menuid != "system": 
121                 return [ ]
122         return [(title, main, "change_root_passwd", 50)]
123
124 def main(session, **kwargs):
125         session.open(ChangePasswdScreen)
126
127 def Plugins(**kwargs):
128         return PluginDescriptor(
129                 name=title, 
130                 description=_("Change or reset the root password of your dreambox"),
131                 where = [PluginDescriptor.WHERE_MENU], fnc = startChange)
132