Merge branch 'master' into dvd_scale_test
[enigma2.git] / lib / python / Components / FileList.py
index 7621ccb0dd9352469ff8dd28cc5923d352d003c6..a93d444141847d27e3fbc016b6c14664a7215018 100644 (file)
@@ -1,47 +1,44 @@
-from HTMLComponent import *
-from GUIComponent import *
-import re
-
+from re import compile as re_compile
+from os import path as os_path, listdir
 from MenuList import MenuList
 from Components.Harddisk import harddiskmanager
 
-from Tools.Directories import *
-
-from enigma import *
-
-RT_HALIGN_LEFT = 0
-RT_HALIGN_RIGHT = 1
-RT_HALIGN_CENTER = 2
-RT_HALIGN_BLOCK = 4
+from Tools.Directories import SCOPE_SKIN_IMAGE, resolveFilename
 
-RT_VALIGN_TOP = 0
-RT_VALIGN_CENTER = 8
-RT_VALIGN_BOTTOM = 16
+from enigma import RT_HALIGN_LEFT, eListboxPythonMultiContent, \
+       eServiceReference, eServiceCenter, gFont
+from Tools.LoadPixmap import LoadPixmap
 
 EXTENSIONS = {
+               "m4a": "music",
+               "mp2": "music",
                "mp3": "music",
                "wav": "music",
                "ogg": "music",
+               "flac": "music",
                "jpg": "picture",
                "jpeg": "picture",
                "png": "picture",
                "bmp": "picture",
                "ts": "movie",
                "avi": "movie",
+               "divx": "movie",
                "mpg": "movie",
                "mpeg": "movie",
+               "mkv": "movie",
+               "mp4": "movie"
        }
 
 def FileEntryComponent(name, absolute = None, isDir = False):
        res = [ (absolute, isDir) ]
        res.append((eListboxPythonMultiContent.TYPE_TEXT, 35, 1, 470, 20, 0, RT_HALIGN_LEFT, name))
        if isDir:
-               png = loadPNG(resolveFilename(SCOPE_SKIN_IMAGE, "extensions/directory.png"))
+               png = LoadPixmap(resolveFilename(SCOPE_SKIN_IMAGE, "extensions/directory.png"))
        else:
                extension = name.split('.')
                extension = extension[-1].lower()
                if EXTENSIONS.has_key(extension):
-                       png = loadPNG(resolveFilename(SCOPE_SKIN_IMAGE, "extensions/" + EXTENSIONS[extension] + ".png"))
+                       png = LoadPixmap(resolveFilename(SCOPE_SKIN_IMAGE, "extensions/" + EXTENSIONS[extension] + ".png"))
                else:
                        png = None
        if png is not None:
@@ -49,55 +46,105 @@ def FileEntryComponent(name, absolute = None, isDir = False):
        
        return res
 
-class FileList(MenuList, HTMLComponent, GUIComponent):
-       def __init__(self, directory, showDirectories = True, showFiles = True, matchingPattern = None, useServiceRef = False, isTop = False):
-               GUIComponent.__init__(self)
-               self.l = eListboxPythonMultiContent()
-               
-               self.mount_point = None
+class FileList(MenuList):
+       def __init__(self, directory, showDirectories = True, showFiles = True, showMountpoints = True, matchingPattern = None, useServiceRef = False, inhibitDirs = False, inhibitMounts = False, isTop = False, enableWrapAround = False, additionalExtensions = None):
+               MenuList.__init__(self, list, enableWrapAround, eListboxPythonMultiContent)
+               self.additional_extensions = additionalExtensions
+               self.mountpoints = []
                self.current_directory = None
+               self.current_mountpoint = None
                self.useServiceRef = useServiceRef
                self.showDirectories = showDirectories
+               self.showMountpoints = showMountpoints
                self.showFiles = showFiles
                self.isTop = isTop
                # example: matching .nfi and .ts files: "^.*\.(nfi|ts)"
                self.matchingPattern = matchingPattern
