OSDN Git Service

Added licensing and revision infro to source files
[radegast/radegast.git] / Radegast / GUI / Consoles / GroupIMTabWindow.cs
1 // \r
2 // Radegast Metaverse Client\r
3 // Copyright (c) 2009, Radegast Development Team\r
4 // All rights reserved.\r
5 // \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
8 // \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
17 // \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
28 //\r
29 // $Id$\r
30 //\r
31 using System;\r
32 using System.Collections.Generic;\r
33 using System.ComponentModel;\r
34 using System.Drawing;\r
35 using System.Data;\r
36 using System.Text;\r
37 using System.Windows.Forms;\r
38 using System.Threading;\r
39 using RadegastNc;\r
40 using OpenMetaverse;\r
41 \r
42 namespace Radegast\r
43 {\r
44     public partial class GroupIMTabWindow : UserControl\r
45     {\r
46         private RadegastInstance instance;\r
47         private GridClient client { get { return instance.Client; } }\r
48         private RadegastNetcom netcom { get { return instance.Netcom; } }\r
49         private UUID session;\r
50         private IMTextManager textManager;\r
51 \r
52         ManualResetEvent WaitForSessionStart = new ManualResetEvent(false);\r
53 \r
54         public GroupIMTabWindow(RadegastInstance instance, UUID session)\r
55         {\r
56             InitializeComponent();\r
57             Disposed += new EventHandler(IMTabWindow_Disposed);\r
58 \r
59             this.instance = instance;\r
60             this.session = session;\r
61 \r
62             textManager = new IMTextManager(this.instance, new RichTextBoxPrinter(rtbIMText), this.session);\r
63             ApplyConfig(this.instance.Config.CurrentConfig);\r
64             \r
65             // Callbacks\r
66             this.instance.Config.ConfigApplied += new EventHandler<ConfigAppliedEventArgs>(Config_ConfigApplied);\r
67             client.Self.OnGroupChatJoin += new AgentManager.GroupChatJoinedCallback(Self_OnGroupChatJoin);\r
68 \r
69             client.Self.RequestJoinGroupChat(session);\r
70         }\r
71 \r
72         private void IMTabWindow_Disposed(object sender, EventArgs e)\r
73         {\r
74             this.instance.Config.ConfigApplied -= new EventHandler<ConfigAppliedEventArgs>(Config_ConfigApplied);\r
75             client.Self.OnGroupChatJoin -= new AgentManager.GroupChatJoinedCallback(Self_OnGroupChatJoin);\r
76             CleanUp();\r
77         }\r
78 \r
79 \r
80         void Self_OnGroupChatJoin(UUID groupChatSessionID, string sessionName, UUID tmpSessionID, bool success)\r
81         {\r
82             if (groupChatSessionID != session && tmpSessionID != session) {\r
83                 return;\r
84             }\r
85 \r
86             if (InvokeRequired) {\r
87                 Invoke(new MethodInvoker(\r
88                     delegate()\r
89                     {\r
90                         Self_OnGroupChatJoin(groupChatSessionID, sessionName, tmpSessionID, success);\r
91                     }\r
92                     )\r
93                 );\r
94                 return;\r
95             }\r
96             if (success) {\r
97                 textManager.TextPrinter.PrintTextLine("Join Group Chat Success!", Color.Green);\r
98                 WaitForSessionStart.Set();\r
99             } else {\r
100                 textManager.TextPrinter.PrintTextLine("Join Group Chat failed.", Color.Red);\r
101             }\r
102         }\r
103 \r
104         private void Config_ConfigApplied(object sender, ConfigAppliedEventArgs e)\r
105         {\r
106             ApplyConfig(e.AppliedConfig);\r
107         }\r
108 \r
109         private void ApplyConfig(Config config)\r
110         {\r
111             /*\r
112             if (config.InterfaceStyle == 0) //System\r
113                 toolStrip1.RenderMode = ToolStripRenderMode.System;\r
114             else if (config.InterfaceStyle == 1) //Office 2003\r
115                 toolStrip1.RenderMode = ToolStripRenderMode.ManagerRenderMode;\r
116              */\r
117         }\r
118 \r
119         private void AddNetcomEvents()\r
120         {\r
121             netcom.ClientLoginStatus += new EventHandler<ClientLoginEventArgs>(netcom_ClientLoginStatus);\r
122             netcom.ClientDisconnected += new EventHandler<ClientDisconnectEventArgs>(netcom_ClientDisconnected);\r
123         }\r
124 \r
125         private void netcom_ClientLoginStatus(object sender, ClientLoginEventArgs e)\r
126         {\r
127             if (e.Status != LoginStatus.Success) return;\r
128 \r
129             RefreshControls();\r
130         }\r
131 \r
132         private void netcom_ClientDisconnected(object sender, ClientDisconnectEventArgs e)\r
133         {\r
134             RefreshControls();\r
135         }\r
136 \r
137         public void CleanUp()\r
138         {\r
139             textManager.CleanUp();\r
140             textManager = null;\r
141         }\r
142 \r
143         private void btnSend_Click(object sender, EventArgs e)\r
144         {\r
145             //netcom.SendInstantMessage(cbgInput.Text, target, session);\r
146             this.ClearIMInput();\r
147         }\r
148 \r
149         private void cbxInput_TextChanged(object sender, EventArgs e)\r
150         {\r
151             RefreshControls();\r
152         }\r
153 \r
154         private void RefreshControls()\r
155         {\r
156             if (!netcom.IsLoggedIn)\r
157             {\r
158                 cbgInput.Enabled = false;\r
159                 btnSend.Enabled = false;\r
160                 return;\r
161             }\r
162 \r
163         }\r
164 \r
165         private void cbxInput_KeyUp(object sender, KeyEventArgs e)\r
166         {\r
167             if (e.KeyCode != Keys.Enter) return;\r
168             e.SuppressKeyPress = true;\r
169             if (cbgInput.Text.Length == 0) return;\r
170 \r
171             string message = cbgInput.Text;\r
172             if (message.Length > 1023) message = message.Remove(1023);\r
173 \r
174             if (!client.Self.GroupChatSessions.ContainsKey(session)) {\r
175                 WaitForSessionStart.Reset();\r
176                 client.Self.RequestJoinGroupChat(session);\r
177             } else {\r
178                 WaitForSessionStart.Set();\r
179             }\r
180 \r
181             if (WaitForSessionStart.WaitOne(10000, false)) {\r
182                 client.Self.InstantMessageGroup(session, message);\r
183             } else {\r
184                 textManager.TextPrinter.PrintTextLine("Cannot send group IM.", Color.Red);\r
185             }\r
186             this.ClearIMInput();\r
187         }\r
188 \r
189         private void ClearIMInput()\r
190         {\r
191             cbgInput.Items.Add(cbgInput.Text);\r
192             cbgInput.Text = string.Empty;\r
193         }\r
194 \r
195         public void SelectIMInput()\r
196         {\r
197             cbgInput.Select();\r
198         }\r
199 \r
200         private void rtbIMText_LinkClicked(object sender, LinkClickedEventArgs e)\r
201         {\r
202             instance.MainForm.processLink(e.LinkText);\r
203         }\r
204 \r
205         private void cbxInput_KeyDown(object sender, KeyEventArgs e)\r
206         {\r
207             if (e.KeyCode == Keys.Enter) e.SuppressKeyPress = true;\r
208         }\r
209 \r
210         public UUID SessionId\r
211         {\r
212             get { return session; }\r
213             set { session = value; }\r
214         }\r
215 \r
216         public IMTextManager TextManager\r
217         {\r
218             get { return textManager; }\r
219             set { textManager = value; }\r
220         }\r
221     }\r
222 }\r