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();
49 private List<string> chatHistory = new List<string>();
50 private int chatPointer;
52 ManualResetEvent WaitForSessionStart = new ManualResetEvent(false);
54 public GroupIMTabWindow(RadegastInstance instance, UUID session, string sessionName)
56 InitializeComponent();
57 Disposed += new EventHandler(IMTabWindow_Disposed);
59 this.instance = instance;
60 this.session = session;
62 textManager = new IMTextManager(this.instance, new RichTextBoxPrinter(rtbIMText), IMTextManagerType.Group, this.session, sessionName);
64 btnShow.Text = "Show";
65 chatSplit.Panel2Collapsed = true;
68 RegisterClientEvents(client);
69 if (!client.Self.GroupChatSessions.ContainsKey(session))
71 client.Self.RequestJoinGroupChat(session);
74 Load += new EventHandler(GroupIMTabWindow_Load);
77 private void RegisterClientEvents(GridClient client)
79 client.Self.GroupChatJoined += new EventHandler<GroupChatJoinedEventArgs>(Self_GroupChatJoined);
80 client.Self.ChatSessionMemberAdded += new EventHandler<ChatSessionMemberAddedEventArgs>(Self_ChatSessionMemberAdded);
81 client.Self.ChatSessionMemberLeft += new EventHandler<ChatSessionMemberLeftEventArgs>(Self_ChatSessionMemberLeft);
82 client.Avatars.UUIDNameReply += new EventHandler<UUIDNameReplyEventArgs>(Avatars_UUIDNameReply);
83 instance.Netcom.ClientConnected += new EventHandler<EventArgs>(Netcom_Connected);
84 instance.Netcom.ClientDisconnected += new EventHandler<DisconnectedEventArgs>(Netcom_Disconnected);
85 instance.GlobalSettings.OnSettingChanged += new Settings.SettingChangedCallback(GlobalSettings_OnSettingChanged);
86 instance.ClientChanged += new EventHandler<ClientChangedEventArgs>(instance_ClientChanged);
89 private void UnregisterClientEvents(GridClient client)
91 client.Self.GroupChatJoined -= new EventHandler<GroupChatJoinedEventArgs>(Self_GroupChatJoined);
92 client.Self.ChatSessionMemberAdded -= new EventHandler<ChatSessionMemberAddedEventArgs>(Self_ChatSessionMemberAdded);
93 client.Self.ChatSessionMemberLeft -= new EventHandler<ChatSessionMemberLeftEventArgs>(Self_ChatSessionMemberLeft);
94 client.Avatars.UUIDNameReply -= new EventHandler<UUIDNameReplyEventArgs>(Avatars_UUIDNameReply);
95 instance.Netcom.ClientConnected -= new EventHandler<EventArgs>(Netcom_Connected);
96 instance.Netcom.ClientDisconnected -= new EventHandler<DisconnectedEventArgs>(Netcom_Disconnected);
97 instance.GlobalSettings.OnSettingChanged -= new Settings.SettingChangedCallback(GlobalSettings_OnSettingChanged);
98 instance.ClientChanged -= new EventHandler<ClientChangedEventArgs>(instance_ClientChanged);
101 void instance_ClientChanged(object sender, ClientChangedEventArgs e)
103 UnregisterClientEvents(e.OldClient);
104 RegisterClientEvents(client);
107 void GroupIMTabWindow_Load(object sender, EventArgs e)
109 UpdateParticipantList();
112 private void IMTabWindow_Disposed(object sender, EventArgs e)
114 if (instance.Netcom.IsLoggedIn)
116 client.Self.RequestLeaveGroupChat(session);
119 UnregisterClientEvents(client);
123 void UpdateFontSize()
125 float size = (float)instance.GlobalSettings["chat_font_size"].AsReal();
126 cbxInput.Font = ChatConsole.ChangeFontSize(cbxInput.Font, size);
127 rtbIMText.Font = ChatConsole.ChangeFontSize(rtbIMText.Font, size);
128 textManager.ReprintAllText();
131 void GlobalSettings_OnSettingChanged(object sender, SettingsEventArgs e)
133 if (e.Key == "chat_font_size")
137 void Netcom_Disconnected(object sender, DisconnectedEventArgs e)
142 void Netcom_Connected(object sender, EventArgs e)
144 client.Self.RequestJoinGroupChat(session);
148 void Avatars_UUIDNameReply(object sender, UUIDNameReplyEventArgs e)
153 BeginInvoke(new MethodInvoker(() => Avatars_UUIDNameReply(sender, e)));
157 lock (AvatarListSyncRoot)
160 Participants.BeginUpdate();
162 foreach (KeyValuePair<UUID, string> kvp in e.Names)
164 if (Participants.Items.ContainsKey(kvp.Key.ToString()))
165 Participants.Items[kvp.Key.ToString()].Text = kvp.Value;
169 Participants.EndUpdate();
173 void Self_ChatSessionMemberLeft(object sender, ChatSessionMemberLeftEventArgs e)
175 if (e.SessionID == session)
176 UpdateParticipantList();
179 void Self_ChatSessionMemberAdded(object sender, ChatSessionMemberAddedEventArgs e)
181 if (e.SessionID == session)
182 UpdateParticipantList();
185 void UpdateParticipantList()
190 BeginInvoke(new MethodInvoker(UpdateParticipantList));
196 lock (AvatarListSyncRoot)
198 Participants.BeginUpdate();
199 Participants.Items.Clear();
201 List<ChatSessionMember> participants;
202 List<UUID> nameLookup = new List<UUID>();
204 if (client.Self.GroupChatSessions.TryGetValue(session, out participants))
206 ChatSessionMember[] members = participants.ToArray();
207 for (int i = 0; i < members.Length; i++)
209 ChatSessionMember participant = members[i];
210 ListViewItem item = new ListViewItem();
211 item.Name = participant.AvatarKey.ToString();
212 if (instance.nameCache.ContainsKey(participant.AvatarKey))
214 item.Text = instance.nameCache[participant.AvatarKey];
218 item.Text = RadegastInstance.INCOMPLETE_NAME;
219 nameLookup.Add(participant.AvatarKey);
221 if (participant.IsModerator)
222 item.Font = new Font(item.Font, FontStyle.Bold);
223 Participants.Items.Add(item);
226 if (nameLookup.Count > 0)
227 client.Avatars.RequestAvatarNames(nameLookup);
231 Participants.EndUpdate();
236 Participants.EndUpdate();
240 void Self_GroupChatJoined(object sender, GroupChatJoinedEventArgs e)
242 if (e.SessionID != session && e.TmpSessionID != session)
250 Invoke(new MethodInvoker(() => Self_GroupChatJoined(sender, e)));
256 textManager.TextPrinter.PrintTextLine("Join Group Chat Success!", Color.Green);
257 WaitForSessionStart.Set();
261 textManager.TextPrinter.PrintTextLine("Join Group Chat failed.", Color.Red);
265 public void CleanUp()
267 textManager.CleanUp();
272 private void btnSend_Click(object sender, EventArgs e)
278 private void btnShow_Click(object sender, EventArgs e)
280 if (chatSplit.Panel2Collapsed)
282 chatSplit.Panel2Collapsed = false;
283 btnShow.Text = "Hide";
287 chatSplit.Panel2Collapsed = true;
288 btnShow.Text = "Show";
292 private void cbxInput_TextChanged(object sender, EventArgs e)
297 private void RefreshControls()
302 BeginInvoke(new MethodInvoker(RefreshControls));
306 if (!netcom.IsLoggedIn)
308 cbxInput.Enabled = false;
309 btnSend.Enabled = false;
310 btnShow.Enabled = false;
311 btnShow.Text = "Show";
312 chatSplit.Panel2Collapsed = true;
316 cbxInput.Enabled = true;
317 btnShow.Enabled = true;
319 if (cbxInput.Text.Length > 0)
321 btnSend.Enabled = true;
325 btnSend.Enabled = false;
329 void ChatHistoryPrev()
331 if (chatPointer == 0) return;
333 if (chatHistory.Count > chatPointer)
335 cbxInput.Text = chatHistory[chatPointer];
336 cbxInput.SelectionStart = cbxInput.Text.Length;
337 cbxInput.SelectionLength = 0;
341 void ChatHistoryNext()
343 if (chatPointer == chatHistory.Count) return;
345 if (chatPointer == chatHistory.Count)
347 cbxInput.Text = string.Empty;
350 cbxInput.Text = chatHistory[chatPointer];
351 cbxInput.SelectionStart = cbxInput.Text.Length;
352 cbxInput.SelectionLength = 0;
356 private void cbxInput_KeyDown(object sender, KeyEventArgs e)
358 if (e.KeyCode == Keys.Up && e.Modifiers == Keys.Control)
360 e.Handled = e.SuppressKeyPress = true;
365 if (e.KeyCode == Keys.Down && e.Modifiers == Keys.Control)
367 e.Handled = e.SuppressKeyPress = true;
372 if (e.KeyCode != Keys.Enter) return;
373 e.Handled = e.SuppressKeyPress = true;
378 private void SendMsg()
380 if (cbxInput.Text.Length == 0) return;
382 chatHistory.Add(cbxInput.Text);
383 chatPointer = chatHistory.Count;
385 string message = cbxInput.Text.Replace(ChatInputBox.NewlineMarker, "\n");
387 if (instance.GlobalSettings["mu_emotes"].AsBoolean() && message.StartsWith(":"))
389 message = "/me " + message.Substring(1);
394 if (instance.RLV.RestictionActive("sendim")) return;
396 if (message.Length > 1023) message = message.Remove(1023);
398 if (!client.Self.GroupChatSessions.ContainsKey(session))
400 WaitForSessionStart.Reset();
401 client.Self.RequestJoinGroupChat(session);
405 WaitForSessionStart.Set();
408 if (WaitForSessionStart.WaitOne(10000, false))
410 client.Self.InstantMessageGroup(session, message);
414 textManager.TextPrinter.PrintTextLine("Cannot send group IM.", Color.Red);
418 private void ClearIMInput()
420 cbxInput.Text = string.Empty;
423 public void SelectIMInput()
428 private void rtbIMText_LinkClicked(object sender, LinkClickedEventArgs e)
430 instance.MainForm.ProcessLink(e.LinkText);
433 public UUID SessionId
435 get { return session; }
436 set { session = value; }
439 public IMTextManager TextManager
441 get { return textManager; }
442 set { textManager = value; }
445 private void Participants_MouseDoubleClick(object sender, MouseEventArgs e)
447 ListViewItem item = Participants.GetItemAt(e.X, e.Y);
452 instance.MainForm.ShowAgentProfile(item.Text, new UUID(item.Name));
454 catch (Exception) { }
458 private void cbxInput_VisibleChanged(object sender, EventArgs e)
460 if (Visible) cbxInput.Focus();
463 private void cbxInput_SizeChanged(object sender, EventArgs e)
465 pnlChatInput.Height = cbxInput.Height + 7;