Much improved error-handling
[enigma2-plugins.git] / youtubeplayer / src / plugin.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 Plugins.Plugin import PluginDescriptor
22 from Tools.BoundFunction import boundFunction
23
24 from YouTubeList import YouTubeListScreen
25 from YouTubePlayList import YouTubePlaylistScreen
26 from YouTubeSearchDialog import YouTubeSearchDialog, SEARCH, STDFEEDS, PLAYLISTS, FAVORITES, CANCEL
27 from YouTubeUserList import YouTubeUserListScreen
28 from YouTubeUserConfig import youTubeUserConfig
29 from YouTubeStdFeedSelection import YouTubeStdFeedSelectionScreen
30 from YouTubeInterface import interface, YouTubeInterface
31 from SkinLoader import loadPluginSkin
32 from Screens.MessageBox import MessageBox
33
34 import gettext
35
36
37 def _(txt):
38         t = gettext.dgettext("YouTube", txt)
39         if t == txt:
40                 print "[YTB] fallback to default translation for", txt
41                 t = gettext.gettext(txt)
42         return t
43
44
45 class YouTubeManager():
46         def __init__(self, session):
47                 self.session = session
48                 interface.open()
49
50
51         def openSearchDialog(self):
52                 self.session.openWithCallback(self.searchDialogClosed, YouTubeSearchDialog)
53
54
55         def searchDialogClosed(self, what, searchContext = None):
56                 print "[YTB] searchDialogClosed: ", what
57                 if what == SEARCH:
58                         dlg = self.session.openWithCallback(self.youTubeListScreenClosed, YouTubeListScreen)
59                         dlg.searchFeed(searchContext)
60                 elif what == CANCEL:
61                         interface.close()
62                 elif what == STDFEEDS:
63                         self.openStandardFeeds()
64                 else:
65                         if what == PLAYLISTS:
66                                 callback = self.openPlaylists
67                         elif what == FAVORITES:
68                                 callback = self.openFavorites
69                         if not interface.isLoggedIn():
70                                 self.session.openWithCallback(callback, YouTubeUserListScreen, youTubeUserConfig.getDefaultUser())
71                         else:
72                                 callback(YouTubeUserListScreen.LOGIN_SUCCESS)
73
74
75         def openStandardFeeds(self):
76                 self.session.openWithCallback(self.standardFeedSelected, YouTubeStdFeedSelectionScreen)
77
78
79         def standardFeedSelected(self, stdFeedUrl):
80                 if stdFeedUrl is not None:
81                         dlg = self.session.openWithCallback(self.youTubeListScreenClosed, YouTubeListScreen)
82                         dlg.loadStandardFeed(stdFeedUrl)
83                 else:
84                         self.openSearchDialog()
85
86
87         def openPlaylists(self, loginState):
88                 if loginState == YouTubeUserListScreen.LOGIN_SUCCESS:
89                         print "[YTB] logged in"
90                         dlg = self.session.openWithCallback(self.playlistChoosen, YouTubePlaylistScreen)
91                         dlg.loadPlaylist()
92                 elif loginState == YouTubeUserListScreen.LOGIN_FAILED:
93                         print "[YTB] not logged in"
94                         self.session.openWithCallback(self.backToSearchDialog, MessageBox, _("Login not successful"), MessageBox.TYPE_INFO)
95                 else:
96                         self.backToSearchDialog()
97
98
99         def playlistChoosen(self, playlist):
100                 if playlist is not None:
101                         dlg = self.session.openWithCallback(self.youTubeListScreenClosed, YouTubeListScreen)
102                         dlg.loadPlaylistFeed(playlist)
103                 else:
104                         self.openSearchDialog()
105
106
107         def openFavorites(self, loginState):
108                 if loginState == YouTubeUserListScreen.LOGIN_SUCCESS:
109                         print "[YTB] logged in"
110                         dlg = self.session.openWithCallback(self.youTubeListScreenClosed, YouTubeListScreen)
111                         dlg.loadFavoritesFeed("default")
112                 elif loginState == YouTubeUserListScreen.LOGIN_FAILED:
113                         print "[YTB] not logged in"
114                         self.session.openWithCallback(self.backToSearchDialog, MessageBox, _("Login not successful"), MessageBox.TYPE_INFO)
115                 else:
116                         self.backToSearchDialog()
117
118
119         def backToSearchDialog(self, dummy = True):
120                 self.openSearchDialog()
121
122
123         def youTubeListScreenClosed(self, proceed):
124                 if proceed:
125                         self.openSearchDialog()
126                 else:
127                         interface.close()
128
129
130 def main(session, **kwargs):
131         try:
132                 youTubeManager = YouTubeManager(session)
133         except Exception, e:
134                 session.open(MessageBox, _("Error contacting YouTube:\n%s" % e), MessageBox.TYPE_ERROR)
135         else:
136                 youTubeManager.openSearchDialog()
137
138
139 def Plugins(**kwargs):
140         loadPluginSkin(kwargs["path"])
141         return PluginDescriptor(
142                 name="YouTube Player",
143                 description=_("Search and play YouTube movies"),
144                 where = [ PluginDescriptor.WHERE_EXTENSIONSMENU, PluginDescriptor.WHERE_PLUGINMENU ],
145                 icon = "plugin.png", fnc = boundFunction(main))