~ubuntu-branches/ubuntu/trusty/unity-china-music-scope/trusty

« back to all changes in this revision

Viewing changes to src/simple-scope.vala

  • Committer: Package Import Robot
  • Author(s): whzhang-kylin
  • Date: 2012-12-13 09:29:34 UTC
  • Revision ID: package-import@ubuntu.com-20121213092934-0z4qywr7i1fp6nsy
Tags: upstream-1.0.0
ImportĀ upstreamĀ versionĀ 1.0.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 abstract class SimpleScope : Object
 
25
  {
 
26
    public Unity.Scope scope { get; protected set; }
 
27
 
 
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; }
 
31
 
 
32
    protected abstract async void perform_search (LensSearch search,
 
33
                                                  SearchType search_type,
 
34
                                                  owned List<FilterParser> filters,
 
35
                                                  int max_results = -1,
 
36
                                                  Cancellable? cancellable = null);
 
37
 
 
38
    public SimpleScope ()
 
39
    {
 
40
    }
 
41
 
 
42
    protected void initialize ()
 
43
    {
 
44
      /* Listen for filter changes */
 
45
      scope.filters_changed.connect (() => {
 
46
        scope.queue_search_changed (SearchType.DEFAULT);
 
47
      });
 
48
 
 
49
      scope.active_sources_changed.connect (() => {
 
50
        scope.queue_search_changed (SearchType.DEFAULT);
 
51
      });
 
52
 
 
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 ();
 
56
      });
 
57
 
 
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);
 
61
      });
 
62
    }
 
63
    
 
64
    private async void update_search_async (LensSearch search,
 
65
                                            SearchType search_type,
 
66
                                            Cancellable cancellable)
 
67
    {
 
68
      int max_results;
 
69
      /*
 
70
       * results for a global search from just hitting Super.
 
71
       * Here we want to return a smaller number of results with 0 filters.
 
72
       */
 
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;
 
77
      else
 
78
        max_results = num_results_lens_search;
 
79
 
 
80
      yield prepare_search (search, search_type, max_results, cancellable);
 
81
    }
 
82
 
 
83
    private async void prepare_search (LensSearch search,
 
84
                                       SearchType search_type,
 
85
                                       int max_results,
 
86
                                       Cancellable cancellable)
 
87
    {
 
88
      var results_model = search.results_model;
 
89
 
 
90
      List<FilterParser> filters = new List<FilterParser> ();
 
91
      // don't apply filters to global searches
 
92
      if (search_type != SearchType.GLOBAL)
 
93
      {
 
94
        Filter filter = scope.get_filter ("genre");
 
95
        if (filter.filtering)
 
96
          filters.append (new GenreFilterParser (filter as CheckOptionFilterCompact));
 
97
 
 
98
        filter = scope.get_filter ("decade");
 
99
        if (filter.filtering)
 
100
          filters.append (new DecadeFilterParser (filter as MultiRangeFilter));
 
101
      }
 
102
 
 
103
      results_model.clear ();
 
104
 
 
105
      // don't perform search is all sources are inactive
 
106
      if (scope.sources.options.length () > 0 && scope.sources.filtering)
 
107
      {
 
108
        bool any_active = false;
 
109
        foreach (var source in scope.sources.options)
 
110
        {
 
111
          if (source.active) any_active = true;
 
112
        }
 
113
        if (!any_active)
 
114
        {
 
115
          search.finished ();
 
116
          return;
 
117
        }
 
118
      }
 
119
 
 
120
      yield perform_search (search, search_type, (owned) filters, max_results, cancellable);
 
121
 
 
122
      if (results_model.get_n_rows () == 0)
 
123
      {
 
124
        search.set_reply_hint ("no-results-hint",
 
125
          _("Sorry, there is no music that matches your search."));
 
126
      }
 
127
 
 
128
      search.finished ();
 
129
    }
 
130
 
 
131
    protected bool is_search_empty (LensSearch search)
 
132
      requires (search.search_string != null)
 
133
    {
 
134
      return search.search_string.strip () == "";
 
135
    }
 
136
  }
 
137
}