banshee r4509 - in trunk/banshee: . src/Core/Banshee.Core/Banshee.Collection src/Core/Banshee.Services src/Core/Banshee.Services/Banshee.Collection.Indexer



Author: abock
Date: Wed Sep 10 18:29:14 2008
New Revision: 4509
URL: http://svn.gnome.org/viewvc/banshee?rev=4509&view=rev

Log:
2008-09-10  Aaron Bockover  <abock gnome org>

    * src/Core/Banshee.Services/Banshee.Collection.Indexer/ICollectionIndexerService.cs:
    * src/Core/Banshee.Services/Banshee.Collection.Indexer/CollectionIndexerService.cs:
    Implement GetAvailableExportFields method

    * src/Core/Banshee.Services/Banshee.Collection.Indexer/ICollectionIndexer.cs:
    * src/Core/Banshee.Services/Banshee.Collection.Indexer/CollectionIndexer.cs:
    Implement SaveToXml; added SetExportFields; added SaveToXmlFinished event

    * src/Core/Banshee.Core/Banshee.Collection/TrackInfo.cs: Support exporting
    only certain fields; make GetExportableProperties public; since
    GetExportableProperties is now public, enforce type derivation; do not
    export 0 value fields



Modified:
   trunk/banshee/ChangeLog
   trunk/banshee/src/Core/Banshee.Core/Banshee.Collection/TrackInfo.cs
   trunk/banshee/src/Core/Banshee.Services/Banshee.Collection.Indexer/CollectionIndexer.cs
   trunk/banshee/src/Core/Banshee.Services/Banshee.Collection.Indexer/CollectionIndexerService.cs
   trunk/banshee/src/Core/Banshee.Services/Banshee.Collection.Indexer/ICollectionIndexer.cs
   trunk/banshee/src/Core/Banshee.Services/Banshee.Collection.Indexer/ICollectionIndexerService.cs
   trunk/banshee/src/Core/Banshee.Services/Banshee.Services.csproj

Modified: trunk/banshee/src/Core/Banshee.Core/Banshee.Collection/TrackInfo.cs
==============================================================================
--- trunk/banshee/src/Core/Banshee.Core/Banshee.Collection/TrackInfo.cs	(original)
+++ trunk/banshee/src/Core/Banshee.Core/Banshee.Collection/TrackInfo.cs	Wed Sep 10 18:29:14 2008
@@ -145,6 +145,11 @@
             get { return uri; }
             set { uri = value; }
         }
+        
+        [Exportable]
+        public string LocalPath {
+            get { return Uri == null || !Uri.IsLocalPath ? null : Uri.LocalPath; }
+        }
 
         [Exportable]
         public SafeUri MoreInfoUri {
@@ -481,11 +486,42 @@
             }
         }
         
