Menu

Plugin Development Request: make MainWindow.SelectEntries() public

2018-11-27
2018-11-29
  • Mirco Babin

    Mirco Babin - 2018-11-27

    Dear KeePass developers,

    Can the MainWindow.SelectEntries() function be made public ?

    Developing a KeePass Plugin I encountered the problem that I can get the selected entry via MainWindow.GetSelectedEntry(). But that I can't reselect this entry later, there is no public function for it. At the moment I hack my way around this (I copied this from Plugin AutoTypeShow ) via Reflection:

    :::csharp
    public class MyPluginExt : Plugin
    {
        /// <summary>
        /// Perform MainWindow.SelectEntries (through reflection)
        /// </summary>
        private MethodInfo _selectEntriesMethod = null;
        private void SelectEntries(PwObjectList<PwEntry> lEntries, bool bDeselectOthers, bool bFocusFirst)
        {
            if (_selectEntriesMethod == null)
            {
                var mainWindowType = KeePassHost.MainWindow.GetType();
                _selectEntriesMethod = mainWindowType.GetMethod("SelectEntries", BindingFlags.Instance | BindingFlags.NonPublic);
            }
    
            if (_selectEntriesMethod != null)
            {
                _selectEntriesMethod.Invoke(KeePassHost.MainWindow, new object[] { lEntries, bDeselectOthers, bFocusFirst });
            }
        }
    
        private void RefreshUI(PwGroup SelectGroup, PwEntry SelectEntry)
        {
            KeePassHost.MainWindow.UpdateUI(true, null,
            true, SelectGroup,
            true, null,
            false);
    
            if (SelectEntry != null)
            {
                SelectEntries(new PwObjectList<PwEntry> { SelectEntry }, true, true);
                KeePassHost.MainWindow.EnsureVisibleEntry(SelectEntry.Uuid);
            }
        }
    
        //Example code of using
        private void Example()
        {
            int docindex = -1;
            PwUuid docuuidgroup = null;
            PwUuid docuuidentry = null;
            try
            {
                docindex = KeePassHost.MainWindow.DocumentManager.Documents.IndexOf(KeePassHost.MainWindow.DocumentManager.ActiveDocument);
    
                PwGroup docgroup = KeePassHost.MainWindow.GetSelectedGroup();
                if (docgroup != null)
                    docuuidgroup = new PwUuid(docgroup.Uuid.UuidBytes);
    
                PwEntry docentry = KeePassHost.MainWindow.GetSelectedEntry(false);
                if (docentry != null)
                    docuuidentry = new PwUuid(docentry.Uuid.UuidBytes);
            }
            catch { }
    
            // Do something. 
            // In my case, synchronizing and reopening the database.
            // After this I want the same document, group and entry be selected.
    
            PwGroup selectGroup = null;
            PwEntry selectEntry = null;
            if (reloadeddocument != null)
            {
                if (docindex >= 0)
                    KeePassHost.MainWindow.DocumentManager.Documents.Insert(docindex, reloadeddocument);
                else
                    KeePassHost.MainWindow.DocumentManager.Documents.Add(reloadeddocument);
    
                KeePassHost.MainWindow.DocumentManager.ActiveDocument = reloadeddocument;
    
                if (docuuidgroup != null)
                    selectGroup = openteamkdbx.document.Database.RootGroup.FindGroup(docuuidgroup, true);
    
                if (docuuidentry != null)
                    selectEntry = openteamkdbx.document.Database.RootGroup.FindEntry(docuuidentry, true);
            }
    
            RefreshUI(selectGroup, selectEntry);
            try { Application.OpenForms[KeePassHost.MainWindow.Name].Activate(); } catch { }
        }
    }
    

    While the Reflection hack works, it's kind of very ugly. And can break at runtime (not compiletime) when MainWindow.SelectEntries function signature changes. Can this function please be made public ?

    Thanks,
    Mirco Babin (The Netherlands)

     
  • Dominik Reichl

    Dominik Reichl - 2018-11-27

    Ok, I've made it public.

    Here's the latest development snapshot for testing:
    https://keepass.info/filepool/KeePass_181127.zip

    Thanks and best regards,
    Dominik

     
  • Mirco Babin

    Mirco Babin - 2018-11-29

    Thank you very much. Works as expected.

    Kind regards,
    Mirco

     

Log in to post a comment.

MongoDB Logo MongoDB