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