+        public static IEnumerable<KeyValuePair<string, PropertyInfo>> GetExportableProperties (Type type)
+        {
+            FindExportableProperties (type);
+            
+            Dictionary<string, PropertyInfo> properties = null;
+            if (exportable_properties.TryGetValue (type, out properties)) {
+                foreach (KeyValuePair<string, PropertyInfo> property in properties) {
+                    yield return property;
+                }
+            }
+        }
+        
         public IDictionary<string, object> GenerateExportable ()
         {
+            return GenerateExportable (null);
+        }
+        
+        public IDictionary<string, object> GenerateExportable (string [] fields)
+        {
             Dictionary<string, object> dict = new Dictionary<string, object> ();
 
             foreach (KeyValuePair<string, PropertyInfo> property in GetExportableProperties (GetType ())) {
+                if (fields != null) {
+                    bool found = false;
+                    foreach (string field in fields) {
+                        if (field == property.Key) {
+                            found = true;
+                            break;
+                        }
+                    }
+                    
+                    if (!found) {
+                        continue;
+                    }
+                }
+                
                 object value = property.Value.GetValue (this, null);
                 if (value == null) {
                     continue;
@@ -500,17 +536,20 @@
                     value = ((SafeUri)value).AbsoluteUri;
                 } else if (value is TrackMediaAttributes) {
                     value = value.ToString ();
-                } else if (!(
-                    value is ushort || value is short || 
-                    value is uint || value is int ||
-                    value is ulong || value is long ||
-                    value is float || value is double ||
-                    value is bool || value is string)) {
+                } else if (!(value.GetType ().IsPrimitive || value is string)) {
                     Log.WarningFormat ("Invalid property in {0} marked as [Exportable]: ({1} is a {2})", 
                         property.Value.DeclaringType, property.Value.Name, value.GetType ());
                     continue;
                 }
                 
+                // A bit lame
+                if (!(value is string)) {
+                    string str_value = value.ToString ();
+                    if (str_value == "0" || str_value == "0.0") {
+                        continue;
+                    }
+                }
+                
                 dict.Add (property.Key, value);
             }
             
@@ -532,13 +571,19 @@
                 // Build a stack of types to reflect
                 Stack<Type> probe_types = new Stack<Type> ();
                 Type probe_type = type;
+                bool is_track_info = false;
                 while (probe_type != null) {
                     probe_types.Push (probe_type);
                     if (probe_type == typeof (TrackInfo)) {
+                        is_track_info = true;
                         break;
                     }
                     probe_type = probe_type.BaseType;
                 }
+                
+                if (!is_track_info) {
+                    throw new ArgumentException ("Type must derive from Banshee.Collection.TrackInfo", "type");
+                }
             
                 // Iterate through all types
                 while (probe_types.Count > 0) {
@@ -603,18 +648,6 @@
             }
         }
         
-        private static IEnumerable<KeyValuePair<string, PropertyInfo>> GetExportableProperties (Type type)
-        {
-            FindExportableProperties (type);
-            
-            Dictionary<string, PropertyInfo> properties = null;
-            if (exportable_properties.TryGetValue (type, out properties)) {
-                foreach (KeyValuePair<string, PropertyInfo> property in properties) {
-                    yield return property;
-                }
-            }
-        }
-        
 #endregion
 
     }

Modified: trunk/banshee/src/Core/Banshee.Services/Banshee.Collection.Indexer/CollectionIndexer.cs
==============================================================================
--- trunk/banshee/src/Core/Banshee.Services/Banshee.Collection.Indexer/CollectionIndexer.cs	(original)
+++ trunk/banshee/src/Core/Banshee.Services/Banshee.Collection.Indexer/CollectionIndexer.cs	Wed Sep 10 18:29:14 2008
@@ -27,10 +27,13 @@
 //
 
 using System;
+using System.Xml;
+using System.Threading;
 using System.Collections.Generic;
 
 using Hyena;
 
+using Banshee.Base;
 using Banshee.Sources;
 using Banshee.Library;
 using Banshee.ServiceStack;
@@ -45,6 +48,7 @@
         
         private CollectionIndexerService service;
         private List<CachedList<DatabaseTrackInfo>> model_caches = new List<CachedList<DatabaseTrackInfo>> ();
+        private string [] export_fields;
         
         private event IndexingFinishedHandler indexing_finished;
         event IndexingFinishedHandler ICollectionIndexer.IndexingFinished {
@@ -52,6 +56,12 @@
             remove { indexing_finished -= value; }
         }
         
+        private event SaveToXmlFinishedHandler save_to_xml_finished;
+        event SaveToXmlFinishedHandler ICollectionIndexer.SaveToXmlFinished {
+            add { save_to_xml_finished += value; }
+            remove { save_to_xml_finished -= value; }
+        }
+        
         public event EventHandler IndexingFinished;
         
         internal CollectionIndexer (CollectionIndexerService service)
@@ -77,7 +87,14 @@
             model_caches.Clear ();
         }
         
