Пример #1
0
        /// <summary>
        /// Delete an entry.
        /// </summary>
        /// <param name="pe">The entry to be deleted. Must not be <c>null</c>.</param>
        /// <param name="permanent">Permanent delete or move to recycle bin</param>
        public void DeleteEntry(PwEntry pe, bool permanent = false)
        {
            if (pe == null)
            {
                throw new ArgumentNullException("pe");
            }

            PwGroup pgRecycleBin = RootGroup.FindGroup(RecycleBinUuid, true);

            PwGroup pgParent = pe.ParentGroup;

            if (pgParent == null)
            {
                return;                               // Can't remove
            }
            pgParent.Entries.Remove(pe);

            bool bPermanent = false;

            if (RecycleBinEnabled == false)
            {
                bPermanent = true;
            }
            else if (permanent)
            {
                bPermanent = true;
            }
            else if (pgRecycleBin == null)
            {
            }                                              // if we cannot find it, we will create it later
            else if (pgParent == pgRecycleBin)
            {
                bPermanent = true;
            }
            else if (pgParent.IsContainedIn(pgRecycleBin))
            {
                bPermanent = true;
            }

            DateTime dtNow = DateTime.UtcNow;

            if (bPermanent)
            {
                PwDeletedObject pdo = new PwDeletedObject(pe.Uuid, dtNow);
                DeletedObjects.Add(pdo);
            }
            else             // Recycle
            {
                EnsureRecycleBin(ref pgRecycleBin);

                pgRecycleBin.AddEntry(pe, true, true);
                pe.Touch(false);
            }
        }
Пример #2
0
        public static void DeleteFrom(this PwGroup group, PwGroup parent, PwDatabase database)
        {
            //first remove all entries in the group
            group.DeleteAllObjects(database);
            // create the delete marker
            PwDeletedObject deleteMarker = new PwDeletedObject(group.Uuid, DateTime.Now);

            database.DeletedObjects.Add(deleteMarker);
            //now we can remove the empty group
            parent.Groups.Remove(group);
        }
Пример #3
0
        public static void DeleteFrom(this PwEntry entry, PwGroup parent, PwDatabase database = null)
        {
            if (database != null)
            {
                // create the delete marker
                PwDeletedObject deleteMarker = new PwDeletedObject(entry.Uuid, DateTime.Now);
                database.DeletedObjects.Add(deleteMarker);
            }
            // TODO CK: Potential memory leak? are there dangling references?
            var success = parent.Entries.Remove(entry);

            Debug.Assert(success);
        }
Пример #4
0
        private void WriteObject(string name, PwDeletedObject value)
        {
            Debug.Assert(name != null);
            Debug.Assert(value != null); if (value == null)
            {
                throw new ArgumentNullException("value");
            }

            m_xmlWriter.WriteStartElement(name);
            WriteObject(ElemUuid, value.Uuid);
            WriteObject(ElemDeletionTime, value.DeletionTime);
            m_xmlWriter.WriteEndElement();
        }
Пример #5
0
 private static void DeleteEntry(PwEntry entryToDelete, PwDatabase dB)
 {
     try
     {
         if (entryToDelete != null)
         {
             var parentGroup = entryToDelete.ParentGroup;
             parentGroup.Entries.Remove(entryToDelete);
             var pdo = new PwDeletedObject(entryToDelete.Uuid, DateTime.Now);
             dB.DeletedObjects.Add(pdo);
         }
     }
     catch (Exception e) { MessageService.ShowInfo(e.Message); }
 }
Пример #6
0
        private static void AddDeletedObject(PwDatabase pd, PwUuid pu)
        {
            foreach (PwDeletedObject pdo in pd.DeletedObjects)
            {
                if (pdo.Uuid.Equals(pu))
                {
                    Debug.Assert(false); return;
                }
            }

            PwDeletedObject pdoNew = new PwDeletedObject(pu, DateTime.UtcNow);

            pd.DeletedObjects.Add(pdoNew);
        }
Пример #7
0
        protected bool DoDeleteGroup(PwGroup pg, List <PwGroup> touchedGroups, List <PwGroup> permanentlyDeletedGroups)
        {
            PwGroup pgParent = pg.ParentGroup;

            if (pgParent == null)
            {
                return(false);
            }

            PwDatabase pd           = Db.KpDatabase;
            PwGroup    pgRecycleBin = pd.RootGroup.FindGroup(pd.RecycleBinUuid, true);

            if (pg.Uuid.Equals(pd.EntryTemplatesGroup))
            {
                pd.EntryTemplatesGroup        = PwUuid.Zero;
                pd.EntryTemplatesGroupChanged = DateTime.Now;
            }

            pgParent.Groups.Remove(pg);
            touchedGroups.Add(pgParent);
            if ((DeletePermanently) || (!CanRecycle))
            {
                pg.DeleteAllObjects(pd);

                PwDeletedObject pdo = new PwDeletedObject(pg.Uuid, DateTime.Now);
                pd.DeletedObjects.Add(pdo);


                permanentlyDeletedGroups.Add(pg);
            }
            else             // Recycle
            {
                bool groupListUpdateRequired = false;
                EnsureRecycleBinExists(ref pgRecycleBin, ref groupListUpdateRequired);

                pgRecycleBin.AddGroup(pg, true, true);
                pg.Touch(false);
                // Mark new parent (Recycle bin) touched
                touchedGroups.Add(pg.ParentGroup);
                // mark root touched if recycle bin was created
                if (groupListUpdateRequired)
                {
                    touchedGroups.Add(Db.Root);
                }
            }
            return(true);
        }
Пример #8
0
 private static void DeleteEntries(List <PwEntry> entriesToDelete, PwDatabase dB)
 {
     try
     {
         if (entriesToDelete.Count() > 0)
         {
             foreach (PwEntry entry in entriesToDelete)
             {
                 var parentGroup = entry.ParentGroup;
                 parentGroup.Entries.Remove(entry);
                 var pdo = new PwDeletedObject(entry.Uuid, DateTime.Now);
                 dB.DeletedObjects.Add(pdo);
             }
         }
     }
     catch (Exception e) { MessageService.ShowInfo(e.Message); }
 }
Пример #9
0
        protected void DoDeleteEntry(PwEntry pe, List <PwGroup> touchedGroups)
        {
            PwDatabase pd = Db.KpDatabase;

            PwGroup pgRecycleBin = pd.RootGroup.FindGroup(pd.RecycleBinUuid, true);

            bool     bUpdateGroupList = false;
            DateTime dtNow            = DateTime.Now;

            PwGroup pgParent = pe.ParentGroup;

            if (pgParent != null)
            {
                pgParent.Entries.Remove(pe);
                //TODO check if RecycleBin is deleted
                //TODO no recycle bin in KDB

                if ((DeletePermanently) || (!CanRecycle))
                {
                    PwDeletedObject pdo = new PwDeletedObject(pe.Uuid, dtNow);
                    pd.DeletedObjects.Add(pdo);
                    touchedGroups.Add(pgParent);
                    Db.EntriesById.Remove(pe.Uuid);
                    Db.Elements.Remove(pe);
                }
                else                 // Recycle
                {
                    EnsureRecycleBinExists(ref pgRecycleBin, ref bUpdateGroupList);

                    pgRecycleBin.AddEntry(pe, true, true);
                    pe.Touch(false);

                    touchedGroups.Add(pgParent);
                    // Mark new parent dirty
                    touchedGroups.Add(pgRecycleBin);
                    // mark root dirty if recycle bin was created
                    touchedGroups.Add(Db.Root);
                }
            }
        }
Пример #10
0
        private KdbContext EndXmlElement(KdbContext ctx, XmlReader xr)
        {
            Debug.Assert(xr.NodeType == XmlNodeType.EndElement);

            if ((ctx == KdbContext.KeePassFile) && (xr.Name == ElemDocNode))
            {
                return(KdbContext.Null);
            }
            else if ((ctx == KdbContext.Meta) && (xr.Name == ElemMeta))
            {
                return(KdbContext.KeePassFile);
            }
            else if ((ctx == KdbContext.Root) && (xr.Name == ElemRoot))
            {
                return(KdbContext.KeePassFile);
            }
            else if ((ctx == KdbContext.MemoryProtection) && (xr.Name == ElemMemoryProt))
            {
                return(KdbContext.Meta);
            }
            else if ((ctx == KdbContext.CustomIcons) && (xr.Name == ElemCustomIcons))
            {
                return(KdbContext.Meta);
            }
            else if ((ctx == KdbContext.CustomIcon) && (xr.Name == ElemCustomIconItem))
            {
                if ((m_uuidCustomIconID != PwUuid.Zero) && (m_pbCustomIconData != null))
                {
                    m_pwDatabase.CustomIcons.Add(new PwCustomIcon(
                                                     m_uuidCustomIconID, m_pbCustomIconData));

                    m_uuidCustomIconID = PwUuid.Zero;
                    m_pbCustomIconData = null;
                }
                else
                {
                    Debug.Assert(false);
                }

                return(KdbContext.CustomIcons);
            }
            else if ((ctx == KdbContext.Group) && (xr.Name == ElemGroup))
            {
                if (PwUuid.Zero.EqualsValue(m_ctxGroup.Uuid))
                {
                    Debug.Assert(false);
                    m_ctxGroup.Uuid = new PwUuid(true);
                }

                m_ctxGroups.Pop();

                if (m_ctxGroups.Count == 0)
                {
                    m_ctxGroup = null;
                    return(KdbContext.Root);
                }
                else
                {
                    m_ctxGroup = m_ctxGroups.Peek();
                    return(KdbContext.Group);
                }
            }
            else if ((ctx == KdbContext.GroupTimes) && (xr.Name == ElemTimes))
            {
                return(KdbContext.Group);
            }
            else if ((ctx == KdbContext.Entry) && (xr.Name == ElemEntry))
            {
                // Create new UUID if absent
                if (PwUuid.Zero.EqualsValue(m_ctxEntry.Uuid))
                {
                    Debug.Assert(false);
                    m_ctxEntry.Uuid = new PwUuid(true);
                }

                if (m_bEntryInHistory)
                {
                    m_ctxEntry = m_ctxHistoryBase;
                    return(KdbContext.EntryHistory);
                }

                return(KdbContext.Group);
            }
            else if ((ctx == KdbContext.EntryTimes) && (xr.Name == ElemTimes))
            {
                return(KdbContext.Entry);
            }
            else if ((ctx == KdbContext.EntryString) && (xr.Name == ElemString))
            {
                m_ctxEntry.Strings.Set(m_ctxStringName, m_ctxStringValue);
                m_ctxStringName  = null;
                m_ctxStringValue = null;
                return(KdbContext.Entry);
            }
            else if ((ctx == KdbContext.EntryBinary) && (xr.Name == ElemBinary))
            {
                m_ctxEntry.Binaries.Set(m_ctxBinaryName, m_ctxBinaryValue);
                m_ctxBinaryName  = null;
                m_ctxBinaryValue = null;
                return(KdbContext.Entry);
            }
            else if ((ctx == KdbContext.EntryAutoType) && (xr.Name == ElemAutoType))
            {
                return(KdbContext.Entry);
            }
            else if ((ctx == KdbContext.EntryAutoTypeItem) && (xr.Name == ElemAutoTypeItem))
            {
                m_ctxEntry.AutoType.Set(m_ctxATName, m_ctxATSeq);
                m_ctxATName = null;
                m_ctxATSeq  = null;
                return(KdbContext.EntryAutoType);
            }
            else if ((ctx == KdbContext.EntryHistory) && (xr.Name == ElemHistory))
            {
                m_bEntryInHistory = false;
                return(KdbContext.Entry);
            }
            else if ((ctx == KdbContext.RootDeletedObjects) && (xr.Name == ElemDeletedObjects))
            {
                return(KdbContext.Root);
            }
            else if ((ctx == KdbContext.DeletedObject) && (xr.Name == ElemDeletedObject))
            {
                m_ctxDeletedObject = null;
                return(KdbContext.RootDeletedObjects);
            }
            else
            {
                Debug.Assert(false);
                throw new FormatException();
            }
        }
Пример #11
0
        public bool RemoveGroup(string uuid)
        {
            // Make sure there is an active database
            if (!ensureDBisOpen()) return false;

            if (uuid != null && uuid.Length > 0)
            {
                PwUuid pwuuid = new PwUuid(KeePassLib.Utility.MemUtil.HexStringToByteArray(uuid));

                PwGroup matchedGroup = GetRootPwGroup(host.Database).FindGroup(pwuuid, true);

                if (matchedGroup == null)
                    throw new Exception("Could not find requested entry.");

                PwGroup matchedGroupParent = matchedGroup.ParentGroup;
                if (matchedGroupParent == null) return false; // Can't remove

                matchedGroupParent.Groups.Remove(matchedGroup);

                PwGroup recycleBin = host.Database.RootGroup.FindGroup(host.Database.RecycleBinUuid, true);

                if (host.Database.RecycleBinEnabled == false)
                {
                    if (!KeePassLib.Utility.MessageService.AskYesNo(KPRes.DeleteGroupQuestion, KPRes.DeleteGroupTitle))
                        return false;

                    PwDeletedObject pdo = new PwDeletedObject();
                    pdo.Uuid = matchedGroup.Uuid;
                    pdo.DeletionTime = DateTime.Now;
                    host.Database.DeletedObjects.Add(pdo);
                }
                else
                {
                    if (recycleBin == null)
                    {
                        recycleBin = new PwGroup(true, true, KPRes.RecycleBin, PwIcon.TrashBin);
                        recycleBin.EnableAutoType = false;
                        recycleBin.EnableSearching = false;
                        host.Database.RootGroup.AddGroup(recycleBin, true);

                        host.Database.RecycleBinUuid = recycleBin.Uuid;
                    }

                    recycleBin.AddGroup(matchedGroup, true);
                    matchedGroup.Touch(false);
                }

                host.MainWindow.BeginInvoke(new dlgSaveDB(saveDB), host.Database);

                return true;
            }
            return false;
        }
