private void PwEntryForm_EntrySaving(object aSender, CancellableOperationEventArgs aEventArgs) { /* Get reference to new settings */ var entryForm = aSender as PwEntryForm; if (entryForm != null && (entryForm.DialogResult == DialogResult.OK || saveBeforeCloseQuestionMessageShown)) { var foundControls = entryForm.Controls.Find("EntryPanel", true); if (foundControls.Length != 1) { return; } var entryPanel = foundControls[0] as EntryPanel; if (entryPanel == null) { return; } var settings = entryPanel.CurrentSettings; if (settings == null) { return; } /* validate KeeAgent Entry Settings */ if (settings.AllowUseOfSshKey) { string errorMessage = null; switch (settings.Location.SelectedType) { case EntrySettings.LocationType.Attachment: if (string.IsNullOrWhiteSpace(settings.Location.AttachmentName)) { errorMessage = "Must specify attachment"; } else if (entryForm.EntryBinaries .Get(settings.Location.AttachmentName) == null) { errorMessage = "Attachment does not exist"; } break; case EntrySettings.LocationType.File: if (string.IsNullOrWhiteSpace(settings.Location.FileName)) { errorMessage = "Must specify file name"; } else if (!File.Exists(settings.Location.FileName.ExpandEnvironmentVariables())) { errorMessage = "File does not exist"; } break; default: errorMessage = "Must select attachment or file"; break; } /* if there was a problem with a KeeAgent settings, activate the * KeeAgent tab, show error message and cancel the save */ if (errorMessage != null) { // Activate KeeAgent tab var keeAgentTab = entryPanel.Parent as TabPage; if (keeAgentTab != null) { foundControls = entryForm.Controls.Find("m_tabMain", true); if (foundControls.Length == 1) { var tabControl = foundControls[0] as TabControl; if (tabControl != null) { tabControl.SelectTab(keeAgentTab); } } } MessageService.ShowWarning(errorMessage); aEventArgs.Cancel = true; } } } saveBeforeCloseQuestionMessageShown = false; }
private bool SaveEntry(PwEntry peTarget, bool bValidate) { if(m_pwEditMode == PwEditMode.ViewReadOnlyEntry) return true; if(bValidate && !m_icgPassword.ValidateData(true)) return false; if(this.EntrySaving != null) { CancellableOperationEventArgs eaCancel = new CancellableOperationEventArgs(); this.EntrySaving(this, eaCancel); if(eaCancel.Cancel) return false; } peTarget.History = m_vHistory; // Must be called before CreateBackup() bool bCreateBackup = (m_pwEditMode != PwEditMode.AddNewEntry); if(bCreateBackup) peTarget.CreateBackup(null); peTarget.IconId = m_pwEntryIcon; peTarget.CustomIconUuid = m_pwCustomIconID; if(m_cbCustomForegroundColor.Checked) peTarget.ForegroundColor = m_clrForeground; else peTarget.ForegroundColor = Color.Empty; if(m_cbCustomBackgroundColor.Checked) peTarget.BackgroundColor = m_clrBackground; else peTarget.BackgroundColor = Color.Empty; peTarget.OverrideUrl = m_cmbOverrideUrl.Text; List<string> vNewTags = StrUtil.StringToTags(m_tbTags.Text); peTarget.Tags.Clear(); foreach(string strTag in vNewTags) peTarget.AddTag(strTag); peTarget.Expires = m_cgExpiry.Checked; if(peTarget.Expires) peTarget.ExpiryTime = m_cgExpiry.Value; UpdateEntryStrings(true, false, false); peTarget.Strings = m_vStrings; peTarget.Binaries = m_vBinaries; m_atConfig.Enabled = m_cbAutoTypeEnabled.Checked; m_atConfig.ObfuscationOptions = (m_cbAutoTypeObfuscation.Checked ? AutoTypeObfuscationOptions.UseClipboard : AutoTypeObfuscationOptions.None); SaveDefaultSeq(); peTarget.AutoType = m_atConfig; peTarget.Touch(true, false); // Touch *after* backup if(object.ReferenceEquals(peTarget, m_pwEntry)) m_bTouchedOnce = true; StrUtil.NormalizeNewLines(peTarget.Strings, true); bool bUndoBackup = false; PwCompareOptions cmpOpt = m_cmpOpt; if(bCreateBackup) cmpOpt |= PwCompareOptions.IgnoreLastBackup; if(peTarget.EqualsEntry(m_pwInitialEntry, cmpOpt, MemProtCmpMode.CustomOnly)) { // No modifications at all => restore last mod time and undo backup peTarget.LastModificationTime = m_pwInitialEntry.LastModificationTime; bUndoBackup = bCreateBackup; } else if(bCreateBackup) { // If only history items have been modified (deleted) => undo // backup, but without restoring the last mod time PwCompareOptions cmpOptNH = (m_cmpOpt | PwCompareOptions.IgnoreHistory); if(peTarget.EqualsEntry(m_pwInitialEntry, cmpOptNH, MemProtCmpMode.CustomOnly)) bUndoBackup = true; } if(bUndoBackup) peTarget.History.RemoveAt(peTarget.History.UCount - 1); peTarget.MaintainBackups(m_pwDatabase); if(this.EntrySaved != null) this.EntrySaved(this, EventArgs.Empty); return true; }
private bool SaveEntry() { if(m_pwEditMode == PwEditMode.ViewReadOnlyEntry) return true; if(this.EntrySaving != null) { CancellableOperationEventArgs eaCancel = new CancellableOperationEventArgs(); this.EntrySaving(this, eaCancel); if(eaCancel.Cancel) return false; } m_pwEntry.History = m_vHistory; // Must be called before CreateBackup() bool bCreateBackup = (m_pwEditMode != PwEditMode.AddNewEntry); if(bCreateBackup) m_pwEntry.CreateBackup(null); m_pwEntry.IconId = m_pwEntryIcon; m_pwEntry.CustomIconUuid = m_pwCustomIconID; if(m_cbCustomForegroundColor.Checked) m_pwEntry.ForegroundColor = m_clrForeground; else m_pwEntry.ForegroundColor = Color.Empty; if(m_cbCustomBackgroundColor.Checked) m_pwEntry.BackgroundColor = m_clrBackground; else m_pwEntry.BackgroundColor = Color.Empty; m_pwEntry.OverrideUrl = m_tbOverrideUrl.Text; List<string> vNewTags = StrUtil.StringToTags(m_tbTags.Text); m_pwEntry.Tags.Clear(); foreach(string strTag in vNewTags) m_pwEntry.AddTag(strTag); m_pwEntry.Expires = m_cbExpires.Checked; if(m_pwEntry.Expires) m_pwEntry.ExpiryTime = m_dtExpireDateTime.Value; UpdateEntryStrings(true, false); m_pwEntry.Strings = m_vStrings; m_pwEntry.Binaries = m_vBinaries; m_atConfig.Enabled = m_cbAutoTypeEnabled.Checked; m_atConfig.ObfuscationOptions = (m_cbAutoTypeObfuscation.Checked ? AutoTypeObfuscationOptions.UseClipboard : AutoTypeObfuscationOptions.None); if(m_rbAutoTypeSeqInherit.Checked) m_atConfig.DefaultSequence = string.Empty; else if(m_rbAutoTypeOverride.Checked) m_atConfig.DefaultSequence = m_tbDefaultAutoTypeSeq.Text; else { Debug.Assert(false); } m_pwEntry.AutoType = m_atConfig; m_pwEntry.Touch(true, false); // Touch *after* backup if(m_pwEntry.EqualsEntry(m_pwInitialEntry, false, true, true, false, bCreateBackup, MemProtCmpMode.CustomOnly)) { m_pwEntry.LastModificationTime = m_pwInitialEntry.LastModificationTime; if(bCreateBackup) m_pwEntry.History.Remove(m_pwEntry.History.GetAt( m_pwEntry.History.UCount - 1)); // Undo backup } m_pwEntry.MaintainBackups(m_pwDatabase); if(this.EntrySaved != null) this.EntrySaved(this, EventArgs.Empty); return true; }