remove duplicate code
[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                         self.session.open(MessageBox, _("Sucessfully changed password for root user to: %s " % self.password), MessageBox.TYPE_INFO)    
89                 else:
90                         self.session.open(MessageBox, _("Unable to change/reset password for root user"), MessageBox.TYPE_ERROR)        
91
92         def dataAvail(self,data):
93                 self.output_line += data
94                 while True:
95                         i = self.output_line.find('\n')
96                         if i == -1:
97                                 break
98                         self.processOutputLine(self.output_line[:i+1])
99                         self.output_line = self.output_line[i+1:]
100
101         def processOutputLine(self,line):
102                 if line.find('password: '):
103                         self.container.write("%s\n"%self.password)
104
105         def runFinished(self,retval):
106                 del self.container.dataAvail[:]
107                 del self.container.appClosed[:]
108                 del self.container
109                 self.close()
110                 
111         def bluePressed(self):
112                 self.session.openWithCallback(self.VirtualKeyBoardTextEntry, VirtualKeyBoard, title = (_("Enter your password here:")), text = self.password)
113         
114         def VirtualKeyBoardTextEntry(self, callback = None):
115                 if callback is not None and len(callback):
116                         self.buildList(callback)
117
118 def startChange(menuid):
119         if menuid != "system": 
120                 return [ ]
121         return [(title, main, "change_root_passwd", 50)]
122
123 def main(session, **kwargs):
124         session.open(ChangePasswdScreen)
125
126 def Plugins(**kwargs):
127         return PluginDescriptor(
128                 name=title, 
129                 description=_("Change or reset the root password of your dreambox"),
130                 where = [PluginDescriptor.WHERE_MENU], fnc = startChange)
131