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