* HttpSessionState.cs (CopyTo): Copy values not keys. Patch by
[mono.git] / mcs / class / System.Web / System.Web.SessionState / HttpSessionState.cs
1 //
2 // System.Web.SessionState.HttpSessionState
3 //
4 // Authors:
5 //      Gonzalo Paniagua Javier (gonzalo@ximian.com)
6 //
7 // (C) 2002,2003 Ximian, Inc (http://www.ximian.com)
8 //
9 using System;
10 using System.Collections;
11 using System.Collections.Specialized;
12 using System.Globalization;
13 using System.Text;
14 using System.Threading;
15 using System.Web;
16
17 namespace System.Web.SessionState {
18 public sealed class HttpSessionState : ICollection, IEnumerable
19 {
20         private string _id;
21         private SessionDictionary _dict;
22         private HttpStaticObjectsCollection _staticObjects;
23         private int _timeout;
24         private bool _newSession;
25         private bool _isCookieless;
26         private SessionStateMode _mode;
27         private bool _isReadonly;
28         private bool _abandoned;
29
30         internal HttpSessionState (string id,
31                                    SessionDictionary dict,
32                                    HttpStaticObjectsCollection staticObjects,
33                                    int timeout,
34                                    bool newSession,
35                                    bool isCookieless,
36                                    SessionStateMode mode,
37                                    bool isReadonly)
38         {
39                 _id = id;
40                 _dict = dict;
41                 _staticObjects = staticObjects;
42                 _timeout = timeout;
43                 _newSession = newSession;
44                 _isCookieless = isCookieless;
45                 _mode = mode;
46                 _isReadonly = isReadonly;
47         }
48
49         public int CodePage {
50                 get {
51                         HttpContext current = HttpContext.Current;
52                         if (current == null)
53                                 return Encoding.Default.CodePage;
54
55                         return current.Response.ContentEncoding.CodePage;
56                 }
57                 
58                 set {
59                         HttpContext current = HttpContext.Current;
60                         if (current != null)
61                                 current.Response.ContentEncoding = Encoding.GetEncoding (value);
62                 }
63         }
64
65         public HttpSessionState Contents {
66                 get { return this; }
67         }
68
69         public int Count {
70                 get { return _dict.Count; }
71         }
72
73         internal bool IsAbandoned {
74                 get { return _abandoned; }
75         }
76
77         public bool IsCookieless {
78                 get { return _isCookieless; }
79         }
80
81         public bool IsNewSession {
82                 get { return _newSession; }
83                 set { _newSession=value; }
84         }
85
86         public bool IsReadOnly {
87                 get { return _isReadonly; }
88         }
89
90         public bool IsSynchronized {
91                 get { return false; }
92         }
93
94         public object this [string key] {
95                 get { return _dict [key]; }
96                 set { _dict [key] = value; }
97         }
98
99         public object this [int index] {
100                 get { return _dict [index]; }
101                 set { _dict [index] = value; }
102         }
103
104         public NameObjectCollectionBase.KeysCollection Keys {
105                 get { return _dict.Keys; }
106         }
107
108         public int LCID {
109                 get { return Thread.CurrentThread.CurrentCulture.LCID; }
110                 set { Thread.CurrentThread.CurrentCulture = new CultureInfo(value); }
111         }
112
113         public SessionStateMode Mode {
114                 get { return _mode; }
115         }
116
117         public string SessionID {
118                 get { return _id; }
119         }
120
121         public HttpStaticObjectsCollection StaticObjects {
122                 get { return _staticObjects; }
123         }
124
125         public object SyncRoot {
126                 get { return this; }
127         }
128
129         public int Timeout {
130                 get { return _timeout; }
131                 set {
132                         if (value < 1)
133                                 throw new ArgumentException ("The argument to SetTimeout must be greater than 0.");
134                         _timeout = value;
135                 }
136         }
137
138         internal SessionDictionary SessionDictionary {
139                 get { return _dict; }
140         }
141
142         public void Abandon ()
143         {
144                 _abandoned = true;
145         }
146
147         public void Add (string name, object value)
148         {
149                 _dict [name] = value;
150         }
151
152         public void Clear ()
153         {
154                 if (_dict != null)
155                         _dict.Clear ();
156         }
157         
158         public void CopyTo (Array array, int index)
159         {
160                 NameObjectCollectionBase.KeysCollection all = Keys;
161                 for (int i = 0; i < all.Count; i++)
162                         array.SetValue (all.Get(i), i + index);
163         }
164
165         public IEnumerator GetEnumerator ()
166         {
167                 return _dict.GetEnumerator ();
168         }
169         
170         public void Remove (string name)
171         {
172                 _dict.Remove (name);
173         }
174
175         public void RemoveAll ()
176         {
177                 _dict.Clear ();
178         }
179
180         public void RemoveAt (int index)
181         {
182                 _dict.RemoveAt (index);
183         }
184 }
185 }
186