corrade-vassal – Blame information for rev 1

Subversion Repositories:
Rev:
Rev Author Line No. Line
1 vero 1 /*
2 * Copyright (c) 2006-2014, openmetaverse.org
3 * All rights reserved.
4 *
5 * - Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * - Redistributions of source code must retain the above copyright notice, this
9 * list of conditions and the following disclaimer.
10 * - Neither the name of the openmetaverse.org nor the names
11 * of its contributors may be used to endorse or promote products derived from
12 * this software without specific prior written permission.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
15 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
18 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
19 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
20 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
22 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
23 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
24 * POSSIBILITY OF SUCH DAMAGE.
25 */
26  
27 using System;
28 using System.Collections.Generic;
29 using System.Drawing;
30 using System.Threading;
31 using System.Windows.Forms;
32  
33 namespace OpenMetaverse.GUI
34 {
35 public class LoginPanel : Panel
36 {
37 private GridClient _Client;
38 private LoginParams _LoginParams = new LoginParams();
39 private Thread LoginThread;
40 private Dictionary<string, string> _Accounts = new Dictionary<string, string>();
41 private Button btnLogin = new Button();
42 private Label label1 = new Label();
43 private Label label2 = new Label();
44 private Label label3 = new Label();
45 private ComboBox listNames = new ComboBox();
46 private ComboBox listStart = new ComboBox();
47 private TextBox txtPass = new TextBox();
48  
49 /// <summary>
50 /// Gets or sets the GridClient associated with this control
51 /// </summary>
52 public GridClient Client
53 {
54 get { return _Client; }
55 set { if (value != null) InitializeClient(value); }
56 }
57  
58 /// <summary>
59 /// Gets or sets the LoginParams associated with this control's GridClient object
60 /// </summary>
61 public LoginParams LoginParams
62 {
63 get { return _LoginParams; }
64 set { _LoginParams = value; }
65 }
66  
67 /// <summary>
68 /// First name parsed from the textbox control
69 /// </summary>
70 public string FirstName
71 {
72 get
73 {
74 string[] names = listNames.Text.Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries);
75 return names.Length > 0 ? names[0] : String.Empty;
76 }
77 }
78  
79 /// <summary>
80 /// Last name parsed from the textbox control
81 /// </summary>
82 public string LastName
83 {
84 get
85 {
86 string[] names = listNames.Text.Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries);
87 return names.Length > 1 ? names[1] : String.Empty;
88 }
89 }
90  
91 /// <summary>
92 /// Password value returned from the textbox control
93 /// </summary>
94 public string Password
95 {
96 get { return txtPass.Text; }
97 }
98  
99 /// <summary>
100 /// Complete start URI based on textbox value
101 /// </summary>
102 public string StartURI
103 {
104 get
105 {
106 if (listStart.Text == String.Empty)
107 return "last";
108  
109 if (listStart.Text == "Last" || listStart.Text == "Home")
110 return listStart.Text.ToLower();
111  
112 else
113 {
114 string[] start = listStart.Text.Split(new char[] { '/' });
115 int x; int y; int z;
116  
117 if (start.Length < 2 || int.TryParse(start[1], out x)) x = 128;
118 if (start.Length < 3 || int.TryParse(start[2], out y)) y = 128;
119 if (start.Length < 4 || int.TryParse(start[3], out z)) z = 0;
120  
121 return NetworkManager.StartLocation(start[0], x, y, z);
122 }
123 }
124 }
125  
126  
127 /// <summary>
128 /// Panel control for an unspecified client's login preferences
129 /// </summary>
130 public LoginPanel()
131 {
132 btnLogin.Text = "Login";
133 btnLogin.Size = new Size(50, 23);
134 btnLogin.Location = new Point(6, 7);
135 btnLogin.TabIndex = 0;
136  
137 label1.Text = "Name:";
138 label1.AutoSize = true;
139 label1.Location = new Point(60, 12);
140  
141 label2.Text = "Pass:";
142 label2.AutoSize = true;
143 label2.Location = new Point(266, 12);
144  
145 label3.Text = "Start:";
146 label3.AutoSize = true;
147 label3.Location = new Point(428, 12);
148  
149 listNames.Location = new Point(100, 8);
150 listNames.Size = new Size(160, 18);
151 listNames.TabIndex = 1;
152 listNames.SelectedIndexChanged += new EventHandler(listNames_SelectedIndexChanged);
153  
154 txtPass.Location = new Point(302, 8);
155 txtPass.PasswordChar = '*';
156 txtPass.Size = new Size(120, 18);
157 txtPass.TabIndex = 2;
158  
159 listStart.Items.Add("Last");
160 listStart.Items.Add("Home");
161 listStart.SelectedIndex = 0;
162 listStart.Location = new Point(463, 8);
163 listStart.Size = new Size(120, 18);
164 listStart.TabIndex = 3;
165  
166 this.Dock = DockStyle.Top;
167 this.Height = 50;
168  
169 this.Controls.Add(btnLogin);
170 this.Controls.Add(label1);
171 this.Controls.Add(label2);
172 this.Controls.Add(label3);
173 this.Controls.Add(listNames);
174 this.Controls.Add(listStart);
175 this.Controls.Add(txtPass);
176  
177 btnLogin.Click += new EventHandler(btnLogin_Click);
178 }
179  
180 /// <summary>
181 /// Begins login sequence using the parameters defined in .LoginParams
182 /// </summary>
183 public void Login()
184 {
185 if (_Client == null) return;
186  
187 if (_LoginParams.MethodName == null)
188 _LoginParams = Client.Network.DefaultLoginParams("", "", "", "", "");
189  
190 SetText(btnLogin, "Logout");
191 SetText(listNames, _LoginParams.FirstName + " " + _LoginParams.LastName);
192 SetText(txtPass, _LoginParams.Password);
193  
194 if (LoginThread != null)
195 {
196 LoginThread.Abort();
197 LoginThread = null;
198 }
199 LoginThread = new Thread(new ThreadStart(delegate() { _Client.Network.Login(_LoginParams); }));
200 LoginThread.Start();
201 }
202  
203  
204 private void SetText(object control, string text)
205 {
206 if (this.InvokeRequired)
207 this.BeginInvoke(new MethodInvoker(delegate() { SetText(control, text); }));
208 else
209 {
210 if (control is Button) ((Button)control).Text = text;
211 else if (control is ComboBox) ((ComboBox)control).Text = text;
212 else if (control is TextBox) ((TextBox)control).Text = text;
213 }
214 }
215  
216 private void InitializeClient(GridClient client)
217 {
218 _Client = client;
219  
220 _Client.Network.Disconnected += Network_OnDisconnected;
221 _Client.Network.LoginProgress += Network_OnLogin;
222 }
223  
224 private void btnLogin_Click(object sender, EventArgs e)
225 {
226 if (btnLogin.Text == "Login")
227 {
228 if (FirstName == String.Empty || LastName == String.Empty)
229 {
230 MessageBox.Show("Please enter a valid first and last name.", "Login", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
231 return;
232 }
233  
234 _LoginParams.FirstName = FirstName;
235 _LoginParams.LastName = LastName;
236 _LoginParams.Password = Password;
237 _LoginParams.Start = StartURI;
238  
239 Login();
240 }
241 else if (btnLogin.Text == "Logout")
242 {
243 if (LoginThread != null)
244 {
245 if (LoginThread.IsAlive)
246 LoginThread.Abort();
247  
248 LoginThread = null;
249 }
250  
251 if (_Client != null)
252 _Client.Network.Logout();
253  
254 btnLogin.Text = "Login";
255 }
256 }
257  
258 private void listNames_SelectedIndexChanged(object sender, EventArgs e)
259 {
260 if (listNames.SelectedIndex > -1)
261 {
262 lock (_Accounts)
263 {
264 string pass;
265 if (_Accounts.TryGetValue(listNames.Text, out pass))
266 txtPass.Text = pass;
267 }
268 }
269 else txtPass.Text = String.Empty;
270 }
271  
272 private void Network_OnDisconnected(object sender, DisconnectedEventArgs e)
273 {
274 if (!this.IsHandleCreated) return;
275  
276 SetText(btnLogin, "Login");
277 }
278  
279 private void Network_OnLogin(object sender, LoginProgressEventArgs e)
280 {
281 if (!this.IsHandleCreated) return;
282  
283 SetText(btnLogin, "Logout");
284  
285 if (e.Status == LoginStatus.Success)
286 {
287 lock (_Accounts)
288 {
289 _Accounts[_Client.Self.Name] = _LoginParams.Password;
290  
291 if (!listNames.Items.Contains(_Client.Self.Name))
292 {
293 this.BeginInvoke((MethodInvoker)delegate
294 {
295 int index = listNames.Items.Add(_Client.Self.Name);
296 listNames.SelectedIndex = index;
297 });
298 }
299 }
300 }
301 else if (e.Status == LoginStatus.Failed)
302 {
303 SetText(btnLogin, "Login");
304 }
305 else
306 {
307 SetText(listNames, _LoginParams.FirstName + " " + _LoginParams.LastName);
308 SetText(txtPass, _LoginParams.Password);
309 }
310 }
311  
312 }
313 }