Much improved error-handling
[enigma2-plugins.git] / youtubeplayer / src / YouTubeUserList.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 Components.ActionMap import ActionMap
22 from Components.Button import Button
23 from Components.Label import Label
24 from Components.MenuList import MenuList
25 from Screens.MessageBox import MessageBox
26 from Screens.Screen import Screen
27 from Tools.Directories import resolveFilename, SCOPE_PLUGINS
28 from Tools.LoadPixmap import LoadPixmap
29 from YouTubeUserConfig import youTubeUserConfig
30 from YouTubeUserConfig import YouTubeUserConfigScreen
31 from enigma import eListboxPythonMultiContent, RT_HALIGN_LEFT, gFont
32
33 from . import _
34
35 def YouTubeUserListEntry(youTubeUser, defaultUser):
36         res = [ youTubeUser ]
37         res.append((eListboxPythonMultiContent.TYPE_TEXT, 35, 1, 470, 20, 0, RT_HALIGN_LEFT, youTubeUser.getName()))
38
39         if defaultUser is not None and defaultUser.getName() == youTubeUser.getName():
40                 png = LoadPixmap(resolveFilename(SCOPE_PLUGINS, "Extensions/YouTubePlayer/user_default.png"))
41         else:
42                 png = LoadPixmap(resolveFilename(SCOPE_PLUGINS, "Extensions/YouTubePlayer/user.png"))
43         if png is not None:
44                 res.append((eListboxPythonMultiContent.TYPE_PIXMAP_ALPHATEST, 10, 2, 20, 20, png))
45
46         return res
47
48
49 class YouTubeUserList(MenuList):
50         def __init__(self):
51                 MenuList.__init__(self, list, False, eListboxPythonMultiContent)
52                 self.l.setFont(0, gFont("Regular", 18))
53                 self.l.setItemHeight(23)
54
55         def update(self, userList, defaultUser):
56                 self.list = []
57                 for user in userList:
58                         self.list.append(YouTubeUserListEntry(user, defaultUser))
59                 self.l.setList(self.list)
60                 self.moveToIndex(0)
61
62         def getSelection(self):
63                 if self.l.getCurrentSelection() is None:
64                         return None
65                 return self.l.getCurrentSelection()[0]
66
67
68 class YouTubeUserListScreen(Screen):
69         LOGIN_SUCCESS = 1
70         LOGIN_CANCEL = 2
71         LOGIN_FAILED = 3
72
73         def __init__(self, session, defaultUser):
74                 Screen.__init__(self, session)
75                 self.session = session
76                 self.userlist = YouTubeUserList()
77                 self.defaultUser = defaultUser
78
79                 self["label_info"] = Label(_("To use the selected feature you have to login to YouTube. Select a user-profile to login!"))
80                 self["userlist"] = self.userlist
81                 self["key_red"] = Button(_("delete user"))
82                 self["key_green"] = Button(_("add user"))
83                 self["key_yellow"] = Button(_("edit user"))
84                 self["key_blue"] = Button(_("set default"))
85
86                 self["actions"] = ActionMap(["YouTubeUserListScreenActions"],
87                 {
88                         "delete":       self.keyDelete,
89                         "add":          self.keyAddUser,
90                         "edit":         self.keyEditUser,
91                         "default":      self.keySetAsDefault,
92                         
93                         "ok":           self.ok,
94                         "cancel":       self.close,
95                         "up":           self.up,
96                         "down":         self.down,
97                         "left":         self.left,
98                         "right":        self.right,
99                 }, -2)
100                 
101                 self.onLayoutFinish.append(self.initialUserlistUpdate)
102
103
104         def showTestScreen(self):
105                 self.suggestionsWindow.show()
106
107
108         def initialUserlistUpdate(self):
109                 self.updateUserlist()
110                 if self.defaultUser is not None:
111                         defaultIndex = youTubeUserConfig.getUserlist().index(self.defaultUser)
112                         self.userlist.moveToIndex(defaultIndex)
113
114
115         def updateUserlist(self):
116                 self.userlist.update(youTubeUserConfig.getUserlist(), self.defaultUser)
117
118
119         def keyDelete(self):
120                 user = self.userlist.getSelection()
121                 if user is not None:
122                         self.session.openWithCallback(self.deleteCallback, MessageBox, _("Really delete %(user)s?") % {"user" : user.getName()})
123
124         def deleteCallback(self, result):
125                 if result:
126                         youTubeUserConfig.delete(self.userlist.getSelection())
127                         self.updateUserlist()
128
129
130         def keyAddUser(self):
131                 newUser = youTubeUserConfig.new()
132                 self.session.openWithCallback(self.addCallback, YouTubeUserConfigScreen, newUser)
133
134
135         def addCallback(self, result, user):
136                 if result:
137                         youTubeUserConfig.save(user)
138                         self.updateUserlist()
139                 else:
140                         youTubeUserConfig.delete(user)
141
142
143         def keyEditUser(self):
144                 user = self.userlist.getSelection()
145                 if user is not None:
146                         self.session.openWithCallback(self.editCallback, YouTubeUserConfigScreen, user)
147
148
149         def editCallback(self, result, user):
150                 if result:
151                         youTubeUserConfig.save(user)
152                         index = self.userlist.getSelectedIndex()
153                         self.updateUserlist()
154                         self.userlist.moveToIndex(index)
155                 else:
156                         youTubeUserConfig.cancel(user)
157
158
159         def keySetAsDefault(self):
160                 self.defaultUser = self.userlist.getSelection()
161                 index = self.userlist.getSelectedIndex()
162                 self.updateUserlist()
163                 self.userlist.moveToIndex(index)
164
165
166         def up(self):
167                 self.userlist.up()
168
169
170         def down(self):
171                 self.userlist.down()
172
173
174         def left(self):
175                 self.userlist.pageUp()
176
177
178         def right(self):
179                 self.userlist.pageDown()
180
181
182         def close(self, loginState = LOGIN_CANCEL):
183                 youTubeUserConfig.setAsDefault(self.defaultUser)
184                 Screen.close(self, loginState)
185
186
187         def ok(self):
188                 try:
189                         if self.userlist.getSelection().login():
190                                 self.close(YouTubeUserListScreen.LOGIN_SUCCESS)
191                         else:
192                                 self.close(YouTubeUserListScreen.LOGIN_FAILED)
193                 except:
194                         self.close(YouTubeUserListScreen.LOGIN_FAILED)