-               self.changeDir(directory)
+               self.inhibitDirs = inhibitDirs or []
+               self.inhibitMounts = inhibitMounts or []
 
+               self.refreshMountpoints()
+               self.changeDir(directory)
                self.l.setFont(0, gFont("Regular", 18))
-               
+               self.l.setItemHeight(23)
+               self.serviceHandler = eServiceCenter.getInstance()
+
+       def refreshMountpoints(self):
+               self.mountpoints = [os_path.join(p.mountpoint, "") for p in harddiskmanager.getMountedPartitions()]
+               self.mountpoints.sort(reverse = True)
+
+       def getMountpoint(self, file):
+               file = os_path.join(os_path.realpath(file), "")
+               for m in self.mountpoints:
+                       if file.startswith(m):
+                               return m
+               return False
+
+       def getMountpointLink(self, file):
+               if os_path.realpath(file) == file:
+                       return self.getMountpoint(file)
+               else:
+                       if file[-1] == "/":
+                               file = file[:-1]
+                       mp = self.getMountpoint(file)
+                       last = file
+                       file = os_path.dirname(file)
+                       while last != "/" and mp == self.getMountpoint(file):
+                               last = file
+                               file = os_path.dirname(file)
+                       return os_path.join(last, "")
+
        def getSelection(self):
                if self.l.getCurrentSelection() is None:
                        return None
                return self.l.getCurrentSelection()[0]
-       
+
+       def getCurrentEvent(self):
+               l = self.l.getCurrentSelection()
+               if not l or l[0][1] == True:
+                       return None
+               else:
+                       return self.serviceHandler.info(l[0][0]).getEvent(l[0][0])
+
        def getFileList(self):
                return self.list
-       
+
+       def inParentDirs(self, dir, parents):
+               dir = os_path.realpath(dir)
+               for p in parents:
+                       if dir.startswith(p):
+                               return True
+               return False
+
        def changeDir(self, directory, select = None):
                self.list = []
-               
+
+               if directory and not os_path.isdir(directory):
+                       directory = None
                # if we are just entering from the list of mount points:
                if self.current_directory is None:
-                       self.mount_point = directory
+                       if directory and self.showMountpoints:
+                               self.current_mountpoint = self.getMountpointLink(directory)
+                       else:
+                               self.current_mountpoint = None
                self.current_directory = directory
                directories = []
                files = []
-               
-               if directory is None: # present available mountpoints
-                       print "listing partitions:"
+
+               if directory is None and self.showMountpoints: # present available mountpoints
                        for p in harddiskmanager.getMountedPartitions():
-                               if p.mountpoint == "/":
-                                       self.list.append(FileEntryComponent(name = p.description, absolute = p.mountpoint, isDir = True))
-                               else:
-                                       self.list.append(FileEntryComponent(name = p.description, absolute = p.mountpoint + "/", isDir = True))
+                               path = os_path.join(p.mountpoint, "")
+                               if path not in self.inhibitMounts and not self.inParentDirs(path, self.inhibitDirs):
+                                       self.list.append(FileEntryComponent(name = p.description, absolute = path, isDir = True))
                        files = [ ]
                        directories = [ ]
                elif self.useServiceRef:
                        root = eServiceReference("2:0:1:0:0:0:0:0:0:0:" + directory)
+                       if self.additional_extensions:
+                               root.setName(self.additional_extensions)
                        serviceHandler = eServiceCenter.getInstance()
                        list = serviceHandler.list(root)
-                       
+
                        while 1:
                                s = list.getNext()
                                if not s.valid():
@@ -107,28 +154,29 @@ class FileList(MenuList, HTMLComponent, GUIComponent):
                                        directories.append(s.getPath())
                                else:
                                        files.append(s)
-                               print s.getName(), s.flags
                        directories.sort()
                        files.sort()
                else:
