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