Much improved error-handling
[enigma2-plugins.git] / youtubeplayer / src / YouTubePlayList.py
1 ############################################################################
2 #    Copyright (C) 2008 by Volker Christian                                #
3 #    Volker.Christian@fh-hagenberg.at                                      #
4 #                                                                          #
5 #    This program is free software; you can redistribute it and#or modify  #
6 #    it under the terms of the GNU General Public License as published by  #
7 #    the Free Software Foundation; either version 2 of the License, or     #
8 #    (at your option) any later version.                                   #
9 #                                                                          #
10 #    This program is distributed in the hope that it will be useful,       #
11 #    but WITHOUT ANY WARRANTY; without even the implied warranty of        #
12 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         #
13 #    GNU General Public License for more details.                          #
14 #                                                                          #
15 #    You should have received a copy of the GNU General Public License     #
16 #    along with this program; if not, write to the                         #
17 #    Free Software Foundation, Inc.,                                       #
18 #    59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             #
19 ############################################################################
20
21 from YouTubeInterface import interface
22 from YouTubeAddPlayList import YouTubeAddPlaylistDialog
23 from Components.ActionMap import ActionMap
24 from Components.MenuList import MenuList
25 from Components.Label import Label
26 from Components.MultiContent import MultiContentEntryText
27 from Screens.MessageBox import MessageBox
28 from Screens.Screen import Screen
29 from enigma import eListboxPythonMultiContent, gFont, RT_HALIGN_LEFT, RT_VALIGN_TOP, RT_WRAP
30
31 from . import _
32
33
34 def YouTubePlaylistEntryComponent(entry):
35         res = [ entry ]
36
37         res.append(MultiContentEntryText(pos = (5, 5), size = (550, 18), font = 0, flags = RT_HALIGN_LEFT | RT_VALIGN_TOP| RT_WRAP, text = entry.getTitle()))
38         res.append(MultiContentEntryText(pos = (5, 23), size = (550, 14), font = 1, color = 0xFFA323, color_sel = 0xFFA323, flags = RT_HALIGN_LEFT | RT_VALIGN_TOP| RT_WRAP, text = entry.getDescription()))
39
40         return res
41
42
43 class YouTubePlaylistList(MenuList):
44         def __init__(self, list, enableWrapAround = False):
45                 MenuList.__init__(self, list, enableWrapAround, eListboxPythonMultiContent)
46                 self.l.setFont(0, gFont("Regular", 18))
47                 self.l.setFont(1, gFont("Regular", 14))
48                 self.l.setItemHeight(41)
49
50
51 class YouTubePlaylistScreen(Screen):
52         def __init__(self, session):
53                 Screen.__init__(self, session)
54
55                 self.session = session
56
57                 self["red"] = Label(_("Delete Playlist"))
58                 self["green"] = Label(_("Add new Playlist"))
59                 
60                 self.list = []
61                 self["list"] = YouTubePlaylistList(self.list)
62                 
63                 self["actions"] = ActionMap(["YouTubePlaylistScreenActions"],
64                 {
65                         "ok"            :       self.choosePlaylist,
66                         "delete"        :       self.deletePlaylist,
67                         "add"           :       self.addPlaylist,
68                         "cancel"        :       self.close
69                 }, -1)
70
71
72         def loadPlaylist(self):
73                 self.list = []
74                 try:
75                         feed = interface.getPlaylistFeed()
76                         for entry in feed.getEntries():
77                                 self.list.append(YouTubePlaylistEntryComponent(entry))
78                 except Exception, e:
79                         self.session.open(MessageBox, _("Error loading playlists:\n%s" %
80                                         e), MessageBox.TYPE_ERROR)
81                 self["list"].setList(self.list)
82
83
84         def choosePlaylist(self):
85                 Screen.close(self, self["list"].getCurrent()[0])
86
87
88         def deletePlaylist(self):
89                 playList = self["list"].getCurrent()[0]
90                 if playList is not None:
91                         self.session.openWithCallback(self.deleteCallback, MessageBox, _("Really delete %(playlist)s?") % {"playlist" : playList.getTitle()})
92
93
94         def deleteCallback(self, result):
95                 if result:
96                         if interface.deletePlaylist(self["list"].getCurrent()[0]):
97                                 self.list.remove(self["list"].getCurrent())
98                                 self["list"].setList(self.list)
99
100
101         def addPlaylist(self):
102                 self.session.openWithCallback(self.addCallback, YouTubeAddPlaylistDialog)
103
104
105         def addCallback(self, result, playlistContext):
106                 if result:
107                         entry = interface.addPlaylist(playlistContext.name.value, playlistContext.description.value, playlistContext.private.value)
108                         self.list.append(YouTubePlaylistEntryComponent(entry))
109                         self["list"].setList(self.list)
110
111
112         def close(self):
113                 Screen.close(self, None)