[banshee] [Audiobook] Keep a last-played bookmark per book



commit 3b96bf0ca80e67b9d16e6f9e6a6b2cc23787902f
Author: Gabriel Burt <gabriel burt gmail com>
Date:   Fri May 14 20:23:02 2010 -0700

    [Audiobook] Keep a last-played bookmark per book
    
    For now show the bookmarks of a book in the BookView in a label.  The
    last-played bookmark is currently updated every few seconds, though that
    label isn't updated in real-time.

 .../Banshee.Audiobook/AudiobookLibrarySource.cs    |   72 +++++++++++++++++++-
 .../Banshee.Audiobook/BookView.cs                  |   36 +++++++++--
 2 files changed, 102 insertions(+), 6 deletions(-)
---
diff --git a/src/Extensions/Banshee.Audiobook/Banshee.Audiobook/AudiobookLibrarySource.cs b/src/Extensions/Banshee.Audiobook/Banshee.Audiobook/AudiobookLibrarySource.cs
index 63c820e..2e36578 100644
--- a/src/Extensions/Banshee.Audiobook/Banshee.Audiobook/AudiobookLibrarySource.cs
+++ b/src/Extensions/Banshee.Audiobook/Banshee.Audiobook/AudiobookLibrarySource.cs
@@ -37,6 +37,7 @@ using Banshee.Library;
 using Banshee.Collection;
 using Banshee.SmartPlaylist;
 using Banshee.Collection.Database;
+using Banshee.MediaEngine;
 using Banshee.Sources;
 using Banshee.Database;
 using Banshee.ServiceStack;
@@ -47,6 +48,8 @@ namespace Banshee.Audiobook
 {
     public class AudiobookLibrarySource : LibrarySource
     {
+        internal const string LAST_PLAYED_BOOKMARK = "audiobook-lastplayed";
+
         AudiobookModel books_model;
         LazyLoadSourceContents<AudiobookContent> grid_view;
         LazyLoadSourceContents<BookView> book_view;
@@ -99,12 +102,17 @@ namespace Banshee.Audiobook
                     MergeBooksAddedSince (DateTime.Now - TimeSpan.FromHours (2));
                 }
             };
+
+            // Listen for playback changes and auto-set the last-played bookmark
+            ServiceManager.PlayerEngine.ConnectEvent (OnPlayerEvent,
+                PlayerEvent.StartOfStream | PlayerEvent.EndOfStream | PlayerEvent.Seek,
+                true);
         }
 
         public void SwitchToBookView (DatabaseAlbumInfo book)
         {
             Properties.Set<ISourceContents> ("Nereid.SourceContents", book_view);
-            book_view.SetSource (null);
+            book_view.SetSource (this);
             book_view.Contents.SetBook (book);
         }
 
@@ -123,6 +131,66 @@ namespace Banshee.Audiobook
             //}
         }
 
+        private DatabaseTrackInfo book_track;
+        private void OnPlayerEvent (PlayerEventArgs args)
+        {
+            book_track = ServiceManager.PlayerEngine.CurrentTrack as DatabaseTrackInfo;
+            if (book_track == null || book_track.PrimarySourceId != this.DbId) {
+                book_track = null;
+            }
+
+            switch (args.Event) {
+                case PlayerEvent.StartOfStream:
+                    if (book_track != null) {
+                        StartTimeout ();
+                    }
+                    break;
+                case PlayerEvent.EndOfStream:
+                    StopTimeout ();
+                    break;
+                case PlayerEvent.Seek:
+                    UpdateLastPlayed ();
+                    break;
+            }
+        }
+
+        private uint timeout_id;
+        private void StartTimeout ()
+        {
+            if (timeout_id == 0) {
+                timeout_id = Application.RunTimeout (5000, delegate { UpdateLastPlayed (); return true; });
+            }
+        }
+
+        private void StopTimeout ()
+        {
+            if (timeout_id != 0) {
+                Application.IdleTimeoutRemove (timeout_id);
+                timeout_id = 0;
+            }
+        }
+
+        private Bookmark bookmark;
+        private void UpdateLastPlayed ()
+        {
+            if (book_track != null) {
+                // Find and remove the last bookmark for this book
+                if (bookmark == null) {
+                    bookmark = Bookmark.Provider.FetchFirstMatching (
+                        "Type = ? AND TrackID IN (SELECT TrackID FROM CoreTracks WHERE PrimarySourceID = ? AND AlbumID = ?)",
+                        LAST_PLAYED_BOOKMARK, this.DbId, book_track.AlbumId
+                    );
+                }
+
+                if (bookmark != null) {
+                    bookmark.Remove ();
+                }
+
+                // Insert the new one
+                bookmark = new Bookmark (book_track, (int)ServiceManager.PlayerEngine.Position, LAST_PLAYED_BOOKMARK);
+            }
+        }
+
         public override void Dispose ()
         {
             if (Actions != null) {
@@ -130,6 +198,8 @@ namespace Banshee.Audiobook
                 Actions = null;
             }
 
+            ServiceManager.PlayerEngine.DisconnectEvent (OnPlayerEvent);
+
             base.Dispose ();
         }
 
