add server manager based off code by AliAbdul
[enigma2-plugins.git] / ftpbrowser / src / plugin.py
1 #
2 # POC FTP Browser for Enigma2
3 #
4
5 # for localized messages         
6 from . import _
7
8 # Config
9 from Components.config import config, ConfigInteger, ConfigSubList, \
10                 ConfigSubsection, ConfigText, ConfigPassword, ConfigYesNo
11
12 config.plugins.ftpbrowser = ConfigSubsection()
13 config.plugins.ftpbrowser.server = ConfigSubList()
14 config.plugins.ftpbrowser.servercount = ConfigInteger(0)
15 i = 0
16 append = config.plugins.ftpbrowser.server.append
17 while i < config.plugins.ftpbrowser.servercount.value:
18         newServer = ConfigSubsection()
19         append(newServer)
20         newServer.name = ConfigText("Name", fixed_size=False)
21         newServer.address = ConfigText("192.168.2.12", fixed_size=False)
22         newServer.username = ConfigText("root", fixed_size=False)
23         newServer.password = ConfigPassword("dreambox")
24         newServer.port = ConfigInteger(21, (1, 65535))
25         newServer.passive = ConfigYesNo(False)
26         i += 1
27         del newServer
28
29 del append, i
30
31 from FTPBrowser import FTPBrowser
32
33 ftpbrowser = None
34
35 def createSingleton(session):
36         global ftpbrowser
37         if not ftpbrowser:
38                 ftpbrowser = session.instantiateDialog(FTPBrowser)
39                 return False
40         return True
41
42 def main(session, **kwargs):
43         createSingleton(session)
44         session.execDialog(ftpbrowser)
45
46 def filescan_chosen(session, item):
47         if item:
48                 createSingleton(session)
49                 ftpbrowser.connect(item[1])
50                 session.execDialog(ftpbrowser)
51
52 def filescan_open_connected(res, items, session, **kwargs):
53         if res:
54                 ftpbrowser.disconnect()
55                 filescan_open(items, session, **kwargs)
56
57 def filescan_open(items, session, **kwargs):
58         if createSingleton(session) and ftpbrowser.ftpclient:
59                 from Screens.MessageBox import MessageBox
60                 from Tools.BoundFunction import boundFunction
61
62                 session.openWithCallback(
63                         boundFunction(filescan_open_connected, items, session, **kwargs),
64                         MessageBox,
65                         _("There already is an active connection.\nDo you want to abort it?"),
66                         type = MessageBox.TYPE_YESNO
67                 )
68                 return
69
70         Len = len(items)
71         if Len > 1:
72                 from Screens.ChoiceBox import ChoiceBox
73                 from Tools.BoundFunction import boundFunction
74
75                 session.openWithCallback(
76                         boundFunction(filescan_chosen, session),
77                         ChoiceBox,
78                         _("Which server do you want to connect to?"),
79                         [(item, item) for item in items]
80                 )
81         elif Len:
82                 filescan_chosen(items[0])
83
84 def filescan(**kwargs):
85         from Components.Scanner import Scanner, ScanPath
86
87         # Overwrite checkFile to detect remote files
88         class RemoteScanner(Scanner):
89                 def checkFile(self, file):
90                         return file.path.startswith("ftp://")
91
92         return [
93                 RemoteScanner(
94                         mimetypes = None,
95                         paths_to_scan =
96                                 (
97                                         ScanPath(path = "", with_subdirs = False),
98                                 ),
99                         name = "Connect",
100                         description = _("Connect to FTP..."),
101                         openfnc = filescan_open,
102                 ),
103         ]
104
105 def Plugins(**kwargs):
106         from Plugins.Plugin import PluginDescriptor
107
108         return [
109                 PluginDescriptor(
110                         name="FTPBrowser",
111                         description = _("A basic FTP client"),
112                         where = PluginDescriptor.WHERE_PLUGINMENU,
113                         fnc = main
114                 ),
115                 PluginDescriptor(
116                         name = "FTPBrowser",
117                         where = PluginDescriptor.WHERE_FILESCAN,
118                         fnc = filescan
119                 ),
120         ]