This repository has been archived by the owner on Jan 26, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
LoginForm.cs
169 lines (149 loc) · 5.84 KB
/
LoginForm.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
using IdleLandsGUI.Model;
using IdleLandsGUI.Properties;
using IdleLandsGUI.SystemTray;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace IdleLandsGUI
{
public partial class LoginForm : Form
{
private IdleLandsComms _comms { get; set; }
private bool _closedByUser { get; set; }
private IdlelandContextMenu _menu { get; set; }
public LoginForm(IdleLandsComms comms, IdlelandContextMenu menu)
{
InitializeComponent();
this.Icon = Icon.FromHandle(Resources.IdleLandsIcon.GetHicon());
AppSettings apps = new AppSettings();
if (!string.IsNullOrEmpty(apps.Server))
{
int index = ServerComboBox.FindString(apps.Server);
if (index >= 0)
ServerComboBox.SelectedIndex = index;
else
{
ServerComboBox.Items.Add(apps.Server);
ServerComboBox.SelectedIndex = ServerComboBox.Items.Count - 1;
}
}
else
ServerComboBox.SelectedIndex = 1;
_comms = comms;
_menu = menu;
_menu.SetForm(this);
if (ServerComboBox.Text.IndexOf('(') >= 0)
_comms.SetServer(ServerComboBox.Text.Substring(0, ServerComboBox.Text.IndexOf('(')).Trim());
else
_comms.SetServer(ServerComboBox.Text.Trim());
_closedByUser = true;
this.FormClosing += LoginForm_FormClosing;
this.ServerComboBox.TextChanged += ServerComboBox_OnChange;
LogRequestsCheckbox.Checked = apps.LogRequests;
LogResponsesCheckbox.Checked = apps.LogResponses;
}
private void DisableControls()
{
RegisterButton.Enabled = false;
LoginButton.Enabled = false;
AdvancedLoginButton.Enabled = false;
LoginFailedLabel.Visible = false;
ServerComboBox.Enabled = false;
UsernameTextbox.Enabled = false;
PasswordTextbox.Enabled = false;
}
private void RegisterButton_Click(object sender, EventArgs e)
{
DisableControls();
_comms.Register(UsernameTextbox.Text, PasswordTextbox.Text, LoginSuccessful, LoginUnsuccessful);
}
private void LoginButton_Click(object sender, EventArgs e)
{
DisableControls();
_comms.Login(UsernameTextbox.Text, PasswordTextbox.Text, LoginSuccessful, LoginUnsuccessful);
}
private void AdvancedLoginButton_Click(object sender, EventArgs e)
{
DisableControls();
_comms.AdvancedLogin(UsernameTextbox.Text, PasswordTextbox.Text, LoginSuccessful, LoginUnsuccessful);
}
private void ServerComboBox_OnChange(object sender, EventArgs e)
{
if(ServerComboBox.Text.Contains('('))
_comms.SetServer(ServerComboBox.Text.Substring(0, ServerComboBox.Text.IndexOf('(')).Trim());
else
_comms.SetServer(ServerComboBox.Text.Trim());
}
private void PasswordTextbox_KeyDown(object send, KeyEventArgs e)
{
if(e.KeyCode == Keys.Enter)
{
LoginButton.PerformClick();
}
}
private void LoginForm_FormClosing(object sender, FormClosingEventArgs e)
{
if (e.CloseReason == CloseReason.UserClosing && _closedByUser)
{
if (MessageBox.Show(this, "Really?", "Closing...",
MessageBoxButtons.OKCancel, MessageBoxIcon.Question)
== DialogResult.Cancel) e.Cancel = true;
else
Application.Exit();
}
}
public delegate void LoginResultDelegate(IdleLandsComms.ActionResponse info);
public void LoginSuccessful(IdleLandsComms.ActionResponse info)
{
this.Invoke((MethodInvoker)delegate
{
string server = ServerComboBox.Text.Trim();
if(server.Contains('('))
server = server.Substring(0, server.IndexOf('(')).Trim();
AppSettings apps = new AppSettings();
apps.Server = server;
apps.Save();
MainForm newForm = new MainForm(info, _comms);
_closedByUser = false;
_menu.SetForm(newForm);
this.Close();
newForm.Show();
});
}
public delegate void LoginFailedDelegate(string message);
public void LoginUnsuccessful(string message)
{
this.Invoke((MethodInvoker)delegate
{
RegisterButton.Enabled = true;
LoginButton.Enabled = true;
AdvancedLoginButton.Enabled = true;
ServerComboBox.Enabled = true;
UsernameTextbox.Enabled = true;
PasswordTextbox.Enabled = true;
LoginFailedLabel.Text = "Login failed: " + message;
LoginFailedLabel.Visible = true;
});
}
private void LogRequestsCheckbox_CheckedChanged(object sender, EventArgs e)
{
AppSettings apps = new AppSettings();
apps.LogRequests = LogRequestsCheckbox.Checked;
apps.Save();
_comms.SetAppSettings(apps);
}
private void LogResponsesCheckbox_CheckedChanged(object sender, EventArgs e)
{
AppSettings apps = new AppSettings();
apps.LogResponses = LogResponsesCheckbox.Checked;
apps.Save();
_comms.SetAppSettings(apps);
}
}
}