2 // Radegast Metaverse Client
3 // Copyright (c) 2009-2011, 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 instance.Names.NameUpdated += new EventHandler<UUIDNameReplyEventArgs>(Names_NameUpdated);
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 instance.Names.NameUpdated -= new EventHandler<UUIDNameReplyEventArgs>(Names_NameUpdated);
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")
135 else if (e.Key == "display_name_mode")
136 UpdateParticipantList();
139 void Netcom_Disconnected(object sender, DisconnectedEventArgs e)
144 void Netcom_Connected(object sender, EventArgs e)
146 client.Self.RequestJoinGroupChat(session);
150 void Names_NameUpdated(object sender, UUIDNameReplyEventArgs e)
154 if (!instance.MonoRuntime || IsHandleCreated)
155 BeginInvoke(new MethodInvoker(() => Names_NameUpdated(sender, e)));
159 lock (AvatarListSyncRoot)
162 Participants.BeginUpdate();
164 foreach (KeyValuePair<UUID, string> kvp in e.Names)
166 if (Participants.Items.ContainsKey(kvp.Key.ToString()))
167 Participants.Items[kvp.Key.ToString()].Text = kvp.Value;
171 Participants.EndUpdate();
175 void Self_ChatSessionMemberLeft(object sender, ChatSessionMemberLeftEventArgs e)
177 if (e.SessionID == session)
178 UpdateParticipantList();
181 void Self_ChatSessionMemberAdded(object sender, ChatSessionMemberAddedEventArgs e)
183 if (e.SessionID == session)
184 UpdateParticipantList();
187 void UpdateParticipantList()
191 if (!instance.MonoRuntime || IsHandleCreated)
192 BeginInvoke(new MethodInvoker(UpdateParticipantList));
198 lock (AvatarListSyncRoot)
200 Participants.BeginUpdate();
201 Participants.Items.Clear();
203 List<ChatSessionMember> participants;
205 if (client.Self.GroupChatSessions.TryGetValue(session, out participants))
207 ChatSessionMember[] members = participants.ToArray();
208 for (int i = 0; i < members.Length; i++)
210 ChatSessionMember participant = members[i];
211 ListViewItem item = new ListViewItem();
212 item.Name = participant.AvatarKey.ToString();
213 item.Text = instance.Names.Get(participant.AvatarKey);
214 if (participant.IsModerator)
215 item.Font = new Font(item.Font, FontStyle.Bold);
216 Participants.Items.Add(item);
221 Participants.EndUpdate();
226 Participants.EndUpdate();
230 void Self_GroupChatJoined(object sender, GroupChatJoinedEventArgs e)
232 if (e.SessionID != session && e.TmpSessionID != session)
239 if (!instance.MonoRuntime || IsHandleCreated)
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()
291 if (!instance.MonoRuntime || IsHandleCreated)
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 void ChatHistoryPrev()
321 if (chatPointer == 0) return;
323 if (chatHistory.Count > chatPointer)
325 cbxInput.Text = chatHistory[chatPointer];
326 cbxInput.SelectionStart = cbxInput.Text.Length;
327 cbxInput.SelectionLength = 0;
331 void ChatHistoryNext()
333 if (chatPointer == chatHistory.Count) return;
335 if (chatPointer == chatHistory.Count)
337 cbxInput.Text = string.Empty;
340 cbxInput.Text = chatHistory[chatPointer];
341 cbxInput.SelectionStart = cbxInput.Text.Length;
342 cbxInput.SelectionLength = 0;
346 private void cbxInput_KeyDown(object sender, KeyEventArgs e)
348 if (e.KeyCode == Keys.Up && e.Modifiers == Keys.Control)
350 e.Handled = e.SuppressKeyPress = true;
355 if (e.KeyCode == Keys.Down && e.Modifiers == Keys.Control)
357 e.Handled = e.SuppressKeyPress = true;
362 if (e.KeyCode != Keys.Enter) return;
363 e.Handled = e.SuppressKeyPress = true;
368 private void SendMsg()
370 if (cbxInput.Text.Length == 0) return;
372 chatHistory.Add(cbxInput.Text);
373 chatPointer = chatHistory.Count;
375 string message = cbxInput.Text.Replace(ChatInputBox.NewlineMarker, "\n");
377 if (instance.GlobalSettings["mu_emotes"].AsBoolean() && message.StartsWith(":"))
379 message = "/me " + message.Substring(1);
384 if (instance.RLV.RestictionActive("sendim")) return;
386 if (message.Length > 1023) message = message.Remove(1023);
388 if (!client.Self.GroupChatSessions.ContainsKey(session))
390 WaitForSessionStart.Reset();
391 client.Self.RequestJoinGroupChat(session);
395 WaitForSessionStart.Set();
398 if (WaitForSessionStart.WaitOne(10000, false))
400 client.Self.InstantMessageGroup(session, message);
404 textManager.TextPrinter.PrintTextLine("Cannot send group IM.", Color.Red);
408 private void ClearIMInput()
410 cbxInput.Text = string.Empty;
413 public void SelectIMInput()
418 private void rtbIMText_LinkClicked(object sender, LinkClickedEventArgs e)
420 instance.MainForm.ProcessLink(e.LinkText);
423 public UUID SessionId
425 get { return session; }
426 set { session = value; }
429 public IMTextManager TextManager
431 get { return textManager; }
432 set { textManager = value; }
435 private void Participants_MouseDoubleClick(object sender, MouseEventArgs e)
437 ListViewItem item = Participants.GetItemAt(e.X, e.Y);
442 instance.MainForm.ShowAgentProfile(item.Text, new UUID(item.Name));
444 catch (Exception) { }
448 private void cbxInput_VisibleChanged(object sender, EventArgs e)
450 if (Visible) cbxInput.Focus();
453 private void cbxInput_SizeChanged(object sender, EventArgs e)
455 pnlChatInput.Height = cbxInput.Height + 7;