Account page changes
[pithos-ms-client] / trunk / Pithos.Client.WPF / PithosAccount.cs
index fda2b6d..7da6f2b 100644 (file)
@@ -1,42 +1,50 @@
-// -----------------------------------------------------------------------
-// <copyright file="PithosAccount.cs" company="GRNet">
-// Copyright 2011-2012 GRNET S.A. All rights reserved.
-// 
-// Redistribution and use in source and binary forms, with or
-// without modification, are permitted provided that the following
-// conditions are met:
-// 
-//   1. Redistributions of source code must retain the above
-//      copyright notice, this list of conditions and the following
-//      disclaimer.
-// 
-//   2. Redistributions in binary form must reproduce the above
-//      copyright notice, this list of conditions and the following
-//      disclaimer in the documentation and/or other materials
-//      provided with the distribution.
-// 
-// THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
-// OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
-// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
-// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
-// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
-// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
-// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
-// USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
-// AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
-// LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
-// ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
-// POSSIBILITY OF SUCH DAMAGE.
-// 
-// The views and conclusions contained in the software and
-// documentation are those of the authors and should not be
-// interpreted as representing official policies, either expressed
-// or implied, of GRNET S.A.
+#region
+/* -----------------------------------------------------------------------
+ * <copyright file="PithosAccount.cs" company="GRNet">
+ * 
+ * Copyright 2011-2012 GRNET S.A. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or
+ * without modification, are permitted provided that the following
+ * conditions are met:
+ *
+ *   1. Redistributions of source code must retain the above
+ *      copyright notice, this list of conditions and the following
+ *      disclaimer.
+ *
+ *   2. Redistributions in binary form must reproduce the above
+ *      copyright notice, this list of conditions and the following
+ *      disclaimer in the documentation and/or other materials
+ *      provided with the distribution.
+ *
+ *
+ * THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
+ * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
+ * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ * The views and conclusions contained in the software and
+ * documentation are those of the authors and should not be
+ * interpreted as representing official policies, either expressed
+ * or implied, of GRNET S.A.
+ * </copyright>
+ * -----------------------------------------------------------------------
+ */
+#endregion
 
 // </copyright>
 // -----------------------------------------------------------------------
 
 using System.IO;
+using System.Reflection;
 using Pithos.Network;
 using log4net;
 
@@ -60,7 +68,7 @@ namespace Pithos.Client.WPF
     /// </summary>
     public static class PithosAccount
     {
-        private static readonly ILog Log = LogManager.GetLogger(typeof(PithosAccount));
+        private static readonly ILog Log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
 
         /// <summary>
         /// Asynchronously retrieves PITHOS credentials
@@ -79,14 +87,17 @@ namespace Pithos.Client.WPF
             //TODO:Force logout here to take care of existing cookies
 
 
-            var listenerUrl = String.Format("http://127.0.0.1:{0}/", port);
+            var listenerUrl = String.Format("http://127.0.0.1:{0}", port);
 
             
 
             var receiveCredentials = ListenForRedirectAsync(port);
 
+            var logoutUrl = loginUrl.Replace("login", "im/logout");
+            var logoutProcess=Process.Start(logoutUrl);            
+            TaskEx.Delay(2000).Wait();
             var uriBuilder=new UriBuilder(loginUrl);                       
-            uriBuilder.Query="next=" + listenerUrl;
+            uriBuilder.Query=String.Format("next={0}&force=", listenerUrl);
 
             var retrieveUri = uriBuilder.Uri;
             Log.InfoFormat("[RETRIEVE] Open Browser at {0}", retrieveUri);
@@ -95,6 +106,7 @@ namespace Pithos.Client.WPF
             return receiveCredentials;
         }
 
+/*
         private static async Task<NetworkCredential> ListenHttpForRedirectAsync(string listenerUrl)
         {
             using (var listener = new HttpListener())
@@ -139,6 +151,7 @@ namespace Pithos.Client.WPF
                 }
             }
         }
+*/
 
         private static async Task<NetworkCredential> ListenForRedirectAsync(int port)
         {
@@ -152,13 +165,16 @@ namespace Pithos.Client.WPF
                 using (var client = await listener.AcceptTcpClientAsync()
                                         .WithTimeout(TimeSpan.FromMinutes(5)))
                 {
-
                     using (var stream = client.GetStream())
                     using (var reader=new StreamReader(stream))
                     {
                         var request = await reader.ReadLineAsync();
-                        if (request == null)
-                            throw new Exception("Nothing retrieved");
+                        //BUG
+                        //TODO: Add proper warnings here if the content is empty, don't just throw an exception
+                        //This may be a common occurence 
+
+                        if (String.IsNullOrWhiteSpace(request))
+                            throw new PithosException("The server did send any information");
                         Log.InfoFormat("[RETRIEVE] Got Connection {0}", request);
 
 
@@ -171,7 +187,7 @@ namespace Pithos.Client.WPF
                         {
                             Respond(stream, "Failure", "The server did not return a username or token");
                             Log.ErrorFormat("[RETRIEVE] No credentials returned by server");
-                            throw new Exception("The server did not return a username or token");
+                            throw new PithosException("The server did not return a username or token");
                         }
                         else
                         {
@@ -185,7 +201,7 @@ namespace Pithos.Client.WPF
             catch (Exception exc)
             {
                 Log.Error("[RETRIEVE][ERROR] Receive connection {0}", exc);
-                throw;
+                throw new PithosException("An error occured while retrieving credentials",exc);
             }
         }