adding icon/logo to pluginlist
[enigma2-plugins.git] / logomanager / src / plugin.py
1 from Plugins.Plugin import PluginDescriptor
2 from Screens.Screen import Screen
3 from Screens.ChoiceBox import ChoiceBox
4 from Components.Label import Label
5 from Components.MenuList import MenuList
6 from Components.ActionMap import ActionMap
7 from Components.config import config,ConfigSubsection,ConfigSelection, getConfigListEntry
8 from Components.ConfigList import ConfigListScreen
9
10 from os import path as os_path, listdir as os_listdir, system as os_system, remove as os_remove
11 ###############################################################################        
12 config.plugins.logomanager = ConfigSubsection()
13 config.plugins.logomanager.path = ConfigSelection([("/media/cf/bootlogos/",_("CF Drive")),("/media/hdd/bootlogos/",_("Harddisk"))],default="/media/hdd/bootlogos/")
14     
15 def main(session,**kwargs):
16     if os_path.isdir(config.plugins.logomanager.path.value) is not True:
17         session.open(LogoManagerConfigScreen)
18     else:    
19         session.open(LogoManagerScreen)
20
21 def Plugins(path,**kwargs):
22     global plugin_path
23     plugin_path = path
24     return  PluginDescriptor(
25                 name="Logo Manager", 
26                 description="manage logos to display at boottime", 
27                 where = PluginDescriptor.WHERE_PLUGINMENU,
28                 icon="plugin.png",
29                 fnc = main
30                 )
31 ############################################################################### 
32 class LogoManagerScreen(Screen):
33     skin = """
34         <screen flags="wfNoBorder" position="60,450" size="600,29" title="Logo Manager" >
35             <widget name="filelist" position="0,0" size="600,30"  />
36          </screen>"""
37      
38     targets = [
39                 ("bootlogo","/boot/bootlogo.mvi")
40                ,("wait","/boot/bootlogo_wait.mvi")
41                ,("backdrop","/boot/backdrop.mvi")
42                ,("radio","/usr/share/enigma2/radio.mvi")
43                ]
44     
45     def __init__(self, session, args = 0):
46         self.session = session
47         config.plugins.logomanager.path.value 
48         self.skin = LogoManagerScreen.skin
49         Screen.__init__(self, session)
50         self["filelist"] = MenuList([])
51         self["filelist"].onSelectionChanged.append(self.showSelected)
52         self["actions"] = ActionMap(["WizardActions", "DirectionActions","MenuActions","ShortcutActions","GlobalActions"], 
53             {
54              "ok": self.showSelected,
55              "back": self.exit,
56              "menu": self.openMenu,
57              }, -1)
58         
59         ## stop current service to free the videodevice
60         self.current_service = self.session.nav.getCurrentlyPlayingServiceReference()
61         self.session.nav.stopService()
62         
63         self.check_backup()
64         self.setlist_to_avaiable()
65         
66         self.makeBootWritable()
67         self.onShown.append(self.showSelected)
68         
69     def check_backup(self):
70         """ if a copy of the original file is not found in the plugindir, backup them """
71         global plugin_path
72         for target in self.targets:
73             file = target[1].split("/")[-1]
74             if os_path.isfile(plugin_path+file) is not True:
75                 print "backing up original ",target[0]," from ",file
76                 os_system("cp '%s' '%s'" %(target[1],plugin_path+"/"+file))
77                 
78     def restoreOriginal(self):
79         """ restoring original mvis from the backuped mvi in the plugindir"""
80         global plugin_path
81         for target in self.targets:
82             file = target[1].split("/")[-1]
83             if os_path.isfile(plugin_path+"/"+file) is True:
84                 print "restoring original ",target[0]," from ",plugin_path+"/"+file,"to",target[1]
85                 os_system("cp '%s' '%s'" %(plugin_path+"/"+file,target[1]))
86                 
87     def exit(self):
88         """ quit me """
89         self.makeBootReadonly()
90         self.session.nav.playService(self.current_service)
91         self.close()
92         
93     def showSelected(self):
94         """ show the currently selected MVI of the list """
95         sel = self["filelist"].getCurrent()
96         if sel is not None:
97           self.showMVI(sel[1])
98     
99     def openMenu(self):
100         """ opens up the Main Menu """
101         menu = []
102         menu.append(("install selected Logo as ...",self.action_install))
103         menu.append(("show aktive Logos",self.setlist_to_current))
104         menu.append(("show avaiable Logos",self.setlist_to_avaiable))
105         menu.append(("reset all Logos to default",self.restoreOriginal))
106         menu.append(("open configuration",self.openConfig))
107         self.session.openWithCallback(self.selectedMenu,ChoiceBox,_("please select a option"),menu)
108     
109     def openConfig(self):
110         self.session.open(LogoManagerConfigScreen)
111         
112     def selectedMenu(self,choice):
113         if choice is not None:
114             choice[1]()
115             
116     def setlist_to_current(self):
117         """ fills the list with the target MVIs"""
118         global plugin_path
119         filelist =[]
120         for i in self.targets:
121             filelist.append(i[1])
122         self.reloadPictures(filelist)
123         
124         
125     def setlist_to_avaiable(self):
126         """ fills the list with all found new MVIs"""
127         filelist =[]
128         for i in os_listdir(config.plugins.logomanager.path.value):
129             if i.endswith(".mvi"):
130                 filelist.append(config.plugins.logomanager.path.value+i)
131         filelist.sort()
132         self.reloadPictures(filelist)
133         
134             
135     def action_install(self):
136         """ choicebox, to select target to install an mvi to"""
137         self.session.openWithCallback(self.selectedTarget,ChoiceBox,_("select Target for logo"),self.targets)
138     
139     def selectedTarget(self,choice):
140         if choice is not None:
141             self.installMVI(choice,self["filelist"].getCurrent()[1])
142     
143     def reloadPictures(self,filelist):
144         """ build the menulist with givven files """
145         list = []
146         for i in filelist:
147                 list.append((i.split("/")[-1],i))
148         self["filelist"].l.setList(list)
149         
150     
151     def showMVI(self,mvifile):
152         """ shows a mvi """
153         print "playing MVI",mvifile
154         os_system("/usr/bin/showiframe '%s'" % mvifile)
155     
156     def installMVI(self,target,sourcefile):
157         """ installs a mvi by overwriting the target with a source mvi """
158         print "installing %s as %s on %s" %(sourcefile,target[0],target[1])
159         if os_path.isfile(target[1]):
160             os_remove(target[1])
161         os_system("cp '%s' '%s'"%(sourcefile,target[1]))
162     
163     def makeBootWritable(self):
164         """ because /boot isnt writeable by default, we will change that here """
165         os_system("mount -o rw,remount /boot")
166     
167     def makeBootReadonly(self):
168         """ make /boot writeprotected back again """
169         os_system("mount -o r,remount /boot")
170         
171 class LogoManagerConfigScreen(ConfigListScreen,Screen):
172     skin = """
173         <screen position="100,100" size="550,400" title="LogoManager Setup" >
174         <widget name="config" position="0,0" size="550,360" scrollbarMode="showOnDemand" />
175         <widget name="buttonred" position="10,360" size="100,40" backgroundColor="red" valign="center" halign="center" zPosition="2"  foregroundColor="white" font="Regular;18"/> 
176         <widget name="buttongreen" position="120,360" size="100,40" backgroundColor="green" valign="center" halign="center" zPosition="2"  foregroundColor="white" font="Regular;18"/> 
177         </screen>"""
178     def __init__(self, session, args = 0):
179         self.session = session
180         Screen.__init__(self, session)
181         self.list = []
182         self.list.append(getConfigListEntry(_("Directory to scan for Logos"), config.plugins.logomanager.path))
183         ConfigListScreen.__init__(self, self.list)
184         self["buttonred"] = Label(_("cancel"))
185         self["buttongreen"] = Label(_("ok"))
186         self["setupActions"] = ActionMap(["SetupActions"],
187         {
188             "green": self.save,
189             "red": self.cancel,
190             "save": self.save,
191             "cancel": self.cancel,
192             "ok": self.save,
193         }, -2)
194
195     def save(self):
196         print "saving"
197         for x in self["config"].list:
198             x[1].save()
199         self.close(True)
200
201     def cancel(self):
202         print "cancel"
203         for x in self["config"].list:
204             x[1].cancel()
205         self.close(False)
206
207