Skip to content

Commit

Permalink
Merge if statements
Browse files Browse the repository at this point in the history
  • Loading branch information
robinvanpoppel committed Apr 11, 2020
1 parent d54495d commit 72896ea
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 57 deletions.
14 changes: 4 additions & 10 deletions KeeTrayTOTP/FormSettings.cs
Expand Up @@ -344,12 +344,9 @@ private void WorkerLoad_RunWorkerCompleted(object sender, RunWorkerCompletedEven
else
{
Working(false, true); // Set controls depending on the state of action.
if (e.Result != null)
if (e.Result != null && e.Result.ToString() == "Reset")
{
if (e.Result.ToString() == "Reset")
{
MessageBox.Show(Localization.Strings.SettingsDefaultValuesRestored, Localization.Strings.TrayTOTPPlugin, MessageBoxButtons.OK, MessageBoxIcon.Information);
}
MessageBox.Show(Localization.Strings.SettingsDefaultValuesRestored, Localization.Strings.TrayTOTPPlugin, MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
}
Expand Down Expand Up @@ -426,12 +423,9 @@ private void WorkerSave_RunWorkerCompleted(object sender, RunWorkerCompletedEven
else
{
Working(false, true); //Set controls depending on the state of action.
if (e.Result != null)
if (e.Result != null && e.Result.ToString() == "OK")
{
if (e.Result.ToString() == "OK")
{
DialogResult = DialogResult.OK;
}
DialogResult = DialogResult.OK;
}
}
}
Expand Down
18 changes: 6 additions & 12 deletions KeeTrayTOTP/FormTimeCorrection.cs
Expand Up @@ -51,24 +51,18 @@ private void FormTimeCorrection_Load(object sender, EventArgs e)
{
GlobalWindowManager.AddWindow(this);

Text = Localization.Strings.TimeCorrection + @" - " + Localization.Strings.TrayTOTPPlugin; //Sets the form's display text.
Text = Localization.Strings.TimeCorrection + " - " + Localization.Strings.TrayTOTPPlugin;
if (_plugin.PluginHost.MainWindow.ActiveDatabase.IsOpen)
{
foreach (var pe in _plugin.PluginHost.MainWindow.ActiveDatabase.RootGroup.GetEntries(true)) //Goes through all entries to find existing urls but excludes existing time corrections.
foreach (var pe in _plugin.PluginHost.MainWindow.ActiveDatabase.RootGroup.GetEntries(true))
{
if (_plugin.SettingsCheck(pe)) //Checks that settings exists for this entry.
if (_plugin.SettingsCheck(pe))
{
string[] settings = _plugin.SettingsGet(pe); //Gets the entry's totp settings.
string[] settings = _plugin.SettingsGet(pe);
bool validUrl;
if (_plugin.SettingsValidate(pe, out validUrl)) //Validates the settings.
if (_plugin.SettingsValidate(pe, out validUrl) && validUrl && !ComboBoxUrlTimeCorrection.Items.Contains(settings[2]) && _plugin.TimeCorrections[settings[2]] == null)
{
if (validUrl) //Makes sure the URL is also valid.
{
if (!ComboBoxUrlTimeCorrection.Items.Contains(settings[2]) && _plugin.TimeCorrections[settings[2]] == null) //Checks if not already in combobox or is an existing time correction.
{
ComboBoxUrlTimeCorrection.Items.Add(settings[2]); //Adds the URL to the combobox for quick adding.
}
}
ComboBoxUrlTimeCorrection.Items.Add(settings[2]);
}
}
}
Expand Down
61 changes: 26 additions & 35 deletions KeeTrayTOTP/TrayTOTP_Plugin.cs
Expand Up @@ -202,13 +202,10 @@ public override ToolStripMenuItem GetMenuItem(PluginMenuType type)
if (PluginHost.MainWindow.GetSelectedEntriesCount() == 1)
{
var currentEntry = PluginHost.MainWindow.GetSelectedEntry(true);
if (SettingsCheck(currentEntry) && SeedCheck(currentEntry))
if (SettingsCheck(currentEntry) && SeedCheck(currentEntry) && SettingsValidate(currentEntry))
{
if (SettingsValidate(currentEntry))
{
enMenuCopyTotp.Enabled = true;
enMenuCopyTotp.Tag = currentEntry;
}
enMenuCopyTotp.Enabled = true;
enMenuCopyTotp.Tag = currentEntry;
}

enMenuSetupTotp.Enabled = true;
Expand Down Expand Up @@ -581,12 +578,9 @@ private void OnTimerTick(object sender, EventArgs e)
_liColumnsCount = KeePass.Program.Config.MainWindow.EntryListColumns.Count;
foreach (var column in KeePass.Program.Config.MainWindow.EntryListColumns)
{
if (column.Type == AceColumnType.PluginExt)
if (column.Type == AceColumnType.PluginExt && column.CustomName == Localization.Strings.TOTP)
{
if (column.CustomName == Localization.Strings.TOTP)
{
_liColumnTotpVisible = true;
}
_liColumnTotpVisible = true;
}
}
}
Expand Down Expand Up @@ -627,40 +621,32 @@ private void OnTimerTick(object sender, EventArgs e)
/// <param name="e"></param>
private void SprEngine_FilterCompile(object sender, SprEventArgs e)
{
if ((e.Context.Flags & SprCompileFlags.ExtActive) == SprCompileFlags.ExtActive)
if ((e.Context.Flags & SprCompileFlags.ExtActive) == SprCompileFlags.ExtActive && e.Text.IndexOf(PluginHost.CustomConfig.GetString(setname_string_AutoType_FieldName, setdef_string_AutoType_FieldName).ExtWithBrackets(), StringComparison.InvariantCultureIgnoreCase) >= 0)
{
if (e.Text.IndexOf(PluginHost.CustomConfig.GetString(setname_string_AutoType_FieldName, setdef_string_AutoType_FieldName).ExtWithBrackets(), StringComparison.InvariantCultureIgnoreCase) >= 0)
if (SettingsCheck(e.Context.Entry) && SeedCheck(e.Context.Entry))
{
if (SettingsCheck(e.Context.Entry) && SeedCheck(e.Context.Entry))
if (SettingsValidate(e.Context.Entry))
{
if (SettingsValidate(e.Context.Entry))
{
string[] settings = SettingsGet(e.Context.Entry);
string[] settings = SettingsGet(e.Context.Entry);

TOTPProvider totpGenerator = new TOTPProvider(settings, ref this.TimeCorrections);
TOTPProvider totpGenerator = new TOTPProvider(settings, ref this.TimeCorrections);

string invalidCharacters;
string invalidCharacters;

if (SeedValidate(e.Context.Entry, out invalidCharacters))
{
e.Context.Entry.Touch(false);
string totp = totpGenerator.GenerateByByte(Base32.Decode(SeedGet(e.Context.Entry).ReadString().ExtWithoutSpaces()));
e.Text = StrUtil.ReplaceCaseInsensitive(e.Text, PluginHost.CustomConfig.GetString(setname_string_AutoType_FieldName, setdef_string_AutoType_FieldName).ExtWithBrackets(), totp);
}
else
{
e.Text = string.Empty;
MessageService.ShowWarning(Localization.Strings.ErrorBadSeed + invalidCharacters.ExtWithParenthesis().ExtWithSpaceBefore());
}
if (totpGenerator.TimeCorrectionError)
{
MessageService.ShowWarning(Localization.Strings.WarningBadURL);
}
if (SeedValidate(e.Context.Entry, out invalidCharacters))
{
e.Context.Entry.Touch(false);
string totp = totpGenerator.GenerateByByte(Base32.Decode(SeedGet(e.Context.Entry).ReadString().ExtWithoutSpaces()));
e.Text = StrUtil.ReplaceCaseInsensitive(e.Text, PluginHost.CustomConfig.GetString(setname_string_AutoType_FieldName, setdef_string_AutoType_FieldName).ExtWithBrackets(), totp);
}
else
{
e.Text = string.Empty;
MessageService.ShowWarning(Localization.Strings.ErrorBadSettings);
MessageService.ShowWarning(Localization.Strings.ErrorBadSeed + invalidCharacters.ExtWithParenthesis().ExtWithSpaceBefore());
}
if (totpGenerator.TimeCorrectionError)
{
MessageService.ShowWarning(Localization.Strings.WarningBadURL);
}
}
else
Expand All @@ -669,6 +655,11 @@ private void SprEngine_FilterCompile(object sender, SprEventArgs e)
MessageService.ShowWarning(Localization.Strings.ErrorBadSettings);
}
}
else
{
e.Text = string.Empty;
MessageService.ShowWarning(Localization.Strings.ErrorBadSettings);
}
}
}

Expand Down

0 comments on commit 72896ea

Please sign in to comment.