Source

KeeCloud / KeeCloud / Utilities / DatabaseHelpers.cs

using KeePassLib;
using System;
using System.Collections.Generic;
using System.Linq;

namespace KeeCloud.Utilities
{
    public static class DatabaseHelpers
    {
        public static IEnumerable<PwGroup> GetAllGroups(this PwDatabase database)
        {
            return Flatten<PwGroup>(database.RootGroup, _ => _.Groups);
        }

        public static IEnumerable<PwEntry> GetAllPasswords(this PwDatabase database)
        {
            return from g in database.GetAllGroups()
                   from pe in g.Entries
                   select pe;
        }

        public static IEnumerable<T> Flatten<T>(T node, Func<T, IEnumerable<T>> getSubEnumerable)
        {
            yield return node;
            if (getSubEnumerable != null)
            {
                foreach (var subNode in getSubEnumerable(node))
                    yield return subNode;
            }
        }

        public static bool EntryStringEquals(this PwEntry entry, string key, string expectedValue)
        {
            return entry.EntryStringExistsAndContidtionMet(key, value =>
            {
                return value.Equals(expectedValue, StringComparison.InvariantCultureIgnoreCase);
            });
        }

        public static bool EntryStringNotNullOrEmpty(this PwEntry entry, string key)
        {
            return entry.EntryStringExistsAndContidtionMet(key, value => !string.IsNullOrEmpty(value));
        }

        public static bool EntryStringExistsAndContidtionMet(this PwEntry entry, string key, Func<string, bool> expectation)
        {
            if (expectation == null)
                return false;
            else
            {
                var value = entry.Strings.Get(key);
                if (value != null && !value.IsEmpty)
                {
                    var rawValue = value.ReadString();
                    return expectation(rawValue);
                }
                else
                    return false;
            }
        }
    }
}
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.