add server manager based off code by AliAbdul
[enigma2-plugins.git] / ftpbrowser / src / FTPServerManager.py
1 # GUI (Screens)
2 from Components.ConfigList import ConfigListScreen
3 from Screens.MessageBox import MessageBox
4 from Screens.Screen import Screen
5 from NTIVirtualKeyBoard import NTIVirtualKeyBoard
6
7 # GUI (Components)
8 from Components.MenuList import MenuList
9 from Components.ActionMap import ActionMap
10 from Components.Label import Label
11
12 # Config
13 from Components.config import config, ConfigInteger, ConfigSubsection, \
14                 ConfigText, ConfigPassword, ConfigYesNo, getConfigListEntry
15
16 # For new and improved _parse
17 from urlparse import urlparse, urlunparse
18
19 def _parse(url, defaultPort = None):
20         url = url.strip()
21         parsed = urlparse(url)
22         scheme = parsed[0]
23         path = urlunparse(('','')+parsed[2:])
24
25         if defaultPort is None:
26                 if scheme == 'https':
27                         defaultPort = 443
28                 elif scheme == 'ftp':
29                         defaultPort = 21
30                 else:
31                         defaultPort = 80
32
33         host, port = parsed[1], defaultPort
34
35         if '@' in host:
36                 username, host = host.split('@')
37                 if ':' in username:
38                         username, password = username.split(':')
39                 else:
40                         password = ""
41         else:
42                 username = ""
43                 password = ""
44
45         if ':' in host:
46                 host, port = host.split(':')
47                 port = int(port)
48
49         if path == "":
50                 path = "/"
51
52         return scheme, host, port, path, username, password
53
54 class FTPServer:
55         def __init__(self, cfg):
56                 self.cfg = cfg
57
58         def getCfg(self):
59                 return self.cfg
60
61         def getName(self):
62                 return self.cfg.name.value
63
64         def getAddress(self):
65                 return self.cfg.address.value
66
67         def getUsername(self):
68                 return self.cfg.username.value
69
70         def getPassword(self):
71                 return self.cfg.password.value
72
73         def getPort(self):
74                 return self.cfg.port.value
75
76         def getPassive(self):
77                 return self.cfg.passive.value
78
79         def getPath(self):
80                 # TODO: implement
81                 return '/'
82
83         def getURI(self):
84                 if self.getUsername() != "":
85                         uri = "ftp://%s:%s@%s:%d%s" % (self.getUsername(), self.getPassword(), self.getAddress(), self.getPort(), self.getPath())
86                 else:
87                         uri = "ftp://%s:%d%s" % (self.getAddress(), self.getPort(), self.getPath())
88                 return uri
89
90         def save(self):
91                 self.cfg.save()
92
93         def cancel(self):
94                 self.cfg.cancel()
95
96 def ftpserverFromURI(uri, name = ""):
97         scheme, host, port, path, username, password = _parse(uri, defaultPort = 21)
98         
99         newServer = ConfigSubsection()
100         config.plugins.ftpbrowser.server.append(newServer)
101         newServer.name = ConfigText(fixed_size = False)
102         newServer.name.value = name or host
103         newServer.address = ConfigText(fixed_size = False)
104         newServer.address.value = host
105         newServer.username = ConfigText(fixed_size = False)
106         newServer.username.value = username
107         newServer.password = ConfigPassword()
108         newServer.password.value = password
109         newServer.port = ConfigInteger(0, (0, 65535))
110         newServer.port.value = port
111         newServer.passive = ConfigYesNo(False)
112
113         newServer.save()
114         config.plugins.ftpbrowser.servercount.value += 1
115         config.plugins.ftpbrowser.servercount.save()
116
117         return newServer
118
119 class FTPServerEditor(ConfigListScreen, Screen):
120         skin = """
121                 <screen position="center,center" size="560,180" title="FTP Server Editor">
122                         <ePixmap pixmap="skin_default/buttons/red.png" position="0,0" size="140,40" transparent="1" alphatest="on" />
123                         <ePixmap pixmap="skin_default/buttons/green.png" position="140,0" size="140,40" transparent="1" alphatest="on" />
124                         <ePixmap pixmap="skin_default/buttons/yellow.png" position="280,0" size="140,40" transparent="1" alphatest="on" />
125                         <ePixmap pixmap="skin_default/buttons/blue.png" position="420,0" size="140,40" transparent="1" alphatest="on" />
126                         <widget name="key_red" position="0,0" zPosition="1" size="140,40" font="Regular;20" valign="center" halign="center" backgroundColor="#1f771f" transparent="1" />
127                         <widget name="key_green" position="140,0" zPosition="1" size="140,40" font="Regular;20" valign="center" halign="center" backgroundColor="#1f771f" transparent="1" />
128                         <widget name="key_yellow" position="280,0" zPosition="1" size="140,40" font="Regular;20" valign="center" halign="center" backgroundColor="#1f771f" transparent="1" />
129                         <widget name="key_blue" position="420,0" zPosition="1" size="140,40" font="Regular;20" valign="center" halign="center" backgroundColor="#1f771f" transparent="1" />
130                         <widget name="config" position="10,50" size="550,130" scrollbarMode="showOnDemand" />
131                 </screen>"""
132
133         def __init__(self, session, server):
134                 Screen.__init__(self, session)
135
136                 self.server = server
137
138                 self["key_red"] = Label(_("Exit"))
139                 self["key_green"] = Label(_("OK"))
140                 self["key_yellow"] = Label("")
141                 self["key_blue"] = Label(_("Enter URI"))
142
143                 ConfigListScreen.__init__(self, [
144                         getConfigListEntry(_("Name:"), server.cfg.name),
145                         getConfigListEntry(_("Address:"), server.cfg.address),
146                         getConfigListEntry(_("Username:"), server.cfg.username),
147                         getConfigListEntry(_("Password:"), server.cfg.password),
148                         getConfigListEntry(_("Port:"), server.cfg.port)
149                 ])
150                 
151                 self["actions"] = ActionMap(["SetupActions", "ColorActions"],
152                         {
153                                 "save": self.keySave,
154                                 "cancel": self.keyCancel,
155                                 "blue": self.getURI,
156                         }, -2)
157
158                 self.onLayoutFinish.append(self.layoutFinished)
159
160         def layoutFinished(self):
161                 self.setTitle(_("FTP Server Editor"))
162
163         def gotURI(self, res):
164                 if res:
165                         cfg = self.server.cfg
166
167                         # _parse gets confused without a scheme
168                         if not res.startswith("ftp://"):
169                                 res = "ftp://" + res
170                         scheme, host, port, path, username, password = _parse(res, defaultPort = 21)
171
172                         cfg.address.value = host
173                         cfg.username.value = username
174                         cfg.password.value = password
175                         cfg.port.value = port
176
177         def getURI(self):
178                 self.session.openWithCallback(
179                         self.gotURI,
180                         NTIVirtualKeyBoard,
181                         title = _("Enter URI of FTP Server:"),
182                         text = self.server.getURI(),
183                 )
184
185         def keySave(self):
186                 self.saveAll()
187                 self.close(True)
188
189 class FTPServerManager(Screen):
190         skin = """
191                 <screen position="center,center" size="560,420" title="FTP Server Manager" >
192                         <ePixmap pixmap="skin_default/buttons/red.png" position="0,0" size="140,40" transparent="1" alphatest="on" />
193                         <ePixmap pixmap="skin_default/buttons/green.png" position="140,0" size="140,40" transparent="1" alphatest="on" />
194                         <ePixmap pixmap="skin_default/buttons/yellow.png" position="280,0" size="140,40" transparent="1" alphatest="on" />
195                         <ePixmap pixmap="skin_default/buttons/blue.png" position="420,0" size="140,40" transparent="1" alphatest="on" />
196                         <widget name="key_red" position="0,0" zPosition="1" size="140,40" font="Regular;20" valign="center" halign="center" backgroundColor="#1f771f" transparent="1" />
197                         <widget name="key_green" position="140,0" zPosition="1" size="140,40" font="Regular;20" valign="center" halign="center" backgroundColor="#1f771f" transparent="1" />
198                         <widget name="key_yellow" position="280,0" zPosition="1" size="140,40" font="Regular;20" valign="center" halign="center" backgroundColor="#1f771f" transparent="1" />
199                         <widget name="key_blue" position="420,0" zPosition="1" size="140,40" font="Regular;20" valign="center" halign="center" backgroundColor="#1f771f" transparent="1" />
200                         <widget name="list" position="0,50" size="560,360" scrollbarMode="showOnDemand" />
201                 </screen>"""
202
203         def __init__(self, session):
204                 Screen.__init__(self, session)
205                 self.session = session
206                 self.changed = False
207                 
208                 self["key_red"] = Label(_("Delete"))
209                 self["key_green"] = Label(_("Add"))
210                 self["key_yellow"] = Label(_("Edit"))
211                 self["key_blue"] = Label(_("Save"))
212                 self["list"] = MenuList([])
213                 
214                 self["actions"] = ActionMap(["OkCancelActions", "ColorActions"],
215                         {
216                                 "cancel": self.exit,
217                                 "ok": self.okClicked,
218                                 "red": self.delete,
219                                 "green": self.add,
220                                 "yellow": self.edit,
221                                 "blue": self.save
222                         }, -1)
223                 
224                 self.onLayoutFinish.extend((
225                         self.updateServerList,
226                         self.layoutFinished,
227                 ))
228
229         def layoutFinished(self):
230                 self.setTitle(_("FTP Server Manager"))
231
232         def updateServerList(self):
233                 list = [server.name.value for server in config.plugins.ftpbrowser.server]
234                 self["list"].setList(list)
235
236         def exit(self, server=None):
237                 if self.changed:
238                         self.save(False)
239                 self.close(server)
240
241         def okClicked(self):
242                 idx = self["list"].getSelectedIndex()
243                 ftpserverconfig = config.plugins.ftpbrowser
244                 Len = ftpserverconfig.servercount.value
245
246                 if Len and idx < Len:
247                         server = FTPServer(ftpserverconfig.server[idx])
248                         self.exit(server)
249
250         def delete(self):
251                 self.session.openWithCallback(
252                         self.deleteConfirm,
253                         MessageBox,
254                         _("Really delete this entry?\nIt cannot be recovered!")
255                 )
256
257         def deleteConfirm(self, ret):
258                 if not ret:
259                         return
260
261                 idx = self["list"].getSelectedIndex()
262                 ftpserverconfig = config.plugins.ftpbrowser
263                 Len = ftpserverconfig.servercount.value
264
265                 if Len and idx < Len:
266                         del ftpserverconfig.server[idx]
267                         ftpserverconfig.servercount.value -= 1
268                         self.updateServerList()
269                         self.changed = True
270
271         def add(self):
272                 newServer = ConfigSubsection()
273                 config.plugins.ftpbrowser.server.append(newServer)
274                 newServer.name = ConfigText("Name", fixed_size = False)
275                 newServer.address = ConfigText("192.168.2.12", fixed_size = False)
276                 newServer.username = ConfigText("root", fixed_size = False)
277                 newServer.password = ConfigPassword("dreambox")
278                 newServer.port = ConfigInteger(21, (1, 65535))
279                 newServer.passive = ConfigYesNo(False)
280                 config.plugins.ftpbrowser.servercount.value += 1
281                 config.plugins.ftpbrowser.servercount.save()
282
283                 self.updateServerList()
284                 self.changed = True
285
286         def edit(self):
287                 idx = self["list"].getSelectedIndex()
288                 ftpserverconfig = config.plugins.ftpbrowser
289                 Len = ftpserverconfig.servercount.value
290
291                 if Len and idx < Len:
292                         self.session.openWithCallback(
293                                 self.editCallback,
294                                 FTPServerEditor,
295                                 FTPServer(ftpserverconfig.server[idx])
296                         )
297
298         def editCallback(self, ret = False):
299                 if ret:
300                         self.updateServerList()
301                         self.changed = True
302
303         def save(self, showMessageBox=True):
304                 config.plugins.ftpbrowser.save()
305                 if showMessageBox:
306                         self.session.open(
307                                 MessageBox,
308                                 _("Configuration saved."),
309                                 type = MessageBox.TYPE_INFO
310                         )
311