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 class FilterParser : Object
26
private Filter _filter;
28
public string Id { get { return _filter.id; } }
30
public FilterParser (Filter filter)
35
public string? parse ()
38
return parse_genre_filter ();
39
else if (Id == "decade")
40
return parse_decade_filter ();
45
/* returns a string like CoreTracks.Genre IS 'Rock' OR CoreTracks.Genre IS 'Metal'... */
46
public string? parse_genre_filter ()
49
StringBuilder builder = new StringBuilder ();
50
List<FilterOption> genres = get_all_selected_genre_options ((_filter as CheckOptionFilter).options);
52
/* CoreTracks.Genre IS ___ OR CoreTracks.Genre IS ____ ... */
53
foreach (FilterOption genre in genres)
55
foreach (string alt in Genre.get_genre_synonyms (genre.id))
58
builder.append ("CoreTracks.Genre IS ");
60
builder.append (" OR ");
62
builder.truncate (builder.len - (" OR ".length));
68
/* returns a string like CoreTracks.Year >= LOWER_BOUND AND CoreTracks.Year < UPPER_BOUND */
69
public string? parse_decade_filter ()
72
int start_year, end_year;
74
MultiRangeFilter range_filter = _filter as MultiRangeFilter;
75
FilterOption start = range_filter.get_first_active ();
76
FilterOption end = range_filter.get_last_active ();
78
start_year = int.parse (start.id);
79
end_year = int.parse (end.id);
81
// bump the end_year up to the beginning of the next range
82
int end_index = range_filter.options.index (end);
83
// if we have already selected the last option
84
if (end_index == range_filter.options.length () - 1)
87
end_year = int.parse (range_filter.options.nth_data (end_index + 1).id);
89
return "CoreTracks.Year >= %d AND CoreTracks.Year < %d ".printf (start_year, end_year);
92
private List<FilterOption> get_all_selected_genre_options (List<FilterOption> options)
94
var active = new List<FilterOption> ();
95
foreach (FilterOption option in options)
98
active.append (option);