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
49 ManualResetEvent WaitForSessionStart = new ManualResetEvent(false);
\r
51 public GroupIMTabWindow(RadegastInstance instance, UUID session, string sessionName)
\r
53 InitializeComponent();
\r
54 Disposed += new EventHandler(IMTabWindow_Disposed);
\r
56 this.instance = instance;
\r
57 this.session = session;
\r
59 textManager = new IMTextManager(this.instance, new RichTextBoxPrinter(rtbIMText), this.session, sessionName);
\r
61 btnShow.Text = "Show";
\r
62 chatSplit.Panel2Collapsed = true;
\r
65 client.Self.GroupChatJoined += new EventHandler<GroupChatJoinedEventArgs>(Self_GroupChatJoined);
\r
66 client.Self.ChatSessionMemberAdded += new EventHandler<ChatSessionMemberAddedEventArgs>(Self_ChatSessionMemberAdded);
\r
67 client.Self.ChatSessionMemberLeft += new EventHandler<ChatSessionMemberLeftEventArgs>(Self_ChatSessionMemberLeft);
\r
68 client.Avatars.OnAvatarNames += new AvatarManager.AvatarNamesCallback(Avatars_OnAvatarNames);
\r
69 client.Self.RequestJoinGroupChat(session);
\r
72 private void IMTabWindow_Disposed(object sender, EventArgs e)
\r
74 client.Self.RequestLeaveGroupChat(session);
\r
75 client.Self.GroupChatJoined -= new EventHandler<GroupChatJoinedEventArgs>(Self_GroupChatJoined);
\r
76 client.Self.ChatSessionMemberAdded -= new EventHandler<ChatSessionMemberAddedEventArgs>(Self_ChatSessionMemberAdded);
\r
77 client.Self.ChatSessionMemberLeft -= new EventHandler<ChatSessionMemberLeftEventArgs>(Self_ChatSessionMemberLeft);
\r
78 client.Avatars.OnAvatarNames -= new AvatarManager.AvatarNamesCallback(Avatars_OnAvatarNames);
\r
82 void Avatars_OnAvatarNames(Dictionary<UUID, string> names)
\r
86 BeginInvoke(new MethodInvoker(() => Avatars_OnAvatarNames(names)));
\r
90 Participants.BeginUpdate();
\r
92 foreach (KeyValuePair<UUID, string> kvp in names)
\r
94 if (Participants.Items.ContainsKey(kvp.Key.ToString()))
\r
95 Participants.Items[kvp.Key.ToString()].Text = kvp.Value;
\r
98 Participants.Sort();
\r
99 Participants.EndUpdate();
\r
102 void Self_ChatSessionMemberLeft(object sender, ChatSessionMemberLeftEventArgs e)
\r
104 if (e.SessionID == session)
\r
105 UpdateParticipantList();
\r
108 void Self_ChatSessionMemberAdded(object sender, ChatSessionMemberAddedEventArgs e)
\r
110 if (e.SessionID == session)
\r
111 UpdateParticipantList();
\r
114 void UpdateParticipantList()
\r
116 if (InvokeRequired)
\r
118 BeginInvoke(new MethodInvoker(UpdateParticipantList));
\r
122 Participants.BeginUpdate();
\r
123 Participants.Items.Clear();
\r
125 List<ChatSessionMember> participants;
\r
126 List<UUID> nameLookup = new List<UUID>();
\r
128 if (client.Self.GroupChatSessions.TryGetValue(session, out participants))
\r
130 ChatSessionMember[] members = participants.ToArray();
\r
131 for (int i = 0; i < members.Length; i++)
\r
133 ChatSessionMember participant = members[i];
\r
134 ListViewItem item = new ListViewItem();
\r
135 item.Name = participant.AvatarKey.ToString();
\r
136 if (instance.nameCache.ContainsKey(participant.AvatarKey))
\r
138 item.Text = instance.nameCache[participant.AvatarKey];
\r
142 item.Text = RadegastInstance.INCOMPLETE_NAME;
\r
143 nameLookup.Add(participant.AvatarKey);
\r
145 if (participant.IsModerator)
\r
146 item.Font = new Font(item.Font, FontStyle.Bold);
\r
147 Participants.Items.Add(item);
\r
150 if (nameLookup.Count > 0)
\r
151 client.Avatars.RequestAvatarNames(nameLookup);
\r
154 Participants.Sort();
\r
155 Participants.EndUpdate();
\r
158 void Self_GroupChatJoined(object sender, GroupChatJoinedEventArgs e)
\r
160 if (e.SessionID != session && e.TmpSessionID != session)
\r
165 if (InvokeRequired)
\r
167 Invoke(new MethodInvoker(() => Self_GroupChatJoined(sender, e)));
\r
173 textManager.TextPrinter.PrintTextLine("Join Group Chat Success!", Color.Green);
\r
174 WaitForSessionStart.Set();
\r
178 textManager.TextPrinter.PrintTextLine("Join Group Chat failed.", Color.Red);
\r
182 private void AddNetcomEvents()
\r
184 netcom.ClientLoginStatus += new EventHandler<ClientLoginEventArgs>(netcom_ClientLoginStatus);
\r
185 netcom.ClientDisconnected += new EventHandler<ClientDisconnectEventArgs>(netcom_ClientDisconnected);
\r
188 private void netcom_ClientLoginStatus(object sender, ClientLoginEventArgs e)
\r
190 if (e.Status != LoginStatus.Success) return;
\r
195 private void netcom_ClientDisconnected(object sender, ClientDisconnectEventArgs e)
\r
200 public void CleanUp()
\r
202 textManager.CleanUp();
\r
203 textManager = null;
\r
206 private void btnSend_Click(object sender, EventArgs e)
\r
209 this.ClearIMInput();
\r
212 private void btnShow_Click(object sender, EventArgs e)
\r
214 if (chatSplit.Panel2Collapsed)
\r
216 chatSplit.Panel2Collapsed = false;
\r
217 btnShow.Text = "Hide";
\r
221 chatSplit.Panel2Collapsed = true;
\r
222 btnShow.Text = "Show";
\r
226 private void cbxInput_TextChanged(object sender, EventArgs e)
\r
231 private void RefreshControls()
\r
233 if (!netcom.IsLoggedIn)
\r
235 cbxInput.Enabled = false;
\r
236 btnSend.Enabled = false;
\r
237 btnShow.Enabled = false;
\r
238 btnShow.Text = "Show";
\r
239 chatSplit.Panel2Collapsed = true;
\r
243 btnShow.Enabled = true;
\r
245 if (cbxInput.Text.Length > 0)
\r
247 btnSend.Enabled = true;
\r
251 btnSend.Enabled = false;
\r
255 private void cbxInput_KeyUp(object sender, KeyEventArgs e)
\r
257 if (e.KeyCode != Keys.Enter) return;
\r
258 e.SuppressKeyPress = true;
\r
263 private void SendMsg()
\r
265 if (cbxInput.Text.Length == 0) return;
\r
267 string message = cbxInput.Text;
\r
268 if (message.Length > 1023) message = message.Remove(1023);
\r
270 if (!client.Self.GroupChatSessions.ContainsKey(session))
\r
272 WaitForSessionStart.Reset();
\r
273 client.Self.RequestJoinGroupChat(session);
\r
277 WaitForSessionStart.Set();
\r
280 if (WaitForSessionStart.WaitOne(10000, false))
\r
282 client.Self.InstantMessageGroup(session, message);
\r
286 textManager.TextPrinter.PrintTextLine("Cannot send group IM.", Color.Red);
\r
288 this.ClearIMInput();
\r
291 private void ClearIMInput()
\r
293 cbxInput.Items.Add(cbxInput.Text);
\r
294 cbxInput.Text = string.Empty;
\r
297 public void SelectIMInput()
\r
302 private void rtbIMText_LinkClicked(object sender, LinkClickedEventArgs e)
\r
304 instance.MainForm.ProcessLink(e.LinkText);
\r
307 private void cbxInput_KeyDown(object sender, KeyEventArgs e)
\r
309 if (e.KeyCode == Keys.Enter) e.SuppressKeyPress = true;
\r
312 public UUID SessionId
\r
314 get { return session; }
\r
315 set { session = value; }
\r
318 public IMTextManager TextManager
\r
320 get { return textManager; }
\r
321 set { textManager = value; }
\r
324 private void Participants_MouseDoubleClick(object sender, MouseEventArgs e)
\r
326 ListViewItem item = Participants.GetItemAt(e.X, e.Y);
\r
331 instance.MainForm.ShowAgentProfile(item.Text, new UUID(item.Name));
\r
333 catch (Exception) { }
\r