2005-08-04 Ben Maurer <bmaurer@ximian.com>
[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 // (c) 2005 Novell
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
33 namespace System.Web.UI.WebControls {
34         [DefaultEvent("AdCreated")]
35         [DefaultProperty("AdvertisementFile")]
36         [Designer("System.Web.UI.Design.WebControls.AdRotatorDesigner, " + Consts.AssemblySystem_Design, "System.ComponentModel.Design.IDesigner")]
37 #if NET_2_0
38         [ToolboxData("<{0}:AdRotator runat=\"server\"></{0}:AdRotator>")]
39 #else
40         [ToolboxData("<{0}:AdRotator runat=\"server\" Height=\"60px\" Width=\"468px\"></{0}:AdRotator>")]
41 #endif          
42         public class AdRotator :
43 #if NET_2_0
44         DataBoundControl
45 #else           
46         WebControl
47 #endif  
48         {
49         
50                 protected override ControlCollection CreateControlCollection ()
51                 {
52                         return new EmptyControlCollection (this);
53                 }
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                 public string AdvertisementFile {
197                         get {
198                                 return ad_file;
199                         }
200                         set {
201                                 ad_file = value;
202                         }
203                         
204                 }
205
206 #if NET_2_0
207                 [DefaultValue ("AlternateText")]
208                 [MonoTODO]
209                 public string AlternateTextField 
210                 {
211                         get {
212                                 throw new NotImplementedException ();
213                         }
214                         set {
215                                 throw new NotImplementedException ();
216                         }
217                 }
218 #endif          
219                 
220                 [Browsable(false)]
221                 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
222                 [EditorBrowsable(EditorBrowsableState.Never)]
223                 public override FontInfo Font {
224                         get {
225                                 return base.Font;
226                         }
227                 }
228
229 #if NET_2_0
230                 [DefaultValue ("ImageUrl")]
231                 [MonoTODO]
232                 public string ImageUrlField 
233                 {
234                         get {
235                                 throw new NotImplementedException ();
236                         }
237                         set {
238                                 throw new NotImplementedException ();
239                         }
240                 }
241 #endif          
242                 
243
244                 [Bindable(true)]
245                 [DefaultValue("")]
246                 public string KeywordFilter {
247                         get {
248                                 return ViewState.GetString ("KeywordFilter", "");
249                         }
250                         set {
251                                 ViewState ["KeywordFilter"] = value;
252                         }
253                         
254                 }
255
256 #if NET_2_0
257                 [DefaultValue ("NavigateUrl")]
258                 [MonoTODO]
259                 public string NavigateUrlField 
260                 {
261                         get {
262                                 throw new NotImplementedException ();
263                         }
264                         set {
265                                 throw new NotImplementedException ();
266                         }
267                 }
268
269                 /* listed in corcompare */
270                 [MonoTODO]
271                 public override Page Page 
272                 {
273                         get {
274                                 return base.Page;
275                         }
276                         set {
277                                 base.Page = value;
278                         }
279                 }
280 #endif          
281                 
282                 [Bindable(true)]
283                 [DefaultValue("_top")]
284                 [TypeConverter(typeof(System.Web.UI.WebControls.TargetConverter))]
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
313                 [MonoTODO]
314                 protected override StateBag ViewState 
315                 {
316                         get {
317                                 return base.ViewState;
318                         }
319                 }
320 #endif          
321         
322                 protected virtual void OnAdCreated (AdCreatedEventArgs e)
323                 {
324                         AdCreatedEventHandler h = (AdCreatedEventHandler) Events [AdCreatedEvent];
325                         if (h != null)
326                                 h (this, e);
327                 }
328                 static readonly object AdCreatedEvent = new object ();
329                 public event AdCreatedEventHandler AdCreated {
330                         add { Events.AddHandler (AdCreatedEvent, value); }
331                         remove { Events.RemoveHandler (AdCreatedEvent, value); }
332                 }
333         }
334 }