1 /*
2 * Copyright (C) 2018 KeePassXC Team <team@keepassxc.org>
3 *
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 2 or (at your option)
7 * version 3 of the License.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18 #include "CustomData.h"
19 #include "Clock.h"
20
21 #include "core/Global.h"
22
23 const QString CustomData::LastModified = QStringLiteral("_LAST_MODIFIED");
24 const QString CustomData::Created = QStringLiteral("_CREATED");
25 const QString CustomData::BrowserKeyPrefix = QStringLiteral("KPXC_BROWSER_");
26 const QString CustomData::BrowserLegacyKeyPrefix = QStringLiteral("Public Key: ");
27
CustomData(QObject * parent)28 CustomData::CustomData(QObject* parent)
29 : QObject(parent)
30 {
31 }
32
keys() const33 QList<QString> CustomData::keys() const
34 {
35 return m_data.keys();
36 }
37
hasKey(const QString & key) const38 bool CustomData::hasKey(const QString& key) const
39 {
40 return m_data.contains(key);
41 }
42
value(const QString & key) const43 QString CustomData::value(const QString& key) const
44 {
45 return m_data.value(key);
46 }
47
contains(const QString & key) const48 bool CustomData::contains(const QString& key) const
49 {
50 return m_data.contains(key);
51 }
52
containsValue(const QString & value) const53 bool CustomData::containsValue(const QString& value) const
54 {
55 return asConst(m_data).values().contains(value);
56 }
57
set(const QString & key,const QString & value)58 void CustomData::set(const QString& key, const QString& value)
59 {
60 bool addAttribute = !m_data.contains(key);
61 bool changeValue = !addAttribute && (m_data.value(key) != value);
62
63 if (addAttribute) {
64 emit aboutToBeAdded(key);
65 }
66
67 if (addAttribute || changeValue) {
68 m_data.insert(key, value);
69 updateLastModified();
70 emit customDataModified();
71 }
72
73 if (addAttribute) {
74 emit added(key);
75 }
76 }
77
remove(const QString & key)78 void CustomData::remove(const QString& key)
79 {
80 emit aboutToBeRemoved(key);
81
82 m_data.remove(key);
83
84 updateLastModified();
85 emit removed(key);
86 emit customDataModified();
87 }
88
rename(const QString & oldKey,const QString & newKey)89 void CustomData::rename(const QString& oldKey, const QString& newKey)
90 {
91 const bool containsOldKey = m_data.contains(oldKey);
92 const bool containsNewKey = m_data.contains(newKey);
93 Q_ASSERT(containsOldKey && !containsNewKey);
94 if (!containsOldKey || containsNewKey) {
95 return;
96 }
97
98 QString data = value(oldKey);
99
100 emit aboutToRename(oldKey, newKey);
101
102 m_data.remove(oldKey);
103 m_data.insert(newKey, data);
104
105 updateLastModified();
106 emit customDataModified();
107 emit renamed(oldKey, newKey);
108 }
109
copyDataFrom(const CustomData * other)110 void CustomData::copyDataFrom(const CustomData* other)
111 {
112 if (*this == *other) {
113 return;
114 }
115
116 emit aboutToBeReset();
117
118 m_data = other->m_data;
119
120 updateLastModified();
121 emit reset();
122 emit customDataModified();
123 }
124
getLastModified() const125 QDateTime CustomData::getLastModified() const
126 {
127 if (m_data.contains(LastModified)) {
128 return Clock::parse(m_data.value(LastModified));
129 }
130 return {};
131 }
132
isProtectedCustomData(const QString & key) const133 bool CustomData::isProtectedCustomData(const QString& key) const
134 {
135 return key.startsWith(CustomData::BrowserKeyPrefix) || key.startsWith(CustomData::Created);
136 }
137
operator ==(const CustomData & other) const138 bool CustomData::operator==(const CustomData& other) const
139 {
140 return (m_data == other.m_data);
141 }
142
operator !=(const CustomData & other) const143 bool CustomData::operator!=(const CustomData& other) const
144 {
145 return (m_data != other.m_data);
146 }
147
clear()148 void CustomData::clear()
149 {
150 emit aboutToBeReset();
151
152 m_data.clear();
153
154 emit reset();
155 emit customDataModified();
156 }
157
isEmpty() const158 bool CustomData::isEmpty() const
159 {
160 return m_data.isEmpty();
161 }
162
size() const163 int CustomData::size() const
164 {
165 return m_data.size();
166 }
167
dataSize() const168 int CustomData::dataSize() const
169 {
170 int size = 0;
171
172 QHashIterator<QString, QString> i(m_data);
173 while (i.hasNext()) {
174 i.next();
175 size += i.key().toUtf8().size() + i.value().toUtf8().size();
176 }
177 return size;
178 }
179
updateLastModified()180 void CustomData::updateLastModified()
181 {
182 if (m_data.size() == 1 && m_data.contains(LastModified)) {
183 m_data.remove(LastModified);
184 return;
185 }
186
187 m_data.insert(LastModified, Clock::currentDateTimeUtc().toString());
188 }
189