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