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