Пример #12
0
        public override void Run()
        {
            StatusLogger.UpdateMessage(UiStringKey.DeletingEntry);
            PwDatabase pd = Db.KpDatabase;

            PwGroup pgRecycleBin = pd.RootGroup.FindGroup(pd.RecycleBinUuid, true);

            bool bUpdateGroupList = false;
            DateTime dtNow = DateTime.Now;
            PwEntry pe = _entry;
            PwGroup pgParent = pe.ParentGroup;
            if(pgParent != null)
            {
                pgParent.Entries.Remove(pe);
                //TODO check if RecycleBin is deleted
                //TODO no recycle bin in KDB

                if ((DeletePermanently) || (!CanRecycle))
                {
                    PwDeletedObject pdo = new PwDeletedObject(pe.Uuid, dtNow);
                    pd.DeletedObjects.Add(pdo);

                    _onFinishToRun = new ActionOnFinish((success, message) =>
                        {
                            if (success)
                            {
                                // Mark parent dirty
                                Db.Dirty.Add(pgParent);
                            }
                            else
                            {
                                // Let's not bother recovering from a failure to save a deleted entry.  It is too much work.
                                App.LockDatabase(false);
                            }
                        }, OnFinishToRun);
                }
                else // Recycle
                {
                    EnsureRecycleBinExists(ref pgRecycleBin, ref bUpdateGroupList);

                    pgRecycleBin.AddEntry(pe, true, true);
                    pe.Touch(false);

                    _onFinishToRun = new ActionOnFinish( (success, message) =>
                                                 {
                        if ( success ) {
                            // Mark previous parent dirty
                            Db.Dirty.Add(pgParent);
                            // Mark new parent dirty
                            Db.Dirty.Add(pgRecycleBin);
                            // mark root dirty if recycle bin was created
                            if (bUpdateGroupList)
                                Db.Dirty.Add(Db.Root);
                        } else {
                            // Let's not bother recovering from a failure to save a deleted entry.  It is too much work.
                            App.LockDatabase(false);
                        }

                    }, OnFinishToRun);
                }
            }

            // Commit database
            SaveDb save = new SaveDb(Ctx, App, OnFinishToRun, false);
            save.SetStatusLogger(StatusLogger);
            save.Run();
        }
 public static void DeleteFrom(this PwGroup group, PwGroup parent, PwDatabase database)
 {
     //first remove all entries in the group
     group.DeleteAllObjects(database);
     // create the delete marker
     PwDeletedObject deleteMarker = new PwDeletedObject(group.Uuid, DateTime.Now);
     database.DeletedObjects.Add(deleteMarker);
     //now we can remove the empty group
     parent.Groups.Remove(group);
 }
		private KdbContext EndXmlElement(KdbContext ctx, XmlReader xr)
		{
			Debug.Assert(xr.NodeType == XmlNodeType.EndElement);

			if((ctx == KdbContext.KeePassFile) && (xr.Name == ElemDocNode))
				return KdbContext.Null;
			else if((ctx == KdbContext.Meta) && (xr.Name == ElemMeta))
				return KdbContext.KeePassFile;
			else if((ctx == KdbContext.Root) && (xr.Name == ElemRoot))
				return KdbContext.KeePassFile;
			else if((ctx == KdbContext.MemoryProtection) && (xr.Name == ElemMemoryProt))
				return KdbContext.Meta;
			else if((ctx == KdbContext.CustomIcons) && (xr.Name == ElemCustomIcons))
				return KdbContext.Meta;
			else if((ctx == KdbContext.CustomIcon) && (xr.Name == ElemCustomIconItem))
			{
				if(!m_uuidCustomIconID.Equals(PwUuid.Zero) &&
					(m_pbCustomIconData != null))
					m_pwDatabase.CustomIcons.Add(new PwCustomIcon(
						m_uuidCustomIconID, m_pbCustomIconData));
				else { Debug.Assert(false); }

				m_uuidCustomIconID = PwUuid.Zero;
				m_pbCustomIconData = null;

				return KdbContext.CustomIcons;
			}
			else if((ctx == KdbContext.Binaries) && (xr.Name == ElemBinaries))
				return KdbContext.Meta;
			else if((ctx == KdbContext.CustomData) && (xr.Name == ElemCustomData))
				return KdbContext.Meta;
			else if((ctx == KdbContext.CustomDataItem) && (xr.Name == ElemStringDictExItem))
			{
				if((m_strCustomDataKey != null) && (m_strCustomDataValue != null))
					m_pwDatabase.CustomData.Set(m_strCustomDataKey, m_strCustomDataValue);
				else { Debug.Assert(false); }

				m_strCustomDataKey = null;
				m_strCustomDataValue = null;

				return KdbContext.CustomData;
			}
			else if((ctx == KdbContext.Group) && (xr.Name == ElemGroup))
			{
				if(PwUuid.Zero.Equals(m_ctxGroup.Uuid))
					m_ctxGroup.Uuid = new PwUuid(true); // No assert (import)

				m_ctxGroups.Pop();

				if(m_ctxGroups.Count == 0)
				{
					m_ctxGroup = null;
					return KdbContext.Root;
				}
				else
				{
					m_ctxGroup = m_ctxGroups.Peek();
					return KdbContext.Group;
				}
			}
			else if((ctx == KdbContext.GroupTimes) && (xr.Name == ElemTimes))
				return KdbContext.Group;
			else if((ctx == KdbContext.Entry) && (xr.Name == ElemEntry))
			{
				// Create new UUID if absent
				if(PwUuid.Zero.Equals(m_ctxEntry.Uuid))
					m_ctxEntry.Uuid = new PwUuid(true); // No assert (import)

				if(m_bEntryInHistory)
				{
					m_ctxEntry = m_ctxHistoryBase;
					return KdbContext.EntryHistory;
				}

				return KdbContext.Group;
			}
			else if((ctx == KdbContext.EntryTimes) && (xr.Name == ElemTimes))
				return KdbContext.Entry;
			else if((ctx == KdbContext.EntryString) && (xr.Name == ElemString))
			{
				m_ctxEntry.Strings.Set(m_ctxStringName, m_ctxStringValue);
				m_ctxStringName = null;
				m_ctxStringValue = null;
				return KdbContext.Entry;
			}
			else if((ctx == KdbContext.EntryBinary) && (xr.Name == ElemBinary))
			{
				if(string.IsNullOrEmpty(m_strDetachBins))
					m_ctxEntry.Binaries.Set(m_ctxBinaryName, m_ctxBinaryValue);
				else
				{
					SaveBinary(m_ctxBinaryName, m_ctxBinaryValue, m_strDetachBins);

					m_ctxBinaryValue = null;
					GC.Collect();
				}

				m_ctxBinaryName = null;
				m_ctxBinaryValue = null;
				return KdbContext.Entry;
			}
			else if((ctx == KdbContext.EntryAutoType) && (xr.Name == ElemAutoType))
				return KdbContext.Entry;
			else if((ctx == KdbContext.EntryAutoTypeItem) && (xr.Name == ElemAutoTypeItem))
			{
				AutoTypeAssociation atAssoc = new AutoTypeAssociation(m_ctxATName,
					m_ctxATSeq);
				m_ctxEntry.AutoType.Add(atAssoc);
				m_ctxATName = null;
				m_ctxATSeq = null;
				return KdbContext.EntryAutoType;
			}
			else if((ctx == KdbContext.EntryHistory) && (xr.Name == ElemHistory))
			{
				m_bEntryInHistory = false;
				return KdbContext.Entry;
			}
			else if((ctx == KdbContext.RootDeletedObjects) && (xr.Name == ElemDeletedObjects))
				return KdbContext.Root;
			else if((ctx == KdbContext.DeletedObject) && (xr.Name == ElemDeletedObject))
			{
				m_ctxDeletedObject = null;
				return KdbContext.RootDeletedObjects;
			}
			else
			{
				Debug.Assert(false);
				throw new FormatException();
			}
		}
