- add meta informations for upcoming extension manager (not yet activated)9
[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                 else:
103                         mounttype = "nfs"
104
105                 if self.mountinfo.has_key('active'):
106                         active = self.mountinfo['active']
107                         if active == 'True':
108                                 active = True
109                         if active == 'False':
110                                 active = False
111                 else:
112                         active = True
113                 if self.mountinfo.has_key('ip'):
114                         if self.mountinfo['ip'] is False:
115                                 ip = [192, 168, 0, 0]
116                         else:
117                                 ip = self.convertIP(self.mountinfo['ip'])
118                 else:
119                         ip = [192, 168, 0, 0]
120                 if self.mountinfo.has_key('sharename'):
121                         sharename = self.mountinfo['sharename']
122                 else:
123                         sharename = "Sharename"
124                 if self.mountinfo.has_key('sharedir'):
125                         sharedir = self.mountinfo['sharedir']
126                 else:
127                         sharedir = "/export/hdd"
128                 if self.mountinfo.has_key('options'):
129                         options = self.mountinfo['options']
130                 else:
131                         options = "rw,nolock"
132                 if self.mountinfo.has_key('username'):
133                         username = self.mountinfo['username']
134                 else:
135                         username = ""
136                 if self.mountinfo.has_key('password'):
137                         password = self.mountinfo['password']
138                 else:
139                         password = ""
140                 if self.mountinfo.has_key('hdd_replacement'):
141                         hdd_replacement = self.mountinfo['hdd_replacement']
142                         if hdd_replacement == 'True':
143                                 hdd_replacement = True
144                         if hdd_replacement == 'False':
145                                 hdd_replacement = False
146                 else:
147                         hdd_replacement = False
148                 if sharename is False:
149                         sharename = "Sharename"
150                 if sharedir is False:
151                         sharedir = "/export/hdd"
152                 if options is False:
153                         if mounttype == "nfs":
154                                 options = "rw,nolock"
155                         else:
156                                 options = "rw"
157                 if username is False:
158                         username = ""
159                 if password is False:
160                         password = ""
161                 if mounttype is False:
162                         mounttype = "nfs"
163
164                 self.activeConfigEntry = NoSave(ConfigEnableDisable(default = active))
165                 self.ipConfigEntry = NoSave(ConfigIP(default = ip))
166                 self.sharenameConfigEntry = NoSave(ConfigText(default = sharename, visible_width = 50, fixed_size = False))
167                 self.sharedirConfigEntry = NoSave(ConfigText(default = sharedir, visible_width = 50, fixed_size = False))
168                 self.optionsConfigEntry = NoSave(ConfigText(default = options, visible_width = 50, fixed_size = False))
169                 self.usernameConfigEntry = NoSave(ConfigText(default = username, visible_width = 50, fixed_size = False))
170                 self.passwordConfigEntry = NoSave(ConfigPassword(default = password, visible_width = 50, fixed_size = False))
171                 self.mounttypeConfigEntry = NoSave(ConfigSelection(self.sharetypelist, default = mounttype ))
172                 self.hdd_replacementConfigEntry = NoSave(ConfigYesNo(default = hdd_replacement))
173
174         def createSetup(self):
175                 self.list = []
176                 self.activeEntry = getConfigListEntry(_("Active"), self.activeConfigEntry)
177                 self.list.append(self.activeEntry)
178                 self.sharenameEntry = getConfigListEntry(_("Local share name"), self.sharenameConfigEntry)
179                 self.list.append(self.sharenameEntry)
180                 self.mounttypeEntry = getConfigListEntry(_("Mount type"), self.mounttypeConfigEntry)
181                 self.list.append(self.mounttypeEntry)
182                 self.ipEntry = getConfigListEntry(_("Server IP"), self.ipConfigEntry)
183                 self.list.append(self.ipEntry)
184                 self.sharedirEntry = getConfigListEntry(_("Server share"), self.sharedirConfigEntry)
185                 self.list.append(self.sharedirEntry)
186                 self.hdd_replacementEntry = getConfigListEntry(_("use as HDD replacement"), self.hdd_replacementConfigEntry)
187                 self.list.append(self.hdd_replacementEntry)
188                 if self.mounttypeConfigEntry.value == "cifs":
189                         self.optionsConfigEntry = NoSave(ConfigText(default = "rw", visible_width = 50, fixed_size = False))
190                 else:
191                         self.optionsConfigEntry = NoSave(ConfigText(default = "rw,nolock", visible_width = 50, fixed_size = False))
192                 self.optionsEntry = getConfigListEntry(_("Mount options"), self.optionsConfigEntry)
193                 self.list.append(self.optionsEntry)
194                 if self.mounttypeConfigEntry.value == "cifs":
195                         self.usernameEntry = getConfigListEntry(_("Username"), self.usernameConfigEntry)
196                         self.list.append(self.usernameEntry)
197                         self.passwordEntry = getConfigListEntry(_("Password"), self.passwordConfigEntry)
198                         self.list.append(self.passwordEntry)
199
200                 self["config"].list = self.list
201                 self["config"].l.setList(self.list)
202                 self["config"].onSelectionChanged.append(self.selectionChanged)
203
204         def newConfig(self):
205                 if self["config"].getCurrent() == self.mounttypeEntry:
206                         self.createSetup()
207
208         def KeyText(self):
209                 print "Green Pressed"
210                 if self["config"].getCurrent() == self.sharenameEntry:
211                         self.session.openWithCallback(lambda x : self.VirtualKeyBoardCallback(x, 'sharename'), VirtualKeyBoard, title = (_("Enter share name:")), text = self.sharenameConfigEntry.value)
212                 if self["config"].getCurrent() == self.sharedirEntry:
213                         self.session.openWithCallback(lambda x : self.VirtualKeyBoardCallback(x, 'sharedir'), VirtualKeyBoard, title = (_("Enter share directory:")), text = self.sharedirConfigEntry.value)
214                 if self["config"].getCurrent() == self.optionsEntry:
215                         self.session.openWithCallback(lambda x : self.VirtualKeyBoardCallback(x, 'options'), VirtualKeyBoard, title = (_("Enter options:")), text = self.optionsConfigEntry.value)
216                 if self["config"].getCurrent() == self.usernameEntry:
217                         self.session.openWithCallback(lambda x : self.VirtualKeyBoardCallback(x, 'username'), VirtualKeyBoard, title = (_("Enter username:")), text = self.usernameConfigEntry.value)
218                 if self["config"].getCurrent() == self.passwordEntry:
219                         self.session.openWithCallback(lambda x : self.VirtualKeyBoardCallback(x, 'password'), VirtualKeyBoard, title = (_("Enter password:")), text = self.passwordConfigEntry.value)
220
221         def VirtualKeyBoardCallback(self, callback = None, entry = None):
222                 if callback is not None and len(callback) and entry is not None and len(entry):
223                         if entry == 'sharename':
224                                 self.sharenameConfigEntry.setValue(callback)
225                                 self["config"].invalidate(self.sharenameConfigEntry)
226                         if entry == 'sharedir':
227                                 self.sharedirConfigEntry.setValue(callback)
228                                 self["config"].invalidate(self.sharedirConfigEntry)
229                         if entry == 'options':
230                                 self.optionsConfigEntry.setValue(callback)
231                                 self["config"].invalidate(self.optionsConfigEntry)                                
232                         if entry == 'username':
233                                 self.usernameConfigEntry.setValue(callback)
234                                 self["config"].invalidate(self.usernameConfigEntry)
235                         if entry == 'password':
236                                 self.passwordConfigEntry.setValue(callback)
237                                 self["config"].invalidate(self.passwordConfigEntry)
238
239         def keyLeft(self):
240                 ConfigListScreen.keyLeft(self)
241                 self.newConfig()
242
243         def keyRight(self):
244                 ConfigListScreen.keyRight(self)
245                 self.newConfig()
246
247         def selectionChanged(self):
248                 current = self["config"].getCurrent()
249                 if current == self.activeEntry or current == self.ipEntry or current == self.mounttypeEntry or current == self.hdd_replacementEntry:
250                         self["VKeyIcon"].hide()
251                         self["VirtualKB"].setEnabled(False)
252                 else:
253                         helpwindowpos = self["HelpWindow"].getPosition()
254                         if current[1].help_window.instance is not None:
255                                 current[1].help_window.instance.move(ePoint(helpwindowpos[0],helpwindowpos[1]))
256                                 self["VKeyIcon"].show()
257                                 self["VirtualKB"].setEnabled(True)
258
259         def ok(self):
260                 current = self["config"].getCurrent()
261                 if current == self.sharenameEntry or current == self.sharedirEntry or current == self.sharedirEntry or current == self.optionsEntry or current == self.usernameEntry or current == self.passwordEntry:
262                         if current[1].help_window.instance is not None:
263                                 current[1].help_window.instance.hide()
264                 sharename = self.sharenameConfigEntry.value
265
266                 if self.mounts.has_key(sharename) is True:
267                         self.session.openWithCallback(self.updateConfig, MessageBox, (_("A mount entry with this name already exists!\nUpdate existing entry and continue?\n") ) )
268                 else:
269                         self.session.openWithCallback(self.applyConfig, MessageBox, (_("Are you sure you want to save this network mount?\n\n") ) )
270
271         def updateConfig(self, ret = False):
272                 if (ret == True):
273                         sharedir = None
274                         if self.sharedirConfigEntry.value.startswith("/"):
275                                 sharedir = self.sharedirConfigEntry.value[1:]
276                         else:
277                                 sharedir = self.sharedirConfigEntry.value
278                         iAutoMount.setMountsAttribute(self.sharenameConfigEntry.value, "sharename", self.sharenameConfigEntry.value)
279                         iAutoMount.setMountsAttribute(self.sharenameConfigEntry.value, "active", self.activeConfigEntry.value)
280                         iAutoMount.setMountsAttribute(self.sharenameConfigEntry.value, "ip", self.ipConfigEntry.getText())
281                         iAutoMount.setMountsAttribute(self.sharenameConfigEntry.value, "sharedir", sharedir)
282                         iAutoMount.setMountsAttribute(self.sharenameConfigEntry.value, "mounttype", self.mounttypeConfigEntry.value)
283                         iAutoMount.setMountsAttribute(self.sharenameConfigEntry.value, "options", self.optionsConfigEntry.value)
284                         iAutoMount.setMountsAttribute(self.sharenameConfigEntry.value, "username", self.usernameConfigEntry.value)
285                         iAutoMount.setMountsAttribute(self.sharenameConfigEntry.value, "password", self.passwordConfigEntry.value)
286                         iAutoMount.setMountsAttribute(self.sharenameConfigEntry.value, "hdd_replacement", self.hdd_replacementConfigEntry.value)
287
288                         self.updateConfigRef = None
289                         self.updateConfigRef = self.session.openWithCallback(self.updateConfigfinishedCB, MessageBox, _("Please wait while updating your network mount..."), type = MessageBox.TYPE_INFO, enable_input = False)
290                         iAutoMount.writeMountsConfig()
291                         iAutoMount.getAutoMountPoints(self.updateConfigDataAvail)
292                 else:
293                         self.close()
294
295         def updateConfigDataAvail(self, data):
296                 if data is True:
297                         self.updateConfigRef.close(True)
298
299         def updateConfigfinishedCB(self,data):
300                 if data is True:
301                         self.session.openWithCallback(self.Updatefinished, MessageBox, _("Your network mount has been updated."), type = MessageBox.TYPE_INFO, timeout = 10)
302
303         def Updatefinished(self,data):
304                 if data is not None:
305                         if data is True:
306                                 self.close()
307
308         def applyConfig(self, ret = False):
309                 if (ret == True):
310                         data = { 'isMounted': False, 'active': False, 'ip': False, 'sharename': False, 'sharedir': False, \
311                                         'username': False, 'password': False, 'mounttype' : False, 'options' : False, 'hdd_replacement' : False }
312                         data['active'] = self.activeConfigEntry.value
313                         data['ip'] = self.ipConfigEntry.getText()
314                         data['sharename'] = re_sub("\W", "", self.sharenameConfigEntry.value)
315                         # "\W" matches everything that is "not numbers, letters, or underscores",where the alphabet defaults to ASCII.
316                         if self.sharedirConfigEntry.value.startswith("/"):
317                                 data['sharedir'] = self.sharedirConfigEntry.value[1:]
318                         else:
319                                 data['sharedir'] = self.sharedirConfigEntry.value
320                         data['options'] =  self.optionsConfigEntry.value
321                         data['mounttype'] = self.mounttypeConfigEntry.value
322                         data['username'] = self.usernameConfigEntry.value
323                         data['password'] = self.passwordConfigEntry.value
324                         data['hdd_replacement'] = self.hdd_replacementConfigEntry.value
325                         self.applyConfigRef = None
326                         self.applyConfigRef = self.session.openWithCallback(self.applyConfigfinishedCB, MessageBox, _("Please wait for activation of your network mount..."), type = MessageBox.TYPE_INFO, enable_input = False)
327                         iAutoMount.automounts[self.sharenameConfigEntry.value] = data
328                         iAutoMount.writeMountsConfig()
329                         iAutoMount.getAutoMountPoints(self.applyConfigDataAvail)
330                 else:
331                         self.close()
332
333         def applyConfigDataAvail(self, data):
334                 if data is True:
335                         self.applyConfigRef.close(True)
336
337         def applyConfigfinishedCB(self,data):
338                 if data is True:
339                         self.session.openWithCallback(self.applyfinished, MessageBox, _("Your network mount has been activated."), type = MessageBox.TYPE_INFO, timeout = 10)
340
341         def applyfinished(self,data):
342                 if data is not None:
343                         if data is True:
344                                 self.close()