2 // Radegast Metaverse Client
\r
3 // Copyright (c) 2009, Radegast Development Team
\r
4 // All rights reserved.
\r
6 // Redistribution and use in source and binary forms, with or without
\r
7 // modification, are permitted provided that the following conditions are met:
\r
9 // * Redistributions of source code must retain the above copyright notice,
\r
10 // this list of conditions and the following disclaimer.
\r
11 // * Redistributions in binary form must reproduce the above copyright
\r
12 // notice, this list of conditions and the following disclaimer in the
\r
13 // documentation and/or other materials provided with the distribution.
\r
14 // * Neither the name of the application "Radegast", nor the names of its
\r
15 // contributors may be used to endorse or promote products derived from
\r
16 // this software without specific prior written permission.
\r
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
\r
19 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
\r
20 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
\r
21 // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
\r
22 // FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
\r
23 // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
\r
24 // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
\r
25 // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
\r
26 // OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
\r
27 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
\r
32 using System.Drawing;
\r
33 using System.Collections.Generic;
\r
34 using System.Windows.Forms;
\r
35 using System.Threading;
\r
36 using Radegast.Netcom;
\r
37 using OpenMetaverse;
\r
41 public partial class GroupIMTabWindow : UserControl
\r
43 private RadegastInstance instance;
\r
44 private GridClient client { get { return instance.Client; } }
\r
45 private RadegastNetcom netcom { get { return instance.Netcom; } }
\r
46 private UUID session;
\r
47 private IMTextManager textManager;
\r
48 private object AvatarListSyncRoot = new object();
\r
50 ManualResetEvent WaitForSessionStart = new ManualResetEvent(false);
\r
52 public GroupIMTabWindow(RadegastInstance instance, UUID session, string sessionName)
\r
54 InitializeComponent();
\r
55 Disposed += new EventHandler(IMTabWindow_Disposed);
\r
57 this.instance = instance;
\r
58 this.session = session;
\r
60 textManager = new IMTextManager(this.instance, new RichTextBoxPrinter(rtbIMText), IMTextManagerType.Group, this.session, sessionName);
\r
62 btnShow.Text = "Show";
\r
63 chatSplit.Panel2Collapsed = true;
\r
66 RegisterClientEvents(client);
\r
67 client.Self.RequestJoinGroupChat(session);
\r
72 private void RegisterClientEvents(GridClient client)
\r
74 client.Self.GroupChatJoined += new EventHandler<GroupChatJoinedEventArgs>(Self_GroupChatJoined);
\r
75 client.Self.ChatSessionMemberAdded += new EventHandler<ChatSessionMemberAddedEventArgs>(Self_ChatSessionMemberAdded);
\r
76 client.Self.ChatSessionMemberLeft += new EventHandler<ChatSessionMemberLeftEventArgs>(Self_ChatSessionMemberLeft);
\r
77 client.Avatars.UUIDNameReply += new EventHandler<UUIDNameReplyEventArgs>(Avatars_UUIDNameReply);
\r
78 instance.Netcom.ClientConnected += new EventHandler<EventArgs>(Netcom_Connected);
\r
79 instance.Netcom.ClientDisconnected += new EventHandler<DisconnectedEventArgs>(Netcom_Disconnected);
\r
80 instance.GlobalSettings.OnSettingChanged += new Settings.SettingChangedCallback(GlobalSettings_OnSettingChanged);
\r
81 instance.ClientChanged += new EventHandler<ClientChangedEventArgs>(instance_ClientChanged);
\r
84 private void UnregisterClientEvents(GridClient client)
\r
86 client.Self.GroupChatJoined -= new EventHandler<GroupChatJoinedEventArgs>(Self_GroupChatJoined);
\r
87 client.Self.ChatSessionMemberAdded -= new EventHandler<ChatSessionMemberAddedEventArgs>(Self_ChatSessionMemberAdded);
\r
88 client.Self.ChatSessionMemberLeft -= new EventHandler<ChatSessionMemberLeftEventArgs>(Self_ChatSessionMemberLeft);
\r
89 client.Avatars.UUIDNameReply -= new EventHandler<UUIDNameReplyEventArgs>(Avatars_UUIDNameReply);
\r
90 instance.Netcom.ClientConnected -= new EventHandler<EventArgs>(Netcom_Connected);
\r
91 instance.Netcom.ClientDisconnected -= new EventHandler<DisconnectedEventArgs>(Netcom_Disconnected);
\r
92 instance.GlobalSettings.OnSettingChanged -= new Settings.SettingChangedCallback(GlobalSettings_OnSettingChanged);
\r
93 instance.ClientChanged -= new EventHandler<ClientChangedEventArgs>(instance_ClientChanged);
\r
96 void instance_ClientChanged(object sender, ClientChangedEventArgs e)
\r
98 UnregisterClientEvents(e.OldClient);
\r
99 RegisterClientEvents(client);
\r
102 private void IMTabWindow_Disposed(object sender, EventArgs e)
\r
104 if (instance.Netcom.IsLoggedIn)
\r
106 client.Self.RequestLeaveGroupChat(session);
\r
109 UnregisterClientEvents(client);
\r
113 void UpdateFontSize()
\r
115 float size = (float)instance.GlobalSettings["chat_font_size"].AsReal();
\r
116 cbxInput.Font = ChatConsole.ChangeFontSize(cbxInput.Font, size);
\r
117 rtbIMText.Font = ChatConsole.ChangeFontSize(rtbIMText.Font, size);
\r
118 textManager.ReprintAllText();
\r
121 void GlobalSettings_OnSettingChanged(object sender, SettingsEventArgs e)
\r
123 if (e.Key == "chat_font_size")
\r
127 void Netcom_Disconnected(object sender, DisconnectedEventArgs e)
\r
132 void Netcom_Connected(object sender, EventArgs e)
\r
134 client.Self.RequestJoinGroupChat(session);
\r
138 void Avatars_UUIDNameReply(object sender, UUIDNameReplyEventArgs e)
\r
140 if (InvokeRequired)
\r
142 if (IsHandleCreated)
\r
143 BeginInvoke(new MethodInvoker(() => Avatars_UUIDNameReply(sender, e)));
\r
147 lock (AvatarListSyncRoot)
\r
150 Participants.BeginUpdate();
\r
152 foreach (KeyValuePair<UUID, string> kvp in e.Names)
\r
154 if (Participants.Items.ContainsKey(kvp.Key.ToString()))
\r
155 Participants.Items[kvp.Key.ToString()].Text = kvp.Value;
\r
158 Participants.Sort();
\r
159 Participants.EndUpdate();
\r
163 void Self_ChatSessionMemberLeft(object sender, ChatSessionMemberLeftEventArgs e)
\r
165 if (e.SessionID == session)
\r
166 UpdateParticipantList();
\r
169 void Self_ChatSessionMemberAdded(object sender, ChatSessionMemberAddedEventArgs e)
\r
171 if (e.SessionID == session)
\r
172 UpdateParticipantList();
\r
175 void UpdateParticipantList()
\r
177 if (InvokeRequired)
\r
179 if (IsHandleCreated)
\r
180 BeginInvoke(new MethodInvoker(UpdateParticipantList));
\r
186 lock (AvatarListSyncRoot)
\r
188 Participants.BeginUpdate();
\r
189 Participants.Items.Clear();
\r
191 List<ChatSessionMember> participants;
\r
192 List<UUID> nameLookup = new List<UUID>();
\r
194 if (client.Self.GroupChatSessions.TryGetValue(session, out participants))
\r
196 ChatSessionMember[] members = participants.ToArray();
\r
197 for (int i = 0; i < members.Length; i++)
\r
199 ChatSessionMember participant = members[i];
\r
200 ListViewItem item = new ListViewItem();
\r
201 item.Name = participant.AvatarKey.ToString();
\r
202 if (instance.nameCache.ContainsKey(participant.AvatarKey))
\r
204 item.Text = instance.nameCache[participant.AvatarKey];
\r
208 item.Text = RadegastInstance.INCOMPLETE_NAME;
\r
209 nameLookup.Add(participant.AvatarKey);
\r
211 if (participant.IsModerator)
\r
212 item.Font = new Font(item.Font, FontStyle.Bold);
\r
213 Participants.Items.Add(item);
\r
216 if (nameLookup.Count > 0)
\r
217 client.Avatars.RequestAvatarNames(nameLookup);
\r
220 Participants.Sort();
\r
221 Participants.EndUpdate();
\r
226 Participants.EndUpdate();
\r
230 void Self_GroupChatJoined(object sender, GroupChatJoinedEventArgs e)
\r
232 if (e.SessionID != session && e.TmpSessionID != session)
\r
237 if (InvokeRequired)
\r
239 if (IsHandleCreated)
\r
240 Invoke(new MethodInvoker(() => Self_GroupChatJoined(sender, e)));
\r
246 textManager.TextPrinter.PrintTextLine("Join Group Chat Success!", Color.Green);
\r
247 WaitForSessionStart.Set();
\r
251 textManager.TextPrinter.PrintTextLine("Join Group Chat failed.", Color.Red);
\r
255 public void CleanUp()
\r
257 textManager.CleanUp();
\r
258 textManager = null;
\r
262 private void btnSend_Click(object sender, EventArgs e)
\r
265 this.ClearIMInput();
\r
268 private void btnShow_Click(object sender, EventArgs e)
\r
270 if (chatSplit.Panel2Collapsed)
\r
272 chatSplit.Panel2Collapsed = false;
\r
273 btnShow.Text = "Hide";
\r
277 chatSplit.Panel2Collapsed = true;
\r
278 btnShow.Text = "Show";
\r
282 private void cbxInput_TextChanged(object sender, EventArgs e)
\r
287 private void RefreshControls()
\r
289 if (InvokeRequired)
\r
291 if (IsHandleCreated)
\r
292 BeginInvoke(new MethodInvoker(RefreshControls));
\r
296 if (!netcom.IsLoggedIn)
\r
298 cbxInput.Enabled = false;
\r
299 btnSend.Enabled = false;
\r
300 btnShow.Enabled = false;
\r
301 btnShow.Text = "Show";
\r
302 chatSplit.Panel2Collapsed = true;
\r
306 cbxInput.Enabled = true;
\r
307 btnShow.Enabled = true;
\r
309 if (cbxInput.Text.Length > 0)
\r
311 btnSend.Enabled = true;
\r
315 btnSend.Enabled = false;
\r
319 private void cbxInput_KeyUp(object sender, KeyEventArgs e)
\r
321 if (e.KeyCode != Keys.Enter) return;
\r
322 e.SuppressKeyPress = true;
\r
327 private void SendMsg()
\r
329 if (cbxInput.Text.Length == 0) return;
\r
331 string message = cbxInput.Text.Replace(ChatInputBox.NewlineMarker, "\n");
\r
333 if (instance.GlobalSettings["mu_emotes"].AsBoolean() && message.StartsWith(":"))
\r
335 message = "/me " + message.Substring(1);
\r
338 this.ClearIMInput();
\r
340 if (instance.RLV.RestictionActive("sendim")) return;
\r
342 if (message.Length > 1023) message = message.Remove(1023);
\r
344 if (!client.Self.GroupChatSessions.ContainsKey(session))
\r
346 WaitForSessionStart.Reset();
\r
347 client.Self.RequestJoinGroupChat(session);
\r
351 WaitForSessionStart.Set();
\r
354 if (WaitForSessionStart.WaitOne(10000, false))
\r
356 client.Self.InstantMessageGroup(session, message);
\r
360 textManager.TextPrinter.PrintTextLine("Cannot send group IM.", Color.Red);
\r
364 private void ClearIMInput()
\r
366 cbxInput.Text = string.Empty;
\r
369 public void SelectIMInput()
\r
374 private void rtbIMText_LinkClicked(object sender, LinkClickedEventArgs e)
\r
376 instance.MainForm.ProcessLink(e.LinkText);
\r
379 private void cbxInput_KeyDown(object sender, KeyEventArgs e)
\r
381 if (e.KeyCode == Keys.Enter) e.SuppressKeyPress = true;
\r
384 public UUID SessionId
\r
386 get { return session; }
\r
387 set { session = value; }
\r
390 public IMTextManager TextManager
\r
392 get { return textManager; }
\r
393 set { textManager = value; }
\r
396 private void Participants_MouseDoubleClick(object sender, MouseEventArgs e)
\r
398 ListViewItem item = Participants.GetItemAt(e.X, e.Y);
\r
403 instance.MainForm.ShowAgentProfile(item.Text, new UUID(item.Name));
\r
405 catch (Exception) { }
\r
409 private void cbxInput_VisibleChanged(object sender, EventArgs e)
\r
411 if (Visible) cbxInput.Focus();
\r
414 private void cbxInput_SizeChanged(object sender, EventArgs e)
\r
416 pnlChatInput.Height = cbxInput.Height + 7;
\r