~chipaca/unity-lens-music/dont-force-us

« back to all changes in this revision

Viewing changes to src/musicstore-scope.vala

  • Committer: Neil Jagdish Patel
  • Author(s): Alex Launi
  • Date: 2011-09-15 18:08:53 UTC
  • mfrom: (41.1.5 musicstore)
  • Revision ID: neil.patel@canonical.com-20110915180853-hc7natia0x01nwap
Merge in initial support for searching one.ubuntu.com for music to purchase

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 MusicStoreScopeProxy : SimpleScope
 
25
  {
 
26
    private MusicStoreCollection collection;
 
27
 
 
28
    public MusicStoreScopeProxy ()
 
29
    {
 
30
      base ();
 
31
 
 
32
      scope = new Unity.Scope ("/com/canonical/unity/scope/musicstore");
 
33
      scope.search_in_global = true;
 
34
      scope.activate_uri.connect (activate);
 
35
 
 
36
      base.initialize ();
 
37
 
 
38
      collection = new MusicStoreCollection ();
 
39
 
 
40
      try {
 
41
        scope.export ();
 
42
      } catch (GLib.IOError e) {
 
43
        stdout.printf (e.message);
 
44
      }
 
45
    }
 
46
 
 
47
    protected override int num_results_without_search { get { return 100; } }
 
48
    protected override int num_results_global_search { get { return 20; } }
 
49
    protected override int num_results_lens_search { get { return 50; } }
 
50
 
 
51
    public Unity.ActivationResponse activate (string uri)
 
52
    {
 
53
      /* launch the music store streaming client  or the webstore or whatevz */
 
54
        File file = File.new_for_uri (uri);
 
55
        get_and_open_purchase_url.begin (file);
 
56
        return new Unity.ActivationResponse (Unity.HandledType.HIDE_DASH);  
 
57
    }
 
58
 
 
59
    public override void perform_search (LensSearch? search, Dee.Model results_model,
 
60
                                         List<FilterParser> filters, int max_results = -1)
 
61
    {
 
62
      try {
 
63
        collection.search (search, results_model, filters, max_results);          
 
64
      } catch (IOError e) {
 
65
        stdout.printf (e.message);
 
66
      }
 
67
    }
 
68
 
 
69
    private async void get_and_open_purchase_url (File file)
 
70
    {
 
71
      string url = "UNASSIGNED";
 
72
 
 
73
      try {
 
74
        var dis = new DataInputStream (file.read ());
 
75
        var parser = new Json.Parser ();
 
76
        parser.load_from_stream (dis, new Cancellable ());
 
77
        var root_object = parser.get_root ().get_object ();
 
78
 
 
79
        url = root_object.get_string_member ("web_purchase_url");
 
80
        AppInfo.launch_default_for_uri (url, null);
 
81
      } catch (GLib.Error e) {
 
82
        warning ("Failed to open url %s. %s", url, e.message);
 
83
      }
 
84
    }
 
85
  }
 
86
}
 
 
b'\\ No newline at end of file'