Adding todos
[nqaap] / src / opt / Nqa-Audiobook-player / Gui.py
index 3fb6d35..9eecb2c 100644 (file)
@@ -24,6 +24,14 @@ _moduleLogger = logging.getLogger(__name__)
 
 class Gui(object):
 
+    # @todo Jump straight to book selection on first launch?
+    # @todo Absolute increments (+/-5 seconds)
+    # @todo Show elapsed time / time lef
+    # @todo Volume control when screen is off
+    # @todo Variable speed
+    #  http://scaletempo.sourceforge.net/0/task-list.html
+    #  http://www.gstreamer.net/data/doc/gstreamer/head/gst-plugins-bad-plugins/html/gst-plugins-bad-plugins-scaletempo.html
+
     def __init__(self):
         _moduleLogger.info("Starting GUI")
         self._clipboard = gtk.clipboard_get()
@@ -42,22 +50,17 @@ class Gui(object):
 
         self.controller = None
         self.sleep_timer = None
-        self.auto_chapter_selected = False # true if we are in the
-                                           # midle of an automatic
-                                           # chapter change
 
-        self.ignore_next_chapter_change = False
         # set up gui
         self.setup()
         self._callMonitor.connect("call_start", self.__on_call_started)
         self._callMonitor.start()
 
     def setup(self):
-        if hildonize.IS_FREMANTLE_SUPPORTED:
-            gtk.set_application_name(constants.__pretty_app_name__) # window title
         self._app = hildonize.get_app_class()()
         self.win = gtk.Window()
         self.win = hildonize.hildonize_window(self._app, self.win)
+        self.win.set_title(constants.__pretty_app_name__)
 
         # Cover image
         self.cover = gtk.Image()
@@ -65,8 +68,12 @@ class Gui(object):
         # Controls:
 
         # Label that hold the title of the book,and maybe the chapter
-        self.title = gtk.Label("Select a book to start listening")
+        self.title = gtk.Label()
         self.title.set_justify(gtk.JUSTIFY_CENTER)
+        self._set_display_title("Select a book to start listening")
+
+        self.chapter = gtk.Label()
+        self.chapter.set_justify(gtk.JUSTIFY_CENTER)
 
         # Seekbar 
         if hildonize.IS_FREMANTLE_SUPPORTED:
@@ -79,23 +86,24 @@ class Gui(object):
         else:
             adjustment = gtk.Adjustment(0, 0, 101, 1, 5, 1)
             self.seek = gtk.HScale(adjustment)
+            self.seek.set_draw_value(False)
             self.seek.connect('change-value', self.seek_changed) # event
 
         # Pause button
         if hildonize.IS_FREMANTLE_SUPPORTED:
             self.backButton = hildon.Button(gtk.HILDON_SIZE_AUTO_WIDTH | gtk.HILDON_SIZE_FINGER_HEIGHT, hildon.BUTTON_ARRANGEMENT_VERTICAL)
-            image = gtk.image_new_from_stock(gtk.STOCK_GO_BACK, gtk.HILDON_SIZE_FINGER_HEIGHT)
+            image = gtk.image_new_from_stock(gtk.STOCK_MEDIA_PREVIOUS, gtk.HILDON_SIZE_FINGER_HEIGHT)
             self.backButton.set_image(image)
 
             self.button = hildon.Button(gtk.HILDON_SIZE_AUTO_WIDTH | gtk.HILDON_SIZE_FINGER_HEIGHT, hildon.BUTTON_ARRANGEMENT_VERTICAL)
 
             self.forwardButton = hildon.Button(gtk.HILDON_SIZE_AUTO_WIDTH | gtk.HILDON_SIZE_FINGER_HEIGHT, hildon.BUTTON_ARRANGEMENT_VERTICAL)
-            image = gtk.image_new_from_stock(gtk.STOCK_GO_FORWARD, gtk.HILDON_SIZE_FINGER_HEIGHT)
+            image = gtk.image_new_from_stock(gtk.STOCK_MEDIA_NEXT, gtk.HILDON_SIZE_FINGER_HEIGHT)
             self.forwardButton.set_image(image)
         else:
-            self.backButton = gtk.Button(stock=gtk.STOCK_GO_BACK)
+            self.backButton = gtk.Button(stock=gtk.STOCK_MEDIA_PREVIOUS)
             self.button = gtk.Button()
