fix selection and bsod
[enigma2-plugins.git] / networkbrowser / src / MountEdit.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.ActionMap import ActionMap
8 from Components.Button import Button
9 from Components.config import config, ConfigIP, NoSave, ConfigText, ConfigEnableDisable, ConfigPassword, ConfigSelection, getConfigListEntry
10 from Components.ConfigList import ConfigListScreen
11 from Components.Label import Label
12 from Components.Pixmap import Pixmap
13 from Components.ActionMap import ActionMap, NumberActionMap
14 from enigma import ePoint
15 from AutoMount import iAutoMount, AutoMount
16
17 class AutoMountEdit(Screen, ConfigListScreen):
18         skin = """
19                 <screen name="AutoMountEdit" position="90,110" size="560,400" title="MountEdit">
20                         <widget name="config" position="10,10" size="540,200" zPosition="1" scrollbarMode="showOnDemand" />
21                         <widget name="ButtonGreen" pixmap="skin_default/buttons/button_green.png" position="30,365" zPosition="10" size="15,16" transparent="1" alphatest="on" />
22                         <widget name="introduction2" position="110,330" size="300,20" zPosition="10" font="Regular;21" halign="center" transparent="1" />
23                         <widget name="ButtonRedtext" position="410,365" size="140,21" zPosition="10" font="Regular;21" transparent="1" />
24                         <widget name="ButtonRed" pixmap="skin_default/buttons/button_red.png" position="390,365" zPosition="10" size="15,16" transparent="1" alphatest="on" />
25                         <widget name="VKeyIcon" pixmap="skin_default/vkey_icon.png" position="40,345" zPosition="10" size="60,48" transparent="1" alphatest="on" />
26                         <widget name="HelpWindow" pixmap="skin_default/vkey_icon.png" position="175,325" zPosition="1" size="1,1" transparent="1" alphatest="on" />
27                         <ePixmap pixmap="skin_default/bottombar.png" position="10,310" size="540,120" zPosition="1" transparent="1" alphatest="on" />
28                 </screen>"""
29
30         def __init__(self, session, plugin_path, mountinfo = None ):
31                 self.skin_path = plugin_path
32                 self.session = session
33                 Screen.__init__(self, self.session)
34
35                 self.mountinfo = mountinfo
36                 if self.mountinfo is None:
37                         #Initialize blank mount enty
38                         self.mountinfo = { 'isMounted': False, 'active': False, 'ip': False, 'sharename': False, 'sharedir': False, 'username': False, 'password': False, 'mounttype' : False, 'options' : False }
39
40                 self.applyConfigRef = None
41                 self.updateConfigRef = None
42                 self.mounts = iAutoMount.getMountsList()
43                 self.createConfig()
44
45                 self["actions"] = NumberActionMap(["SetupActions"],
46                 {
47                         "ok": self.ok,
48                         "back": self.close,
49                         "cancel": self.close,
50                         "red": self.close,
51                 }, -2)
52
53                 self["VirtualKB"] = ActionMap(["ShortcutActions"],
54                 {
55                         "green": self.KeyGreen,
56                 }, -2)
57
58                 self.list = []
59                 ConfigListScreen.__init__(self, self.list,session = self.session)
60                 self.createSetup()
61                 self.onLayoutFinish.append(self.layoutFinished)
62                 # Initialize Buttons
63                 self["ButtonGreen"] = Pixmap()
64                 self["VKeyIcon"] = Pixmap()
65                 self["HelpWindow"] = Pixmap()
66                 self["introduction2"] = Label(_("Press OK to activate the settings."))
67                 self["ButtonRed"] = Pixmap()
68                 self["ButtonRedtext"] = Label(_("Close"))
69
70
71         def layoutFinished(self):
72                 self.setTitle(_("Mounts editor"))
73                 self["ButtonGreen"].hide()
74                 self["VKeyIcon"].hide()
75                 self["VirtualKB"].setEnabled(False)
76                 self["HelpWindow"].hide()
77
78         # helper function to convert ips from a sring to a list of ints
79         def convertIP(self, ip):
80                 strIP = ip.split('.')
81                 ip = []
82                 for x in strIP:
83                         ip.append(int(x))
84                 return ip
85
86         def exit(self):
87                 self.close()
88
89         def createConfig(self):
90                 self.sharenameEntry = None
91                 self.mounttypeEntry = None
92                 self.activeEntry = None
93                 self.ipEntry = None
94                 self.sharedirEntry = None
95                 self.optionsEntry = None
96                 self.usernameEntry = None
97                 self.passwordEntry = None
98
99                 self.sharetypelist = []
100                 self.sharetypelist.append(("nfs", _("NFS share")))
101                 self.sharetypelist.append(("cifs", _("CIFS share")))
102
103                 if self.mountinfo.has_key('mounttype'):
104                         mounttype = self.mountinfo['mounttype']
105                 else:
106                         mounttype = "nfs"
107
108                 if self.mountinfo.has_key('active'):
109                         active = self.mountinfo['active']
110                         if active == 'True':
111                                 active = True
112                         if active == 'False':
113                                 active = False
114                 else:
115                         active = True
116                 if self.mountinfo.has_key('ip'):
117                         if self.mountinfo['ip'] is False:
118                                 ip = [192, 168, 0, 0]
119                         else:
120                                 ip = self.convertIP(self.mountinfo['ip'])
121                 else:
122                         ip = [192, 168, 0, 0]
123                 if self.mountinfo.has_key('sharename'):
124                         sharename = self.mountinfo['sharename']
125                 else:
126                         sharename = "Sharename"
127                 if self.mountinfo.has_key('sharedir'):
128                         sharedir = self.mountinfo['sharedir']
129                 else:
130                         sharedir = "/export/hdd"
131                 if self.mountinfo.has_key('options'):
132                         options = self.mountinfo['options']
133                 else:
134                         options = "rw,nolock"
135                 if self.mountinfo.has_key('username'):
136                         username = self.mountinfo['username']
137                 else:
138                         username = ""
139                 if self.mountinfo.has_key('password'):
140                         password = self.mountinfo['password']
141                 else:
142                         password = ""
143                 if sharename is False:
144                         sharename = "Sharename"
145                 if sharedir is False:
146                         sharedir = "/export/hdd"
147                 if options is False:
148                         if mounttype == "nfs":
149                                 options = "rw,nolock"
150                         else:
151                                 options = "rw"
152                 if username is False:
153                         username = ""
154                 if password is False:
155                         password = ""
156                 if mounttype is False:
157                         mounttype = "nfs"
158
159                 self.activeConfigEntry = NoSave(ConfigEnableDisable(default = active))
160                 self.ipConfigEntry = NoSave(ConfigIP(default = ip))
161                 self.sharenameConfigEntry = NoSave(ConfigText(default = sharename, visible_width = 50, fixed_size = False))
162                 self.sharedirConfigEntry = NoSave(ConfigText(default = sharedir, visible_width = 50, fixed_size = False))
163                 self.optionsConfigEntry = NoSave(ConfigText(default = options, visible_width = 50, fixed_size = False))
164                 self.usernameConfigEntry = NoSave(ConfigText(default = username, visible_width = 50, fixed_size = False))
165                 self.passwordConfigEntry = NoSave(ConfigPassword(default = password, visible_width = 50, fixed_size = False))
166                 self.mounttypeConfigEntry = NoSave(ConfigSelection(self.sharetypelist, default = mounttype ))
167
168         def createSetup(self):
169                 self.list = []
170                 self.activeEntry = getConfigListEntry(_("Active"), self.activeConfigEntry)
171                 self.list.append(self.activeEntry)
172                 self.sharenameEntry = getConfigListEntry(_("Local share name"), self.sharenameConfigEntry)
173                 self.list.append(self.sharenameEntry)
174                 self.mounttypeEntry = getConfigListEntry(_("Mount type"), self.mounttypeConfigEntry)
175                 self.list.append(self.mounttypeEntry)
176                 self.ipEntry = getConfigListEntry(_("Server IP"), self.ipConfigEntry)
177                 self.list.append(self.ipEntry)
178                 self.sharedirEntry = getConfigListEntry(_("Server share"), self.sharedirConfigEntry)
179                 self.list.append(self.sharedirEntry)
180                 if self.mounttypeConfigEntry.value == "cifs":
181                         self.optionsConfigEntry = NoSave(ConfigText(default = "rw", visible_width = 50, fixed_size = False))
182                 else:
183                         self.optionsConfigEntry = NoSave(ConfigText(default = "rw,nolock", visible_width = 50, fixed_size = False))
184                 self.optionsEntry = getConfigListEntry(_("Mount options"), self.optionsConfigEntry)
185                 self.list.append(self.optionsEntry)
186                 if self.mounttypeConfigEntry.value == "cifs":
187                         self.usernameEntry = getConfigListEntry(_("Username"), self.usernameConfigEntry)
188                         self.list.append(self.usernameEntry)
189                         self.passwordEntry = getConfigListEntry(_("Password"), self.passwordConfigEntry)
190                         self.list.append(self.passwordEntry)
191
192                 self["config"].list = self.list
193                 self["config"].l.setList(self.list)
194                 self["config"].onSelectionChanged.append(self.selectionChanged)
195
196         def newConfig(self):
197                 if self["config"].getCurrent() == self.mounttypeEntry:
198                         self.createSetup()
199
200         def KeyGreen(self):
201                 print "Green Pressed"
202                 if self["config"].getCurrent() == self.sharenameEntry:
203                         self.session.openWithCallback(lambda x : self.VirtualKeyBoardCallback(x, 'sharename'), VirtualKeyBoard, title = (_("Enter share name:")), text = self.sharenameConfigEntry.value)
204                 if self["config"].getCurrent() == self.sharedirEntry:
205                         self.session.openWithCallback(lambda x : self.VirtualKeyBoardCallback(x, 'sharedir'), VirtualKeyBoard, title = (_("Enter share directory:")), text = self.sharedirConfigEntry.value)
206                 if self["config"].getCurrent() == self.optionsEntry:
207                         self.session.openWithCallback(lambda x : self.VirtualKeyBoardCallback(x, 'options'), VirtualKeyBoard, title = (_("Enter options:")), text = self.optionsConfigEntry.value)
208                 if self["config"].getCurrent() == self.usernameEntry:
209                         self.session.openWithCallback(lambda x : self.VirtualKeyBoardCallback(x, 'username'), VirtualKeyBoard, title = (_("Enter username:")), text = self.usernameConfigEntry.value)
210                 if self["config"].getCurrent() == self.passwordEntry:
211                         self.session.openWithCallback(lambda x : self.VirtualKeyBoardCallback(x, 'password'), VirtualKeyBoard, title = (_("Enter password:")), text = self.passwordConfigEntry.value)
212
213         def VirtualKeyBoardCallback(self, callback = None, entry = None):
214                 if callback is not None and len(callback) and entry is not None and len(entry):
215                         if entry == 'sharename':
216                                 self.sharenameConfigEntry = NoSave(ConfigText(default = callback, visible_width = 50, fixed_size = False))
217                                 self.createSetup()
218                         if entry == 'sharedir':
219                                 self.sharedirConfigEntry = NoSave(ConfigText(default = callback, visible_width = 50, fixed_size = False))
220                                 self.createSetup()
221                         if entry == 'options':
222                                 self.optionsConfigEntry = NoSave(ConfigText(default = callback, visible_width = 50, fixed_size = False))
223                                 self.createSetup()
224                         if entry == 'username':
225                                 self.usernameConfigEntry = NoSave(ConfigText(default = callback, visible_width = 50, fixed_size = False))
226                                 self.createSetup()
227                         if entry == 'password':
228                                 self.passwordConfigEntry = NoSave(ConfigPassword(default = callback, visible_width = 50, fixed_size = False))
229                                 self.createSetup()
230
231         def keyLeft(self):
232                 ConfigListScreen.keyLeft(self)
233                 self.newConfig()
234
235         def keyRight(self):
236                 ConfigListScreen.keyRight(self)
237                 self.newConfig()
238
239         def selectionChanged(self):
240                 current = self["config"].getCurrent()
241                 if current == self.activeEntry or current == self.ipEntry or current == self.mounttypeEntry:
242                         self["ButtonGreen"].hide()
243                         self["VKeyIcon"].hide()
244                         self["VirtualKB"].setEnabled(False)
245                 else:
246                         helpwindowpos = self["HelpWindow"].getPosition()
247                         if current[1].help_window.instance is not None:
248                                 current[1].help_window.instance.move(ePoint(helpwindowpos[0],helpwindowpos[1]))
249                                 self["ButtonGreen"].show()
250                                 self["VKeyIcon"].show()
251                                 self["VirtualKB"].setEnabled(True)
252
253         def ok(self):
254                 current = self["config"].getCurrent()
255                 if current == self.sharenameEntry or current == self.sharedirEntry or current == self.sharedirEntry or current == self.optionsEntry or current == self.usernameEntry or current == self.passwordEntry:
256                         if current[1].help_window.instance is not None:
257                                 current[1].help_window.instance.hide()
258                 sharename = self.sharenameConfigEntry.value
259
260                 if self.mounts.has_key(sharename) is True:
261                         self.session.openWithCallback(self.updateConfig, MessageBox, (_("A mount entry with this name already exists!\nUpdate existing entry and continue?\n") ) )
262                 else:
263                         self.session.openWithCallback(self.applyConfig, MessageBox, (_("Are you sure you want to save this network mount?\n\n") ) )
264
265         def updateConfig(self, ret = False):
266                 if (ret == True):
267                         iAutoMount.setMountsAttribute(self.sharenameConfigEntry.value, "sharename", self.sharenameConfigEntry.value)
268                         iAutoMount.setMountsAttribute(self.sharenameConfigEntry.value, "active", self.activeConfigEntry.value)
269                         iAutoMount.setMountsAttribute(self.sharenameConfigEntry.value, "ip", self.ipConfigEntry.getText())
270                         iAutoMount.setMountsAttribute(self.sharenameConfigEntry.value, "sharedir", self.sharedirConfigEntry.value)
271                         iAutoMount.setMountsAttribute(self.sharenameConfigEntry.value, "mounttype", self.mounttypeConfigEntry.value)
272                         iAutoMount.setMountsAttribute(self.sharenameConfigEntry.value, "options", self.optionsConfigEntry.value)
273                         iAutoMount.setMountsAttribute(self.sharenameConfigEntry.value, "username", self.usernameConfigEntry.value)
274                         iAutoMount.setMountsAttribute(self.sharenameConfigEntry.value, "password", self.passwordConfigEntry.value)
275
276                         self.updateConfigRef = None
277                         self.updateConfigRef = self.session.openWithCallback(self.updateConfigfinishedCB, MessageBox, _("Please wait while updating your network mount..."), type = MessageBox.TYPE_INFO, enable_input = False)
278                         iAutoMount.writeMountsConfig()
279                         iAutoMount.getAutoMountPoints(self.updateConfigDataAvail)
280                 else:
281                         self.close()
282
283         def updateConfigDataAvail(self, data):
284                 if data is True:
285                         self.updateConfigRef.close(True)
286
287         def updateConfigfinishedCB(self,data):
288                 if data is True:
289                         self.session.openWithCallback(self.Updatefinished, MessageBox, _("Your network mount has been updated."), type = MessageBox.TYPE_INFO, timeout = 10)
290
291         def Updatefinished(self,data):
292                 if data is not None:
293                         if data is True:
294                                 self.close()
295
296         def applyConfig(self, ret = False):
297                 if (ret == True):
298                         data = { 'isMounted': False, 'active': False, 'ip': False, 'sharename': False, 'sharedir': False, 'username': False, 'password': False, 'mounttype' : False, 'options' : False }
299                         data['active'] = self.activeConfigEntry.value
300                         data['ip'] = self.ipConfigEntry.getText()
301                         data['sharename'] = self.sharenameConfigEntry.value
302                         data['sharedir'] = self.sharedirConfigEntry.value
303                         data['options'] =  self.optionsConfigEntry.value
304                         data['mounttype'] = self.mounttypeConfigEntry.value
305                         data['username'] = self.usernameConfigEntry.value
306                         data['password'] = self.passwordConfigEntry.value
307                         self.applyConfigRef = None
308                         self.applyConfigRef = self.session.openWithCallback(self.applyConfigfinishedCB, MessageBox, _("Please wait for activation of your network mount..."), type = MessageBox.TYPE_INFO, enable_input = False)
309                         iAutoMount.automounts[self.sharenameConfigEntry.value] = data
310                         iAutoMount.writeMountsConfig()
311                         iAutoMount.getAutoMountPoints(self.applyConfigDataAvail)
312                 else:
313                         self.close()
314
315         def applyConfigDataAvail(self, data):
316                 if data is True:
317                         self.applyConfigRef.close(True)
318
319         def applyConfigfinishedCB(self,data):
320                 if data is True:
321                         self.session.openWithCallback(self.applyfinished, MessageBox, _("Your network mount has been activated."), type = MessageBox.TYPE_INFO, timeout = 10)
322
323         def applyfinished(self,data):
324                 if data is not None:
325                         if data is True:
326                                 self.close()
327