Show what changed between entry history items
* Also show what is changed on the current state * Closes #2621
This commit is contained in:
@@ -17,8 +17,12 @@
|
||||
|
||||
#include "EntryHistoryModel.h"
|
||||
|
||||
#include "core/Clock.h"
|
||||
#include "core/Entry.h"
|
||||
#include "core/Global.h"
|
||||
#include "core/Tools.h"
|
||||
|
||||
#include <QFont>
|
||||
|
||||
EntryHistoryModel::EntryHistoryModel(QObject* parent)
|
||||
: QAbstractTableModel(parent)
|
||||
@@ -27,8 +31,11 @@ EntryHistoryModel::EntryHistoryModel(QObject* parent)
|
||||
|
||||
Entry* EntryHistoryModel::entryFromIndex(const QModelIndex& index) const
|
||||
{
|
||||
Q_ASSERT(index.isValid() && index.row() < m_historyEntries.size());
|
||||
return m_historyEntries.at(index.row());
|
||||
if (!index.isValid() || index.row() >= m_historyEntries.size()) {
|
||||
return nullptr;
|
||||
}
|
||||
auto entry = m_historyEntries.at(index.row());
|
||||
return entry == m_parentEntry ? nullptr : entry;
|
||||
}
|
||||
|
||||
int EntryHistoryModel::columnCount(const QModelIndex& parent) const
|
||||
@@ -48,31 +55,50 @@ int EntryHistoryModel::rowCount(const QModelIndex& parent) const
|
||||
|
||||
QVariant EntryHistoryModel::data(const QModelIndex& index, int role) const
|
||||
{
|
||||
if (!index.isValid()) {
|
||||
return QVariant();
|
||||
if (index.row() >= m_historyEntries.size()) {
|
||||
return {};
|
||||
}
|
||||
const auto entry = m_historyEntries[index.row()];
|
||||
|
||||
if (role == Qt::DisplayRole || role == Qt::UserRole) {
|
||||
Entry* entry = entryFromIndex(index);
|
||||
const TimeInfo& timeInfo = entry->timeInfo();
|
||||
QDateTime lastModificationLocalTime = timeInfo.lastModificationTime().toLocalTime();
|
||||
QDateTime lastModified = entry->timeInfo().lastModificationTime().toLocalTime();
|
||||
QDateTime now = Clock::currentDateTime();
|
||||
|
||||
switch (index.column()) {
|
||||
case 0:
|
||||
if (role == Qt::DisplayRole) {
|
||||
return lastModificationLocalTime.toString(Qt::SystemLocaleShortDate);
|
||||
return lastModified.toString(Qt::SystemLocaleShortDate);
|
||||
} else {
|
||||
return lastModificationLocalTime;
|
||||
return lastModified;
|
||||
}
|
||||
case 1:
|
||||
return entry->title();
|
||||
case 2:
|
||||
return entry->username();
|
||||
case 3:
|
||||
return entry->url();
|
||||
case 1: {
|
||||
const auto seconds = lastModified.secsTo(now);
|
||||
if (role == Qt::DisplayRole) {
|
||||
if (entry == m_parentEntry) {
|
||||
return tr("Current (%1)").arg(Tools::humanReadableTimeDifference(seconds));
|
||||
}
|
||||
return Tools::humanReadableTimeDifference(seconds);
|
||||
}
|
||||
return seconds;
|
||||
}
|
||||
case 2:
|
||||
if (index.row() < m_historyModifications.size()) {
|
||||
return m_historyModifications[index.row()];
|
||||
}
|
||||
return {};
|
||||
case 3:
|
||||
if (role == Qt::DisplayRole) {
|
||||
return Tools::humanReadableFileSize(entry->size(), 0);
|
||||
}
|
||||
return entry->size();
|
||||
}
|
||||
} else if (role == Qt::FontRole && entry == m_parentEntry) {
|
||||
QFont font;
|
||||
font.setBold(true);
|
||||
return font;
|
||||
}
|
||||
|
||||
return QVariant();
|
||||
return {};
|
||||
}
|
||||
|
||||
QVariant EntryHistoryModel::headerData(int section, Qt::Orientation orientation, int role) const
|
||||
@@ -82,24 +108,29 @@ QVariant EntryHistoryModel::headerData(int section, Qt::Orientation orientation,
|
||||
case 0:
|
||||
return tr("Last modified");
|
||||
case 1:
|
||||
return tr("Title");
|
||||
return tr("Age");
|
||||
case 2:
|
||||
return tr("Username");
|
||||
return tr("Difference");
|
||||
case 3:
|
||||
return tr("URL");
|
||||
return tr("Size");
|
||||
}
|
||||
}
|
||||
|
||||
return QVariant();
|
||||
return {};
|
||||
}
|
||||
|
||||
void EntryHistoryModel::setEntries(const QList<Entry*>& entries)
|
||||
void EntryHistoryModel::setEntries(const QList<Entry*>& entries, Entry* parentEntry)
|
||||
{
|
||||
beginResetModel();
|
||||
|
||||
m_parentEntry = parentEntry;
|
||||
m_historyEntries = entries;
|
||||
m_historyEntries << parentEntry;
|
||||
// Sort the entries by last modified (newest -> oldest) so we can calculate the differences
|
||||
std::sort(m_historyEntries.begin(), m_historyEntries.end(), [](const Entry* lhs, const Entry* rhs) {
|
||||
return lhs->timeInfo().lastModificationTime() > rhs->timeInfo().lastModificationTime();
|
||||
});
|
||||
m_deletedHistoryEntries.clear();
|
||||
|
||||
calculateHistoryModifications();
|
||||
endResetModel();
|
||||
}
|
||||
|
||||
@@ -125,8 +156,8 @@ QList<Entry*> EntryHistoryModel::deletedEntries()
|
||||
|
||||
void EntryHistoryModel::deleteIndex(QModelIndex index)
|
||||
{
|
||||
if (index.isValid()) {
|
||||
Entry* entry = entryFromIndex(index);
|
||||
auto entry = entryFromIndex(index);
|
||||
if (entry) {
|
||||
beginRemoveRows(QModelIndex(), m_historyEntries.indexOf(entry), m_historyEntries.indexOf(entry));
|
||||
m_historyEntries.removeAll(entry);
|
||||
m_deletedHistoryEntries << entry;
|
||||
@@ -141,8 +172,83 @@ void EntryHistoryModel::deleteAll()
|
||||
beginRemoveRows(QModelIndex(), 0, m_historyEntries.size() - 1);
|
||||
|
||||
for (Entry* entry : asConst(m_historyEntries)) {
|
||||
m_deletedHistoryEntries << entry;
|
||||
if (entry != m_parentEntry) {
|
||||
m_deletedHistoryEntries << entry;
|
||||
}
|
||||
}
|
||||
m_historyEntries.clear();
|
||||
endRemoveRows();
|
||||
}
|
||||
|
||||
void EntryHistoryModel::calculateHistoryModifications()
|
||||
{
|
||||
m_historyModifications.clear();
|
||||
|
||||
Entry* compare = nullptr;
|
||||
for (const auto curr : m_historyEntries) {
|
||||
if (!compare) {
|
||||
compare = curr;
|
||||
continue;
|
||||
}
|
||||
|
||||
QStringList modifiedFields;
|
||||
|
||||
if (*curr->attributes() != *compare->attributes()) {
|
||||
bool foundAttribute = false;
|
||||
|
||||
if (curr->title() != compare->title()) {
|
||||
modifiedFields << tr("Title");
|
||||
foundAttribute = true;
|
||||
}
|
||||
if (curr->username() != compare->username()) {
|
||||
modifiedFields << tr("Username");
|
||||
foundAttribute = true;
|
||||
}
|
||||
if (curr->password() != compare->password()) {
|
||||
modifiedFields << tr("Password");
|
||||
foundAttribute = true;
|
||||
}
|
||||
if (curr->url() != compare->url()) {
|
||||
modifiedFields << tr("URL");
|
||||
foundAttribute = true;
|
||||
}
|
||||
if (curr->notes() != compare->notes()) {
|
||||
modifiedFields << tr("Notes");
|
||||
foundAttribute = true;
|
||||
}
|
||||
|
||||
if (!foundAttribute) {
|
||||
modifiedFields << tr("Custom Attributes");
|
||||
}
|
||||
}
|
||||
if (curr->iconNumber() != compare->iconNumber() || curr->iconUuid() != compare->iconUuid()) {
|
||||
modifiedFields << tr("Icon");
|
||||
}
|
||||
if (curr->foregroundColor() != compare->foregroundColor()
|
||||
|| curr->backgroundColor() != compare->backgroundColor()) {
|
||||
modifiedFields << tr("Color");
|
||||
}
|
||||
if (curr->timeInfo().expires() != compare->timeInfo().expires()
|
||||
|| curr->timeInfo().expiryTime() != compare->timeInfo().expiryTime()) {
|
||||
modifiedFields << tr("Expiration");
|
||||
}
|
||||
if (curr->totpSettingsString() != compare->totpSettingsString()) {
|
||||
modifiedFields << tr("TOTP");
|
||||
}
|
||||
if (*curr->customData() != *compare->customData()) {
|
||||
modifiedFields << tr("Custom Data");
|
||||
}
|
||||
if (*curr->attachments() != *compare->attachments()) {
|
||||
modifiedFields << tr("Attachments");
|
||||
}
|
||||
if (*curr->autoTypeAssociations() != *compare->autoTypeAssociations()
|
||||
|| curr->autoTypeEnabled() != compare->autoTypeEnabled()
|
||||
|| curr->defaultAutoTypeSequence() != compare->defaultAutoTypeSequence()) {
|
||||
modifiedFields << tr("Auto-Type");
|
||||
}
|
||||
|
||||
m_historyModifications << modifiedFields.join(", ");
|
||||
|
||||
compare = curr;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user