コード例 #1
0
        /// <summary>
        /// Show the configuration form
        /// </summary>
        private bool AskForConfiguration()
        {
            if (!m_host.Database.IsOpen)
            {
                return(false);
            }

            // find google accounts
            SearchParameters sp = new SearchParameters();

            sp.SearchString   = Defs.AccountSearchString;
            sp.ComparisonMode = StringComparison.OrdinalIgnoreCase;
            sp.RespectEntrySearchingDisabled = false;
            sp.SearchInGroupNames            = false;
            sp.SearchInNotes     = false;
            sp.SearchInOther     = false;
            sp.SearchInPasswords = false;
            sp.SearchInTags      = false;
            sp.SearchInTitles    = true;
            sp.SearchInUrls      = true;
            sp.SearchInUserNames = false;
            sp.SearchInUuids     = false;
            PwObjectList <PwEntry> accounts = new PwObjectList <PwEntry>();

            m_host.Database.RootGroup.SearchEntries(sp, accounts);

            // find the active account
            string  strUuid = null;
            PwEntry entry   = null;
            PwObjectList <PwEntry> activeAccounts = FindActiveAccounts();

            if (activeAccounts != null && activeAccounts.UCount == 1)
            {
                entry = activeAccounts.GetAt(0);
            }
            else
            {
                // alternatively try to find the active account in the config file (old configuration) (may be removed in later versions)
                strUuid = m_host.CustomConfig.GetString(Defs.ConfigUUID);
                try
                {
                    entry = m_host.Database.RootGroup.FindEntry(new PwUuid(KeePassLib.Utility.MemUtil.HexStringToByteArray(strUuid)), true);
                }
                catch (ArgumentException) { }
            }

            // find configured entry in account list
            int idx = -1;

            if (entry != null)
            {
                idx = accounts.IndexOf(entry);
                // add configured entry to account list if not already present
                if (idx < 0)
                {
                    accounts.Insert(0, entry);
                    idx = 0;
                }
            }

            ConfigurationForm form1 = new ConfigurationForm(accounts, idx, m_autoSync);

            if (DialogResult.OK != UIUtil.ShowDialogAndDestroy(form1))
            {
                return(false);
            }

            entry   = null;
            strUuid = form1.Uuid;
            try
            {
                // will throw ArgumentException when Uuid is empty and association shall be removed
                entry = m_host.Database.RootGroup.FindEntry(new PwUuid(KeePassLib.Utility.MemUtil.HexStringToByteArray(strUuid)), true);
            }
            catch (ArgumentException) { }

            if (entry == null && !String.IsNullOrEmpty(strUuid))
            {
                ShowMessage("没有找到 UUID:'" + strUuid + "' 关联密码");
                return(false);
            }

            m_entry        = entry;
            m_clientId     = form1.ClientId;
            m_clientSecret = new ProtectedString(true, form1.ClientSecrect);
            m_refreshToken = (m_entry != null) ? m_entry.Strings.Get(Defs.ConfigRefreshToken) : null;
            m_autoSync     = form1.AutoSync;

            return(true);
        }