Add this for backwards compatibility
[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                                 // This is the list of different windows platforms that browscap.ini has.
370                                 // Win16 Win2000 Win2003 Win32 Win95 Win98 WinME WinNT WinVI WinXP
371                                 if (!Get (HaveWin32)) {
372                                         Set (HaveWin32);
373                                         string platform = Platform;
374                                         win32 = (platform != "Win16" && platform.StartsWith ("Win"));
375                                 }
376                                 return win32;
377                         }
378                 }
379
380 #if NET_1_1
381                 public Version [] GetClrVersions ()
382                 {
383                         if ((clrVersions == null) && (clrVersion == null))
384                                 InternalGetClrVersions ();
385
386                         return clrVersions;
387                 }
388 #endif
389
390                 void InternalGetClrVersions ()
391                 {
392                         char [] anychars = new char [] { ';', ')' };
393                         string s = useragent;
394                         ArrayList list = null;
395                         int idx;
396                         while ((s != null) && (idx = s.IndexOf (".NET CLR ")) != -1) {
397                                 int end = s.IndexOfAny (anychars, idx + 9);
398                                 if (end == -1)
399                                         break;
400
401                                 string ver = s.Substring (idx + 9, end - idx - 9);
402                                 Version v = null;
403                                 try {
404                                         v = new Version (ver);
405                                         if (clrVersion == null || v > clrVersion)
406                                                 clrVersion = v;
407
408                                         if (list == null)
409                                                 list = new ArrayList (4);
410
411                                         list.Add (v);
412                                 } catch { }
413                                 s = s.Substring (idx + 9);
414                         }
415                         
416                         if (list == null || list.Count == 0) {
417                                 clrVersion = new Version ();
418 #if NET_2_0
419                                 clrVersions = null;
420 #else
421                                 clrVersions = new Version [1] { clrVersion };
422 #endif
423                         } else {
424                                 list.Sort ();
425                                 clrVersions = (Version []) list.ToArray (typeof (Version));
426                         }
427                 }
428
429                 bool ReadBoolean (string key, bool dflt)
430                 {
431                         string v = this [key];
432                         if (v == null)
433                                 return dflt;
434
435                         return (String.Compare (v, "True", true, CultureInfo.InvariantCulture) == 0);
436                 }
437
438                 int ReadInt32 (string key, int dflt)
439                 {
440                         string v = this [key];
441                         if (v == null)
442                                 return dflt;
443
444                         try {
445                                 return Int32.Parse (v);
446                         } catch {
447                                 return dflt;
448                         }
449                 }
450
451                 double ReadDouble (string key, double dflt)
452                 {
453                         string v = this [key];
454                         if (v == null)
455                                 return dflt;
456
457                         try {
458                                 return Double.Parse (v);
459                         } catch {
460                                 return dflt;
461                         }
462                 }
463
464                 bool Get (int idx)
465                 {
466                         return (flags & (1 << idx)) != 0;
467                 }
468
469                 void Set (int idx)
470                 {
471                         flags |= (1 << idx);
472                 }
473         }
474 }