Menu

[r25]: / trunk / WhatsappClient / Forms / Form_Login.cs  Maximize  Restore  History

Download this file

273 lines (239 with data), 9.8 kB

  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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
/*
* by Swen Kooij aka Kirk - swenkooij@gmail.com
*/
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WhatsappClient
{
/// <summary>
/// The whatsapp login form, select an account from the list or create one here.
/// </summary>
public partial class Form_Login : Form
{
/// <summary>
/// An instance of the SQLiteDatabase class, represents a connection to
/// a SQL Lite database.
/// </summary>
public SQLiteDatabase Database;
/// <summary>
/// A list that holds instances of the the Account class. This list will be filled on form load.
/// </summary>
private List<WhatsappAccount> WhatsappAccounts = new List<WhatsappAccount>();
/// <summary>
/// Class Constructor
/// </summary>
public Form_Login()
{
InitializeComponent();
// Check if the databas exists
if (System.IO.File.Exists("whatsapp_db.s3db"))
{
Database = new SQLiteDatabase("whatsapp_db.s3db");
}
else
{
MessageBox.Show("The database is missing (whatsapp_db.s3db), make sure it's in the same folder as the application.", "Error: Database missing", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
}
/// <summary>
/// Event: Called when the form has loaded.
/// </summary>
private void Form_Login_Load(object sender, EventArgs e)
{
// Load accounts
updateAccounts();
// Set form size
this.Size = new Size(483, 191);
// Set default Device OS
ComboBox_DeviceOS.Text = "Android";
}
/// <summary>
/// Reloads all accounts from the database
/// </summary>
private void updateAccounts()
{
// Read all info from the database
WhatsappAccounts.Clear();
ComboBox_Login.DataSource = null;
ComboBox_Login.Items.Clear();
DataRowCollection Rows = Database.GetTableRows("accounts", "");
foreach (DataRow Row in Rows)
{
WhatsappAccount Acc = new WhatsappAccount(Row["phonenumber"].ToString(), Row["password"].ToString());
WhatsappAccounts.Add(Acc);
}
// Set data source and display member of combobox
ComboBox_Login.DataSource = WhatsappAccounts;
ComboBox_Login.DisplayMember = "Phonenumber";
}
/// <summary>
/// Event: 'Add account' button has been clicked, resize form
/// </summary>
private void Button_AddAccount_Click(object sender, EventArgs e)
{
if (this.Size == new Size(483, 440))
{
CollapseForm();
}
else
{
ExpandForm();
}
}
/// <summary>
/// Expands the size of the form so the 'Add account' groupbox is visible.
/// </summary>
private void ExpandForm()
{
this.Size = new Size(483, 440);
}
/// <summary>
/// Collapses the size of the form so the 'Add account' groupbox is no longer visible.
/// </summary>
private void CollapseForm()
{
this.Size = new Size(483, 191);
}
/// <summary>
/// Sets the Enabled property of all controls on the form.
/// </summary>
/// <param name="trueFalse">Boolean value, true to enable and false to disable.</param>
private void SetControlsEnabled(bool trueFalse)
{
ComboBox_DeviceOS.Enabled = trueFalse;
Button_AddAccount.Enabled = trueFalse;
Button_Login.Enabled = trueFalse;
Button_AddThisAccount.Enabled = trueFalse;
TextBox_ImeiMac.Enabled = trueFalse;
TextBox_Phonenumber.Enabled = trueFalse;
ComboBox_Login.Enabled = trueFalse;
}
/// <summary>
/// Event: The selected item in de DeviceOS combo box changed
/// </summary>
private void ComboBox_DeviceOS_SelectedIndexChanged(object sender, EventArgs e)
{
if (ComboBox_DeviceOS.Text == "iOS")
Label_ImeiMac.Text = "Wifi Mac Adress:";
else
Label_ImeiMac.Text = "International Mobile Equipment Identity (IMEI):";
}
/// <summary>
/// Event: The 'Add this account' button has been clicked.
/// </summary>
private void Button_AddThisAccount_Click(object sender, EventArgs e)
{
// Disable controls
SetControlsEnabled(false);
// Show 'please wait' message
Label_Status.Visible = true;
// Start verifyAccount() in a new thread
ThreadedMethod tMethod = new ThreadedMethod(verifyAccount);
tMethod.Start();
}
/// <summary>
/// Verifies the new account that is about to be added, this method is supposed to run
/// in a seperated thread.
/// </summary>
private void verifyAccount()
{
WhatsAppHelper Helper = new WhatsAppHelper();
WhatsAppHelper.DeviceOS OS = WhatsAppHelper.DeviceOS.Other;
if (ComboBox_DeviceOS.Text == "iOS") OS = WhatsAppHelper.DeviceOS.iOS;
string Password = Helper.generatePassword(TextBox_ImeiMac.Text, OS);
bool Result = Helper.verifyAccount(TextBox_Phonenumber.Text, Password);
if (Result == true)
{
// Account exists, insert into database
Dictionary<string, string> Values = new Dictionary<string,string>();
Values.Add("phonenumber", TextBox_Phonenumber.Text);
Values.Add("password", Password);
Database.Insert("accounts", Values);
// Resize form
CollapseForm();
// Update combobox
updateAccounts();
// Set phonenumber as selected
ComboBox_Login.Text = TextBox_Phonenumber.Text;
// Clear all fields
TextBox_ImeiMac.Text = "";
TextBox_Phonenumber.Text = "";
ComboBox_DeviceOS.Text = "Android";
Label_ImeiMac.Text = "International Mobile Equipment Identity (IMEI):";
// Re-enable controls
SetControlsEnabled(true);
Label_Status.Visible = false;
}
else
{
// Account does not exists
MessageBox.Show("Sorry, your account does not exists or you have given invalid information. Please re-check all field.", "Error: Account does not exists", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
}
/// <summary>
/// Event: The login button has been clicked
/// </summary>
private void Button_Login_Click(object sender, EventArgs e)
{
// Diable constrols and show message
Label_Status.Visible = true;
Label_Status.Text = "Logging in...";
SetControlsEnabled(false);
ThreadedMethod tMethod = new ThreadedMethod(login);
tMethod.Start();
}
/// <summary>
/// Handles logging in, supposed to run in a seperated thread
/// </summary>
private void login()
{
if (ComboBox_Login.SelectedItem == null)
return;
WhatsappAccount Account = (WhatsappAccount)ComboBox_Login.SelectedItem;
// Verify Account
WhatsAppHelper Helper = new WhatsAppHelper();
bool Result = Helper.verifyAccount(Account.Phonenumber, Account.Password);
if (Result == true)
{
// Account is valid, show main form
Form_Main frmMain = new Form_Main(Account);
this.Close();
frmMain.ShowDialog();
}
else
{
DialogResult Re = MessageBox.Show("The selected acocunt does not appear to be working, do you want to remove it?", "Error: Account not working", MessageBoxButtons.YesNo, MessageBoxIcon.Error);
if (Re == DialogResult.Yes)
{
// Delete account
Database.Query("DELETE FROM accounts WHERE phonenumber='" + Account.Phonenumber + "'");
// Reload combobox
updateAccounts();
// Hide status label
Label_Status.Visible = false;
Label_Status.Text = "Please wait while we verify your account.";
// Enable controls
SetControlsEnabled(true);
}
else
{
// Reload combobox
updateAccounts();
// Hide status label
Label_Status.Visible = false;
Label_Status.Text = "Please wait while we verify your account.";
// Enable controls
SetControlsEnabled(true);
}
}
}
}
}
MongoDB Logo MongoDB