[hyena] Add some classes from Banshee's Hyena



commit cb2e0d772f17536969d0e100230149d9ce16827c
Author: Gabriel Burt <gabriel burt gmail com>
Date:   Tue Jan 12 11:54:31 2010 -0800

    Add some classes from Banshee's Hyena

 src/Hyena/Hyena.Data/MemoryListModel.cs |   97 +++++++++++++++++++++++++++
 src/Hyena/Hyena.Query/EnumQueryValue.cs |  111 +++++++++++++++++++++++++++++++
 src/Hyena/Hyena.csproj                  |    4 +
 src/Hyena/Hyena/EventArgs.cs            |   44 ++++++++++++
 src/Hyena/Hyena/XdgBaseDirectorySpec.cs |   12 ++++
 5 files changed, 268 insertions(+), 0 deletions(-)
---
diff --git a/src/Hyena/Hyena.Data/MemoryListModel.cs b/src/Hyena/Hyena.Data/MemoryListModel.cs
new file mode 100644
index 0000000..ae037ee
--- /dev/null
+++ b/src/Hyena/Hyena.Data/MemoryListModel.cs
@@ -0,0 +1,97 @@
+//
+// MemoryListModel.cs
+//
+// Author:
+//   Gabriel Burt <gburt novell com>
+//
+// Copyright (C) 2009 Novell, Inc.
+//
+// Permission is hereby granted, free of charge, to any person obtaining
+// a copy of this software and associated documentation files (the
+// "Software"), to deal in the Software without restriction, including
+// without limitation the rights to use, copy, modify, merge, publish,
+// distribute, sublicense, and/or sell copies of the Software, and to
+// permit persons to whom the Software is furnished to do so, subject to
+// the following conditions:
+//
+// The above copyright notice and this permission notice shall be
+// included in all copies or substantial portions of the Software.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+//
+
+using System;
+using System.Collections.Generic;
+
+using Hyena.Collections;
+
+namespace Hyena.Data
+{
+    public class MemoryListModel<T> : BaseListModel<T>
+    {
+        private List<T> list;
+
+        public MemoryListModel ()
+        {
+            list = new List<T> ();
+            selection = new Selection ();
+        }
+
+        public override void Clear ()
+        {
+            lock (list) {
+                list.Clear ();
+            }
+
+            OnCleared ();
+        }
+
+        public override void Reload ()
+        {
+            OnReloaded ();
+        }
+
+        public int IndexOf (T item)
+        {
+            lock (list) {
+                return list.IndexOf (item);
+            }
+        }
+
+        public void Add (T item)
+        {
+            lock (list) {
+                list.Add (item);
+            }
+        }
+
+        public void Remove (T item)
+        {
+            lock (list) {
+                list.Remove (item);
+            }
+        }
+
+        public override T this[int index] {
+            get {
+                lock (list) {
+                    return list[index];
+                }
+            }
+        }
+
+        public override int Count {
+            get {
+                lock (list) {
+                    return list.Count;
+                }
+            }
+        }
+    }
+}
diff --git a/src/Hyena/Hyena.Query/EnumQueryValue.cs b/src/Hyena/Hyena.Query/EnumQueryValue.cs
new file mode 100644
index 0000000..6b83945
--- /dev/null
+++ b/src/Hyena/Hyena.Query/EnumQueryValue.cs
@@ -0,0 +1,111 @@
+//
+// EnumQueryValue.cs
+//
+// Author:
+//   Alexander Kojevnikov <alexander kojevnikov com>
+//
+// Copyright (C) 2009 Alexander Kojevnikov
+//
+// Permission is hereby granted, free of charge, to any person obtaining
+// a copy of this software and associated documentation files (the
+// "Software"), to deal in the Software without restriction, including
+// without limitation the rights to use, copy, modify, merge, publish,
+// distribute, sublicense, and/or sell copies of the Software, and to
+// permit persons to whom the Software is furnished to do so, subject to
+// the following conditions:
+//
+// The above copyright notice and this permission notice shall be
+// included in all copies or substantial portions of the Software.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+//
+
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Xml;
+using System.Text;
+
+using Mono.Unix;
+
+using Hyena;
+
+namespace Hyena.Query
+{
+    public abstract class EnumQueryValue : QueryValue
+    {
+        public static readonly Operator Equal    = new Operator ("equals", Catalog.GetString ("is"), "= {0}", "=", "==", ":");
+        public static readonly Operator NotEqual = new Operator ("notEqual", Catalog.GetString ("is not"), "!= {0}", true, "!=", "!:");
+
+        protected int value;
+
+        public abstract IEnumerable<EnumQueryValueItem> Items { get; }
+
+        public override string XmlElementName {
+            get { return "int"; }
+        }
+
+        public override object Value {
+            get { return value; }
+        }
+
+        public void SetValue (int value)
+        {
+            this.value = value;
+            IsEmpty = false;
+        }
+
+        protected static AliasedObjectSet<Operator> operators = new AliasedObjectSet<Operator> (Equal, NotEqual);
+        public override AliasedObjectSet<Operator> OperatorSet {
+            get { return operators; }
+        }
+
+        public override void ParseUserQuery (string input)
+        {
+            foreach (var item in Items) {
+                if (input == item.ID.ToString () || input == item.Name || item.Aliases.Contains (input)) {
+                    value = item.ID;
+                    IsEmpty = false;
+                    break;
+                }
+            }
+        }
+
+        public override void ParseXml (XmlElement node)
+        {
+            IsEmpty = !Int32.TryParse (node.InnerText, out value);
+        }
+
+        public override void LoadString (string str)
+        {
+            ParseUserQuery (str);
+        }
+
+        public override string ToSql (Operator op)
+        {
+            return Convert.ToString (value, System.Globalization.CultureInfo.InvariantCulture);
+        }
+    }
+
+    public sealed class EnumQueryValueItem : IAliasedObject
+    {
+        public int ID { get; private set; }
+        public string Name { get; private set; }
+        public string DisplayName { get; private set; }
+        public string[] Aliases { get; private set; }
+
+        public EnumQueryValueItem (int id, string name, string display_name, params string[] aliases)
+        {
+            ID = id;
+            Name = name;
+            DisplayName = display_name;
+            Aliases = aliases;
+        }
+    }
+}
diff --git a/src/Hyena/Hyena.csproj b/src/Hyena/Hyena.csproj
index 9e06756..ac0815e 100644
--- a/src/Hyena/Hyena.csproj
+++ b/src/Hyena/Hyena.csproj
@@ -58,6 +58,7 @@
     <Compile Include="Hyena.Data\IPropertyStoreExpose.cs" />
     <Compile Include="Hyena\IUndoAction.cs" />
     <Compile Include="Hyena\UndoManager.cs" />
