[banshee] [Bookmarks] Revamp to use SqliteModelProvider
- From: Gabriel Burt <gburt src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [banshee] [Bookmarks] Revamp to use SqliteModelProvider
- Date: Sat, 15 May 2010 02:20:19 +0000 (UTC)
commit 3094f6e68cc9878d12266ec0080bcc2b358d34d7
Author: Gabriel Burt <gabriel burt gmail com>
Date: Fri May 14 18:51:47 2010 -0700
[Bookmarks] Revamp to use SqliteModelProvider
.../Banshee.Bookmarks/Bookmark.cs | 142 ++++++++------------
.../Banshee.Bookmarks/BookmarkUI.cs | 111 ++++++++--------
.../Banshee.Bookmarks/BookmarksService.cs | 1 -
3 files changed, 109 insertions(+), 145 deletions(-)
---
diff --git a/src/Extensions/Banshee.Bookmarks/Banshee.Bookmarks/Bookmark.cs b/src/Extensions/Banshee.Bookmarks/Banshee.Bookmarks/Bookmark.cs
index 6e6b6fe..c63c22f 100644
--- a/src/Extensions/Banshee.Bookmarks/Banshee.Bookmarks/Bookmark.cs
+++ b/src/Extensions/Banshee.Bookmarks/Banshee.Bookmarks/Bookmark.cs
@@ -28,7 +28,9 @@
using System;
using System.Data;
+using System.Linq;
using System.Collections.Generic;
+
using Gtk;
using Mono.Unix;
@@ -46,73 +48,70 @@ namespace Banshee.Bookmarks
{
public class Bookmark
{
- private int id;
- private int track_id;
- private uint position;
- private DateTime created_at;
+ private static SqliteModelProvider<Bookmark> provider;
+ public static SqliteModelProvider<Bookmark> Provider {
+ get {
+ return provider ?? (provider = new SqliteModelProvider<Bookmark> (ServiceManager.DbConnection, "Bookmarks", true));
+ }
+ }
- // Translators: This is used to generate bookmark names. {0} is track title, {1} is minutes
- // (possibly more than two digits) and {2} is seconds (between 00 and 60).
- private readonly string bookmark_format = Catalog.GetString("{0} ({1}:{2:00})");
+ [DatabaseColumn (Constraints = DatabaseColumnConstraints.PrimaryKey)]
+ public long BookmarkId { get; private set; }
- private string name;
- public string Name {
- get { return name; }
- set { name = value; }
+ [DatabaseColumn]
+ protected int TrackId {
+ get { return Track.TrackId; }
+ set {
+ Track = DatabaseTrackInfo.Provider.FetchSingle (value);
+ }
}
- public DateTime CreatedAt {
- get { return created_at; }
- }
+ [DatabaseColumn]
+ public TimeSpan Position { get; private set; }
- public TrackInfo Track {
- get { return DatabaseTrackInfo.Provider.FetchSingle(track_id); }
- }
+ [DatabaseColumn]
+ public DateTime CreatedAt { get; private set; }
- private Bookmark(int id, int track_id, uint position, DateTime created_at)
- {
- this.id = id;
- this.track_id = track_id;
- this.position = position;
- this.created_at = created_at;
- uint position_seconds = position/1000;
- Name = String.Format(bookmark_format, Track.DisplayTrackTitle, position_seconds/60, position_seconds%60);
+ public string Name {
+ get {
+ int position_seconds = (int)Position.TotalSeconds;
+ return String.Format (NAME_FMT,
+ Track.DisplayTrackTitle, position_seconds / 60, position_seconds % 60
+ );
+ }
}
- public Bookmark(int track_id, uint position)
+ public DatabaseTrackInfo Track { get; private set; }
+
+ public Bookmark () {}
+
+ public Bookmark (DatabaseTrackInfo track, int position_ms)
{
- Console.WriteLine ("Bookmark, main thread? {0}", ThreadAssist.InMainThread);
- this.track_id = track_id;
- this.position = position;
- this.created_at = DateTime.Now;
- uint position_seconds = position/1000;
- Name = String.Format(bookmark_format, Track.DisplayTrackTitle, position_seconds/60, position_seconds%60);
-
- this.id = ServiceManager.DbConnection.Execute (new HyenaSqliteCommand (@"
- INSERT INTO Bookmarks
- (TrackID, Position, CreatedAt)
- VALUES (?, ?, ?)",
- track_id, position, DateTimeUtil.FromDateTime(created_at) ));
+ Track = track;
+ Position = TimeSpan.FromMilliseconds (position_ms);
+ CreatedAt = DateTime.Now;
+
+ Provider.Save (this);
}
- public void JumpTo()
+ public void JumpTo ()
{
- DatabaseTrackInfo track = Track as DatabaseTrackInfo;
- DatabaseTrackInfo current_track = ServiceManager.PlayerEngine.CurrentTrack as DatabaseTrackInfo;
+ var track = Track;
+ var current_track = ServiceManager.PlayerEngine.CurrentTrack as DatabaseTrackInfo;
if (track != null) {
if (current_track == null || current_track.TrackId != track.TrackId) {
ServiceManager.PlayerEngine.ConnectEvent (HandleStateChanged, PlayerEvent.StateChange);
ServiceManager.PlayerEngine.OpenPlay (track);
} else {
if (ServiceManager.PlayerEngine.CanSeek) {
- ServiceManager.PlayerEngine.Position = position;
+ ServiceManager.PlayerEngine.Position = (uint)Position.TotalMilliseconds;
} else {
ServiceManager.PlayerEngine.ConnectEvent (HandleStateChanged, PlayerEvent.StateChange);
ServiceManager.PlayerEngine.Play ();
}
}
} else {
- Remove();
+ Remove ();
}
}
@@ -131,64 +130,31 @@ namespace Banshee.Bookmarks
}
if (ServiceManager.PlayerEngine.CanSeek) {
- ServiceManager.PlayerEngine.Position = position;
+ ServiceManager.PlayerEngine.Position = (uint)Position.TotalMilliseconds;
}
}
}
- public void Remove()
+ public void Remove ()
{
try {
- ServiceManager.DbConnection.Execute(String.Format(
- "DELETE FROM Bookmarks WHERE BookmarkID = {0}", id
- ));
+ Provider.Delete (this);
- if (BookmarkUI.Instantiated)
- BookmarkUI.Instance.RemoveBookmark(this);
+ if (BookmarkUI.Instantiated) {
+ BookmarkUI.Instance.RemoveBookmark (this);
+ }
} catch (Exception e) {
- Log.Warning("Error Removing Bookmark", e.ToString(), false);
+ Log.Exception ("Error Removing Bookmark", e);
}
}
- public static List<Bookmark> LoadAll()
+ public static List<Bookmark> LoadAll ()
{
- List<Bookmark> bookmarks = new List<Bookmark>();
-
- IDataReader reader = ServiceManager.DbConnection.Query(
- "SELECT BookmarkID, TrackID, Position, CreatedAt FROM Bookmarks"
- );
-
- while (reader.Read()) {
- try {
- bookmarks.Add(new Bookmark(
- reader.GetInt32 (0), reader.GetInt32 (1), Convert.ToUInt32 (reader[2]),
- DateTimeUtil.ToDateTime(Convert.ToInt64(reader[3]))
- ));
- } catch (Exception e) {
- ServiceManager.DbConnection.Execute(String.Format(
- "DELETE FROM Bookmarks WHERE BookmarkID = {0}", reader.GetInt32 (0)
- ));
-
- Log.Warning("Error Loading Bookmark", e.ToString(), false);
- }
- }
- reader.Dispose();
-
- return bookmarks;
+ return Provider.FetchAll ().ToList ();
}
- public static void Initialize()
- {
- if (!ServiceManager.DbConnection.TableExists("Bookmarks")) {
- ServiceManager.DbConnection.Execute(@"
- CREATE TABLE Bookmarks (
- BookmarkID INTEGER PRIMARY KEY,
- TrackID INTEGER NOT NULL,
- Position INTEGER NOT NULL,
- CreatedAt INTEGER NOT NULL
- )
- ");
- }
- }
+ // Translators: This is used to generate bookmark names. {0} is track title, {1} is minutes
+ // (possibly more than two digits) and {2} is seconds (between 00 and 60).
+ private static readonly string NAME_FMT = Catalog.GetString ("{0} ({1}:{2:00})");
}
}
diff --git a/src/Extensions/Banshee.Bookmarks/Banshee.Bookmarks/BookmarkUI.cs b/src/Extensions/Banshee.Bookmarks/Banshee.Bookmarks/BookmarkUI.cs
index d671390..1cc8f64 100644
--- a/src/Extensions/Banshee.Bookmarks/Banshee.Bookmarks/BookmarkUI.cs
+++ b/src/Extensions/Banshee.Bookmarks/Banshee.Bookmarks/BookmarkUI.cs
@@ -54,10 +54,10 @@ namespace Banshee.Bookmarks
private ImageMenuItem remove_item;
private SeparatorMenuItem separator;
- private List<Bookmark> bookmarks = new List<Bookmark>();
- private Dictionary<Bookmark, MenuItem> select_items = new Dictionary<Bookmark, MenuItem>();
- private Dictionary<Bookmark, MenuItem> remove_items = new Dictionary<Bookmark, MenuItem>();
- private Dictionary<MenuItem, Bookmark> bookmark_map = new Dictionary<MenuItem, Bookmark>();
+ private List<Bookmark> bookmarks = new List<Bookmark> ();
+ private Dictionary<Bookmark, MenuItem> select_items = new Dictionary<Bookmark, MenuItem> ();
+ private Dictionary<Bookmark, MenuItem> remove_items = new Dictionary<Bookmark, MenuItem> ();
+ private Dictionary<MenuItem, Bookmark> bookmark_map = new Dictionary<MenuItem, Bookmark> ();
private InterfaceActionService action_service;
private ActionGroup actions;
@@ -67,7 +67,7 @@ namespace Banshee.Bookmarks
public static BookmarkUI Instance {
get {
if (instance == null)
- instance = new BookmarkUI();
+ instance = new BookmarkUI ();
return instance;
}
}
@@ -76,125 +76,124 @@ namespace Banshee.Bookmarks
get { return instance != null; }
}
- private BookmarkUI()
+ private BookmarkUI ()
{
action_service = ServiceManager.Get<InterfaceActionService> ("InterfaceActionService");
- actions = new ActionGroup("Bookmarks");
+ actions = new ActionGroup ("Bookmarks");
- actions.Add(new ActionEntry [] {
- new ActionEntry("BookmarksAction", null,
- Catalog.GetString("_Bookmarks"), null,
+ actions.Add (new ActionEntry [] {
+ new ActionEntry ("BookmarksAction", null,
+ Catalog.GetString ("_Bookmarks"), null,
null, null),
- new ActionEntry("BookmarksAddAction", Stock.Add,
- Catalog.GetString("_Add Bookmark"), "<control>D",
- Catalog.GetString("Bookmark the Position in the Current Track"),
+ new ActionEntry ("BookmarksAddAction", Stock.Add,
+ Catalog.GetString ("_Add Bookmark"), "<control>D",
+ Catalog.GetString ("Bookmark the Position in the Current Track"),
HandleNewBookmark)
});
- action_service.UIManager.InsertActionGroup(actions, 0);
- ui_manager_id = action_service.UIManager.AddUiFromResource("BookmarksMenu.xml");
- bookmark_item = action_service.UIManager.GetWidget("/MainMenu/ToolsMenu/Bookmarks") as ImageMenuItem;
- new_item = action_service.UIManager.GetWidget("/MainMenu/ToolsMenu/Bookmarks/Add") as ImageMenuItem;
+ action_service.UIManager.InsertActionGroup (actions, 0);
+ ui_manager_id = action_service.UIManager.AddUiFromResource ("BookmarksMenu.xml");
+ bookmark_item = action_service.UIManager.GetWidget ("/MainMenu/ToolsMenu/Bookmarks") as ImageMenuItem;
+ new_item = action_service.UIManager.GetWidget ("/MainMenu/ToolsMenu/Bookmarks/Add") as ImageMenuItem;
bookmark_menu = bookmark_item.Submenu as Menu;
bookmark_item.Selected += HandleMenuShown;
- remove_item = new ImageMenuItem(Catalog.GetString("_Remove Bookmark"));
+ remove_item = new ImageMenuItem (Catalog.GetString ("_Remove Bookmark"));
remove_item.Sensitive = false;
- remove_item.Image = new Image(Stock.Remove, IconSize.Menu);
+ remove_item.Image = new Image (Stock.Remove, IconSize.Menu);
- remove_item.Submenu = remove_menu = new Menu();
- bookmark_menu.Append(remove_item);
+ remove_item.Submenu = remove_menu = new Menu ();
+ bookmark_menu.Append (remove_item);
LoadBookmarks ();
}
- private void HandleMenuShown(object sender, EventArgs args)
+ private void HandleMenuShown (object sender, EventArgs args)
{
new_item.Sensitive = (ServiceManager.PlayerEngine.CurrentTrack != null);
}
- private void HandleNewBookmark(object sender, EventArgs args)
+ private void HandleNewBookmark (object sender, EventArgs args)
{
- DatabaseTrackInfo track = ServiceManager.PlayerEngine.CurrentTrack as DatabaseTrackInfo;
+ var track = ServiceManager.PlayerEngine.CurrentTrack as DatabaseTrackInfo;
if (track != null) {
try {
- Bookmark bookmark = new Bookmark(track.TrackId, ServiceManager.PlayerEngine.Position);
- AddBookmark(bookmark);
+ AddBookmark (new Bookmark (track, (int)ServiceManager.PlayerEngine.Position));
} catch (Exception e) {
- Log.Warning("Unable to Add New Bookmark", e.ToString(), false);
+ Log.Exception ("Unable to Add New Bookmark", e);
}
}
}
private void LoadBookmarks ()
{
- separator = new SeparatorMenuItem();
+ separator = new SeparatorMenuItem ();
- foreach (Bookmark bookmark in Bookmark.LoadAll()) {
- AddBookmark(bookmark);
+ foreach (Bookmark bookmark in Bookmark.LoadAll ()) {
+ AddBookmark (bookmark);
}
- bookmark_item.ShowAll();
+ bookmark_item.ShowAll ();
}
- public void AddBookmark(Bookmark bookmark)
+ public void AddBookmark (Bookmark bookmark)
{
- if (select_items.ContainsKey(bookmark))
+ if (select_items.ContainsKey (bookmark))
return;
- bookmarks.Add(bookmark);
+ bookmarks.Add (bookmark);
if (bookmarks.Count == 1) {
- bookmark_menu.Append(separator);
+ bookmark_menu.Append (separator);
remove_item.Sensitive = true;
}
// Add menu item to jump to this bookmark
- ImageMenuItem select_item = new ImageMenuItem(bookmark.Name.Replace("_", "__"));
- select_item.Image = new Image(Stock.JumpTo, IconSize.Menu);
+ ImageMenuItem select_item = new ImageMenuItem (bookmark.Name.Replace ("_", "__"));
+ select_item.Image = new Image (Stock.JumpTo, IconSize.Menu);
select_item.Activated += delegate {
Console.WriteLine ("item delegate, main thread? {0}", ThreadAssist.InMainThread);
- bookmark.JumpTo();
+ bookmark.JumpTo ();
};
- bookmark_menu.Append(select_item);
+ bookmark_menu.Append (select_item);
select_items[bookmark] = select_item;
// Add menu item to remove this bookmark
- ImageMenuItem rem = new ImageMenuItem(bookmark.Name.Replace("_", "__"));
- rem.Image = new Image(Stock.Remove, IconSize.Menu);
+ ImageMenuItem rem = new ImageMenuItem (bookmark.Name.Replace ("_", "__"));
+ rem.Image = new Image (Stock.Remove, IconSize.Menu);
rem.Activated += delegate {
- bookmark.Remove();
+ bookmark.Remove ();
};
- remove_menu.Append(rem);
+ remove_menu.Append (rem);
remove_items[bookmark] = rem;
bookmark_map[rem] = bookmark;
- bookmark_menu.ShowAll();
+ bookmark_menu.ShowAll ();
}
- public void RemoveBookmark(Bookmark bookmark)
+ public void RemoveBookmark (Bookmark bookmark)
{
- if (!remove_items.ContainsKey(bookmark))
+ if (!remove_items.ContainsKey (bookmark))
return;
- bookmark_menu.Remove(select_items[bookmark]);
- remove_menu.Remove(remove_items[bookmark]);
- bookmarks.Remove(bookmark);
- select_items.Remove(bookmark);
- bookmark_map.Remove(remove_items[bookmark]);
- remove_items.Remove(bookmark);
+ bookmark_menu.Remove (select_items[bookmark]);
+ remove_menu.Remove (remove_items[bookmark]);
+ bookmarks.Remove (bookmark);
+ select_items.Remove (bookmark);
+ bookmark_map.Remove (remove_items[bookmark]);
+ remove_items.Remove (bookmark);
if (bookmarks.Count == 0) {
- bookmark_menu.Remove(separator);
+ bookmark_menu.Remove (separator);
remove_item.Sensitive = false;
}
}
- public void Dispose()
+ public void Dispose ()
{
- action_service.UIManager.RemoveUi(ui_manager_id);
- action_service.UIManager.RemoveActionGroup(actions);
+ action_service.UIManager.RemoveUi (ui_manager_id);
+ action_service.UIManager.RemoveActionGroup (actions);
actions = null;
instance = null;
diff --git a/src/Extensions/Banshee.Bookmarks/Banshee.Bookmarks/BookmarksService.cs b/src/Extensions/Banshee.Bookmarks/Banshee.Bookmarks/BookmarksService.cs
index 244e624..25486fd 100644
--- a/src/Extensions/Banshee.Bookmarks/Banshee.Bookmarks/BookmarksService.cs
+++ b/src/Extensions/Banshee.Bookmarks/Banshee.Bookmarks/BookmarksService.cs
@@ -54,7 +54,6 @@ namespace Banshee.Bookmarks
void IExtensionService.Initialize ()
{
- Bookmark.Initialize();
ui = BookmarkUI.Instance;
}
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]