using System;
using System.Collections.Generic;
using System.Text;
using Fluid.Controls;
using PasswordSafe.Xml;
using System.Drawing;
using System.Windows.Forms;
using PasswordSafe.Classes;
namespace PasswordSafe.Templates
{
public class PasswordTemplate : FluidTemplate
{
protected override void InitControl()
{
base.InitControl();
this.Bounds = new Rectangle(0, 0, 240, 64);
FluidLabel l;
//l = new FluidLabel("", 3, 0, 228, 20);
//l.LineAlignment = StringAlignment.Near;
// l.Anchor = AnchorStyles.Left | AnchorStyles.Right | AnchorStyles.Top;
l = new FluidLabel("", 3, 0, 228, 64 - 3);
l.Anchor = AnchorStyles.Left | AnchorStyles.Right | AnchorStyles.Top | AnchorStyles.Bottom;
l.LineAlignment = StringAlignment.Center;
l.Font = new Font(FontFamily.GenericSansSerif, 10f, FontStyle.Bold);
nameLabel = l;
Controls.Add(l);
l = new FluidLabel("", 8, 62 - 12, 224, 12);
l.LineAlignment = StringAlignment.Far;
l.Alignment = StringAlignment.Far;
l.Anchor = AnchorStyles.Left | AnchorStyles.Right | AnchorStyles.Bottom;
l.Font = new Font(FontFamily.GenericSansSerif, 6f, FontStyle.Regular);
l.ForeColor = Theme.Current.ListSecondaryForeColor;
secondaryLabel = l;
Controls.Add(l);
}
private FluidLabel nameLabel;
private FluidLabel secondaryLabel;
protected override void OnBindValue(object value)
{
PasswordData password = value as PasswordData;
if (password != null)
{
nameLabel.Text = password.Name;
secondaryLabel.Text = GetSecondaryLabel(password);
}
else
{
nameLabel.Text = "";
secondaryLabel.Text = "";
}
}
/// <summary>
/// Get the secondary label from a password.
/// User is prefered, but if emtpy, select the next field that is not empty and which is not a password.
/// </summary>
/// <param name="password">The PasswordData from which to retreive the secondary label.</param>
/// <returns>A string, otherwise String.Empty.</returns>
private string GetSecondaryLabel(PasswordData password)
{
if (!string.IsNullOrEmpty(password.User)) return password.User;
bool first = true;
foreach (string name in password.Values.AllKeys)
{
if (first)
{
first = false;
continue;
}
string value = password.Values[name];
if (value == password.Password) continue;
if (!string.IsNullOrEmpty(value))
{
return value;
}
}
return string.Empty;
}
protected override void OnItemUpdate(object value)
{
PasswordsListBox lb = Parent as PasswordsListBox;
secondaryLabel.ForeColor = lb.SelectedItemIndex == this.ItemIndex ? Theme.Current.ListSecondarySelectedForeColor : Theme.Current.ListSecondaryForeColor;
}
}
}