Beispiel #1
0
        private void ListViewFormAdded(ListViewForm f)
        {
            var lv = f.Controls.OfType <ListView>().ToList().FirstOrDefault();

            if (lv == null)
            {
                return;
            }
            lv.RS_Sortable(true);
            f.Shown += OnShowListviewForm;
        }
Beispiel #2
0
 public static void ShowReadOnlyListViewForm(IList columns, IList items, Control parentControl)
 {
     if (items.Count > 0)
     {
         ListViewForm            popup           = new ListViewForm();
         ListViewPopupController popupController = new ListViewPopupController(popup, columns, items);
         popupController.LoadView();
         DialogResult dialogResult = popup.ShowDialog(parentControl);
         popup.Dispose();
     }
     else
     {
         MessageBox.Show("No records found.");
     }
 }
Beispiel #3
0
        private static string FindUI(MainForm mf, FsxMatchFn fMatch, string strRootPath)
        {
            if (mf == null)
            {
                Debug.Assert(false); return(null);
            }
            if (fMatch == null)
            {
                Debug.Assert(false); return(null);
            }

            Form          fOptDialog;
            IStatusLogger sl = StatusUtil.CreateStatusDialog(mf, out fOptDialog,
                                                             null, GetSearchingText(null), true, true);

            mf.UIBlockInteraction(true);

            List <FsxContext> lContexts = new List <FsxContext>();
            string            strExcp   = null;

            try { lContexts = Find(fMatch, sl, strRootPath); }
            catch (Exception ex) { strExcp = ex.Message; }

            bool bAborted = !sl.ContinueWork();

            mf.UIBlockInteraction(false);
            sl.EndLogging();

            if (!string.IsNullOrEmpty(strExcp))
            {
                MessageService.ShowWarning(strExcp);
                return(null);
            }

            Action <ListView> fInit = delegate(ListView lv)
            {
                int w  = lv.ClientSize.Width - UIUtil.GetVScrollBarWidth();
                int ws = w / 70;
                lv.Columns.Add(KPRes.File, w / 5);
                lv.Columns.Add(KPRes.Folder, (int)(((long)w * 2L) / 5L - (ws * 3)));
                lv.Columns.Add(KPRes.Size, w / 10 + ws, HorizontalAlignment.Right);
                lv.Columns.Add(KPRes.Type, w / 10 + ws);
                lv.Columns.Add(KPRes.LastModified, w / 5 + ws);
            };

            List <object> lItems = new List <object>();
            int           cFiles = 0;

            foreach (FsxContext ctx in lContexts)
            {
                List <FsxResult> lResults = ctx.GetResults();
                if (lResults.Count == 0)
                {
                    continue;
                }

                string strGroup = UrlUtil.EnsureTerminatingSeparator(ctx.RootDirectory, false);
                if (ctx.VolumeLabel.Length != 0)
                {
                    strGroup += " (" + ctx.VolumeLabel + ")";
                }
                lItems.Add(new ListViewGroup(strGroup));

                foreach (FsxResult r in lResults)
                {
                    try
                    {
                        FileInfo fi = new FileInfo(r.Path);

                        ListViewItem lvi = new ListViewItem(UrlUtil.GetFileName(r.Path));
                        lvi.SubItems.Add(UrlUtil.GetFileDirectory(r.Path, true, false));
                        lvi.SubItems.Add(StrUtil.FormatDataSizeKB((ulong)fi.Length));
                        lvi.SubItems.Add(r.Type);
                        lvi.SubItems.Add(TimeUtil.ToDisplayString(
                                             TimeUtil.ToLocal(fi.LastWriteTimeUtc, false)));
                        lvi.Tag = r.Path;

                        lItems.Add(lvi);
                        ++cFiles;
                    }
                    catch (Exception) { Debug.Assert(false); }
                }
            }

            string strSub = KPRes.ObjectsFound.Replace("{PARAM}", cFiles.ToString()) + ".";

            if (bAborted)
            {
                strSub += " " + (new OperationCanceledException()).Message;
            }

            ListViewForm dlg = new ListViewForm();

            dlg.InitEx(KPRes.SearchGroupName, strSub, null,
                       Properties.Resources.B48x48_XMag, lItems, null, fInit);
            UIUtil.ShowDialogAndDestroy(dlg, mf);

            return(dlg.ResultItem as string);
        }
