2
* Copyright (C) 2011 Canonical Ltd
4
* This program is free software: you can redistribute it and/or modify
5
* it under the terms of the GNU General Public License version 3 as
6
* published by the Free Software Foundation.
8
* This program is distributed in the hope that it will be useful,
9
* but WITHOUT ANY WARRANTY; without even the implied warranty of
10
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
* GNU General Public License for more details.
13
* You should have received a copy of the GNU General Public License
14
* along with this program. If not, see <http://www.gnu.org/licenses/>.
16
* Authored by Alex Launi <alex.launi@canonical.com>
22
namespace Unity.MusicLens
24
public abstract class SimpleScope : Object
26
public Unity.Scope Scope { get; protected set; }
28
protected abstract int num_results_without_search { get; }
29
protected abstract int num_results_global_search { get; }
30
protected abstract int num_results_lens_search { get; }
32
protected abstract void perform_search (LensSearch? search, Dee.Model results_model, List<Filter> filters, int max_results = -1);
34
protected bool is_dirty;
35
protected LensSearch? previous_search;
40
previous_search = null;
42
/* Listen for filter changes */
43
Scope.filters_changed.connect(
45
if (Scope.active_search != null)
48
Scope.notify_property ("active-search");
53
/* Listen for changes to the lens search entry */
54
Scope.notify["active-search"].connect (
56
var search = Scope.active_search;
57
if (!(is_dirty || search_has_really_changed (previous_search, search)))
60
update_search_async.begin (search);
61
previous_search = search;
65
/* Listen for changes to the global search */
66
Scope.notify["active-global-search"].connect (
68
var search = Scope.active_global_search;
70
if (search_is_invalid (search))
73
if (!search_has_really_changed (previous_search, search))
76
update_global_search_async.begin (search);
77
previous_search = search;
82
/* based on the filters(?) get the default results to show when the music lens is
83
* just opened without filters, like when someone hits Super+M
85
private async void update_without_search_async (LensSearch search)
87
perform_search_async.begin (search, num_results_without_search);
91
* results for a global search from just hitting Super. here we want to return
92
* a smaller number of results with 0 filters.
94
private async void update_global_search_async (LensSearch search)
96
var results_model = Scope.global_results_model;
98
if (search_is_invalid (search))
100
results_model.clear ();
104
perform_search_async.begin (search, num_results_global_search);
107
private async void update_search_async (LensSearch search)
109
// just pretend like there's no search
110
if (search_is_invalid (search))
112
update_without_search_async.begin (search);
116
perform_search_async.begin (search, num_results_lens_search);
119
private async void perform_search_async (LensSearch search, int max_results)
121
var results_model = Scope.results_model;
123
/* Prevent concurrent searches and concurrent updates of our models,
124
* by preventing any notify signals from propagating to us.
125
* Important: Remeber to thaw the notifys again! */
126
List<Filter> filters = new List<Filter> ();
127
Filter filter = Scope.get_filter ("genre");
128
if (filter.filtering)
129
filters.append (filter);
131
filter = Scope.get_filter ("decade");
132
if (filter.filtering)
133
filters.append (filter);
135
Scope.freeze_notify ();
137
results_model.clear ();
138
perform_search (search, results_model, filters, max_results);
140
/* Allow new searches once we enter an idle again.
141
* We don't do it directly from here as that could mean we start
142
* changing the model even before we had flushed out current changes
145
Scope.thaw_notify ();
152
private bool search_is_invalid (LensSearch? search)
154
/* This boolean expression is unfolded as we seem to get
155
* some null dereference if we join them in a big || expression */
158
else if (search.search_string == null)
161
return search.search_string.strip() == "";
164
private bool search_has_really_changed (LensSearch? old_search,
165
LensSearch? new_search)
167
if (old_search == null && new_search == null)
172
if (old_search == null)
174
s1 = new_search.search_string;
175
if (s1 == null || s1.strip() == "")
180
else if (new_search == null)
182
s2 = old_search.search_string;
183
if (s2 == null || s2.strip() == "")
190
s1 = new_search.search_string;
191
s2 = old_search.search_string;
194
if (s2 == null || s2.strip() == "")
201
if (s1 == null || s1.strip() == "")
207
return s1.strip () != s2.strip ();