3
// GNOME Do is the legal property of its developers. Please refer to the
4
// COPYRIGHT file distributed with this source distribution.
6
// This program is free software: you can redistribute it and/or modify
7
// it under the terms of the GNU General Public License as published by
8
// the Free Software Foundation, either version 3 of the License, or
9
// (at your option) any later version.
11
// This program is distributed in the hope that it will be useful,
12
// but WITHOUT ANY WARRANTY; without even the implied warranty of
13
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
// GNU General Public License for more details.
16
// You should have received a copy of the GNU General Public License
17
// along with this program. If not, see <http://www.gnu.org/licenses/>.
21
using System.Collections.Generic;
29
public abstract class SimpleSearchController : ISearchController
31
protected bool textMode = false;
33
protected SimpleSearchContext context;
34
protected Type[] searchFilter;
35
protected Type[] defaultFilter = Type.EmptyTypes;
36
protected const int Timeout = 300;
38
public IUIContext UIContext {
40
return context.GetIUIContext (TextMode);
44
public IObject[] Results {
46
return context.Results;
49
context.Results = value;
50
OnSelectionChanged ();
54
public IObject[] FullSelection {
56
return context.FullSelection;
60
public virtual IObject Selection {
62
return context.Selection;
68
return context.Cursor;
71
IObject tmp = Selection;
72
int ctmp = context.Cursor;
73
context.Cursor = value;
74
if (tmp != Selection || context.Cursor != ctmp) {
76
OnSelectionChanged ();
82
public int[] SecondaryCursors {
84
return context.SecondaryCursorsToIntArray ();
88
public virtual bool TextMode {
95
public bool DefaultFilter {
97
return (SearchTypes == defaultFilter);
101
public abstract Type[] SearchTypes {
105
public string Query {
107
return context.Query;
111
protected SimpleSearchController ()
113
context = new SimpleSearchContext ();
114
searchFilter = defaultFilter;
117
public void AddChar (char character)
119
context.LastContext = (SimpleSearchContext) context.Clone ();
120
context.Query += character;
127
protected abstract void UpdateResults ();
129
protected virtual List<IObject> InitialResults ()
131
//We continue off our previous results if possible
132
if (context.LastContext != null &&
133
context.LastContext.Results.Length != 0) {
134
return new List<IObject> (Do.UniverseManager.
135
Search (context.Query, SearchTypes,
136
context.LastContext.Results));
137
} else if (context.ParentContext != null) {
138
//If we have a parent context we NEVER do a full search. Just return
139
//the results as they are.
140
return new List<IObject> (context.Results);
142
//else we do things the slow way
143
return new List<IObject> (Do.UniverseManager.
144
Search (context.Query, SearchTypes));
148
public virtual void DeleteChar ()
150
if (context.LastContext == null) {
154
IObject tmp = context.Selection;
155
context = context.LastContext;
158
if (tmp != context.Selection)
163
public virtual bool ToggleSecondaryCursor (int cursorLocation)
165
if (Results.Length - 1 < cursorLocation) return false;
167
List<IObject> secondary;
168
secondary = new List<IObject> (context.SecondaryCursors);
170
IObject newObject = Results[cursorLocation];
172
if (secondary.Contains (newObject))
173
secondary.Remove (newObject);
174
else if (newObject is IItem)
175
secondary.Add (newObject);
179
context.SecondaryCursors = secondary.ToArray ();
184
public bool ItemChildSearch ()
186
if (context.Selection is IAction)
189
IItem item = context.Selection as IItem;
190
List<IObject> children = new List<IObject> ();
192
foreach (DoItemSource source in PluginManager.GetItemSources ()) {
193
foreach (IObject child in source.ChildrenOfItem (item))
194
children.Add (child);
197
if (children.Count == 0)
200
SimpleSearchContext newContext = new SimpleSearchContext ();
201
newContext.ParentContext = context;
202
context = newContext;
204
context.Results = Do.UniverseManager.Search (Query, defaultFilter, children);
205
OnSelectionChanged ();
209
public bool ItemParentSearch ()
211
if (context.ParentContext == null) return false;
213
SimpleSearchContext parent = context.ParentContext;
214
context.Destroy (true);
216
OnSelectionChanged ();
220
public virtual void Reset ()
222
searchFilter = defaultFilter;
224
context = new SimpleSearchContext ();
227
protected void OnSelectionChanged ()
232
protected void OnSearchStarted (bool upstream_search)
234
SearchStarted (upstream_search);
237
protected void OnSearchFinished (bool selection_changed)
239
SearchFinished (selection_changed);
242
protected void OnQueryChanged ()
244
if (QueryChanged != null)
248
public event NullEventHandler SelectionChanged;
249
public event NullEventHandler QueryChanged;
250
public event SearchStartedEventHandler SearchStarted;
251
public event SearchFinishedEventHandler SearchFinished;