Пример #15
0
        internal KdbContext ReadXmlElement(KdbContext ctx, XmlReader xr)
        {
            Debug.Assert(xr.NodeType == XmlNodeType.Element);

            switch (ctx)
            {
            case KdbContext.Null:
                if (xr.Name == ElemDocNode)
                {
                    return(SwitchContext(ctx, KdbContext.KeePassFile, xr));
                }
                else
                {
                    ReadUnknown(xr);
                }
                break;

            case KdbContext.KeePassFile:
                if (xr.Name == ElemMeta)
                {
                    return(SwitchContext(ctx, KdbContext.Meta, xr));
                }
                else if (xr.Name == ElemRoot)
                {
                    return(SwitchContext(ctx, KdbContext.Root, xr));
                }
                else
                {
                    ReadUnknown(xr);
                }
                break;

            case KdbContext.Meta:
                if (xr.Name == ElemGenerator)
                {
                    ReadString(xr);                             // Ignore
                }
                else if (xr.Name == ElemDbName)
                {
                    m_pwDatabase.Name = ReadString(xr);
                }
                else if (xr.Name == ElemDbNameChanged)
                {
                    m_pwDatabase.NameChanged = ReadTime(xr);
                }
                else if (xr.Name == ElemDbDesc)
                {
                    m_pwDatabase.Description = ReadString(xr);
                }
                else if (xr.Name == ElemDbDescChanged)
                {
                    m_pwDatabase.DescriptionChanged = ReadTime(xr);
                }
                else if (xr.Name == ElemDbDefaultUser)
                {
                    m_pwDatabase.DefaultUserName = ReadString(xr);
                }
                else if (xr.Name == ElemDbDefaultUserChanged)
                {
                    m_pwDatabase.DefaultUserNameChanged = ReadTime(xr);
                }
                else if (xr.Name == ElemDbMntncHistoryDays)
                {
                    m_pwDatabase.MaintenanceHistoryDays = ReadUInt(xr, 365);
                }
                else if (xr.Name == ElemMemoryProt)
                {
                    return(SwitchContext(ctx, KdbContext.MemoryProtection, xr));
                }
                else if (xr.Name == ElemCustomIcons)
                {
                    return(SwitchContext(ctx, KdbContext.CustomIcons, xr));
                }
                else if (xr.Name == ElemRecycleBinEnabled)
                {
                    m_pwDatabase.RecycleBinEnabled = ReadBool(xr, true);
                }
                else if (xr.Name == ElemRecycleBinUuid)
                {
                    m_pwDatabase.RecycleBinUuid = ReadUuid(xr);
                }
                else if (xr.Name == ElemRecycleBinChanged)
                {
                    m_pwDatabase.RecycleBinChanged = ReadTime(xr);
                }
                else if (xr.Name == ElemEntryTemplatesGroup)
                {
                    m_pwDatabase.EntryTemplatesGroup = ReadUuid(xr);
                }
                else if (xr.Name == ElemEntryTemplatesGroupChanged)
                {
                    m_pwDatabase.EntryTemplatesGroupChanged = ReadTime(xr);
                }
                else if (xr.Name == ElemLastSelectedGroup)
                {
                    m_pwDatabase.LastSelectedGroup = ReadUuid(xr);
                }
                else if (xr.Name == ElemLastTopVisibleGroup)
                {
                    m_pwDatabase.LastTopVisibleGroup = ReadUuid(xr);
                }
                else if (xr.Name == ElemCustomData)
                {
                    return(SwitchContext(ctx, KdbContext.CustomData, xr));
                }
                else
                {
                    ReadUnknown(xr);
                }
                break;

            case KdbContext.MemoryProtection:
                if (xr.Name == ElemProtTitle)
                {
                    m_pwDatabase.MemoryProtection.ProtectTitle = ReadBool(xr, false);
                }
                else if (xr.Name == ElemProtUserName)
                {
                    m_pwDatabase.MemoryProtection.ProtectUserName = ReadBool(xr, false);
                }
                else if (xr.Name == ElemProtPassword)
                {
                    m_pwDatabase.MemoryProtection.ProtectPassword = ReadBool(xr, true);
                }
                else if (xr.Name == ElemProtURL)
                {
                    m_pwDatabase.MemoryProtection.ProtectUrl = ReadBool(xr, false);
                }
                else if (xr.Name == ElemProtNotes)
                {
                    m_pwDatabase.MemoryProtection.ProtectNotes = ReadBool(xr, false);
                }
                else if (xr.Name == ElemProtAutoHide)
                {
                    m_pwDatabase.MemoryProtection.AutoEnableVisualHiding = ReadBool(xr, true);
                }
                else
                {
                    ReadUnknown(xr);
                }
                break;

            case KdbContext.CustomIcons:
                if (xr.Name == ElemCustomIconItem)
                {
                    return(SwitchContext(ctx, KdbContext.CustomIcon, xr));
                }
                else
                {
                    ReadUnknown(xr);
                }
                break;

            case KdbContext.CustomIcon:
                if (xr.Name == ElemCustomIconItemID)
                {
                    m_uuidCustomIconID = ReadUuid(xr);
                }
                else if (xr.Name == ElemCustomIconItemData)
                {
                    string strData = ReadString(xr);
                    if ((strData != null) && (strData.Length > 0))
                    {
                        m_pbCustomIconData = Convert.FromBase64String(strData);
                    }
                    else
                    {
                        Debug.Assert(false);
                    }
                }
                else
                {
                    ReadUnknown(xr);
                }
                break;

            case KdbContext.CustomData:
                if (xr.Name == ElemStringDictExItem)
                {
                    return(SwitchContext(ctx, KdbContext.CustomDataItem, xr));
                }
                else
                {
                    ReadUnknown(xr);
                }
                break;

            case KdbContext.CustomDataItem:
                if (xr.Name == ElemKey)
                {
                    m_strCustomDataKey = ReadString(xr);
                }
                else if (xr.Name == ElemValue)
                {
                    m_strCustomDataValue = ReadString(xr);
                }
                else
                {
                    ReadUnknown(xr);
                }
                break;

            case KdbContext.Root:
                if (xr.Name == ElemGroup)
                {
                    Debug.Assert(m_ctxGroups.Count == 0);
                    if (m_ctxGroups.Count != 0)
                    {
                        throw new FormatException();
                    }

                    m_pwDatabase.RootGroup = new PwGroup(false, false);
                    m_ctxGroups.Push(m_pwDatabase.RootGroup);
                    m_ctxGroup = m_ctxGroups.Peek();

                    return(SwitchContext(ctx, KdbContext.Group, xr));
                }
                else if (xr.Name == ElemDeletedObjects)
                {
                    return(SwitchContext(ctx, KdbContext.RootDeletedObjects, xr));
                }
                else
                {
                    ReadUnknown(xr);
                }
                break;

            case KdbContext.Group:
                if (xr.Name == ElemUuid)
                {
                    m_ctxGroup.Uuid = ReadUuid(xr);
                }
                else if (xr.Name == ElemName)
                {
                    m_ctxGroup.Name = ReadString(xr);
                }
                else if (xr.Name == ElemNotes)
                {
                    m_ctxGroup.Notes = ReadString(xr);
                }
                else if (xr.Name == ElemIcon)
                {
                    m_ctxGroup.IconId = (PwIcon)ReadUInt(xr, (uint)PwIcon.Folder);
                }
                else if (xr.Name == ElemCustomIconID)
                {
                    m_ctxGroup.CustomIconUuid = ReadUuid(xr);
                }
                else if (xr.Name == ElemTimes)
                {
                    return(SwitchContext(ctx, KdbContext.GroupTimes, xr));
                }
                else if (xr.Name == ElemIsExpanded)
                {
                    m_ctxGroup.IsExpanded = ReadBool(xr, true);
                }
                else if (xr.Name == ElemGroupDefaultAutoTypeSeq)
                {
                    m_ctxGroup.DefaultAutoTypeSequence = ReadString(xr);
                }
                else if (xr.Name == ElemEnableAutoType)
                {
                    m_ctxGroup.EnableAutoType = StrUtil.StringToBoolEx(ReadString(xr));
                }
                else if (xr.Name == ElemEnableSearching)
                {
                    m_ctxGroup.EnableSearching = StrUtil.StringToBoolEx(ReadString(xr));
                }
                else if (xr.Name == ElemLastTopVisibleEntry)
                {
                    m_ctxGroup.LastTopVisibleEntry = ReadUuid(xr);
                }
                else if (xr.Name == ElemGroup)
                {
                    m_ctxGroup = new PwGroup(false, false);
                    m_ctxGroups.Peek().AddGroup(m_ctxGroup, true);

                    m_ctxGroups.Push(m_ctxGroup);

                    return(SwitchContext(ctx, KdbContext.Group, xr));
                }
                else if (xr.Name == ElemEntry)
                {
                    m_ctxEntry = new PwEntry(false, false);
                    m_ctxGroup.AddEntry(m_ctxEntry, true);

                    m_bEntryInHistory = false;
                    return(SwitchContext(ctx, KdbContext.Entry, xr));
                }
                else
                {
                    ReadUnknown(xr);
                }
                break;

            case KdbContext.Entry:
                if (xr.Name == ElemUuid)
                {
                    m_ctxEntry.Uuid = ReadUuid(xr);
                }
                else if (xr.Name == ElemIcon)
                {
                    m_ctxEntry.IconId = (PwIcon)ReadUInt(xr, (uint)PwIcon.Key);
                }
                else if (xr.Name == ElemCustomIconID)
                {
                    m_ctxEntry.CustomIconUuid = ReadUuid(xr);
                }
                else if (xr.Name == ElemFgColor)
                {
                    string strColor = ReadString(xr);
                    if ((strColor != null) && (strColor.Length > 0))
                    {
                        m_ctxEntry.ForegroundColor = ColorTranslator.FromHtml(strColor);
                    }
                }
                else if (xr.Name == ElemBgColor)
                {
                    string strColor = ReadString(xr);
                    if ((strColor != null) && (strColor.Length > 0))
                    {
                        m_ctxEntry.BackgroundColor = ColorTranslator.FromHtml(strColor);
                    }
                }
                else if (xr.Name == ElemOverrideUrl)
                {
                    m_ctxEntry.OverrideUrl = ReadString(xr);
                }
                else if (xr.Name == ElemTimes)
                {
                    return(SwitchContext(ctx, KdbContext.EntryTimes, xr));
                }
                else if (xr.Name == ElemString)
                {
                    return(SwitchContext(ctx, KdbContext.EntryString, xr));
                }
                else if (xr.Name == ElemBinary)
                {
                    return(SwitchContext(ctx, KdbContext.EntryBinary, xr));
                }
                else if (xr.Name == ElemAutoType)
                {
                    return(SwitchContext(ctx, KdbContext.EntryAutoType, xr));
                }
                else if (xr.Name == ElemHistory)
                {
                    Debug.Assert(m_bEntryInHistory == false);

                    if (m_bEntryInHistory == false)
                    {
                        m_ctxHistoryBase = m_ctxEntry;
                        return(SwitchContext(ctx, KdbContext.EntryHistory, xr));
                    }
                    else
                    {
                        ReadUnknown(xr);
                    }
                }
                else
                {
                    ReadUnknown(xr);
                }
                break;

            case KdbContext.GroupTimes:
            case KdbContext.EntryTimes:
                ITimeLogger tl = ((ctx == KdbContext.GroupTimes) ?
                                  (ITimeLogger)m_ctxGroup : (ITimeLogger)m_ctxEntry);
                Debug.Assert(tl != null);

                if (xr.Name == ElemLastModTime)
                {
                    tl.LastModificationTime = ReadTime(xr);
                }
                else if (xr.Name == ElemCreationTime)
                {
                    tl.CreationTime = ReadTime(xr);
                }
                else if (xr.Name == ElemLastAccessTime)
                {
                    tl.LastAccessTime = ReadTime(xr);
                }
                else if (xr.Name == ElemExpiryTime)
                {
                    tl.ExpiryTime = ReadTime(xr);
                }
                else if (xr.Name == ElemExpires)
                {
                    tl.Expires = ReadBool(xr, false);
                }
                else if (xr.Name == ElemUsageCount)
                {
                    tl.UsageCount = ReadULong(xr, 0);
                }
                else if (xr.Name == ElemLocationChanged)
                {
                    tl.LocationChanged = ReadTime(xr);
                }
                else
                {
                    ReadUnknown(xr);
                }
                break;

            case KdbContext.EntryString:
                if (xr.Name == ElemKey)
                {
                    m_ctxStringName = ReadString(xr);
                }
                else if (xr.Name == ElemValue)
                {
                    m_ctxStringValue = ReadProtectedString(xr);
                }
                else
                {
                    ReadUnknown(xr);
                }
                break;

            case KdbContext.EntryBinary:
                if (xr.Name == ElemKey)
                {
                    m_ctxBinaryName = ReadString(xr);
                }
                else if (xr.Name == ElemValue)
                {
                    m_ctxBinaryValue = ReadProtectedBinary(xr);
                }
                else
                {
                    ReadUnknown(xr);
                }
                break;

            case KdbContext.EntryAutoType:
                if (xr.Name == ElemAutoTypeEnabled)
                {
                    m_ctxEntry.AutoType.Enabled = ReadBool(xr, true);
                }
                else if (xr.Name == ElemAutoTypeObfuscation)
                {
                    m_ctxEntry.AutoType.ObfuscationOptions =
                        (AutoTypeObfuscationOptions)ReadUInt(xr, 0);
                }
                else if (xr.Name == ElemAutoTypeDefaultSeq)
                {
                    m_ctxEntry.AutoType.DefaultSequence = ReadString(xr);
                }
                else if (xr.Name == ElemAutoTypeItem)
                {
                    return(SwitchContext(ctx, KdbContext.EntryAutoTypeItem, xr));
                }
                else
                {
                    ReadUnknown(xr);
                }
                break;

            case KdbContext.EntryAutoTypeItem:
                if (xr.Name == ElemWindow)
                {
                    m_ctxATName = ReadString(xr);
                }
                else if (xr.Name == ElemKeystrokeSequence)
                {
                    m_ctxATSeq = ReadString(xr);
                }
                else
                {
                    ReadUnknown(xr);
                }
                break;

            case KdbContext.EntryHistory:
                if (xr.Name == ElemEntry)
                {
                    m_ctxEntry = new PwEntry(false, false);
                    m_ctxHistoryBase.History.Add(m_ctxEntry);

                    m_bEntryInHistory = true;
                    return(SwitchContext(ctx, KdbContext.Entry, xr));
                }
                else
                {
                    ReadUnknown(xr);
                }
                break;

            case KdbContext.RootDeletedObjects:
                if (xr.Name == ElemDeletedObject)
                {
                    m_ctxDeletedObject = new PwDeletedObject();
                    m_pwDatabase.DeletedObjects.Add(m_ctxDeletedObject);

                    return(SwitchContext(ctx, KdbContext.DeletedObject, xr));
                }
                else
                {
                    ReadUnknown(xr);
                }
                break;

            case KdbContext.DeletedObject:
                if (xr.Name == ElemUuid)
                {
                    m_ctxDeletedObject.Uuid = ReadUuid(xr);
                }
                else if (xr.Name == ElemDeletionTime)
                {
                    m_ctxDeletedObject.DeletionTime = ReadTime(xr);
                }
                else
                {
                    ReadUnknown(xr);
                }
                break;

            default:
                ReadUnknown(xr);
                break;
            }

            return(ctx);
        }
Пример #16
0
		private void DeleteSelectedEntries()
		{
			PwEntry[] vSelected = GetSelectedEntries();
			if((vSelected == null) || (vSelected.Length == 0)) return;

			PwDatabase pd = m_docMgr.ActiveDatabase;
			PwGroup pgRecycleBin = pd.RootGroup.FindGroup(pd.RecycleBinUuid, true);
			bool bShiftPressed = ((Control.ModifierKeys & Keys.Shift) != Keys.None);

			bool bAtLeastOnePermanent = false;
			if(pd.RecycleBinEnabled == false) bAtLeastOnePermanent = true;
			else if(bShiftPressed) bAtLeastOnePermanent = true;
			else if(pgRecycleBin == null) { } // Not permanent
			else
			{
				foreach(PwEntry peEnum in vSelected)
				{
					if((peEnum.ParentGroup == pgRecycleBin) ||
						peEnum.ParentGroup.IsContainedIn(pgRecycleBin))
					{
						bAtLeastOnePermanent = true;
						break;
					}
				}
			}

			bool bSingle = (vSelected.Length == 1);
			string strSummary = EntryUtil.CreateSummaryList(null, vSelected);

			if(bAtLeastOnePermanent)
			{
				VistaTaskDialog dlg = new VistaTaskDialog();
				dlg.CommandLinks = false;
				dlg.Content = strSummary;
				dlg.MainInstruction = (bSingle ? KPRes.DeleteEntriesQuestionSingle :
					KPRes.DeleteEntriesQuestion);
				dlg.SetIcon(VtdCustomIcon.Question);
				dlg.WindowTitle = PwDefs.ShortProductName;
				dlg.AddButton((int)DialogResult.OK, KPRes.DeleteCmd, null);
				dlg.AddButton((int)DialogResult.Cancel, KPRes.Cancel, null);

				if(dlg.ShowDialog())
				{
					if(dlg.Result == (int)DialogResult.Cancel) return;
				}
				else
				{
					string strText = (bSingle ? KPRes.DeleteEntriesQuestionSingle :
						KPRes.DeleteEntriesQuestion);
					if(strSummary.Length > 0)
						strText += MessageService.NewParagraph + strSummary;

					if(!MessageService.AskYesNo(strText, (bSingle ?
						KPRes.DeleteEntriesTitleSingle : KPRes.DeleteEntriesTitle)))
						return;
				}
			}
			else if(Program.Config.UI.ShowRecycleConfirmDialog)
			{
				VistaTaskDialog dlg = new VistaTaskDialog();
				dlg.CommandLinks = false;
				dlg.Content = strSummary;
				dlg.MainInstruction = (bSingle ? KPRes.RecycleEntryConfirmSingle :
					KPRes.RecycleEntryConfirm);
				dlg.SetIcon(VtdCustomIcon.Question);
				dlg.VerificationText = KPRes.DialogNoShowAgain;
				dlg.WindowTitle = PwDefs.ShortProductName;
				dlg.AddButton((int)DialogResult.OK, KPRes.YesCmd, null);
				dlg.AddButton((int)DialogResult.Cancel, KPRes.NoCmd, null);

				if(dlg.ShowDialog())
				{
					if(dlg.Result == (int)DialogResult.Cancel) return;

					if(dlg.ResultVerificationChecked)
						Program.Config.UI.ShowRecycleConfirmDialog = false;
				}
				else
				{
					string strText = (bSingle ? KPRes.RecycleEntryConfirmSingle :
						KPRes.RecycleEntryConfirm);
					if(strSummary.Length > 0)
						strText += MessageService.NewParagraph + strSummary;

					if(!MessageService.AskYesNo(strText, (bSingle ?
						KPRes.DeleteEntriesTitleSingle : KPRes.DeleteEntriesTitle)))
						return;
				}
			}

			bool bUpdateGroupList = false;
			DateTime dtNow = DateTime.Now;
			foreach(PwEntry pe in vSelected)
			{
				PwGroup pgParent = pe.ParentGroup;
				if(pgParent == null) continue; // Can't remove

				pgParent.Entries.Remove(pe);

				bool bPermanent = false;
				if(pd.RecycleBinEnabled == false) bPermanent = true;
				else if(bShiftPressed) bPermanent = true;
				else if(pgRecycleBin == null) { } // Recycle
				else if(pgParent == pgRecycleBin) bPermanent = true;
				else if(pgParent.IsContainedIn(pgRecycleBin)) bPermanent = true;

				if(bPermanent)
				{
					PwDeletedObject pdo = new PwDeletedObject(pe.Uuid, dtNow);
					pd.DeletedObjects.Add(pdo);
				}
				else // Recycle
				{
					EnsureRecycleBin(ref pgRecycleBin, pd, ref bUpdateGroupList);

					pgRecycleBin.AddEntry(pe, true, true);
					pe.Touch(false);
				}
			}

			RemoveEntriesFromList(vSelected, true);
			UpdateUI(false, null, bUpdateGroupList, null, false, null, true);
		}
Пример #17
0
        private void WriteObject(string name, PwDeletedObject value)
        {
            Debug.Assert(name != null);
            Debug.Assert(value != null); if(value == null) throw new ArgumentNullException("value");

            m_xmlWriter.WriteStartElement(name);
            WriteObject(ElemUuid, value.Uuid);
            WriteObject(ElemDeletionTime, value.DeletionTime);
            m_xmlWriter.WriteEndElement();
        }
