5
// Aaron Bockover <abockover@novell.com>
6
// Gabriel Burt <gburt@novell.com>
8
// Copyright 2010 Novell, Inc.
10
// Permission is hereby granted, free of charge, to any person obtaining a copy
11
// of this software and associated documentation files (the "Software"), to deal
12
// in the Software without restriction, including without limitation the rights
13
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14
// copies of the Software, and to permit persons to whom the Software is
15
// furnished to do so, subject to the following conditions:
17
// The above copyright notice and this permission notice shall be included in
18
// all copies or substantial portions of the Software.
20
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
33
using Hyena.Downloader;
37
using Banshee.WebBrowser;
38
using Banshee.WebSource;
39
using Banshee.AmazonMp3;
41
namespace Banshee.AmazonMp3.Store
43
public class StoreView : WebView
45
// We ask that no one change this redirect URL. ALL (100%) revenue
46
// generated by this Banshee Amazon integration is sent directly to the
47
// non-profit GNOME Foundation.
48
public const string REDIRECT_URL = "http://integrated-services.banshee.fm/amz/redirect.do/";
50
private static string [] domains = new [] {
61
private static bool IsAmzContentType (string contentType)
63
switch (contentType) {
64
// The German store uses this mimetype, see bgo#625210
65
case "audio/x-amzaudio":
66
// The US and hopefully all other stores use this one
67
case "audio/x-amzxml":
74
public event EventHandler SignInChanged;
76
public bool IsSignedIn { get; private set; }
78
private string country;
79
public string Country {
80
get { return country ?? "geo"; }
81
set { country = value; }
87
FixupJavascriptUrl = "http://integrated-services.banshee.fm/amz/amz-fixups.js";
89
OssiferSession.CookieChanged += (o, n) => CheckSignIn ();
91
// Ensure that Amazon knows a valid downloader is available,
92
// otherwise the purchase experience is interrupted with a
93
// confusing message about downloading and installing software.
94
foreach (var domain in domains) {
95
OssiferSession.SetCookie ("dmusic_download_manager_enabled",
96
AmzMp3Downloader.AmazonMp3DownloaderCompatVersion,
97
".amazon." + domain, "/", TimeSpan.FromDays (365.2422));
100
Country = StoreSourcePreferences.StoreCountry.Get ();
106
protected override OssiferNavigationResponse OnMimeTypePolicyDecisionRequested (string mimetype)
108
// We only explicitly accept (render) text/html types, and only
109
// download what we can import or preview.
110
if (IsAmzContentType (mimetype) || mimetype == "audio/x-mpegurl") {
111
return OssiferNavigationResponse.Download;
114
return base.OnMimeTypePolicyDecisionRequested (mimetype);
117
protected override string OnDownloadRequested (string mimetype, string uri, string suggestedFilename)
119
if (IsAmzContentType (mimetype)) {
120
var dest_uri_base = "file://" + Paths.Combine (Paths.TempDir, suggestedFilename);
121
var dest_uri = new SafeUri (dest_uri_base);
122
for (int i = 1; File.Exists (dest_uri);
123
dest_uri = new SafeUri (String.Format ("{0} ({1})", dest_uri_base, ++i)));
124
return dest_uri.AbsoluteUri;
125
} else if (mimetype == "audio/x-mpegurl") {
126
Banshee.Streaming.RadioTrackInfo.OpenPlay (uri);
127
Banshee.ServiceStack.ServiceManager.PlaybackController.StopWhenFinished = true;
134
protected override void OnDownloadStatusChanged (OssiferDownloadStatus status, string mimetype, string destinationUri)
136
// FIXME: handle the error case
137
if (status != OssiferDownloadStatus.Finished) {
141
if (IsAmzContentType (mimetype)) {
142
Log.Debug ("OssiferWebView: downloaded purchase list", destinationUri);
143
Banshee.ServiceStack.ServiceManager.Get<AmazonMp3DownloaderService> ()
144
.DownloadAmz (new SafeUri (destinationUri).LocalPath);
148
public override void GoHome ()
150
LoadUri (GetActionUrl ("home/"));
153
public override void GoSearch (string query)
155
query = System.Uri.EscapeDataString (query);
156
LoadUri (new Uri (GetActionUrl ("search/" + query)).AbsoluteUri);
159
public void SignOut ()
161
// Shouldn't just clear these cookies; going to Amazon's signout page is more secure,
162
// since it will invalidate the session itself.
163
/*foreach (var name in new [] { "at-main", "x-main", "session-id",
164
"session-id-time", "session-token", "uidb-main", "pf"}) {
165
foreach (var domain in domains) {
166
OssiferSession.DeleteCookie (name, ".amazon." + domain, "/");
170
LoadUri (GetActionUrl ("sign_out/"));
173
private void CheckSignIn ()
175
var signed_in = false;
176
foreach (var domain in domains) {
177
signed_in |= OssiferSession.GetCookie ("at-main", ".amazon." + domain, "/") != null;
178
signed_in |= OssiferSession.GetCookie ("at-acbuk", ".amazon." + domain, "/") != null;
181
if (IsSignedIn != signed_in) {
182
IsSignedIn = signed_in;
187
protected virtual void OnSignInChanged ()
189
var handler = SignInChanged;
190
if (handler != null) {
191
handler (this, EventArgs.Empty);
195
public string GetActionUrl (string action)
197
return String.Concat (StoreSourcePreferences.RedirectUrl.Get (), Country, "/", action);