SafeDispatch/SubscriberAndUserManager/Login.cs

352 lines
14 KiB
C#
Raw Permalink Normal View History

2024-02-22 16:43:59 +00:00
using System;
using System.Windows.Forms;
using SafeMobileLib;
using Telerik.WinControls;
using Nini.Config;
using System.Drawing;
using Telerik.WinControls.UI;
using System.ComponentModel;
using System.Collections.Generic;
using System.Threading;
using System.Timers;
namespace SubscriberAndUserManager
{
public partial class Login : Telerik.WinControls.UI.RadForm
{
private IConfigSource source = null;
private string adminPassword;
private RegistrationStatus regStatus = RegistrationStatus.NOT_REGISTERED;
public static Color ButtonColor = Color.White;
public static Color ButtonColorDisabled = Color.FromArgb(200, 200, 200);
public DBuserManager dbUserManager;
public enum RegistrationStatus { REGISTERED, NOT_REGISTERED, UNAUTHORIZED }
public Login(RegistrationStatus registrationStatus)
{
regStatus = registrationStatus;
InitializeComponent();
FireWallException fw = new FireWallException();
string name = System.AppDomain.CurrentDomain.FriendlyName;
string path = System.Windows.Forms.Application.ExecutablePath;
fw.checkFirewall(name, path);
RadMessageBox.SetThemeName("TelerikMetroBlue");
#region LANGUAGE POPULATE
List<String> languages = new List<string>() { "English", "Chinese", "Czech", "French", "German",
"Italian", "Japanese", "Polish", "Portuguese", "Romanian", "Russian", "Serbian", "Spanish",
"Swedish", "Turkish", "Arabic" };
languageImages.Images.Add(SubscriberAndUserManager.Properties.Resources.f_en);
languageImages.Images.Add(SubscriberAndUserManager.Properties.Resources.f_cn);
languageImages.Images.Add(SubscriberAndUserManager.Properties.Resources.f_cz);
languageImages.Images.Add(SubscriberAndUserManager.Properties.Resources.f_fr);
languageImages.Images.Add(SubscriberAndUserManager.Properties.Resources.f_de);
languageImages.Images.Add(SubscriberAndUserManager.Properties.Resources.f_it);
languageImages.Images.Add(SubscriberAndUserManager.Properties.Resources.f_jp);
languageImages.Images.Add(SubscriberAndUserManager.Properties.Resources.f_pl);
languageImages.Images.Add(SubscriberAndUserManager.Properties.Resources.f_pt);
languageImages.Images.Add(SubscriberAndUserManager.Properties.Resources.f_ro);
languageImages.Images.Add(SubscriberAndUserManager.Properties.Resources.f_ru);
languageImages.Images.Add(SubscriberAndUserManager.Properties.Resources.f_rs);
languageImages.Images.Add(SubscriberAndUserManager.Properties.Resources.f_es);
languageImages.Images.Add(SubscriberAndUserManager.Properties.Resources.f_se);
languageImages.Images.Add(SubscriberAndUserManager.Properties.Resources.f_tr);
languageImages.Images.Add(SubscriberAndUserManager.Properties.Resources.f_ar);
BindingList<DropListItem> langs = new BindingList<DropListItem>();
DropListItem item = new DropListItem();
for (int i = 0; i < languages.Count; i++ )
langs.Add(new DropListItem() { Text = languages[i], Image = languageImages.Images[i] });
cbLang.DataSource = null;
cbLang.DataSource = langs;
cbLang.DropDownListElement.ItemHeight = 36;
#endregion
#region THEMING
Icon appIcon = SubscriberAndUserManager.Properties.Resources.sdadmin_icon;
Image appImage = SubscriberAndUserManager.Properties.Resources.sd256PNG;
Image backImage = SubscriberAndUserManager.Properties.Resources.bg_moto;
String appName = "SafeDispatch Admin";
Color errorColor = Color.LimeGreen;
Color appNameForeColor = Color.WhiteSmoke;
switch(MainForm.radioType)
{
case RADIOTYPE.SIMOCO:
{
backImage = SubscriberAndUserManager.Properties.Resources.bg_simoco;
appImage = SubscriberAndUserManager.Properties.Resources.simoco_setup_128;
appIcon = SubscriberAndUserManager.Properties.Resources.simoco_setup_64;
appName = "Simoco Admin";
errorColor = Color.Firebrick;
appNameForeColor = Color.WhiteSmoke;
break;
}
case RADIOTYPE.EXCERA:
{
backImage = SubscriberAndUserManager.Properties.Resources.axys;
this.BackgroundImageLayout = ImageLayout.Stretch;
appImage = SubscriberAndUserManager.Properties.Resources.AXYS_TOOLS_ICON;
appIcon = SubscriberAndUserManager.Properties.Resources.AXYS_ICON;
appName = "AXYS Admin";
errorColor = Color.Firebrick;
appNameForeColor = Color.WhiteSmoke;
break;
}
case RADIOTYPE.HYT:
{
backImage = SubscriberAndUserManager.Properties.Resources.bg_hytera;
appImage = SubscriberAndUserManager.Properties.Resources.SafeSyt;
appIcon = SubscriberAndUserManager.Properties.Resources.SYTADMIN64x;
appName = "SafeSyt Admin";
errorColor = Color.Firebrick;
appNameForeColor = Color.Black;
break;
}
case RADIOTYPE.HARRIS:
{
backImage = SubscriberAndUserManager.Properties.Resources.bg_harris;
appImage = SubscriberAndUserManager.Properties.Resources.vision96PNG;
appIcon = SubscriberAndUserManager.Properties.Resources.visionSmall;
appName = "Vision Admin";
errorColor = Color.Black;
appNameForeColor = Color.Black;
break;
}
case RADIOTYPE.ATLAS:
{
backImage = SubscriberAndUserManager.Properties.Resources.bg_atlas;
appImage = SubscriberAndUserManager.Properties.Resources.atlas256PNG;
appIcon = SubscriberAndUserManager.Properties.Resources.ATLASadmin32;
appName = "ATL25 Admin";
errorColor = Color.LimeGreen;
appNameForeColor = Color.WhiteSmoke;
break;
}
case RADIOTYPE.LINX:
case RADIOTYPE.LINXB:
{
appName = "LINX Admin";
break;
}
default: break;
}
this.BackgroundImage = backImage;
this.labelAppName.Text = appName.ToUpper();
lbStatus.ForeColor = errorColor;
labelAppName.ForeColor = appNameForeColor;
this.pbIcon.Image = appImage;
this.Icon = appIcon;
#endregion
//cbLang.SelectedIndex = 1;
cbLang.SelectedIndex = 0;
if ((MainForm.Langidx>=0)&&(MainForm.Langidx<cbLang.Items.Count))
cbLang.SelectedIndex = MainForm.Langidx;
Version v = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version;
lbVersion.Text = "Version: " + v.ToString();
updateUI(registrationStatus);
this.TopLevel = true;
this.TopMost = true;
}
public void OnRegistrationStatusChanged(RegistrationStatus registrationStatus)
{
regStatus = registrationStatus;
updateUI(regStatus);
}
private System.Timers.Timer timer;
private void updateUI(RegistrationStatus regStatus)
{
timer?.Stop();
if (regStatus == RegistrationStatus.REGISTERED)
{
try
{
adminPassword = dbUserManager.getAdminPsw();
Console.WriteLine("admin=" + adminPassword);
}
catch (Exception ex)
{
Console.WriteLine("DB failed:" + ex.ToString());
RadMessageBox.Show(MainForm.returnLNGString("dberr"), MainForm.returnLNGString("err"), MessageBoxButtons.OK, RadMessageIcon.Exclamation);
//CustomMessageBox.Show(CustomMessageBoxType.exclamation, MainForm.returnLNGString("dberr"), MainForm.returnLNGString("err"));00
Application.Exit();
}
tbPassword.Enabled = true;
btnLogin.Enabled = true;
btnLogin.Text = "Login";
//DialogResult = DialogResult.No;
if (adminPassword == "")
lbStatus.Text = "User \"administrator\" not found in DB !!!";
}
else if (regStatus == RegistrationStatus.UNAUTHORIZED)
{
lbStatus.Visible = false;
tbPassword.Enabled = false;
btnLogin.Text = "Unregistered";
}
else if (regStatus == RegistrationStatus.NOT_REGISTERED)
{
lbStatus.Visible = false;
tbPassword.Enabled = false;
btnLogin.Text = "Registering";
timer = new System.Timers.Timer();
// Tell the timer what to do when it elapses
timer.Elapsed += delegate(object source, ElapsedEventArgs e)
{
btnLogin?.Invoke((MethodInvoker)delegate
{
if (btnLogin.Text.ToString().Contains("..."))
btnLogin.Text = btnLogin.Text.ToString().Replace(".", "");
else
btnLogin.Text += ".";
});
};
// Set it to go off every five seconds
timer.Interval = 500;
// And start it
timer.Enabled = true;
}
btnLogin.Enabled = false;
}
private void btnLogin_Click(object sender, EventArgs e)
{
if (regStatus != RegistrationStatus.REGISTERED)
{
DialogResult = DialogResult.OK;
MainForm.Langidx = cbLang.SelectedIndex;
SaveConfig();
this.Close();
}
else
{
if (adminPassword == tbPassword.Text)
{
DialogResult = DialogResult.OK;
MainForm.Langidx = cbLang.SelectedIndex;
SaveConfig();
this.Close();
}
else
{
lbStatus.Text = "Incorect password!!!";
lbStatus.Visible = Visible;
tbPassword.Text = "";
}
}
}
private void tbPassword_KeyPress(object sender, KeyPressEventArgs e)
{
if ((int)e.KeyChar == 13) btnLogin.PerformClick();
}
public void SaveConfig()
{
try
{
source = new IniConfigSource(MainForm.CFG_FILE);
if (source.Configs["Language"] == null)
source.Configs.Add("Language");
source.Configs["Language"].Set("Language", MainForm.Langidx.ToString());
source.Save();
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
}
private void Login_FormClosing(object sender, FormClosingEventArgs e)
{
dbUserManager?.CloseConnection();
}
private void Login_FormClosed(object sender, FormClosedEventArgs e)
{
if(DialogResult != DialogResult.OK)
System.Windows.Forms.Application.Exit();
}
private void Login_Shown(object sender, EventArgs e)
{
tbPassword.Focus();
}
/// <summary>
/// Display the image and text for each unit in the combobox
/// </summary>
private void cbLang_ItemDataBound(object sender, ListItemDataBoundEventArgs args)
{
// display the text and image for each item in the comboBox
DropListItem item = (DropListItem)args.NewItem.DataBoundItem;
args.NewItem.Text = item.Text;
args.NewItem.Image = item.Image;
}
/// <summary>
/// intercept the changes in password text in order to enable or disable the
/// login button
/// </summary>
private void tbPassword_TextChanged(object sender, EventArgs e)
{
if (tbPassword.Text.Length > 0)
btnLogin.Enabled = true;
else
btnLogin.Enabled = false;
}
/// <summary>
/// Change the buttons background color for the animation buttons depending on their enabled/disabled
/// state
/// </summary>
private void btnLogin_EnabledChanged(object sender, EventArgs e)
{
if (((RadButton)sender).Enabled)
((RadButton)sender).ButtonElement.ButtonFillElement.BackColor = ButtonColor;
else
((RadButton)sender).ButtonElement.ButtonFillElement.BackColor = ButtonColorDisabled;
}
/// <summary>
/// Select all text when password field get focused
/// </summary>
private void tbPassword_Enter(object sender, EventArgs e)
{
tbPassword.SelectAll();
}
}
}