Пример #18
0
		private static void AddDeletedObject(PwDatabase pd, PwUuid pu)
		{
			foreach(PwDeletedObject pdo in pd.DeletedObjects)
			{
				if(pdo.Uuid.Equals(pu)) { Debug.Assert(false); return; }
			}

			PwDeletedObject pdoNew = new PwDeletedObject(pu, DateTime.UtcNow);
			pd.DeletedObjects.Add(pdoNew);
		}
Пример #19
0
        /// <summary>
        /// Delete a group.
        /// </summary>
        /// <param name="pg">Group to be added. Must not be <c>null</c>.</param>
        /// <param name="permanent">Permanent delete or move to recycle bin</param>
        public void DeleteGroup(PwGroup pg, bool permanent = false)
        {
            if (pg == null)
            {
                throw new ArgumentNullException("pg");
            }

            PwGroup pgParent = pg.ParentGroup;

            if (pgParent == null)
            {
                throw new ArgumentNullException("pgParent");                                // Can't remove virtual or root group
            }
            PwGroup pgRecycleBin = RootGroup.FindGroup(RecycleBinUuid, true);

            bool bPermanent = false;

            if (RecycleBinEnabled == false)
            {
                bPermanent = true;
            }
            else if (permanent)
            {
                bPermanent = true;
            }
            else if (pgRecycleBin == null)
            {
            }                                              // if we cannot find it, we will create it later
            else if (pg == pgRecycleBin)
            {
                bPermanent = true;
            }
            else if (pg.IsContainedIn(pgRecycleBin))
            {
                bPermanent = true;
            }
            else if (pgRecycleBin.IsContainedIn(pg))
            {
                bPermanent = true;
            }

            pgParent.Groups.Remove(pg);

            if (bPermanent)
            {
                pg.DeleteAllObjects(this);

                PwDeletedObject pdo = new PwDeletedObject(pg.Uuid, DateTime.UtcNow);
                DeletedObjects.Add(pdo);
            }
            else             // Recycle
            {
                EnsureRecycleBin(ref pgRecycleBin);

                try { pgRecycleBin.AddGroup(pg, true, true); }
                catch (Exception)
                {
                    if (pgRecycleBin.Groups.IndexOf(pg) < 0)
                    {
                        pgParent.AddGroup(pg, true, true);                         // Undo removal
                    }
                }

                pg.Touch(false);
            }
        }
        private KdbContext ReadXmlElement(KdbContext ctx, XmlReader xr)
        {
            Debug.Assert(xr.NodeType == XmlNodeType.Element);

            switch(ctx)
            {
                case KdbContext.Null:
                    if(xr.Name == ElemDocNode)
                        return SwitchContext(ctx, KdbContext.KeePassFile, xr);
                    else ReadUnknown(xr);
                    break;

                case KdbContext.KeePassFile:
                    if(xr.Name == ElemMeta)
                        return SwitchContext(ctx, KdbContext.Meta, xr);
                    else if(xr.Name == ElemRoot)
                        return SwitchContext(ctx, KdbContext.Root, xr);
                    else ReadUnknown(xr);
                    break;

                case KdbContext.Meta:
                    if(xr.Name == ElemGenerator)
                        ReadString(xr); // Ignore
                    else if(xr.Name == ElemDbName)
                        m_pwDatabase.Name = ReadString(xr);
                    else if(xr.Name == ElemDbDesc)
                        m_pwDatabase.Description = ReadString(xr);
                    else if(xr.Name == ElemDbDefaultUser)
                        m_pwDatabase.DefaultUserName = ReadString(xr);
                    else if(xr.Name == ElemDbMntncHistoryDays)
                        m_pwDatabase.MaintenanceHistoryDays = ReadUInt(xr, 365);
                    else if(xr.Name == ElemMemoryProt)
                        return SwitchContext(ctx, KdbContext.MemoryProtection, xr);
                    else if(xr.Name == ElemCustomIcons)
                        return SwitchContext(ctx, KdbContext.CustomIcons, xr);
                    else if(xr.Name == ElemLastSelectedGroup)
                        m_pwDatabase.LastSelectedGroup = ReadUuid(xr);
                    else if(xr.Name == ElemLastTopVisibleGroup)
                        m_pwDatabase.LastTopVisibleGroup = ReadUuid(xr);
                    else ReadUnknown(xr);
                    break;

                case KdbContext.MemoryProtection:
                    if(xr.Name == ElemProtTitle)
                        m_pwDatabase.MemoryProtection.ProtectNotes = ReadBool(xr, false);
                    else if(xr.Name == ElemProtUserName)
                        m_pwDatabase.MemoryProtection.ProtectUserName = ReadBool(xr, false);
                    else if(xr.Name == ElemProtPassword)
                        m_pwDatabase.MemoryProtection.ProtectPassword = ReadBool(xr, true);
                    else if(xr.Name == ElemProtURL)
                        m_pwDatabase.MemoryProtection.ProtectUrl = ReadBool(xr, false);
                    else if(xr.Name == ElemProtNotes)
                        m_pwDatabase.MemoryProtection.ProtectNotes = ReadBool(xr, false);
                    else if(xr.Name == ElemProtAutoHide)
                        m_pwDatabase.MemoryProtection.AutoEnableVisualHiding = ReadBool(xr, true);
                    else ReadUnknown(xr);
                    break;

                case KdbContext.CustomIcons:
                    if(xr.Name == ElemCustomIconItem)
                        return SwitchContext(ctx, KdbContext.CustomIcon, xr);
                    else ReadUnknown(xr);
                    break;

                case KdbContext.CustomIcon:
                    if(xr.Name == ElemCustomIconItemID)
                        m_uuidCustomIconID = ReadUuid(xr);
                    else if(xr.Name == ElemCustomIconItemData)
                    {
                        string strData = ReadString(xr);
                        if((strData != null) && (strData.Length > 0))
                            m_pbCustomIconData = Convert.FromBase64String(strData);
                        else { Debug.Assert(false); }
                    }
                    else ReadUnknown(xr);
                    break;

                case KdbContext.Root:
                    if(xr.Name == ElemGroup)
                    {
                        Debug.Assert(m_ctxGroups.Count == 0);
                        if(m_ctxGroups.Count != 0) throw new FormatException();

                        m_pwDatabase.RootGroup = new PwGroup(false, false);
                        m_ctxGroups.Push(m_pwDatabase.RootGroup);
                        m_ctxGroup = m_ctxGroups.Peek();

                        return SwitchContext(ctx, KdbContext.Group, xr);
                    }
                    else if(xr.Name == ElemDeletedObjects)
                        return SwitchContext(ctx, KdbContext.RootDeletedObjects, xr);
                    else ReadUnknown(xr);
                    break;

                case KdbContext.Group:
                    if(xr.Name == ElemUuid)
                        m_ctxGroup.Uuid = ReadUuid(xr);
                    else if(xr.Name == ElemName)
                        m_ctxGroup.Name = ReadString(xr);
                    else if(xr.Name == ElemNotes)
                        m_ctxGroup.Notes = ReadString(xr);
                    else if(xr.Name == ElemIcon)
                        m_ctxGroup.IconId = (PwIcon)ReadUInt(xr, (uint)PwIcon.Folder);
                    else if(xr.Name == ElemCustomIconID)
                        m_ctxGroup.CustomIconUuid = ReadUuid(xr);
                    else if(xr.Name == ElemTimes)
                        return SwitchContext(ctx, KdbContext.GroupTimes, xr);
                    else if(xr.Name == ElemIsExpanded)
                        m_ctxGroup.IsExpanded = ReadBool(xr, true);
                    else if(xr.Name == ElemGroupDefaultAutoTypeSeq)
                        m_ctxGroup.DefaultAutoTypeSequence = ReadString(xr);
                    else if(xr.Name == ElemLastTopVisibleEntry)
                        m_ctxGroup.LastTopVisibleEntry = ReadUuid(xr);
                    else if(xr.Name == ElemGroup)
                    {
                        m_ctxGroup = new PwGroup(false, false);
                        m_ctxGroups.Peek().AddGroup(m_ctxGroup, true);

                        m_ctxGroups.Push(m_ctxGroup);

                        return SwitchContext(ctx, KdbContext.Group, xr);
                    }
                    else if(xr.Name == ElemEntry)
                    {
                        m_ctxEntry = new PwEntry(false, false);
                        m_ctxGroup.AddEntry(m_ctxEntry, true);

                        m_bEntryInHistory = false;
                        return SwitchContext(ctx, KdbContext.Entry, xr);
                    }
                    else ReadUnknown(xr);
                    break;

                case KdbContext.Entry:
                    if(xr.Name == ElemUuid)
                        m_ctxEntry.Uuid = ReadUuid(xr);
                    else if(xr.Name == ElemIcon)
                        m_ctxEntry.IconId = (PwIcon)ReadUInt(xr, (uint)PwIcon.Key);
                    else if(xr.Name == ElemCustomIconID)
                        m_ctxEntry.CustomIconUuid = ReadUuid(xr);
                    else if(xr.Name == ElemFgColor)
                    {
                        string strColor = ReadString(xr);
                        if((strColor != null) && (strColor.Length > 0))
                            m_ctxEntry.ForegroundColor = ColorTranslator.FromHtml(strColor);
                    }
                    else if(xr.Name == ElemBgColor)
                    {
                        string strColor = ReadString(xr);
                        if((strColor != null) && (strColor.Length > 0))
                            m_ctxEntry.BackgroundColor = ColorTranslator.FromHtml(strColor);
                    }
                    else if(xr.Name == ElemOverrideUrl)
                        m_ctxEntry.OverrideUrl = ReadString(xr);
                    else if(xr.Name == ElemTimes)
                        return SwitchContext(ctx, KdbContext.EntryTimes, xr);
                    else if(xr.Name == ElemString)
                        return SwitchContext(ctx, KdbContext.EntryString, xr);
                    else if(xr.Name == ElemBinary)
                        return SwitchContext(ctx, KdbContext.EntryBinary, xr);
                    else if(xr.Name == ElemAutoType)
                        return SwitchContext(ctx, KdbContext.EntryAutoType, xr);
                    else if(xr.Name == ElemHistory)
                    {
                        Debug.Assert(m_bEntryInHistory == false);

                        if(m_bEntryInHistory == false)
                        {
                            m_ctxHistoryBase = m_ctxEntry;
                            return SwitchContext(ctx, KdbContext.EntryHistory, xr);
                        }
                        else ReadUnknown(xr);
                    }
                    else ReadUnknown(xr);
                    break;

                case KdbContext.GroupTimes:
                case KdbContext.EntryTimes:
                    ITimeLogger tl = ((ctx == KdbContext.GroupTimes) ?
                        (ITimeLogger)m_ctxGroup : (ITimeLogger)m_ctxEntry);
                    Debug.Assert(tl != null);

                    if(xr.Name == ElemLastModTime)
                        tl.LastModificationTime = ReadTime(xr);
                    else if(xr.Name == ElemCreationTime)
                        tl.CreationTime = ReadTime(xr);
                    else if(xr.Name == ElemLastAccessTime)
                        tl.LastAccessTime = ReadTime(xr);
                    else if(xr.Name == ElemExpiryTime)
                        tl.ExpiryTime = ReadTime(xr);
                    else if(xr.Name == ElemExpires)
                        tl.Expires = ReadBool(xr, false);
                    else if(xr.Name == ElemUsageCount)
                        tl.UsageCount = ReadULong(xr, 0);
                    else ReadUnknown(xr);
                    break;

                case KdbContext.EntryString:
                    if(xr.Name == ElemKey)
                        m_ctxStringName = ReadString(xr);
                    else if(xr.Name == ElemValue)
                        m_ctxStringValue = ReadProtectedString(xr);
                    else ReadUnknown(xr);
                    break;

                case KdbContext.EntryBinary:
                    if(xr.Name == ElemKey)
                        m_ctxBinaryName = ReadString(xr);
                    else if(xr.Name == ElemValue)
                        m_ctxBinaryValue = ReadProtectedBinary(xr);
                    else ReadUnknown(xr);
                    break;

                case KdbContext.EntryAutoType:
                    if(xr.Name == ElemAutoTypeEnabled)
                        m_ctxEntry.AutoType.Enabled = ReadBool(xr, true);
                    else if(xr.Name == ElemAutoTypeObfuscation)
                        m_ctxEntry.AutoType.ObfuscationOptions =
                            (AutoTypeObfuscationOptions)ReadUInt(xr, 0);
                    else if(xr.Name == ElemAutoTypeDefaultSeq)
                        m_ctxEntry.AutoType.DefaultSequence = ReadString(xr);
                    else if(xr.Name == ElemAutoTypeItem)
                        return SwitchContext(ctx, KdbContext.EntryAutoTypeItem, xr);
                    else ReadUnknown(xr);
                    break;

                case KdbContext.EntryAutoTypeItem:
                    if(xr.Name == ElemWindow)
                        m_ctxATName = ReadString(xr);
                    else if(xr.Name == ElemKeystrokeSequence)
                        m_ctxATSeq = ReadString(xr);
                    else ReadUnknown(xr);
                    break;

                case KdbContext.EntryHistory:
                    if(xr.Name == ElemEntry)
                    {
                        m_ctxEntry = new PwEntry(false, false);
                        m_ctxHistoryBase.History.Add(m_ctxEntry);

                        m_bEntryInHistory = true;
                        return SwitchContext(ctx, KdbContext.Entry, xr);
                    }
                    else ReadUnknown(xr);
                    break;

                case KdbContext.RootDeletedObjects:
                    if(xr.Name == ElemDeletedObject)
                    {
                        m_ctxDeletedObject = new PwDeletedObject();
                        m_pwDatabase.DeletedObjects.Add(m_ctxDeletedObject);

                        return SwitchContext(ctx, KdbContext.DeletedObject, xr);
                    }
                    else ReadUnknown(xr);
                    break;

                case KdbContext.DeletedObject:
                    if(xr.Name == ElemUuid)
                        m_ctxDeletedObject.Uuid = ReadUuid(xr);
                    else if(xr.Name == ElemDeletionTime)
                        m_ctxDeletedObject.DeletionTime = ReadTime(xr);
                    else ReadUnknown(xr);
                    break;

                default:
                    ReadUnknown(xr);
                    break;
            }

            return ctx;
        }
