Statistics
| Branch: | Revision:

root / trunk / Pithos.Client.WPF / Preferences / LoginView.xaml.cs @ 6b0de454

History | View | Annotate | Download (2.9 kB)

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
            Log.Debug(e.ToString());
69

    
70
            if (e.Uri.Scheme == "pithos")
71
            {
72
                Log.DebugFormat("Authentication response [{0}]",e.Uri);
73
                e.Cancel = true;
74
                var response = ParseResponse(e.Uri.Query);
75
                Account = response["user"];
76
                Token = response["token"];
77
                Log.InfoFormat("Token received for [{0}]", Account);
78
                DialogResult = true;
79
                this.Close();
80
            }
81
        }
82

    
83
        private static Dictionary<string, string> ParseResponse(string request)
84
        {
85
            //var parts = request.Split(' ');
86
            var query = request.TrimStart('?');
87

    
88
            var items = query.Split('&')
89
                .Select(pair => pair.Split('='))
90
                .ToDictionary(arr => arr[0].ToLower(), arr => Uri.UnescapeDataString(arr[1]));
91
            return items;
92
        }
93

    
94
        public event PropertyChangedEventHandler PropertyChanged;
95

    
96
        private void Retry_Click(object sender, RoutedEventArgs e)
97
        {
98
            LoginBrowser.Navigate(Uri);
99
        }
100
    }
101
}