예제 #1
0
        private void WriteDocument(PwGroup pgRoot)
        {
            Debug.Assert(m_xmlWriter != null);
            if (m_xmlWriter == null)
            {
                throw new InvalidOperationException();
            }

            uint uNumGroups, uNumEntries, uCurEntry = 0;

            pgRoot.GetCounts(true, out uNumGroups, out uNumEntries);

            m_xmlWriter.WriteStartDocument(true);
            m_xmlWriter.WriteStartElement(ElemDocNode);

            WriteMeta();

            m_xmlWriter.WriteStartElement(ElemRoot);
            StartGroup(pgRoot);

            Stack <PwGroup> groupStack = new Stack <PwGroup>();

            groupStack.Push(pgRoot);

            GroupHandler gh = delegate(PwGroup pg)
            {
                Debug.Assert(pg != null);
                if (pg == null)
                {
                    throw new ArgumentNullException("pg");
                }

                while (true)
                {
                    if (pg.ParentGroup == groupStack.Peek())
                    {
                        groupStack.Push(pg);
                        StartGroup(pg);
                        break;
                    }
                    else
                    {
                        groupStack.Pop();
                        if (groupStack.Count <= 0)
                        {
                            return(false);
                        }

                        EndGroup();
                    }
                }

                return(true);
            };

            EntryHandler eh = delegate(PwEntry pe)
            {
                Debug.Assert(pe != null);
                WriteEntry(pe, false);

                ++uCurEntry;
                if (m_slLogger != null)
                {
                    if (!m_slLogger.SetProgress((100 * uCurEntry) / uNumEntries))
                    {
                        return(false);
                    }
                }

                return(true);
            };

            if (!pgRoot.TraverseTree(TraversalMethod.PreOrder, gh, eh))
            {
                throw new InvalidOperationException();
            }

            while (groupStack.Count > 1)
            {
                m_xmlWriter.WriteEndElement();
                groupStack.Pop();
            }

            EndGroup();

            WriteList(ElemDeletedObjects, m_pwDatabase.DeletedObjects);
            m_xmlWriter.WriteEndElement();             // Root

            m_xmlWriter.WriteEndElement();             // ElemDocNode
            m_xmlWriter.WriteEndDocument();
        }
예제 #2
0
		private void WriteEntries(KdbManager mgr, Dictionary<PwGroup, uint> dictGroups,
			PwGroup pgRoot)
		{
			bool bWarnedOnce = false;
			uint uGroupCount, uEntryCount, uEntriesSaved = 0;
			pgRoot.GetCounts(true, out uGroupCount, out uEntryCount);

			DateTime dtNeverExpire = KdbManager.GetNeverExpireTime();

			EntryHandler eh = delegate(PwEntry pe)
			{
				KdbEntry e = new KdbEntry();

				e.Uuid.Set(pe.Uuid.UuidBytes);

				if(pe.ParentGroup != pgRoot)
					e.GroupId = dictGroups[pe.ParentGroup];
				else
				{
					e.GroupId = 1;
					if((m_slLogger != null) && !bWarnedOnce)
					{
						m_slLogger.SetText(KdbPrefix +
							KPRes.FormatNoRootEntries, LogStatusType.Warning);
						bWarnedOnce = true;
					}

					if(dictGroups.Count == 0)
						throw new Exception(KPRes.FormatNoSubGroupsInRoot);
				}

				e.ImageId = (uint)pe.IconId;

				e.Title = pe.Strings.ReadSafe(PwDefs.TitleField);
				e.UserName = pe.Strings.ReadSafe(PwDefs.UserNameField);
				e.Password = pe.Strings.ReadSafe(PwDefs.PasswordField);
				e.Url = pe.Strings.ReadSafe(PwDefs.UrlField);

				string strNotes = pe.Strings.ReadSafe(PwDefs.NotesField);
				ExportCustomStrings(pe, ref strNotes);
				ExportAutoType(pe, ref strNotes);
				ExportUrlOverride(pe, ref strNotes);
				e.Additional = strNotes;

				e.PasswordLength = (uint)e.Password.Length;

				Debug.Assert(TimeUtil.PwTimeLength == 7);
				e.CreationTime.Set(pe.CreationTime);
				e.LastModificationTime.Set(pe.LastModificationTime);
				e.LastAccessTime.Set(pe.LastAccessTime);

				if(pe.Expires) e.ExpirationTime.Set(pe.ExpiryTime);
				else e.ExpirationTime.Set(dtNeverExpire);

				IntPtr hBinaryData = IntPtr.Zero;
				if(pe.Binaries.UCount >= 1)
				{
					foreach(KeyValuePair<string, ProtectedBinary> kvp in pe.Binaries)
					{
						e.BinaryDescription = kvp.Key;

						byte[] pbAttached = kvp.Value.ReadData();
						e.BinaryDataLength = (uint)pbAttached.Length;

						if(e.BinaryDataLength > 0)
						{
							hBinaryData = Marshal.AllocHGlobal((int)e.BinaryDataLength);
							Marshal.Copy(pbAttached, 0, hBinaryData, (int)e.BinaryDataLength);

							e.BinaryData = hBinaryData;
						}

						break;
					}

					if((pe.Binaries.UCount > 1) && (m_slLogger != null))
						m_slLogger.SetText(KdbPrefix + KPRes.FormatOnlyOneAttachment + "\r\n\r\n" +
							KPRes.Entry + ":\r\n" + KPRes.Title + ": " + e.Title + "\r\n" +
							KPRes.UserName + ": " + e.UserName, LogStatusType.Warning);
				}

				bool bResult = mgr.AddEntry(ref e);

				Marshal.FreeHGlobal(hBinaryData);
				hBinaryData = IntPtr.Zero;

				if(!bResult)
				{
					Debug.Assert(false);
					throw new InvalidOperationException();
				}

				++uEntriesSaved;
				if(m_slLogger != null)
					if(!m_slLogger.SetProgress((100 * uEntriesSaved) / uEntryCount))
						return false;

				return true;
			};

			if(!pgRoot.TraverseTree(TraversalMethod.PreOrder, null, eh))
				throw new InvalidOperationException();
		}
