2012-04-14 09:38:20 -04: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"
|
|
|
|
|
2013-12-01 03:43:41 -05:00
|
|
|
const QString EntryAttributes::TitleKey = "Title";
|
2013-11-30 10:05:10 -05:00
|
|
|
const QString EntryAttributes::UserNameKey = "UserName";
|
|
|
|
const QString EntryAttributes::PasswordKey = "Password";
|
2013-12-01 03:43:41 -05:00
|
|
|
const QString EntryAttributes::URLNameKey = "URL";
|
|
|
|
const QString EntryAttributes::NotesKey = "Notes";
|
|
|
|
const QStringList EntryAttributes::DefaultAttributes(QStringList() << TitleKey << UserNameKey
|
|
|
|
<< PasswordKey << URLNameKey << NotesKey);
|
2012-04-14 09:38:20 -04:00
|
|
|
|
|
|
|
EntryAttributes::EntryAttributes(QObject* parent)
|
|
|
|
: QObject(parent)
|
|
|
|
{
|
2012-04-14 12:47:40 -04:00
|
|
|
clear();
|
2012-04-14 09:38:20 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
QList<QString> EntryAttributes::keys() const
|
|
|
|
{
|
|
|
|
return m_attributes.keys();
|
|
|
|
}
|
|
|
|
|
2012-10-23 18:15:23 -04:00
|
|
|
QList<QString> EntryAttributes::customKeys()
|
|
|
|
{
|
|
|
|
QList<QString> customKeys;
|
|
|
|
Q_FOREACH (const QString& key, keys()) {
|
|
|
|
if (!isDefaultAttribute(key)) {
|
|
|
|
customKeys.append(key);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return customKeys;
|
|
|
|
}
|
|
|
|
|
2012-04-14 09:38:20 -04:00
|
|
|
QString EntryAttributes::value(const QString& key) const
|
|
|
|
{
|
|
|
|
return m_attributes.value(key);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool EntryAttributes::isProtected(const QString& key) const
|
|
|
|
{
|
|
|
|
return m_protectedAttributes.contains(key);
|
|
|
|
}
|
|
|
|
|
|
|
|
void EntryAttributes::set(const QString& key, const QString& value, bool protect)
|
|
|
|
{
|
|
|
|
bool emitModified = false;
|
|
|
|
|
|
|
|
bool addAttribute = !m_attributes.contains(key);
|
2012-05-11 08:13:22 -04:00
|
|
|
bool changeValue = !addAttribute && (m_attributes.value(key) != value);
|
2012-04-14 09:38:20 -04:00
|
|
|
bool defaultAttribute = isDefaultAttribute(key);
|
|
|
|
|
|
|
|
if (addAttribute && !defaultAttribute) {
|
|
|
|
Q_EMIT aboutToBeAdded(key);
|
|
|
|
}
|
|
|
|
|
2012-05-11 08:13:22 -04:00
|
|
|
if (addAttribute || changeValue) {
|
2012-04-14 09:38:20 -04:00
|
|
|
m_attributes.insert(key, value);
|
|
|
|
emitModified = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (protect) {
|
|
|
|
if (!m_protectedAttributes.contains(key)) {
|
|
|
|
emitModified = true;
|
|
|
|
}
|
|
|
|
m_protectedAttributes.insert(key);
|
|
|
|
}
|
|
|
|
else if (m_protectedAttributes.remove(key)) {
|
|
|
|
emitModified = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (emitModified) {
|
|
|
|
Q_EMIT modified();
|
|
|
|
}
|
|
|
|
|
2012-05-11 08:13:22 -04:00
|
|
|
if (defaultAttribute && changeValue) {
|
2012-04-14 09:38:20 -04:00
|
|
|
Q_EMIT defaultKeyModified();
|
|
|
|
}
|
|
|
|
else if (addAttribute) {
|
|
|
|
Q_EMIT added(key);
|
|
|
|
}
|
|
|
|
else if (emitModified) {
|
|
|
|
Q_EMIT customKeyModified(key);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void EntryAttributes::remove(const QString& key)
|
|
|
|
{
|
|
|
|
Q_ASSERT(!isDefaultAttribute(key));
|
|
|
|
|
|
|
|
if (!m_attributes.contains(key)) {
|
|
|
|
Q_ASSERT(false);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Q_EMIT aboutToBeRemoved(key);
|
|
|
|
|
|
|
|
m_attributes.remove(key);
|
|
|
|
m_protectedAttributes.remove(key);
|
|
|
|
|
|
|
|
Q_EMIT removed(key);
|
|
|
|
Q_EMIT modified();
|
|
|
|
}
|
|
|
|
|
2012-04-27 06:34:15 -04: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 13:11:15 -04:00
|
|
|
if (m_attributes.contains(newKey)) {
|
|
|
|
Q_ASSERT(false);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-04-27 06:34:15 -04:00
|
|
|
QString data = value(oldKey);
|
|
|
|
bool protect = isProtected(oldKey);
|
|
|
|
|
2012-04-28 13:11:15 -04:00
|
|
|
Q_EMIT aboutToRename(oldKey, newKey);
|
|
|
|
|
|
|
|
m_attributes.remove(oldKey);
|
|
|
|
m_attributes.insert(newKey, data);
|
|
|
|
if (protect) {
|
|
|
|
m_protectedAttributes.remove(oldKey);
|
|
|
|
m_protectedAttributes.insert(newKey);
|
|
|
|
}
|
|
|
|
|
|
|
|
Q_EMIT modified();
|
|
|
|
Q_EMIT renamed(oldKey, newKey);
|
2012-04-27 06:34:15 -04:00
|
|
|
}
|
|
|
|
|
2012-04-14 13:38:45 -04:00
|
|
|
void EntryAttributes::copyCustomKeysFrom(const EntryAttributes* other)
|
2012-04-14 09:38:20 -04:00
|
|
|
{
|
2012-04-14 13:38:45 -04:00
|
|
|
if (!areCustomKeysDifferent(other)) {
|
|
|
|
return;
|
|
|
|
}
|
2012-04-14 09:38:20 -04:00
|
|
|
|
2012-04-14 13:38:45 -04:00
|
|
|
Q_EMIT aboutToBeReset();
|
2012-04-14 09:38:20 -04:00
|
|
|
|
2012-04-14 13:38:45 -04:00
|
|
|
// remove all non-default keys
|
|
|
|
Q_FOREACH (const QString& key, keys()) {
|
|
|
|
if (!isDefaultAttribute(key)) {
|
|
|
|
m_attributes.remove(key);
|
|
|
|
m_protectedAttributes.remove(key);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Q_FOREACH (const QString& key, other->keys()) {
|
|
|
|
if (!isDefaultAttribute(key)) {
|
2012-04-14 09:38:20 -04:00
|
|
|
m_attributes.insert(key, other->value(key));
|
|
|
|
if (other->isProtected(key)) {
|
|
|
|
m_protectedAttributes.insert(key);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-04-14 13:38:45 -04:00
|
|
|
|
|
|
|
Q_EMIT reset();
|
|
|
|
Q_EMIT modified();
|
2012-04-14 09:38:20 -04:00
|
|
|
}
|
|
|
|
|
2012-04-14 13:38:45 -04:00
|
|
|
bool EntryAttributes::areCustomKeysDifferent(const EntryAttributes* other)
|
2012-04-14 09:38:20 -04:00
|
|
|
{
|
2012-04-14 13:38:45 -04:00
|
|
|
// check if they are equal ignoring the order of the keys
|
|
|
|
if (keys().toSet() != other->keys().toSet()) {
|
|
|
|
return true;
|
2012-04-14 12:47:40 -04:00
|
|
|
}
|
|
|
|
|
2012-04-14 13:38:45 -04:00
|
|
|
Q_FOREACH (const QString& key, keys()) {
|
|
|
|
if (isDefaultAttribute(key)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isProtected(key) != other->isProtected(key) || value(key) != other->value(key)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-07-20 06:13:26 -04:00
|
|
|
void EntryAttributes::copyDataFrom(const EntryAttributes* other)
|
2012-04-23 15:06:04 -04:00
|
|
|
{
|
2012-07-20 06:13:26 -04:00
|
|
|
if (*this != *other) {
|
2012-04-23 15:06:04 -04:00
|
|
|
Q_EMIT aboutToBeReset();
|
|
|
|
|
2012-07-20 06:13:26 -04:00
|
|
|
m_attributes = other->m_attributes;
|
|
|
|
m_protectedAttributes = other->m_protectedAttributes;
|
2012-04-23 15:06:04 -04:00
|
|
|
|
|
|
|
Q_EMIT reset();
|
|
|
|
Q_EMIT modified();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool EntryAttributes::operator==(const EntryAttributes& other) const
|
|
|
|
{
|
|
|
|
return (m_attributes == other.m_attributes
|
|
|
|
&& m_protectedAttributes == other.m_protectedAttributes);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool EntryAttributes::operator!=(const EntryAttributes& other) const
|
|
|
|
{
|
|
|
|
return (m_attributes != other.m_attributes
|
|
|
|
|| m_protectedAttributes != other.m_protectedAttributes);
|
|
|
|
}
|
|
|
|
|
2012-04-14 13:38:45 -04:00
|
|
|
void EntryAttributes::clear()
|
|
|
|
{
|
2012-04-14 09:38:20 -04:00
|
|
|
Q_EMIT aboutToBeReset();
|
|
|
|
|
|
|
|
m_attributes.clear();
|
|
|
|
m_protectedAttributes.clear();
|
|
|
|
|
2012-05-14 13:10:42 -04:00
|
|
|
Q_FOREACH (const QString& key, DefaultAttributes) {
|
2012-04-14 12:47:40 -04:00
|
|
|
m_attributes.insert(key, "");
|
|
|
|
}
|
|
|
|
|
2012-04-14 09:38:20 -04:00
|
|
|
Q_EMIT reset();
|
|
|
|
Q_EMIT modified();
|
|
|
|
}
|
|
|
|
|
2012-06-29 18:22:07 -04:00
|
|
|
int EntryAttributes::attributesSize()
|
|
|
|
{
|
2012-05-04 17:45:34 -04:00
|
|
|
int size = 0;
|
|
|
|
|
2012-05-10 03:56:41 -04:00
|
|
|
QMapIterator<QString, QString> i(m_attributes);
|
|
|
|
while (i.hasNext()) {
|
|
|
|
i.next();
|
|
|
|
size += i.value().toUtf8().size();
|
2012-05-04 17:45:34 -04:00
|
|
|
}
|
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
2012-04-14 09:38:20 -04:00
|
|
|
bool EntryAttributes::isDefaultAttribute(const QString& key)
|
|
|
|
{
|
2012-05-14 13:10:42 -04:00
|
|
|
return DefaultAttributes.contains(key);
|
2012-04-14 09:38:20 -04:00
|
|
|
}
|