add plugin icon (thanks jackdaniel)
[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 from FTPServerManager import ftpserverFromURI
33
34 ftpbrowser = None
35
36 def createSingleton(session):
37         global ftpbrowser
38         if not ftpbrowser:
39                 ftpbrowser = session.instantiateDialog(FTPBrowser)
40                 return False
41         return True
42
43 def main(session, **kwargs):
44         createSingleton(session)
45         session.execDialog(ftpbrowser)
46
47 def filescan_chosen(session, item):
48         if item:
49                 createSingleton(session)
50                 ftpbrowser.connect(ftpserverFromURI(item[1], save = False))
51                 session.execDialog(ftpbrowser)
52
53 def filescan_open_connected(res, items, session, **kwargs):
54         if res:
55                 ftpbrowser.disconnect()
56                 filescan_open(items, session, **kwargs)
57
58 def filescan_open(items, session, **kwargs):
59         if createSingleton(session) and ftpbrowser.ftpclient:
60                 from Screens.MessageBox import MessageBox
61                 from Tools.BoundFunction import boundFunction
62
63                 session.openWithCallback(
64                         boundFunction(filescan_open_connected, items, session, **kwargs),
65                         MessageBox,
66                         _("There already is an active connection.\nDo you want to abort it?"),
67                         type = MessageBox.TYPE_YESNO
68                 )
69                 return
70
71         Len = len(items)
72         if Len > 1:
73                 from Screens.ChoiceBox import ChoiceBox
74                 from Tools.BoundFunction import boundFunction
75
76                 session.openWithCallback(
77                         boundFunction(filescan_chosen, session),
78                         ChoiceBox,
79                         _("Which server do you want to connect to?"),
80                         [(item, item) for item in items]
81                 )
82         elif Len:
83                 filescan_chosen(items[0])
84
85 def filescan(**kwargs):
86         from Components.Scanner import Scanner, ScanPath
87
88         # Overwrite checkFile to detect remote files
89         class RemoteScanner(Scanner):
90                 def checkFile(self, file):
91                         return file.path.startswith("ftp://")
92
93         return [
94                 RemoteScanner(
95                         mimetypes = None,
96                         paths_to_scan =
97                                 (
98                                         ScanPath(path = "", with_subdirs = False),
99                                 ),
100                         name = "Connect",
101                         description = _("Connect to FTP..."),
102                         openfnc = filescan_open,
103                 ),
104         ]
105
106 def Plugins(**kwargs):
107         from Plugins.Plugin import PluginDescriptor
108
109         return [
110                 PluginDescriptor(
111                         name="FTPBrowser",
112                         description = _("A basic FTP client"),
113                         where = PluginDescriptor.WHERE_PLUGINMENU,
114                         icon = "plugin.png",
115                         fnc = main
116                 ),
117                 PluginDescriptor(
118                         name = "FTPBrowser",
119                         where = PluginDescriptor.WHERE_FILESCAN,
120                         fnc = filescan
121                 ),
122         ]