Added BusyIndicator to Login View
[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.Windows;
9 using System.Windows.Controls;
10 using System.Windows.Data;
11 using System.Windows.Documents;
12 using System.Windows.Input;
13 using System.Windows.Media;
14 using System.Windows.Media.Imaging;
15 using System.Windows.Shapes;
16 using Caliburn.Micro;
17
18
19 namespace Pithos.Client.WPF.Preferences
20 {
21     /// <summary>
22     /// Interaction logic for LoginView.xaml
23     /// </summary>
24     public partial class LoginView : Window,INotifyPropertyChanged
25     {
26         private static readonly log4net.ILog Log = log4net.LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
27
28         private Uri _uri;
29         public Uri Uri
30         {
31             get
32             {
33                 return _uri;
34             }
35             set
36             {
37                 _uri = value;
38                 RaisePropertyChanged(() => Uri);
39             }
40         }
41
42         private void RaisePropertyChanged(Expression<Func<Uri>> property)
43         {
44             if (PropertyChanged != null)
45             {
46                 PropertyChanged(this, new PropertyChangedEventArgs(property.GetMemberInfo().Name));
47             }
48         }
49
50         public LoginView(Uri uri,string account=null)
51         {
52             InitializeComponent();
53             if (String.IsNullOrWhiteSpace(account))
54                 this.Title = "Retrieve Pithos credentials";
55             else
56                 this.Title = "Retrieve Pithos credentials for " + account;
57             Uri = uri;
58             LoginBrowser.Navigate(uri);
59         }
60
61
62         public string Token { get; set; }
63         public string Account { get; set; }
64
65
66         private void LoginBrowser_Navigating(object sender, System.Windows.Navigation.NavigatingCancelEventArgs e)
67         {
68             IsBusy.IsBusy = true;
69             Log.Debug(e.ToString());
70
71             if (e.Uri.Scheme == "pithos")
72             {
73                 Log.DebugFormat("Authentication response [{0}]",e.Uri);
74                 e.Cancel = true;
75                 var response = ParseResponse(e.Uri.Query);
76                 Account = response["user"];
77                 Token = response["token"];
78                 Log.InfoFormat("Token received for [{0}]", Account);
79                 DialogResult = true;
80                 this.Close();
81             }
82         }
83
84         private static Dictionary<string, string> ParseResponse(string request)
85         {
86             //var parts = request.Split(' ');
87             var query = request.TrimStart('?');
88
89             var items = query.Split('&')
90                 .Select(pair => pair.Split('='))
91                 .ToDictionary(arr => arr[0].ToLower(), arr => Uri.UnescapeDataString(arr[1]));
92             return items;
93         }
94
95         public event PropertyChangedEventHandler PropertyChanged;
96
97         private void Retry_Click(object sender, RoutedEventArgs e)
98         {
99             LoginBrowser.Navigate(Uri);
100         }
101
102         private void LoginBrowser_Navigated(object sender, System.Windows.Navigation.NavigationEventArgs e)
103         {
104             IsBusy.IsBusy = false;
105         }
106     }
107 }