Пример #21
0
        private KdbContext EndXmlElement(KdbContext ctx, XmlReader xr)
        {
            Debug.Assert(xr.NodeType == XmlNodeType.EndElement);

            if ((ctx == KdbContext.KeePassFile) && (xr.Name == ElemDocNode))
            {
                return(KdbContext.Null);
            }
            else if ((ctx == KdbContext.Meta) && (xr.Name == ElemMeta))
            {
                return(KdbContext.KeePassFile);
            }
            else if ((ctx == KdbContext.Root) && (xr.Name == ElemRoot))
            {
                return(KdbContext.KeePassFile);
            }
            else if ((ctx == KdbContext.MemoryProtection) && (xr.Name == ElemMemoryProt))
            {
                return(KdbContext.Meta);
            }
            else if ((ctx == KdbContext.CustomIcons) && (xr.Name == ElemCustomIcons))
            {
                return(KdbContext.Meta);
            }
            else if ((ctx == KdbContext.CustomIcon) && (xr.Name == ElemCustomIconItem))
            {
                if (!m_uuidCustomIconID.Equals(PwUuid.Zero) &&
                    (m_pbCustomIconData != null))
                {
                    m_pwDatabase.CustomIcons.Add(new PwCustomIcon(
                                                     m_uuidCustomIconID, m_pbCustomIconData));
                }
                else
                {
                    Debug.Assert(false);
                }

                m_uuidCustomIconID = PwUuid.Zero;
                m_pbCustomIconData = null;

                return(KdbContext.CustomIcons);
            }
            else if ((ctx == KdbContext.Binaries) && (xr.Name == ElemBinaries))
            {
                return(KdbContext.Meta);
            }
            else if ((ctx == KdbContext.CustomData) && (xr.Name == ElemCustomData))
            {
                return(KdbContext.Meta);
            }
            else if ((ctx == KdbContext.CustomDataItem) && (xr.Name == ElemStringDictExItem))
            {
                if ((m_strCustomDataKey != null) && (m_strCustomDataValue != null))
                {
                    m_pwDatabase.CustomData.Set(m_strCustomDataKey, m_strCustomDataValue);
                }
                else
                {
                    Debug.Assert(false);
                }

                m_strCustomDataKey   = null;
                m_strCustomDataValue = null;

                return(KdbContext.CustomData);
            }
            else if ((ctx == KdbContext.Group) && (xr.Name == ElemGroup))
            {
                if (PwUuid.Zero.Equals(m_ctxGroup.Uuid))
                {
                    m_ctxGroup.Uuid = new PwUuid(true);                     // No assert (import)
                }
                m_ctxGroups.Pop();

                if (m_ctxGroups.Count == 0)
                {
                    m_ctxGroup = null;
                    return(KdbContext.Root);
                }
                else
                {
                    m_ctxGroup = m_ctxGroups.Peek();
                    return(KdbContext.Group);
                }
            }
            else if ((ctx == KdbContext.GroupTimes) && (xr.Name == ElemTimes))
            {
                return(KdbContext.Group);
            }
            else if ((ctx == KdbContext.GroupCustomData) && (xr.Name == ElemCustomData))
            {
                return(KdbContext.Group);
            }
            else if ((ctx == KdbContext.GroupCustomDataItem) && (xr.Name == ElemStringDictExItem))
            {
                if ((m_strGroupCustomDataKey != null) && (m_strGroupCustomDataValue != null))
                {
                    m_ctxGroup.CustomData.Set(m_strGroupCustomDataKey, m_strGroupCustomDataValue);
                }
                else
                {
                    Debug.Assert(false);
                }

                m_strGroupCustomDataKey   = null;
                m_strGroupCustomDataValue = null;

                return(KdbContext.GroupCustomData);
            }
            else if ((ctx == KdbContext.Entry) && (xr.Name == ElemEntry))
            {
                // Create new UUID if absent
                if (PwUuid.Zero.Equals(m_ctxEntry.Uuid))
                {
                    m_ctxEntry.Uuid = new PwUuid(true);                     // No assert (import)
                }
                if (m_bEntryInHistory)
                {
                    m_ctxEntry = m_ctxHistoryBase;
                    return(KdbContext.EntryHistory);
                }

                return(KdbContext.Group);
            }
            else if ((ctx == KdbContext.EntryTimes) && (xr.Name == ElemTimes))
            {
                return(KdbContext.Entry);
            }
            else if ((ctx == KdbContext.EntryString) && (xr.Name == ElemString))
            {
                m_ctxEntry.Strings.Set(m_ctxStringName, m_ctxStringValue);
                m_ctxStringName  = null;
                m_ctxStringValue = null;
                return(KdbContext.Entry);
            }
            else if ((ctx == KdbContext.EntryBinary) && (xr.Name == ElemBinary))
            {
                if (string.IsNullOrEmpty(m_strDetachBins))
                {
                    m_ctxEntry.Binaries.Set(m_ctxBinaryName, m_ctxBinaryValue);
                }
                else
                {
                    SaveBinary(m_ctxBinaryName, m_ctxBinaryValue, m_strDetachBins);

                    m_ctxBinaryValue = null;
                    GC.Collect();
                }

                m_ctxBinaryName  = null;
                m_ctxBinaryValue = null;
                return(KdbContext.Entry);
            }
            else if ((ctx == KdbContext.EntryAutoType) && (xr.Name == ElemAutoType))
            {
                return(KdbContext.Entry);
            }
            else if ((ctx == KdbContext.EntryAutoTypeItem) && (xr.Name == ElemAutoTypeItem))
            {
                AutoTypeAssociation atAssoc = new AutoTypeAssociation(m_ctxATName,
                                                                      m_ctxATSeq);
                m_ctxEntry.AutoType.Add(atAssoc);
                m_ctxATName = null;
                m_ctxATSeq  = null;
                return(KdbContext.EntryAutoType);
            }
            else if ((ctx == KdbContext.EntryCustomData) && (xr.Name == ElemCustomData))
            {
                return(KdbContext.Entry);
            }
            else if ((ctx == KdbContext.EntryCustomDataItem) && (xr.Name == ElemStringDictExItem))
            {
                if ((m_strEntryCustomDataKey != null) && (m_strEntryCustomDataValue != null))
                {
                    m_ctxEntry.CustomData.Set(m_strEntryCustomDataKey, m_strEntryCustomDataValue);
                }
                else
                {
                    Debug.Assert(false);
                }

                m_strEntryCustomDataKey   = null;
                m_strEntryCustomDataValue = null;

                return(KdbContext.EntryCustomData);
            }
            else if ((ctx == KdbContext.EntryHistory) && (xr.Name == ElemHistory))
            {
                m_bEntryInHistory = false;
                return(KdbContext.Entry);
            }
            else if ((ctx == KdbContext.RootDeletedObjects) && (xr.Name == ElemDeletedObjects))
            {
                return(KdbContext.Root);
            }
            else if ((ctx == KdbContext.DeletedObject) && (xr.Name == ElemDeletedObject))
            {
                m_ctxDeletedObject = null;
                return(KdbContext.RootDeletedObjects);
            }
            else
            {
                Debug.Assert(false);
                throw new FormatException();
            }
        }
		private KdbContext ReadXmlElement(KdbContext ctx, XmlReader xr)
		{
			Debug.Assert(xr.NodeType == XmlNodeType.Element);

			switch(ctx)
			{
				case KdbContext.Null:
					if(xr.Name == ElemDocNode)
						return SwitchContext(ctx, KdbContext.KeePassFile, xr);
					else ReadUnknown(xr);
					break;

				case KdbContext.KeePassFile:
					if(xr.Name == ElemMeta)
						return SwitchContext(ctx, KdbContext.Meta, xr);
					else if(xr.Name == ElemRoot)
						return SwitchContext(ctx, KdbContext.Root, xr);
					else ReadUnknown(xr);
					break;

				case KdbContext.Meta:
					if(xr.Name == ElemGenerator)
						ReadString(xr); // Ignore
					else if(xr.Name == ElemHeaderHash)
					{
						string strHash = ReadString(xr);
						if(!string.IsNullOrEmpty(strHash) && (m_pbHashOfHeader != null) &&
							!m_bRepairMode)
						{
							byte[] pbHash = Convert.FromBase64String(strHash);
							if(!MemUtil.ArraysEqual(pbHash, m_pbHashOfHeader))
								throw new IOException(KLRes.FileCorrupted);
						}
					}
					else if(xr.Name == ElemDbName)
						m_pwDatabase.Name = ReadString(xr);
					else if(xr.Name == ElemDbNameChanged)
						m_pwDatabase.NameChanged = ReadTime(xr);
					else if(xr.Name == ElemDbDesc)
						m_pwDatabase.Description = ReadString(xr);
					else if(xr.Name == ElemDbDescChanged)
						m_pwDatabase.DescriptionChanged = ReadTime(xr);
					else if(xr.Name == ElemDbDefaultUser)
						m_pwDatabase.DefaultUserName = ReadString(xr);
					else if(xr.Name == ElemDbDefaultUserChanged)
						m_pwDatabase.DefaultUserNameChanged = ReadTime(xr);
					else if(xr.Name == ElemDbMntncHistoryDays)
						m_pwDatabase.MaintenanceHistoryDays = ReadUInt(xr, 365);
					else if(xr.Name == ElemDbColor)
					{
						string strColor = ReadString(xr);
						if(!string.IsNullOrEmpty(strColor))
							m_pwDatabase.Color = ColorTranslator.FromHtml(strColor);
					}
					else if(xr.Name == ElemDbKeyChanged)
						m_pwDatabase.MasterKeyChanged = ReadTime(xr);
					else if(xr.Name == ElemDbKeyChangeRec)
						m_pwDatabase.MasterKeyChangeRec = ReadLong(xr, -1);
					else if(xr.Name == ElemDbKeyChangeForce)
						m_pwDatabase.MasterKeyChangeForce = ReadLong(xr, -1);
					else if(xr.Name == ElemMemoryProt)
						return SwitchContext(ctx, KdbContext.MemoryProtection, xr);
					else if(xr.Name == ElemCustomIcons)
						return SwitchContext(ctx, KdbContext.CustomIcons, xr);
					else if(xr.Name == ElemRecycleBinEnabled)
						m_pwDatabase.RecycleBinEnabled = ReadBool(xr, true);
					else if(xr.Name == ElemRecycleBinUuid)
						m_pwDatabase.RecycleBinUuid = ReadUuid(xr);
					else if(xr.Name == ElemRecycleBinChanged)
						m_pwDatabase.RecycleBinChanged = ReadTime(xr);
					else if(xr.Name == ElemEntryTemplatesGroup)
						m_pwDatabase.EntryTemplatesGroup = ReadUuid(xr);
					else if(xr.Name == ElemEntryTemplatesGroupChanged)
						m_pwDatabase.EntryTemplatesGroupChanged = ReadTime(xr);
					else if(xr.Name == ElemHistoryMaxItems)
						m_pwDatabase.HistoryMaxItems = ReadInt(xr, -1);
					else if(xr.Name == ElemHistoryMaxSize)
						m_pwDatabase.HistoryMaxSize = ReadLong(xr, -1);
					else if(xr.Name == ElemLastSelectedGroup)
						m_pwDatabase.LastSelectedGroup = ReadUuid(xr);
					else if(xr.Name == ElemLastTopVisibleGroup)
						m_pwDatabase.LastTopVisibleGroup = ReadUuid(xr);
					else if(xr.Name == ElemBinaries)
						return SwitchContext(ctx, KdbContext.Binaries, xr);
					else if(xr.Name == ElemCustomData)
						return SwitchContext(ctx, KdbContext.CustomData, xr);
					else ReadUnknown(xr);
					break;

				case KdbContext.MemoryProtection:
					if(xr.Name == ElemProtTitle)
						m_pwDatabase.MemoryProtection.ProtectTitle = ReadBool(xr, false);
					else if(xr.Name == ElemProtUserName)
						m_pwDatabase.MemoryProtection.ProtectUserName = ReadBool(xr, false);
					else if(xr.Name == ElemProtPassword)
						m_pwDatabase.MemoryProtection.ProtectPassword = ReadBool(xr, true);
					else if(xr.Name == ElemProtUrl)
						m_pwDatabase.MemoryProtection.ProtectUrl = ReadBool(xr, false);
					else if(xr.Name == ElemProtNotes)
						m_pwDatabase.MemoryProtection.ProtectNotes = ReadBool(xr, false);
					// else if(xr.Name == ElemProtAutoHide)
					//	m_pwDatabase.MemoryProtection.AutoEnableVisualHiding = ReadBool(xr, true);
					else ReadUnknown(xr);
					break;

				case KdbContext.CustomIcons:
					if(xr.Name == ElemCustomIconItem)
						return SwitchContext(ctx, KdbContext.CustomIcon, xr);
					else ReadUnknown(xr);
					break;

				case KdbContext.CustomIcon:
					if(xr.Name == ElemCustomIconItemID)
						m_uuidCustomIconID = ReadUuid(xr);
					else if(xr.Name == ElemCustomIconItemData)
					{
						string strData = ReadString(xr);
						if(!string.IsNullOrEmpty(strData))
							m_pbCustomIconData = Convert.FromBase64String(strData);
						else { Debug.Assert(false); }
					}
					else ReadUnknown(xr);
					break;

				case KdbContext.Binaries:
					if(xr.Name == ElemBinary)
					{
						if(xr.MoveToAttribute(AttrId))
						{
							string strKey = xr.Value;
							ProtectedBinary pbData = ReadProtectedBinary(xr);

							m_dictBinPool[strKey ?? string.Empty] = pbData;
						}
						else ReadUnknown(xr);
					}
					else ReadUnknown(xr);
					break;

				case KdbContext.CustomData:
					if(xr.Name == ElemStringDictExItem)
						return SwitchContext(ctx, KdbContext.CustomDataItem, xr);
					else ReadUnknown(xr);
					break;

				case KdbContext.CustomDataItem:
					if(xr.Name == ElemKey)
						m_strCustomDataKey = ReadString(xr);
					else if(xr.Name == ElemValue)
						m_strCustomDataValue = ReadString(xr);
					else ReadUnknown(xr);
					break;

				case KdbContext.Root:
					if(xr.Name == ElemGroup)
					{
						Debug.Assert(m_ctxGroups.Count == 0);
						if(m_ctxGroups.Count != 0) throw new FormatException();

						m_pwDatabase.RootGroup = new PwGroup(false, false);
						m_ctxGroups.Push(m_pwDatabase.RootGroup);
						m_ctxGroup = m_ctxGroups.Peek();

						return SwitchContext(ctx, KdbContext.Group, xr);
					}
					else if(xr.Name == ElemDeletedObjects)
						return SwitchContext(ctx, KdbContext.RootDeletedObjects, xr);
					else ReadUnknown(xr);
					break;

				case KdbContext.Group:
					if(xr.Name == ElemUuid)
						m_ctxGroup.Uuid = ReadUuid(xr);
					else if(xr.Name == ElemName)
						m_ctxGroup.Name = ReadString(xr);
					else if(xr.Name == ElemNotes)
						m_ctxGroup.Notes = ReadString(xr);
					else if(xr.Name == ElemIcon)
						m_ctxGroup.IconId = (PwIcon)ReadInt(xr, (int)PwIcon.Folder);
					else if(xr.Name == ElemCustomIconID)
						m_ctxGroup.CustomIconUuid = ReadUuid(xr);
					else if(xr.Name == ElemTimes)
						return SwitchContext(ctx, KdbContext.GroupTimes, xr);
					else if(xr.Name == ElemIsExpanded)
						m_ctxGroup.IsExpanded = ReadBool(xr, true);
					else if(xr.Name == ElemGroupDefaultAutoTypeSeq)
						m_ctxGroup.DefaultAutoTypeSequence = ReadString(xr);
					else if(xr.Name == ElemEnableAutoType)
						m_ctxGroup.EnableAutoType = StrUtil.StringToBoolEx(ReadString(xr));
					else if(xr.Name == ElemEnableSearching)
						m_ctxGroup.EnableSearching = StrUtil.StringToBoolEx(ReadString(xr));
					else if(xr.Name == ElemLastTopVisibleEntry)
						m_ctxGroup.LastTopVisibleEntry = ReadUuid(xr);
					else if(xr.Name == ElemGroup)
					{
						m_ctxGroup = new PwGroup(false, false);
						m_ctxGroups.Peek().AddGroup(m_ctxGroup, true);

						m_ctxGroups.Push(m_ctxGroup);

						return SwitchContext(ctx, KdbContext.Group, xr);
					}
					else if(xr.Name == ElemEntry)
					{
						m_ctxEntry = new PwEntry(false, false);
						m_ctxGroup.AddEntry(m_ctxEntry, true);

						m_bEntryInHistory = false;
						return SwitchContext(ctx, KdbContext.Entry, xr);
					}
					else ReadUnknown(xr);
					break;

				case KdbContext.Entry:
					if(xr.Name == ElemUuid)
						m_ctxEntry.Uuid = ReadUuid(xr);
					else if(xr.Name == ElemIcon)
						m_ctxEntry.IconId = (PwIcon)ReadInt(xr, (int)PwIcon.Key);
					else if(xr.Name == ElemCustomIconID)
						m_ctxEntry.CustomIconUuid = ReadUuid(xr);
					else if(xr.Name == ElemFgColor)
					{
						string strColor = ReadString(xr);
						if(!string.IsNullOrEmpty(strColor))
							m_ctxEntry.ForegroundColor = ColorTranslator.FromHtml(strColor);
					}
					else if(xr.Name == ElemBgColor)
					{
						string strColor = ReadString(xr);
						if(!string.IsNullOrEmpty(strColor))
							m_ctxEntry.BackgroundColor = ColorTranslator.FromHtml(strColor);
					}
					else if(xr.Name == ElemOverrideUrl)
						m_ctxEntry.OverrideUrl = ReadString(xr);
					else if(xr.Name == ElemTags)
						m_ctxEntry.Tags = StrUtil.StringToTags(ReadString(xr));
					else if(xr.Name == ElemTimes)
						return SwitchContext(ctx, KdbContext.EntryTimes, xr);
					else if(xr.Name == ElemString)
						return SwitchContext(ctx, KdbContext.EntryString, xr);
					else if(xr.Name == ElemBinary)
						return SwitchContext(ctx, KdbContext.EntryBinary, xr);
					else if(xr.Name == ElemAutoType)
						return SwitchContext(ctx, KdbContext.EntryAutoType, xr);
					else if(xr.Name == ElemHistory)
					{
						Debug.Assert(m_bEntryInHistory == false);

						if(m_bEntryInHistory == false)
						{
							m_ctxHistoryBase = m_ctxEntry;
							return SwitchContext(ctx, KdbContext.EntryHistory, xr);
						}
						else ReadUnknown(xr);
					}
					else ReadUnknown(xr);
					break;

				case KdbContext.GroupTimes:
				case KdbContext.EntryTimes:
					ITimeLogger tl = ((ctx == KdbContext.GroupTimes) ?
						(ITimeLogger)m_ctxGroup : (ITimeLogger)m_ctxEntry);
					Debug.Assert(tl != null);

					if(xr.Name == ElemCreationTime)
						tl.CreationTime = ReadTime(xr);
					else if(xr.Name == ElemLastModTime)
						tl.LastModificationTime = ReadTime(xr);
					else if(xr.Name == ElemLastAccessTime)
						tl.LastAccessTime = ReadTime(xr);
					else if(xr.Name == ElemExpiryTime)
						tl.ExpiryTime = ReadTime(xr);
					else if(xr.Name == ElemExpires)
						tl.Expires = ReadBool(xr, false);
					else if(xr.Name == ElemUsageCount)
						tl.UsageCount = ReadULong(xr, 0);
					else if(xr.Name == ElemLocationChanged)
						tl.LocationChanged = ReadTime(xr);
					else ReadUnknown(xr);
					break;

				case KdbContext.EntryString:
					if(xr.Name == ElemKey)
						m_ctxStringName = ReadString(xr);
					else if(xr.Name == ElemValue)
						m_ctxStringValue = ReadProtectedString(xr);
					else ReadUnknown(xr);
					break;

				case KdbContext.EntryBinary:
					if(xr.Name == ElemKey)
						m_ctxBinaryName = ReadString(xr);
					else if(xr.Name == ElemValue)
						m_ctxBinaryValue = ReadProtectedBinary(xr);
					else ReadUnknown(xr);
					break;

				case KdbContext.EntryAutoType:
					if(xr.Name == ElemAutoTypeEnabled)
						m_ctxEntry.AutoType.Enabled = ReadBool(xr, true);
					else if(xr.Name == ElemAutoTypeObfuscation)
						m_ctxEntry.AutoType.ObfuscationOptions =
							(AutoTypeObfuscationOptions)ReadInt(xr, 0);
					else if(xr.Name == ElemAutoTypeDefaultSeq)
						m_ctxEntry.AutoType.DefaultSequence = ReadString(xr);
					else if(xr.Name == ElemAutoTypeItem)
						return SwitchContext(ctx, KdbContext.EntryAutoTypeItem, xr);
					else ReadUnknown(xr);
					break;

				case KdbContext.EntryAutoTypeItem:
					if(xr.Name == ElemWindow)
						m_ctxATName = ReadString(xr);
					else if(xr.Name == ElemKeystrokeSequence)
						m_ctxATSeq = ReadString(xr);
					else ReadUnknown(xr);
					break;

				case KdbContext.EntryHistory:
					if(xr.Name == ElemEntry)
					{
						m_ctxEntry = new PwEntry(false, false);
						m_ctxHistoryBase.History.Add(m_ctxEntry);

						m_bEntryInHistory = true;
						return SwitchContext(ctx, KdbContext.Entry, xr);
					}
					else ReadUnknown(xr);
					break;

				case KdbContext.RootDeletedObjects:
					if(xr.Name == ElemDeletedObject)
					{
						m_ctxDeletedObject = new PwDeletedObject();
						m_pwDatabase.DeletedObjects.Add(m_ctxDeletedObject);

						return SwitchContext(ctx, KdbContext.DeletedObject, xr);
					}
					else ReadUnknown(xr);
					break;

				case KdbContext.DeletedObject:
					if(xr.Name == ElemUuid)
						m_ctxDeletedObject.Uuid = ReadUuid(xr);
					else if(xr.Name == ElemDeletionTime)
						m_ctxDeletedObject.DeletionTime = ReadTime(xr);
					else ReadUnknown(xr);
					break;

				default:
					ReadUnknown(xr);
					break;
			}

			return ctx;
		}
