2
// UbuntuOneMusicStoreSource.cs
5
// Jo Shields <directhex@apebox.org>
6
// Rodney Dawes <rodney.dawes@canonical.com>
8
// Copyright (C) 2010 Jo Shields
9
// Copyright (C) 2011 Canonical, Ltd.
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:
19
// The above copyright notice and this permission notice shall be
20
// included in all copies or substantial portions of the Software.
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.
37
using Banshee.Collection;
39
using Banshee.ServiceStack;
40
using Banshee.Sources;
41
using Banshee.Sources.Gui;
43
namespace Banshee.UbuntuOneMusicStore
45
public class UbuntuOneMusicStoreSource : Source
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;
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")
56
Properties.SetString ("Icon.Name", "ubuntuone");
58
// So we can handle u1ms:// URIs
59
ServiceManager.Get<DBusCommandService> ().ArgumentPushed += OnCommandLineArgument;
62
~UbuntuOneMusicStoreSource ()
64
ServiceManager.Get<DBusCommandService> ().ArgumentPushed -= OnCommandLineArgument;
67
private void OnCommandLineArgument (string uri, object value, bool isFile)
69
if (!isFile || String.IsNullOrEmpty (uri)) {
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);
82
// A count of 0 will be hidden in the source TreeView
83
public override int Count {
87
// Defer any UI creation until it's actually needed.
88
public override void Activate ()
90
if (custom_view == null) {
91
Properties.Set<ISourceContents> ("Nereid.SourceContents", custom_view = new CustomView ());
95
Log.Debug ("U1MS: Initialized");
98
public class StoreWrapper: UbuntuOne.U1MusicStore, IDisableKeybindings
100
string U1LibraryLocation = System.IO.Path.Combine (System.IO.Path.Combine (System.Environment.GetFolderPath (System.Environment.SpecialFolder.Personal), ".ubuntuone"), "Purchased from Ubuntu One");
102
public StoreWrapper (): base ()
104
this.PreviewMp3 += PlayMP3Preview;
105
this.DownloadFinished += AddDownloadToLibrary;
106
this.PlayLibrary += PlayU1MSLibrary;
107
this.UrlLoaded += U1MSUrlLoaded;
110
private void PlayMP3Preview (object Sender, UbuntuOne.PreviewMp3Args a)
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;
122
private void AddDownloadToLibrary (object Sender, UbuntuOne.DownloadFinishedArgs a)
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 ();
129
private void PlayU1MSLibrary (object Sender, UbuntuOne.PlayLibraryArgs a)
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));
136
var track = Banshee.Collection.Database.DatabaseTrackInfo.Provider.FetchSingle (track_id);
137
ServiceManager.PlaybackController.NextSource = ServiceManager.SourceManager.MusicLibrary;
138
ServiceManager.PlayerEngine.OpenPlay (track);
142
private void U1MSUrlLoaded (object Sender, UbuntuOne.UrlLoadedArgs a)
144
Log.Debug ("U1MS: Url Loaded: ", a.Url);
148
private class CustomView : ISourceContents
150
StoreWrapper store = new StoreWrapper ();
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; } }