This adds depth and breadth first searching to QueryNode Index: src/Core/Banshee.Services/Banshee.SmartPlaylist/SmartPlaylistSource.cs =================================================================== --- src/Core/Banshee.Services/Banshee.SmartPlaylist/SmartPlaylistSource.cs (revision 3360) +++ src/Core/Banshee.Services/Banshee.SmartPlaylist/SmartPlaylistSource.cs (working copy) @@ -224,22 +224,6 @@ #region Private Methods - private void UpdateDependencies (QueryNode node) - { - if (node is QueryListNode) { - foreach (QueryNode child in (node as QueryListNode).Children) { - UpdateDependencies (child); - } - } else { - QueryTermNode term = node as QueryTermNode; - if (term != null && term.Field == BansheeQuery.SmartPlaylistField) { - SmartPlaylistSource s = (term.Value as SmartPlaylistQueryValue).ObjectValue; - s.Updated += OnDependencyUpdated; - dependencies.Add (s); - } - } - } - private void UpdateDependencies () { foreach (SmartPlaylistSource s in dependencies) { @@ -248,7 +232,11 @@ dependencies.Clear (); - UpdateDependencies (ConditionTree); + foreach (SmartPlaylistQueryValue value in ConditionTree.SearchForValues<SmartPlaylistQueryValue> ()) { + SmartPlaylistSource playlist = value.ObjectValue; + playlist.Updated += OnDependencyUpdated; + dependencies.Add (playlist); + } } private void OnDependencyUpdated (object sender, EventArgs args) Index: src/Libraries/Hyena/Hyena.mdp =================================================================== --- src/Libraries/Hyena/Hyena.mdp (revision 3360) +++ src/Libraries/Hyena/Hyena.mdp (working copy) @@ -81,6 +81,7 @@ <File name="Hyena/Log.cs" subtype="Code" buildaction="Compile" /> <File name="Hyena/CryptoUtil.cs" subtype="Code" buildaction="Compile" /> <File name="Hyena.Query/IntegerKeyedObjectQueryValue.cs" subtype="Code" buildaction="Compile" /> + <File name="Hyena.Query/QueryNodeSearchMethod.cs" subtype="Code" buildaction="Compile" /> </Contents> <References> <ProjectReference type="Gac" localcopy="True" refto="System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" /> Index: src/Libraries/Hyena/Hyena.Query/QueryNode.cs =================================================================== --- src/Libraries/Hyena/Hyena.Query/QueryNode.cs (revision 3358) +++ src/Libraries/Hyena/Hyena.Query/QueryNode.cs (working copy) @@ -28,6 +28,7 @@ // using System; +using System.Collections.Generic; using System.Xml; using System.IO; using System.Text; @@ -108,6 +109,63 @@ } } } + + public IEnumerable<T> SearchForValues<T> () where T : QueryValue + { + return SearchForValues<T> (QueryNodeSearchMethod.DepthFirst); + } + + public IEnumerable<T> SearchForValues<T> (QueryNodeSearchMethod method) where T : QueryValue + { + if (method == QueryNodeSearchMethod.DepthFirst) { + return SearchForValuesByDepth<T> (this); + } else { + return SearchForValuesByBreadth<T> (); + } + } + + private static IEnumerable<T> SearchForValuesByDepth<T> (QueryNode node) where T : QueryValue + { + QueryListNode list = node as QueryListNode; + if (list != null) { + foreach (QueryNode child in list.Children) { + foreach (T item in SearchForValuesByDepth<T> (child)) { + yield return item; + } + } + } else { + QueryTermNode term = node as QueryTermNode; + if (term != null) { + T value = term.Value as T; + if (value != null) { + yield return value; + } + } + } + } + + private IEnumerable<T> SearchForValuesByBreadth<T> () where T : QueryValue + { + Queue<QueryNode> queue = new Queue<QueryNode> (); + queue.Enqueue (this); + do { + QueryNode node = queue.Dequeue (); + QueryListNode list = node as QueryListNode; + if (list != null) { + foreach (QueryNode child in list.Children) { + queue.Enqueue (child); + } + } else { + QueryTermNode term = node as QueryTermNode; + if (term != null && term.Value is T) { + T value = term.Value as T; + if (value != null) { + yield return value; + } + } + } + } while (queue.Count > 0); + } public override string ToString () { Index: src/Libraries/Hyena/Hyena.Query/QueryNodeSearchMethod.cs =================================================================== --- src/Libraries/Hyena/Hyena.Query/QueryNodeSearchMethod.cs (revision 0) +++ src/Libraries/Hyena/Hyena.Query/QueryNodeSearchMethod.cs (revision 0) @@ -0,0 +1,38 @@ +// +// QueryNodeSearchMethod.cs +// +// Authors: +// Scott Peterson <lunchtimemama gmail com> +// +// Copyright (C) 2008 Scott Peterson +// +// 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; + +namespace Hyena.Query +{ + public enum QueryNodeSearchMethod + { + DepthFirst, + BreadthFirst + } +} Index: src/Libraries/Hyena/Makefile.am =================================================================== --- src/Libraries/Hyena/Makefile.am (revision 3360) +++ src/Libraries/Hyena/Makefile.am (working copy) @@ -43,6 +43,7 @@ Hyena.Query/QueryLimit.cs \ Hyena.Query/QueryListNode.cs \ Hyena.Query/QueryNode.cs \ + Hyena.Query/QueryNodeSearchMethod.cs \ Hyena.Query/QueryOperator.cs \ Hyena.Query/QueryOrder.cs \ Hyena.Query/QueryParser.cs \
Attachment:
patch
Description: Binary data