~kelemeng/banshee/bug743928

« back to all changes in this revision

Viewing changes to .pc/0001-Add-support-for-u1ms-links.patch/src/Extensions/Banshee.UbuntuOneMusicStore/Banshee.UbuntuOneMusicStore/UbuntuOneMusicStoreSource.cs

  • Committer: Bazaar Package Importer
  • Author(s): Rodney Dawes
  • Date: 2011-02-28 14:06:16 UTC
  • Revision ID: james.westby@ubuntu.com-20110228140616-qkdvtz1ywphu3bbi
Tags: 1.9.4-1ubuntu2
* 0001-Add-support-for-u1ms-links.patch:
  - Handle u1ms:// links in the U1 Music Store (LP: #723960)

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