initial checkin of MerlinSkinThemes
[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 from enigma import getDesktop
18
19 import string
20 import sys 
21 import time
22 from random import Random 
23
24 setpasswd_title=_("Change Root Password")
25
26 sz_w = getDesktop(0).size().width()  
27
28 class ChangePasswdScreen(Screen):
29         if sz_w == 1920:  
30                 skin = """
31                 <screen name="ChangePasswdScreen" position="center,center" size="1200,150" title="Change Root Password">
32                         <ePixmap pixmap="skin_default/buttons/red.png" position="10,5" size="295,70" />
33                         <ePixmap pixmap="skin_default/buttons/green.png" position="305,5" size="295,70" />
34                         <ePixmap pixmap="skin_default/buttons/yellow.png" position="600,5" size="295,70" />
35                         <ePixmap pixmap="skin_default/buttons/blue.png" position="895,5" size="295,70" />
36                         <widget backgroundColor="#9f1313" font="Regular;30" halign="center" position="10,5" render="Label" foregroundColor="white" shadowColor="black" shadowOffset="-2,-2" size="295,70" source="key_red" transparent="1" valign="center" zPosition="1" />
37                         <widget backgroundColor="#1f771f" font="Regular;30" halign="center" position="305,5" render="Label" foregroundColor="white" shadowColor="black" shadowOffset="-2,-2" size="295,70" source="key_green" transparent="1" valign="center" zPosition="1" />
38                         <widget backgroundColor="#a08500" font="Regular;30" halign="center" position="600,5" render="Label" foregroundColor="white" shadowColor="black" shadowOffset="-2,-2" size="295,70" source="key_yellow" transparent="1" valign="center" zPosition="1" />
39                         <widget backgroundColor="#18188b" font="Regular;30" halign="center" position="895,5" render="Label" foregroundColor="white" shadowColor="black" shadowOffset="-2,-2" size="295,70" source="key_blue" transparent="1" valign="center" zPosition="1" />
40                         <eLabel backgroundColor="grey" position="10,80" size="1180,1" />
41                         <widget name="passwd" position="10,90" size="1180,45" />
42                 </screen>"""
43         else:
44                 skin="""
45                 <screen name="ChangePasswdScreen" position="center,center" size="820,100" title="Change Root Password" >
46                         <ePixmap pixmap="skin_default/buttons/red.png" position="10,5" size="200,40" alphatest="on" />
47                         <ePixmap pixmap="skin_default/buttons/green.png" position="210,5" size="200,40" alphatest="on" />
48                         <ePixmap pixmap="skin_default/buttons/yellow.png" position="410,5" size="200,40" alphatest="on" />
49                         <ePixmap pixmap="skin_default/buttons/blue.png" position="610,5" size="200,40" alphatest="on" />
50                         <widget source="key_red" render="Label" position="10,5" size="200,40" zPosition="1" font="Regular;20" halign="center" valign="center" backgroundColor="#9f1313" transparent="1" shadowColor="black" shadowOffset="-2,-2" />
51                         <widget source="key_green" render="Label" position="210,5" size="200,40" zPosition="1" font="Regular;20" halign="center" valign="center" backgroundColor="#1f771f" transparent="1" shadowColor="black" shadowOffset="-2,-2" />
52                         <widget source="key_yellow" render="Label" position="410,5" size="200,40" zPosition="1" font="Regular;20" halign="center" valign="center" backgroundColor="#a08500" transparent="1" shadowColor="black" shadowOffset="-2,-2" />
53                         <widget source="key_blue" render="Label" position="610,5" size="200,40" zPosition="1" font="Regular;20" halign="center" valign="center" backgroundColor="#18188b" transparent="1" shadowColor="black" shadowOffset="-2,-2" />
54                         <eLabel position="10,50" size="800,1" backgroundColor="grey" />
55                         <widget name="passwd" position="10,60" size="800,45" scrollbarMode="showOnDemand" />
56                 </screen>"""
57
58         def __init__(self, session, args = 0):
59                 Screen.__init__(self, session)
60                 self.skin = ChangePasswdScreen.skin
61
62                 self.user="root"
63                 self.output_line = ""
64                 self.list = []
65
66                 self.onShown.append(self.setWindowTitle)
67                 
68                 self["passwd"] = ConfigList(self.list)
69                 self["key_red"] = StaticText(_("Password")+" "+_("delete").lower())
70                 self["key_green"] = StaticText(_("Set Password"))
71                 self["key_yellow"] = StaticText(_("new Random"))
72                 self["key_blue"] = StaticText(_("virt. Keyboard"))
73
74                 self["actions"] = ActionMap(["OkCancelActions", "ColorActions"],
75                                 {
76                                                 "exit": self.close,
77                                                 "red": self.emptyPasswd,
78                                                 "green": self.SetPasswd,
79                                                 "yellow": self.newRandom,
80                                                 "blue": self.bluePressed,
81                                                 "cancel": self.close
82                                 }, -1)
83         
84                 self.buildList(self.GeneratePassword())
85
86         def setWindowTitle(self):
87                 if config.usage.setup_level.value != "expert":
88                         p=open("/etc/shadow","r")
89                         rp=p.readline()
90                         p.close()
91                         if not rp.startswith("root::"): # root password already set but not expert ...
92                                 message=_("Unable to change/reset password for %s user") % self.user
93                                 type=MessageBox.TYPE_ERROR
94                                 self.session.open(MessageBox, message , type)
95                                 self.close()
96                 else:
97                         self.setTitle(setpasswd_title)
98
99         def newRandom(self):
100                 self.buildList(self.GeneratePassword())
101         
102         def buildList(self, password):
103                 self.password=password
104                 self.list = []
105                 self.list.append(getConfigListEntry(_('Enter new Password'), ConfigText(default = self.password, fixed_size = False)))
106                 self["passwd"].setList(self.list)
107                 
108         def GeneratePassword(self): 
109                 passwdChars = string.letters + string.digits
110                 passwdLength = 8
111                 return ''.join(Random().sample(passwdChars, passwdLength)) 
112
113         def SetPasswd(self):
114                 print "Changing password for %s to %s" % (self.user,self.password) 
115                 self.container = eConsoleAppContainer()
116                 self.appClosed_conn = self.container.appClosed.connect(self.runFinished)
117                 self.container.execute("echo \"%s:%s\" | chpasswd" % (self.user,self.password))
118
119         def emptyPasswd(self):
120                 print "Emptying password for %s" % (self.user) 
121                 self.password=_("empty")
122                 self.container = eConsoleAppContainer()
123                 self.appClosed_conn = self.container.appClosed.connect(self.runFinished)
124                 self.container.execute("passwd -d %s" % self.user)
125
126         def runFinished(self,retval):
127                 if retval==0:
128                         message=_("Sucessfully changed password for %s user to: %s") % (self.user, self.password)
129                         type=MessageBox.TYPE_INFO
130                 else:
131                         message=_("Unable to change/reset password for %s user") % self.user
132                         type=MessageBox.TYPE_ERROR
133                 self.session.open(MessageBox, message , type)
134                 del self.container
135                 self.close()
136                 
137         def bluePressed(self):
138                 self.session.openWithCallback(self.VirtualKeyBoardTextEntry, VirtualKeyBoard, title = (_("Enter your password here:")), text = self.password)
139         
140         def VirtualKeyBoardTextEntry(self, callback = None):
141                 if callback is not None and len(callback):
142                         self.buildList(callback)
143
144 def startChange(menuid):
145         if menuid != "system": 
146                 return [ ]
147         return [(setpasswd_title, main, "change_root_passwd", 50)]
148
149 def main(session, **kwargs):
150         session.open(ChangePasswdScreen)
151
152 def Plugins(**kwargs):
153         return PluginDescriptor(
154                 name=setpasswd_title,  
155                 description=_("Change or reset the root password of your dreambox"),
156                 where = [PluginDescriptor.WHERE_MENU], fnc = startChange)
157