Esempio n. 1
0
        public static void ReorderEntriesAsInDatabase(PwObjectList <PwEntry> v,
                                                      PwDatabase pd)
        {
            if ((v == null) || (pd == null))
            {
                Debug.Assert(false); return;
            }

            PwObjectList <PwEntry> vRem = v.CloneShallow();

            v.Clear();

            EntryHandler eh = delegate(PwEntry pe)
            {
                int p = vRem.IndexOf(pe);
                if (p >= 0)
                {
                    v.Add(pe);
                    vRem.RemoveAt((uint)p);
                }

                return(true);
            };

            pd.RootGroup.TraverseTree(TraversalMethod.PreOrder, null, eh);

            foreach (PwEntry peRem in vRem)
            {
                v.Add(peRem);                                        // Entries not found
            }
        }
Esempio n. 2
0
		internal void ReorderObjectList<T>(PwObjectList<T> vItems,
			PwObjectPool ppOrgStructure, PwObjectPool ppSrcStructure, bool bEntries)
			where T : class, IStructureItem, IDeepClonable<T>
		{
			if(!ObjectListRequiresReorder<T>(vItems, ppOrgStructure, ppSrcStructure,
				bEntries)) return;

#if DEBUG
			PwObjectList<T> vOrgListItems = vItems.CloneShallow();
#endif

			Queue<KeyValuePair<uint, uint>> qToDo = new Queue<KeyValuePair<uint, uint>>();
			qToDo.Enqueue(new KeyValuePair<uint, uint>(0, vItems.UCount - 1));

			while(qToDo.Count > 0)
			{
				if((m_slStatus != null) && !m_slStatus.ContinueWork()) break;

				KeyValuePair<uint, uint> kvp = qToDo.Dequeue();
				if(kvp.Value <= kvp.Key) { Debug.Assert(false); continue; }

				Queue<PwUuid> qRelBefore = new Queue<PwUuid>();
				Queue<PwUuid> qRelAfter = new Queue<PwUuid>();
				uint uPivot = FindLocationChangedPivot<T>(vItems, kvp, ppOrgStructure,
					ppSrcStructure, qRelBefore, qRelAfter, bEntries);
				T ptPivot = vItems.GetAt(uPivot);

				List<T> vToSort = vItems.GetRange(kvp.Key, kvp.Value);
				Queue<T> qBefore = new Queue<T>();
				Queue<T> qAfter = new Queue<T>();
				bool bBefore = true;

				foreach(T pt in vToSort)
				{
					if(pt == ptPivot) { bBefore = false; continue; }

					bool bAdded = false;
					foreach(PwUuid puBefore in qRelBefore)
					{
						if(puBefore.EqualsValue(pt.Uuid))
						{
							qBefore.Enqueue(pt);
							bAdded = true;
							break;
						}
					}
					if(bAdded) continue;

					foreach(PwUuid puAfter in qRelAfter)
					{
						if(puAfter.EqualsValue(pt.Uuid))
						{
							qAfter.Enqueue(pt);
							bAdded = true;
							break;
						}
					}
					if(bAdded) continue;

					if(bBefore) qBefore.Enqueue(pt);
					else qAfter.Enqueue(pt);
				}
				Debug.Assert(bBefore == false);

				uint uPos = kvp.Key;
				while(qBefore.Count > 0) vItems.SetAt(uPos++, qBefore.Dequeue());
				vItems.SetAt(uPos++, ptPivot);
				while(qAfter.Count > 0) vItems.SetAt(uPos++, qAfter.Dequeue());
				Debug.Assert(uPos == (kvp.Value + 1));

				int iNewPivot = vItems.IndexOf(ptPivot);
				if((iNewPivot < (int)kvp.Key) || (iNewPivot > (int)kvp.Value))
				{
					Debug.Assert(false);
					continue;
				}

				if((iNewPivot - 1) > (int)kvp.Key)
					qToDo.Enqueue(new KeyValuePair<uint, uint>(kvp.Key,
						(uint)(iNewPivot - 1)));

				if((iNewPivot + 1) < (int)kvp.Value)
					qToDo.Enqueue(new KeyValuePair<uint, uint>((uint)(iNewPivot + 1),
						kvp.Value));
			}

#if DEBUG
			foreach(T ptItem in vOrgListItems)
			{
				Debug.Assert(vItems.IndexOf(ptItem) >= 0);
			}
#endif
		}
Esempio n. 3
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);
        }