* roottypes.cs: Rename from tree.cs.
[mono.git] / mcs / class / System.Web / System.Web.UI.WebControls / AdRotator.cs
1 // 
2 // System.Web.UI.WebControls.AdRotator
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.Xml;
30 using System.Collections;
31 using System.ComponentModel;
32 using System.Security.Permissions;
33
34 namespace System.Web.UI.WebControls {
35
36         // CAS
37         [AspNetHostingPermissionAttribute (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
38         [AspNetHostingPermissionAttribute (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
39         // attributes
40         [DefaultEvent("AdCreated")]
41         [DefaultProperty("AdvertisementFile")]
42         [Designer("System.Web.UI.Design.WebControls.AdRotatorDesigner, " + Consts.AssemblySystem_Design, "System.ComponentModel.Design.IDesigner")]
43 #if NET_2_0
44         [ToolboxData("<{0}:AdRotator runat=\"server\"></{0}:AdRotator>")]
45 #else
46         [ToolboxData("<{0}:AdRotator runat=\"server\" Height=\"60px\" Width=\"468px\"></{0}:AdRotator>")]
47 #endif          
48         public class AdRotator :
49 #if NET_2_0
50         DataBoundControl
51 #else           
52         WebControl
53 #endif  
54         {
55 #if NET_2_0
56                 [MonoTODO]
57                 protected internal override void OnInit (EventArgs e)
58                 {
59                         base.OnInit(e);
60                 }
61 #endif
62                 
63 #if NET_2_0
64                 protected internal
65 #else           
66                 protected
67 #endif          
68                 override void OnPreRender (EventArgs eee)
69                 {
70                         Hashtable ht = null;
71                         
72                         if (ad_file != "" && ad_file != null) {
73                                 ReadAdsFromFile (Page.MapPath (ad_file));
74                                 ht = ChooseAd ();
75                         }
76
77                         AdCreatedEventArgs e = new AdCreatedEventArgs (ht);
78                         OnAdCreated (e);
79                         createdargs = e;
80                         
81                 }
82
83 #if NET_2_0
84                 [MonoTODO]
85                 protected internal override void PerformDataBinding (IEnumerable data)
86                 {
87                         throw new NotImplementedException ();
88                 }
89
90                 [MonoTODO]
91                 protected override void PerformSelect ()
92                 {
93                         throw new NotImplementedException ();
94                 }
95 #endif          
96
97                 AdCreatedEventArgs createdargs;
98
99 #if NET_2_0
100                 protected internal
101 #else           
102                 protected
103 #endif          
104                 override void Render (HtmlTextWriter w)
105                 {
106                         AdCreatedEventArgs e = createdargs;
107
108                         base.AddAttributesToRender (w);
109                         
110                         if (e.NavigateUrl != null)
111                                 w.AddAttribute (HtmlTextWriterAttribute.Href, ResolveUrl (e.NavigateUrl));
112                         w.AddAttribute (HtmlTextWriterAttribute.Target, Target);
113                         
114                         w.RenderBeginTag (HtmlTextWriterTag.A);
115
116                         if (e.NavigateUrl != null)
117                                 w.AddAttribute (HtmlTextWriterAttribute.Src, ResolveUrl (e.ImageUrl));
118                         
119                         w.AddAttribute (HtmlTextWriterAttribute.Alt, e.AlternateText == null ? "" : e.AlternateText);
120                         w.AddAttribute (HtmlTextWriterAttribute.Border, "0");
121                         w.RenderBeginTag (HtmlTextWriterTag.Img);
122                         w.RenderEndTag (); // img
123                         w.RenderEndTag (); // a
124                 }
125
126                 //
127                 // We take all the ads in the ad file and add up their
128                 // impression weight. We then take a random number [0,
129                 // TotalImpressions). We then choose the ad that is
130                 // that number or less impressions for the beginning
131                 // of the file. This lets us respect the weights
132                 // given.
133                 //
134                 Hashtable ChooseAd ()
135                 {
136                         // cache for performance
137                         string KeywordFilter = this.KeywordFilter;
138                         
139                         int total_imp = 0;
140                         int cur_imp = 0;
141                         
142                         foreach (Hashtable a in ads) {
143                                 if (KeywordFilter == "" || KeywordFilter == (string) a ["Keyword"])
144                                         total_imp += a ["Impressions"] != null ? int.Parse ((string) a ["Impressions"]) : 1;
145                         }
146
147                         int r = new Random ().Next (total_imp);
148
149                         foreach (Hashtable a in ads) {
150                                 if (KeywordFilter != "" && KeywordFilter != (string) a ["Keyword"])
151                                         continue;
152                                 cur_imp += a ["Impressions"] != null ? int.Parse ((string) a ["Impressions"]) : 1;
153                                 
154                                 if (cur_imp > r)
155                                         return a;
156                         }
157
158                         if (total_imp != 0)
159                                 throw new Exception ("I should only get here if no ads matched");
160                         
161                         return null;
162                 }
163
164                 ArrayList ads = new ArrayList ();
165                 
166                 void ReadAdsFromFile (string s)
167                 {
168                         XmlDocument d = new XmlDocument ();
169                         try {
170                                 d.Load (s);
171                         } catch (Exception e) {
172                                 throw new HttpException ("AdRotator could not parse the xml file", e);
173                         }
174                         
175                         ads.Clear ();
176                         
177                         foreach (XmlNode n in d.DocumentElement.ChildNodes) {
178
179                                 Hashtable ad = new Hashtable ();
180                                 
181                                 foreach (XmlNode nn in n.ChildNodes)
182                                         ad.Add (nn.Name, nn.InnerText);
183                                 
184                                 ads.Add (ad);
185                         }
186                 }
187                 
188                 string ad_file = "";
189
190 #if NET_2_0
191                 [UrlProperty]
192 #endif          
193                 [Bindable(true)]
194                 [DefaultValue("")]
195                 [Editor("System.Web.UI.Design.XmlUrlEditor, " + Consts.AssemblySystem_Design, typeof(System.Drawing.Design.UITypeEditor))]
196                 [WebSysDescription("")]
197                 [WebCategory("Behavior")]
198                 public string AdvertisementFile {
199                         get {
200                                 return ad_file;
201                         }
202                         set {
203                                 ad_file = value;
204                         }
205                         
206                 }
207
208 #if NET_2_0
209                 [DefaultValue ("AlternateText")]
210                 [WebSysDescriptionAttribute ("")]
211                 [WebCategoryAttribute ("Behavior")]
212                 [MonoTODO]
213                 public string AlternateTextField 
214                 {
215                         get {
216                                 throw new NotImplementedException ();
217                         }
218                         set {
219                                 throw new NotImplementedException ();
220                         }
221                 }
222 #endif          
223                 
224                 [Browsable(false)]
225                 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
226                 [EditorBrowsable(EditorBrowsableState.Never)]
227                 public override FontInfo Font {
228                         get {
229                                 return base.Font;
230                         }
231                 }
232
233 #if NET_2_0
234                 [DefaultValue ("ImageUrl")]
235                 [MonoTODO]
236                 [WebSysDescriptionAttribute ("")]
237                 [WebCategoryAttribute ("Behavior")]
238                 public string ImageUrlField 
239                 {
240                         get {
241                                 throw new NotImplementedException ();
242                         }
243                         set {
244                                 throw new NotImplementedException ();
245                         }
246                 }
247 #endif          
248                 
249
250                 [Bindable(true)]
251                 [DefaultValue("")]
252                 [WebSysDescription("")]
253                 [WebCategory("Behavior")]
254                 public string KeywordFilter {
255                         get {
256                                 return ViewState.GetString ("KeywordFilter", "");
257                         }
258                         set {
259                                 ViewState ["KeywordFilter"] = value;
260                         }
261                         
262                 }
263
264 #if NET_2_0
265                 [DefaultValue ("NavigateUrl")]
266                 [MonoTODO]
267                 [WebSysDescriptionAttribute ("")]
268                 [WebCategoryAttribute ("Behavior")]
269                 public string NavigateUrlField 
270                 {
271                         get {
272                                 throw new NotImplementedException ();
273                         }
274                         set {
275                                 throw new NotImplementedException ();
276                         }
277                 }
278 #endif          
279                 
280                 [Bindable(true)]
281                 [DefaultValue("_top")]
282                 [TypeConverter(typeof(System.Web.UI.WebControls.TargetConverter))]
283                 [WebSysDescription("")]
284                 [WebCategory("Behavior")]
285                 public string Target {
286                         get {
287                                 return ViewState.GetString ("Target", "_top");
288                         }
289                         set {
290                                 ViewState ["Target"] = value;
291                         }
292                         
293                 }
294
295 #if NET_2_0
296                 /* all these are listed in corcompare */
297                 [MonoTODO]
298                 public override string UniqueID
299                 {
300                         get {
301                                 return base.UniqueID;
302                         }
303                 }
304
305                 [MonoTODO]
306                 protected override HtmlTextWriterTag TagKey 
307                 {
308                         get {
309                                 return base.TagKey;
310                         }
311                 }
312 #endif          
313         
314                 protected virtual void OnAdCreated (AdCreatedEventArgs e)
315                 {
316                         AdCreatedEventHandler h = (AdCreatedEventHandler) Events [AdCreatedEvent];
317                         if (h != null)
318                                 h (this, e);
319                 }
320
321                 static readonly object AdCreatedEvent = new object ();
322
323                 [WebSysDescription("")]
324                 [WebCategory("Action")]
325                 public event AdCreatedEventHandler AdCreated {
326                         add { Events.AddHandler (AdCreatedEvent, value); }
327                         remove { Events.RemoveHandler (AdCreatedEvent, value); }
328                 }
329         }
330 }