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