[banshee] Implemented sign in detection & sign out button



commit 95ed61a971c958232a9c42859174cb77fcbaae96
Author: Aaron Bockover <abockover novell com>
Date:   Mon Jul 12 15:03:20 2010 -0400

    Implemented sign in detection & sign out button
    
    Ossifer supports getting and deleting arbitrary cookies now.

 .../Banshee.WebBrowser/OssiferSession.cs           |   51 ++++++++++++++++
 .../libossifer/ossifer-session.c                   |   62 +++++++++++++++++--
 .../Banshee.AmazonMp3.Store/StoreView.cs           |   61 ++++++++++++++++----
 .../Banshee.AmazonMp3.Store/WebBrowserShell.cs     |   22 ++++++-
 4 files changed, 175 insertions(+), 21 deletions(-)
---
diff --git a/src/Core/Banshee.WebBrowser/Banshee.WebBrowser/OssiferSession.cs b/src/Core/Banshee.WebBrowser/Banshee.WebBrowser/OssiferSession.cs
index fdc910c..1561a70 100644
--- a/src/Core/Banshee.WebBrowser/Banshee.WebBrowser/OssiferSession.cs
+++ b/src/Core/Banshee.WebBrowser/Banshee.WebBrowser/OssiferSession.cs
@@ -102,5 +102,56 @@ namespace Banshee.WebBrowser
                 handler (oldCookie, newCookie);
             }
         }
+
+        [DllImport (LIBOSSIFER)]
+        private static extern IntPtr ossifer_session_get_cookie (IntPtr name, IntPtr domain, IntPtr path);
+
+        [DllImport (LIBOSSIFER)]
+        private static extern void ossifer_cookie_free (IntPtr cookie);
+
+        public static OssiferCookie GetCookie (string name, string domain, string path)
+        {
+            var name_raw = IntPtr.Zero;
+            var domain_raw = IntPtr.Zero;
+            var path_raw = IntPtr.Zero;
+
+            try {
+                var ptr = ossifer_session_get_cookie (
+                    name_raw = GLib.Marshaller.StringToPtrGStrdup (name),
+                    domain_raw = GLib.Marshaller.StringToPtrGStrdup (domain),
+                    path_raw = GLib.Marshaller.StringToPtrGStrdup (path));
+                if (ptr != IntPtr.Zero) {
+                    var cookie = new OssiferCookie (ptr);
+                    ossifer_cookie_free (ptr);
+                    return cookie;
+                }
+                return null;
+            } finally {
+                GLib.Marshaller.Free (name_raw);
+                GLib.Marshaller.Free (domain_raw);
+                GLib.Marshaller.Free (path_raw);
+            }
+        }
+
+        [DllImport (LIBOSSIFER)]
+        private static extern bool ossifer_session_delete_cookie (IntPtr name, IntPtr domain, IntPtr path);
+
+        public static bool DeleteCookie (string name, string domain, string path)
+        {
+            var name_raw = IntPtr.Zero;
+            var domain_raw = IntPtr.Zero;
+            var path_raw = IntPtr.Zero;
+
+            try {
+                return ossifer_session_delete_cookie (
+                    name_raw = GLib.Marshaller.StringToPtrGStrdup (name),
+                    domain_raw = GLib.Marshaller.StringToPtrGStrdup (domain),
+                    path_raw = GLib.Marshaller.StringToPtrGStrdup (path));
+            } finally {
+                GLib.Marshaller.Free (name_raw);
+                GLib.Marshaller.Free (domain_raw);
+                GLib.Marshaller.Free (path_raw);
+            }
+        }
     }
 }
\ No newline at end of file
diff --git a/src/Core/Banshee.WebBrowser/libossifer/ossifer-session.c b/src/Core/Banshee.WebBrowser/libossifer/ossifer-session.c
index 447d6e4..265aa95 100644
--- a/src/Core/Banshee.WebBrowser/libossifer/ossifer-session.c
+++ b/src/Core/Banshee.WebBrowser/libossifer/ossifer-session.c
@@ -24,6 +24,13 @@ ossifer_session_cookie_jar_changed (SoupCookieJar *jar,
     }
 }
 
+static SoupCookieJar *
+ossifer_session_get_cookie_jar ()
+{
+    return (SoupCookieJar *)soup_session_get_feature (webkit_get_default_session (),
+        SOUP_TYPE_COOKIE_JAR);
+}
+
 OssiferSession *
 ossifer_session_initialize (const gchar *cookie_db_path,
     OssiferSessionCookieJarChanged cookie_jar_changed_callback)
