Making text on lists more thumb friendly
[watersofshiloah] / src / windows / radio.py
1 import datetime
2 import logging
3
4 import gobject
5 import gtk
6
7 import hildonize
8 import util.misc as misc_utils
9 import banners
10 import presenter
11
12 import windows
13
14
15 _moduleLogger = logging.getLogger(__name__)
16
17
18 class RadioWindow(windows._base.BasicWindow):
19
20         def __init__(self, app, player, store, node):
21                 windows._base.BasicWindow.__init__(self, app, player, store)
22                 self._node = node
23                 self._childNode = None
24
25                 self.connect_auto(self._player, "state-change", self._on_player_state_change)
26                 self.connect_auto(self._player, "title-change", self._on_player_title_change)
27
28                 self._loadingBanner = banners.GenericBanner()
29
30                 headerPath = self._store.STORE_LOOKUP["radio_header"]
31                 self._header = self._store.get_image_from_store(headerPath)
32                 self._headerNavigation = presenter.NavigationBox()
33                 self._headerNavigation.toplevel.add(self._header)
34                 self._headerNavigation.connect("action", self._on_nav_action)
35                 self._headerNavigation.connect("navigating", self._on_navigating)
36
37                 self._programmingModel = gtk.ListStore(
38                         gobject.TYPE_STRING,
39                         gobject.TYPE_STRING,
40                 )
41
42                 textrenderer = gtk.CellRendererText()
43                 timeColumn = gtk.TreeViewColumn("Time")
44                 timeColumn.set_property("sizing", gtk.TREE_VIEW_COLUMN_FIXED)
45                 timeColumn.set_property("fixed-width", 80)
46                 timeColumn.pack_start(textrenderer, expand=True)
47                 timeColumn.add_attribute(textrenderer, "text", 0)
48
49                 textrenderer = gtk.CellRendererText()
50                 hildonize.set_cell_thumb_selectable(textrenderer)
51                 titleColumn = gtk.TreeViewColumn("Program")
52                 titleColumn.set_property("sizing", gtk.TREE_VIEW_COLUMN_FIXED)
53                 titleColumn.pack_start(textrenderer, expand=True)
54                 titleColumn.add_attribute(textrenderer, "text", 1)
55
56                 self._treeView = gtk.TreeView()
57                 self._treeView.set_property("fixed-height-mode", True)
58                 self._treeView.set_headers_visible(False)
59                 self._treeView.set_model(self._programmingModel)
60                 self._treeView.append_column(timeColumn)
61                 self._treeView.append_column(titleColumn)
62                 self._treeView.get_selection().connect("changed", self._on_row_changed)
63
64                 self._treeScroller = gtk.ScrolledWindow()
65                 self._treeScroller.add(self._treeView)
66                 self._treeScroller.set_policy(gtk.POLICY_NEVER, gtk.POLICY_AUTOMATIC)
67
68                 self._presenter = presenter.StreamMiniPresenter(self._store)
69                 self._presenterNavigation = presenter.NavigationBox()
70                 self._presenterNavigation.toplevel.add(self._presenter.toplevel)
71                 self._presenterNavigation.connect("action", self._on_nav_action)
72                 self._presenterNavigation.connect("navigating", self._on_navigating)
73
74                 self._radioLayout = gtk.VBox(False)
75                 self._radioLayout.pack_start(self._headerNavigation.toplevel, False, False)
76                 self._radioLayout.pack_start(self._treeScroller, True, True)
77                 self._radioLayout.pack_start(self._presenterNavigation.toplevel, False, True)
78
79                 self._layout.pack_start(self._loadingBanner.toplevel, False, False)
80                 self._layout.pack_start(self._radioLayout, True, True)
81
82                 self._dateShown = datetime.datetime.now()
83                 self._update_title()
84
85         def show(self):
86                 windows._base.BasicWindow.show(self)
87
88                 self._errorBanner.toplevel.hide()
89                 self._loadingBanner.toplevel.hide()
90
91                 self._refresh()
92
93         def jump_to(self, node):
94                 _moduleLogger.info("Only 1 channel, nothing to jump to")
95
96         def _update_title(self):
97                 self._window.set_title("%s - %s" % (self._node.title, self._dateShown.strftime("%m/%d")))
98
99         @property
100         def _active(self):
101                 return self._player.node is self._childNode
102
103         def _set_context(self, state):
104                 if state == self._player.STATE_PLAY:
105                         if self._active:
106                                 self._presenter.set_state(self._store.STORE_LOOKUP["pause"])
107                         else:
108                                 self._presenter.set_state(self._store.STORE_LOOKUP["play"])
109                 elif state == self._player.STATE_PAUSE:
110                         self._presenter.set_state(self._store.STORE_LOOKUP["play"])
111                 elif state == self._player.STATE_STOP:
112                         self._presenter.set_state(self._store.STORE_LOOKUP["play"])
113                 else:
114                         _moduleLogger.info("Unhandled player state %s" % state)
115                         self._presenter.set_state(self._store.STORE_LOOKUP["play"])
116
117         def _show_loading(self):
118                 animationPath = self._store.STORE_LOOKUP["loading"]
119                 animation = self._store.get_pixbuf_animation_from_store(animationPath)
120                 self._loadingBanner.show(animation, "Loading...")
121
122         def _hide_loading(self):
123                 self._loadingBanner.hide()
124
125         def _refresh(self):
126                 self._show_loading()
127                 self._programmingModel.clear()
128                 self._node.get_children(
129                         self._on_channels,
130                         self._on_load_error,
131                 )
132                 self._set_context(self._player.state)
133
134         def _get_current_row(self):
135                 nowTime = self._dateShown.strftime("%H:%M:%S")
136                 i = 0
137                 for i, row in enumerate(self._programmingModel):
138                         if nowTime < row[0]:
139                                 if i == 0:
140                                         return 0
141                                 else:
142                                         return i - 1
143                 else:
144                         return i
145
146         @misc_utils.log_exception(_moduleLogger)
147         def _on_player_state_change(self, player, newState):
148                 if self._headerNavigation.is_active() or self._presenterNavigation.is_active():
149                         return
150
151                 self._set_context(newState)
152
153         @misc_utils.log_exception(_moduleLogger)
154         def _on_player_title_change(self, player, node):
155                 if node is not self._childNode or node is None:
156                         _moduleLogger.info("Player title magically changed to %s" % player.title)
157                         return
158
159         @misc_utils.log_exception(_moduleLogger)
160         def _on_navigating(self, widget, navState):
161                 if navState == "clicking":
162                         if self._player.state == self._player.STATE_PLAY:
163                                 if self._active:
164                                         imageName = "pause_pressed"
165                                 else:
166                                         imageName = "play_pressed"
167                         elif self._player.state == self._player.STATE_PAUSE:
168                                 imageName = "play_pressed"
169                         elif self._player.state == self._player.STATE_STOP:
170                                 imageName = "play_pressed"
171                         else:
172                                 imageName = "play_pressed"
173                                 _moduleLogger.info("Unhandled player state %s" % self._player.state)
174                 elif navState == "down":
175                         imageName = "home"
176                 else:
177                         if self._player.state == self._player.STATE_PLAY:
178                                 imageName = "pause"
179                         else:
180                                 imageName = "play"
181
182                 self._presenter.set_state(self._store.STORE_LOOKUP[imageName])
183
184         @misc_utils.log_exception(_moduleLogger)
185         def _on_nav_action(self, widget, navState):
186                 self._set_context(self._player.state)
187
188                 if navState == "clicking":
189                         if self._player.state == self._player.STATE_PLAY:
190                                 if self._active:
191                                         self._player.pause()
192                                 else:
193                                         self._player.set_piece_by_node(self._childNode)
194                                         self._player.play()
195                         elif self._player.state == self._player.STATE_PAUSE:
196                                 self._player.play()
197                         elif self._player.state == self._player.STATE_STOP:
198                                 self._player.set_piece_by_node(self._childNode)
199                                 self._player.play()
200                         else:
201                                 _moduleLogger.info("Unhandled player state %s" % self._player.state)
202                 elif navState == "down":
203                         self.window.destroy()
204                 elif navState == "up":
205                         pass
206                 elif navState == "left":
207                         self._dateShown += datetime.timedelta(days=1)
208                         self._update_title()
209                         self._refresh()
210                 elif navState == "right":
211                         self._dateShown -= datetime.timedelta(days=1)
212                         self._update_title()
213                         self._refresh()
214
215         @misc_utils.log_exception(_moduleLogger)
216         def _on_channels(self, channels):
217                 if self._isDestroyed:
218                         _moduleLogger.info("Download complete but window destroyed")
219                         return
220
221                 channels = channels
222                 if 1 < len(channels):
223                         _moduleLogger.warning("More channels now available!")
224                 self._childNode = channels[0]
225                 self._childNode.get_programming(
226                         self._dateShown,
227                         self._on_channel,
228                         self._on_load_error,
229                 )
230
231         @misc_utils.log_exception(_moduleLogger)
232         def _on_channel(self, programs):
233                 if self._isDestroyed:
234                         _moduleLogger.info("Download complete but window destroyed")
235                         return
236
237                 self._hide_loading()
238                 for program in programs:
239                         row = program["time"], program["title"]
240                         self._programmingModel.append(row)
241
242                 currentDate = datetime.datetime.now()
243                 if currentDate.date() != self._dateShown.date():
244                         self._treeView.get_selection().set_mode(gtk.SELECTION_NONE)
245                 else:
246                         self._treeView.get_selection().set_mode(gtk.SELECTION_SINGLE)
247                         path = (self._get_current_row(), )
248                         self._treeView.scroll_to_cell(path)
249                         self._treeView.get_selection().select_path(path)
250
251         @misc_utils.log_exception(_moduleLogger)
252         def _on_load_error(self, exception):
253                 self._hide_loading()
254                 self._errorBanner.push_message(str(exception))
255
256         @misc_utils.log_exception(_moduleLogger)
257         def _on_row_changed(self, selection):
258                 if len(self._programmingModel) == 0:
259                         return
260
261                 rowIndex = self._get_current_row()
262                 path = (rowIndex, )
263                 if not selection.path_is_selected(path):
264                         # Undo the user's changing of the selection
265                         selection.select_path(path)
266
267
268 gobject.type_register(RadioWindow)