Skip to content

Commit

Permalink
Show what changed between entry history items
Browse files Browse the repository at this point in the history
* Also show what is changed on the current state
* Closes #2621
  • Loading branch information
shemeshg authored and droidmonkey committed Dec 21, 2021
1 parent 12990e5 commit be17d20
Show file tree
Hide file tree
Showing 6 changed files with 277 additions and 32 deletions.
98 changes: 98 additions & 0 deletions share/translations/keepassxc_en.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3614,6 +3614,62 @@ Would you like to overwrite the existing attachment?</source>
<source>URL</source>
<translation>URL</translation>
</message>
<message>
<source>Age</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>Difference</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>Size</source>
<translation type="unfinished">Size</translation>
</message>
<message>
<source>Password</source>
<translation type="unfinished">Password</translation>
</message>
<message>
<source>Notes</source>
<translation type="unfinished">Notes</translation>
</message>
<message>
<source>Custom Attributes</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>Icon</source>
<translation type="unfinished">Icon</translation>
</message>
<message>
<source>Color</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>Expiration</source>
<translation type="unfinished">Expiration</translation>
</message>
<message>
<source>TOTP</source>
<translation type="unfinished">TOTP</translation>
</message>
<message>
<source>Custom Data</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>Attachments</source>
<translation type="unfinished">Attachments</translation>
</message>
<message>
<source>Auto-Type</source>
<translation type="unfinished">Auto-Type</translation>
</message>
<message>
<source>Current (%1)</source>
<translation type="unfinished"></translation>
</message>
</context>
<context>
<name>EntryModel</name>
Expand Down Expand Up @@ -7633,6 +7689,48 @@ Please consider generating a new key file.</source>
<source>KeeShare</source>
<translation type="unfinished"></translation>
</message>
<message numerus="yes">
<source>over %1 year(s)</source>
<translation type="unfinished">
<numerusform></numerusform>
<numerusform></numerusform>
</translation>
</message>
<message numerus="yes">
<source>about %1 month(s)</source>
<translation type="unfinished">
<numerusform></numerusform>
<numerusform></numerusform>
</translation>
</message>
<message numerus="yes">
<source>%1 week(s)</source>
<translation type="unfinished">
<numerusform></numerusform>
<numerusform></numerusform>
</translation>
</message>
<message numerus="yes">
<source>%1 day(s)</source>
<translation type="unfinished">
<numerusform></numerusform>
<numerusform></numerusform>
</translation>
</message>
<message numerus="yes">
<source>%1 hour(s)</source>
<translation type="unfinished">
<numerusform></numerusform>
<numerusform></numerusform>
</translation>
</message>
<message numerus="yes">
<source>%1 minute(s)</source>
<translation type="unfinished">
<numerusform></numerusform>
<numerusform></numerusform>
</translation>
</message>
</context>
<context>
<name>QtIOCompressor</name>
Expand Down
31 changes: 31 additions & 0 deletions src/core/Tools.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,37 @@ namespace Tools
return QString("%1 %2").arg(QLocale().toString(size, 'f', precision), units.at(i));
}

QString humanReadableTimeDifference(qint64 seconds)
{
constexpr double secondsInHour = 3600;
constexpr double secondsInDay = secondsInHour * 24;
constexpr double secondsInWeek = secondsInDay * 7;
constexpr double secondsInMonth = secondsInDay * 30; // Approximation
constexpr double secondsInYear = secondsInDay * 365;

seconds = abs(seconds);

if (seconds >= secondsInYear) {
auto years = std::floor(seconds / secondsInYear);
return QObject::tr("over %1 year(s)", nullptr, years).arg(years);
} else if (seconds >= secondsInMonth) {
auto months = std::round(seconds / secondsInMonth);
return QObject::tr("about %1 month(s)", nullptr, months).arg(months);
} else if (seconds >= secondsInWeek) {
auto weeks = std::round(seconds / secondsInWeek);
return QObject::tr("%1 week(s)", nullptr, weeks).arg(weeks);
} else if (seconds >= secondsInDay) {
auto days = std::floor(seconds / secondsInDay);
return QObject::tr("%1 day(s)", nullptr, days).arg(days);
} else if (seconds >= secondsInHour) {
auto hours = std::floor(seconds / secondsInHour);
return QObject::tr("%1 hour(s)", nullptr, hours).arg(hours);
}

auto minutes = std::floor(seconds / 60);
return QObject::tr("%1 minute(s)", nullptr, minutes).arg(minutes);
}