Пример #23
0
        private void OnEntryDelete(object sender, EventArgs e)
        {
            PwEntry[] vSelected = GetSelectedEntries();
            if((vSelected == null) || (vSelected.Length == 0)) return;

            string strText = KPRes.DeleteEntriesInfo + MessageService.NewParagraph +
                KPRes.DeleteEntriesQuestion;
            if(!MessageService.AskYesNo(strText, KPRes.DeleteEntriesCaption))
                return;

            DateTime dtNow = DateTime.Now;
            foreach(PwEntry pe in vSelected)
            {
                if(pe.ParentGroup == null) continue; // Can't remove

                pe.ParentGroup.Entries.Remove(pe);

                PwDeletedObject pdo = new PwDeletedObject();
                pdo.Uuid = pe.Uuid;
                pdo.DeletionTime = dtNow;
                m_docMgr.ActiveDatabase.DeletedObjects.Add(pdo);
            }

            RemoveEntriesFromList(vSelected, true);
            UpdateUI(false, null, false, null, false, null, true);
        }
Пример #24
0
        private void DeleteSelectedEntries()
        {
            PwEntry[] vSelected = GetSelectedEntries();
            if((vSelected == null) || (vSelected.Length == 0)) return;

            PwDatabase pd = m_docMgr.ActiveDatabase;
            PwGroup pgRecycleBin = pd.RootGroup.FindGroup(pd.RecycleBinUuid, true);
            bool bShiftPressed = ((Control.ModifierKeys & Keys.Shift) != Keys.None);

            bool bAtLeastOnePermanent = false;
            if(pd.RecycleBinEnabled == false) bAtLeastOnePermanent = true;
            else if(bShiftPressed) bAtLeastOnePermanent = true;
            else if(pgRecycleBin == null) { } // Not permanent
            else
            {
                foreach(PwEntry peEnum in vSelected)
                {
                    if((peEnum.ParentGroup == pgRecycleBin) ||
                        peEnum.ParentGroup.IsContainedIn(pgRecycleBin))
                    {
                        bAtLeastOnePermanent = true;
                        break;
                    }
                }
            }

            if(bAtLeastOnePermanent)
            {
                bool bSingle = (vSelected.Length == 1);

                int nSummaryShow = Math.Min(10, vSelected.Length);
                if(nSummaryShow == (vSelected.Length - 1)) --nSummaryShow; // Plural msg
                string strSummary = string.Empty;
                for(int iSumEnum = 0; iSumEnum < nSummaryShow; ++iSumEnum)
                {
                    if(strSummary.Length > 0) strSummary += MessageService.NewLine;

                    PwEntry peDel = vSelected[iSumEnum];
                    strSummary += ("- " + StrUtil.CompactString3Dots(
                        peDel.Strings.ReadSafe(PwDefs.TitleField), 39));
                    if(PwDefs.IsTanEntry(peDel))
                    {
                        string strTanIdx = peDel.Strings.ReadSafe(PwDefs.UserNameField);
                        if(!string.IsNullOrEmpty(strTanIdx))
                            strSummary += (@" (#" + strTanIdx + @")");
                    }
                }
                if(nSummaryShow != vSelected.Length)
                    strSummary += (MessageService.NewLine + "- " +
                        KPRes.MoreEntries.Replace(@"{PARAM}", (vSelected.Length -
                        nSummaryShow).ToString()));

                VistaTaskDialog dlg = new VistaTaskDialog(this.Handle);
                dlg.CommandLinks = false;
                dlg.Content = strSummary;
                dlg.MainInstruction = (bSingle ? KPRes.DeleteEntriesQuestionSingle :
                    KPRes.DeleteEntriesQuestion);
                dlg.SetIcon(VtdCustomIcon.Question);
                dlg.WindowTitle = PwDefs.ProductName;
                dlg.AddButton((int)DialogResult.OK, KPRes.DeleteCmd, null);
                dlg.AddButton((int)DialogResult.Cancel, KPRes.DontDeleteCmd, null);

                if(dlg.ShowDialog())
                {
                    if(dlg.Result == (int)DialogResult.Cancel) return;
                }
                else
                {
                    if(!MessageService.AskYesNo(bSingle ? KPRes.DeleteEntriesQuestionSingle :
                        KPRes.DeleteEntriesQuestion, bSingle ? KPRes.DeleteEntriesTitleSingle :
                        KPRes.DeleteEntriesTitle))
                        return;
                }
            }

            bool bUpdateGroupList = false;
            DateTime dtNow = DateTime.Now;
            foreach(PwEntry pe in vSelected)
            {
                PwGroup pgParent = pe.ParentGroup;
                if(pgParent == null) continue; // Can't remove

                pgParent.Entries.Remove(pe);

                bool bPermanent = false;
                if(pd.RecycleBinEnabled == false) bPermanent = true;
                else if(bShiftPressed) bPermanent = true;
                else if(pgRecycleBin == null) { } // Recycle
                else if(pgParent == pgRecycleBin) bPermanent = true;
                else if(pgParent.IsContainedIn(pgRecycleBin)) bPermanent = true;

                if(bPermanent)
                {
                    PwDeletedObject pdo = new PwDeletedObject();
                    pdo.Uuid = pe.Uuid;
                    pdo.DeletionTime = dtNow;
                    pd.DeletedObjects.Add(pdo);
                }
                else // Recycle
                {
                    EnsureRecycleBin(ref pgRecycleBin, pd, ref bUpdateGroupList);

                    pgRecycleBin.AddEntry(pe, true, true);
                    pe.Touch(false);
                }
            }

            RemoveEntriesFromList(vSelected, true);
            UpdateUI(false, null, bUpdateGroupList, null, false, null, true);
        }
 public static void DeleteFrom(this PwEntry entry, PwGroup parent, PwDatabase database = null)
 {
     if (database != null)
     {
         // create the delete marker
         PwDeletedObject deleteMarker = new PwDeletedObject(entry.Uuid, DateTime.Now);
         database.DeletedObjects.Add(deleteMarker);
     }
     // TODO CK: Potential memory leak? are there dangling references?
     var success = parent.Entries.Remove(entry);
     Debug.Assert(success);
 }
