~ubuntu-branches/ubuntu/quantal/banshee/quantal

« back to all changes in this revision

Viewing changes to .pc/0014-Change-Amazon-redirect-url.patch/src/Extensions/Banshee.AmazonMp3.Store/Banshee.AmazonMp3.Store/StoreView.cs

  • Committer: Package Import Robot
  • Author(s): Chow Loong Jin
  • Date: 2012-04-20 23:34:40 UTC
  • mfrom: (6.4.23 sid)
  • Revision ID: package-import@ubuntu.com-20120420233440-g8br3wfrtxakwn5u
Tags: 2.4.0-2ubuntu1
* [980ad3d] Merge from Debian Unstable, remaining changes:
  + Enable and recommend SoundMenu and Disable NotificationArea by default
  + Disable boo and karma extensions
  + Move desktop file for Meego UI to /usr/share/une/applications
  + Change the url for the Amazon store redirector
  * [9b356d6] Add workaround for set_Height exception.
* [57afffa] Reexport patches via gbp-pq.
  The numbering of the Ubuntu specific patches have been upset due to the new
  patches imported into the previous Debian revision.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
//
2
 
// StoreView.cs
3
 
//
4
 
// Authors:
5
 
//   Aaron Bockover <abockover@novell.com>
6
 
//   Gabriel Burt <gburt@novell.com>
7
 
//
8
 
// Copyright 2010 Novell, Inc.
9
 
//
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:
16
 
// 
17
 
// The above copyright notice and this permission notice shall be included in
18
 
// all copies or substantial portions of the Software.
19
 
// 
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
26
 
// THE SOFTWARE.
27
 
 
28
 
using System;
29
 
 
30
 
using Gtk;
31
 
 
32
 
using Hyena;
33
 
using Hyena.Downloader;
34
 
 
35
 
using Banshee.Base;
36
 
using Banshee.IO;
37
 
using Banshee.WebBrowser;
38
 
using Banshee.WebSource;
39
 
using Banshee.AmazonMp3;
40
 
 
41
 
namespace Banshee.AmazonMp3.Store
42
 
{
43
 
    public class StoreView : WebView
44
 
    {
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/";
49
 
 
50
 
        private static string [] domains = new [] {
51
 
            "com",
52
 
            "co.uk",
53
 
            "co.jp",
54
 
            "de",
55
 
            "fr",
56
 
            "ca",
57
 
            "cn",
58
 
            "it"
59
 
        };
60
 
 
61
 
        private static bool IsAmzContentType (string contentType)
62
 
        {
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":
68
 
                    return true;
69
 
            }
70
 
 
71
 
            return false;
72
 
        }
73
 
 
74
 
        public event EventHandler SignInChanged;
75
 
 
76
 
        public bool IsSignedIn { get; private set; }
77
 
 
78
 
        private string country;
79
 
        public string Country {
80
 
            get { return country ?? "geo"; }
81
 
            set { country = value; }
82
 
        }
83
 
 
84
 
        public StoreView ()
85
 
        {
86
 
            CanSearch = true;
87
 
            FixupJavascriptUrl = "http://integrated-services.banshee.fm/amz/amz-fixups.js";
88
 
 
89
 
            OssiferSession.CookieChanged += (o, n) => CheckSignIn ();
90
 
 
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));
98
 
            }
99
 
 
100
 
            Country = StoreSourcePreferences.StoreCountry.Get ();
101
 
 
102
 
            CheckSignIn ();
103
 
            FullReload ();
104
 
        }
105
 
 
106
 
        protected override OssiferNavigationResponse OnMimeTypePolicyDecisionRequested (string mimetype)
107
 
        {
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;
112
 
            }
113
 
 
114
 
            return base.OnMimeTypePolicyDecisionRequested (mimetype);
115
 
        }
116
 
 
117
 
        protected override string OnDownloadRequested (string mimetype, string uri, string suggestedFilename)
118
 
        {
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;
128
 
                return null;
129
 
            }
130
 
 
131
 
            return null;
132
 
        }
133
 
 
134
 
        protected override void OnDownloadStatusChanged (OssiferDownloadStatus status, string mimetype, string destinationUri)
135
 
        {
136
 
            // FIXME: handle the error case
137
 
            if (status != OssiferDownloadStatus.Finished) {
138
 
                return;
139
 
            }
140
 
 
141
 
            if (IsAmzContentType (mimetype)) {
142
 
                Log.Debug ("OssiferWebView: downloaded purchase list", destinationUri);
143
 
                Banshee.ServiceStack.ServiceManager.Get<AmazonMp3DownloaderService> ()
144
 
                    .DownloadAmz (new SafeUri (destinationUri).LocalPath);
145
 
            }
146
 
        }
147
 
 
148
 
        public override void GoHome ()
149
 
        {
150
 
            LoadUri (GetActionUrl ("home/"));
151
 
        }
152
 
 
153
 
        public override void GoSearch (string query)
154
 
        {
155
 
            query = System.Uri.EscapeDataString (query);
156
 
            LoadUri (new Uri (GetActionUrl ("search/" + query)).AbsoluteUri);
157
 
        }
158
 
 
159
 
        public void SignOut ()
160
 
        {
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, "/");
167
 
                }
168
 
            }*/
169
 
 
170
 
            LoadUri (GetActionUrl ("sign_out/"));
171
 
        }
172
 
 
173
 
        private void CheckSignIn ()
174
 
        {
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;
179
 
            }
180
 
 
181
 
            if (IsSignedIn != signed_in) {
182
 
                IsSignedIn = signed_in;
183
 
                OnSignInChanged ();
184
 
            }
185
 
        }
186
 
 
187
 
        protected virtual void OnSignInChanged ()
188
 
        {
189
 
            var handler = SignInChanged;
190
 
            if (handler != null) {
191
 
                handler (this, EventArgs.Empty);
192
 
            }
193
 
        }
194
 
 
195
 
        public string GetActionUrl (string action)
196
 
        {
197
 
            return String.Concat (StoreSourcePreferences.RedirectUrl.Get (), Country, "/", action);
198
 
        }
199
 
    }
200
 
}