-add Networkbrowser and Mountmanager to easy browse network for networkdevices and...
[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, ConfigYesNo, ConfigIP, NoSave, ConfigText, ConfigEnableDisable, ConfigPassword, ConfigSelection, getConfigListEntry, ConfigNothing
10 from Components.ConfigList import ConfigListScreen
11 from Components.Label import Label,MultiColorLabel
12 from Components.Pixmap import Pixmap,MultiPixmap
13 from Components.ActionMap import ActionMap, NumberActionMap
14 from enigma import eTimer, ePoint, eSize, RT_HALIGN_LEFT, eListboxPythonMultiContent, gFont
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         
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'] = self.sharenameConfigEntry.value
303                         data['sharedir'] = self.sharedirConfigEntry.value                       
304                         data['options'] =  self.optionsConfigEntry.value
305                         data['mounttype'] = self.mounttypeConfigEntry.value
306                         data['username'] = self.usernameConfigEntry.value
307                         data['password'] = self.passwordConfigEntry.value
308                         self.applyConfigRef = None
309                         self.applyConfigRef = self.session.openWithCallback(self.applyConfigfinishedCB, MessageBox, _("Please wait for activation of your network mount..."), type = MessageBox.TYPE_INFO, enable_input = False)
310                         iAutoMount.automounts[self.sharenameConfigEntry.value] = data 
311                         iAutoMount.writeMountsConfig()
312                         iAutoMount.getAutoMountPoints(self.applyConfigDataAvail)                        
313                 else:
314                         self.close()
315
316         def applyConfigDataAvail(self, data):
317                 if data is True:
318                         self.applyConfigRef.close(True)
319     
320         def applyConfigfinishedCB(self,data):
321                 if data is True:
322                         self.session.openWithCallback(self.applyfinished, MessageBox, _("Your network mount has been activated."), type = MessageBox.TYPE_INFO, timeout = 10)
323         
324         def applyfinished(self,data):
325                 if data is not None:
326                         if data is True:
327                                 self.close()
328