예제 #3
0
		private void WriteDocument(PwGroup pgRoot)
		{
			Debug.Assert(m_xmlWriter != null);
			if(m_xmlWriter == null) throw new InvalidOperationException();

			uint uNumGroups, uNumEntries, uCurEntry = 0;
			pgRoot.GetCounts(true, out uNumGroups, out uNumEntries);

			m_xmlWriter.WriteStartDocument(true);
			m_xmlWriter.WriteStartElement(ElemDocNode);

			WriteMeta();

			m_xmlWriter.WriteStartElement(ElemRoot);
			StartGroup(pgRoot);

			Stack<PwGroup> groupStack = new Stack<PwGroup>();
			groupStack.Push(pgRoot);

			GroupHandler gh = delegate(PwGroup pg)
			{
				Debug.Assert(pg != null);
				if(pg == null) throw new ArgumentNullException("pg");

				while(true)
				{
					if(pg.ParentGroup == groupStack.Peek())
					{
						groupStack.Push(pg);
						StartGroup(pg);
						break;
					}
					else
					{
						groupStack.Pop();
						if(groupStack.Count <= 0) return false;

						EndGroup();
					}
				}

				return true;
			};

			EntryHandler eh = delegate(PwEntry pe)
			{
				Debug.Assert(pe != null);
				WriteEntry(pe, false);

				++uCurEntry;
				if(m_slLogger != null)
					if(!m_slLogger.SetProgress((100 * uCurEntry) / uNumEntries))
						return false;

				return true;
			};

			if(!pgRoot.TraverseTree(TraversalMethod.PreOrder, gh, eh))
				throw new InvalidOperationException();

			while(groupStack.Count > 1)
			{
				m_xmlWriter.WriteEndElement();
				groupStack.Pop();
			}

			EndGroup();

			WriteList(ElemDeletedObjects, m_pwDatabase.DeletedObjects);
			m_xmlWriter.WriteEndElement(); // Root

			m_xmlWriter.WriteEndElement(); // ElemDocNode
			m_xmlWriter.WriteEndDocument();
		}
