fix selection and bsod
[enigma2-plugins.git] / networkbrowser / src / MountManager.py
1 # -*- coding: utf-8 -*-
2 # for localized messages
3 from __init__ import _
4 from Screens.Screen import Screen
5 from Screens.MessageBox import MessageBox
6 from Screens.VirtualKeyBoard import VirtualKeyBoard
7 from Components.Label import Label
8 from Components.Pixmap import Pixmap
9 from Components.ActionMap import ActionMap
10 from Components.Network import iNetwork
11 from Components.Sources.List import List
12 from Tools.LoadPixmap import LoadPixmap
13 from Tools.Directories import resolveFilename, SCOPE_PLUGINS, SCOPE_SKIN_IMAGE
14 from os import path as os_path
15
16 from MountView import AutoMountView
17 from MountEdit import AutoMountEdit
18 from AutoMount import iAutoMount, AutoMount
19 from UserManager import UserManager
20
21 class AutoMountManager(Screen):
22         skin = """
23                 <screen name="AutoMountManager" position="90,140" size="560,350" title="AutoMountManager">
24                         <widget source="config" render="Listbox" position="10,10" size="540,220" scrollbarMode="showOnDemand" >
25                                 <convert type="TemplatedMultiContent">
26                                         {"template": [
27                                                         MultiContentEntryText(pos = (0, 3), size = (480, 25), font=0, flags = RT_HALIGN_LEFT, text = 0), # index 2 is the Menu Titel
28                                                         MultiContentEntryText(pos = (10, 29), size = (480, 17), font=1, flags = RT_HALIGN_LEFT, text = 2), # index 3 is the Description
29                                                         MultiContentEntryPixmapAlphaTest(pos = (500, 1), size = (48, 48), png = 3), # index 4 is the pixmap
30                                                 ],
31                                         "fonts": [gFont("Regular", 20),gFont("Regular", 14)],
32                                         "itemHeight": 50
33                                         }
34                                 </convert>
35                         </widget>
36                         <widget name="introduction" position="90,260" size="300,20" zPosition="10" font="Regular;21" halign="center" transparent="1" />
37                         <widget name="ButtonRedtext" position="410,305" size="140,21" zPosition="10" font="Regular;21" transparent="1" />
38                         <widget name="ButtonRed" pixmap="skin_default/buttons/button_red.png" position="390,305" zPosition="10" size="15,16" transparent="1" alphatest="on" />
39                         <ePixmap pixmap="skin_default/bottombar.png" position="10,250" size="540,120" zPosition="1" transparent="1" alphatest="on" />
40                 </screen>"""
41         def __init__(self, session, iface ,plugin_path):
42                 self.skin_path = plugin_path
43                 self.session = session
44                 self.hostname = None
45                 self.restartLanRef = None
46                 Screen.__init__(self, session)
47                 self["shortcuts"] = ActionMap(["ShortcutActions", "WizardActions"],
48                 {
49                         "ok": self.keyOK,
50                         "back": self.exit,
51                         "cancel": self.exit,
52                         "red": self.exit,
53                 })
54                 self["ButtonRed"] = Pixmap()
55                 self["ButtonRedtext"] = Label(_("Close"))
56                 self["introduction"] = Label(_("Press OK to select."))
57
58                 self.list = []
59                 self["config"] = List(self.list)
60                 self.updateList()
61                 self.onClose.append(self.cleanup)
62                 self.onShown.append(self.setWindowTitle)
63
64         def setWindowTitle(self):
65                 self.setTitle(_("MountManager"))
66
67         def cleanup(self):
68                 iNetwork.stopRestartConsole()
69                 iNetwork.stopGetInterfacesConsole()
70
71         def updateList(self):
72                 self.list = []
73                 okpng = LoadPixmap(cached=True, path=resolveFilename(SCOPE_PLUGINS, "SystemPlugins/NetworkBrowser/icons/ok.png"))
74                 self.list.append((_("Add new network mount point"),"add", _("Add a new NFS or CIFS mount point to your Dreambox."), okpng ))
75                 self.list.append((_("Mountpoints management"),"view", _("View, edit or delete mountpoints on your Dreambox."), okpng ))
76                 self.list.append((_("User management"),"user", _("View, edit or delete usernames and passwords for your network."), okpng))
77                 self.list.append((_("Change hostname"),"hostname", _("Change the hostname of your Dreambox."), okpng))
78                 self["config"].setList(self.list)
79
80         def exit(self):
81                 self.close()
82
83         def keyOK(self, returnValue = None):
84                 if returnValue == None:
85                         returnValue = self["config"].getCurrent()[1]
86                         if returnValue is "add":
87                                 self.addMount()
88                         elif returnValue is "view":
89                                 self.viewMounts()
90                         elif returnValue is "user":
91                                 self.userEdit()
92                         elif returnValue is "hostname":
93                                 self.hostEdit()
94
95         def addMount(self):
96                 self.session.open(AutoMountEdit, self.skin_path)
97
98         def viewMounts(self):
99                 self.session.open(AutoMountView, self.skin_path)
100
101         def userEdit(self):
102                 self.session.open(UserManager, self.skin_path)
103
104         def hostEdit(self):
105                 if os_path.exists("/etc/hostname"):
106                         fp = open('/etc/hostname', 'r')
107                         self.hostname = fp.read()
108                         fp.close()
109                         self.session.openWithCallback(self.hostnameCallback, VirtualKeyBoard, title = (_("Enter new hostname for your Dreambox")), text = self.hostname)
110
111         def hostnameCallback(self, callback = None):
112                 if callback is not None and len(callback):
113                         fp = open('/etc/hostname', 'w+')
114                         fp.write(callback)
115                         fp.close()
116                         self.restartLan()
117
118         def restartLan(self):
119                 iNetwork.restartNetwork(self.restartLanDataAvail)
120                 self.restartLanRef = self.session.openWithCallback(self.restartfinishedCB, MessageBox, _("Please wait while your network is restarting..."), type = MessageBox.TYPE_INFO, enable_input = False)
121
122         def restartLanDataAvail(self, data):
123                 if data is True:
124                         iNetwork.getInterfaces(self.getInterfacesDataAvail)
125
126         def getInterfacesDataAvail(self, data):
127                 if data is True:
128                         if self.restartLanRef.execing:
129                                 self.restartLanRef.close(True)
130
131         def restartfinishedCB(self,data):
132                 if data is True:
133                         self.session.open(MessageBox, _("Finished restarting your network"), type = MessageBox.TYPE_INFO, timeout = 10, default = False)
134