Beispiel #4
0
        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();
        }
Beispiel #5
0
        private void OnClickFindEntry(object sender, EventArgs e)
        {
            string   f  = (sender as ToolStripItem).Name;
            FindInfo fi = SearchHelp.FindList.Find(x => x.Name == (sender as ToolStripItem).Name);

            if (CallStandardSearch(fi, (sender as ToolStripItem).Name))
            {
                if (fi != null)
                {
                    foreach (Delegate d in fi.StandardEventHandlers)
                    {
                        d.DynamicInvoke(new object[] { sender, e });
                    }
                }
                return;
            }

            PluginDebug.AddInfo("Call own find routine", 0, "Action: " + f);
            //Show status logger
            Form          fOptDialog = null;
            IStatusLogger sl         = StatusUtil.CreateStatusDialog(m_host.MainWindow, out fOptDialog, null,
                                                                     (KPRes.SearchingOp ?? "..."), true, false);

            m_host.MainWindow.UIBlockInteraction(true);

            m_aStandardLvInit = null;

            //Perform find for all open databases
            PwDatabase    dbAll = MergeDatabases();
            List <object> l     = null;

            try
            {
                object[] parameters;
                if (fi.SearchType != SearchType.BuiltIn)
                {
                    parameters = new object[] { dbAll, sl, null, fi }
                }
                ;
                else
                {
                    parameters = new object[] { dbAll, sl, null }
                };

                l = (List <object>)fi.StandardMethod.Invoke(m_host, parameters);
                m_aStandardLvInit = (Action <ListView>)parameters[2];
            }
            catch (Exception ex)
            {
                l = null;
                PluginDebug.AddError("Call standard find routine", 0, "Action: " + f, "Reason for standard call: " + ex.Message);
                foreach (Delegate d in fi.StandardEventHandlers)
                {
                    d.DynamicInvoke(new object[] { sender, e });
                }
            }
            finally { dbAll.Close(); }

            m_host.MainWindow.UIBlockInteraction(false);
            sl.EndLogging();

            if (l == null)
            {
                return;
            }

            //Fill db column
            ImageList il  = new ImageList();
            ImageList il2 = (ImageList)Tools.GetField("m_ilCurrentIcons", m_host.MainWindow);

            foreach (Image img in il2.Images)
            {
                il.Images.Add(img);
            }

            foreach (var o in l)
            {
                ListViewItem lvi = o as ListViewItem;
                if (lvi == null)
                {
                    continue;
                }
                ListViewItem.ListViewSubItem lvsi = new ListViewItem.ListViewSubItem();
                if (lvi.Tag is PwEntry)
                {
                    lvsi.Text = SearchHelp.GetDBName(lvi.Tag as PwEntry);
                    PwEntry    pe = lvi.Tag as PwEntry;
                    PwDatabase db = m_host.MainWindow.DocumentManager.FindContainerOf(pe);
                    if (!pe.CustomIconUuid.Equals(PwUuid.Zero))
                    {
                        il.Images.Add(db.GetCustomIcon(pe.CustomIconUuid, DpiUtil.ScaleIntX(16), DpiUtil.ScaleIntY(16)));
                        lvi.ImageIndex = il.Images.Count - 1;
                    }
                    else
                    {
                        lvi.ImageIndex = (int)pe.IconId;
                    }
                }
                else if (lvi.Tag is PwGroup)
                {
                    PwGroup pg = lvi.Tag as PwGroup;
                    lvsi.Text = SearchHelp.GetDBName(pg.Entries.GetAt(0));
                }
                lvi.SubItems.Insert(0, lvsi);
            }

            if ((l.Count == 0) && !string.IsNullOrEmpty(fi.NothingFound))
            {
                Tools.ShowInfo(fi.NothingFound);
                il.Dispose();
                return;
            }

            //Show results
            ListViewForm dlg = new ListViewForm();

            //Prepare ImageList (CustomIcons can be different per database)
            dlg.InitEx(fi.Title, fi.SubTitle, fi.Note, fi.img, l, il, InitListView);
            UIUtil.ShowDialogAndDestroy(dlg);
            if (dlg.DialogResult != DialogResult.OK)
            {
                return;
            }
            il.Dispose();
            NavigateToSelectedEntry(dlg, false);
        }