예제 #4
0
        private void WriteEntries(Kdb3Manager mgr, Dictionary <PwGroup, uint> dictGroups,
                                  PwGroup pgRoot)
        {
            bool bWarnedOnce = false;
            uint uGroupCount, uEntryCount, uEntriesSaved = 0;

            pgRoot.GetCounts(true, out uGroupCount, out uEntryCount);

            DateTime dtNeverExpire = Kdb3Manager.GetNeverExpireTime();

            EntryHandler eh = delegate(PwEntry pe)
            {
                Kdb3Entry e = new Kdb3Entry();

                e.Uuid.Set(pe.Uuid.UuidBytes);

                if (pe.ParentGroup != pgRoot)
                {
                    e.GroupId = dictGroups[pe.ParentGroup];
                }
                else
                {
                    e.GroupId = 1;
                    if ((m_slLogger != null) && !bWarnedOnce)
                    {
                        m_slLogger.SetText(Kdb3Prefix +
                                           KPRes.FormatNoRootEntries, LogStatusType.Warning);
                        bWarnedOnce = true;
                    }

                    if (dictGroups.Count == 0)
                    {
                        throw new Exception(KPRes.FormatNoSubGroupsInRoot);
                    }
                }

                e.ImageId = (uint)pe.IconId;

                e.Title    = pe.Strings.ReadSafe(PwDefs.TitleField);
                e.UserName = pe.Strings.ReadSafe(PwDefs.UserNameField);
                e.Password = pe.Strings.ReadSafe(PwDefs.PasswordField);
                e.Url      = pe.Strings.ReadSafe(PwDefs.UrlField);

                string strNotes = pe.Strings.ReadSafe(PwDefs.NotesField);
                ExportCustomStrings(pe, ref strNotes);
                ExportAutoType(pe, ref strNotes);
                ExportUrlOverride(pe, ref strNotes);
                e.Additional = strNotes;

                e.PasswordLength = (uint)e.Password.Length;

                Debug.Assert(TimeUtil.PwTimeLength == 7);
                e.CreationTime.Set(pe.CreationTime);
                e.LastModificationTime.Set(pe.LastModificationTime);
                e.LastAccessTime.Set(pe.LastAccessTime);

                if (pe.Expires)
                {
                    e.ExpirationTime.Set(pe.ExpiryTime);
                }
                else
                {
                    e.ExpirationTime.Set(dtNeverExpire);
                }

                IntPtr hBinaryData = IntPtr.Zero;
                if (pe.Binaries.UCount >= 1)
                {
                    foreach (KeyValuePair <string, ProtectedBinary> kvp in pe.Binaries)
                    {
                        e.BinaryDescription = kvp.Key;

                        byte[] pbAttached = kvp.Value.ReadData();
                        e.BinaryDataLength = (uint)pbAttached.Length;

                        if (e.BinaryDataLength > 0)
                        {
                            hBinaryData = Marshal.AllocHGlobal((int)e.BinaryDataLength);
                            Marshal.Copy(pbAttached, 0, hBinaryData, (int)e.BinaryDataLength);

                            e.BinaryData = hBinaryData;
                        }

                        break;
                    }

                    if ((pe.Binaries.UCount > 1) && (m_slLogger != null))
                    {
                        m_slLogger.SetText(Kdb3Prefix + KPRes.FormatOnlyOneAttachment + "\r\n\r\n" +
                                           KPRes.Entry + ":\r\n" + KPRes.Title + ": " + e.Title + "\r\n" +
                                           KPRes.UserName + ": " + e.UserName, LogStatusType.Warning);
                    }
                }

                bool bResult = mgr.AddEntry(ref e);

                Marshal.FreeHGlobal(hBinaryData);
                hBinaryData = IntPtr.Zero;

                if (!bResult)
                {
                    Debug.Assert(false);
                    throw new InvalidOperationException();
                }

                ++uEntriesSaved;
                if (m_slLogger != null)
                {
                    if (!m_slLogger.SetProgress((100 * uEntriesSaved) / uEntryCount))
                    {
                        return(false);
                    }
                }

                return(true);
            };

            if (!pgRoot.TraverseTree(TraversalMethod.PreOrder, null, eh))
            {
                throw new InvalidOperationException();
            }
        }
예제 #5
0
        private void loadSubMenuItems(PwGroup group, MenuItem currMenu)
        {
            const string UserName = "******";
            const string Title = "Title";
            const string URL = "URL";
            const string Password = "******";
            const string CopyPwdCaption = "Copy Password";
            const string CopyUrlPwdCaption = "Launch URL && Copy Password";
            const string EditItemCaption = "Edit this item";

            uint grpCount = 0, entryCount = 0;
            group.GetCounts(false, out grpCount, out entryCount);
            if (entryCount > 0) {
                foreach (var j in group.GetEntries(false)) {

                    var titleMenu = new MenuItem(j.Strings.ReadSafe(Title));
                    currMenu.MenuItems.Add(titleMenu);

                    titleMenu.MenuItems.Add(new MenuItem(j.Strings.ReadSafe(UserName)));
                    var name = j.ParentGroup.Name + MenuSeparator + j.Strings.ReadSafe(Title) + MenuSeparator + j.Strings.ReadSafe(UserName);
                    var pwMnu = new MenuItem(CopyPwdCaption, onPwdMenuClick);
                    pwMnu.Name = name;
                    titleMenu.MenuItems.Add(pwMnu);
                    var pwlMnu = new MenuItem(CopyUrlPwdCaption, onUrlPwdMenuClick);
                    pwlMnu.Name = name;
                    titleMenu.MenuItems.Add(pwlMnu);

                    currMenu.MenuItems.Add(new MenuItem(MenuSeparator));

                    var editMnu = new MenuItem(EditItemCaption, OnEditItemClick);
                    editMnu.Name = name;
                    titleMenu.MenuItems.Add(editMnu);

                    try {
                        localPwdHash.Add(name, j.Strings.ReadSafe(Password));
                        localURLHash.Add(name, j.Strings.ReadSafe(URL));
                    } catch (Exception e) {
                        MessageBox.Show("Dupe found: " + name + MenuSeparator + j.Strings.ReadSafe(URL));
                    }

                }
            }
            // are there any group children
            if (grpCount > 0) {
                foreach (var j in group.Groups) {
                    var newGroupMenu = new MenuItem(j.Name);
                    currMenu.MenuItems.Add(newGroupMenu);
                    loadSubMenuItems(j, newGroupMenu);
                }
            }
        }