@@ -69,15 +76,56 @@ void
 ossifer_session_set_cookie (const gchar *name, const gchar *value,
     const gchar *domain, const gchar *path, gint max_age)
 {
-    SoupSession *session;
-    SoupCookieJar *cookies;
     SoupCookie *cookie;
+    SoupCookieJar *cookie_jar = ossifer_session_get_cookie_jar ();
 
-    session = webkit_get_default_session ();
-    cookies = (SoupCookieJar *)soup_session_get_feature (session, SOUP_TYPE_COOKIE_JAR);
-
-    g_return_if_fail (cookies != NULL);
+    g_return_if_fail (cookie_jar != NULL);
 
     cookie = soup_cookie_new (name, value, domain, path, max_age);
-    soup_cookie_jar_add_cookie (cookies, cookie);
+    soup_cookie_jar_add_cookie (cookie_jar, cookie);
+}
+
+SoupCookie *
+ossifer_session_get_cookie (const gchar *name, const gchar *domain, const gchar *path)
+{
+    GSList *cookies;
+    GSList *item;
+    SoupCookie *found_cookie = NULL;
+
+    cookies = soup_cookie_jar_all_cookies (ossifer_session_get_cookie_jar ());
+
+    for (item = cookies; item != NULL; item = item->next) {
+        SoupCookie *cookie = (SoupCookie *)item->data;
+
+        if (g_str_equal (name, cookie->name) &&
+            g_str_equal (domain, cookie->domain) &&
+            g_str_equal (path, cookie->path)) {
+            found_cookie = soup_cookie_copy (cookie);
+            break;
+        }
+    }
+
+    soup_cookies_free (cookies);
+
+    return found_cookie;
+}
+
+gboolean
+ossifer_session_delete_cookie (const gchar *name, const gchar *domain, const gchar *path)
+{
+    SoupCookie *cookie = ossifer_session_get_cookie (name, domain, path);
+
+    if (cookie != NULL) {
+        soup_cookie_jar_delete_cookie (ossifer_session_get_cookie_jar (), cookie);
+        soup_cookie_free (cookie);
+        return TRUE;
+    }
+
+    return FALSE;
+}
+
+void
+ossifer_cookie_free (SoupCookie *cookie)
+{
+    soup_cookie_free (cookie);
 }
\ No newline at end of file
diff --git a/src/Extensions/Banshee.AmazonMp3.Store/Banshee.AmazonMp3.Store/StoreView.cs b/src/Extensions/Banshee.AmazonMp3.Store/Banshee.AmazonMp3.Store/StoreView.cs
index 4d4a0f5..3f78bac 100644
--- a/src/Extensions/Banshee.AmazonMp3.Store/Banshee.AmazonMp3.Store/StoreView.cs
+++ b/src/Extensions/Banshee.AmazonMp3.Store/Banshee.AmazonMp3.Store/StoreView.cs
@@ -40,8 +40,14 @@ namespace Banshee.AmazonMp3.Store
 {
     public class StoreView : OssiferWebView
     {
+        public bool IsSignedIn { get; private set; }
+
+        public event EventHandler SignInChanged;
+
         public StoreView ()
         {
+            OssiferSession.CookieChanged += (o, n) => CheckSignIn ();
+
             // Ensure that Amazon knows a valid downloader is available,
             // otherwise the purchase experience is interrupted with a
             // confusing message about downloading and installing software.
@@ -49,17 +55,8 @@ namespace Banshee.AmazonMp3.Store
                 AmzMp3Downloader.AmazonMp3DownloaderCompatVersion,
                 ".amazon.com", "/", TimeSpan.FromDays (365.2422));
 
-            // This is an HTML5 Canvas/JS spinner icon. It is awesome
-            // and renders immediately, going away when the store loads.
-            LoadString (AssemblyResource.GetFileContents ("loading.html"),
-                "text/html", "UTF-8", null);
-
-            // We defer this to another main loop iteration, otherwise
-            // our load placeholder document will never be rendered.
-            GLib.Idle.Add (delegate {
-                GoHome ();
-                return false;
-            });
+            CheckSignIn ();
+            FullReload ();
         }
 
         protected override OssiferNavigationResponse OnMimeTypePolicyDecisionRequested (string mimetype)
@@ -119,5 +116,47 @@ namespace Banshee.AmazonMp3.Store
         {
             LoadUri (new Uri ("http://amz-proxy.banshee.fm/search/"; + query).AbsoluteUri);
         }
+
+        public void SignOut ()
+        {
+            foreach (var name in new [] { "at-main", "x-main", "session-id",
+                "session-id-time", "session-token", "uidb-main", "pf"}) {
+                OssiferSession.DeleteCookie (name, ".amazon.com", "/");
+            }
+
+            FullReload ();
+        }
+
+        public void FullReload ()
+        {
+            // This is an HTML5 Canvas/JS spinner icon. It is awesome
+            // and renders immediately, going away when the store loads.
+            LoadString (AssemblyResource.GetFileContents ("loading.html"),
+                "text/html", "UTF-8", null);
+
+            // We defer this to another main loop iteration, otherwise
+            // our load placeholder document will never be rendered.
+            GLib.Idle.Add (delegate {
+                GoHome ();
+                return false;
+            });
+        }
+
+        private void CheckSignIn ()
+        {
+            var signed_in = OssiferSession.GetCookie ("at-main", ".amazon.com", "/") != null;
+            if (IsSignedIn != signed_in) {
+                IsSignedIn = signed_in;
+                OnSignInChanged ();
+            }
+        }
+
+        protected virtual void OnSignInChanged ()
+        {
+            var handler = SignInChanged;
+            if (handler != null) {
+                handler (this, EventArgs.Empty);
+            }
+        }
     }
 }
