2012-04-14 13:38:20 +00:00
|
|
|
/*
|
2023-10-25 14:12:55 +00:00
|
|
|
* Copyright (C) 2023 KeePassXC Team <team@keepassxc.org>
|
2012-04-14 13:38:20 +00:00
|
|
|
* Copyright (C) 2012 Felix Geyer <debfx@fobos.de>
|
|
|
|
|
*
|
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
|
* the Free Software Foundation, either version 2 or (at your option)
|
|
|
|
|
* version 3 of the License.
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include "EntryAttributes.h"
|
2019-01-17 06:23:22 +00:00
|
|
|
#include "core/Global.h"
|
|
|
|
|
|
2021-07-12 02:10:29 +00:00
|
|
|
#include <QRegularExpression>
|
|
|
|
|
#include <QUuid>
|
|
|
|
|
|
2013-12-01 08:43:41 +00:00
|
|
|
const QString EntryAttributes::TitleKey = "Title";
|
2013-11-30 15:05:10 +00:00
|
|
|
const QString EntryAttributes::UserNameKey = "UserName";
|
|
|
|
|
const QString EntryAttributes::PasswordKey = "Password";
|
2013-12-01 23:10:47 +00:00
|
|
|
const QString EntryAttributes::URLKey = "URL";
|
2013-12-01 08:43:41 +00:00
|
|
|
const QString EntryAttributes::NotesKey = "Notes";
|
2018-11-01 03:27:38 +00:00
|
|
|
const QStringList EntryAttributes::DefaultAttributes(QStringList()
|
|
|
|
|
<< TitleKey << UserNameKey << PasswordKey << URLKey << NotesKey);
|
2017-11-12 15:35:54 +00:00
|
|
|
const QString EntryAttributes::WantedFieldGroupName = "WantedField";
|
|
|
|
|
const QString EntryAttributes::SearchInGroupName = "SearchIn";
|
|
|
|
|
const QString EntryAttributes::SearchTextGroupName = "SearchText";
|
|
|
|
|
|
2017-01-28 13:18:43 +00:00
|
|
|
const QString EntryAttributes::RememberCmdExecAttr = "_EXEC_CMD";
|
2023-11-23 04:11:25 +00:00
|
|
|
const QString EntryAttributes::AdditionalUrlAttribute = "KP2A_URL";
|
2023-10-25 14:12:55 +00:00
|
|
|
const QString EntryAttributes::PasskeyAttribute = "KPEX_PASSKEY";
|
2012-04-14 13:38:20 +00:00
|
|
|
|
|
|
|
|
EntryAttributes::EntryAttributes(QObject* parent)
|
2021-05-28 01:50:15 +00:00
|
|
|
: ModifiableObject(parent)
|
2012-04-14 13:38:20 +00:00
|
|
|
{
|
2012-04-14 16:47:40 +00:00
|
|
|
clear();
|
2012-04-14 13:38:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QList<QString> EntryAttributes::keys() const
|
|
|
|
|
{
|
|
|
|
|
return m_attributes.keys();
|
|
|
|
|
}
|
|
|
|
|
|
2014-12-03 22:36:24 +00:00
|
|
|
bool EntryAttributes::hasKey(const QString& key) const
|
|
|
|
|
{
|
2017-09-24 09:45:58 +00:00
|
|
|
return m_attributes.contains(key);
|
2014-12-03 22:36:24 +00:00
|
|
|
}
|
|
|
|
|
|
2023-11-23 04:11:25 +00:00
|
|
|
bool EntryAttributes::hasPasskey() const
|
|
|
|
|
{
|
|
|
|
|
const auto keyList = keys();
|
|
|
|
|
for (const auto& key : keyList) {
|
|
|
|
|
if (isPasskeyAttribute(key)) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2017-12-25 13:53:22 +00:00
|
|
|
QList<QString> EntryAttributes::customKeys() const
|
2012-10-23 22:15:23 +00:00
|
|
|
{
|
|
|
|
|
QList<QString> customKeys;
|
2016-09-02 17:51:51 +00:00
|
|
|
const QList<QString> keyList = keys();
|
|
|
|
|
for (const QString& key : keyList) {
|
2023-10-25 14:12:55 +00:00
|
|
|
if (!isDefaultAttribute(key) && !isPasskeyAttribute(key)) {
|
2012-10-23 22:15:23 +00:00
|
|
|
customKeys.append(key);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return customKeys;
|
|
|
|
|
}
|
|
|
|
|
|
2012-04-14 13:38:20 +00:00
|
|
|
QString EntryAttributes::value(const QString& key) const
|
|
|
|
|
{
|
|
|
|
|
return m_attributes.value(key);
|
|
|
|
|
}
|
|
|
|
|
|
2019-02-22 22:17:51 +00:00
|
|
|
QList<QString> EntryAttributes::values(const QList<QString>& keys) const
|
|
|
|
|
{
|
|
|
|
|
QList<QString> values;
|
|
|
|
|
for (const QString& key : keys) {
|
|
|
|
|
values.append(m_attributes.value(key));
|
|
|
|
|
}
|
|
|
|
|
return values;
|
|
|
|
|
}
|
|
|
|
|
|
2017-11-19 19:06:09 +00:00
|
|
|
bool EntryAttributes::contains(const QString& key) const
|
2013-04-16 20:58:42 +00:00
|
|
|
{
|
|
|
|
|
return m_attributes.contains(key);
|
|
|
|
|
}
|
|
|
|
|
|
2017-11-19 19:06:09 +00:00
|
|
|
bool EntryAttributes::containsValue(const QString& value) const
|
2017-11-12 15:35:54 +00:00
|
|
|
{
|
2019-01-17 06:23:22 +00:00
|
|
|
return asConst(m_attributes).values().contains(value);
|
2017-11-12 15:35:54 +00:00
|
|
|
}
|
|
|
|
|
|
2012-04-14 13:38:20 +00:00
|
|
|
bool EntryAttributes::isProtected(const QString& key) const
|
|
|
|
|
{
|
|
|
|
|
return m_protectedAttributes.contains(key);
|
|
|
|
|
}
|
|
|
|
|
|
2017-03-05 22:47:08 +00:00
|
|
|
bool EntryAttributes::isReference(const QString& key) const
|
|
|
|
|
{
|
|
|
|
|
if (!m_attributes.contains(key)) {
|
|
|
|
|
Q_ASSERT(false);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2017-11-12 15:35:54 +00:00
|
|
|
const QString data = value(key);
|
2017-11-19 19:06:09 +00:00
|
|
|
return matchReference(data).hasMatch();
|
2017-03-07 16:19:41 +00:00
|
|
|
}
|
|
|
|
|
|
2012-04-14 13:38:20 +00:00
|
|
|
void EntryAttributes::set(const QString& key, const QString& value, bool protect)
|
|
|
|
|
{
|
2021-05-28 01:50:15 +00:00
|
|
|
bool shouldEmitModified = false;
|
2012-04-14 13:38:20 +00:00
|
|
|
|
|
|
|
|
bool addAttribute = !m_attributes.contains(key);
|
2012-05-11 12:13:22 +00:00
|
|
|
bool changeValue = !addAttribute && (m_attributes.value(key) != value);
|
2012-04-14 13:38:20 +00:00
|
|
|
bool defaultAttribute = isDefaultAttribute(key);
|
|
|
|
|
|
|
|
|
|
if (addAttribute && !defaultAttribute) {
|
2017-03-10 14:58:42 +00:00
|
|
|
emit aboutToBeAdded(key);
|
2012-04-14 13:38:20 +00:00
|
|
|
}
|
|
|
|
|
|
2012-05-11 12:13:22 +00:00
|
|
|
if (addAttribute || changeValue) {
|
2012-04-14 13:38:20 +00:00
|
|
|
m_attributes.insert(key, value);
|
2021-05-28 01:50:15 +00:00
|
|
|
shouldEmitModified = true;
|
2012-04-14 13:38:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (protect) {
|
|
|
|
|
if (!m_protectedAttributes.contains(key)) {
|
2021-05-28 01:50:15 +00:00
|
|
|
shouldEmitModified = true;
|
2012-04-14 13:38:20 +00:00
|
|
|
}
|
|
|
|
|
m_protectedAttributes.insert(key);
|
2018-03-31 20:01:30 +00:00
|
|
|
} else if (m_protectedAttributes.remove(key)) {
|
2021-05-28 01:50:15 +00:00
|
|
|
shouldEmitModified = true;
|
2012-04-14 13:38:20 +00:00
|
|
|
}
|
|
|
|
|
|
2021-05-28 01:50:15 +00:00
|
|
|
if (shouldEmitModified) {
|
|
|
|
|
emitModified();
|
2012-04-14 13:38:20 +00:00
|
|
|
}
|
|
|
|
|
|
2012-05-11 12:13:22 +00:00
|
|
|
if (defaultAttribute && changeValue) {
|
2017-03-10 14:58:42 +00:00
|
|
|
emit defaultKeyModified();
|
2018-03-31 20:01:30 +00:00
|
|
|
} else if (addAttribute) {
|
2017-03-10 14:58:42 +00:00
|
|
|
emit added(key);
|
2021-05-28 01:50:15 +00:00
|
|
|
} else if (shouldEmitModified) {
|
2017-03-10 14:58:42 +00:00
|
|
|
emit customKeyModified(key);
|
2012-04-14 13:38:20 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void EntryAttributes::remove(const QString& key)
|
|
|
|
|
{
|
|
|
|
|
Q_ASSERT(!isDefaultAttribute(key));
|
|
|
|
|
|
|
|
|
|
if (!m_attributes.contains(key)) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2017-03-10 14:58:42 +00:00
|
|
|
emit aboutToBeRemoved(key);
|
2012-04-14 13:38:20 +00:00
|
|
|
|
|
|
|
|
m_attributes.remove(key);
|
|
|
|
|
m_protectedAttributes.remove(key);
|
|
|
|
|
|
2017-03-10 14:58:42 +00:00
|
|
|
emit removed(key);
|
2021-05-28 01:50:15 +00:00
|
|
|
emitModified();
|
2012-04-14 13:38:20 +00:00
|
|
|
}
|
|
|
|
|
|
2012-04-27 10:34:15 +00:00
|
|
|
void EntryAttributes::rename(const QString& oldKey, const QString& newKey)
|
|
|
|
|
{
|
|
|
|
|
Q_ASSERT(!isDefaultAttribute(oldKey));
|
|
|
|
|
Q_ASSERT(!isDefaultAttribute(newKey));
|
|
|
|
|
|
|
|
|
|
if (!m_attributes.contains(oldKey)) {
|
|
|
|
|
Q_ASSERT(false);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2012-04-28 17:11:15 +00:00
|
|
|
if (m_attributes.contains(newKey)) {
|
|
|
|
|
Q_ASSERT(false);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2012-04-27 10:34:15 +00:00
|
|
|
QString data = value(oldKey);
|
|
|
|
|
bool protect = isProtected(oldKey);
|
|
|
|
|
|
2017-03-10 14:58:42 +00:00
|
|
|
emit aboutToRename(oldKey, newKey);
|
2012-04-28 17:11:15 +00:00
|
|
|
|
|
|
|
|
m_attributes.remove(oldKey);
|
|
|
|
|
m_attributes.insert(newKey, data);
|
|
|
|
|
if (protect) {
|
|
|
|
|
m_protectedAttributes.remove(oldKey);
|
|
|
|
|
m_protectedAttributes.insert(newKey);
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-28 01:50:15 +00:00
|
|
|
emitModified();
|
2017-03-10 14:58:42 +00:00
|
|
|
emit renamed(oldKey, newKey);
|
2012-04-27 10:34:15 +00:00
|
|
|
}
|
|
|
|
|
|
2012-04-14 17:38:45 +00:00
|
|
|
void EntryAttributes::copyCustomKeysFrom(const EntryAttributes* other)
|
2012-04-14 13:38:20 +00:00
|
|
|
{
|
2012-04-14 17:38:45 +00:00
|
|
|
if (!areCustomKeysDifferent(other)) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2012-04-14 13:38:20 +00:00
|
|
|
|
2017-03-10 14:58:42 +00:00
|
|
|
emit aboutToBeReset();
|
2012-04-14 13:38:20 +00:00
|
|
|
|
2012-04-14 17:38:45 +00:00
|
|
|
// remove all non-default keys
|
2016-09-02 17:51:51 +00:00
|
|
|
const QList<QString> keyList = keys();
|
|
|
|
|
for (const QString& key : keyList) {
|
2012-04-14 17:38:45 +00:00
|
|
|
if (!isDefaultAttribute(key)) {
|
|
|
|
|
m_attributes.remove(key);
|
|
|
|
|
m_protectedAttributes.remove(key);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-09-02 17:51:51 +00:00
|
|
|
const QList<QString> otherKeyList = other->keys();
|
|
|
|
|
for (const QString& key : otherKeyList) {
|
2012-04-14 17:38:45 +00:00
|
|
|
if (!isDefaultAttribute(key)) {
|
2012-04-14 13:38:20 +00:00
|
|
|
m_attributes.insert(key, other->value(key));
|
|
|
|
|
if (other->isProtected(key)) {
|
|
|
|
|
m_protectedAttributes.insert(key);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2012-04-14 17:38:45 +00:00
|
|
|
|
2017-03-10 14:58:42 +00:00
|
|
|
emit reset();
|
2021-05-28 01:50:15 +00:00
|
|
|
emitModified();
|
2012-04-14 13:38:20 +00:00
|
|
|
}
|
|
|
|
|
|
2012-04-14 17:38:45 +00:00
|
|
|
bool EntryAttributes::areCustomKeysDifferent(const EntryAttributes* other)
|
2012-04-14 13:38:20 +00:00
|
|
|
{
|
2012-04-14 17:38:45 +00:00
|
|
|
// check if they are equal ignoring the order of the keys
|
|
|
|
|
if (keys().toSet() != other->keys().toSet()) {
|
|
|
|
|
return true;
|
2012-04-14 16:47:40 +00:00
|
|
|
}
|
|
|
|
|
|
2016-09-02 17:51:51 +00:00
|
|
|
const QList<QString> keyList = keys();
|
|
|
|
|
for (const QString& key : keyList) {
|
2012-04-14 17:38:45 +00:00
|
|
|
if (isDefaultAttribute(key)) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (isProtected(key) != other->isProtected(key) || value(key) != other->value(key)) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2012-07-20 10:13:26 +00:00
|
|
|
void EntryAttributes::copyDataFrom(const EntryAttributes* other)
|
2012-04-23 19:06:04 +00:00
|
|
|
{
|
2012-07-20 10:13:26 +00:00
|
|
|
if (*this != *other) {
|
2017-03-10 14:58:42 +00:00
|
|
|
emit aboutToBeReset();
|
2012-04-23 19:06:04 +00:00
|
|
|
|
2012-07-20 10:13:26 +00:00
|
|
|
m_attributes = other->m_attributes;
|
|
|
|
|
m_protectedAttributes = other->m_protectedAttributes;
|
2012-04-23 19:06:04 +00:00
|
|
|
|
2017-03-10 14:58:42 +00:00
|
|
|
emit reset();
|
2021-05-28 01:50:15 +00:00
|
|
|
emitModified();
|
2012-04-23 19:06:04 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-21 19:24:57 +00:00
|
|
|
QUuid EntryAttributes::referenceUuid(const QString& key) const
|
|
|
|
|
{
|
|
|
|
|
if (!m_attributes.contains(key)) {
|
|
|
|
|
Q_ASSERT(false);
|
|
|
|
|
return {};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
auto match = matchReference(value(key));
|
|
|
|
|
if (match.hasMatch()) {
|
|
|
|
|
const QString uuid = match.captured("SearchText");
|
|
|
|
|
if (!uuid.isEmpty()) {
|
|
|
|
|
return QUuid::fromRfc4122(QByteArray::fromHex(uuid.toLatin1()));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return {};
|
|
|
|
|
}
|
|
|
|
|
|
2012-04-23 19:06:04 +00:00
|
|
|
bool EntryAttributes::operator==(const EntryAttributes& other) const
|
|
|
|
|
{
|
2018-03-31 20:01:30 +00:00
|
|
|
return (m_attributes == other.m_attributes && m_protectedAttributes == other.m_protectedAttributes);
|
2012-04-23 19:06:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool EntryAttributes::operator!=(const EntryAttributes& other) const
|
|
|
|
|
{
|
2018-03-31 20:01:30 +00:00
|
|
|
return (m_attributes != other.m_attributes || m_protectedAttributes != other.m_protectedAttributes);
|
2012-04-23 19:06:04 +00:00
|
|
|
}
|
|
|
|
|
|
2017-11-19 19:06:09 +00:00
|
|
|
QRegularExpressionMatch EntryAttributes::matchReference(const QString& text)
|
|
|
|
|
{
|
|
|
|
|
static QRegularExpression referenceRegExp(
|
2018-03-31 20:01:30 +00:00
|
|
|
"\\{REF:(?<WantedField>[TUPANI])@(?<SearchIn>[TUPANIO]):(?<SearchText>[^}]+)\\}",
|
|
|
|
|
QRegularExpression::CaseInsensitiveOption);
|
2017-11-19 19:06:09 +00:00
|
|
|
|
|
|
|
|
return referenceRegExp.match(text);
|
|
|
|
|
}
|
|
|
|
|
|
2012-04-14 17:38:45 +00:00
|
|
|
void EntryAttributes::clear()
|
|
|
|
|
{
|
2017-03-10 14:58:42 +00:00
|
|
|
emit aboutToBeReset();
|
2012-04-14 13:38:20 +00:00
|
|
|
|
|
|
|
|
m_attributes.clear();
|
|
|
|
|
m_protectedAttributes.clear();
|
|
|
|
|
|
2016-09-02 17:51:51 +00:00
|
|
|
for (const QString& key : DefaultAttributes) {
|
2012-04-14 16:47:40 +00:00
|
|
|
m_attributes.insert(key, "");
|
|
|
|
|
}
|
|
|
|
|
|
2017-03-10 14:58:42 +00:00
|
|
|
emit reset();
|
2021-05-28 01:50:15 +00:00
|
|
|
emitModified();
|
2012-04-14 13:38:20 +00:00
|
|
|
}
|
|
|
|
|
|
2018-01-15 16:20:16 +00:00
|
|
|
int EntryAttributes::attributesSize() const
|
2012-06-29 22:22:07 +00:00
|
|
|
{
|
2012-05-04 21:45:34 +00:00
|
|
|
int size = 0;
|
2018-01-16 07:34:56 +00:00
|
|
|
for (auto it = m_attributes.constBegin(); it != m_attributes.constEnd(); ++it) {
|
|
|
|
|
size += it.key().toUtf8().size() + it.value().toUtf8().size();
|
2012-05-04 21:45:34 +00:00
|
|
|
}
|
|
|
|
|
return size;
|
|
|
|
|
}
|
|
|
|
|
|
2012-04-14 13:38:20 +00:00
|
|
|
bool EntryAttributes::isDefaultAttribute(const QString& key)
|
|
|
|
|
{
|
2012-05-14 17:10:42 +00:00
|
|
|
return DefaultAttributes.contains(key);
|
2012-04-14 13:38:20 +00:00
|
|
|
}
|
2023-10-25 14:12:55 +00:00
|
|
|
|
|
|
|
|
bool EntryAttributes::isPasskeyAttribute(const QString& key)
|
|
|
|
|
{
|
|
|
|
|
return key.startsWith(PasskeyAttribute);
|
|
|
|
|
}
|