OSDN Git Service

Update copyright year
[radegast/radegast.git] / Radegast / GUI / Consoles / ConferenceIMTabWindow.cs
1 // 
2 // Radegast Metaverse Client
3 // Copyright (c) 2009-2014, Radegast Development Team
4 // All rights reserved.
5 // 
6 // Redistribution and use in source and binary forms, with or without
7 // modification, are permitted provided that the following conditions are met:
8 // 
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.
17 // 
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.
28 //
29 // $Id$
30 //
31 using System;
32 using System.Collections.Generic;
33 using System.Windows.Forms;
34 using System.Threading;
35 using Radegast.Netcom;
36 using OpenMetaverse;
37
38 namespace Radegast
39 {
40     public partial class ConferenceIMTabWindow : UserControl
41     {
42         private RadegastInstance instance;
43         private RadegastNetcom netcom;
44         private UUID session;
45         private IMTextManager textManager;
46         private GridClient client;
47         private List<UUID> participants = new List<UUID>();
48         ManualResetEvent WaitForSessionStart = new ManualResetEvent(false);
49         private List<string> chatHistory = new List<string>();
50         private int chatPointer;
51
52         public string SessionName { get; set; }
53
54         public ConferenceIMTabWindow(RadegastInstance instance, UUID session, string sessionName)
55         {
56             InitializeComponent();
57             Disposed += new EventHandler(IMTabWindow_Disposed);
58
59             this.instance = instance;
60             this.client = instance.Client;
61             this.SessionName = sessionName;
62             netcom = this.instance.Netcom;
63
64             this.session = session;
65
66             textManager = new IMTextManager(this.instance, new RichTextBoxPrinter(rtbIMText), IMTextManagerType.Conference, this.session, sessionName);
67             
68             // Callbacks
69             netcom.ClientLoginStatus += new EventHandler<LoginProgressEventArgs>(netcom_ClientLoginStatus);
70             netcom.ClientDisconnected += new EventHandler<DisconnectedEventArgs>(netcom_ClientDisconnected);
71             instance.GlobalSettings.OnSettingChanged += new Settings.SettingChangedCallback(GlobalSettings_OnSettingChanged);
72
73             if (!client.Self.GroupChatSessions.ContainsKey(session))
74             {
75                 client.Self.ChatterBoxAcceptInvite(session);
76             }
77
78             UpdateFontSize();
79         }
80
81         private void IMTabWindow_Disposed(object sender, EventArgs e)
82         {
83             netcom.ClientLoginStatus -= new EventHandler<LoginProgressEventArgs>(netcom_ClientLoginStatus);
84             netcom.ClientDisconnected -= new EventHandler<DisconnectedEventArgs>(netcom_ClientDisconnected);
85             instance.GlobalSettings.OnSettingChanged -= new Settings.SettingChangedCallback(GlobalSettings_OnSettingChanged);
86             CleanUp();
87         }
88
89         private void netcom_ClientLoginStatus(object sender, LoginProgressEventArgs e)
90         {
91             if (e.Status != LoginStatus.Success) return;
92
93             RefreshControls();
94         }
95
96         void UpdateFontSize()
97         {
98             float size = (float)instance.GlobalSettings["chat_font_size"].AsReal();
99             cbxInput.Font = ChatConsole.ChangeFontSize(cbxInput.Font, size);
100             rtbIMText.Font = ChatConsole.ChangeFontSize(rtbIMText.Font, size);
101             textManager.ReprintAllText();
102         }
103
104         void GlobalSettings_OnSettingChanged(object sender, SettingsEventArgs e)
105         {
106             if (e.Key == "chat_font_size")
107                 UpdateFontSize();
108         }
109
110         private void netcom_ClientDisconnected(object sender, DisconnectedEventArgs e)
111         {
112             RefreshControls();
113         }
114
115         public void CleanUp()
116         {
117             textManager.CleanUp();
118             textManager = null;
119         }
120
121         private void btnSend_Click(object sender, EventArgs e)
122         {
123             if (cbxInput.Text.Length == 0) return;
124
125             SendMessage(cbxInput.Text);
126             this.ClearIMInput();
127         }
128
129         private void cbxInput_TextChanged(object sender, EventArgs e)
130         {
131             RefreshControls();
132         }
133
134         private void RefreshControls()
135         {
136             if (!netcom.IsLoggedIn)
137             {
138                 cbxInput.Enabled = false;
139                 btnSend.Enabled = false;
140                 return;
141             }
142
143             cbxInput.Enabled = true;
144
145             if (cbxInput.Text.Length > 0)
146             {
147                 btnSend.Enabled = true;
148             }
149             else
150             {
151                 btnSend.Enabled = false;
152             }
153         }
154
155         void ChatHistoryPrev()
156         {
157             if (chatPointer == 0) return;
158             chatPointer--;
159             if (chatHistory.Count > chatPointer)
160             {
161                 cbxInput.Text = chatHistory[chatPointer];
162                 cbxInput.SelectionStart = cbxInput.Text.Length;
163                 cbxInput.SelectionLength = 0;
164             }
165         }
166
167         void ChatHistoryNext()
168         {
169             if (chatPointer == chatHistory.Count) return;
170             chatPointer++;
171             if (chatPointer == chatHistory.Count)
172             {
173                 cbxInput.Text = string.Empty;
174                 return;
175             }
176             cbxInput.Text = chatHistory[chatPointer];
177             cbxInput.SelectionStart = cbxInput.Text.Length;
178             cbxInput.SelectionLength = 0;
179         }
180
181         private void cbxInput_KeyDown(object sender, KeyEventArgs e)
182         {
183             if (e.KeyCode == Keys.Up && e.Modifiers == Keys.Control)
184             {
185                 e.Handled = e.SuppressKeyPress = true;
186                 ChatHistoryPrev();
187                 return;
188             }
189
190             if (e.KeyCode == Keys.Down && e.Modifiers == Keys.Control)
191             {
192                 e.Handled = e.SuppressKeyPress = true;
193                 ChatHistoryNext();
194                 return;
195             }
196
197             if (e.KeyCode != Keys.Enter) return;
198             e.Handled = e.SuppressKeyPress = true;
199             if (cbxInput.Text.Length == 0) return;
200
201             string message = cbxInput.Text;
202
203             SendMessage(message);
204
205             this.ClearIMInput();
206         }
207
208         private void SendMessage(string msg)
209         {
210             if (msg == string.Empty) return;
211
212             chatHistory.Add(msg);
213             chatPointer = chatHistory.Count;
214
215             if (instance.RLV.RestictionActive("sendim")) return;
216
217             string message = msg.Replace(ChatInputBox.NewlineMarker, "\n");
218
219             if (instance.GlobalSettings["mu_emotes"].AsBoolean() && message.StartsWith(":"))
220             {
221                 message = "/me " + message.Substring(1);
222             }
223
224             if (message.Length > 1023)
225             {
226                 message = message.Remove(1023);
227             }
228             if (message.Length > 0)
229             {
230                 client.Self.InstantMessageGroup(session, message);
231             }
232         }
233
234         private void ClearIMInput()
235         {
236             cbxInput.Text = string.Empty;
237         }
238
239         public void SelectIMInput()
240         {
241             cbxInput.Select();
242         }
243
244         private void rtbIMText_LinkClicked(object sender, LinkClickedEventArgs e)
245         {
246             instance.MainForm.ProcessLink(e.LinkText);
247         }
248
249         public UUID SessionId
250         {
251             get { return session; }
252             set { session = value; }
253         }
254
255         public IMTextManager TextManager
256         {
257             get { return textManager; }
258             set { textManager = value; }
259         }
260
261         private void cbxInput_VisibleChanged(object sender, EventArgs e)
262         {
263             if (Visible) cbxInput.Focus();
264         }
265
266         private void cbxInput_SizeChanged(object sender, EventArgs e)
267         {
268             pnlChatInput.Height = cbxInput.Height + 7;
269         }
270     }
271 }