using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace RDPManager
{
public partial class PassManage : Form
{
private Dictionary<String, Credentials> users = null; // = new Dictionary<String, Credentials>();
public Dictionary<String, Credentials> Users
{
get { return users; }
set { users = value; }
}
public PassManage()
{
InitializeComponent();
}
private void RefreshLB()
{
int len = 0;
if (lbIDs.Items.Count > 0)
{
lbIDs.Items.Clear();
}
if( users != null )
{
foreach (String SvrName in users.Keys)
{
String spaces = "\t";
len = 3 - SvrName.Length/8;
for (int i = 0; i < len; i++)
{
spaces = spaces + "\t";
}
if (users[SvrName].Domain != "")
{
lbIDs.Items.Add(String.Format("{0}{1}{2}\\{3}", SvrName, spaces, users[SvrName].Domain, users[SvrName].UserName));
}
else
{
lbIDs.Items.Add(String.Format("{0}{1}{2}", SvrName, spaces, users[SvrName].UserName));
}
}
}
}
private void btnAdd_Click(object sender, EventArgs e)
{
String SvrName = "";
Credentials cred = new Credentials();
while (true)
{
PasswordForm passForm = new PasswordForm();
if (passForm.ShowDialog() == DialogResult.OK)
{
SvrName = passForm.Machine;
SvrName = SvrName.ToUpper();
if (users.ContainsKey(SvrName))
{
MessageBox.Show("Error, a Username/Password already exists for " + SvrName);
return;
}
cred.UserName = passForm.UserName;
cred.Password = Program.GetEncodedPassword(passForm.Password);
cred.Domain = passForm.Domain;
if (cred.UserName.Length <= 0)
{
MessageBox.Show("You must put in a username!");
continue;
}
if (cred.Password.Length <= 0)
{
MessageBox.Show("You must put in a password!");
continue;
}
if (SvrName.Length <= 0)
{
MessageBox.Show("You must put in a Machine!");
continue;
}
break;
}
else
{
return;
}
}
users[SvrName] = cred;
RefreshLB();
}
private void btnEdit_Click(object sender, EventArgs e)
{
if (lbIDs.SelectedIndex == -1) return;
String selItem = lbIDs.SelectedItem.ToString(); ;
String SvrName = selItem.Substring(0, selItem.IndexOf('\t'));
if(SvrName != "Default" )
SvrName = SvrName.Trim().ToUpper();
Credentials cred = users[SvrName];
PasswordForm passForm = new PasswordForm();
passForm.UserName = cred.UserName;
//pForm.Password = users[SvrName].Password;
passForm.Domain = cred.Domain;
passForm.Machine = SvrName;
if(SvrName == "Default") passForm.DisMachineCtrl = true;
while (true)
{
passForm.ShowDialog();
if (passForm.DialogResult == DialogResult.OK)
{
string newSvrName = passForm.Machine;
if (!users.ContainsKey(newSvrName))
{
users.Remove(SvrName);
users[newSvrName] = cred;
}
SvrName = newSvrName;
cred.UserName = passForm.UserName;
cred.Password = Program.GetEncodedPassword(passForm.Password);
cred.Domain = passForm.Domain;
if (cred.UserName.Length <= 0)
{
MessageBox.Show("Username can not be blank!");
continue;
}
if (cred.Password.Length <= 0)
{
MessageBox.Show("Password can not be blank!");
continue;
}
if (SvrName.Length <= 0)
{
MessageBox.Show("Machine must not be blank!");
continue;
}
break;
}
else
{
return;
}
}
RefreshLB();
}
private void btnDel_Click(object sender, EventArgs e)
{
if (lbIDs.SelectedIndex <= 0) return;
String selItem = lbIDs.SelectedItem.ToString();
String SvrName = selItem.Substring(0, selItem.IndexOf('\t'));
SvrName = SvrName.Trim();
users.Remove(SvrName);
RefreshLB();
}
private void PassManage_Shown(object sender, EventArgs e)
{
RefreshLB();
}
private void btnOK_Click(object sender, EventArgs e)
{
DialogResult = DialogResult.OK;
Close();
}
private void btnCancel_Click(object sender, EventArgs e)
{
DialogResult = DialogResult.Cancel;
Close();
}
}
}