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