-                       files = os.listdir(directory)
-                       files.sort()
-                       tmpfiles = files[:]
-                       for x in tmpfiles:
-                               if os.path.isdir(directory + x):
-                                       directories.append(directory + x + "/")
-                                       files.remove(x)
-               
+                       if os_path.exists(directory):
+                               files = listdir(directory)
+                               files.sort()
+                               tmpfiles = files[:]
+                               for x in tmpfiles:
+                                       if os_path.isdir(directory + x):
+                                               directories.append(directory + x + "/")
+                                               files.remove(x)
+
                if directory is not None and self.showDirectories and not self.isTop:
-                       if directory == self.mount_point:
-                               self.list.append(FileEntryComponent(name = ".. (" +_("List of Storage Devices") + ")", absolute = None, isDir = True))
-                       else:
-                               self.list.append(FileEntryComponent(name = "..", absolute = '/'.join(directory.split('/')[:-2]) + '/', isDir = True))
+                       if directory == self.current_mountpoint and self.showMountpoints:
+                               self.list.append(FileEntryComponent(name = "<" +_("List of Storage Devices") + ">", absolute = None, isDir = True))
+                       elif (directory != "/") and not (self.inhibitMounts and self.getMountpoint(directory) in self.inhibitMounts):
+                               self.list.append(FileEntryComponent(name = "<" +_("Parent Directory") + ">", absolute = '/'.join(directory.split('/')[:-2]) + '/', isDir = True))
 
                if self.showDirectories:
                        for x in directories:
-                               name = x.split('/')[-2]
-                               self.list.append(FileEntryComponent(name = name, absolute = x, isDir = True))
+                               if not (self.inhibitMounts and self.getMountpoint(x) in self.inhibitMounts) and not self.inParentDirs(x, self.inhibitDirs):
+                                       name = x.split('/')[-2]
+                                       self.list.append(FileEntryComponent(name = name, absolute = x, isDir = True))
 
                if self.showFiles:
                        for x in files:
@@ -138,15 +186,12 @@ class FileList(MenuList, HTMLComponent, GUIComponent):
                                else:
                                        path = directory + x
                                        name = x
-                               
-                               if self.matchingPattern is not None:
-                                       if re.compile(self.matchingPattern).search(path):
-                                               self.list.append(FileEntryComponent(name = name, absolute = x , isDir = False))
-                               else:
+
+                               if (self.matchingPattern is None) or re_compile(self.matchingPattern).search(path):
                                        self.list.append(FileEntryComponent(name = name, absolute = x , isDir = False))
 
                self.l.setList(self.list)
-               
+
                if select is not None:
                        i = 0
                        self.moveToIndex(0)
@@ -167,12 +212,12 @@ class FileList(MenuList, HTMLComponent, GUIComponent):
                if self.getSelection() is None:
                        return False
                return self.getSelection()[1]
-       
+
        def descent(self):
                if self.getSelection() is None:
                        return
                self.changeDir(self.getSelection()[0], select = self.current_directory)
-               
+
        def getFilename(self):
                if self.getSelection() is None:
                        return None
@@ -189,8 +234,16 @@ class FileList(MenuList, HTMLComponent, GUIComponent):
                        return x
                return None
 
-       GUI_WIDGET = eListbox
+       def execBegin(self):
+               harddiskmanager.on_partition_list_change.append(self.partitionListChanged)
+
+       def execEnd(self):
+               harddiskmanager.on_partition_list_change.remove(self.partitionListChanged)
 
-       def postWidgetCreate(self, instance):
-               instance.setContent(self.l)
-               instance.setItemHeight(23)
+       def refresh(self):
+               self.changeDir(self.current_directory, self.getFilename())
+
+       def partitionListChanged(self, action, device):
+               self.refreshMountpoints()
+               if self.current_directory is None:
+                       self.refresh()