-            self.forwardButton = gtk.Button(stock=gtk.STOCK_GO_FORWARD)
+            self.forwardButton = gtk.Button(stock=gtk.STOCK_MEDIA_NEXT)
         self.set_button_text("Play", "Start playing the audiobook")
         self.backButton.connect('clicked', self._on_previous_chapter)
         self.button.connect('clicked', self.play_pressed) # event
@@ -108,8 +116,11 @@ class Gui(object):
 
         # Box to hold the controls:
         self._controlLayout = gtk.VBox()
-        self._controlLayout.pack_start(self.title, True, True, 0)
-        self._controlLayout.pack_start(self.seek, True, True, 0)
+        self._controlLayout.pack_start(gtk.Label(), True, True, 0)
+        self._controlLayout.pack_start(self.title, False, True, 0)
+        self._controlLayout.pack_start(self.chapter, False, True, 0)
+        self._controlLayout.pack_start(gtk.Label(), True, True, 0)
+        self._controlLayout.pack_start(self.seek, False, True, 0)
         self._controlLayout.pack_start(self._toolbar, False, True, 0)
 
         #Box that divides the layout in two: cover on the lefta
@@ -169,6 +180,10 @@ class Gui(object):
             settings_button.set_label("Settings")
             settings_button.connect("clicked", self._on_settings)
 
+            about_button = hildon.Button(gtk.HILDON_SIZE_AUTO, hildon.BUTTON_ARRANGEMENT_VERTICAL)
+            about_button.set_label("About")
+            about_button.connect("clicked", self._on_about_activate)
+
             help_button = hildon.Button(gtk.HILDON_SIZE_AUTO, hildon.BUTTON_ARRANGEMENT_VERTICAL)
             help_button.set_label("Help")
             help_button.connect("clicked", self.get_help)
@@ -178,6 +193,7 @@ class Gui(object):
             self._menuBar.append(self.sleeptime_button)        # Add the button to menu
             self._menuBar.append(settings_button)
             self._menuBar.append(help_button)
+            self._menuBar.append(about_button)
             self._menuBar.show_all()
         else:
             self._audiobookMenuItem = gtk.MenuItem("Audiobook: ")
@@ -192,6 +208,9 @@ class Gui(object):
             settingsMenuItem = gtk.MenuItem("Settings")
             settingsMenuItem.connect("activate", self._on_settings)
 
+            aboutMenuItem = gtk.MenuItem("About")
+            aboutMenuItem.connect("activate", self._on_about_activate)
+
             helpMenuItem = gtk.MenuItem("Help")
             helpMenuItem.connect("activate", self.get_help)
 
@@ -201,6 +220,7 @@ class Gui(object):
             booksMenu.append(self._sleepMenuItem)
             booksMenu.append(settingsMenuItem)
             booksMenu.append(helpMenuItem)
+            booksMenu.append(aboutMenuItem)
 
             booksMenuItem = gtk.MenuItem("Books")
             booksMenuItem.show()
@@ -216,6 +236,7 @@ class Gui(object):
         self._save_settings(config)
         with open(constants._user_settings_, "wb") as configFile:
             config.write(configFile)
+        self.controller.save()
 
     def _save_settings(self, config):
         config.add_section(constants.__pretty_app_name__)
@@ -257,7 +278,7 @@ class Gui(object):
         else:
             self.win.unfullscreen()
 
-        self.controller.load_books_path(booksPath)
+        self.controller.load(booksPath)
 
     @staticmethod
     def __format_name(path):
@@ -279,6 +300,7 @@ class Gui(object):
         self._bookSelectionIndex = index
         bookName = self._bookSelection[index]
         self.controller.set_book(bookName)
+        self.set_button_text("Play", "Start playing the audiobook") # reset button
 
     @gtk_toolbox.log_exception(_moduleLogger)
     def _on_select_chapter(self, *args):
@@ -293,6 +315,7 @@ class Gui(object):
         self._chapterSelectionIndex = index
         chapterName = self._chapterSelection[index]
         self.controller.set_chapter(chapterName)
+        self.set_button_text("Play", "Start playing the audiobook") # reset button
 
     @gtk_toolbox.log_exception(_moduleLogger)
     def _on_select_sleep(self, *args):
