Entry.swift 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. // Entry.swift
  2. // This file is part of KeePassKit.
  3. //
  4. // Copyright © 2019 Maxime Epain. All rights reserved.
  5. //
  6. // KeePassKit is free software: you can redistribute it and/or modify
  7. // it under the terms of the GNU General Public License as published by
  8. // the Free Software Foundation, either version 3 of the License, or
  9. // (at your option) any later version.
  10. //
  11. // KeePassKit is distributed in the hope that it will be useful,
  12. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. // GNU General Public License for more details.
  15. //
  16. // You should have received a copy of the GNU General Public License
  17. // along with KeePassKit. If not, see <https://www.gnu.org/licenses/>.
  18. import Foundation
  19. import Binary
  20. let MetaEntryBinaryDescription = "bin-stream"
  21. let MetaEntryTitle = "Meta-Info"
  22. let MetaEntryUsername = "SYSTEM"
  23. let MetaEntryURL = "$"
  24. let MetaEntryUIState = "Simple UI State"
  25. let MetaEntryDefaultUsername = "Default User Name"
  26. let MetaEntrySearchHistoryItem = "Search History Item"
  27. let MetaEntryCustomKVP = "Custom KVP"
  28. let MetaEntryDatabaseColor = "Database Color"
  29. let MetaEntryKeePassXCustomIcon = "KPX_CUSTOM_ICONS_2"
  30. let MetaEntryKeePassXCustomIcon2 = "KPX_CUSTOM_ICONS_4"
  31. let MetaEntryKeePassXGroupTreeState = "KPX_GROUP_TREE_STATE"
  32. let MetaEntryKeePassKitGroupUUIDs = "KeePassKit Group UUIDs"
  33. let MetaEntryKeePassKitDeletedObjects = "KeePassKit Deleted Objects"
  34. let MetaEntryKeePassKitDatabaseName = "KeePassKit Database Name"
  35. let MetaEntryKeePassKitDatabaseDescription = "KeePassKit Database Description"
  36. let MetaEntryKeePassKitTrash = "KeePassKit Trash"
  37. let MetaEntryKeePassKitUserTemplates = "KeePassKit User Templates"
  38. public final class Entry: Row, Streamable {
  39. public static let End = Type.end
  40. public enum `Type`: UInt16, Streamable {
  41. case reserved = 0x0000
  42. case uuid = 0x0001
  43. case groupID = 0x0002
  44. case iconID = 0x0003
  45. case title = 0x0004
  46. case url = 0x0005
  47. case username = 0x0006
  48. case password = 0x0007
  49. case notes = 0x0008
  50. case creationTime = 0x0009
  51. case lastModifiedTime = 0x000A
  52. case lastAccessTime = 0x000B
  53. case expirationTime = 0x000C
  54. case binaryDesc = 0x000D
  55. case binaryData = 0x000E
  56. case end = 0xFFFF
  57. }
  58. var parent: Group?
  59. public var properties: [Property<Type>]
  60. public required init() {
  61. properties = []
  62. }
  63. }
  64. extension Entry {
  65. public var creationDate: Date {
  66. date(at: .creationTime) ?? Date.distantPast
  67. }
  68. public var lastModifiedDate: Date {
  69. get { date(at: .lastModifiedTime) ?? Date.distantPast }
  70. set { set(newValue, at: .lastModifiedTime) }
  71. }
  72. public var lastAccessDate: Date {
  73. get { date(at: .lastAccessTime) ?? Date.distantPast }
  74. set { set(newValue, at: .lastAccessTime) }
  75. }
  76. var isMetaEntry: Bool {
  77. return false
  78. }
  79. public func removeFromParent() {
  80. parent?.entries.removeAll(where: { $0 == self })
  81. self[.groupID] = -1
  82. }
  83. }
  84. extension Entry: Hashable {
  85. public static func == (lhs: Entry, rhs: Entry) -> Bool {
  86. guard let lhs = lhs[.uuid], let rhs = rhs[.uuid] else { return false }
  87. return lhs == rhs
  88. }
  89. public func hash(into hasher: inout Hasher) {
  90. if let uuid = self[.uuid] { hasher.combine(uuid) }
  91. else if let title = self[.title] { hasher.combine(title) }
  92. }
  93. }