-        public void Start ()
+        public void SetExportFields (string [] fields)
+        {
+            lock (this) {
+                export_fields = fields;
+            }
+        }
+        
+        public void Index ()
         {
             lock (this) {
                 DisposeModels ();
@@ -94,13 +111,88 @@
             OnIndexingFinished ();
         }
         
-        public bool SaveToXml (string path)
+        void ICollectionIndexer.Index ()
+        {
+            ThreadPool.QueueUserWorkItem (delegate { Index (); });
+        }
+        
+        public void SaveToXml (string path)
         {
             lock (this) {
-                return false;
+                bool success = false;
+                try {
+                    XmlTextWriter writer = new XmlTextWriter (path, System.Text.Encoding.UTF8);
+                    writer.Formatting = Formatting.Indented;
+                    writer.Indentation = 2;
+                    writer.IndentChar = ' ';
+                    
+                    writer.WriteStartDocument (true);
+                    
+                    writer.WriteStartElement ("banshee-collection");
+                    writer.WriteStartAttribute ("version");
+                    writer.WriteString ("1.0");
+                    writer.WriteEndAttribute ();
+                   
+                    for (int i = 0; i < model_caches.Count; i++) { 
+                        CachedList<DatabaseTrackInfo> model = model_caches[i];
+                        if (model.Count <= 0) {
+                            continue;
+                        }
+                        
+                        writer.WriteStartElement ("model");
+                        for (int j = 0; j < model.Count; j++) {
+                            writer.WriteStartElement ("item");
+                            
+                            foreach (KeyValuePair<string, object> item in model[j].GenerateExportable (export_fields)) {
+                                string type = "string";
+                                if      (item.Value is Boolean) type = "bool";
+                                else if (item.Value is Byte)    type = "byte";
+                                else if (item.Value is SByte)   type = "sbyte";
+                                else if (item.Value is Int16)   type = "short";
+                                else if (item.Value is UInt16)  type = "ushort";
+                                else if (item.Value is Int32)   type = "int";
+                                else if (item.Value is UInt32)  type = "uint";
+                                else if (item.Value is Int64)   type = "long";
+                                else if (item.Value is UInt64)  type = "ulong";
+                                else if (item.Value is Char)    type = "char";
+                                else if (item.Value is Double)  type = "double";
+                                else if (item.Value is Single)  type = "float";
+                                
+                                writer.WriteStartElement (item.Key);
+                                writer.WriteStartAttribute ("type");
+                                writer.WriteString (type);
+                                writer.WriteEndAttribute ();
+                                writer.WriteString (item.Value.ToString ());
+                                writer.WriteEndElement ();
+                            }
+                            
+                            writer.WriteEndElement ();
+                        }
+                        
+                        writer.WriteEndElement ();
+                    }
+                    
+                    writer.WriteEndElement ();
+                    writer.WriteEndDocument ();
+                    writer.Close ();
+                    
+                    success = true;
+                } catch (Exception e) {
+                    Log.Exception (e);
+                }
+                
+                SaveToXmlFinishedHandler handler = save_to_xml_finished;
+                if (handler != null) {
+                    handler (success, path);
+                }
             }
         }
         
+        void ICollectionIndexer.SaveToXml (string path)
+        {   
+            ThreadPool.QueueUserWorkItem (delegate { SaveToXml (path); });
+        }
+        
         public IDictionary<string, object> GetResult (int modelIndex, int itemIndex)
         {
             lock (this) {
@@ -114,7 +206,7 @@
                     throw new IndexOutOfRangeException ("itemIndex");
                 }
                 
-                return model[modelIndex].GenerateExportable ();
+                return model[modelIndex].GenerateExportable (export_fields);
             }
         }
         

