- add meta informations for upcoming extension manager (not yet activated)9
[enigma2-plugins.git] / filebrowser / src / plugin.py
1 from Plugins.Plugin import PluginDescriptor
2 from Components.config import config, ConfigSubList, ConfigSubsection, ConfigInteger, ConfigYesNo, ConfigText, getConfigListEntry
3 from Components.FileList import FileList
4 from Components.ConfigList import ConfigListScreen
5 from Screens.Console import Console
6 from Screens.ChoiceBox import ChoiceBox
7 from Screens.InputBox import InputBox
8 from Screens.MessageBox import MessageBox
9 from Components.Label import Label
10 from Screens.Screen import Screen
11 from Components.ActionMap import ActionMap
12 from Components.Scanner import openFile
13 from os.path import isdir as os_path_isdir
14 from mimetypes import guess_type
15
16 ##################################
17 pname = _("Filebrowser")
18 pdesc = _("manage local Files")
19
20 config.plugins.filebrowser = ConfigSubsection()
21 config.plugins.filebrowser.savedirs = ConfigYesNo(default = True)
22 config.plugins.filebrowser.add_mainmenu_entry = ConfigYesNo(default = True)
23 config.plugins.filebrowser.add_extensionmenu_entry = ConfigYesNo(default = True)
24 config.plugins.filebrowser.path_left = ConfigText(default = "/")
25 config.plugins.filebrowser.path_right = ConfigText(default = "/")
26
27
28 ##################################
29 class FilebrowserConfigScreen(ConfigListScreen,Screen):
30     skin = """
31         <screen position="100,100" size="550,400" title="" >
32             <widget name="config" position="0,0" size="550,360" scrollbarMode="showOnDemand" />
33             <widget name="buttonred" position="10,360" size="100,40" valign="center" halign="center" zPosition="1"  transparent="1" foregroundColor="white" font="Regular;18"/>
34             <widget name="buttongreen" position="120,360" size="100,40" valign="center" halign="center" zPosition="1"  transparent="1" foregroundColor="white" font="Regular;18"/>
35             <ePixmap name="pred" position="10,360" size="100,40" zPosition="0" pixmap="skin_default/buttons/red.png" transparent="1" alphatest="on"/>
36             <ePixmap name="pgreen" position="120,360" size="100,40" zPosition="0" pixmap="skin_default/buttons/green.png" transparent="1" alphatest="on"/>
37         </screen>"""
38     def __init__(self, session):
39         self.session = session
40         Screen.__init__(self, session)
41         self.list = []
42         self.list.append(getConfigListEntry(_("add Plugin to Mainmenu"), config.plugins.filebrowser.add_mainmenu_entry))
43         self.list.append(getConfigListEntry(_("add Plugin to Extensionmenu"), config.plugins.filebrowser.add_extensionmenu_entry))
44         self.list.append(getConfigListEntry(_("save Filesystemposition on exit"), config.plugins.filebrowser.savedirs))
45         self.list.append(getConfigListEntry(_("Filesystemposition list left"), config.plugins.filebrowser.path_left))
46         self.list.append(getConfigListEntry(_("Filesystemposition list right"), config.plugins.filebrowser.path_right))
47
48         ConfigListScreen.__init__(self, self.list)
49         self["buttonred"] = Label(_("cancel"))
50         self["buttongreen"] = Label(_("ok"))
51         self["setupActions"] = ActionMap(["SetupActions"],
52         {
53             "green": self.save,
54             "red": self.cancel,
55             "save": self.save,
56             "cancel": self.cancel,
57             "ok": self.save,
58         }, -2)
59         self.onLayoutFinish.append(self.onLayout)
60
61     def onLayout(self):
62         self.setTitle(pname+" "+_("Settings"))
63
64     def save(self):
65         print "saving"
66         for x in self["config"].list:
67             x[1].save()
68         self.close(True)
69
70     def cancel(self):
71         print "cancel"
72         for x in self["config"].list:
73             x[1].cancel()
74         self.close(False)
75
76
77 ##################################
78 class FilebrowserScreen(Screen):
79     skin = """
80         <screen position="110,83" size="530,430" title="">
81             <widget name="list_left" position="0,0" size="265,380" scrollbarMode="showOnDemand" />
82             <widget name="list_right" position="265,0" size="265,380" scrollbarMode="showOnDemand" />
83
84             <widget name="red" position="10,390" size="120,30" valign="center" halign="center" zPosition="1" transparent="1" foregroundColor="white" font="Regular;18"/>
85             <widget name="green" position="140,390" size="120,30" valign="center" halign="center" zPosition="1" transparent="1" foregroundColor="white" font="Regular;18"/>
86             <widget name="yellow" position="270,390" size="120,30" valign="center" halign="center" zPosition="1" transparent="1" foregroundColor="white" font="Regular;18"/>
87             <widget name="blue" position="400,390" size="120,30" valign="center" halign="center" zPosition="1" transparent="1" foregroundColor="white" font="Regular;18"/>
88
89             <ePixmap name="pred" position="10,390" size="120,30" zPosition="0" pixmap="skin_default/buttons/red.png" transparent="1" alphatest="on"/>
90             <ePixmap name="pgreen" position="140,390" size="120,30" zPosition="0" pixmap="skin_default/buttons/green.png" transparent="1" alphatest="on"/>
91             <ePixmap name="pyellow" position="270,390" size="120,30" zPosition="0" pixmap="skin_default/buttons/yellow.png" transparent="1" alphatest="on"/>
92             <ePixmap name="pblue" position="400,390" size="120,30" zPosition="0" pixmap="skin_default/buttons/blue.png" transparent="1" alphatest="on"/>
93         </screen>
94         """
95     def __init__(self, session,path_left=None):
96         if path_left is None:
97             if os_path_isdir(config.plugins.filebrowser.path_left.value) and config.plugins.filebrowser.savedirs.value:
98                 path_left = config.plugins.filebrowser.path_left.value
99             else:
100                 path_left = "/"
101
102         if os_path_isdir(config.plugins.filebrowser.path_right.value) and config.plugins.filebrowser.savedirs.value:
103             path_right = config.plugins.filebrowser.path_right.value
104         else:
105             path_right = "/"
106
107         self.session = session
108         Screen.__init__(self, session)
109
110         self["list_left"] = FileList(path_left, matchingPattern = "^.*")
111         self["list_right"] = FileList(path_right, matchingPattern = "^.*")
112         self["red"] = Label(_("delete"))
113         self["green"] = Label(_("move"))
114         self["yellow"] = Label(_("copy"))
115         self["blue"] = Label(_("rename"))
116
117
118         self["actions"] = ActionMap(["ChannelSelectBaseActions","WizardActions", "DirectionActions","MenuActions","NumberActions","ColorActions"],
119             {
120              "ok":      self.ok,
121              "back":    self.exit,
122              "menu":    self.goMenu,
123              "nextMarker": self.listRight,
124              "prevMarker": self.listLeft,
125              "up": self.goUp,
126              "down": self.goDown,
127              "left": self.goLeft,
128              "right": self.goRight,
129              "red": self.goRed,
130              "green": self.goGreen,
131              "yellow": self.goYellow,
132              "blue": self.goBlue,
133              "0": self.doRefresh,
134              }, -1)
135         self.onLayoutFinish.append(self.listLeft)
136
137     def exit(self):
138         if self["list_left"].getCurrentDirectory() and config.plugins.filebrowser.savedirs.value:
139             config.plugins.filebrowser.path_left.value = self["list_left"].getCurrentDirectory()
140             config.plugins.filebrowser.path_left.save()
141
142         if self["list_right"].getCurrentDirectory() and config.plugins.filebrowser.savedirs.value:
143             config.plugins.filebrowser.path_right.value = self["list_right"].getCurrentDirectory()
144             config.plugins.filebrowser.path_right.save()
145
146         self.close()
147
148     def ok(self):
149         if self.SOURCELIST.canDescent(): # isDir
150             self.SOURCELIST.descent()
151             if self.SOURCELIST.getCurrentDirectory(): #??? when is it none
152                 self.setTitle(self.SOURCELIST.getCurrentDirectory())
153         else:
154             self.onFileAction()
155
156     def goMenu(self):
157         self.session.open(FilebrowserConfigScreen)
158
159     def goLeft(self):
160         self.SOURCELIST.pageUp()
161
162     def goRight(self):
163         self.SOURCELIST.pageDown()
164
165     def goUp(self):
166         self.SOURCELIST.up()
167
168     def goDown(self):
169         self.SOURCELIST.down()
170
171     # copy ###################
172     def goYellow(self):
173         filename = self.SOURCELIST.getFilename()
174         sourceDir = self.SOURCELIST.getCurrentDirectory()
175         targetDir = self.TARGETLIST.getCurrentDirectory()
176         self.session.openWithCallback(self.doCopy,ChoiceBox, title = _("copy file")+"?\n%s\nfrom\n%s\n%s"%(filename,sourceDir,targetDir),list=[(_("yes"), True ),(_("no"), False )])
177
178     def doCopy(self,result):
179         if result is not None:
180             if result[1]:
181                 filename = self.SOURCELIST.getFilename()
182                 sourceDir = self.SOURCELIST.getCurrentDirectory()
183                 targetDir = self.TARGETLIST.getCurrentDirectory()
184                 self.session.openWithCallback(self.doCopyCB,Console, title = _("copying file ..."), cmdlist = ["cp \""+sourceDir+filename+"\" \""+targetDir+"\""])
185
186     def doCopyCB(self):
187         self.doRefresh()
188
189     # delete ###################
190     def goRed(self):
191         filename = self.SOURCELIST.getFilename()
192         sourceDir = self.SOURCELIST.getCurrentDirectory()
193         self.session.openWithCallback(self.doDelete,ChoiceBox, title = _("delete file")+"?\n%s\nfrom dir\n%s"%(filename,sourceDir),list=[(_("yes"), True ),(_("no"), False )])
194
195     def doDelete(self,result):
196         if result is not None:
197             if result[1]:
198                 filename = self.SOURCELIST.getFilename()
199                 sourceDir = self.SOURCELIST.getCurrentDirectory()
200                 self.session.openWithCallback(self.doDeleteCB,Console, title = _("deleting file ..."), cmdlist = ["rm \""+sourceDir+filename+"\""])
201
202     def doDeleteCB(self):
203         self.doRefresh()
204
205     # move ###################
206     def goGreen(self):
207         filename = self.SOURCELIST.getFilename()
208         sourceDir = self.SOURCELIST.getCurrentDirectory()
209         targetDir = self.TARGETLIST.getCurrentDirectory()
210         self.session.openWithCallback(self.doMove,ChoiceBox, title = _("move file")+"?\n%s\nfrom dir\n%s\nto dir\n%s"%(filename,sourceDir,targetDir),list=[(_("yes"), True ),(_("no"), False )])
211
212     def doMove(self,result):
213         if result is not None:
214             if result[1]:
215                 filename = self.SOURCELIST.getFilename()
216                 sourceDir = self.SOURCELIST.getCurrentDirectory()
217                 targetDir = self.TARGETLIST.getCurrentDirectory()
218                 self.session.openWithCallback(self.doMoveCB,Console, title = _("moving file ..."), cmdlist = ["mv \""+sourceDir+filename+"\" \""+targetDir+"\""])
219
220     def doMoveCB(self):
221         self.doRefresh()
222
223     # move ###################
224     def goBlue(self):
225         filename = self.SOURCELIST.getFilename()
226         sourceDir = self.SOURCELIST.getCurrentDirectory()
227         self.session.openWithCallback(self.doRename,InputBox,text=filename, title = filename, windowTitle=_("rename file"))
228
229     def doRename(self,newname):
230         if newname:
231             filename = self.SOURCELIST.getFilename()
232             sourceDir = self.SOURCELIST.getCurrentDirectory()
233             self.session.openWithCallback(self.doRenameCB,Console, title = _("renaming file ..."), cmdlist = ["mv \""+sourceDir+filename+"\" \""+sourceDir+newname+"\""])
234
235     def doRenameCB(self):
236         self.doRefresh()
237
238     #############
239     def doRefresh(self):
240         self.SOURCELIST.refresh()
241         self.TARGETLIST.refresh()
242
243     def listRight(self):
244         self["list_left"].selectionEnabled(0)
245         self["list_right"].selectionEnabled(1)
246         self.SOURCELIST = self["list_right"]
247         self.TARGETLIST = self["list_left"]
248         self.setTitle(self.SOURCELIST.getCurrentDirectory())
249
250     def listLeft(self):
251         self["list_left"].selectionEnabled(1)
252         self["list_right"].selectionEnabled(0)
253         self.SOURCELIST = self["list_left"]
254         self.TARGETLIST = self["list_right"]
255         self.setTitle(self.SOURCELIST.getCurrentDirectory())
256
257     def onFileAction(self):
258         try:
259             x = openFile(self.session,guess_type(self.SOURCELIST.getFilename())[0],self.SOURCELIST.getCurrentDirectory()+self.SOURCELIST.getFilename())
260             print "RESULT OPEN FILE",x
261         except TypeError,e:
262             # catching error
263             #  File "/home/tmbinc/opendreambox/1.5/dm8000/experimental/build/tmp/work/enigma2-2.6git20090627-r1/image/usr/lib/enigma2/python/Components/Scanner.py", line 43, in handleFile
264             #  TypeError: 'in <string>' requires string as left operand
265             self.session.open(MessageBox,_("no Viewer installed for this mimetype!"), type = MessageBox.TYPE_ERROR, timeout = 5, close_on_any_key = True)
266
267
268
269 ##################################
270
271 def filescan_open(list, session, **kwargs):
272     path = "/".join(list[0].path.split("/")[:-1])+"/"
273     session.open(FilebrowserScreen,path_left=path)
274
275 def start_from_filescan(**kwargs):
276     from Components.Scanner import Scanner, ScanPath
277     return \
278         Scanner(mimetypes=None,
279             paths_to_scan =
280                 [
281                     ScanPath(path = "", with_subdirs = False),
282                 ],
283             name = pname,
284             description = pdesc,
285             openfnc = filescan_open,
286         )
287
288 def start_from_mainmenu(menuid, **kwargs):
289     #starting from main menu
290     if menuid == "mainmenu":
291         return [(pname, start_from_pluginmenu, "filecommand", 46)]
292     return []
293
294 def start_from_pluginmenu(session,**kwargs):
295     session.open(FilebrowserScreen)
296
297 def Plugins(path,**kwargs):
298     desc_mainmenu  = PluginDescriptor(name=pname, description=pdesc,  where = PluginDescriptor.WHERE_MENU, fnc = start_from_mainmenu)
299     desc_pluginmenu = PluginDescriptor(name=pname, description=pdesc,  where = PluginDescriptor.WHERE_PLUGINMENU, fnc = start_from_pluginmenu)
300     desc_extensionmenu = PluginDescriptor(name=pname, description=pdesc, where = PluginDescriptor.WHERE_EXTENSIONSMENU, fnc = start_from_pluginmenu)
301     desc_filescan = PluginDescriptor(name=pname, where = PluginDescriptor.WHERE_FILESCAN, fnc = start_from_filescan)
302     list = []
303     list.append(desc_pluginmenu)
304     #buggie list.append(desc_filescan)
305     if config.plugins.filebrowser.add_extensionmenu_entry.value:
306         list.append(desc_extensionmenu)
307     if config.plugins.filebrowser.add_mainmenu_entry.value:
308         list.append(desc_mainmenu)
309     return list
310
311