fix caching, allow path= to override last path component (for LCD picons or different...
[enigma2.git] / lib / python / Components / Renderer / Picon.py
1 ##
2 ## Picon renderer by Gruffy .. some speedups by Ghost
3 ##
4 from Renderer import Renderer
5 from enigma import ePixmap
6 from Tools.Directories import pathExists, fileExists, SCOPE_SKIN_IMAGE, resolveFilename
7
8 class Picon(Renderer):
9         searchPaths = ['/usr/share/enigma2/%s/',
10                                 '/media/cf/%s/',
11                                 '/media/usb/%s/']
12
13         def __init__(self):
14                 Renderer.__init__(self)
15                 self.path = "picon"
16                 self.nameCache = { }
17                 self.pngname = ""
18
19         def applySkin(self, desktop):
20                 print "-> Picon, applySkin:", self.skinAttributes
21                 attribs = [ ]
22                 for (attrib, value) in self.skinAttributes:
23                         if attrib == "path":
24                                 self.path = value
25                         else:
26                                 attribs.append((attrib,value))
27                 self.skinAttributes = attribs
28                 return Renderer.applySkin(self, desktop)
29
30         GUI_WIDGET = ePixmap
31
32         def changed(self, what):
33                 print "PICON: path=%s" % self.path
34                 if self.instance:
35                         pngname = ""
36                         if what[0] != self.CHANGED_CLEAR:
37                                 sname = self.source.text
38                                 # strip all after last :
39                                 pos = sname.rfind(':')
40                                 if pos != -1:
41                                         sname = sname[:pos].rstrip(':').replace(':','_')
42                                 pngname = self.nameCache.get(sname, "")
43                                 if pngname == "":
44                                         pngname = self.findPicon(sname)
45                                         if pngname != "":
46                                                 self.nameCache[sname] = pngname
47                         if pngname == "": # no picon for service found
48                                 pngname = self.nameCache.get("default", "")
49                                 if pngname == "": # no default yet in cache..
50                                         pngname = self.findPicon("picon_default")
51                                         if pngname == "": # Fallback to enigma2 logo
52                                                 pngname = resolveFilename(SCOPE_SKIN_IMAGE, "skin_default/enigma2.png")
53                                         self.nameCache["default"] = pngname
54                         if self.pngname != pngname:
55                                 self.instance.setPixmapFromFile(pngname)
56                                 self.pngname = pngname
57
58         def findPicon(self, serviceName):
59                 for path in self.searchPaths:
60                         pngname = (path % self.path) + serviceName + ".png"
61                         if fileExists(pngname):
62                                 return pngname
63                 return ""