+    <Compile Include="Hyena\XdgBaseDirectorySpec.cs" />
     <Compile Include="Hyena.Data\ColumnDescription.cs" />
     <Compile Include="Hyena.Data\ICacheableModel.cs" />
     <Compile Include="Hyena.Collections\RangeCollection.cs" />
@@ -143,6 +144,9 @@
     <Compile Include="Hyena\ApplicationContext.cs" />
     <Compile Include="Hyena\ThreadAssist.cs" />
     <Compile Include="Hyena\XdgBaseDirectorySpec.cs" />
+    <Compile Include="Hyena/Hyena.Query/EnumQueryValue.cs" />
+    <Compile Include="Hyena/Hyena/EventArgs.cs" />
+    <Compile Include="Hyena/Hyena/XdgBaseDirectorySpec.cs" />
   </ItemGroup>
   <ItemGroup>
     <Reference Include="Mono.Posix" />
diff --git a/src/Hyena/Hyena/EventArgs.cs b/src/Hyena/Hyena/EventArgs.cs
new file mode 100644
index 0000000..7603a9c
--- /dev/null
+++ b/src/Hyena/Hyena/EventArgs.cs
@@ -0,0 +1,44 @@
+//
+// EventArgs.cs
+//
+// Author:
+//   Alexander Kojevnikov <alexander kojevnikov com>
+//
+// Copyright (C) 2009 Alexander Kojevnikov
+//
+// Permission is hereby granted, free of charge, to any person obtaining
+// a copy of this software and associated documentation files (the
+// "Software"), to deal in the Software without restriction, including
+// without limitation the rights to use, copy, modify, merge, publish,
+// distribute, sublicense, and/or sell copies of the Software, and to
+// permit persons to whom the Software is furnished to do so, subject to
+// the following conditions:
+//
+// The above copyright notice and this permission notice shall be
+// included in all copies or substantial portions of the Software.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+//
+
+namespace Hyena
+{
+    public class EventArgs<T> : System.EventArgs
+    {
+        private readonly T value;
+
+        public EventArgs (T value)
+        {
+            this.value = value;
+        }
+
+        public T Value {
+            get { return value; }
+        }
+    }
+}
diff --git a/src/Hyena/Hyena/XdgBaseDirectorySpec.cs b/src/Hyena/Hyena/XdgBaseDirectorySpec.cs
index 701e0c1..b190f8d 100644
--- a/src/Hyena/Hyena/XdgBaseDirectorySpec.cs
+++ b/src/Hyena/Hyena/XdgBaseDirectorySpec.cs
@@ -78,5 +78,17 @@ namespace Hyena
 
             return Path.Combine (home_dir, fallback);
         }
+
+        public static string GetXdgDirectoryUnderHome (string key, string fallback)
+        {
+            string xdg_dir = XdgBaseDirectorySpec.GetUserDirectory (key, fallback);
+            string home_dir = Environment.GetFolderPath (Environment.SpecialFolder.Personal);
+
+            if (xdg_dir == null || xdg_dir == home_dir || !xdg_dir.StartsWith (home_dir)) {
+                xdg_dir = Path.Combine (home_dir, fallback);
+            }
+
+            return xdg_dir;
+        }
     }
 }



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