diff --git a/src/Extensions/Banshee.Audiobook/Banshee.Audiobook/BookView.cs b/src/Extensions/Banshee.Audiobook/Banshee.Audiobook/BookView.cs
index 8ab87e2..c2d5734 100644
--- a/src/Extensions/Banshee.Audiobook/Banshee.Audiobook/BookView.cs
+++ b/src/Extensions/Banshee.Audiobook/Banshee.Audiobook/BookView.cs
@@ -65,8 +65,10 @@ namespace Banshee.Audiobook
         AudiobookLibrarySource library;
 
         Alignment align;
-        Label title;
+        Label title_label;
+        Label bookmarks_label;
         BookCover cover;
+        RatingEntry rating_entry;
         BaseTrackListView track_list;
 
         public BookView ()
@@ -79,7 +81,7 @@ namespace Banshee.Audiobook
         {
             ThreadAssist.AssertInMainThread ();
 
-            title.Markup = String.Format (
+            title_label.Markup = String.Format (
                 "<span size=\"x-large\" weight=\"bold\">{0}</span>\n" +
                 "{1}",
                 GLib.Markup.EscapeText (book.Title),
@@ -90,6 +92,20 @@ namespace Banshee.Audiobook
                 TrackMediaAttributes.AudioStream | TrackMediaAttributes.AudioBook,
                 CoverArtSpec.CreateArtistAlbumId (book.ArtistName, book.Title)
             );
+
+            var bookmarks = Bookmark.Provider.FetchAllMatching (
+                "TrackID IN (SELECT TrackID FROM CoreTracks WHERE PrimarySourceID = ? AND AlbumID = ?)",
+                library.DbId, book.DbId
+            );
+
+            bookmarks_label.Text = "";
+            foreach (var bookmark in bookmarks) {
+                bookmarks_label.Text += String.Format ("{0}{1}\n", bookmark.Type == AudiobookLibrarySource.LAST_PLAYED_BOOKMARK ? "* " : "", bookmark.Name);
+            }
+
+            rating_entry.Value = (int) Math.Round (ServiceManager.DbConnection.Query<double> (
+                "SELECT AVG(RATING) FROM CoreTracks WHERE PrimarySourceID = ? AND AlbumID = ?", library.DbId, book.DbId
+            ));
         }
 
         public override void Dispose ()
@@ -143,14 +159,23 @@ namespace Banshee.Audiobook
             var editable_cover = TrackInfoDisplay.GetEditable (cover);
 
             // Title
-            title = new Label () {
+            title_label = new Label () {
                 Xalign = 0,
                 Yalign = 0
             };
 
+            // FIXME the left padding on this is not right
+            rating_entry = new RatingEntry () {
+                AlwaysShowEmptyStars = true,
+                HasFrame = false
+            };
+            var rating = new HBox ();
+            rating.PackStart (rating_entry, false, false, 0);
+
             // Packing
             left_box.PackStart (editable_cover, false, false,  0);
-            left_box.PackStart (title, true, true,  0);
+            left_box.PackStart (title_label, false, false,  0);
+            //left_box.PackStart (rating, false, false,  0);
 
             hbox.PackStart (left_box, false, false, 0);
 
@@ -159,7 +184,8 @@ namespace Banshee.Audiobook
 
             var notebook = new Notebook () { WidthRequest = 450, HeightRequest = 600 };
             notebook.ShowBorder = false;
-            notebook.AppendPage (new Label ("Bookmark support coming soon!"), new Label (Catalog.GetString ("Bookmarks")));
+            bookmarks_label = new Label ();
+            notebook.AppendPage (bookmarks_label, new Label (Catalog.GetString ("Bookmarks")));
 
             // Tracks
 



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]