added virtual keyboard
[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
6 from Screens.Setup import SetupSummary
7 from Components.ConfigList import ConfigList
8 from Components.config import config, getConfigListEntry, ConfigSelection, ConfigSubsection, ConfigText
9
10 from Components.ActionMap import ActionMap
11 from Components.Label import Label
12 from Components.Pixmap import Pixmap
13 from Components.Sources.StaticText import StaticText
14 from Components.Sources.List import List
15 from Plugins.Plugin import PluginDescriptor
16
17 from __init__ import _
18
19 import string
20 import sys 
21 import time
22 from random import Random 
23
24 title=_("Change Root Password")
25
26 class ChangePasswdScreen(Screen):
27         skin = """
28                 <screen position="110,145" size="490,260" title="%s" >
29                 <widget name="passwd" position="10,10" size="470,200" scrollbarMode="showOnDemand" />
30                 <ePixmap pixmap="skin_default/div-h.png" position="10,205" size="470,2" transparent="1" alphatest="on" />
31                 <ePixmap pixmap="skin_default/buttons/red.png" position="10,210" size="150,40" alphatest="on" />
32                 <ePixmap pixmap="skin_default/buttons/green.png" position="170,210" size="150,40" alphatest="on" />
33                 <ePixmap pixmap="skin_default/buttons/yellow.png" position="330,210" size="150,40" alphatest="on" />
34                 <widget source="key_red" render="Label" position="10,210" zPosition="1" size="150,40" font="Regular;17" halign="center" valign="center" backgroundColor="#9f1313" transparent="1" />
35                 <widget source="key_green" render="Label" position="170,210" zPosition="1" size="150,40" font="Regular;17" halign="center" valign="center" backgroundColor="#1f771f" transparent="1" />
36                 <widget source="key_yellow" render="Label" position="330,210" zPosition="1" size="150,40" font="Regular;17" halign="center" valign="center" backgroundColor="#1f771f" transparent="1" />
37         </screen>""" % _("Change Root Password")
38         
39         def __init__(self, session, args = 0):
40                 Screen.__init__(self, session)
41                 self.skin = ChangePasswdScreen.skin
42
43                 self.user="root"
44                 self.output_line = ""
45                 
46                 self.password=self.GeneratePassword()
47                 self.list = []
48                 self.list.append(getConfigListEntry(_('Enter new Password'), ConfigText(default = self.password, fixed_size = False)))      
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
55                 self["actions"] = ActionMap(["OkCancelActions", "ColorActions"],
56                                 {
57                                                 "green": self.SetPasswd,
58                                                 "red": self.close,
59                                                 "yellow": self.buildList,
60                                                 "cancel": self.close
61                                 }, -1)
62
63         def buildList(self):
64                 self.password=self.GeneratePassword()
65                 self.list = []
66                 self.list.append(getConfigListEntry(_('Enter new Password'), ConfigText(default = self.password, fixed_size = False)))
67                 self["passwd"].setList(self.list)
68                 
69         def GeneratePassword(self): 
70                 passwdChars = string.letters + string.digits
71 #               passwdChars = string.letters + string.digits + '~!@#$%^&*-_=+?'   # use for more security :)
72                 passwdLength = 8
73                 return ''.join(Random().sample(passwdChars, passwdLength)) 
74
75         def SetPasswd(self):
76                 print "Changing password for %s to %s" % (self.user,self.password) 
77                 self.container = eConsoleAppContainer()
78                 self.container.appClosed.append(self.runFinished)
79                 self.container.dataAvail.append(self.dataAvail)
80                 retval = self.container.execute("passwd %s" % self.user)
81                 if retval==0:
82                         self.session.open(MessageBox, _("Sucessfully changed password for root user to: %s " % self.password), MessageBox.TYPE_INFO)    
83                 else:
84                         self.session.open(MessageBox, _("Unable to change/reset password for root user"), MessageBox.TYPE_ERROR)        
85
86         def dataAvail(self,data):
87                 self.output_line += data
88                 while True:
89                         i = self.output_line.find('\n')
90                         if i == -1:
91                                 break
92                         self.processOutputLine(self.output_line[:i+1])
93                         self.output_line = self.output_line[i+1:]
94
95         def processOutputLine(self,line):
96                 if line.find('password: '):
97                         self.container.write("%s\n"%self.password)
98
99         def runFinished(self,retval):
100                 del self.container.dataAvail[:]
101                 del self.container.appClosed[:]
102                 del self.container
103                 self.close()
104
105 def startChange(menuid):
106         if menuid != "system": 
107                 return [ ]
108         return [(title, main, "change_root_passwd", 50)]
109
110 def main(session, **kwargs):
111         session.open(ChangePasswdScreen)
112
113 def Plugins(**kwargs):
114         return PluginDescriptor(
115                 name=title, 
116                 description=_("Change or reset the root password of your dreambox"),
117                 where = [PluginDescriptor.WHERE_MENU], fnc = startChange)
118