bool readFromDevice(QIODevice* device, QByteArray& data, int size)
{
QByteArray buffer;
Expand Down
2 changes: 2 additions & 0 deletions src/core/Tools.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

#include "core/Global.h"

#include <QDateTime>
#include <QProcessEnvironment>

class QIODevice;
Expand All @@ -30,6 +31,7 @@ namespace Tools
{
QString debugInfo();
QString humanReadableFileSize(qint64 bytes, quint32 precision = 2);
QString humanReadableTimeDifference(qint64 seconds);
bool readFromDevice(QIODevice* device, QByteArray& data, int size = 16384);
bool readAllFromDevice(QIODevice* device, QByteArray& data);
bool isHex(const QByteArray& ba);
Expand Down
18 changes: 11 additions & 7 deletions src/gui/entry/EditEntryWidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -504,7 +504,9 @@ void EditEntryWidget::emitHistoryEntryActivated(const QModelIndex& index)
Q_ASSERT(!m_history);

Entry* entry = m_historyModel->entryFromIndex(index);
emit historyEntryActivated(entry);
if (entry) {
emit historyEntryActivated(entry);
}
}

void EditEntryWidget::histEntryActivated(const QModelIndex& index)
Expand All @@ -521,7 +523,7 @@ void EditEntryWidget::updateHistoryButtons(const QModelIndex& current, const QMo
{
Q_UNUSED(previous);

if (current.isValid()) {
if (m_historyModel->entryFromIndex(current)) {
m_historyUi->showButton->setEnabled(true);
m_historyUi->restoreButton->setEnabled(true);
m_historyUi->deleteButton->setEnabled(true);
Expand Down Expand Up @@ -1025,7 +1027,7 @@ void EditEntryWidget::setForms(Entry* entry, bool restore)
m_editWidgetProperties->setFields(entry->timeInfo(), entry->uuid());

if (!m_history && !restore) {
m_historyModel->setEntries(entry->historyItems());
m_historyModel->setEntries(entry->historyItems(), entry);
m_historyUi->historyView->sortByColumn(0, Qt::DescendingOrder);
}
if (m_historyModel->rowCount() > 0) {
Expand Down Expand Up @@ -1129,7 +1131,8 @@ bool EditEntryWidget::commitEntry()
m_entry->endUpdate();
}

m_historyModel->setEntries(m_entry->historyItems());
m_historyModel->setEntries(m_entry->historyItems(), m_entry);
m_advancedUi->attachmentsWidget->linkAttachments(m_entry->attachments());

showMessage(tr("Entry updated successfully."), MessageWidget::Positive);
setModified(false);
Expand Down Expand Up @@ -1538,16 +1541,17 @@ void EditEntryWidget::showHistoryEntry()
void EditEntryWidget::restoreHistoryEntry()
{
QModelIndex index = m_sortModel->mapToSource(m_historyUi->historyView->currentIndex());
if (index.isValid()) {
setForms(m_historyModel->entryFromIndex(index), true);
auto entry = m_historyModel->entryFromIndex(index);
if (entry) {
setForms(entry, true);
setModified(true);
}
}

void EditEntryWidget::deleteHistoryEntry()
{
QModelIndex index = m_sortModel->mapToSource(m_historyUi->historyView->currentIndex());
if (index.isValid()) {
if (m_historyModel->entryFromIndex(index)) {
m_historyModel->deleteIndex(index);
if (m_historyModel->rowCount() > 0) {
m_historyUi->deleteAllButton->setEnabled(true);
Expand Down
Loading

0 comments on commit be17d20

Please sign in to comment.