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