using System; using System.Collections; using System.Collections.Generic; using Mono.Unix; using System.Data; using Banshee.Base; using Banshee.Sources; using Banshee.Database; using Gtk; public static class PluginModuleEntry { public static Type [] GetTypes() { return new Type [] { typeof(Banshee.Plugins.ArtistList.ArtistListPlugin) }; } } namespace Banshee.Plugins.ArtistList { public class ArtistListPlugin : Banshee.Plugins.Plugin { protected override string ConfigurationName { get { return "ArtistList"; } } public override string DisplayName { get { return "ArtisList"; } } public override string Description { get { return Catalog.GetString( "A plugin that shows a tree of songs grouped by artist and albums." ); } } public override string [] Authors { get { return new string [] { "Johannes Schlüter" }; } } // --------------------------------------------------------------- // private EmptySource source; private HierChildSource[] artistSources; private HierChildSource[] albumSources; protected override void PluginInitialize() { int tracknum = Convert.ToInt32(Globals.Library.Db.QuerySingle("SELECT Count(*) FROM Tracks")); source = new EmptySource("Brwse by artist"); SourceManager.AddSource(source); /* this is currently wrong since we should use the number of artitsts/albums, not tracks to save memory */ artistSources = new HierChildSource[tracknum]; albumSources = new HierChildSource[tracknum]; // This might be reduced to one query instead of num artists + num albums queries but since it works // nice for me and needs rework when making hiers configureable I let my lazyness win ;-) int ar = 0, al = 0; IDataReader artistrdr; artistrdr = Globals.Library.Db.Query("SELECT Artist FROM Tracks GROUP BY Artist ORDER BY Artist"); while (artistrdr.Read()) { artistSources[ar] = new HierChildSource("Artist", artistrdr.GetString(0), ""); IDataReader albumrdr = Globals.Library.Db.Query(new DbCommand( "SELECT AlbumTitle FROM Tracks WHERE Artist=:artist GROUP BY AlbumTitle ORDER BY AlbumTitle", "artist", artistrdr.GetString(0))); while (albumrdr.Read()) { // The artist isn't escaped this way - needs a fix! albumSources[al] = new HierChildSource("AlbumTitle", albumrdr.GetString(0), "Artist = '"+artistrdr.GetString(0)+"'"); artistSources[ar].AddChildSource(albumSources[al]); al++; } source.AddChildSource(artistSources[ar]); ar++; } artistrdr.Close(); } protected override void PluginDispose() { // I doubt it's enough to clean just that hier but I ned to remove the childs, too... SourceManager.RemoveSource(source); } public override Gtk.Widget GetConfigurationWidget() { return new ConfigPage();; } } public class EmptySource : Banshee.Sources.Source { public EmptySource(String caption) : base(caption, 150 ) {} } public class HierChildSource : Banshee.Sources.ChildSource { private List tracks = new List(); private string hier; private string val; private string SQLCondition; public override int Count { get { return tracks.Count; } } public override IEnumerable Tracks { get { return tracks; } } public HierChildSource(String hier, String val, String SQLCondition) : base(val, 150) { this.hier = hier; this.val = val; this.SQLCondition = SQLCondition; } public override void AddTrack(TrackInfo track) { if(track is LibraryTrackInfo) { lock(TracksMutex) { tracks.Add(track); } OnTrackAdded (track); } } public override void Activate() { String sql = @"SELECT TrackID FROM Tracks WHERE "; if (SQLCondition.Length > 0) { sql += SQLCondition + " AND "; } sql += hier + " = :val"; IDataReader reader = Globals.Library.Db.Query(new DbCommand( sql, "val", val )); while(reader.Read()) { if (Globals.Library.Tracks.ContainsKey(Convert.ToInt32(reader[0]))) { AddTrack (Globals.Library.Tracks[Convert.ToInt32(reader[0])] as TrackInfo); } } reader.Close(); } } // Ths doesn't make any sense, yet but helps me remeberng how to add a configuration thingy :-) public class ConfigPage : VBox { public ConfigPage() { Label title = new Label(); title.Markup = String.Format("{0}", GLib.Markup.EscapeText(Catalog.GetString("ArtistList"))); title.Xalign = 0.0f; PackStart(title, false, false, 0); ShowAll(); } } }