Modified: trunk/banshee/src/Core/Banshee.Services/Banshee.Collection.Indexer/CollectionIndexerService.cs
==============================================================================
--- trunk/banshee/src/Core/Banshee.Services/Banshee.Collection.Indexer/CollectionIndexerService.cs	(original)
+++ trunk/banshee/src/Core/Banshee.Services/Banshee.Collection.Indexer/CollectionIndexerService.cs	Wed Sep 10 18:29:14 2008
@@ -38,6 +38,7 @@
     public class CollectionIndexerService : ICollectionIndexerService
     {
         private List<TrackListModel> models = new List<TrackListModel> ();
+        private string [] available_export_fields;
         
         public void AddModel (TrackListModel model)
         {
@@ -74,6 +75,23 @@
             return ServiceManager.DBusServiceManager.RegisterObject (new CollectionIndexer (this));
         }
         
+        public string [] GetAvailableExportFields ()
+        {
+            if (available_export_fields != null) {
+                return available_export_fields;
+            }
+            
+            List<string> fields = new List<string> ();
+            
+            foreach (KeyValuePair<string, System.Reflection.PropertyInfo> field in TrackInfo.GetExportableProperties (
+                typeof (Banshee.Collection.Database.DatabaseTrackInfo))) {
+                fields.Add (field.Key);
+            }
+            
+            available_export_fields = fields.ToArray ();
+            return available_export_fields;
+        }
+        
         IDBusExportable IDBusExportable.Parent { 
             get { return null; }
         }

Modified: trunk/banshee/src/Core/Banshee.Services/Banshee.Collection.Indexer/ICollectionIndexer.cs
==============================================================================
--- trunk/banshee/src/Core/Banshee.Services/Banshee.Collection.Indexer/ICollectionIndexer.cs	(original)
+++ trunk/banshee/src/Core/Banshee.Services/Banshee.Collection.Indexer/ICollectionIndexer.cs	Wed Sep 10 18:29:14 2008
@@ -35,19 +35,23 @@
 namespace Banshee.Collection.Indexer
 {
     public delegate void IndexingFinishedHandler ();
+    public delegate void SaveToXmlFinishedHandler (bool success, string path);
     
     [Interface ("org.bansheeproject.Banshee.CollectionIndexer")]
     public interface ICollectionIndexer : IService, IDBusExportable
     {
         event IndexingFinishedHandler IndexingFinished;
-            
-        void Start ();
+        event SaveToXmlFinishedHandler SaveToXmlFinished;
+        
+        void Index ();
         void Dispose ();
         
+        void SetExportFields (string [] fields);
+        
         int GetModelCounts ();
         int GetModelResultsCount (int modelIndex);
         IDictionary<string, object> GetResult (int modelIndex, int itemIndex);
         
-        bool SaveToXml (string path);
+        void SaveToXml (string path);
     }
 }

Modified: trunk/banshee/src/Core/Banshee.Services/Banshee.Collection.Indexer/ICollectionIndexerService.cs
==============================================================================
--- trunk/banshee/src/Core/Banshee.Services/Banshee.Collection.Indexer/ICollectionIndexerService.cs	(original)
+++ trunk/banshee/src/Core/Banshee.Services/Banshee.Collection.Indexer/ICollectionIndexerService.cs	Wed Sep 10 18:29:14 2008
@@ -39,5 +39,6 @@
     public interface ICollectionIndexerService : IService, IDBusExportable
     {
         ObjectPath CreateIndexer ();
+        string [] GetAvailableExportFields ();
     }
 }

Modified: trunk/banshee/src/Core/Banshee.Services/Banshee.Services.csproj
==============================================================================
--- trunk/banshee/src/Core/Banshee.Services/Banshee.Services.csproj	(original)
+++ trunk/banshee/src/Core/Banshee.Services/Banshee.Services.csproj	Wed Sep 10 18:29:14 2008
@@ -31,6 +31,7 @@
     <Reference Include="TagLib, Version=0.0.0.0, Culture=neutral" />
     <Reference Include="NDesk.DBus, Version=1.0.0.0, Culture=neutral, PublicKeyToken=f6716e4f9b2ed099" />
     <Reference Include="NDesk.DBus.GLib, Version=1.0.0.0, Culture=neutral, PublicKeyToken=f6716e4f9b2ed099" />
+    <Reference Include="System.Xml" />
   </ItemGroup>
   <ItemGroup>
     <ProjectReference Include="..\..\Libraries\Hyena\Hyena.csproj">



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