Group.swift 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. // Group.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. public final class Group: Row, Streamable {
  21. public static let End = Type.end
  22. public enum `Type`: UInt16, Streamable {
  23. case reserved = 0x0000
  24. case groupID = 0x0001
  25. case name = 0x0002
  26. case creationTime = 0x0003
  27. case lastModifiedTime = 0x0004
  28. case lastAccessTime = 0x0005
  29. case expirationTime = 0x0006
  30. case iconID = 0x0007
  31. case groupLevel = 0x0008
  32. case groupFlags = 0x0009
  33. case end = 0xFFFF
  34. }
  35. var parent: Group?
  36. public var properties: [Property<Type>]
  37. public var childs: [Group]
  38. public var entries: [Entry]
  39. public required init() {
  40. properties = []
  41. childs = []
  42. entries = []
  43. }
  44. }
  45. extension Group {
  46. public var name: String {
  47. get { self[.name] ?? "" }
  48. set { self[.name] = newValue }
  49. }
  50. public var level: Int {
  51. get { self[.groupLevel] ?? 0 }
  52. set { self[.groupLevel] = newValue }
  53. }
  54. public var icon: Int {
  55. get { self[.iconID] ?? 0 }
  56. set { self[.iconID] = newValue }
  57. }
  58. public var creationDate: Date {
  59. date(at: .creationTime) ?? Date.distantPast
  60. }
  61. public var lastModifiedDate: Date {
  62. get { date(at: .lastModifiedTime) ?? Date.distantPast }
  63. set { set(newValue, at: .lastModifiedTime) }
  64. }
  65. public var lastAccessDate: Date {
  66. get { date(at: .lastAccessTime) ?? Date.distantPast }
  67. set { set(newValue, at: .lastAccessTime) }
  68. }
  69. public func removeFromParent() {
  70. parent?.childs.removeAll(where: { $0 == self })
  71. }
  72. public func add(_ entry: Entry) {
  73. entry.removeFromParent()
  74. entries.append(entry)
  75. entry[.groupID] = self[.groupID]
  76. }
  77. public func add(_ group: Group) {
  78. group.removeFromParent()
  79. childs.append(group)
  80. group.level = level + 1
  81. }
  82. }
  83. extension Group: Hashable {
  84. public static func == (lhs: Group, rhs: Group) -> Bool {
  85. guard let lhs = lhs[.groupLevel], let rhs = rhs[.groupLevel] else { return false }
  86. return lhs == rhs
  87. }
  88. public func hash(into hasher: inout Hasher) {
  89. if let groupID = self[.groupID] { hasher.combine(groupID) }
  90. if let name = self[.name] { hasher.combine(name) }
  91. if let groupLevel = self[.groupLevel] { hasher.combine(groupLevel) }
  92. }
  93. }