Fix when wiping an account that hasn't been authenticated
[pithos-ms-client] / trunk / Pithos.Client.WPF / Preferences / LoginView.xaml.cs
1 using System;
2 using System.Collections.Generic;
3 using System.ComponentModel;
4 using System.Linq;
5 using System.Linq.Expressions;
6 using System.Reflection;
7 using System.Text;
8 using System.Threading.Tasks;
9 using System.Windows;
10 using System.Windows.Controls;
11 using System.Windows.Data;
12 using System.Windows.Documents;
13 using System.Windows.Input;
14 using System.Windows.Media;
15 using System.Windows.Media.Imaging;
16 using System.Windows.Shapes;
17 using Caliburn.Micro;
18 using Pithos.Client.WPF.Properties;
19
20
21 namespace Pithos.Client.WPF.Preferences
22 {
23     /// <summary>
24     /// Interaction logic for LoginView.xaml
25     /// </summary>
26     public partial class LoginView : Window,INotifyPropertyChanged
27     {
28         private static readonly log4net.ILog Log = log4net.LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
29
30         private Uri _uri;
31         private Uri _logoutUri;
32         private bool _loggingOut;
33
34         public Uri Uri
35         {
36             get
37             {
38                 return _uri;
39             }
40             set
41             {
42                 _uri = value;
43                 RaisePropertyChanged(() => Uri);
44             }
45         }
46
47         private void RaisePropertyChanged(Expression<Func<Uri>> property)
48         {
49             if (PropertyChanged != null)
50             {
51                 PropertyChanged(this, new PropertyChangedEventArgs(property.GetMemberInfo().Name));
52             }
53         }
54
55         public LoginView(Uri loginUri, Uri logoutUri, string account = null)
56         {
57             InitializeComponent();
58             if (String.IsNullOrWhiteSpace(account))
59                 this.Title = "Retrieve Pithos credentials";
60             else
61                 this.Title = "Retrieve Pithos credentials for " + account;
62             Uri = loginUri;
63
64             
65             _logoutUri = logoutUri;
66
67             _loggingOut = true;
68             
69             LoginBrowser.Navigate(logoutUri ?? loginUri);
70         }
71
72
73         public string Token { get; set; }
74         public string Account { get; set; }
75
76
77         private void LoginBrowser_Navigating(object sender, System.Windows.Navigation.NavigatingCancelEventArgs e)
78         {
79             IsBusy.IsBusy = true;
80             LoginBrowser.Visibility=Visibility.Hidden;
81
82             if (Log.IsDebugEnabled)
83                 Log.Debug(e.ToString());
84
85             if (e.Uri.Scheme == "pithos")
86             {
87                 Log.DebugFormat("Authentication response [{0}]",e.Uri);
88                 e.Cancel = true;
89                 var response = ParseResponse(e.Uri.Query);
90                 Account = response["user"];
91                 Token = response["token"];
92                 Log.InfoFormat("Token received for [{0}]", Account);
93                 DialogResult = true;
94                 this.Close();
95             }
96         }
97
98         private static Dictionary<string, string> ParseResponse(string request)
99         {
100             //var parts = request.Split(' ');
101             var query = request.TrimStart('?');
102
103             var items = query.Split('&')
104                 .Select(pair => pair.Split('='))
105                 .ToDictionary(arr => arr[0].ToLower(), arr => Uri.UnescapeDataString(arr[1]));
106             return items;
107         }
108
109         public event PropertyChangedEventHandler PropertyChanged;
110
111         private void Retry_Click(object sender, RoutedEventArgs e)
112         {
113             LoginBrowser.Navigate(Uri);
114         }
115
116         private void LoginBrowser_Navigated(object sender, System.Windows.Navigation.NavigationEventArgs e)
117         {
118             if (_loggingOut)
119             {
120                 _loggingOut = false;
121                 IsBusy.BusyContent = "Opening login screen";
122                 LoginBrowser.Navigate(Uri);
123             }
124             else
125             {
126                 IsBusy.IsBusy = false;
127                 LoginBrowser.Visibility = Visibility.Visible;
128             }
129         }
130     }
131 }