keepassxc/src/core/Entry.cpp

355 lines
6.8 KiB
C++
Raw Normal View History

2010-08-07 09:10:44 -04:00
/*
* Copyright (C) 2010 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 "Entry.h"
2011-07-08 08:51:14 -04:00
#include "core/Database.h"
#include "core/DatabaseIcons.h"
#include "core/Group.h"
#include "core/Metadata.h"
2010-08-13 12:08:06 -04:00
Entry::Entry()
{
2010-08-13 12:08:06 -04:00
m_group = 0;
2012-04-11 13:51:54 -04:00
m_updateTimeinfo = true;
2011-07-07 06:52:30 -04:00
m_iconNumber = 0;
m_autoTypeEnabled = true;
m_autoTypeObfuscation = 0;
m_attributes = new EntryAttributes(this);
connect(m_attributes, SIGNAL(modified()), this, SIGNAL(modified()));
connect(m_attributes, SIGNAL(defaultKeyModified()), SLOT(emitDataChanged()));
m_attachments = new EntryAttachments(this);
connect(m_attachments, SIGNAL(modified()), this, SIGNAL(modified()));
}
2010-08-18 10:22:48 -04:00
Entry::~Entry()
{
if (m_group) {
m_group->removeEntry(this);
}
qDeleteAll(m_history);
2010-08-18 10:22:48 -04:00
}
2012-04-11 13:51:54 -04:00
template <class T> bool Entry::set(T& property, const T& value) {
if (property != value) {
property = value;
if (m_updateTimeinfo) {
m_timeInfo.setLastModificationTime(QDateTime::currentDateTime());
m_timeInfo.setLastAccessTime(QDateTime::currentDateTime());
}
Q_EMIT modified();
return true;
}
else {
return false;
}
}
void Entry::setUpdateTimeinfo(bool value) {
m_updateTimeinfo = value;
}
Uuid Entry::uuid() const
{
return m_uuid;
}
QImage Entry::icon() const
{
2010-09-19 15:22:24 -04:00
if (m_customIcon.isNull()) {
return databaseIcons()->icon(m_iconNumber);
2010-08-24 17:12:01 -04:00
}
else {
// TODO check if database() is 0
return database()->metadata()->customIcon(m_customIcon);
2010-08-24 17:12:01 -04:00
}
}
QPixmap Entry::iconPixmap() const
{
if (m_customIcon.isNull()) {
return databaseIcons()->iconPixmap(m_iconNumber);
}
else {
QPixmap pixmap;
if (!QPixmapCache::find(m_pixmapCacheKey, &pixmap)) {
// TODO check if database() is 0
pixmap = QPixmap::fromImage(database()->metadata()->customIcon(m_customIcon));
*const_cast<QPixmapCache::Key*>(&m_pixmapCacheKey) = QPixmapCache::insert(pixmap);
}
return pixmap;
}
}
int Entry::iconNumber() const
{
return m_iconNumber;
}
Uuid Entry::iconUuid() const
{
return m_customIcon;
}
QColor Entry::foregroundColor() const
{
return m_foregroundColor;
}
QColor Entry::backgroundColor() const
{
return m_backgroundColor;
}
QString Entry::overrideUrl() const
{
return m_overrideUrl;
}
QString Entry::tags() const
{
return m_tags;
}
TimeInfo Entry::timeInfo() const
{
return m_timeInfo;
}
bool Entry::autoTypeEnabled() const
{
return m_autoTypeEnabled;
}
int Entry::autoTypeObfuscation() const
{
return m_autoTypeObfuscation;
}
QString Entry::defaultAutoTypeSequence() const
{
return m_defaultAutoTypeSequence;
}
const QList<AutoTypeAssociation>& Entry::autoTypeAssociations() const
{
return m_autoTypeAssociations;
}
QString Entry::title() const
{
return m_attributes->value("Title");
}
QString Entry::url() const
{
return m_attributes->value("URL");
}
QString Entry::username() const
{
return m_attributes->value("UserName");
}
QString Entry::password() const
{
return m_attributes->value("Password");
}
QString Entry::notes() const
{
return m_attributes->value("Notes");
}
EntryAttributes* Entry::attributes()
{
return m_attributes;
}
const EntryAttributes* Entry::attributes() const
{
return m_attributes;
}
EntryAttachments* Entry::attachments()
{
return m_attachments;
}
const EntryAttachments* Entry::attachments() const
{
return m_attachments;
}
void Entry::setUuid(const Uuid& uuid)
{
2010-08-13 12:08:06 -04:00
Q_ASSERT(!uuid.isNull());
2012-04-11 13:51:54 -04:00
set(m_uuid, uuid);
}
void Entry::setIcon(int iconNumber)
{
2010-08-14 06:24:35 -04:00
Q_ASSERT(iconNumber >= 0);
2012-04-11 13:51:54 -04:00
if (m_iconNumber != iconNumber || !m_customIcon.isNull()) {
m_iconNumber = iconNumber;
m_customIcon = Uuid();
m_pixmapCacheKey = QPixmapCache::Key();
2012-04-11 13:51:54 -04:00
Q_EMIT modified();
}
}
void Entry::setIcon(const Uuid& uuid)
{
2010-08-14 06:24:35 -04:00
Q_ASSERT(!uuid.isNull());
if (m_customIcon != uuid) {
m_customIcon = uuid;
2012-04-11 13:51:54 -04:00
m_iconNumber = 0;
2012-04-11 13:51:54 -04:00
m_pixmapCacheKey = QPixmapCache::Key();
Q_EMIT modified();
2012-04-11 13:51:54 -04:00
}
}
void Entry::setForegroundColor(const QColor& color)
{
2012-04-11 13:51:54 -04:00
set(m_foregroundColor, color);
}
void Entry::setBackgroundColor(const QColor& color)
{
2012-04-11 13:51:54 -04:00
set(m_backgroundColor, color);
}
void Entry::setOverrideUrl(const QString& url)
{
2012-04-11 13:51:54 -04:00
set(m_overrideUrl, url);
}
void Entry::setTags(const QString& tags)
{
2012-04-11 13:51:54 -04:00
set(m_tags, tags);
}
void Entry::setTimeInfo(const TimeInfo& timeInfo)
{
m_timeInfo = timeInfo;
}
void Entry::setAutoTypeEnabled(bool enable)
{
2012-04-11 13:51:54 -04:00
set(m_autoTypeEnabled, enable);
}
void Entry::setAutoTypeObfuscation(int obfuscation)
{
2012-04-11 13:51:54 -04:00
set(m_autoTypeObfuscation, obfuscation);
}
void Entry::setDefaultAutoTypeSequence(const QString& sequence)
{
2012-04-11 13:51:54 -04:00
set(m_defaultAutoTypeSequence, sequence);
}
void Entry::addAutoTypeAssociation(const AutoTypeAssociation& assoc)
{
m_autoTypeAssociations << assoc;
2012-04-11 13:51:54 -04:00
Q_EMIT modified();
}
void Entry::setTitle(const QString& title)
{
m_attributes->set("Title", title, m_attributes->isProtected("Title"));
}
void Entry::setUrl(const QString& url)
{
m_attributes->set("URL", url, m_attributes->isProtected("URL"));
}
void Entry::setUsername(const QString& username)
{
m_attributes->set("UserName", username, m_attributes->isProtected("UserName"));
}
void Entry::setPassword(const QString& password)
{
m_attributes->set("Password", password, m_attributes->isProtected("Password"));
}
void Entry::setNotes(const QString& notes)
{
m_attributes->set("Notes", notes, m_attributes->isProtected("Notes"));
}
QList<Entry*> Entry::historyItems()
{
return m_history;
}
const QList<Entry*>& Entry::historyItems() const
{
return m_history;
}
void Entry::addHistoryItem(Entry* entry)
{
Q_ASSERT(!entry->parent());
m_history.append(entry);
2012-04-11 13:51:54 -04:00
Q_EMIT modified();
}
2010-10-06 16:54:07 -04:00
Group* Entry::group()
{
return m_group;
}
void Entry::setGroup(Group* group)
2010-08-07 09:10:44 -04:00
{
if (m_group) {
2010-08-13 12:08:06 -04:00
m_group->removeEntry(this);
}
group->addEntry(this);
m_group = group;
QObject::setParent(group);
2010-08-07 09:10:44 -04:00
}
void Entry::emitDataChanged()
{
Q_EMIT dataChanged(this);
}
const Database* Entry::database() const
{
if (m_group) {
return m_group->database();
}
else {
return 0;
}
}