public static PwDocument CreateRecycleBin(this PwDocument pwDocument) { var recycleBin = pwDocument.Database.RootGroup.FindCreateGroup("Recycle bin", true); pwDocument.Database.RecycleBinUuid = recycleBin.Uuid; return(pwDocument); }
public void BuildMenuItemsForRootDropDown_ShouldReturnEntryMenuItems_IfThereIsOnlyASingleDatabase() { var pwDocument = new PwDocument().New().WithNonTotpEntries(2).WithTotpEnabledEntries(4); var sut = _trayMenuItemProvider.BuildMenuItemsForRootDropDown(pwDocument.AsList()).ToList(); sut.Count.Should().Be(4, "because, the items are added directly to the root tray menuitem if there is only a single database opened."); }
public void BuildMenuItemsForRootDropDown_ShouldCreateASingleMenuItemWithLocked_IfASingleLockedDatabasesIsPresent() { var pwDocument = new PwDocument().New().Locked(); var sut = _trayMenuItemProvider.BuildMenuItemsForRootDropDown(pwDocument.AsList()).ToList(); sut.Count.Should().Be(1); sut.First().Text.Should().Contain("[" + Localization.Strings.Locked + "]", "because, there is only a locked database."); }
private static ToolStripMenuItem MenuCreateGroup(PwDocument ds, PwGroup pg) { ToolStripMenuItem tsmi = new ToolStripMenuItem(); tsmi.Text = pg.Name; tsmi.ImageIndex = MenuGetImageIndex(ds, pg.IconId, pg.CustomIconUuid); return(tsmi); }
public void BuildMenuItemsForRootDropDown_ShouldReturnEntryMenuItemsNotInRecycleBin() { var pwDocument = new PwDocument().New().WithNonTotpEntries(2).WithTotpEnabledEntries(4).WithDeletedTotpEnabledEntries(2); var sut = _trayMenuItemProvider.BuildMenuItemsForRootDropDown(pwDocument.AsList()).ToList(); sut.Count.Should().Be(4, "because, valid entries in the recycle bin should not show up."); }
public void BuildMenuItemsForRootDropDown_ShouldReturnCorrectInfo_IfThereAreNoTotpEntries() { var pwDocument = new PwDocument().New().WithNonTotpEntries(4); var sut = _trayMenuItemProvider.BuildMenuItemsForRootDropDown(pwDocument.AsList()).ToList(); sut.Count.Should().Be(1); sut.First().Text.Should().Contain(Localization.Strings.NoTOTPEntriesFound, "because, there were no totp entries found in the database."); }
public void BuildMenuItemsForRootDropDown_ShouldCreateDisabledMenuItems_IfTotpSettingsNotValid() { var pwDocument = new PwDocument().New().WithFaultyTotpEnabledEntries(2); var sut = _trayMenuItemProvider.BuildMenuItemsForRootDropDown(pwDocument.AsList()).ToList(); sut.Count.Should().Be(2); sut.Should().OnlyContain(s => !s.Enabled, "because all entries contain invalid settings and can't be used"); }
public void BuildMenuItemsForRootDropDown_ShouldReturnCorrectMenuItem_IfNoDatabaseIsOpened() { var pwDocument = new PwDocument(); var sut = _trayMenuItemProvider.BuildMenuItemsForRootDropDown(pwDocument.AsList()).ToList(); sut.Count.Should().Be(1); sut.First().Text.Should().Be(Localization.Strings.NoDatabaseIsOpened, "because, there is no open database. (KeePass always provides a (new) PwDocument, even if there is no database open."); }
private IEnumerable <ToolStripMenuItem> NoTOTPEntriesFoundMenuItem(PwDocument pwDocument) { return(new[] { new ToolStripMenuItem("[" + Localization.Strings.NoTOTPEntriesFound + "]", Resources.TOTP_Error, OnClickShowDatabase) { Tag = pwDocument } }); }
public static PwDocument WithNonTotpEntries(this PwDocument pwDocument, int count) { for (int i = 0; i < count; i++) { pwDocument.Database.RootGroup.AddEntry( new PwEntry(true, true), true); } return(pwDocument); }
public static PwDocument WithFaultyTotpEnabledEntries(this PwDocument pwDocument, int count) { for (int i = 0; i < count; i++) { pwDocument.Database.RootGroup.AddEntry( new PwEntry(true, true).WithInvalidTotpSettings(), true); } return(pwDocument); }
public static PwDocument WithTotpEnabledEntries(this PwDocument pwDocument, int count, Func <PwEntry, PwEntry> additionalConfigurations) { for (int i = 0; i < count; i++) { var withValidTotpSettings = new PwEntry(true, true).WithValidTotpSettings(); var pwEntry = additionalConfigurations(withValidTotpSettings); pwDocument.Database.RootGroup.AddEntry(pwEntry, true); } return(pwDocument); }
public void BuildMenuItemsForRootDropDown_ShouldReturnEntryMenuItems_WhenRecycleBinNotEnabled() { var pwDocument = new PwDocument().New().WithNonTotpEntries(2).WithTotpEnabledEntries(4).WithDeletedTotpEnabledEntries(2); // Treat recycle bin as a regular folder pwDocument.Database.RecycleBinEnabled = false; var sut = _trayMenuItemProvider.BuildMenuItemsForRootDropDown(pwDocument.AsList()).ToList(); sut.Count.Should().Be(6, "because, the recycle bin is treated as a regular folder."); }
public static PwDocument WithDeletedTotpEnabledEntries(this PwDocument pwDocument, int count) { var recycleBin = pwDocument.Database.RootGroup.FindGroup(pwDocument.Database.RecycleBinUuid, true); for (int i = 0; i < count; i++) { recycleBin.AddEntry( new PwEntry(true, true).WithValidTotpSettings(), true); } return(pwDocument); }
private void ActivateDB(PwEntry pe) { PwDatabase db = m_host.MainWindow.DocumentManager.FindContainerOf(pe); if (db == null) { return; } PwDocument doc = m_host.MainWindow.DocumentManager.FindDocument(db); if (doc == null) { return; } m_host.MainWindow.MakeDocumentActive(doc); }
private static void MenuAddEntry(PwDocument ds, ToolStripMenuItem tsmiContainer, PwEntry pe) { ToolStripMenuItem tsmiEntry = new ToolStripMenuItem(); string strTitle = pe.Strings.ReadSafe(PwDefs.TitleField); string strUser = pe.Strings.ReadSafe(PwDefs.UserNameField); string strText = string.Empty; if ((strTitle.Length > 0) && (strUser.Length > 0)) { strText = strTitle + ": " + strUser; } else if (strTitle.Length > 0) { strText = strTitle; } else if (strUser.Length > 0) { strText = strUser; } tsmiEntry.Text = strText; tsmiEntry.ImageIndex = MenuGetImageIndex(ds, pe.IconId, pe.CustomIconUuid); tsmiContainer.DropDownItems.Add(tsmiEntry); ToolStripMenuItem tsmi; tsmi = new ToolStripMenuItem(KPRes.AutoType); tsmi.ImageIndex = (int)PwIcon.Run; tsmi.Tag = pe; tsmi.Click += OnAutoType; tsmi.Enabled = pe.GetAutoTypeEnabled(); tsmiEntry.DropDownItems.Add(tsmi); tsmiEntry.DropDownItems.Add(new ToolStripSeparator()); tsmi = new ToolStripMenuItem(KPRes.Copy + " " + KPRes.UserName); tsmi.ImageIndex = (int)PwIcon.UserKey; tsmi.Tag = pe; tsmi.Click += OnCopyUserName; tsmiEntry.DropDownItems.Add(tsmi); tsmi = new ToolStripMenuItem(KPRes.Copy + " " + KPRes.Password); tsmi.ImageIndex = (int)PwIcon.Key; tsmi.Tag = pe; tsmi.Click += OnCopyPassword; tsmiEntry.DropDownItems.Add(tsmi); }
private static int MenuGetImageIndex(PwDocument ds, PwIcon pwID, PwUuid pwCustomID) { if (!pwCustomID.Equals(PwUuid.Zero) && (ds == Program.MainForm.DocumentManager.ActiveDocument)) { return((int)PwIcon.Count + Program.MainForm.DocumentManager.ActiveDatabase.GetCustomIconIndex( pwCustomID)); } if ((int)pwID < (int)PwIcon.Count) { return((int)pwID); } return((int)PwIcon.Key); }
private static void MenuProcessGroup(PwDocument ds, ToolStripMenuItem tsmiContainer, PwGroup pgSource) { if ((pgSource.Groups.UCount == 0) && (pgSource.Entries.UCount == 0)) { return; } foreach (PwGroup pg in pgSource.Groups) { ToolStripMenuItem tsmi = MenuCreateGroup(ds, pg); tsmiContainer.DropDownItems.Add(tsmi); MenuProcessGroup(ds, tsmi, pg); } foreach (PwEntry pe in pgSource.Entries) { MenuAddEntry(ds, tsmiContainer, pe); } }
private ToolStripMenuItem CreateDatabaseMenuItemForDocument(PwDocument document) { ToolStripMenuItemEx mainDropDownItem; if (!document.Database.IsOpen) { var documentName = UrlUtil.GetFileName(document.LockedIoc.Path); documentName += " [" + Localization.Strings.Locked + "]"; mainDropDownItem = new ToolStripMenuItemEx(documentName, Resources.TOTP_Error, OnClickOpenDatabase); } else { var documentName = UrlUtil.GetFileName(document.Database.IOConnectionInfo.Path); mainDropDownItem = new ToolStripMenuItemEx(documentName, ImageExtensions.CreateImageFromColor(document.Database.Color)); mainDropDownItem.ForceDropDownArrow = true; mainDropDownItem.DropDownOpening += OnDatabaseDropDownOpening; mainDropDownItem.DropDownOpening += MenuItemHelper.OnDatabaseDropDownOpening; } mainDropDownItem.Tag = document; return(mainDropDownItem); }
public static PwDocument NewAs(this PwDocument pwDocument, string filename) { pwDocument.Database.New(IOConnectionInfo.FromPath(filename), new CompositeKey()); return(pwDocument.CreateRecycleBin()); }
public static PwDocument Locked(this PwDocument pwDocument) { pwDocument.LockedIoc = pwDocument.Database.IOConnectionInfo.CloneDeep(); pwDocument.Database.Close(); return(pwDocument); }
public static PwDocument WithTotpEnabledEntries(this PwDocument pwDocument, int count) { return(WithTotpEnabledEntries(pwDocument, count, entry => entry)); }
public static IEnumerable <PwDocument> AsEnumerable(this PwDocument pwDocument) { yield return(pwDocument); }
private void NavigateToSelectedEntry(ListViewForm dlg, bool CalledFromSearchForm) { PwGroup pg = dlg.ResultGroup as PwGroup; //parent group of selected entry PwEntry pe = dlg.ResultItem as PwEntry; if (pe == null) //try getting the virtual group for the selected entries database { object[] oEntryAndGroup = dlg.ResultItem as object[]; if ((oEntryAndGroup != null) && (oEntryAndGroup.Length == 2)) { pe = oEntryAndGroup[0] as PwEntry; pg = oEntryAndGroup[1] as PwGroup; } } if (pe != null) { ActivateDB(pe); } if ((pg == null) && (pe == null)) { pg = (dlg.ResultItem as PwGroup); } if ((pg != null) || (pe != null)) { if (pg != null) { PwDocument doc = null; if (pe == null) { doc = m_host.MainWindow.DocumentManager.FindDocument(m_host.MainWindow.DocumentManager.SafeFindContainerOf(pg.Entries.GetAt(0))); } else { doc = m_host.MainWindow.DocumentManager.FindDocument(m_host.MainWindow.DocumentManager.SafeFindContainerOf(pe)); } bool bCleanUpDone = false; for (int i = (int)pg.Entries.UCount - 1; i >= 0; i--) { if (doc != m_host.MainWindow.DocumentManager.FindDocument(m_host.MainWindow.DocumentManager.SafeFindContainerOf(pg.Entries.GetAt((uint)i)))) { bCleanUpDone = true; pg.Entries.RemoveAt((uint)i); } } if (bCleanUpDone && !CalledFromSearchForm) { ShowMultiDBInfo(CalledFromSearchForm); } if (pg != null) { pe = pg.Entries.GetAt(0); foreach (KeyValuePair <PwDatabase, PwGroup> kvp in m_dDBGroups) { if (kvp.Value.FindEntry(pe.Uuid, true) != null) { pg = kvp.Value; break; } } } m_host.MainWindow.UpdateUI(false, doc, false, null, true, pg, false, m_lvEntries); } else { PwDocument doc = m_host.MainWindow.DocumentManager.FindDocument(m_host.MainWindow.DocumentManager.SafeFindContainerOf(pe)); m_host.MainWindow.UpdateUI(false, doc, true, pe.ParentGroup, true, null, false, m_lvEntries); } MethodInfo mi = null; if (pe != null) { KeePassLib.Collections.PwObjectList <PwEntry> lSel = new KeePassLib.Collections.PwObjectList <PwEntry>(); lSel.Add(pe); m_host.MainWindow.SelectEntries(lSel, true, true); mi = m_host.MainWindow.GetType().GetMethod("EnsureVisibleSelected", BindingFlags.Instance | BindingFlags.NonPublic); if (mi != null) { mi.Invoke(m_host.MainWindow, new object[] { false }); } } else { mi = m_host.MainWindow.GetType().GetMethod("SelectFirstEntryIfNoneSelected", BindingFlags.Instance | BindingFlags.NonPublic); if (mi != null) { mi.Invoke(m_host.MainWindow, null); } } mi = m_host.MainWindow.GetType().GetMethod("UpdateUIState", BindingFlags.Instance | BindingFlags.NonPublic, null, new Type[] { typeof(bool) }, null); if (mi != null) { mi.Invoke(m_host.MainWindow, new object[] { false }); } } m_dDBGroups.Clear(); }
public static List <PwDocument> AsList(this PwDocument pwDocument) { return(pwDocument.AsEnumerable().ToList()); }
/// <summary> /// Called when [file new]. /// </summary> /// <remarks>Review whenever private KeePass.MainForm.OnFileNew method changes. Last reviewed 20180416</remarks> /// <param name="sender">The sender.</param> /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param> internal void CreateNewDatabase() { if (!AppPolicy.Try(AppPolicyId.SaveFile)) { return; } DialogResult dr; string strPath; using (SaveFileDialog sfd = UIUtil.CreateSaveFileDialog(KPRes.CreateNewDatabase, KPRes.NewDatabaseFileName, UIUtil.CreateFileTypeFilter( AppDefs.FileExtension.FileExt, KPRes.KdbxFiles, true), 1, AppDefs.FileExtension.FileExt, false)) { GlobalWindowManager.AddDialog(sfd); dr = sfd.ShowDialog(_host.MainWindow); GlobalWindowManager.RemoveDialog(sfd); strPath = sfd.FileName; } if (dr != DialogResult.OK) { return; } KeePassLib.Keys.CompositeKey key = null; bool showUsualKeePassKeyCreationDialog = false; using (KeyCreationSimpleForm kcsf = new KeyCreationSimpleForm()) { // Don't show the simple key creation form if the user has set // security policies that restrict the allowable composite key sources if (KeePass.Program.Config.UI.KeyCreationFlags == 0) { kcsf.InitEx(KeePassLib.Serialization.IOConnectionInfo.FromPath(strPath), true); dr = kcsf.ShowDialog(_host.MainWindow); if ((dr == DialogResult.Cancel) || (dr == DialogResult.Abort)) { return; } if (dr == DialogResult.No) { showUsualKeePassKeyCreationDialog = true; } else { key = kcsf.CompositeKey; } } else { showUsualKeePassKeyCreationDialog = true; } if (showUsualKeePassKeyCreationDialog) { using (KeyCreationForm kcf = new KeyCreationForm()) { kcf.InitEx(KeePassLib.Serialization.IOConnectionInfo.FromPath(strPath), true); dr = kcf.ShowDialog(_host.MainWindow); if ((dr == DialogResult.Cancel) || (dr == DialogResult.Abort)) { return; } key = kcf.CompositeKey; } } PwDocument dsPrevActive = _host.MainWindow.DocumentManager.ActiveDocument; PwDatabase pd = _host.MainWindow.DocumentManager.CreateNewDocument(true).Database; pd.New(KeePassLib.Serialization.IOConnectionInfo.FromPath(strPath), key); if (!string.IsNullOrEmpty(kcsf.DatabaseName)) { pd.Name = kcsf.DatabaseName; pd.NameChanged = DateTime.Now; } InsertStandardKeePassData(pd); var conf = pd.GetKPRPCConfig(); pd.SetKPRPCConfig(conf); // save the new database & update UI appearance pd.Save(_host.MainWindow.CreateStatusBarLogger()); } _host.MainWindow.UpdateUI(true, null, true, null, true, null, false); }
public static PwDocument New(this PwDocument pwDocument) { return(pwDocument.NewAs("foobar")); }
protected IEnumerable <ToolStripMenuItem> CreateDatabaseSubMenuItemsFromPwDocument(PwDocument pwDocument) { var validPwEntries = Plugin.GetVisibleAndValidPasswordEntries(pwDocument.Database).ToArray(); if (validPwEntries.Length > 0) { return(validPwEntries .Select(entry => CreateMenuItemFromPwEntry(entry, pwDocument.Database)) .OrderBy(t => t.Text)); } return(NoTOTPEntriesFoundMenuItem(pwDocument)); }
/// <summary> /// Called when [file new]. /// </summary> /// <remarks>Review whenever private KeePass.MainForm.OnFileNew method changes.</remarks> /// <param name="sender">The sender.</param> /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param> internal void CreateNewDatabase() { if (!AppPolicy.Try(AppPolicyId.SaveFile)) { return; } SaveFileDialog sfd = UIUtil.CreateSaveFileDialog(KPRes.CreateNewDatabase, KPRes.NewDatabaseFileName, UIUtil.CreateFileTypeFilter( AppDefs.FileExtension.FileExt, KPRes.KdbxFiles, true), 1, AppDefs.FileExtension.FileExt, false); GlobalWindowManager.AddDialog(sfd); DialogResult dr = sfd.ShowDialog(_host.MainWindow); GlobalWindowManager.RemoveDialog(sfd); string strPath = sfd.FileName; if (dr != DialogResult.OK) { return; } KeePassLib.Keys.CompositeKey key; KeyCreationSimpleForm kcsf = new KeyCreationSimpleForm(); kcsf.InitEx(KeePassLib.Serialization.IOConnectionInfo.FromPath(strPath), true); dr = kcsf.ShowDialog(_host.MainWindow); if ((dr == DialogResult.Cancel) || (dr == DialogResult.Abort)) { return; } if (dr == DialogResult.No) { KeyCreationForm kcf = new KeyCreationForm(); kcf.InitEx(KeePassLib.Serialization.IOConnectionInfo.FromPath(strPath), true); dr = kcf.ShowDialog(_host.MainWindow); if ((dr == DialogResult.Cancel) || (dr == DialogResult.Abort)) { return; } key = kcf.CompositeKey; } else { key = kcsf.CompositeKey; } PwDocument dsPrevActive = _host.MainWindow.DocumentManager.ActiveDocument; PwDatabase pd = _host.MainWindow.DocumentManager.CreateNewDocument(true).Database; pd.New(KeePassLib.Serialization.IOConnectionInfo.FromPath(strPath), key); if (!string.IsNullOrEmpty(kcsf.DatabaseName)) { pd.Name = kcsf.DatabaseName; pd.NameChanged = DateTime.Now; } InsertStandardKeePassData(pd); InstallKeeFoxSampleEntries(pd); _host.MainWindow.UpdateUI(true, null, true, null, true, null, true); }