Пример #26
0
		private void DeleteSelectedGroup()
		{
			PwGroup pg = GetSelectedGroup();
			if(pg == null) { Debug.Assert(false); return; }

			PwGroup pgParent = pg.ParentGroup;
			if(pgParent == null) return; // Can't remove virtual or root group

			PwDatabase pd = m_docMgr.ActiveDatabase;
			PwGroup pgRecycleBin = pd.RootGroup.FindGroup(pd.RecycleBinUuid, true);
			bool bShiftPressed = ((Control.ModifierKeys & Keys.Shift) != Keys.None);

			bool bPermanent = false;
			if(pd.RecycleBinEnabled == false) bPermanent = true;
			else if(bShiftPressed) bPermanent = true;
			else if(pgRecycleBin == null) { }
			else if(pg == pgRecycleBin) bPermanent = true;
			else if(pg.IsContainedIn(pgRecycleBin)) bPermanent = true;
			else if(pgRecycleBin.IsContainedIn(pg)) bPermanent = true;

			string strContent = EntryUtil.CreateSummaryList(pg, false);
			if(strContent.Length > 0)
				strContent = KPRes.DeleteGroupInfo + MessageService.NewParagraph +
					strContent;

			if(bPermanent)
			{
				VistaTaskDialog dlg = new VistaTaskDialog();
				dlg.CommandLinks = false;
				dlg.Content = strContent;
				dlg.MainInstruction = KPRes.DeleteGroupQuestion;
				dlg.SetIcon(VtdCustomIcon.Question);
				dlg.WindowTitle = PwDefs.ShortProductName;
				dlg.AddButton((int)DialogResult.OK, KPRes.DeleteCmd, null);
				dlg.AddButton((int)DialogResult.Cancel, KPRes.Cancel, null);

				if(dlg.ShowDialog())
				{
					if(dlg.Result == (int)DialogResult.Cancel) return;
				}
				else
				{
					string strText = KPRes.DeleteGroupQuestion;
					if(strContent.Length > 0)
						strText += MessageService.NewParagraph + strContent;

					if(!MessageService.AskYesNo(strText, KPRes.DeleteGroupTitle))
						return;
				}
			}
			else if(Program.Config.UI.ShowRecycleConfirmDialog)
			{
				VistaTaskDialog dlg = new VistaTaskDialog();
				dlg.CommandLinks = false;
				dlg.Content = strContent;
				dlg.MainInstruction = KPRes.RecycleGroupConfirm;
				dlg.SetIcon(VtdCustomIcon.Question);
				dlg.VerificationText = KPRes.DialogNoShowAgain;
				dlg.WindowTitle = PwDefs.ShortProductName;
				dlg.AddButton((int)DialogResult.OK, KPRes.YesCmd, null);
				dlg.AddButton((int)DialogResult.Cancel, KPRes.NoCmd, null);

				if(dlg.ShowDialog())
				{
					if(dlg.Result == (int)DialogResult.Cancel) return;

					if(dlg.ResultVerificationChecked)
						Program.Config.UI.ShowRecycleConfirmDialog = false;
				}
				else
				{
					string strText = KPRes.RecycleGroupConfirm;
					if(strContent.Length > 0)
						strText += MessageService.NewParagraph + strContent;

					if(!MessageService.AskYesNo(strText, KPRes.DeleteGroupTitle))
						return;
				}
			}

			pgParent.Groups.Remove(pg);

			if(bPermanent)
			{
				pg.DeleteAllObjects(pd);

				PwDeletedObject pdo = new PwDeletedObject(pg.Uuid, DateTime.Now);
				pd.DeletedObjects.Add(pdo);
			}
			else // Recycle
			{
				bool bDummy = false;
				EnsureRecycleBin(ref pgRecycleBin, pd, ref bDummy);

				pgRecycleBin.AddGroup(pg, true, true);
				pg.Touch(false);
			}

			UpdateUI(false, null, true, pgParent, true, null, true);
		}
Пример #27
0
        private void DeleteSelectedGroup()
        {
            PwGroup pg = GetSelectedGroup();
            if(pg == null) { Debug.Assert(false); return; }

            PwGroup pgParent = pg.ParentGroup;
            if(pgParent == null) return; // Can't remove virtual or root group

            PwDatabase pd = m_docMgr.ActiveDatabase;
            PwGroup pgRecycleBin = pd.RootGroup.FindGroup(pd.RecycleBinUuid, true);
            bool bShiftPressed = ((Control.ModifierKeys & Keys.Shift) != Keys.None);

            bool bPermanent = false;
            if(pd.RecycleBinEnabled == false) bPermanent = true;
            else if(bShiftPressed) bPermanent = true;
            else if(pgRecycleBin == null) { }
            else if(pg == pgRecycleBin) bPermanent = true;
            else if(pg.IsContainedIn(pgRecycleBin)) bPermanent = true;
            else if(pgRecycleBin.IsContainedIn(pg)) bPermanent = true;

            if(bPermanent)
            {
                VistaTaskDialog dlg = new VistaTaskDialog(this.Handle);
                dlg.CommandLinks = false;
                dlg.Content = KPRes.DeleteGroupInfo;
                dlg.MainInstruction = KPRes.DeleteGroupQuestion;
                dlg.SetIcon(VtdCustomIcon.Question);
                dlg.WindowTitle = PwDefs.ProductName;
                dlg.AddButton((int)DialogResult.OK, KPRes.DeleteCmd, null);
                dlg.AddButton((int)DialogResult.Cancel, KPRes.DontDeleteCmd, null);

                if(dlg.ShowDialog())
                {
                    if(dlg.Result == (int)DialogResult.Cancel) return;
                }
                else
                {
                    string strText = KPRes.DeleteGroupInfo + MessageService.NewParagraph +
                        KPRes.DeleteGroupQuestion;
                    if(!MessageService.AskYesNo(strText, KPRes.DeleteGroupTitle))
                        return;
                }
            }

            pgParent.Groups.Remove(pg);

            if(bPermanent)
            {
                PwDeletedObject pdo = new PwDeletedObject();
                pdo.Uuid = pg.Uuid;
                pdo.DeletionTime = DateTime.Now;
                pd.DeletedObjects.Add(pdo);
            }
            else // Recycle
            {
                bool bDummy = false;
                EnsureRecycleBin(ref pgRecycleBin, pd, ref bDummy);

                pgRecycleBin.AddGroup(pg, true, true);
                pg.Touch(false);
            }

            UpdateUI(false, null, true, pgParent, true, null, true);
        }
