1
/* SimpleSearchContext.cs
3
* GNOME Do is the legal property of its developers. Please refer to the
4
* COPYRIGHT file distributed with this
7
* This program is free software: you can redistribute it and/or modify
8
* it under the terms of the GNU General Public License as published by
9
* the Free Software Foundation, either version 3 of the License, or
10
* (at your option) any later version.
12
* This program is distributed in the hope that it will be useful,
13
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
* GNU General Public License for more details.
17
* You should have received a copy of the GNU General Public License
18
* along with this program. If not, see <http://www.gnu.org/licenses/>.
22
using System.Collections.Generic;
29
public class SimpleSearchContext : ICloneable
35
public SimpleSearchContext ()
37
SecondaryCursors = new IObject[0];
39
results = new IObject[0];
42
public SimpleSearchContext LastContext { get; set; }
43
public SimpleSearchContext ParentContext { get; set; }
44
public IObject[] SecondaryCursors { get; set; }
48
get { return query ?? query = string.Empty; }
49
set { query = value; }
52
public IObject[] Results
54
get { return results ?? results = new IObject[0]; }
56
results = value ?? new IObject[0];
60
if (SecondaryCursors.Length == 0) return;
62
List<IObject> secondary = new List<IObject> ();
63
foreach (IObject obj in SecondaryCursors) {
64
foreach (IObject robj in Results) {
71
SecondaryCursors = secondary.ToArray ();
75
public IObject Selection
79
return results[cursor];
86
public IObject[] FullSelection
89
List<IObject> outList = new List<IObject> ();
90
outList.AddRange (SecondaryCursors);
92
//Juggle our selection to front to give best possible legacy plugin support. Ideally this
94
outList.Remove (Selection);
95
outList.Insert (0, Selection);
97
return outList.ToArray ();
103
get { return cursor; }
105
if (value > Results.Length - 1)
106
cursor = Results.Length - 1;
107
else if ( value <= 0 )
115
/// Creates a new IUIContext from the current context. Contexts however are not away if
116
/// the controller wishes for them to be displayed in Large Text Mode, so this value must
117
/// be passed in. Parent contexts are always assumed to not use large text mode.
119
/// <param name="textMode">
120
/// A <see cref="System.Boolean"/>
123
/// A <see cref="IUIContext"/>
125
public IUIContext GetIUIContext (bool textMode)
127
if (ParentContext == null)
128
return new UIContext (Selection, Results, Cursor, SecondaryCursorsToIntArray (),
129
Query, textMode, null);
131
return new UIContext (Selection, Results, Cursor, SecondaryCursorsToIntArray (),
132
Query, textMode, ParentContext.GetIUIContext (false));
136
/// It is very convinient to convert to an int array.
139
/// A <see cref="System.Int32"/>
141
public int[] SecondaryCursorsToIntArray ()
143
if (SecondaryCursors.Length == 0)
145
List<int> cursors = new List<int> ();
147
foreach (IObject obj in SecondaryCursors) {
148
for (int i = 0; i < Results.Length; i++) {
149
if (Results[i] == obj)
154
return cursors.ToArray ();
157
public object Clone ()
159
SimpleSearchContext clone;
161
clone = new SimpleSearchContext ();
163
clone.LastContext = LastContext;
164
clone.ParentContext = ParentContext;
165
clone.Cursor = Cursor;
166
clone.SecondaryCursors = SecondaryCursors; //Cloning these makes no sense
167
clone.Results = results.Clone () as IObject[];
171
public void Destroy (bool preserveParent)
173
if (preserveParent) {
174
if (LastContext != null)
175
LastContext.Destroy ();
182
public void Destroy ()
184
if (LastContext != null)
185
LastContext.Destroy ();
187
if (ParentContext != null)
188
ParentContext.Destroy ();
191
public override int GetHashCode ()
193
return base.GetHashCode ();
196
public override string ToString ()
198
return "SearchContext " + base.ToString () +
199
"\n\tQuery: \"" + query + "\"" +
200
"\n\tHas last context: " + (LastContext != null) +
201
"\n\tHas parent context: " + (ParentContext != null) +
202
"\n\tCursor: " + cursor +
203
"\n\tResults: " + results;