~kelemeng/banshee/bug743928

« back to all changes in this revision

Viewing changes to .pc/91_u1ms-initialize-earlier.patch/src/Extensions/Banshee.UbuntuOneMusicStore/Banshee.UbuntuOneMusicStore/UbuntuOneMusicStoreSource.cs

  • Committer: Bazaar Package Importer
  • Author(s): Chow Loong Jin
  • Date: 2011-05-14 22:25:36 UTC
  • mfrom: (6.3.15 experimental)
  • Revision ID: james.westby@ubuntu.com-20110514222536-u1x7ikxdqkmfvyuz
Tags: 2.1.0-1ubuntu1
* [2396c18] Merge from Debian Unstable, remaining changes:
  + Enable SoundMenu and Disable NotificationArea by default
  + Disable boo and karma extensions
  + Enable and recommnd u1ms and soundmenu extensions
  + Move desktop file for Meego UI to /usr/share/une/applications
  + Change the url for the Amazon store redirector
  + Create the U1MS widget earlier and bump libu1 requirement
* [9d7c600] Drop upstreamed u1ms-initialize-earlier patch

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
//
2
 
// UbuntuOneMusicStoreSource.cs
3
 
//
4
 
// Authors:
5
 
//   Jo Shields <directhex@apebox.org>
6
 
//   Rodney Dawes <rodney.dawes@canonical.com>
7
 
//
8
 
// Copyright (C) 2010 Jo Shields
9
 
// Copyright (C) 2011 Canonical, Ltd.
10
 
//
11
 
// Permission is hereby granted, free of charge, to any person obtaining
12
 
// a copy of this software and associated documentation files (the
13
 
// "Software"), to deal in the Software without restriction, including
14
 
// without limitation the rights to use, copy, modify, merge, publish,
15
 
// distribute, sublicense, and/or sell copies of the Software, and to
16
 
// permit persons to whom the Software is furnished to do so, subject to
17
 
// the following conditions:
18
 
//
19
 
// The above copyright notice and this permission notice shall be
20
 
// included in all copies or substantial portions of the Software.
21
 
//
22
 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23
 
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24
 
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25
 
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26
 
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27
 
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28
 
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29
 
//
30
 
 
31
 
using Mono.Unix;
32
 
using Gdk;
33
 
using System;
34
 
 
35
 
using Hyena;
36
 
 
37
 
using Banshee.Collection;
38
 
using Banshee.Gui;
39
 
using Banshee.ServiceStack;
40
 
using Banshee.Sources;
41
 
using Banshee.Sources.Gui;
42
 
 
43
 
namespace Banshee.UbuntuOneMusicStore
44
 
