4.3.1r21
[enigma2.git] / usr / lib / enigma2 / python / Plugins / Extensions / MediaCenter / plugin.py
1 # -*- coding: UTF-8 -*-
2 from Screens.Screen import Screen
3
4 from Components.ActionMap import ActionMap
5 from Components.Label import Label
6 from Components.Sources.List import List
7
8 from Tools.Directories import resolveFilename, pathExists, SCOPE_PLUGINS, SCOPE_CURRENT_SKIN
9 from Tools.Log import Log
10
11 from AudioPlayer import AudioPlayer
12 from VideoPlayer import VideoPlayer
13 from Helpers import EasyPixmap
14 from MediaCore import MediaCore, mediaCore
15
16 class MediaCenterMenuSummary(Screen):
17         skin = ("""
18         <screen name="MediaCenterMenuSummary" position="0,0" size="132,64" id="1">
19                 <widget source="Title" render="Label" position="6,0" size="120,32" font="Display;15" halign="center" valign="top"/>
20                 <widget source="parent.menulist" render="Label" position="6,32" size="120,32" font="Display;16" halign="center" valign="center">
21                         <convert type="StringListSelection" />
22                 </widget>
23         </screen>""",
24         """<screen name="MediaCenterMenuSummary" position="0,0" size="96,64" id="2">
25                 <widget source="Title" render="Label" position="0,0" size="96,32" font="Display;14" halign="center" valign="top"/>
26                 <widget source="parent.menulist" render="Label" position="0,32" size="96,32" font="Display;16" halign="center" valign="center">
27                         <convert type="StringListSelection" />
28                 </widget>
29         </screen>
30         """)
31
32         def __init__(self, session, parent, windowTitle=_("MediaCenter")):
33                 Screen.__init__(self, session, parent, windowTitle)
34
35 class MainMenu(Screen):
36         icon_path = resolveFilename(SCOPE_PLUGINS, "Extensions/MediaCenter/icons/")
37
38         skin = """
39                         <screen name="MainMenu" position="center,120" size="720,520" title="MediaCenter Menu">
40                                 <ePixmap position="10,5" size="300,500" pixmap="skin_default/menu.png" zPosition="-1"/>
41                                 <widget name="header" position="320,10" size="390,50" halign="center" font="Regular;45" transparent="1"/>
42                                 <eLabel position="330,65" size="370,2" backgroundColor="grey" />
43                                 <widget name="subheader" position="30,350" size="260,70" halign="center" valign="center" font="Regular;26" backgroundColor="background" transparent="1"/>
44                                 <widget source="menulist" render="Listbox" position="320,100" size="390,450" enableWrapAround="1" scrollbarMode="showOnDemand">
45                                         <convert type="TemplatedMultiContent">
46                                                 {"template": [
47                                                                 MultiContentEntryText(pos = (5,0), size = (640,45), font=0, flags = RT_HALIGN_LEFT|RT_VALIGN_CENTER, text = 0), # index 0 is the name
48                                                         ],
49                                                 "fonts": [gFont("Regular", 30)],
50                                                 "itemHeight": 45
51                                                 }
52                                         </convert>
53                                 </widget>
54                                 <widget name="menuIcon" position="65,50" size="192,192" alphatest="on" />
55                                 <ePixmap pixmap="skin_default/icons/dmm_logo.png" position="20,470" size="280,17" alphatest="on" />
56                         </screen>
57                 """
58
59         def __init__(self, session, type=None, playlist=None):
60                 Screen.__init__(self, session)
61                 self.session = session
62
63                 self.list = []
64                 self["menuIcon"] = EasyPixmap(cached=True)
65                 self["menulist"] = List(self.list, True)
66                 self["header"] = Label(_("MediaCenter"))
67                 self["subheader"] = Label(_("Video, audio and pictures"))
68
69                 self["actions"] = ActionMap(["WizardActions", "MenuActions"],
70                         {       "ok" : self.ok,
71                                 "back" : self._close,
72                                 "up" : self.up,
73                                 "down" : self.down,
74                                 "menu" : self.menu
75                         }, -1);
76
77                 self.onFirstExecBegin.append(self._onFirstExecBegin)
78                 self.onShown.append(self._onItemChanged)
79
80                 self.type = type
81                 self.playlist = playlist
82                 if self.type != None and self.playlist != None:
83                         self.onShown.append(self.runOnFirstExec)
84
85         def menu(self):
86                 try:
87                         from Plugins.SystemPlugins.MediaDatabaseManager.MediaDatabaseManager import MediaDatabaseManager
88                         self.session.open(MediaDatabaseManager)
89                 except:
90                         pass
91
92         def createSummary(self):
93                 return MediaCenterMenuSummary
94
95         def runOnFirstExec(self):
96                 self["menulist"].setIndex(self.type)
97
98                 playlist = self.playlist
99                 self.type = None
100                 self.playlist = None
101
102                 i = 0
103                 for fnc in self.onShown:
104                         if fnc == self.runOnFirstExec:
105                                 self.onShown.pop(i)
106                         i = i + 1
107
108                 self.ok(playlist)
109
110         def _onFirstExecBegin(self):
111                 l = mediaCore.getMainMenuItems()
112                 self.list = l
113                 self["menulist"].setList(l)
114                 self["menulist"].setIndex(0)
115
116         def ok(self, playlist=None):
117                 choice = self["menulist"].getCurrent()
118                 if choice != None:
119                         if choice[2].get("featuresPlaylist", False):
120                                 self.session.open(choice[1], playlist)
121                         else:
122                                 self.session.open(choice[1])
123
124         def _close(self):
125                 self.close()
126
127         def _onItemChanged(self):
128                 if self.list and len(self.list) > 0:
129                         choice = self["menulist"].getCurrent()
130
131                         iconPath = choice[2].get("icon", None)
132                         if iconPath is not None:
133                                 self["menuIcon"].setPicturePath(iconPath)
134                         else:
135                                 self["menuIcon"].setDefaultPicture()
136
137         def up(self):
138                 self["menulist"].selectPrevious()
139                 self._onItemChanged()
140
141         def down(self):
142                 self["menulist"].selectNext()
143                 self._onItemChanged()
144
145 def filescan(**kwargs):
146         from Components.Scanner import Scanner, ScanPath
147         mediatypes = [
148                 Scanner(mimetypes=["video/mpeg", "video/MP2T", "video/x-msvideo", "video/x-matroska"],
149                         paths_to_scan=
150                                 [
151                                         ScanPath(path="", with_subdirs=False),
152                                 ],
153                         name="MediaCenter Movies",
154                         description=_("View Movies... (MediaCenter)"),
155                         openfnc=filescan_open_video,
156                 ),
157                 Scanner(mimetypes=["audio/mpeg", "audio/x-wav", "application/ogg", "audio/x-flac", "audio/x-matroska"],
158                         paths_to_scan=
159                                 [
160                                         ScanPath(path="", with_subdirs=False),
161                                 ],
162                         name="MediaCenter Music",
163                         description=_("Play Music... (MediaCenter)"),
164                         openfnc=filescan_open_audio,
165                 )]
166         return mediatypes
167
168 def filescan_open_video(filelist, session, **kwargs):
169         filescan_open(MediaCore.TYPE_VIDEO, filelist, session, **kwargs)
170
171 def filescan_open_audio(filelist, session, **kwargs):
172         filescan_open(MediaCore.TYPE_AUDIO, filelist, session, **kwargs)
173
174 def filescan_open(type, filelist, session, **kwargs):
175         from enigma import eServiceReference
176         mediaCore.setSession(session)
177
178         playlist = []
179         for file in filelist:
180                 if file.mimetype == "video/MP2T":
181                         stype = eServiceReference.idDVB
182                 else:
183                         stype = eServiceReference.idGST
184                 ref = eServiceReference(stype, 0, file.path)
185                 playlist.append(ref)
186
187         session.open(MainMenu, type, playlist)
188
189 def getIcon(key):
190         filename = resolveFilename(SCOPE_CURRENT_SKIN, "menu/mc_%s.svg" %key)
191         if not pathExists(filename):
192                 filename = resolveFilename(SCOPE_CURRENT_SKIN, "menu/mc_%s.png" %key)
193         if pathExists(filename):
194                 return filename
195         else:
196                 return resolveFilename(SCOPE_PLUGINS, "Extensions/MediaCenter/icons/%s.png" %key)
197
198 def addDefaultMenuItems():
199         mediaCore.addToMainMenu((
200                 _("Music and Audiobooks"),
201                 AudioPlayer,
202                 {
203                         "key" : "music",
204                         "icon": getIcon("music"),
205                         "featuresPlaylist" : True
206                 }
207         ))
208         mediaCore.addToMainMenu((
209                 _("Videos"),
210                 VideoPlayer,
211                 {
212                         "key" : "movies",
213                         "icon" : getIcon("movies"),
214                         "featuresPlaylist" : True
215                 }
216         ))
217         try:
218                 from Plugins.Extensions.PicturePlayer.plugin import picshow
219                 mediaCore.addToMainMenu((
220                         _("Pictures"),
221                         picshow,
222                         {
223                                 "key" : "pictures",
224                                 "icon": getIcon("pictures"),
225                                 "featuresPlaylist" : False
226                         }
227                 ))
228         except:
229                 Log.w("PicturePlayer is not available")
230
231 addDefaultMenuItems()
232 def menu(menuid, **kwargs):
233         if menuid == "mainmenu":
234                 return [(_("MediaCenter"), main, "mediacenter", 20)]
235         return []
236
237 def main(session, **kwargs):
238         mediaCore.setSession(session)
239         session.open(MainMenu)
240
241 from Plugins.Plugin import PluginDescriptor
242 def Plugins(**kwargs):
243         return [
244                         PluginDescriptor(name="MediaCenter",
245                                                           description="Play and watch all your media",
246                                                           where=PluginDescriptor.WHERE_MENU,
247                                                           fnc=menu),
248
249                         PluginDescriptor(name="MediaCenter",
250                                                           where=PluginDescriptor.WHERE_FILESCAN,
251                                                           fnc=filescan)
252                         ]