2 // Radegast Metaverse Client
3 // Copyright (c) 2009, 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.
33 using System.Collections.Generic;
34 using System.Windows.Forms;
35 using System.Threading;
36 using Radegast.Netcom;
41 public partial class GroupIMTabWindow : UserControl
43 private RadegastInstance instance;
44 private GridClient client { get { return instance.Client; } }
45 private RadegastNetcom netcom { get { return instance.Netcom; } }
47 private IMTextManager textManager;
48 private object AvatarListSyncRoot = new object();
50 ManualResetEvent WaitForSessionStart = new ManualResetEvent(false);
52 public GroupIMTabWindow(RadegastInstance instance, UUID session, string sessionName)
54 InitializeComponent();
55 Disposed += new EventHandler(IMTabWindow_Disposed);
57 this.instance = instance;
58 this.session = session;
60 textManager = new IMTextManager(this.instance, new RichTextBoxPrinter(rtbIMText), IMTextManagerType.Group, this.session, sessionName);
62 btnShow.Text = "Show";
63 chatSplit.Panel2Collapsed = true;
66 RegisterClientEvents(client);
67 client.Self.RequestJoinGroupChat(session);
72 private void RegisterClientEvents(GridClient client)
74 client.Self.GroupChatJoined += new EventHandler<GroupChatJoinedEventArgs>(Self_GroupChatJoined);
75 client.Self.ChatSessionMemberAdded += new EventHandler<ChatSessionMemberAddedEventArgs>(Self_ChatSessionMemberAdded);
76 client.Self.ChatSessionMemberLeft += new EventHandler<ChatSessionMemberLeftEventArgs>(Self_ChatSessionMemberLeft);
77 client.Avatars.UUIDNameReply += new EventHandler<UUIDNameReplyEventArgs>(Avatars_UUIDNameReply);
78 instance.Netcom.ClientConnected += new EventHandler<EventArgs>(Netcom_Connected);
79 instance.Netcom.ClientDisconnected += new EventHandler<DisconnectedEventArgs>(Netcom_Disconnected);
80 instance.GlobalSettings.OnSettingChanged += new Settings.SettingChangedCallback(GlobalSettings_OnSettingChanged);
81 instance.ClientChanged += new EventHandler<ClientChangedEventArgs>(instance_ClientChanged);
84 private void UnregisterClientEvents(GridClient client)
86 client.Self.GroupChatJoined -= new EventHandler<GroupChatJoinedEventArgs>(Self_GroupChatJoined);
87 client.Self.ChatSessionMemberAdded -= new EventHandler<ChatSessionMemberAddedEventArgs>(Self_ChatSessionMemberAdded);
88 client.Self.ChatSessionMemberLeft -= new EventHandler<ChatSessionMemberLeftEventArgs>(Self_ChatSessionMemberLeft);
89 client.Avatars.UUIDNameReply -= new EventHandler<UUIDNameReplyEventArgs>(Avatars_UUIDNameReply);
90 instance.Netcom.ClientConnected -= new EventHandler<EventArgs>(Netcom_Connected);
91 instance.Netcom.ClientDisconnected -= new EventHandler<DisconnectedEventArgs>(Netcom_Disconnected);
92 instance.GlobalSettings.OnSettingChanged -= new Settings.SettingChangedCallback(GlobalSettings_OnSettingChanged);
93 instance.ClientChanged -= new EventHandler<ClientChangedEventArgs>(instance_ClientChanged);
96 void instance_ClientChanged(object sender, ClientChangedEventArgs e)
98 UnregisterClientEvents(e.OldClient);
99 RegisterClientEvents(client);
102 private void IMTabWindow_Disposed(object sender, EventArgs e)
104 if (instance.Netcom.IsLoggedIn)
106 client.Self.RequestLeaveGroupChat(session);
109 UnregisterClientEvents(client);
113 void UpdateFontSize()
115 float size = (float)instance.GlobalSettings["chat_font_size"].AsReal();
116 cbxInput.Font = ChatConsole.ChangeFontSize(cbxInput.Font, size);
117 rtbIMText.Font = ChatConsole.ChangeFontSize(rtbIMText.Font, size);
118 textManager.ReprintAllText();
121 void GlobalSettings_OnSettingChanged(object sender, SettingsEventArgs e)
123 if (e.Key == "chat_font_size")
127 void Netcom_Disconnected(object sender, DisconnectedEventArgs e)
132 void Netcom_Connected(object sender, EventArgs e)
134 client.Self.RequestJoinGroupChat(session);
138 void Avatars_UUIDNameReply(object sender, UUIDNameReplyEventArgs e)
143 BeginInvoke(new MethodInvoker(() => Avatars_UUIDNameReply(sender, e)));
147 lock (AvatarListSyncRoot)
150 Participants.BeginUpdate();
152 foreach (KeyValuePair<UUID, string> kvp in e.Names)
154 if (Participants.Items.ContainsKey(kvp.Key.ToString()))
155 Participants.Items[kvp.Key.ToString()].Text = kvp.Value;
159 Participants.EndUpdate();
163 void Self_ChatSessionMemberLeft(object sender, ChatSessionMemberLeftEventArgs e)
165 if (e.SessionID == session)
166 UpdateParticipantList();
169 void Self_ChatSessionMemberAdded(object sender, ChatSessionMemberAddedEventArgs e)
171 if (e.SessionID == session)
172 UpdateParticipantList();
175 void UpdateParticipantList()
180 BeginInvoke(new MethodInvoker(UpdateParticipantList));
186 lock (AvatarListSyncRoot)
188 Participants.BeginUpdate();
189 Participants.Items.Clear();
191 List<ChatSessionMember> participants;
192 List<UUID> nameLookup = new List<UUID>();
194 if (client.Self.GroupChatSessions.TryGetValue(session, out participants))
196 ChatSessionMember[] members = participants.ToArray();
197 for (int i = 0; i < members.Length; i++)
199 ChatSessionMember participant = members[i];
200 ListViewItem item = new ListViewItem();
201 item.Name = participant.AvatarKey.ToString();
202 if (instance.nameCache.ContainsKey(participant.AvatarKey))
204 item.Text = instance.nameCache[participant.AvatarKey];
208 item.Text = RadegastInstance.INCOMPLETE_NAME;
209 nameLookup.Add(participant.AvatarKey);
211 if (participant.IsModerator)
212 item.Font = new Font(item.Font, FontStyle.Bold);
213 Participants.Items.Add(item);
216 if (nameLookup.Count > 0)
217 client.Avatars.RequestAvatarNames(nameLookup);
221 Participants.EndUpdate();
226 Participants.EndUpdate();
230 void Self_GroupChatJoined(object sender, GroupChatJoinedEventArgs e)
232 if (e.SessionID != session && e.TmpSessionID != session)
240 Invoke(new MethodInvoker(() => Self_GroupChatJoined(sender, e)));
246 textManager.TextPrinter.PrintTextLine("Join Group Chat Success!", Color.Green);
247 WaitForSessionStart.Set();
251 textManager.TextPrinter.PrintTextLine("Join Group Chat failed.", Color.Red);
255 public void CleanUp()
257 textManager.CleanUp();
262 private void btnSend_Click(object sender, EventArgs e)
268 private void btnShow_Click(object sender, EventArgs e)
270 if (chatSplit.Panel2Collapsed)
272 chatSplit.Panel2Collapsed = false;
273 btnShow.Text = "Hide";
277 chatSplit.Panel2Collapsed = true;
278 btnShow.Text = "Show";
282 private void cbxInput_TextChanged(object sender, EventArgs e)
287 private void RefreshControls()
292 BeginInvoke(new MethodInvoker(RefreshControls));
296 if (!netcom.IsLoggedIn)
298 cbxInput.Enabled = false;
299 btnSend.Enabled = false;
300 btnShow.Enabled = false;
301 btnShow.Text = "Show";
302 chatSplit.Panel2Collapsed = true;
306 cbxInput.Enabled = true;
307 btnShow.Enabled = true;
309 if (cbxInput.Text.Length > 0)
311 btnSend.Enabled = true;
315 btnSend.Enabled = false;
319 private void cbxInput_KeyUp(object sender, KeyEventArgs e)
321 if (e.KeyCode != Keys.Enter) return;
322 e.SuppressKeyPress = true;
327 private void SendMsg()
329 if (cbxInput.Text.Length == 0) return;
331 string message = cbxInput.Text.Replace(ChatInputBox.NewlineMarker, "\n");
333 if (instance.GlobalSettings["mu_emotes"].AsBoolean() && message.StartsWith(":"))
335 message = "/me " + message.Substring(1);
340 if (instance.RLV.RestictionActive("sendim")) return;
342 if (message.Length > 1023) message = message.Remove(1023);
344 if (!client.Self.GroupChatSessions.ContainsKey(session))
346 WaitForSessionStart.Reset();
347 client.Self.RequestJoinGroupChat(session);
351 WaitForSessionStart.Set();
354 if (WaitForSessionStart.WaitOne(10000, false))
356 client.Self.InstantMessageGroup(session, message);
360 textManager.TextPrinter.PrintTextLine("Cannot send group IM.", Color.Red);
364 private void ClearIMInput()
366 cbxInput.Text = string.Empty;
369 public void SelectIMInput()
374 private void rtbIMText_LinkClicked(object sender, LinkClickedEventArgs e)
376 instance.MainForm.ProcessLink(e.LinkText);
379 private void cbxInput_KeyDown(object sender, KeyEventArgs e)
381 if (e.KeyCode == Keys.Enter) e.SuppressKeyPress = true;
384 public UUID SessionId
386 get { return session; }
387 set { session = value; }
390 public IMTextManager TextManager
392 get { return textManager; }
393 set { textManager = value; }
396 private void Participants_MouseDoubleClick(object sender, MouseEventArgs e)
398 ListViewItem item = Participants.GetItemAt(e.X, e.Y);
403 instance.MainForm.ShowAgentProfile(item.Text, new UUID(item.Name));
405 catch (Exception) { }
409 private void cbxInput_VisibleChanged(object sender, EventArgs e)
411 if (Visible) cbxInput.Focus();
414 private void cbxInput_SizeChanged(object sender, EventArgs e)
416 pnlChatInput.Height = cbxInput.Height + 7;