diff --git a/src/Extensions/Banshee.AmazonMp3.Store/Banshee.AmazonMp3.Store/WebBrowserShell.cs b/src/Extensions/Banshee.AmazonMp3.Store/Banshee.AmazonMp3.Store/WebBrowserShell.cs
index 88aaa4c..38ca3a2 100644
--- a/src/Extensions/Banshee.AmazonMp3.Store/Banshee.AmazonMp3.Store/WebBrowserShell.cs
+++ b/src/Extensions/Banshee.AmazonMp3.Store/Banshee.AmazonMp3.Store/WebBrowserShell.cs
@@ -40,10 +40,11 @@ namespace Banshee.AmazonMp3.Store
         private StoreView store_view = new StoreView ();
         private NavigationControl navigation_control = new NavigationControl ();
         private Label title = new Label ();
+        private Button sign_out_button = new Button (Catalog.GetString ("Sign out of Amazon")) { Relief = ReliefStyle.None };
         private SearchEntry search_entry = new SearchEntry ();
         private int search_clear_on_navigate_state;
 
-        public WebBrowserShell () : base (2, 3, false)
+        public WebBrowserShell () : base (2, 4, false)
         {
             RowSpacing = 5;
 
@@ -80,6 +81,13 @@ namespace Banshee.AmazonMp3.Store
                 AttachOptions.Shrink,
                 0, 0);
 
+            sign_out_button.Clicked += (o, e) => store_view.SignOut ();
+
+            Attach (sign_out_button, 2, 3, 0, 1,
+                AttachOptions.Shrink,
+                AttachOptions.Shrink,
+                0, 0);
+
             search_entry.EmptyMessage = String.Format (Catalog.GetString ("Search the Amazon MP3 Store"));
             search_entry.SetSizeRequest (260, -1);
             // FIXME: dummy option to make the "search" icon show up;
@@ -93,7 +101,7 @@ namespace Banshee.AmazonMp3.Store
                 store_view.HasFocus = true;
                 search_clear_on_navigate_state = 1;
             };
-            Attach (search_entry, 2, 3, 0, 1,
+            Attach (search_entry, 3, 4, 0, 1,
                 AttachOptions.Fill,
                 AttachOptions.Shrink,
                 0, 0);
@@ -101,7 +109,7 @@ namespace Banshee.AmazonMp3.Store
             store_view_scroll.Add (store_view);
             store_view_scroll.ShadowType = ShadowType.In;
 
-            Attach (store_view_scroll, 0, 3, 1, 2,
+            Attach (store_view_scroll, 0, 4, 1, 2,
                 AttachOptions.Expand | AttachOptions.Fill,
                 AttachOptions.Expand | AttachOptions.Fill,
                 0, 0);
@@ -109,6 +117,14 @@ namespace Banshee.AmazonMp3.Store
             UpdateTitle (Catalog.GetString ("Loading Amazon MP3 Store..."));
 
             ShowAll ();
+
+            store_view.SignInChanged += (o, e) => UpdateSignInButton ();
+            UpdateSignInButton ();
+        }
+
+        private void UpdateSignInButton ()
+        {
+            sign_out_button.Visible = store_view.IsSignedIn;
         }
 
         private void UpdateTitle (string titleText)



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]