ccce0bc0ace040b9bea38474e770e7b588ae4824
[mono.git] / mcs / class / System.Web / System.Web.UI.WebControls.WebParts / PersonalizableAttribute.cs
1 //
2 // System.Web.Compilation.PersonalizableAttribute
3 //
4 // Authors:
5 //   Marek Habersack (grendello@gmail.com)
6 //
7 // (C) 2006 Marek Habersack
8 //
9
10 //
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
18 // 
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
21 // 
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 //
30 #if NET_2_0
31 using System;
32 using System.Collections;
33 using System.Collections.Generic;
34 using System.Reflection;
35
36 namespace System.Web.UI.WebControls.WebParts
37 {
38         [AttributeUsageAttribute(AttributeTargets.Property)] 
39         public sealed class PersonalizableAttribute : Attribute
40         {
41                 public static readonly PersonalizableAttribute Default;
42                 public static readonly PersonalizableAttribute NotPersonalizable;
43                 public static readonly PersonalizableAttribute Personalizable;
44                 public static readonly PersonalizableAttribute SharedPersonalizable;
45                 public static readonly PersonalizableAttribute UserPersonalizable;
46
47                 bool isPersonalizable;
48                 bool isSensitive;
49                 PersonalizationScope scope;
50                 
51                 static PersonalizableAttribute ()
52                 {
53                         Default = new PersonalizableAttribute (false);
54                         NotPersonalizable = Default;
55                         Personalizable = new PersonalizableAttribute (PersonalizationScope.User, false);
56                         SharedPersonalizable = new PersonalizableAttribute (PersonalizationScope.Shared, false);
57                         UserPersonalizable = new PersonalizableAttribute (PersonalizationScope.User, false);
58                 }
59                 
60                 public PersonalizableAttribute () : this (true)
61                 {
62                 }
63
64                 public PersonalizableAttribute (bool isPersonalizable)
65                 {
66                         this.isPersonalizable = isPersonalizable;
67                         this.scope = PersonalizationScope.User;
68                         this.isSensitive = false;
69                 }
70
71                 public PersonalizableAttribute (PersonalizationScope scope) : this (scope, false)
72                 {
73                 }
74
75                 public PersonalizableAttribute (PersonalizationScope scope, bool isSensitive)
76                 {
77                         this.isPersonalizable = true;
78                         this.scope = scope;
79                         this.isSensitive = isSensitive;
80                 }
81
82                 public bool IsPersonalizable {
83                         get { return isPersonalizable; }
84                 }
85
86                 public bool IsSensitive {
87                         get { return isSensitive; }
88                 }
89
90                 public PersonalizationScope Scope {
91                         get { return scope; }
92                 }
93
94                 public override bool Equals (object obj)
95                 {
96                         PersonalizableAttribute attr = obj as PersonalizableAttribute;
97                         if (attr == null)
98                                 return false;
99
100                         return (this.isPersonalizable == attr.IsPersonalizable &&
101                                 this.isSensitive == attr.IsSensitive &&
102                                 this.scope == attr.Scope);
103                 }
104                 
105                 public static bool Equals (object objA, object objB)
106                 {
107                         PersonalizableAttribute attrA = objA as PersonalizableAttribute;
108                         PersonalizableAttribute attrB = objB as PersonalizableAttribute;
109
110                         if (objA == null || objB == null)
111                                 return false;
112                         return (attrA.IsPersonalizable == attrB.IsPersonalizable &&
113                                 attrA.IsSensitive == attrB.IsSensitive &&
114                                 attrA.Scope == attrB.Scope);
115                 }
116
117                 public override int GetHashCode ()
118                 {
119                         return (this.isPersonalizable.GetHashCode () ^
120                                 this.isSensitive.GetHashCode () ^
121                                 this.scope.GetHashCode ());
122                 }
123
124                 public static ICollection GetPersonalizableProperties (Type type)
125                 {
126                         if (type == null)
127                                 throw new ArgumentNullException ("type");
128                         
129                         PropertyInfo[] properties = type.GetProperties ();
130                         if (properties == null || properties.Length == 0)
131                                 return new PropertyInfo [0];
132                         List <PropertyInfo> ret = null;
133                         
134                         foreach (PropertyInfo pi in properties)
135                                 if (PropertyQualifies (pi)) {
136                                         if (ret == null)
137                                                 ret = new List <PropertyInfo> ();
138                                         ret.Add (pi);
139                                 }
140                         return ret;
141                 }
142
143                 static bool PropertyQualifies (PropertyInfo pi)
144                 {
145                         object[] attributes = pi.GetCustomAttributes (false);
146                         if (attributes == null || attributes.Length == 0)
147                                 return false;
148
149                         PersonalizableAttribute attr;
150                         MethodInfo mi;
151                         foreach (object a in attributes) {
152                                 attr = a as PersonalizableAttribute;
153                                 if (attr == null || !attr.IsPersonalizable)
154                                         continue;
155                                 mi = pi.GetSetMethod (false);
156                                 if (mi == null)
157                                         throw new HttpException ("A public property on the type is marked as personalizable but is read-only.");
158                                 return true;
159                         }
160
161                         return false;
162                 }
163
164                 public override bool IsDefaultAttribute ()
165                 {
166                         return PersonalizableAttribute.Equals (this, Default);
167                 }
168
169                 public override bool Match (object obj)
170                 {
171                         PersonalizableAttribute attr = obj as PersonalizableAttribute;
172                         if (obj == null)
173                                 return false;
174                         return (this.isPersonalizable == attr.IsPersonalizable);
175                 }
176         }
177 }
178 #endif