* PEAPI.cs: Clean up indentation. Split into ...
[mono.git] / mcs / class / System.Web / System.Web / BrowserCapabilities.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
11 //
12 // Permission is hereby granted, free of charge, to any person obtaining
13 // a copy of this software and associated documentation files (the
14 // "Software"), to deal in the Software without restriction, including
15 // without limitation the rights to use, copy, modify, merge, publish,
16 // distribute, sublicense, and/or sell copies of the Software, and to
17 // permit persons to whom the Software is furnished to do so, subject to
18 // the following conditions:
19 // 
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
22 // 
23 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
27 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
28 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
29 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30 //
31
32 using System.Collections;
33 using System.Globalization;
34 using System.Web.Configuration;
35 using System.Web.UI;
36
37 #if NET_2_0
38 namespace System.Web.Configuration {
39         public partial class HttpCapabilitiesBase
40 #else
41
42 namespace System.Web {
43         public partial class HttpBrowserCapabilities : HttpCapabilitiesBase
44 #endif
45         {
46                 const int HaveActiveXControls = 1;
47                 const int HaveAOL = 2;
48                 const int HaveBackGroundSounds = 3;
49                 const int HaveBeta = 4;
50                 const int HaveBrowser = 5;
51                 const int HaveCDF = 6;
52                 //const int HaveClrVersion = 7;
53                 const int HaveCookies = 8;
54                 const int HaveCrawler = 9;
55                 const int HaveEcmaScriptVersion = 10;
56                 const int HaveFrames = 11;
57                 const int HaveJavaApplets = 12;
58                 const int HaveJavaScript = 13;
59                 const int HaveMajorVersion = 14;
60                 const int HaveMinorVersion = 15;
61                 //const int HaveMSDomVersion = 16;
62                 const int HavePlatform = 17;
63                 const int HaveTables = 18;
64                 //const int HaveTagWriter = 19;
65                 const int HaveVBScript = 20;
66                 const int HaveVersion = 21;
67                 const int HaveW3CDomVersion = 22;
68                 const int HaveWin16 = 23;
69                 const int HaveWin32 = 24;
70
71                 int flags;
72                 bool activeXControls;
73                 bool aol;
74                 bool backgroundSounds;
75                 bool beta;
76                 string browser;
77                 bool cdf;
78                 Version clrVersion;
79                 bool cookies;
80                 bool crawler;
81                 Version ecmaScriptVersion;
82                 bool frames;
83                 bool javaApplets;
84                 bool javaScript;
85                 int majorVersion;
86                 double minorVersion;
87                 //Version msDomVersion;
88                 string platform;
89                 bool tables;
90                 //Type tagWriter;
91                 bool vbscript;
92                 string version;
93                 Version w3CDomVersion;
94                 bool win16;
95                 bool win32;
96                 Version [] clrVersions;
97                 internal string useragent;
98
99                 public bool ActiveXControls {
100                         get {
101                                 if (!Get (HaveActiveXControls)) {
102                                         Set (HaveActiveXControls);
103                                         activeXControls = ReadBoolean ("activexcontrols", false);
104                                 }
105
106                                 return activeXControls;
107                         }
108                 }
109
110                 public bool AOL {
111                         get {
112                                 if (!Get (HaveAOL)) {
113                                         Set (HaveAOL);
114                                         aol = ReadBoolean ("aol", false);
115                                 }
116
117                                 return aol;
118                         }
119                 }
120
121                 public bool BackgroundSounds {
122                         get {
123                                 if (!Get (HaveBackGroundSounds)) {
124                                         Set (HaveBackGroundSounds);
125                                         backgroundSounds = ReadBoolean ("backgroundsounds", false);
126                                 }
127
128                                 return backgroundSounds;
129                         }
130                 }
131
132                 public bool Beta {
133                         get {
134                                 if (!Get (HaveBeta)) {
135                                         Set (HaveBeta);
136                                         beta = ReadBoolean ("beta", false);
137                                 }
138
139                                 return beta;
140                         }
141                 }
142
143                 public string Browser {
144                         get {
145                                 if (!Get (HaveBrowser)) {
146                                         Set (HaveBrowser);
147                                         browser = this ["browser"];
148                                         if (browser == null)
149                                                 browser = "Unknown";
150                                 }
151
152                                 return browser;
153                         }
154                 }
155 #if NET_2_0
156                 [MonoTODO]
157                 public ArrayList Browsers {
158                         get { throw new NotImplementedException (); }
159                 }
160 #endif
161                 public bool CDF {
162                         get {
163                                 if (!Get (HaveCDF)) {
164                                         Set (HaveCDF);
165                                         cdf = ReadBoolean ("cdf", false);
166                                 }
167
168                                 return cdf;
169                         }
170                 }
171
172                 public Version ClrVersion {
173                         get {
174                                 if (clrVersion == null)
175                                         InternalGetClrVersions ();
176
177                                 return clrVersion;
178                         }
179                 }
180
181                 public bool Cookies {
182                         get {
183                                 if (!Get (HaveCookies)) {
184                                         Set (HaveCookies);
185                                         cookies = ReadBoolean ("cookies", false);
186                                 }
187
188                                 return cookies;
189                         }
190                 }
191
192                 public bool Crawler {
193                         get {
194                                 if (!Get (HaveCrawler)) {
195                                         Set (HaveCrawler);
196                                         crawler = ReadBoolean ("crawler", false);
197                                 }
198
199                                 return crawler;
200                         }
201                 }
202
203                 public Version EcmaScriptVersion {
204                         get {
205                                 if (!Get (HaveEcmaScriptVersion)) {
206                                         string ver_str;
207                                         Set (HaveEcmaScriptVersion);
208                                         ver_str = this ["ecmascriptversion"];
209                                         if (ver_str == null)
210                                                 ecmaScriptVersion = new Version (0, 0);
211                                         else
212                                                 ecmaScriptVersion = new Version (ver_str);
213                                 }
214
215                                 return ecmaScriptVersion;
216                         }
217                 }
218
219                 public bool Frames {
220                         get {
221                                 if (!Get (HaveFrames)) {
222                                         Set (HaveFrames);
223                                         frames = ReadBoolean ("frames", false);
224                                 }
225
226                                 return frames;
227                         }
228                 }
229
230                 public bool JavaApplets {
231                         get {
232                                 if (!Get (HaveJavaApplets)) {
233                                         Set (HaveJavaApplets);
234                                         javaApplets = ReadBoolean ("javaapplets", false);
235                                 }
236
237                                 return javaApplets;
238                         }
239                 }
240
241                 public bool JavaScript {
242                         get {
243                                 if (!Get (HaveJavaScript)) {
244                                         Set (HaveJavaScript);
245                                         javaScript = ReadBoolean ("javascript", false);
246                                 }
247
248                                 return javaScript;
249                         }
250                 }
251
252                 public int MajorVersion {
253                         get {
254                                 if (!Get (HaveMajorVersion)) {
255                                         Set (HaveMajorVersion);
256                                         majorVersion = ReadInt32 ("majorver", 0);
257                                 }
258
259                                 return majorVersion;
260                         }
261                 }
262
263                 public double MinorVersion {
264                         get {
265                                 if (!Get (HaveMinorVersion)) {
266                                         Set (HaveMinorVersion);
267                                         minorVersion = ReadDouble ("minorver", 0);
268                                 }
269
270                                 return minorVersion;
271                         }
272                 }
273
274                 public Version MSDomVersion {
275                         get {
276                                 return new Version (0, 0);
277                         }
278                 }
279
280                 public string Platform {
281                         get {
282                                 if (!Get (HavePlatform)) {
283                                         Set (HavePlatform);
284                                         platform = this ["platform"];
285                                         if (platform == null)
286                                                 platform = "";
287                                 }
288
289                                 return platform;
290                         }
291                 }
292
293                 public bool Tables {
294                         get {
295                                 if (!Get (HaveTables)) {
296                                         Set (HaveTables);
297                                         tables = ReadBoolean ("tables", false);
298                                 }
299
300                                 return tables;
301                         }
302                 }
303
304                 public Type TagWriter {
305                         get {
306                                 return typeof (HtmlTextWriter);
307                         }
308                 }
309
310                 public string Type {
311                         get {
312                                 return Browser + MajorVersion;
313                         }
314                 }
315
316                 public bool VBScript {
317                         get {
318                                 if (!Get (HaveVBScript)) {
319                                         Set (HaveVBScript);
320                                         vbscript = ReadBoolean ("vbscript", false);
321                                 }
322
323                                 return vbscript;
324                         }
325                 }
326
327                 public string Version {
328                         get {
329                                 if (!Get (HaveVersion)) {
330                                         Set (HaveVersion);
331                                         version = this ["version"];
332                                         if (version == null)
333                                                 version = "";
334                                 }
335
336                                 return version;
337                         }
338                 }
339
340                 public Version W3CDomVersion {
341                         get {
342                                 if (!Get (HaveW3CDomVersion)) {
343                                         string ver_str;
344                                         Set (HaveW3CDomVersion);
345                                         ver_str = this ["w3cdomversion"];
346                                         if (ver_str == null)
347                                                 w3CDomVersion = new Version (0, 0);
348                                         else
349                                                 w3CDomVersion = new Version (ver_str);
350                                 }
351
352                                 return w3CDomVersion;
353                         }
354                 }
355
356                 public bool Win16 {
357                         get {
358                                 if (!Get (HaveWin16)) {
359                                         Set (HaveWin16);
360                                         win16 = ReadBoolean ("win16", false);
361                                 }
362
363                                 return win16;
364                         }
365                 }
366
367                 public bool Win32 {
368                         get {
369                                 if (!Get (HaveWin32)) {
370                                         Set (HaveWin32);
371                                         win32 = ReadBoolean ("win32", false);
372                                 }
373
374                                 return win32;
375                         }
376                 }
377
378 #if NET_1_1
379                 public Version [] GetClrVersions ()
380                 {
381                         if ((clrVersions == null) && (clrVersion == null))
382                                 InternalGetClrVersions ();
383
384                         return clrVersions;
385                 }
386 #endif
387
388                 void InternalGetClrVersions ()
389                 {
390                         char [] anychars = new char [] { ';', ')' };
391                         string s = useragent;
392                         ArrayList list = null;
393                         int idx;
394                         while ((s != null) && (idx = s.IndexOf (".NET CLR ")) != -1) {
395                                 int end = s.IndexOfAny (anychars, idx + 9);
396                                 if (end == -1)
397                                         break;
398
399                                 string ver = s.Substring (idx + 9, end - idx - 9);
400                                 Version v = null;
401                                 try {
402                                         v = new Version (ver);
403                                         if (clrVersion == null || v > clrVersion)
404                                                 clrVersion = v;
405
406                                         if (list == null)
407                                                 list = new ArrayList (4);
408
409                                         list.Add (v);
410                                 } catch { }
411                                 s = s.Substring (idx + 9);
412                         }
413                         
414                         if (list == null || list.Count == 0) {
415                                 clrVersion = new Version ();
416 #if NET_2_0
417                                 clrVersions = null;
418 #else
419                                 clrVersions = new Version [1] { clrVersion };
420 #endif
421                         } else {
422                                 list.Sort ();
423                                 clrVersions = (Version []) list.ToArray (typeof (Version));
424                         }
425                 }
426
427                 bool ReadBoolean (string key, bool dflt)
428                 {
429                         string v = this [key];
430                         if (v == null)
431                                 return dflt;
432
433                         return (String.Compare (v, "True", true, CultureInfo.InvariantCulture) == 0);
434                 }
435
436                 int ReadInt32 (string key, int dflt)
437                 {
438                         string v = this [key];
439                         if (v == null)
440                                 return dflt;
441
442                         try {
443                                 return Int32.Parse (v);
444                         } catch {
445                                 return dflt;
446                         }
447                 }
448
449                 double ReadDouble (string key, double dflt)
450                 {
451                         string v = this [key];
452                         if (v == null)
453                                 return dflt;
454
455                         try {
456                                 return Double.Parse (v);
457                         } catch {
458                                 return dflt;
459                         }
460                 }
461
462                 bool Get (int idx)
463                 {
464                         return (flags & (1 << idx)) != 0;
465                 }
466
467                 void Set (int idx)
468                 {
469                         flags |= (1 << idx);
470                 }
471         }
472 }