using System;
using System.Configuration;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Net.NetworkInformation;
using System.Windows.Forms;
using AutoTypeSearch.Properties;
using KeePass.Forms;
using KeePass.Plugins;
using KeePassLib;
using KeePassLib.Native;
namespace AutoTypeSearch
{
internal partial class Options : UserControl
{
private const string OptionsConfigRoot = "AutoTypeSearchExt.";
private static int sRegisteredHotkeyId;
// ReSharper disable once MemberCanBePrivate.Global - Public for forms designer
public Options()
{
InitializeComponent();
// Must mach order and values of Actions enum
var actions = new object[] { Resources.PerformAutoType, Resources.EditEntry, Resources.ShowEntry, Resources.OpenEntryUrl, Resources.CopyPassword };
mDefaultAction.Items.AddRange(actions);
mAlternativeAction.Items.AddRange(actions);
// Read options
mShowOnFailedSearch.Checked = Settings.Default.ShowOnFailedAutoType;
if (NativeLib.IsUnix())
{
mShowOnHotKey.Enabled = false;
mShowOnHotKey.Checked = false;
mShowHotKeyControl.Clear();
}
else
{
mShowOnHotKey.Checked = Settings.Default.ShowOnHotKey;
ShowHotKey = Settings.Default.ShowHotKey;
}
mShowOnHotKey_CheckedChanged(null, EventArgs.Empty);
mShowOnIPC.Checked = Settings.Default.ShowOnIPC;
mSearchInTitle.Checked = Settings.Default.SearchTitle;
mSearchInUserName.Checked = Settings.Default.SearchUserName;
mSearchInUrl.Checked = Settings.Default.SearchUrl;
mSearchInNotes.Checked = Settings.Default.SearchNotes;
mSearchInTags.Checked = Settings.Default.SearchTags;
mSearchInOtherFields.Checked = Settings.Default.SearchCustomFields;
mCaseSensitive.Checked = Settings.Default.CaseSensitive;
mExcludeExpired.Checked = Settings.Default.ExcludeExpired;
mResolveReferences.Checked = Settings.Default.ResolveReferences;
mDefaultAction.SelectedIndex = (int)Settings.Default.DefaultAction;
mAlternativeAction.SelectedIndex = (int)Settings.Default.AlternativeAction;
mAdditionalSequences.BeginUpdate();
mAdditionalSequences.Items.Clear();
if (Settings.Default.AdditionalCustomSequences != null)
{
foreach (string s in Settings.Default.AdditionalCustomSequences)
{
mAdditionalSequences.Items.Add(s);
}
}
mAdditionalSequences.Items.Add("new sequence");
mAdditionalSequences.EndUpdate();
}
private Keys ShowHotKey
{
get { return mShowHotKeyControl.HotKey; }
set { mShowHotKeyControl.HotKey = value; }
}
private void mShowOnHotKey_CheckedChanged(object sender, EventArgs e)
{
mShowHotKeyControl.Enabled = mShowOnHotKey.Checked;
}
private void ApplySettings()
{
// Apply settings
Settings.Default.ShowOnFailedAutoType = mShowOnFailedSearch.Checked;
Settings.Default.ShowOnHotKey = mShowOnHotKey.Checked;
Settings.Default.ShowOnIPC = mShowOnIPC.Checked;
Settings.Default.SearchTitle = mSearchInTitle.Checked;
Settings.Default.SearchUserName = mSearchInUserName.Checked;
Settings.Default.SearchUrl = mSearchInUrl.Checked;
Settings.Default.SearchNotes = mSearchInNotes.Checked;
Settings.Default.SearchTags = mSearchInTags.Checked;
Settings.Default.SearchCustomFields = mSearchInOtherFields.Checked;
Settings.Default.CaseSensitive = mCaseSensitive.Checked;
Settings.Default.ExcludeExpired = mExcludeExpired.Checked;
Settings.Default.ResolveReferences = mResolveReferences.Checked;
Settings.Default.DefaultAction = (Actions)mDefaultAction.SelectedIndex;
Settings.Default.AlternativeAction = (Actions)mAlternativeAction.SelectedIndex;
Settings.Default.ShowHotKey = ShowHotKey;
ApplyHotKey();
}
#region Settings persistence
public static void SaveSettings(IPluginHost host)
{
if (host != null)
{
foreach (SettingsPropertyValue property in Settings.Default.PropertyValues)
{
if (property.IsDirty)
{
var value = property.SerializedValue as String;
if (value != null)
{
host.CustomConfig.SetString(OptionsConfigRoot + property.Name, value);
}
else
{
Debug.Fail("Non-string serialized settings property");
}
}
}
}
}
public static void LoadSettings(IPluginHost host)
{
if (host != null)
{
// ReSharper disable once UnusedVariable
var ignored = Settings.Default.ShowOnFailedAutoType; //Access any property just to make it load settings.
foreach (SettingsPropertyValue property in Settings.Default.PropertyValues)
{
var value = host.CustomConfig.GetString(OptionsConfigRoot + property.Name);
if (value != null)
{
property.SerializedValue = value;
property.Deserialized = false;
property.IsDirty = false;
}
}
ApplyHotKey();
}
}
#endregion
#region Hotkey
private static void ApplyHotKey()
{
UnregisterHotKey();
if (Settings.Default.ShowOnHotKey && Settings.Default.ShowHotKey != Keys.None)
{
sRegisteredHotkeyId = HotKeyManager.RegisterHotKey(Settings.Default.ShowHotKey);
}
}
public static void UnregisterHotKey()
{
if (sRegisteredHotkeyId != 0)
{
var result = HotKeyManager.UnregisterHotKey(sRegisteredHotkeyId);
Debug.Assert(result);
sRegisteredHotkeyId = 0;
}
}
#endregion
public static void AddToWindow(OptionsForm optionsForm)
{
var tabControl = optionsForm.Controls.Find("m_tabMain", false).FirstOrDefault() as TabControl;
var okButton = optionsForm.Controls.Find("m_btnOK", false).FirstOrDefault() as Button;
if (tabControl == null || okButton == null)
{
Debug.Fail("Could not integrate with options form");
}
var tabPage = new TabPage(Resources.AutoTypeSearch)
{
UseVisualStyleBackColor = true,
AutoScroll = true,
ImageIndex = (int)PwIcon.EMailSearch
};
var options = new Options { Dock = DockStyle.Fill };
tabPage.Controls.Add(options);
tabControl.TabPages.Add(tabPage);
okButton.Click += delegate
{
options.ApplySettings();
};
}
private void Options_Paint(object sender, PaintEventArgs e)
{
Rectangle r = new Rectangle(mPanel.Location, mPanel.ClientSize);
//r.Offset(mPanel.Location);
r.X -= 1;
r.Y -= 1;
r.Width += 2 + SystemInformation.VerticalScrollBarWidth;
r.Height += 2;
ControlPaint.DrawBorder(e.Graphics, r, SystemColors.GrayText, ButtonBorderStyle.Solid);
}
}
}