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