Beispiel #6
0
        private void OnSearchExecute(object sender, EventArgs e)
        {
            //Perform search in all open databases
            m_dDBGroups = new Dictionary <PwDatabase, PwGroup>();
            PwGroup           g       = null;
            List <PwDatabase> lOpenDB = m_host.MainWindow.DocumentManager.GetOpenDatabases();

            m_btnOK.Click -= OnSearchExecute;
            List <string> lMsg = new List <string>();

            foreach (PwDatabase db in lOpenDB)
            {
                lMsg.Clear();
                lMsg.Add("DB: " + db.IOConnectionInfo.Path);
                if ((m_sf != null) && (m_sf.SearchResultsGroup != null) && (m_sf.SearchResultsGroup.Entries != null))
                {
                    lMsg.Add("Previos search results cleared: " + true.ToString());
                    m_sf.SearchResultsGroup.Entries.Clear();
                }
                m_sf.InitEx(db, db.RootGroup);
                FindInfo fi = SearchHelp.FindList.Find(x => x.Name == SearchHelp.SearchForm);
                if (fi.StandardEventHandlers.Count > 0)
                {
                    using (MonoWorkaroundDialogResult mwaDR = new MonoWorkaroundDialogResult(sender))
                    {
                        foreach (Delegate onclick in fi.StandardEventHandlers)
                        {
                            lMsg.Add("Calling method: " + onclick.Method.Name + " - " + onclick.Method.ReflectedType.Name);
                            onclick.DynamicInvoke(new object[] { sender, e });
                        }
                    }
                }
                else
                {
                    lMsg.Add("Calling standard method");
                    m_btnOK.PerformClick();
                }
                if ((m_sf.SearchResultsGroup == null) || (m_sf.SearchResultsGroup.Entries == null))
                {
                    lMsg.Add("Found entries: 0");
                }
                else
                {
                    lMsg.Add("Found entries: " + m_sf.SearchResultsGroup.Entries.UCount.ToString());
                }

                //Do NOT use m_sf.SearchResultsGroup.CloneDeep
                //It makes the virtual SearchResultsGroup the
                //parent group of the found entries
                if (g == null)
                {
                    g = new PwGroup(true, true, m_sf.SearchResultsGroup.Name, m_sf.SearchResultsGroup.IconId);
                }
                foreach (PwEntry pe in m_sf.SearchResultsGroup.Entries)
                {
                    g.AddEntry(pe, false);
                }
                PluginDebug.AddInfo("Executing search", 0, lMsg.ToArray());
            }

            //Don't continue if not even a single entry was found
            if ((g == null) || (g.GetEntriesCount(true) == 0))
            {
                if (m_sf.DialogResult == DialogResult.None)
                {
                    m_sf.DialogResult = DialogResult.OK;
                }
                return;
            }
            //Prepare ImageList (CustomIcons can be different per database)
            ImageList il  = new ImageList();
            ImageList il2 = (ImageList)Tools.GetField("m_ilCurrentIcons", m_host.MainWindow);

            foreach (Image img in il2.Images)
            {
                il.Images.Add(img);
            }
            Dictionary <PwEntry, int> dEntryIconIndex = new Dictionary <PwEntry, int>();
            PwDatabase dbFirst     = null;
            bool       bMultipleDB = false;

            foreach (PwEntry pe in g.Entries)
            {
                PwDatabase db = m_host.MainWindow.DocumentManager.FindContainerOf(pe);
                if (db == null)
                {
                    PluginDebug.AddError("Could not get database for entry", 0, pe.Uuid.ToHexString());
                    continue;
                }
                if (!m_dDBGroups.ContainsKey(db))
                {
                    m_dDBGroups[db] = new PwGroup(true, false, SearchHelp.GetDBName(pe), PwIcon.Folder)
                    {
                        IsVirtual = true
                    }
                }
                ;
                m_dDBGroups[db].AddEntry(pe, false);
                if (dbFirst == null)
                {
                    dbFirst = db;
                }
                bMultipleDB |= db != dbFirst;
                if (!pe.CustomIconUuid.Equals(PwUuid.Zero))
                {
                    il.Images.Add(db.GetCustomIcon(pe.CustomIconUuid, DpiUtil.ScaleIntX(16), DpiUtil.ScaleIntY(16)));
                    dEntryIconIndex[pe] = dEntryIconIndex.Count - 1;
                }
                else
                {
                    dEntryIconIndex[pe] = (int)pe.IconId;
                }
            }

            //If all found entries are contained in the same database
            //simply activate this database (might not be active yet) and return
            if (!bMultipleDB)
            {
                PwGroup pgSF = (PwGroup)Tools.GetField("m_pgResultsGroup", m_sf);
                if (pgSF != null)
                {
                    //clear list of found entries
                    //otherwise duplicates might be shown if last searched db is the only one that is to be shown
                    pgSF.Entries.Clear();
                    pgSF.Entries.Add(g.Entries);
                }
                else                 //KeePass 2.47
                {
                    pgSF           = new PwGroup(true, true, g.Name, g.IconId);
                    pgSF.IsVirtual = true;
                    pgSF.Entries.Add(g.Entries);
                }
                PluginDebug.AddInfo("Found " + pgSF.Entries.UCount.ToString() + " entries in 1 database");
                m_host.MainWindow.UpdateUI(false, m_host.MainWindow.DocumentManager.FindDocument(dbFirst), true, pgSF, false, null, false);
                il.Dispose();
                m_sf.SearchResultsGroup.Entries.Clear();
                m_sf.SearchResultsGroup.Entries.Add(pgSF.Entries);
                m_sf.DialogResult = DialogResult.OK;
                return;
            }

            //We found entries from at least 2 databases
            //Show the results in ListViewForm and close SearchForm
            try
            {
                PluginDebug.AddInfo("Found " + g.Entries.UCount.ToString() + " entries in multiple database");
                m_sf.DialogResult = DialogResult.Abort;
                m_sf.Visible      = false;
                m_sf.Close();
            }
            catch (Exception ex)
            {
                PluginDebug.AddError("Error closing searchform", new string[] { ex.Message });
            }

            m_aStandardLvInit = InitListViewMain;
            List <object> l = GetFoundEntriesList(g, dEntryIconIndex);

            ListViewForm dlg       = new ListViewForm();
            int          iCount    = l.FindAll(x => (x as ListViewItem) != null).Count;
            string       sSubTitle = iCount == 1 ? KPRes.SearchEntriesFound1 : KPRes.SearchEntriesFound;

            sSubTitle = sSubTitle.Replace("{PARAM}", iCount.ToString());
            dlg.InitEx(KPRes.Search, sSubTitle, null, null, l, il, InitListView);
            ShowMultiDBInfo(true);
            PluginDebug.AddInfo("Multi-DB results: Show", 0);
            if (dlg.ShowDialog(m_host.MainWindow) != DialogResult.OK)
            {
                PluginDebug.AddInfo("Multi-DB results: Shown", 0);
                UIUtil.DestroyForm(dlg);
                return;
            }
            PluginDebug.AddInfo("Multi-DB results: Show and navigate", 0);
            il.Dispose();
            NavigateToSelectedEntry(dlg, true);
            PluginDebug.AddInfo("Multi-DB results: Dispose form", 0);
            UIUtil.DestroyForm(dlg);
            PluginDebug.AddInfo("Multi-DB results: Disposed form", 0);
        }