summaryrefslogtreecommitdiffstats
path: root/source/Janrain.OpenId/RegistrationExtension/OpenIdProfileFields.cs
blob: a62157cc22ad44aa4668f1bfd85896835ab8aae3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/********************************************************
 * Copyright (C) 2007 Andrew Arnott
 * Released under the New BSD License
 * License available here: http://www.opensource.org/licenses/bsd-license.php
 * For news or support on this file: http://blog.nerdbank.net/
 ********************************************************/

using System;
using System.Globalization;
using System.Net.Mail;
using Janrain.OpenId.RegistrationExtension;
using System.Xml.Serialization;

namespace Janrain.OpenId.RegistrationExtension
{
	[Serializable()]
	public class OpenIdProfileFields
	{
		internal static OpenIdProfileFields Empty = new OpenIdProfileFields();

		public OpenIdProfileFields()
		{

		}

		private string nickname;
		public string Nickname
		{
			get { return nickname; }
			set { nickname = value; }
		}

		private string email;
		public string Email
		{
			get { return email; }
			set { email = value; }
		}

		public MailAddress MailAddress
		{
			get
			{
				if (Email == null) return null;
				if (string.IsNullOrEmpty(Fullname))
					return new MailAddress(Email);
				else
					return new MailAddress(Email, Fullname);
			}
		}

		private string fullname;
		public string Fullname
		{
			get { return fullname; }
			set { fullname = value; }
		}

		private DateTime? birthdate;
		public DateTime? Birthdate
		{
			get { return birthdate; }
			set { birthdate = value; }
		}

		private Gender? gender;
		public Gender? Gender
		{
			get { return gender; }
			set { gender = value; }
		}

		private string postalCode;
		public string PostalCode
		{
			get { return postalCode; }
			set { postalCode = value; }
		}

		private string country;
		public string Country
		{
			get { return country; }
			set { country = value; }
		}

		private string language;
		public string Language
		{
			get { return language; }
			set { language = value; }
		}

		private CultureInfo culture;
		[XmlIgnore]
		public CultureInfo Culture
		{
			get
			{
				if (culture == null && !string.IsNullOrEmpty(Language))
				{
					string cultureString = "";
					cultureString = Language;
					if (!string.IsNullOrEmpty(Country))
						cultureString += "-" + Country;
					culture = CultureInfo.GetCultureInfo(cultureString);
				}

				return culture;
			}
			set
			{
				culture = value;
				Language = (value != null) ? value.TwoLetterISOLanguageName : null;
				int indexOfHyphen = (value != null) ? value.Name.IndexOf('-') : -1;
				Country = indexOfHyphen > 0 ? value.Name.Substring(indexOfHyphen + 1) : null;
			}
		}

		private string timeZone;
		public string TimeZone
		{
			get { return timeZone; }
			set { timeZone = value; }
		}

		public override bool Equals(object obj)
		{
			OpenIdProfileFields other = obj as OpenIdProfileFields;
			if (other == null) return false;

			return
				safeEquals(this.Birthdate, other.Birthdate) &&
				safeEquals(this.Country, other.Country) &&
				safeEquals(this.Language, other.Language) &&
				safeEquals(this.Email, other.Email) &&
				safeEquals(this.Fullname, other.Fullname) &&
				safeEquals(this.Gender, other.Gender) &&
				safeEquals(this.Nickname, other.Nickname) &&
				safeEquals(this.PostalCode, other.PostalCode) &&
				safeEquals(this.TimeZone, other.TimeZone);
		}

		bool safeEquals(object one, object other)
		{
			if (one == null && other == null) return true;
			if (one == null ^ other == null) return false;
			return one.Equals(other);
		}
	}
}