{
45
 
    public class UbuntuOneMusicStoreSource : Source
46
 
    {
47
 
        // In the sources TreeView, sets the order value for this source, small on top
48
 
        const int sort_order = 190;
49
 
        CustomView custom_view;
50
 
 
51
 
        public UbuntuOneMusicStoreSource () : base (
52
 
            Catalog.GetString ("Ubuntu One Music Store"),
53
 
            Catalog.GetString ("Ubuntu One Music Store"),
54
 
            sort_order, "ubuntu-one-music-store")
55
 
        {
56
 
            Properties.SetString ("Icon.Name", "ubuntuone");
57
 
 
58
 
            // So we can handle u1ms:// URIs
59
 
            ServiceManager.Get<DBusCommandService> ().ArgumentPushed += OnCommandLineArgument;
60
 
        }
61
 
 
62
 
        ~UbuntuOneMusicStoreSource ()
63
 
        {
64
 
            ServiceManager.Get<DBusCommandService> ().ArgumentPushed -= OnCommandLineArgument;
65
 
        }
66
 
 
67
 
        private void OnCommandLineArgument (string uri, object value, bool isFile)
68
 
        {
69
 
            if (!isFile || String.IsNullOrEmpty (uri)) {
70
 
                return;
71
 
            }
72
 
 
73
 
            Log.Debug ("U1MS: URI requested: ", uri);
74
 
            // Handle u1ms:// URIs
75
 
            if (uri.StartsWith ("u1ms://")) {
76
 
                string http_url = uri.Replace ("u1ms://", "http://");
77
 
                custom_view.Store.LoadStoreLink (http_url);
78
 
                ServiceManager.SourceManager.SetActiveSource (this);
79
 
            }
80
 
        }
81
 
 
82
 
        // A count of 0 will be hidden in the source TreeView
83
 
        public override int Count {
84
 
            get { return 0; }
85
 
        }
86
 
 
87
 
        // Defer any UI creation until it's actually needed.
88
 
        public override void Activate ()
89
 
        {
90
 
            if (custom_view == null) {
91
 
                Properties.Set<ISourceContents> ("Nereid.SourceContents", custom_view = new CustomView ());
92
 
            }
93
 
 
94
 
            base.Activate ();
95
 
            Log.Debug ("U1MS: Initialized");
96
 
        }
97
 
 
98
 
        public class StoreWrapper: UbuntuOne.U1MusicStore, IDisableKeybindings
99
 
        {
100
 
            string U1LibraryLocation = System.IO.Path.Combine (System.IO.Path.Combine (System.Environment.GetFolderPath (System.Environment.SpecialFolder.Personal), ".ubuntuone"), "Purchased from Ubuntu One");
101
 
 
102
 
            public StoreWrapper (): base ()
103
 
            {
104
 
                this.PreviewMp3 += PlayMP3Preview;
105
 
                this.DownloadFinished += AddDownloadToLibrary;
106
 
                this.PlayLibrary += PlayU1MSLibrary;
107
 
                this.UrlLoaded += U1MSUrlLoaded;
108
 
            }
109
 
 
110
 
            private void PlayMP3Preview (object Sender, UbuntuOne.PreviewMp3Args a)
111
 
            {
112
 
                Log.Debug ("U1MS: Playing preview: ", a.Url );
113
 
                TrackInfo PreviewTrack = new TrackInfo ();
114
 
                PreviewTrack.TrackTitle = a.Title;
115
 
                PreviewTrack.ArtistName = Catalog.GetString ("Track Preview");
116
 
                PreviewTrack.AlbumTitle = Catalog.GetString ("Ubuntu One Music Store");
117
 
                PreviewTrack.Uri = new SafeUri (a.Url);
118
 
                ServiceManager.PlayerEngine.OpenPlay (PreviewTrack);
119
 
                ServiceManager.PlaybackController.StopWhenFinished = true;
120
 
            }
121
 
 
122
 
            private void AddDownloadToLibrary (object Sender, UbuntuOne.DownloadFinishedArgs a)
123
 
            {
124
 
                Log.Debug ("U1MS: Track downloaded: ", a.Path);
125
 
                ServiceManager.Get<Banshee.Library.LibraryImportManager> ().ImportTrack (new SafeUri (a.Path));
126
 
                ServiceManager.Get<Banshee.Library.LibraryImportManager> ().NotifyAllSources ();
127
 
            }
128
 
 
129
 
            private void PlayU1MSLibrary (object Sender, UbuntuOne.PlayLibraryArgs a)
130
 
            {
131
 
                Log.Debug ("U1MS: Playing from library: ", a.Path);
132
 
                Log.Debug ("U1MS: U1 library location: ", U1LibraryLocation);
133
 
                int track_id = Banshee.Collection.Database.DatabaseTrackInfo.GetTrackIdForUri (System.IO.Path.Combine (U1LibraryLocation, a.Path));
134
 
                if (track_id > 0)
135
 
                {
136
 
                    var track = Banshee.Collection.Database.DatabaseTrackInfo.Provider.FetchSingle (track_id);
137
 
                    ServiceManager.PlaybackController.NextSource = ServiceManager.SourceManager.MusicLibrary;
138
 
                    ServiceManager.PlayerEngine.OpenPlay (track);
139
 
                }
140
 
            }
141
 
 
142
 
            private void U1MSUrlLoaded (object Sender, UbuntuOne.UrlLoadedArgs a)
143
 
            {
144
 
                Log.Debug ("U1MS: Url Loaded: ", a.Url);
145
 
            }
146
 
        }
147
 
 
148
 
        private class CustomView : ISourceContents
149
 
        {
150
 
            StoreWrapper store = new StoreWrapper ();
151
 
 
152
 
            public bool SetSource (ISource source) { return true; }
153
 
            public void ResetSource () { }
154
 
            public Gtk.Widget Widget { get { return store; } }
155
 
            public ISource Source { get { return null; } }
156
 
            public UbuntuOne.U1MusicStore Store { get { return store; } }
157
 
        }
158
 
    }
159
 
}