~ubuntu-branches/ubuntu/precise/unity-lens-music/precise-proposed

« back to all changes in this revision

Viewing changes to src/filter-parser.vala

  • Committer: Bazaar Package Importer
  • Author(s): Didier Roche
  • Date: 2011-08-11 19:37:31 UTC
  • Revision ID: james.westby@ubuntu.com-20110811193731-0dym5igg8sfanj5z
Tags: upstream-0.1.0
ImportĀ upstreamĀ versionĀ 0.1.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (C) 2011 Canonical Ltd
 
3
 *
 
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.
 
7
 *
 
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.
 
12
 *
 
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/>.
 
15
 *
 
16
 * Authored by Alex Launi <alex.launi@canonical.com>
 
17
 *
 
18
 */
 
19
 
 
20
using GLib;
 
21
 
 
22
namespace Unity.MusicLens {
 
23
 
 
24
  public class FilterParser : Object
 
25
  {
 
26
    private Filter _filter;
 
27
 
 
28
    public string Id { get { return _filter.id; } }
 
29
 
 
30
    public FilterParser (Filter filter)
 
31
    {
 
32
      _filter = filter;
 
33
    }
 
34
 
 
35
    public string? parse ()
 
36
    {
 
37
      if (Id == "genre")
 
38
        return parse_genre_filter ();
 
39
      else if (Id == "decade")
 
40
        return parse_decade_filter ();
 
41
      
 
42
      return null;
 
43
    }
 
44
 
 
45
    /* returns a string like CoreTracks.Genre IS 'Rock' OR CoreTracks.Genre IS 'Metal'... */
 
46
    public string? parse_genre_filter ()
 
47
    {
 
48
      return "";
 
49
      StringBuilder builder = new StringBuilder ();
 
50
      List<FilterOption> genres = get_all_selected_genre_options ((_filter as CheckOptionFilter).options);
 
51
 
 
52
      /* CoreTracks.Genre IS ___ OR CoreTracks.Genre IS ____ ... */
 
53
      foreach (FilterOption genre in genres)
 
54
      {
 
55
        foreach (string alt in Genre.get_genre_synonyms (genre.id))
 
56
        {
 
57
          // fixme obviously
 
58
          builder.append ("CoreTracks.Genre IS ");
 
59
          builder.append (alt);
 
60
          builder.append (" OR ");
 
61
        }
 
62
        builder.truncate (builder.len - (" OR ".length));
 
63
      }
 
64
 
 
65
      return builder.str;
 
66
    }
 
67
 
 
68
    /* returns a string like CoreTracks.Year >= LOWER_BOUND AND CoreTracks.Year < UPPER_BOUND */
 
69
    public string? parse_decade_filter ()
 
70
    {
 
71
      return "";
 
72
      int start_year, end_year;
 
73
 
 
74
      MultiRangeFilter range_filter = _filter as MultiRangeFilter;
 
75
      FilterOption start = range_filter.get_first_active ();
 
76
      FilterOption end = range_filter.get_last_active ();
 
77
 
 
78
      start_year = int.parse (start.id);
 
79
      end_year = int.parse (end.id);
 
80
      
 
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)
 
85
        end_year += 10;
 
86
      else
 
87
        end_year = int.parse (range_filter.options.nth_data (end_index + 1).id);
 
88
 
 
89
      return "CoreTracks.Year >= %d AND CoreTracks.Year < %d ".printf (start_year, end_year);
 
90
    }
 
91
 
 
92
    private List<FilterOption> get_all_selected_genre_options (List<FilterOption> options)
 
93
    {
 
94
      var active = new List<FilterOption> ();
 
95
      foreach (FilterOption option in options)
 
96
      {
 
97
        if (option.active)
 
98
          active.append (option);
 
99
      }
 
100
 
 
101
      return active;
 
102
    }
 
103
  }
 
104
}