Tweaking the UI based on feedback from Addison
[nqaap] / src / opt / Nqa-Audiobook-player / Gui.py
1 from __future__ import with_statement
2
3 import os
4 import ConfigParser
5 import logging
6
7 import gobject
8 import gtk
9
10 import constants
11 import hildonize
12 import gtk_toolbox
13 import Browser
14 import CallMonitor
15 import settings
16
17 if hildonize.IS_FREMANTLE_SUPPORTED:
18     # I don't normally do this but I want to error as loudly as possibly when an issue arises
19     import hildon
20
21
22 _moduleLogger = logging.getLogger(__name__)
23
24
25 class Gui(object):
26
27     def __init__(self):
28         _moduleLogger.info("Starting GUI")
29         self._clipboard = gtk.clipboard_get()
30         self._callMonitor = CallMonitor.CallMonitor()
31         self.__settingsWindow = None
32         self.__settingsManager = None
33         self._bookSelection = []
34         self._bookSelectionIndex = -1
35         self._chapterSelection = []
36         self._chapterSelectionIndex = -1
37         self._sleepSelection = ["0", "1", "10", "20", "30", "60"]
38         self._sleepSelectionIndex = 0
39
40         self.__window_in_fullscreen = False #The window isn't in full screen mode initially.
41         self.__isPortrait = False
42
43         self.controller = None
44         self.sleep_timer = None
45         self.auto_chapter_selected = False # true if we are in the
46                                            # midle of an automatic
47                                            # chapter change
48
49         self.ignore_next_chapter_change = False
50         # set up gui
51         self.setup()
52         self._callMonitor.connect("call_start", self.__on_call_started)
53         self._callMonitor.start()
54
55     def setup(self):
56         self._app = hildonize.get_app_class()()
57         self.win = gtk.Window()
58         self.win = hildonize.hildonize_window(self._app, self.win)
59         self.win.set_title(constants.__pretty_app_name__)
60
61         # Cover image
62         self.cover = gtk.Image()
63
64         # Controls:
65
66         # Label that hold the title of the book,and maybe the chapter
67         self.title = gtk.Label()
68         self.title.set_justify(gtk.JUSTIFY_CENTER)
69         self._set_display_title("Select a book to start listening")
70
71         self.chapter = gtk.Label()
72         self.chapter.set_justify(gtk.JUSTIFY_CENTER)
73
74         # Seekbar 
75         if hildonize.IS_FREMANTLE_SUPPORTED:
76             self.seek = hildon.Seekbar()
77             self.seek.set_range(0.0, 100)
78             self.seek.set_draw_value(False)
79             self.seek.set_update_policy(gtk.UPDATE_DISCONTINUOUS)
80             self.seek.connect('change-value', self.seek_changed) # event
81             # self.seek.connect('value-changed',self.seek_changed) # event
82         else:
83             adjustment = gtk.Adjustment(0, 0, 101, 1, 5, 1)
84             self.seek = gtk.HScale(adjustment)
85             self.seek.set_draw_value(False)
86             self.seek.connect('change-value', self.seek_changed) # event
87
88         # Pause button
89         if hildonize.IS_FREMANTLE_SUPPORTED:
90             self.backButton = hildon.Button(gtk.HILDON_SIZE_AUTO_WIDTH | gtk.HILDON_SIZE_FINGER_HEIGHT, hildon.BUTTON_ARRANGEMENT_VERTICAL)
91             image = gtk.image_new_from_stock(gtk.STOCK_MEDIA_PREVIOUS, gtk.HILDON_SIZE_FINGER_HEIGHT)
92             self.backButton.set_image(image)
93
94             self.button = hildon.Button(gtk.HILDON_SIZE_AUTO_WIDTH | gtk.HILDON_SIZE_FINGER_HEIGHT, hildon.BUTTON_ARRANGEMENT_VERTICAL)
95
96             self.forwardButton = hildon.Button(gtk.HILDON_SIZE_AUTO_WIDTH | gtk.HILDON_SIZE_FINGER_HEIGHT, hildon.BUTTON_ARRANGEMENT_VERTICAL)
97             image = gtk.image_new_from_stock(gtk.STOCK_MEDIA_NEXT, gtk.HILDON_SIZE_FINGER_HEIGHT)
98             self.forwardButton.set_image(image)
99         else:
100             self.backButton = gtk.Button(stock=gtk.STOCK_MEDIA_PREVIOUS)
101             self.button = gtk.Button()
102             self.forwardButton = gtk.Button(stock=gtk.STOCK_MEDIA_NEXT)
103         self.set_button_text("Play", "Start playing the audiobook")
104         self.backButton.connect('clicked', self._on_previous_chapter)
105         self.button.connect('clicked', self.play_pressed) # event
106         self.forwardButton.connect('clicked', self._on_next_chapter)
107
108         self._toolbar = gtk.HBox()
109         self._toolbar.pack_start(self.backButton, False, False, 0)
110         self._toolbar.pack_start(self.button, True, True, 0)
111         self._toolbar.pack_start(self.forwardButton, False, False, 0)
112
113         # Box to hold the controls:
114         self._controlLayout = gtk.VBox()
115         self._controlLayout.pack_start(gtk.Label(), True, True, 0)
116         self._controlLayout.pack_start(self.title, False, True, 0)
117         self._controlLayout.pack_start(self.chapter, False, True, 0)
118         self._controlLayout.pack_start(gtk.Label(), True, True, 0)
119         self._controlLayout.pack_start(self.seek, False, True, 0)
120         self._controlLayout.pack_start(self._toolbar, False, True, 0)
121
122         #Box that divides the layout in two: cover on the lefta
123         #and controls on the right
124         self._viewLayout = gtk.HBox()
125         self._viewLayout.pack_start(self.cover, True, True, 0)
126         self._viewLayout.add(self._controlLayout)
127
128         self._menuBar = gtk.MenuBar()
129         self._menuBar.show()
130
131         self._mainLayout = gtk.VBox()
132         self._mainLayout.pack_start(self._menuBar, False, False, 0)
133         self._mainLayout.pack_start(self._viewLayout)
134
135         # Add hbox to the window
136         self.win.add(self._mainLayout)
137
138         #Menu:
139         # Create menu
140         self._populate_menu()
141
142         self.win.connect("delete_event", self.quit) # Add shutdown event
143         self.win.connect("key-press-event", self.on_key_press)
144         self.win.connect("window-state-event", self._on_window_state_change)
145
146         self.win.show_all()
147
148         # Run update timer
149         self.setup_timers()
150
151     def _populate_menu(self):
152         self._menuBar = hildonize.hildonize_menu(
153             self.win,
154             self._menuBar,
155         )
156         if hildonize.IS_FREMANTLE_SUPPORTED:
157             # Create a picker button 
158             self.book_button = hildon.Button(gtk.HILDON_SIZE_AUTO,
159                                               hildon.BUTTON_ARRANGEMENT_VERTICAL)
160             self.book_button.set_title("Audiobook") # Set a title to the button 
161             self.book_button.connect("clicked", self._on_select_audiobook)
162
163             # Create a picker button 
164             self.chapter_button = hildon.Button(gtk.HILDON_SIZE_AUTO,
165                                               hildon.BUTTON_ARRANGEMENT_VERTICAL)
166             self.chapter_button.set_title("Chapter") # Set a title to the button 
167             self.chapter_button.connect("clicked", self._on_select_chapter)
168
169             # Create a picker button 
170             self.sleeptime_button = hildon.Button(gtk.HILDON_SIZE_AUTO,
171                                               hildon.BUTTON_ARRANGEMENT_VERTICAL)
172             self.sleeptime_button.set_title("Sleeptimer") # Set a title to the button 
173             self.sleeptime_button.connect("clicked", self._on_select_sleep)
174
175             settings_button = hildon.Button(gtk.HILDON_SIZE_AUTO, hildon.BUTTON_ARRANGEMENT_VERTICAL)
176             settings_button.set_label("Settings")
177             settings_button.connect("clicked", self._on_settings)
178
179             help_button = hildon.Button(gtk.HILDON_SIZE_AUTO, hildon.BUTTON_ARRANGEMENT_VERTICAL)
180             help_button.set_label("Help")
181             help_button.connect("clicked", self.get_help)
182
183             self._menuBar.append(self.book_button)        # Add the button to menu
184             self._menuBar.append(self.chapter_button)        # Add the button to menu
185             self._menuBar.append(self.sleeptime_button)        # Add the button to menu
186             self._menuBar.append(settings_button)
187             self._menuBar.append(help_button)
188             self._menuBar.show_all()
189         else:
190             self._audiobookMenuItem = gtk.MenuItem("Audiobook: ")
191             self._audiobookMenuItem.connect("activate", self._on_select_audiobook)
192
193             self._chapterMenuItem = gtk.MenuItem("Chapter: ")
194             self._chapterMenuItem.connect("activate", self._on_select_chapter)
195
196             self._sleepMenuItem = gtk.MenuItem("Sleeptimer: 0")
197             self._sleepMenuItem.connect("activate", self._on_select_sleep)
198
199             settingsMenuItem = gtk.MenuItem("Settings")
200             settingsMenuItem.connect("activate", self._on_settings)
201
202             helpMenuItem = gtk.MenuItem("Help")
203             helpMenuItem.connect("activate", self.get_help)
204
205             booksMenu = gtk.Menu()
206             booksMenu.append(self._audiobookMenuItem)
207             booksMenu.append(self._chapterMenuItem)
208             booksMenu.append(self._sleepMenuItem)
209             booksMenu.append(settingsMenuItem)
210             booksMenu.append(helpMenuItem)
211
212             booksMenuItem = gtk.MenuItem("Books")
213             booksMenuItem.show()
214             booksMenuItem.set_submenu(booksMenu)
215             self._menuBar.append(booksMenuItem)
216             self._menuBar.show_all()
217
218     def setup_timers(self):
219         self.seek_timer = timeout_add_seconds(3, self.update_seek)
220
221     def save_settings(self):
222         config = ConfigParser.SafeConfigParser()
223         self._save_settings(config)
224         with open(constants._user_settings_, "wb") as configFile:
225             config.write(configFile)
226
227     def _save_settings(self, config):
228         config.add_section(constants.__pretty_app_name__)
229         config.set(constants.__pretty_app_name__, "portrait", str(self.__isPortrait))
230         config.set(constants.__pretty_app_name__, "fullscreen", str(self.__window_in_fullscreen))
231         config.set(constants.__pretty_app_name__, "audiopath", self.controller.get_books_path())
232
233     def load_settings(self):
234         config = ConfigParser.SafeConfigParser()
235         config.read(constants._user_settings_)
236         self._load_settings(config)
237
238     def _load_settings(self, config):
239         isPortrait = False
240         window_in_fullscreen = False
241         booksPath = constants._default_book_path_
242         try:
243             isPortrait = config.getboolean(constants.__pretty_app_name__, "portrait")
244             window_in_fullscreen = config.getboolean(constants.__pretty_app_name__, "fullscreen")
245             booksPath = config.get(constants.__pretty_app_name__, "audiopath")
246         except ConfigParser.NoSectionError, e:
247             _moduleLogger.info(
248                 "Settings file %s is missing section %s" % (
249                     constants._user_settings_,
250                     e.section,
251                 )
252             )
253
254         if isPortrait ^ self.__isPortrait:
255             if isPortrait:
256                 orientation = gtk.ORIENTATION_VERTICAL
257             else:
258                 orientation = gtk.ORIENTATION_HORIZONTAL
259             self.set_orientation(orientation)
260
261         self.__window_in_fullscreen = window_in_fullscreen
262         if self.__window_in_fullscreen:
263             self.win.fullscreen()
264         else:
265             self.win.unfullscreen()
266
267         self.controller.load_books_path(booksPath)
268
269     @staticmethod
270     def __format_name(path):
271         if os.path.isfile(path):
272             return os.path.basename(path).rsplit(".", 1)[0]
273         else:
274             return os.path.basename(path)
275
276     @gtk_toolbox.log_exception(_moduleLogger)
277     def _on_select_audiobook(self, *args):
278         if not self._bookSelection:
279             return
280         index = hildonize.touch_selector(
281             self.win,
282             "Audiobook",
283             (self.__format_name(bookPath) for bookPath in self._bookSelection),
284             self._bookSelectionIndex if 0 <= self._bookSelectionIndex else 0,
285         )
286         self._bookSelectionIndex = index
287         bookName = self._bookSelection[index]
288         self.controller.set_book(bookName)
289
290     @gtk_toolbox.log_exception(_moduleLogger)
291     def _on_select_chapter(self, *args):
292         if not self._chapterSelection:
293             return
294         index = hildonize.touch_selector(
295             self.win,
296             "Chapter",
297             (self.__format_name(chapterPath) for chapterPath in self._chapterSelection),
298             self._chapterSelectionIndex if 0 <= self._chapterSelectionIndex else 0,
299         )
300         self._chapterSelectionIndex = index
301         chapterName = self._chapterSelection[index]
302         self.controller.set_chapter(chapterName)
303
304     @gtk_toolbox.log_exception(_moduleLogger)
305     def _on_select_sleep(self, *args):
306         if self.sleep_timer is not None:
307             gobject.source_remove(self.sleep_timer)
308
309         try:
310             index = hildonize.touch_selector(
311                 self.win,
312                 "Sleeptimer",
313                 self._sleepSelection,
314                 self._sleepSelectionIndex if 0 <= self._sleepSelectionIndex else 0,
315             )
316         except RuntimeError:
317             _moduleLogger.exception("Handling as if user cancelled")
318             hildonize.show_information_banner(self.win, "Sleep timer canceled")
319             index = 0
320
321         self._sleepSelectionIndex = index
322         sleepName = self._sleepSelection[index]
323
324         time_out = int(sleepName)
325         if 0 < time_out:
326             timeout_add_seconds(time_out * 60, self.sleep)
327
328         if hildonize.IS_FREMANTLE_SUPPORTED:
329             self.sleeptime_button.set_text("Sleeptimer", sleepName)
330         else:
331             self._sleepMenuItem.get_child().set_text("Sleeptimer: %s" % (sleepName, ))
332
333     @gtk_toolbox.log_exception(_moduleLogger)
334     def __on_call_started(self, callMonitor):
335         self.pause()
336
337     @gtk_toolbox.log_exception(_moduleLogger)
338     def _on_settings(self, *args):
339         if self.__settingsWindow is None:
340             vbox = gtk.VBox()
341             self.__settingsManager = settings.SettingsDialog(vbox)
342
343             self.__settingsWindow = gtk.Window()
344             self.__settingsWindow.add(vbox)
345             self.__settingsWindow = hildonize.hildonize_window(self._app, self.__settingsWindow)
346             self.__settingsManager.window = self.__settingsWindow
347
348             self.__settingsWindow.set_title("Settings")
349             self.__settingsWindow.set_transient_for(self.win)
350             self.__settingsWindow.set_default_size(*self.win.get_size())
351             self.__settingsWindow.connect("delete-event", self._on_settings_delete)
352         self.__settingsManager.set_portrait_state(self.__isPortrait)
353         self.__settingsManager.set_audiobook_path(self.controller.get_books_path())
354         self.__settingsWindow.set_modal(True)
355         self.__settingsWindow.show_all()
356
357     @gtk_toolbox.log_exception(_moduleLogger)
358     def _on_settings_delete(self, *args):
359         self.__settingsWindow.emit_stop_by_name("delete-event")
360         self.__settingsWindow.hide()
361         self.__settingsWindow.set_modal(False)
362
363         isPortrait = self.__settingsManager.is_portrait()
364         if isPortrait ^ self.__isPortrait:
365             if isPortrait:
366                 orientation = gtk.ORIENTATION_VERTICAL
367             else:
368                 orientation = gtk.ORIENTATION_HORIZONTAL
369             self.set_orientation(orientation)
370         if self.__settingsManager.get_audiobook_path() != self.controller.get_books_path():
371             self.controller.load_books_path(self.__settingsManager.get_audiobook_path())
372
373         return True
374
375     @gtk_toolbox.log_exception(_moduleLogger)
376     def update_seek(self):
377         #print self.controller.get_percentage()
378         if self.controller.is_playing():
379             gtk.gdk.threads_enter()
380             self.seek.set_value(self.controller.get_percentage() * 100)
381             gtk.gdk.threads_leave()
382         #self.controller.get_percentage() 
383         return True                     # run again
384
385     @gtk_toolbox.log_exception(_moduleLogger)
386     def sleep(self):
387         _moduleLogger.info("sleep time timeout")
388         hildonize.show_information_banner(self.win, "Sleep timer")
389         self.controller.stop()
390         self.set_button_text("Resume", "Resume playing the audiobook")
391         return False                    # do not repeat
392
393     @gtk_toolbox.log_exception(_moduleLogger)
394     def get_help(self, button):
395         Browser.open("file:///opt/Nqa-Audiobook-player/Help/nqaap.html")
396
397     @gtk_toolbox.log_exception(_moduleLogger)
398     def seek_changed(self, seek, scroll , value):
399         # print "sok", scroll
400         self.controller.seek_percent(seek.get_value() / 100.0)
401
402     @gtk_toolbox.log_exception(_moduleLogger)
403     def _on_next_chapter(self, *args):
404         self.controller.next_chapter()
405
406     @gtk_toolbox.log_exception(_moduleLogger)
407     def _on_previous_chapter(self, *args):
408         self.controller.previous_chapter()
409
410     @gtk_toolbox.log_exception(_moduleLogger)
411     def play_pressed(self, button):
412         if self.controller.is_playing():
413             self.pause()
414         else:
415             self.play()
416
417     @gtk_toolbox.log_exception(_moduleLogger)
418     def on_key_press(self, widget, event, *args):
419         RETURN_TYPES = (gtk.keysyms.Return, gtk.keysyms.ISO_Enter, gtk.keysyms.KP_Enter)
420         isCtrl = bool(event.get_state() & gtk.gdk.CONTROL_MASK)
421         if (
422             event.keyval == gtk.keysyms.F6 or
423             event.keyval in RETURN_TYPES and isCtrl
424         ):
425             # The "Full screen" hardware key has been pressed 
426             if self.__window_in_fullscreen:
427                 self.win.unfullscreen ()
428             else:
429                 self.win.fullscreen ()
430             return True
431         elif event.keyval == gtk.keysyms.o and isCtrl:
432             self._toggle_rotate()
433             return True
434         elif (
435             event.keyval in (gtk.keysyms.w, gtk.keysyms.q) and
436             event.get_state() & gtk.gdk.CONTROL_MASK
437         ):
438             self.quit()
439         elif event.keyval == gtk.keysyms.l and event.get_state() & gtk.gdk.CONTROL_MASK:
440             with open(constants._user_logpath_, "r") as f:
441                 logLines = f.xreadlines()
442                 log = "".join(logLines)
443                 self._clipboard.set_text(str(log))
444             return True
445         elif event.keyval in RETURN_TYPES:
446             if self.controller.is_playing():
447                 self.pause()
448             else:
449                 self.play()
450             return True
451         elif event.keyval == gtk.keysyms.Left:
452             self.controller.previous_chapter()
453             return True
454         elif event.keyval == gtk.keysyms.Right:
455             self.controller.next_chapter()
456             return True
457
458     @gtk_toolbox.log_exception(_moduleLogger)
459     def _on_window_state_change(self, widget, event, *args):
460         if event.new_window_state & gtk.gdk.WINDOW_STATE_FULLSCREEN:
461             self.__window_in_fullscreen = True
462         else:
463             self.__window_in_fullscreen = False
464
465     @gtk_toolbox.log_exception(_moduleLogger)
466     def quit(self, *args):             # what are the arguments?
467         _moduleLogger.info("Shutting down")
468         try:
469             self.save_settings()
470             self.controller.stop()          # to save the state
471         finally:
472             gtk.main_quit()
473
474     # Actions:  
475
476     def play(self):
477         self.set_button_text("Stop", "Stop playing the audiobook")
478         self.controller.play()
479
480     def pause(self):
481         self.set_button_text("Resume", "Resume playing the audiobook")
482         self.controller.stop()
483
484     def set_orientation(self, orientation):
485         if orientation == gtk.ORIENTATION_VERTICAL:
486             if self.__isPortrait:
487                 return
488             hildonize.window_to_portrait(self.win)
489             self.__isPortrait = True
490
491             self._viewLayout.remove(self._controlLayout)
492             self._mainLayout.add(self._controlLayout)
493         elif orientation == gtk.ORIENTATION_HORIZONTAL:
494             if not self.__isPortrait:
495                 return
496             hildonize.window_to_landscape(self.win)
497             self.__isPortrait = False
498
499             self._mainLayout.remove(self._controlLayout)
500             self._viewLayout.add(self._controlLayout)
501         else:
502             raise NotImplementedError(orientation)
503
504     def get_orientation(self):
505         return gtk.ORIENTATION_VERTICAL if self.__isPortrait else gtk.ORIENTATION_HORIZONTAL
506
507     def _toggle_rotate(self):
508         if self.__isPortrait:
509             self.set_orientation(gtk.ORIENTATION_HORIZONTAL)
510         else:
511             self.set_orientation(gtk.ORIENTATION_VERTICAL)
512
513     def change_chapter(self, chapterName):
514         if chapterName is None:
515             _moduleLogger.debug("chapter selection canceled.")
516             #import pdb; pdb.set_trace()     # start debugger
517             self.ignore_next_chapter_change = True
518             return True                   # this should end the function and indicate it has been handled
519
520         if self.ignore_next_chapter_change:
521             self.ignore_next_chapter_change = False
522             _moduleLogger.debug("followup chapter selection canceled.")
523             #import pdb; pdb.set_trace()     # start debugger
524             return True                   # this should end the function and indicate it has been handled
525
526         if self.auto_chapter_selected:
527             _moduleLogger.debug("chapter changed (by controller) to: %s" % chapterName)
528             self.auto_chapter_selected = False
529             # do nothing
530         else:
531             _moduleLogger.debug("chapter selection sendt to controller: %s" % chapterName)
532             self.controller.set_chapter(chapterName) # signal controller
533             self.set_button_text("Play", "Start playing the audiobook") # reset button
534
535     def set_button_text(self, title, text):
536         if hildonize.IS_FREMANTLE_SUPPORTED:
537             self.button.set_text(title, text)
538         else:
539             self.button.set_label("%s" % (title, ))
540
541     def set_books(self, books):
542         _moduleLogger.debug("new books")
543         del self._bookSelection[:]
544         self._bookSelection.extend(books)
545         if len(books) == 0 and self.controller is not None:
546             hildonize.show_information_banner(self.win, "No audiobooks found. \nPlease place your audiobooks in the directory %s" % self.controller.get_books_path())
547
548     def set_book(self, bookPath, cover):
549         bookName = self.__format_name(bookPath)
550
551         self.set_button_text("Play", "Start playing the audiobook") # reset button
552         self._set_display_title(bookName)
553         if hildonize.IS_FREMANTLE_SUPPORTED:
554             self.book_button.set_text("Audiobook", bookName)
555         else:
556             self._audiobookMenuItem.get_child().set_text("Audiobook: %s" % (bookName, ))
557         if cover != "":
558             self.cover.set_from_file(cover)
559
560     def set_chapter(self, chapterIndex):
561         '''
562         Called from controller whenever a new chapter is started
563
564         chapter parameter is supposed to be the index for the chapter, not the name
565         '''
566         self.auto_chapter_selected = True
567         self._set_display_chapter(str(chapterIndex + 1))
568         if hildonize.IS_FREMANTLE_SUPPORTED:
569             self.chapter_button.set_text("Chapter", str(chapterIndex))
570         else:
571             self._chapterMenuItem.get_child().set_text("Chapter: %s" % (chapterIndex, ))
572
573     def set_chapters(self, chapters):
574         _moduleLogger.debug("setting chapters" )
575         del self._chapterSelection[:]
576         self._chapterSelection.extend(chapters)
577
578     def set_sleep_timer(self, mins):
579         pass
580
581     # Utils
582     def set_selected_value(self, button, value):
583         i = button.get_selector().get_model(0).index[value] # get index of value from list
584         button.set_active(i)                                # set active index to that index
585
586     def _set_display_title(self, title):
587         self.title.set_markup("<b><big>%s</big></b>" % title)
588
589     def _set_display_chapter(self, chapter):
590         self.chapter.set_markup("<b><big>Chapter %s</big></b>" % chapter)
591
592
593 def _old_timeout_add_seconds(timeout, callback):
594     return gobject.timeout_add(timeout * 1000, callback)
595
596
597 def _timeout_add_seconds(timeout, callback):
598     return gobject.timeout_add_seconds(timeout, callback)
599
600
601 try:
602     gobject.timeout_add_seconds
603     timeout_add_seconds = _timeout_add_seconds
604 except AttributeError:
605     timeout_add_seconds = _old_timeout_add_seconds
606
607
608 if __name__ == "__main__":
609     g = Gui(None)