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