Пример #1
0
        /// <summary>
        /// Search this group and all groups in the current one for entries.
        /// </summary>
        /// <param name="searchParams">Specifies the search method.</param>
        /// <param name="listStorage">Entry list in which the search results will
        /// be stored.</param>
        public void SearchEntries(SearchParameters searchParams, PwObjectList <PwEntry> listStorage,
                                  bool bRespectEntrySearchingDisabled)
        {
            Debug.Assert(searchParams != null); if (searchParams == null)
            {
                throw new ArgumentNullException("searchParams");
            }
            Debug.Assert(listStorage != null); if (listStorage == null)
            {
                throw new ArgumentNullException("listStorage");
            }

            string strSearch = searchParams.SearchString;

            Debug.Assert(strSearch != null); if (strSearch == null)
            {
                throw new ArgumentException();
            }

            StringComparison scType = searchParams.ComparisonMode;
            Regex            rx     = null;
            EntryHandler     eh     = null;

            bool bTitle          = searchParams.SearchInTitles;
            bool bUserName       = searchParams.SearchInUserNames;
            bool bPassword       = searchParams.SearchInPasswords;
            bool bUrl            = searchParams.SearchInUrls;
            bool bNotes          = searchParams.SearchInNotes;
            bool bOther          = searchParams.SearchInOther;
            bool bUuids          = searchParams.SearchInUuids;
            bool bGroupName      = searchParams.SearchInGroupNames;
            bool bExcludeExpired = searchParams.ExcludeExpired;

            DateTime dtNow = DateTime.Now;

            if (searchParams.RegularExpression)
            {
                RegexOptions ro = RegexOptions.Compiled;
                if ((scType == StringComparison.CurrentCultureIgnoreCase) ||
                    (scType == StringComparison.InvariantCultureIgnoreCase) ||
                    (scType == StringComparison.OrdinalIgnoreCase))
                {
                    ro |= RegexOptions.IgnoreCase;
                }

                rx = new Regex(strSearch, ro);
            }

            if (strSearch.Length <= 0)            // Report all
            {
                eh = delegate(PwEntry pe)
                {
                    if (bRespectEntrySearchingDisabled && !pe.GetSearchingEnabled())
                    {
                        return(true);                        // Skip
                    }
                    if (bExcludeExpired && pe.Expires && (dtNow > pe.ExpiryTime))
                    {
                        return(true);                        // Skip
                    }
                    listStorage.Add(pe);
                    return(true);
                };
            }
            else
            {
                eh = delegate(PwEntry pe)
                {
                    if (bRespectEntrySearchingDisabled && !pe.GetSearchingEnabled())
                    {
                        return(true);                        // Skip
                    }
                    if (bExcludeExpired && pe.Expires && (dtNow > pe.ExpiryTime))
                    {
                        return(true);                        // Skip
                    }
                    uint uInitialResults = listStorage.UCount;

                    foreach (KeyValuePair <string, ProtectedString> kvp in pe.Strings)
                    {
                        string strKey = kvp.Key;

                        if (strKey == PwDefs.TitleField)
                        {
                            if (bTitle)
                            {
                                SearchEvalAdd(strSearch, kvp.Value.ReadString(),
                                              scType, rx, pe, listStorage);
                            }
                        }
                        else if (strKey == PwDefs.UserNameField)
                        {
                            if (bUserName)
                            {
                                SearchEvalAdd(strSearch, kvp.Value.ReadString(),
                                              scType, rx, pe, listStorage);
                            }
                        }
                        else if (strKey == PwDefs.PasswordField)
                        {
                            if (bPassword)
                            {
                                SearchEvalAdd(strSearch, kvp.Value.ReadString(),
                                              scType, rx, pe, listStorage);
                            }
                        }
                        else if (strKey == PwDefs.UrlField)
                        {
                            if (bUrl)
                            {
                                SearchEvalAdd(strSearch, kvp.Value.ReadString(),
                                              scType, rx, pe, listStorage);
                            }
                        }
                        else if (strKey == PwDefs.NotesField)
                        {
                            if (bNotes)
                            {
                                SearchEvalAdd(strSearch, kvp.Value.ReadString(),
                                              scType, rx, pe, listStorage);
                            }
                        }
                        else if (bOther)
                        {
                            SearchEvalAdd(strSearch, kvp.Value.ReadString(),
                                          scType, rx, pe, listStorage);
                        }

                        // An entry can match only once => break if we have added it
                        if (listStorage.UCount > uInitialResults)
                        {
                            break;
                        }
                    }

                    if (bUuids && (listStorage.UCount == uInitialResults))
                    {
                        SearchEvalAdd(strSearch, pe.Uuid.ToHexString(),
                                      scType, rx, pe, listStorage);
                    }

                    if (bGroupName && (listStorage.UCount == uInitialResults) &&
                        (pe.ParentGroup != null))
                    {
                        SearchEvalAdd(strSearch, pe.ParentGroup.Name,
                                      scType, rx, pe, listStorage);
                    }

                    return(true);
                };
            }

            PreOrderTraverseTree(null, eh);
        }