Merge branch 'master' into master_internal
[enigma2-plugins.git] / networkbrowser / src / MountManager.py
1 # -*- coding: utf-8 -*-
2 # for localized messages
3 #from __init__ import _
4 from enigma import eConsoleAppContainer
5 from Screens.Screen import Screen
6 from Screens.MessageBox import MessageBox
7 from Screens.VirtualKeyBoard import VirtualKeyBoard
8 from Components.Sources.StaticText import StaticText
9 from Components.ActionMap import ActionMap
10 from Components.Sources.List import List
11 from Tools.LoadPixmap import LoadPixmap
12 from Tools.Directories import resolveFilename, SCOPE_PLUGINS
13 from os import path as os_path, fsync
14
15 from MountView import AutoMountView
16 from MountEdit import AutoMountEdit
17 from UserManager import UserManager
18
19 class AutoMountManager(Screen):
20         skin = """
21                 <screen name="AutoMountManager" position="center,center" size="560,400" title="AutoMountManager">
22                         <ePixmap pixmap="skin_default/buttons/red.png" position="0,0" size="140,40" alphatest="on" />
23                         <widget source="key_red" render="Label" position="0,0" zPosition="1" size="140,40" font="Regular;20" halign="center" valign="center" backgroundColor="#9f1313" transparent="1" />
24                         <widget source="config" render="Listbox" position="5,50" size="540,300" 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                         <ePixmap pixmap="skin_default/div-h.png" position="0,360" zPosition="1" size="560,2" />
37                         <widget source="introduction" render="Label" position="10,370" size="540,21" zPosition="10" font="Regular;21" halign="center" valign="center" backgroundColor="#25062748" transparent="1"/>
38                 </screen>"""
39         def __init__(self, session, iface ,plugin_path):
40                 self.skin_path = plugin_path
41                 self.session = session
42                 self.hostname = None
43                 self._applyHostnameMsgBox = None
44                 Screen.__init__(self, session)
45                 self["shortcuts"] = ActionMap(["ShortcutActions", "WizardActions"],
46                 {
47                         "ok": self.keyOK,
48                         "back": self.exit,
49                         "cancel": self.exit,
50                         "red": self.exit,
51                 })
52                 self["key_red"] = StaticText(_("Close"))
53                 self["introduction"] = StaticText(_("Press OK to select."))
54
55                 self.list = []
56                 self["config"] = List(self.list)
57                 self.updateList()
58                 self.onShown.append(self.setWindowTitle)
59                 self._appContainer = eConsoleAppContainer()
60                 self._appClosed_conn = self.container.appClosed.connect(self._containerCmdFinished)
61
62
63         def setWindowTitle(self):
64                 self.setTitle(_("MountManager"))
65
66         def updateList(self):
67                 self.list = []
68                 okpng = LoadPixmap(cached=True, path=resolveFilename(SCOPE_PLUGINS, "SystemPlugins/NetworkBrowser/icons/ok.png"))
69                 self.list.append((_("Add new network mount point"),"add", _("Add a new NFS or CIFS mount point to your Dreambox."), okpng ))
70                 self.list.append((_("Mountpoints management"),"view", _("View, edit or delete mountpoints on your Dreambox."), okpng ))
71                 self.list.append((_("User management"),"user", _("View, edit or delete usernames and passwords for your network."), okpng))
72                 self.list.append((_("Change hostname"),"hostname", _("Change the hostname of your Dreambox."), okpng))
73                 self["config"].setList(self.list)
74
75         def exit(self):
76                 self.close()
77
78         def keyOK(self, returnValue = None):
79                 if returnValue == None:
80                         returnValue = self["config"].getCurrent()[1]
81                         if returnValue is "add":
82                                 self.addMount()
83                         elif returnValue is "view":
84                                 self.viewMounts()
85                         elif returnValue is "user":
86                                 self.userEdit()
87                         elif returnValue is "hostname":
88                                 self.hostEdit()
89
90         def addMount(self):
91                 self.session.open(AutoMountEdit, self.skin_path)
92
93         def viewMounts(self):
94                 self.session.open(AutoMountView, self.skin_path)
95
96         def userEdit(self):
97                 self.session.open(UserManager, self.skin_path)
98
99         def hostEdit(self):
100                 if os_path.exists("/etc/hostname"):
101                         fp = open('/etc/hostname', 'r')
102                         self.hostname = fp.read()
103                         fp.close()
104                         self.session.openWithCallback(self.hostnameCallback, VirtualKeyBoard, title = (_("Enter new hostname for your Dreambox")), text = self.hostname)
105
106         def hostnameCallback(self, callback = None):
107                 if callback is not None and len(callback):
108                         fp = open('/etc/hostname', 'w+')
109                         fp.write(callback)
110                         fp.flush()
111                         fsync(fp.fileno())
112                         fp.close()
113                         self.applyHostname()
114
115         def applyHostname(self):
116                 binary = '/bin/hostname'
117                 cmd = [binary, binary, '-F', '/etc/hostname']
118                 self._appContainer.execute(*cmd)
119                 self._applyHostnameMsgBox = self.session.openWithCallback(self._onApplyHostnameFinished, MessageBox, _("Please wait while the new hostname is being applied..."), type = MessageBox.TYPE_INFO, enable_input = False)
120
121
122         def _onApplyHostnameFinished(self,data):
123                 if data:
124                         self.session.open(MessageBox, _("Hostname has been applied!"), type = MessageBox.TYPE_INFO, timeout = 10, default = False)
125