Switching from file-based book settings to json based, making it much easier to add...
[nqaap] / src / opt / Nqa-Audiobook-player / Player.py
index 21945dc..8535a43 100644 (file)
@@ -18,7 +18,7 @@ class Player(object):
         self.storage = FileStorage.FileStorage(path = constants._data_path_)
         if hildonize.IS_HILDON_SUPPORTED and not hildonize.IS_FREMANTLE_SUPPORTED:
             import SimpleOSSOPlayer as _SimplePlayer
-            SimpleGStreamer = _SimplePlayer # silence PyFlakes
+            SimplePlayer = _SimplePlayer # silence PyFlakes
         else:
             import SimpleGStreamer as SimplePlayer
         self.player = SimplePlayer.SimplePlayer(self.next_chapter)
@@ -26,16 +26,20 @@ class Player(object):
         self.audiobook = None
         self._bookDir = None
         self._bookPaths = {}
-        self.load_books_path(constants._default_book_path_)
 
     def get_books_path(self):
         return self._bookDir
 
-    def load_books_path(self, booksPath):
+    def reload(self, booksPath):
         if self.audiobook is not None:
             position = self.player.elapsed()
             self.storage.set_time(self.audiobook.current_chapter, position)
+        self.save()
+        self.load(booksPath)
 
+    def load(self, booksPath):
+        _moduleLogger.info("Loading books from %s" % booksPath)
+        self.storage.load()
         self._bookDir = booksPath
 
         self._bookPaths = dict(
@@ -54,9 +58,18 @@ class Player(object):
                 bookPath = self._bookPaths[lastBookName]
                 self.set_book(bookPath)
             except KeyError:
-                _moduleLogger.info("Audiobook was not found")
+                _moduleLogger.exception("Audiobook was not found")
+            except IndexError:
+                _moduleLogger.exception("Chapter was not found")
             except IOError:
-                _moduleLogger.info("Audiobook could not be loaded")
+                _moduleLogger.exception("Audiobook could not be loaded")
+            except Exception:
+                _moduleLogger.exception("Can you say 'confusion'?")
+
+    def save(self):
+        position = self.player.elapsed()
+        self.storage.set_time(self.audiobook.current_chapter, position)
+        self.storage.save()
 
     @staticmethod
     def __format_name(path):
@@ -86,7 +99,7 @@ class Player(object):
             self.ui.set_book(bookPath, self.audiobook.get_cover_img())
             self.ui.set_chapters(self.audiobook.chapters)
 
-        chapter_title = self.audiobook.chapters[chapter_num]
+        chapter_title = self.audiobook.chapters[self.audiobook.current_chapter]
         self.set_chapter(chapter_title, True)
 
     def set_chapter(self, chapter, continuing = False):