2010-01-20 Zoltan Varga <vargaz@gmail.com>
[mono.git] / mcs / class / System.Web / System.Web.UI / StateBag.cs
1 // 
2 // System.Web.UI.StateBag
3 //
4 // Author:
5 //        Ben Maurer <bmaurer@novell.com>
6 //
7 // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
8 //
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
16 // 
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
19 // 
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 //
28
29 using System.Collections;
30 using System.Collections.Specialized;
31 using System.Security.Permissions;
32
33 namespace System.Web.UI {
34
35         // CAS - no InheritanceDemand here as the class is sealed
36         [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
37         public sealed class StateBag : IDictionary, IStateManager {
38
39                 HybridDictionary ht;
40                 bool track;
41                 
42                 public StateBag (bool ignoreCase)
43                 {
44                         ht = new HybridDictionary (ignoreCase);
45                 }
46         
47                 public StateBag () : this (false)
48                 {
49                 }
50
51                 void IStateManager.LoadViewState (object savedState)
52                 {
53                         LoadViewState (savedState);
54                 }
55
56                 object IStateManager.SaveViewState ()
57                 {
58                         return SaveViewState ();
59                 }
60                 
61                 
62                 void IStateManager.TrackViewState ()
63                 {
64                         TrackViewState ();
65                 }
66
67                 bool IStateManager.IsTrackingViewState {
68                         get {
69                                 return track;
70                         }
71                 }
72                 
73                 internal bool IsTrackingViewState {
74                         get {
75                                 return track;
76                         }
77                 }       
78
79                 
80                 internal void LoadViewState (object savedState)
81                 {
82                         if (savedState == null)
83                                 return;
84                         
85                         foreach (DictionaryEntry de in (Hashtable) savedState)
86                                 Add ((string) de.Key, de.Value);
87                 }
88                 
89                 internal object SaveViewState ()
90                 {
91                         Hashtable h = null;
92
93                         foreach (DictionaryEntry de in ht) {
94                                 StateItem si = (StateItem) de.Value;
95                                 if (si.IsDirty) {
96                                         if (h == null)
97                                                 h = new Hashtable ();
98                                         h.Add (de.Key, si.Value);
99                                 }
100                         }
101
102                         return h;
103                 }
104                 
105                 internal void TrackViewState ()
106                 {
107                         track = true;
108                 }
109         
110                 public StateItem Add (string key, object value)
111                 {
112                         StateItem si = ht [key] as StateItem;
113                         if (si == null)
114                                 ht [key] = si = new StateItem (value);
115                         si.Value = value;
116                         si.IsDirty |= track;
117                         
118                         return si;
119                 }
120
121                 internal string GetString (string key, string def)
122                 {
123                         string s = (string) this [key];
124                         return s == null ? def : s;
125                 }
126                 
127                 internal bool GetBool (string key, bool def)
128                 {
129                         object o = this [key];
130                         return o == null ? def : (bool) o;
131                 }
132
133                 internal char GetChar (string key, char def)
134                 {
135                         object o = this [key];
136                         return o == null ? def : (char) o;
137                 }
138
139                 internal int GetInt (string key, int def)
140                 {
141                         object o = this [key];
142                         return o == null ? def : (int) o;
143                 }
144
145                 internal short GetShort (string key, short def)
146                 {
147                         object o = this [key];
148                         return o == null ? def : (short) o;
149                 }
150                 
151                 public void Clear ()
152                 {
153                         ht.Clear ();
154                 }
155         
156                 public IDictionaryEnumerator GetEnumerator ()
157                 {
158                         return ht.GetEnumerator ();
159                 }
160
161                 IEnumerator IEnumerable.GetEnumerator ()
162                 {
163                         return GetEnumerator ();
164                 }
165                 
166                 public bool IsItemDirty (string key)
167                 {
168                         StateItem si = ht [key] as StateItem;
169                         return si != null && si.IsDirty;
170                 }
171         
172                 public void Remove (string key)
173                 {
174                         ht.Remove (key);
175                 }
176         
177                 public void SetItemDirty (string key, bool dirty) 
178                 {
179                         StateItem si = (StateItem) ht [key];
180                         if (si != null)
181                                 si.IsDirty = dirty;
182                 }
183
184                 public int Count {
185                         get {
186                                 return ht.Count;
187                         }
188                 }
189         
190                 public object this [string key] {
191                         get {
192                                 StateItem i = ht [key] as StateItem;
193                                 if (i != null)
194                                         return i.Value;
195                                 return null;
196                         }
197                 
198                         set {
199                                 if (value == null && ! IsTrackingViewState)
200                                         Remove (key);
201                                 else
202                                         Add (key, value);
203                         }
204                 }
205         
206                 public ICollection Keys {
207                         get {
208                                 return ht.Keys;
209                         }
210                 
211                 }
212         
213                 public ICollection Values {
214                         get {
215                                 return ht.Values;
216                         }
217                 }
218
219                 void IDictionary.Add (object key, object value)
220                 {
221                         Add ((string) key, value);
222                 }
223         
224                 void IDictionary.Remove (object key)
225                 {
226                         Remove ((string) key);
227                 }
228
229                 void ICollection.CopyTo (Array array, int index)
230                 {
231                         ht.CopyTo (array, index);
232                 }
233         
234                 bool IDictionary.Contains (object key) 
235                 {
236                         return ht.Contains (key);
237                 }
238
239                 bool ICollection.IsSynchronized {
240                         get {
241                                 return false;
242                         }
243                 }
244
245                 object ICollection.SyncRoot {
246                         get {
247                                 return ht;
248                         }       
249                 }
250
251                 object IDictionary.this [object key] {
252                         get {
253                                 return this [(string) key];
254                         }
255                 
256                         set {
257                                 this [(string) key] = value;
258                         }
259                 }
260
261                 bool IDictionary.IsFixedSize {
262                         get {
263                                 return false;
264                         }
265                 }
266         
267                 bool IDictionary.IsReadOnly {
268                         get {
269                                 return false;
270                         }
271                 }
272
273 #if NET_2_0
274                 public
275 #else
276                 internal
277 #endif
278                 void SetDirty (bool dirty)
279                 {
280                         foreach (DictionaryEntry de in ht) {
281                                 StateItem si = (StateItem) de.Value;
282                                 si.IsDirty = dirty;
283                         }
284                 }
285         }
286 }