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 async void perform_search (LensSearch search,
33
SearchType search_type,
34
owned List<FilterParser> filters,
36
Cancellable? cancellable = null);
42
protected void initialize ()
44
/* Listen for filter changes */
45
scope.filters_changed.connect (() => {
46
scope.queue_search_changed (SearchType.DEFAULT);
49
scope.active_sources_changed.connect (() => {
50
scope.queue_search_changed (SearchType.DEFAULT);
53
/* No need to search if only the whitespace changes */
54
scope.generate_search_key.connect ((lens_search) => {
55
return lens_search.search_string.strip ();
58
/* Listen for changes to the lens search entry */
59
scope.search_changed.connect ((search, search_type, cancellable) => {
60
update_search_async.begin (search, search_type, cancellable);
64
private async void update_search_async (LensSearch search,
65
SearchType search_type,
66
Cancellable cancellable)
70
* results for a global search from just hitting Super.
71
* Here we want to return a smaller number of results with 0 filters.
73
if (search_type == SearchType.GLOBAL)
74
max_results = num_results_global_search;
75
else if (is_search_empty (search))
76
max_results = num_results_without_search;
78
max_results = num_results_lens_search;
80
yield prepare_search (search, search_type, max_results, cancellable);
83
private async void prepare_search (LensSearch search,
84
SearchType search_type,
86
Cancellable cancellable)
88
var results_model = search.results_model;
90
List<FilterParser> filters = new List<FilterParser> ();
91
// don't apply filters to global searches
92
if (search_type != SearchType.GLOBAL)
94
Filter filter = scope.get_filter ("genre");
96
filters.append (new GenreFilterParser (filter as CheckOptionFilterCompact));
98
filter = scope.get_filter ("decade");
100
filters.append (new DecadeFilterParser (filter as MultiRangeFilter));
103
results_model.clear ();
105
// don't perform search is all sources are inactive
106
if (scope.sources.options.length () > 0 && scope.sources.filtering)
108
bool any_active = false;
109
foreach (var source in scope.sources.options)
111
if (source.active) any_active = true;
120
yield perform_search (search, search_type, (owned) filters, max_results, cancellable);
122
if (results_model.get_n_rows () == 0)
124
search.set_reply_hint ("no-results-hint",
125
_("Sorry, there is no music that matches your search."));
131
protected bool is_search_empty (LensSearch search)
132
requires (search.search_string != null)
134
return search.search_string.strip () == "";