4ad533a2ddc610a24a7dfb140a91da6ca218b28d
[mono.git] / mcs / class / System.Web / System.Web / HttpBrowserCapabilities.cs
1 // 
2 // System.Web.HttpBrowserCapabilities
3 //
4 // Authors:
5 //   Patrik Torstensson (Patrik.Torstensson@labs2.com)
6 //   Gonzalo Paniagua Javier (gonzalo@ximian.com)
7 //
8 // (c) 2003 Novell, Inc. (http://www.novell.com)
9 //
10 using System;
11 using System.Web.Configuration;
12 using System.Web.UI;
13
14 namespace System.Web
15 {
16         public class HttpBrowserCapabilities : HttpCapabilitiesBase
17         {
18                 const int HaveActiveXControls = 1;
19                 const int HaveAOL = 2;
20                 const int HaveBackGroundSounds = 3;
21                 const int HaveBeta = 4;
22                 const int HaveBrowser = 5;
23                 const int HaveCDF = 6;
24                 const int HaveClrVersion = 7;
25                 const int HaveCookies = 8;
26                 const int HaveCrawler = 9;
27                 const int HaveEcmaScriptVersion = 10;
28                 const int HaveFrames = 11;
29                 const int HaveJavaApplets = 12;
30                 const int HaveJavaScript = 13;
31                 const int HaveMajorVersion = 14;
32                 const int HaveMinorVersion = 15;
33                 const int HaveMSDomVersion = 16;
34                 const int HavePlatform = 17;
35                 const int HaveTables = 18;
36                 const int HaveTagWriter = 19;
37                 const int HaveVBScript = 20;
38                 const int HaveVersion = 21;
39                 const int HaveW3CDomVersion = 22;
40                 const int HaveWin16 = 23;
41                 const int HaveWin32 = 24;
42
43                 int flags;
44                 bool activeXControls;
45                 bool aol;
46                 bool backgroundSounds;
47                 bool beta;
48                 string browser;
49                 bool cdf;
50                 Version clrVersion;
51                 bool cookies;
52                 bool crawler;
53                 Version ecmaScriptVersion;
54                 bool frames;
55                 bool javaApplets;
56                 bool javaScript;
57                 int majorVersion;
58                 double minorVersion;
59                 Version msDomVersion;
60                 string platform;
61                 bool tables;
62                 Type tagWriter;
63                 bool vbscript;
64                 string version;
65                 Version w3CDomVersion;
66                 bool win16;
67
68                 public HttpBrowserCapabilities () { }
69
70                 public bool ActiveXControls {
71                         get {
72                                 if (!Get (HaveActiveXControls)) {
73                                         Set (HaveActiveXControls);
74                                         activeXControls = ReadBoolean ("activexcontrols", false);
75                                 }
76
77                                 return activeXControls;
78                         }
79                 }
80
81                 public bool AOL {
82                         get {
83                                 if (!Get (HaveAOL)) {
84                                         Set (HaveAOL);
85                                         aol = ReadBoolean ("aol", false);
86                                 }
87
88                                 return aol;
89                         }
90                 }
91
92                 public bool BackgroundSounds {
93                         get {
94                                 if (!Get (HaveBackGroundSounds)) {
95                                         Set (HaveBackGroundSounds);
96                                         backgroundSounds = ReadBoolean ("backgroundsounds", false);
97                                 }
98
99                                 return backgroundSounds;
100                         }
101                 }
102
103                 public bool Beta {
104                         get {
105                                 if (!Get (HaveBeta)) {
106                                         Set (HaveBeta);
107                                         beta = ReadBoolean ("beta", false);
108                                 }
109
110                                 return beta;
111                         }
112                 }
113
114                 public string Browser {
115                         get {
116                                 if (!Get (HaveBrowser)) {
117                                         Set (HaveBrowser);
118                                         browser = this ["browser"];
119                                         if (browser == null)
120                                                 browser = "Unknown";
121                                 }
122
123                                 return browser;
124                         }
125                 }
126
127                 public bool CDF {
128                         get {
129                                 if (!Get (HaveCDF)) {
130                                         Set (HaveCDF);
131                                         cdf = ReadBoolean ("cdf", false);
132                                 }
133
134                                 return cdf;
135                         }
136                 }
137
138                 public Version ClrVersion {
139                         get {
140                                 if (!Get (HaveClrVersion)) {
141                                         Set (HaveClrVersion);
142                                         if (ReadBoolean ("netclr", false)) {
143                                                 clrVersion = new Version (1, 0);
144                                         } else {
145                                                 clrVersion = new Version (0, 0);
146                                         }
147                                 }
148
149                                 return clrVersion;
150                         }
151                 }
152
153                 public bool Cookies {
154                         get {
155                                 if (!Get (HaveCookies)) {
156                                         Set (HaveCookies);
157                                         cookies = ReadBoolean ("cookies", false);
158                                 }
159
160                                 return cookies;
161                         }
162                 }
163
164                 public bool Crawler {
165                         get {
166                                 if (!Get (HaveCrawler)) {
167                                         Set (HaveCrawler);
168                                         crawler = ReadBoolean ("crawler", false);
169                                 }
170
171                                 return crawler;
172                         }
173                 }
174
175                 [MonoTODO]
176                 public Version EcmaScriptVersion {
177                         get {
178                                 return new Version (0, 0);
179                         }
180                 }
181
182                 public bool Frames {
183                         get {
184                                 if (!Get (HaveFrames)) {
185                                         Set (HaveFrames);
186                                         frames = ReadBoolean ("frames", false);
187                                 }
188
189                                 return frames;
190                         }
191                 }
192
193                 public bool JavaApplets {
194                         get {
195                                 if (!Get (HaveJavaApplets)) {
196                                         Set (HaveJavaApplets);
197                                         javaApplets = ReadBoolean ("javaapplets", false);
198                                 }
199
200                                 return javaApplets;
201                         }
202                 }
203
204                 public bool JavaScript {
205                         get {
206                                 if (!Get (HaveJavaScript)) {
207                                         Set (HaveJavaScript);
208                                         javaScript = ReadBoolean ("javascript", false);
209                                 }
210
211                                 return javaScript;
212                         }
213                 }
214
215                 public int MajorVersion {
216                         get {
217                                 if (!Get (HaveMajorVersion)) {
218                                         Set (HaveMajorVersion);
219                                         majorVersion = ReadInt32 ("majorver", 0);
220                                 }
221
222                                 return majorVersion;
223                         }
224                 }
225
226                 public double MinorVersion {
227                         get {
228                                 if (!Get (HaveMinorVersion)) {
229                                         Set (HaveMinorVersion);
230                                         minorVersion = ReadDouble ("minorver", 0);
231                                 }
232
233                                 return minorVersion;
234                         }
235                 }
236
237                 [MonoTODO]
238                 public Version MSDomVersion {
239                         get {
240                                 return new Version (0, 0);
241                         }
242                 }
243
244                 public string Platform {
245                         get {
246                                 if (!Get (HavePlatform)) {
247                                         Set (HavePlatform);
248                                         platform = this ["platform"];
249                                         if (platform == null)
250                                                 platform = "";
251                                 }
252
253                                 return platform;
254                         }
255                 }
256
257                 public bool Tables {
258                         get {
259                                 if (!Get (HaveTables)) {
260                                         Set (HaveTables);
261                                         tables = ReadBoolean ("tables", false);
262                                 }
263
264                                 return tables;
265                         }
266                 }
267
268                 [MonoTODO]
269                 public Type TagWriter {
270                         get {
271                                 return typeof (HtmlTextWriter);
272                         }
273                 }
274
275                 public string Type {
276                         get {
277                                 return Browser + MajorVersion;
278                         }
279                 }
280
281                 public bool VBScript {
282                         get {
283                                 if (!Get (HaveVBScript)) {
284                                         Set (HaveVBScript);
285                                         vbscript = ReadBoolean ("vbscript", false);
286                                 }
287
288                                 return vbscript;
289                         }
290                 }
291
292                 public string Version {
293                         get {
294                                 if (!Get (HaveVersion)) {
295                                         Set (HaveVersion);
296                                         version = this ["version"];
297                                         if (version == null)
298                                                 version = "";
299                                 }
300
301                                 return version;
302                         }
303                 }
304
305                 [MonoTODO]
306                 public Version W3CDomVersion {
307                         get {
308                                 return new Version (0, 0);
309                         }
310                 }
311
312                 public bool Win16 {
313                         get {
314                                 if (!Get (HaveWin16)) {
315                                         Set (HaveWin16);
316                                         win16 = ReadBoolean ("win16", false);
317                                 }
318
319                                 return win16;
320                         }
321                 }
322
323                 public bool Win32 {
324                         get {
325                                 return !Win16;
326                         }
327                 }
328
329                 bool ReadBoolean (string key, bool dflt)
330                 {
331                         string v = this [key];
332                         if (v == null)
333                                 return dflt;
334
335                         return (v == "True");
336                 }
337
338                 int ReadInt32 (string key, int dflt)
339                 {
340                         string v = this [key];
341                         if (v == null)
342                                 return dflt;
343
344                         try {
345                                 return Int32.Parse (v);
346                         } catch {
347                                 return dflt;
348                         }
349                 }
350
351                 double ReadDouble (string key, double dflt)
352                 {
353                         string v = this [key];
354                         if (v == null)
355                                 return dflt;
356
357                         try {
358                                 return Double.Parse (v);
359                         } catch {
360                                 return dflt;
361                         }
362                 }
363
364                 bool Get (int idx)
365                 {
366                         return (flags & (1 << idx)) != 0;
367                 }
368
369                 void Set (int idx)
370                 {
371                         flags |= (1 << idx);
372                 }
373         }
374 }
375