2 // Radegast Metaverse Client
3 // Copyright (c) 2009-2014, Radegast Development Team
4 // All rights reserved.
6 // Redistribution and use in source and binary forms, with or without
7 // modification, are permitted provided that the following conditions are met:
9 // * Redistributions of source code must retain the above copyright notice,
10 // this list of conditions and the following disclaimer.
11 // * Redistributions in binary form must reproduce the above copyright
12 // notice, this list of conditions and the following disclaimer in the
13 // documentation and/or other materials provided with the distribution.
14 // * Neither the name of the application "Radegast", nor the names of its
15 // contributors may be used to endorse or promote products derived from
16 // this software without specific prior written permission.
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21 // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
22 // FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24 // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
25 // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
26 // OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 // $Id: FriendshipOfferNotification.cs 118 2009-07-20 00:39:00Z latifer $
32 using System.Collections.Generic;
33 #if (COGBOT_LIBOMV || USE_STHREADS)
35 using Thread = ThreadPoolUtil.Thread;
36 using ThreadPool = ThreadPoolUtil.ThreadPool;
37 using Monitor = ThreadPoolUtil.Monitor;
41 using System.Windows.Forms;
47 /// Base class for all notificatiosn (blue dialogs)
49 public class Notification : UserControl
54 public NotificationType Type;
57 /// Callback when blue dialog notification is displayed or closed
59 /// <param name="sender">Notification dialog</param>
60 /// <param name="e">Notification parameters</param>
61 public delegate void NotificationCallback(object sender, NotificationEventArgs e);
64 /// Callback when blue dialog notification button is clicked
66 /// <param name="sender">Notification dialog</param>
67 /// <param name="e">Notification parameters</param>
68 public delegate void NotificationClickedCallback(object sender, EventArgs e, NotificationEventArgs notice);
71 /// Fired when a notification is displayed
73 public static event NotificationCallback OnNotificationDisplayed;
77 Type = NotificationType.Generic;
80 public Notification(NotificationType type)
85 protected void FireNotificationCallback(NotificationEventArgs e)
87 if (OnNotificationDisplayed == null) return;
91 WorkPool.QueueUserWorkItem((object o) => Notificaton_Displayed(this, e));
95 Console.WriteLine("" + ex);
96 OpenMetaverse.Logger.Log("Error executing notification callback", OpenMetaverse.Helpers.LogLevel.Warning, ex);
100 private void Notificaton_Displayed(Notification notification, NotificationEventArgs e)
104 e.HookNotification(this);
105 if (OnNotificationDisplayed != null)
106 OnNotificationDisplayed(notification, e);
110 Console.WriteLine("" + ex);
111 OpenMetaverse.Logger.Log("Error executing notification displayed", OpenMetaverse.Helpers.LogLevel.Warning, ex);
116 /// What kind of notification this is (blue dialog)
118 public enum NotificationType
133 /// Fired when blue dialog notification is displayed
135 public class NotificationEventArgs : EventArgs, IDisposable
137 public event Notification.NotificationCallback OnNotificationClosed;
139 public event Notification.NotificationClickedCallback OnNotificationClicked;
142 /// The Notfication form itself
144 public Notification Notice;
147 /// Type of notfication
149 public NotificationType Type;
152 /// Instance of Radegast where the event occured
154 public RadegastInstance Instance;
157 /// Notification text
159 public string Text = string.Empty;
162 /// Buttons displayed on the notification window
164 public List<Button> Buttons = new List<Button>();
167 /// When set true the Dialog Can send the Close Event
169 public bool CanClose = false;
172 /// The button has been pushed once
174 public bool ButtonSelected = false;
177 /// Create new event args object
179 /// <param name="instance">Instance of Radegast notification is coming from</param>
180 public NotificationEventArgs(RadegastInstance instance)
185 private void Notification_Closing(object sender, EventArgs e)
187 Notification_Close();
191 /// Triggers the OnNotificationClosing event.
193 internal void Notification_Close()
199 if (OnNotificationClosed != null)
200 OnNotificationClosed(this, this);
204 OpenMetaverse.Logger.Log("Error executing OnNotificationClosed " + Text,
205 OpenMetaverse.Helpers.LogLevel.Warning, ex);
207 if (!CanClose) Dispose();
213 /// Triggers the OnNotificationClicked event.
215 internal void Notification_Click(object sender, EventArgs e)
217 if (ButtonSelected) throw new InvalidOperationException("Clicked twice " + Text + " item: " + sender);
218 ButtonSelected = true;
221 if (OnNotificationClicked != null)
222 OnNotificationClicked(sender, e, this);
226 OpenMetaverse.Logger.Log("Error executing OnNotificationClicked", OpenMetaverse.Helpers.LogLevel.Warning, ex);
230 Notification_Close();
234 public void HookNotification(Notification notification)
236 Notice = notification;
240 notification.HandleDestroyed += Notification_Closing;
242 foreach (var button in Buttons)
244 button.Click += Notification_Click;
249 throw new InvalidOperationException("No buttons found on Dialog " + Text);
253 public void Dispose()
258 ButtonSelected = true;
259 Notification_Close();
263 Notice.HandleDestroyed -= Notification_Closing;
265 foreach (var button in Buttons)
266 button.Click -= Notification_Click;