EduNetworkBuilder/EduNetworkBuilder/PersonProfileForm.cs

165 lines
5.4 KiB
C#

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;
using System.IO;
namespace EduNetworkBuilder
{
public partial class PersonProfileForm : Form
{
PersonClass CurrentUser = null;
string FileName = "";
public PersonProfileForm(string filename = "")
{
InitializeComponent();
FileName = filename;
//We do LocalSetup at load time. This saves us some grief.
}
public PersonProfileForm(PersonClass User)
{
InitializeComponent();
CurrentUser = User;
if (CurrentUser != null)
{
FileName = Path.Combine(CurrentUser.filepath, CurrentUser.UserName);
}
//We do LocalSetup at load time. This saves us some grief.
}
private void PersonProfileForm_Load(object sender, EventArgs e)
{
LocalSetup();
UpdateFormFromUser();
}
private void LanguagifyComponents()
{
lblFullName.Text = NB.Translate("PPF_FullName");
lblUsername.Text = NB.Translate("PPF_UserName");
TabClasswork.Text = NB.Translate("PPF_TabClasswork");
TabProfile.Text = NB.Translate("PPF_TabProfile");
btnExit.Text = NB.Translate("NB_exitToolStripMenuItem");
}
private void LocalSetup()
{
Icon = Properties.Resources.NBIco;
LanguagifyComponents();
if (CurrentUser == null && FileName == "")
{
//Prompt for a username
String Dest = NB.TextPromptBox(NB.Translate("PPF_EnterUserName"));
if (Dest == "") Close();//No name given or canceled.
if (Dest[0] != '_') Dest = "_Teacher_" + Dest; //Make sure it begins with _
//Find a directory for it.
CurrentUser = new PersonClass(Dest, true); //Make an admin person class
//Choose an initial folder for the file:
OpenFileDialog OFD = new OpenFileDialog();
OFD.ValidateNames = false;
OFD.CheckFileExists = false;
OFD.CheckPathExists = true;
OFD.FileName = NB.Translate("PPF_SelectThisFolder");
OFD.ShowDialog();
Console.WriteLine(Path.GetDirectoryName(OFD.FileName));
}
else if (CurrentUser == null)
{
//Try to load the file. Close form & give error if it fails
try
{
//Load in from the file
}
catch(Exception e)
{
CurrentUser = null;
Close(); //we did not succeed. Exit out
}
}
//Make sure we update any profile settings they change
tbFullName.LostFocus += SaveUserInfoFromForm;
}
public PersonClass Edit()
{
BuilderWindow BW = NB.GetBuilderWin();
if(BW != null)
{
BW.Hide();
this.ShowDialog();
BW.Show();
}
return CurrentUser;
}
private void UpdateFormFromUser()
{
if (CurrentUser == null) return;
tbFullName.Text = CurrentUser.FullName;
tbUsername.Text = CurrentUser.UserName;
if (CurrentUser.isAdmin) this.Text = "Admin: " + CurrentUser.UserName;
else this.Text = "Student: " + CurrentUser.UserName;
}
private void SaveUserInfoFromForm()
{
CurrentUser.FullName = tbFullName.Text;
}
private void SaveUserInfoFromForm(object sender, EventArgs e)
{
SaveUserInfoFromForm();
}
private void btnExit_Click(object sender, EventArgs e)
{
//Save during closing
Close();
}
private void btnChangePassword_Click(object sender, EventArgs e)
{
string OldPassword = "";
if (CurrentUser.Password() != "")
{
OldPassword = NB.TextPromptBox(NB.Translate("PPF_OldPassword"), Properties.Resources.NBIco, true);
if(OldPassword != CurrentUser.Password())
{
MessageBox.Show(NB.Translate("PPF_PassMismatch"));
return;
}
}
//The passwords match, or there was no initial password.
string Password1 = NB.TextPromptBox(NB.Translate("PPF_NewPass"), Properties.Resources.NBIco, true);
if(Password1.Length < 4)
{
MessageBox.Show(NB.Translate("PPF_PassTooShort"));
return;
}
if(Password1 == OldPassword)
{
MessageBox.Show(NB.Translate("PPF_PassDidNotChange"));
return;
}
string Password2 = NB.TextPromptBox(NB.Translate("PPF_VerifyPass"), Properties.Resources.NBIco, true);
if(Password1 != Password2)
{
MessageBox.Show(NB.Translate("PPF_PassValidateMismatch"));
return;
}
CurrentUser.ChangePassword(Password1);
}
}
}