@@ -361,7 +384,7 @@ class Gui(object):
                 orientation = gtk.ORIENTATION_HORIZONTAL
             self.set_orientation(orientation)
         if self.__settingsManager.get_audiobook_path() != self.controller.get_books_path():
-            self.controller.load_books_path(self.__settingsManager.get_audiobook_path())
+            self.controller.reload(self.__settingsManager.get_audiobook_path())
 
         return True
 
@@ -464,6 +487,18 @@ class Gui(object):
         finally:
             gtk.main_quit()
 
+    @gtk_toolbox.log_exception(_moduleLogger)
+    def _on_about_activate(self, *args):
+        dlg = gtk.AboutDialog()
+        dlg.set_name(constants.__pretty_app_name__)
+        dlg.set_version("%s-%d" % (constants.__version__, constants.__build__))
+        dlg.set_copyright("Copyright 2010")
+        dlg.set_comments("")
+        dlg.set_website("http://nqaap.garage.maemo.org/")
+        dlg.set_authors(["Pengman <pengmeister@gmail.com>", "Ed Page <eopage@byu.net>"])
+        dlg.run()
+        dlg.destroy()
+
     # Actions:  
 
     def play(self):
@@ -506,30 +541,15 @@ class Gui(object):
     def change_chapter(self, chapterName):
         if chapterName is None:
             _moduleLogger.debug("chapter selection canceled.")
-            #import pdb; pdb.set_trace()     # start debugger
-            self.ignore_next_chapter_change = True
             return True                   # this should end the function and indicate it has been handled
 
-        if self.ignore_next_chapter_change:
-            self.ignore_next_chapter_change = False
-            _moduleLogger.debug("followup chapter selection canceled.")
-            #import pdb; pdb.set_trace()     # start debugger
-            return True                   # this should end the function and indicate it has been handled
-
-        if self.auto_chapter_selected:
-            _moduleLogger.debug("chapter changed (by controller) to: %s" % chapterName)
-            self.auto_chapter_selected = False
-            # do nothing
-        else:
-            _moduleLogger.debug("chapter selection sendt to controller: %s" % chapterName)
-            self.controller.set_chapter(chapterName) # signal controller
-            self.set_button_text("Play", "Start playing the audiobook") # reset button
+        _moduleLogger.debug("chapter changed (by controller) to: %s" % chapterName)
 
     def set_button_text(self, title, text):
         if hildonize.IS_FREMANTLE_SUPPORTED:
             self.button.set_text(title, text)
         else:
-            self.button.set_label("%s - %s" % (title, text))
+            self.button.set_label("%s" % (title, ))
 
     def set_books(self, books):
         _moduleLogger.debug("new books")
@@ -542,7 +562,7 @@ class Gui(object):
         bookName = self.__format_name(bookPath)
 
         self.set_button_text("Play", "Start playing the audiobook") # reset button
-        self.title.set_text(bookName)
+        self._set_display_title(bookName)
         if hildonize.IS_FREMANTLE_SUPPORTED:
             self.book_button.set_text("Audiobook", bookName)
         else:
@@ -557,10 +577,11 @@ class Gui(object):
         chapter parameter is supposed to be the index for the chapter, not the name
         '''
         self.auto_chapter_selected = True
+        self._set_display_chapter(str(chapterIndex + 1))
         if hildonize.IS_FREMANTLE_SUPPORTED:
             self.chapter_button.set_text("Chapter", str(chapterIndex))
         else:
-            self._chapterMenuItem.get_child().set_text("Chapter: %s" % (chapterIndex, ))
+            self._chapterMenuItem.get_child().set_text("Chapter: %s" % (chapterIndex+1, ))
 
     def set_chapters(self, chapters):
         _moduleLogger.debug("setting chapters" )
@@ -575,6 +596,12 @@ class Gui(object):
         i = button.get_selector().get_model(0).index[value] # get index of value from list
         button.set_active(i)                                # set active index to that index
 
+    def _set_display_title(self, title):
+        self.title.set_markup("<b><big>%s</big></b>" % title)
+
+    def _set_display_chapter(self, chapter):
+        self.chapter.set_markup("<b><big>Chapter %s</big></b>" % chapter)
+
 
 def _old_timeout_add_seconds(timeout, callback):
     return gobject.timeout_add(timeout * 1000, callback)