Skip to main content

keepass_ng/db/types/
meta.rs

1use chrono::NaiveDateTime;
2use indexmap::IndexMap;
3use uuid::Uuid;
4
5use crate::db::{Color, CustomDataItem, CustomIcon};
6
7/// Database metadata
8#[derive(Debug, Default, Eq, PartialEq, Clone)]
9#[cfg_attr(feature = "serialization", derive(serde::Serialize))]
10pub struct Meta {
11    /// the program that generated the database file.
12    pub generator: Option<String>,
13
14    /// name of the database
15    pub database_name: Option<String>,
16
17    /// time the database name was last changed
18    pub database_name_changed: Option<NaiveDateTime>,
19
20    /// description of the database
21    pub database_description: Option<String>,
22
23    /// time the database description was last changed
24    pub database_description_changed: Option<NaiveDateTime>,
25
26    /// All custom icons in the database, indexed by their UUID.
27    pub(crate) custom_icons: IndexMap<Uuid, CustomIcon>,
28
29    /// default username
30    pub default_username: Option<String>,
31
32    /// time the default username was last changed
33    pub default_username_changed: Option<NaiveDateTime>,
34
35    /// number of days of maintenance history to keep
36    pub maintenance_history_days: Option<usize>,
37
38    /// color code for the database
39    pub color: Option<Color>,
40
41    /// time the master key was last changed
42    pub master_key_changed: Option<NaiveDateTime>,
43
44    pub master_key_change_rec: Option<isize>,
45
46    pub master_key_change_force: Option<isize>,
47
48    /// memory protection settings
49    pub memory_protection: Option<MemoryProtection>,
50
51    /// whether the recycle bin is enabled
52    pub(crate) recyclebin_enabled: Option<bool>,
53
54    /// A UUID for the recycle bin group
55    pub(crate) recyclebin_uuid: Option<Uuid>,
56
57    /// last time the recycle bin was changed
58    pub(crate) recyclebin_changed: Option<NaiveDateTime>,
59
60    /// UUID of the group containing entry templates
61    pub entry_templates_group: Option<Uuid>,
62
63    /// last time the group containing entry templates was changed
64    pub entry_templates_group_changed: Option<NaiveDateTime>,
65
66    /// UUID of the last selected group
67    pub last_selected_group: Option<Uuid>,
68
69    /// UUID of the last top-visible group
70    pub last_top_visible_group: Option<Uuid>,
71
72    /// Maximum number of items of history to keep
73    pub history_max_items: Option<isize>,
74
75    /// Maximum size of the history to keep
76    pub history_max_size: Option<isize>,
77
78    /// Last time the settings were changed
79    pub settings_changed: Option<NaiveDateTime>,
80
81    /// Additional custom data fields
82    pub custom_data: IndexMap<String, CustomDataItem>,
83}
84
85impl Meta {
86    /// Create a new Meta object
87    pub fn new() -> Self {
88        Self {
89            recyclebin_enabled: Some(true),
90            ..Meta::default()
91        }
92    }
93
94    pub fn custom_icon(&self, uuid: Uuid) -> Option<&CustomIcon> {
95        self.custom_icons.get(&uuid)
96    }
97
98    pub fn insert_custom_icon(&mut self, icon: CustomIcon) -> Option<CustomIcon> {
99        self.custom_icons.insert(icon.id(), icon)
100    }
101
102    /// Set recycle bin enabled
103    pub fn set_recycle_bin_enabled(&mut self, enabled: bool) {
104        self.recyclebin_enabled = Some(enabled);
105        self.set_recycle_bin_changed();
106    }
107
108    pub fn recycle_bin_enabled(&self) -> bool {
109        self.recyclebin_enabled.unwrap_or(false)
110    }
111
112    pub fn recycle_bin_changed(&self) -> Option<NaiveDateTime> {
113        self.recyclebin_changed
114    }
115
116    /// Set recycle bin changed time
117    pub fn set_recycle_bin_changed(&mut self) {
118        let time = chrono::Local::now().naive_local();
119        self.recyclebin_changed = Some(time);
120    }
121}
122
123/// Database memory protection settings
124#[derive(Debug, PartialEq, Eq, Clone)]
125#[cfg_attr(feature = "serialization", derive(serde::Serialize))]
126pub struct MemoryProtection {
127    /// Whether titles should be protected
128    pub protect_title: bool,
129
130    /// Whether user names should be protected
131    pub protect_username: bool,
132
133    /// Whether passwords should be protected
134    pub protect_password: bool,
135
136    /// Whether URLs should be protected
137    pub protect_url: bool,
138
139    /// Whether notes should be protected
140    pub protect_notes: bool,
141}
142
143impl Default for MemoryProtection {
144    fn default() -> Self {
145        Self {
146            protect_title: false,
147            protect_username: false,
148            protect_password: true,
149            protect_url: false,
150            protect_notes: false,
151        }
152    }
153}