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