Пример #28
0
        private KdbContext ReadXmlElement(KdbContext ctx, XmlReader xr)
        {
            Debug.Assert(xr.NodeType == XmlNodeType.Element);

            switch (ctx)
            {
            case KdbContext.Null:
                if (xr.Name == ElemDocNode)
                {
                    return(SwitchContext(ctx, KdbContext.KeePassFile, xr));
                }
                else
                {
                    ReadUnknown(xr);
                }
                break;

            case KdbContext.KeePassFile:
                if (xr.Name == ElemMeta)
                {
                    return(SwitchContext(ctx, KdbContext.Meta, xr));
                }
                else if (xr.Name == ElemRoot)
                {
                    return(SwitchContext(ctx, KdbContext.Root, xr));
                }
                else
                {
                    ReadUnknown(xr);
                }
                break;

            case KdbContext.Meta:
                if (xr.Name == ElemGenerator)
                {
                    ReadString(xr);                             // Ignore
                }
                else if (xr.Name == ElemHeaderHash)
                {
                    // The header hash is typically only stored in
                    // KDBX <= 3.1 files, not in KDBX >= 4 files
                    // (here, the header is verified via a HMAC),
                    // but we also support it for KDBX >= 4 files
                    // (i.e. if it's present, we check it)

                    string strHash = ReadString(xr);
                    if (!string.IsNullOrEmpty(strHash) && (m_pbHashOfHeader != null) &&
                        !m_bRepairMode)
                    {
                        Debug.Assert(m_uFileVersion < FileVersion32_4);

                        byte[] pbHash = Convert.FromBase64String(strHash);
                        if (!MemUtil.ArraysEqual(pbHash, m_pbHashOfHeader))
                        {
                            throw new InvalidDataException(KLRes.FileCorrupted);
                        }
                    }
                }
                else if (xr.Name == ElemSettingsChanged)
                {
                    m_pwDatabase.SettingsChanged = ReadTime(xr);
                }
                else if (xr.Name == ElemDbName)
                {
                    m_pwDatabase.Name = ReadString(xr);
                }
                else if (xr.Name == ElemDbNameChanged)
                {
                    m_pwDatabase.NameChanged = ReadTime(xr);
                }
                else if (xr.Name == ElemDbDesc)
                {
                    m_pwDatabase.Description = ReadString(xr);
                }
                else if (xr.Name == ElemDbDescChanged)
                {
                    m_pwDatabase.DescriptionChanged = ReadTime(xr);
                }
                else if (xr.Name == ElemDbDefaultUser)
                {
                    m_pwDatabase.DefaultUserName = ReadString(xr);
                }
                else if (xr.Name == ElemDbDefaultUserChanged)
                {
                    m_pwDatabase.DefaultUserNameChanged = ReadTime(xr);
                }
                else if (xr.Name == ElemDbMntncHistoryDays)
                {
                    m_pwDatabase.MaintenanceHistoryDays = ReadUInt(xr, 365);
                }
                else if (xr.Name == ElemDbColor)
                {
                    string strColor = ReadString(xr);
                    if (!string.IsNullOrEmpty(strColor))
                    {
                        if (strColor.StartsWith("#"))
                        {
                            m_pwDatabase.Color = Color.FromArgb(0xFF, Color.FromArgb(
                                                                    Convert.ToInt32(strColor.Substring(1), 16)));
                        }
                        else
                        {
                            m_pwDatabase.Color = Color.FromName(strColor);
                        }
                    }
                }
                else if (xr.Name == ElemDbKeyChanged)
                {
                    m_pwDatabase.MasterKeyChanged = ReadTime(xr);
                }
                else if (xr.Name == ElemDbKeyChangeRec)
                {
                    m_pwDatabase.MasterKeyChangeRec = ReadLong(xr, -1);
                }
                else if (xr.Name == ElemDbKeyChangeForce)
                {
                    m_pwDatabase.MasterKeyChangeForce = ReadLong(xr, -1);
                }
                else if (xr.Name == ElemDbKeyChangeForceOnce)
                {
                    m_pwDatabase.MasterKeyChangeForceOnce = ReadBool(xr, false);
                }
                else if (xr.Name == ElemMemoryProt)
                {
                    return(SwitchContext(ctx, KdbContext.MemoryProtection, xr));
                }
                else if (xr.Name == ElemCustomIcons)
                {
                    return(SwitchContext(ctx, KdbContext.CustomIcons, xr));
                }
                else if (xr.Name == ElemRecycleBinEnabled)
                {
                    m_pwDatabase.RecycleBinEnabled = ReadBool(xr, true);
                }
                else if (xr.Name == ElemRecycleBinUuid)
                {
                    m_pwDatabase.RecycleBinUuid = ReadUuid(xr);
                }
                else if (xr.Name == ElemRecycleBinChanged)
                {
                    m_pwDatabase.RecycleBinChanged = ReadTime(xr);
                }
                else if (xr.Name == ElemEntryTemplatesGroup)
                {
                    m_pwDatabase.EntryTemplatesGroup = ReadUuid(xr);
                }
                else if (xr.Name == ElemEntryTemplatesGroupChanged)
                {
                    m_pwDatabase.EntryTemplatesGroupChanged = ReadTime(xr);
                }
                else if (xr.Name == ElemHistoryMaxItems)
                {
                    m_pwDatabase.HistoryMaxItems = ReadInt(xr, -1);
                }
                else if (xr.Name == ElemHistoryMaxSize)
                {
                    m_pwDatabase.HistoryMaxSize = ReadLong(xr, -1);
                }
                else if (xr.Name == ElemLastSelectedGroup)
                {
                    m_pwDatabase.LastSelectedGroup = ReadUuid(xr);
                }
                else if (xr.Name == ElemLastTopVisibleGroup)
                {
                    m_pwDatabase.LastTopVisibleGroup = ReadUuid(xr);
                }
                else if (xr.Name == ElemBinaries)
                {
                    return(SwitchContext(ctx, KdbContext.Binaries, xr));
                }
                else if (xr.Name == ElemCustomData)
                {
                    return(SwitchContext(ctx, KdbContext.CustomData, xr));
                }
                else
                {
                    ReadUnknown(xr);
                }
                break;

            case KdbContext.MemoryProtection:
                if (xr.Name == ElemProtTitle)
                {
                    m_pwDatabase.MemoryProtection.ProtectTitle = ReadBool(xr, false);
                }
                else if (xr.Name == ElemProtUserName)
                {
                    m_pwDatabase.MemoryProtection.ProtectUserName = ReadBool(xr, false);
                }
                else if (xr.Name == ElemProtPassword)
                {
                    m_pwDatabase.MemoryProtection.ProtectPassword = ReadBool(xr, true);
                }
                else if (xr.Name == ElemProtUrl)
                {
                    m_pwDatabase.MemoryProtection.ProtectUrl = ReadBool(xr, false);
                }
                else if (xr.Name == ElemProtNotes)
                {
                    m_pwDatabase.MemoryProtection.ProtectNotes = ReadBool(xr, false);
                }
                // else if(xr.Name == ElemProtAutoHide)
                //	m_pwDatabase.MemoryProtection.AutoEnableVisualHiding = ReadBool(xr, true);
                else
                {
                    ReadUnknown(xr);
                }
                break;

            case KdbContext.CustomIcons:
                if (xr.Name == ElemCustomIconItem)
                {
                    return(SwitchContext(ctx, KdbContext.CustomIcon, xr));
                }
                else
                {
                    ReadUnknown(xr);
                }
                break;

            case KdbContext.CustomIcon:
                if (xr.Name == ElemCustomIconItemID)
                {
                    m_uuidCustomIconID = ReadUuid(xr);
                }
                else if (xr.Name == ElemCustomIconItemData)
                {
                    string strData = ReadString(xr);
                    if (!string.IsNullOrEmpty(strData))
                    {
                        m_pbCustomIconData = Convert.FromBase64String(strData);
                    }
                    else
                    {
                        Debug.Assert(false);
                    }
                }
                else
                {
                    ReadUnknown(xr);
                }
                break;

            case KdbContext.Binaries:
                if (xr.Name == ElemBinary)
                {
                    if (xr.MoveToAttribute(AttrId))
                    {
                        string          strKey = xr.Value;
                        ProtectedBinary pbData = ReadProtectedBinary(xr);

                        int iKey;
                        if (!StrUtil.TryParseIntInvariant(strKey, out iKey))
                        {
                            throw new FormatException();
                        }
                        if (iKey < 0)
                        {
                            throw new FormatException();
                        }

                        Debug.Assert(m_pbsBinaries.Get(iKey) == null);
                        Debug.Assert(m_pbsBinaries.Find(pbData) < 0);
                        m_pbsBinaries.Set(iKey, pbData);
                    }
                    else
                    {
                        ReadUnknown(xr);
                    }
                }
                else
                {
                    ReadUnknown(xr);
                }
                break;

            case KdbContext.CustomData:
                if (xr.Name == ElemStringDictExItem)
                {
                    return(SwitchContext(ctx, KdbContext.CustomDataItem, xr));
                }
                else
                {
                    ReadUnknown(xr);
                }
                break;

            case KdbContext.CustomDataItem:
                if (xr.Name == ElemKey)
                {
                    m_strCustomDataKey = ReadString(xr);
                }
                else if (xr.Name == ElemValue)
                {
                    m_strCustomDataValue = ReadString(xr);
                }
                else
                {
                    ReadUnknown(xr);
                }
                break;

            case KdbContext.Root:
                if (xr.Name == ElemGroup)
                {
                    Debug.Assert(m_ctxGroups.Count == 0);
                    if (m_ctxGroups.Count != 0)
                    {
                        throw new FormatException();
                    }

                    m_pwDatabase.RootGroup = new PwGroup(false, false);
                    m_ctxGroups.Push(m_pwDatabase.RootGroup);
                    m_ctxGroup = m_ctxGroups.Peek();

                    return(SwitchContext(ctx, KdbContext.Group, xr));
                }
                else if (xr.Name == ElemDeletedObjects)
                {
                    return(SwitchContext(ctx, KdbContext.RootDeletedObjects, xr));
                }
                else
                {
                    ReadUnknown(xr);
                }
                break;

            case KdbContext.Group:
                if (xr.Name == ElemUuid)
                {
                    m_ctxGroup.Uuid = ReadUuid(xr);
                }
                else if (xr.Name == ElemName)
                {
                    m_ctxGroup.Name = ReadString(xr);
                }
                else if (xr.Name == ElemNotes)
                {
                    m_ctxGroup.Notes = ReadString(xr);
                }
                else if (xr.Name == ElemIcon)
                {
                    m_ctxGroup.IconId = ReadIconId(xr, PwIcon.Folder);
                }
                else if (xr.Name == ElemCustomIconID)
                {
                    m_ctxGroup.CustomIconUuid = ReadUuid(xr);
                }
                else if (xr.Name == ElemTimes)
                {
                    return(SwitchContext(ctx, KdbContext.GroupTimes, xr));
                }
                else if (xr.Name == ElemIsExpanded)
                {
                    m_ctxGroup.IsExpanded = ReadBool(xr, true);
                }
                else if (xr.Name == ElemGroupDefaultAutoTypeSeq)
                {
                    m_ctxGroup.DefaultAutoTypeSequence = ReadString(xr);
                }
                else if (xr.Name == ElemEnableAutoType)
                {
                    m_ctxGroup.EnableAutoType = StrUtil.StringToBoolEx(ReadString(xr));
                }
                else if (xr.Name == ElemEnableSearching)
                {
                    m_ctxGroup.EnableSearching = StrUtil.StringToBoolEx(ReadString(xr));
                }
                else if (xr.Name == ElemLastTopVisibleEntry)
                {
                    m_ctxGroup.LastTopVisibleEntry = ReadUuid(xr);
                }
                else if (xr.Name == ElemCustomData)
                {
                    return(SwitchContext(ctx, KdbContext.GroupCustomData, xr));
                }
                else if (xr.Name == ElemGroup)
                {
                    m_ctxGroup = new PwGroup(false, false);
                    m_ctxGroups.Peek().AddGroup(m_ctxGroup, true);

                    m_ctxGroups.Push(m_ctxGroup);

                    return(SwitchContext(ctx, KdbContext.Group, xr));
                }
                else if (xr.Name == ElemEntry)
                {
                    m_ctxEntry = new PwEntry(false, false);
                    m_ctxGroup.AddEntry(m_ctxEntry, true);

                    m_bEntryInHistory = false;
                    return(SwitchContext(ctx, KdbContext.Entry, xr));
                }
                else
                {
                    ReadUnknown(xr);
                }
                break;

            case KdbContext.GroupCustomData:
                if (xr.Name == ElemStringDictExItem)
                {
                    return(SwitchContext(ctx, KdbContext.GroupCustomDataItem, xr));
                }
                else
                {
                    ReadUnknown(xr);
                }
                break;

            case KdbContext.GroupCustomDataItem:
                if (xr.Name == ElemKey)
                {
                    m_strGroupCustomDataKey = ReadString(xr);
                }
                else if (xr.Name == ElemValue)
                {
                    m_strGroupCustomDataValue = ReadString(xr);
                }
                else
                {
                    ReadUnknown(xr);
                }
                break;

            case KdbContext.Entry:
                if (xr.Name == ElemUuid)
                {
                    m_ctxEntry.Uuid = ReadUuid(xr);
                }
                else if (xr.Name == ElemIcon)
                {
                    m_ctxEntry.IconId = ReadIconId(xr, PwIcon.Key);
                }
                else if (xr.Name == ElemCustomIconID)
                {
                    m_ctxEntry.CustomIconUuid = ReadUuid(xr);
                }
                else if (xr.Name == ElemFgColor)
                {
                    string strColor = ReadString(xr);
                    if (!string.IsNullOrEmpty(strColor))
                    {
                        m_ctxEntry.ForegroundColor = Color.FromName(strColor);
                    }
                }
                else if (xr.Name == ElemBgColor)
                {
                    string strColor = ReadString(xr);
                    if (!string.IsNullOrEmpty(strColor))
                    {
                        m_ctxEntry.BackgroundColor = Color.FromName(strColor);
                    }
                }
                else if (xr.Name == ElemOverrideUrl)
                {
                    m_ctxEntry.OverrideUrl = ReadString(xr);
                }
                else if (xr.Name == ElemTags)
                {
                    m_ctxEntry.Tags = StrUtil.StringToTags(ReadString(xr));
                }
                else if (xr.Name == ElemTimes)
                {
                    return(SwitchContext(ctx, KdbContext.EntryTimes, xr));
                }
                else if (xr.Name == ElemString)
                {
                    return(SwitchContext(ctx, KdbContext.EntryString, xr));
                }
                else if (xr.Name == ElemBinary)
                {
                    return(SwitchContext(ctx, KdbContext.EntryBinary, xr));
                }
                else if (xr.Name == ElemAutoType)
                {
                    return(SwitchContext(ctx, KdbContext.EntryAutoType, xr));
                }
                else if (xr.Name == ElemCustomData)
                {
                    return(SwitchContext(ctx, KdbContext.EntryCustomData, xr));
                }
                else if (xr.Name == ElemHistory)
                {
                    Debug.Assert(m_bEntryInHistory == false);

                    if (m_bEntryInHistory == false)
                    {
                        m_ctxHistoryBase = m_ctxEntry;
                        return(SwitchContext(ctx, KdbContext.EntryHistory, xr));
                    }
                    else
                    {
                        ReadUnknown(xr);
                    }
                }
                else
                {
                    ReadUnknown(xr);
                }
                break;

            case KdbContext.GroupTimes:
            case KdbContext.EntryTimes:
                ITimeLogger tl = ((ctx == KdbContext.GroupTimes) ?
                                  (ITimeLogger)m_ctxGroup : (ITimeLogger)m_ctxEntry);
                Debug.Assert(tl != null);

                if (xr.Name == ElemCreationTime)
                {
                    tl.CreationTime = ReadTime(xr);
                }
                else if (xr.Name == ElemLastModTime)
                {
                    tl.LastModificationTime = ReadTime(xr);
                }
                else if (xr.Name == ElemLastAccessTime)
                {
                    tl.LastAccessTime = ReadTime(xr);
                }
                else if (xr.Name == ElemExpiryTime)
                {
                    tl.ExpiryTime = ReadTime(xr);
                }
                else if (xr.Name == ElemExpires)
                {
                    tl.Expires = ReadBool(xr, false);
                }
                else if (xr.Name == ElemUsageCount)
                {
                    tl.UsageCount = ReadULong(xr, 0);
                }
                else if (xr.Name == ElemLocationChanged)
                {
                    tl.LocationChanged = ReadTime(xr);
                }
                else
                {
                    ReadUnknown(xr);
                }
                break;

            case KdbContext.EntryString:
                if (xr.Name == ElemKey)
                {
                    m_ctxStringName = ReadString(xr);
                }
                else if (xr.Name == ElemValue)
                {
                    m_ctxStringValue = ReadProtectedString(xr);
                }
                else
                {
                    ReadUnknown(xr);
                }
                break;

            case KdbContext.EntryBinary:
                if (xr.Name == ElemKey)
                {
                    m_ctxBinaryName = ReadString(xr);
                }
                else if (xr.Name == ElemValue)
                {
                    m_ctxBinaryValue = ReadProtectedBinary(xr);
                }
                else
                {
                    ReadUnknown(xr);
                }
                break;

            case KdbContext.EntryAutoType:
                if (xr.Name == ElemAutoTypeEnabled)
                {
                    m_ctxEntry.AutoType.Enabled = ReadBool(xr, true);
                }
                else if (xr.Name == ElemAutoTypeObfuscation)
                {
                    m_ctxEntry.AutoType.ObfuscationOptions =
                        (AutoTypeObfuscationOptions)ReadInt(xr, 0);
                }
                else if (xr.Name == ElemAutoTypeDefaultSeq)
                {
                    m_ctxEntry.AutoType.DefaultSequence = ReadString(xr);
                }
                else if (xr.Name == ElemAutoTypeItem)
                {
                    return(SwitchContext(ctx, KdbContext.EntryAutoTypeItem, xr));
                }
                else
                {
                    ReadUnknown(xr);
                }
                break;

            case KdbContext.EntryAutoTypeItem:
                if (xr.Name == ElemWindow)
                {
                    m_ctxATName = ReadString(xr);
                }
                else if (xr.Name == ElemKeystrokeSequence)
                {
                    m_ctxATSeq = ReadString(xr);
                }
                else
                {
                    ReadUnknown(xr);
                }
                break;

            case KdbContext.EntryCustomData:
                if (xr.Name == ElemStringDictExItem)
                {
                    return(SwitchContext(ctx, KdbContext.EntryCustomDataItem, xr));
                }
                else
                {
                    ReadUnknown(xr);
                }
                break;

            case KdbContext.EntryCustomDataItem:
                if (xr.Name == ElemKey)
                {
                    m_strEntryCustomDataKey = ReadString(xr);
                }
                else if (xr.Name == ElemValue)
                {
                    m_strEntryCustomDataValue = ReadString(xr);
                }
                else
                {
                    ReadUnknown(xr);
                }
                break;

            case KdbContext.EntryHistory:
                if (xr.Name == ElemEntry)
                {
                    m_ctxEntry = new PwEntry(false, false);
                    m_ctxHistoryBase.History.Add(m_ctxEntry);

                    m_bEntryInHistory = true;
                    return(SwitchContext(ctx, KdbContext.Entry, xr));
                }
                else
                {
                    ReadUnknown(xr);
                }
                break;

            case KdbContext.RootDeletedObjects:
                if (xr.Name == ElemDeletedObject)
                {
                    m_ctxDeletedObject = new PwDeletedObject();
                    m_pwDatabase.DeletedObjects.Add(m_ctxDeletedObject);

                    return(SwitchContext(ctx, KdbContext.DeletedObject, xr));
                }
                else
                {
                    ReadUnknown(xr);
                }
                break;

            case KdbContext.DeletedObject:
                if (xr.Name == ElemUuid)
                {
                    m_ctxDeletedObject.Uuid = ReadUuid(xr);
                }
                else if (xr.Name == ElemDeletionTime)
                {
                    m_ctxDeletedObject.DeletionTime = ReadTime(xr);
                }
                else
                {
                    ReadUnknown(xr);
                }
                break;

            default:
                ReadUnknown(xr);
                break;
            }

            return(ctx);
        }
Пример #29
0
 public PwDeletedObjectBuffer(PwDeletedObject deletedObject)
 {
     mDeletedObject = deletedObject;
 }
Пример #30
0
 public PwDeletedObjectBuffer()
 {
     mDeletedObject = new PwDeletedObject();
 }
Пример #31
0
        private void OnGroupsDelete(object sender, EventArgs e)
        {
            PwGroup pg = GetSelectedGroup();
            if(pg == null) { Debug.Assert(false); return; }

            PwGroup pgParent = pg.ParentGroup;
            if(pgParent != null)
            {
                string strText = KPRes.DeleteGroupInfo + MessageService.NewParagraph +
                    KPRes.DeleteGroupQuestion;

                if(!MessageService.AskYesNo(strText, KPRes.DeleteGroupCaption))
                    return;

                pgParent.Groups.Remove(pg);

                PwDeletedObject pdo = new PwDeletedObject();
                pdo.Uuid = pg.Uuid;
                pdo.DeletionTime = DateTime.Now;
                m_docMgr.ActiveDatabase.DeletedObjects.Add(pdo);

                UpdateGroupList(null);
                UpdateEntryList(null, false);
                UpdateUIState(true);
            }
        }