From 4797926be6b2e720532e6a9d7c4470b3dcf67630 Mon Sep 17 00:00:00 2001 From: Florian Geyer Date: Fri, 18 May 2012 19:22:22 +0200 Subject: [PATCH] History items can now be removed. Refs #23 --- src/core/Entry.cpp | 15 +++++++ src/core/Entry.h | 1 + src/gui/entry/EditEntryWidget.cpp | 53 +++++++++++++++++++++++-- src/gui/entry/EditEntryWidget.h | 3 ++ src/gui/entry/EditEntryWidgetHistory.ui | 34 ++++++++++++++++ src/gui/entry/EntryHistoryModel.cpp | 24 +++++++++-- src/gui/entry/EntryHistoryModel.h | 3 ++ 7 files changed, 126 insertions(+), 7 deletions(-) diff --git a/src/core/Entry.cpp b/src/core/Entry.cpp index 878bb641..a3fefde4 100644 --- a/src/core/Entry.cpp +++ b/src/core/Entry.cpp @@ -367,6 +367,21 @@ void Entry::addHistoryItem(Entry* entry) Q_EMIT modified(); } +void Entry::removeHistoryItems(QList historyEntries) +{ + bool emitModified = historyEntries.count() > 0; + Q_FOREACH (Entry* entry, historyEntries) { + Q_ASSERT(!entry->parent()); + Q_ASSERT(entry->uuid() == uuid()); + Q_ASSERT(m_history.removeAll(entry) > 0); + delete entry; + } + + if (emitModified) { + Q_EMIT modified(); + } +} + void Entry::truncateHistory() { const Database* db = database(); diff --git a/src/core/Entry.h b/src/core/Entry.h index 0f45bef0..1a32a8c8 100644 --- a/src/core/Entry.h +++ b/src/core/Entry.h @@ -116,6 +116,7 @@ public: QList historyItems(); const QList& historyItems() const; void addHistoryItem(Entry* entry); + void removeHistoryItems(QList historyEntries); void truncateHistory(); Entry* clone() const; diff --git a/src/gui/entry/EditEntryWidget.cpp b/src/gui/entry/EditEntryWidget.cpp index 3622fc09..89c3fc56 100644 --- a/src/gui/entry/EditEntryWidget.cpp +++ b/src/gui/entry/EditEntryWidget.cpp @@ -98,6 +98,11 @@ EditEntryWidget::EditEntryWidget(QWidget* parent) connect(m_historyUi->historyView, SIGNAL(activated(const QModelIndex&)), SLOT(emitHistoryEntryActivated(const QModelIndex&))); + connect(m_historyUi->historyView->selectionModel(), + SIGNAL(currentChanged(QModelIndex ,QModelIndex)), + SLOT(updateHistoryButtons(QModelIndex, QModelIndex))); + connect(m_historyUi->showButton, SIGNAL(clicked()), SLOT(showHistoryEntry())); + connect(m_historyUi->deleteButton, SIGNAL(clicked()), SLOT(deleteHistoryEntry())); connect(this, SIGNAL(accepted()), SLOT(saveEntry())); connect(this, SIGNAL(rejected()), SLOT(cancel())); @@ -119,6 +124,22 @@ void EditEntryWidget::emitHistoryEntryActivated(const QModelIndex& index) Q_EMIT historyEntryActivated(entry); } +void EditEntryWidget::updateHistoryButtons(const QModelIndex& current, const QModelIndex& previous) +{ + Q_UNUSED(previous); + + if (current.isValid()) { + m_historyUi->showButton->setEnabled(true); + m_historyUi->restoreButton->setEnabled(false); // TODO: + m_historyUi->deleteButton->setEnabled(true); + } + else { + m_historyUi->showButton->setEnabled(false); + m_historyUi->restoreButton->setEnabled(false); + m_historyUi->deleteButton->setEnabled(false); + } +} + void EditEntryWidget::loadEntry(Entry* entry, bool create, bool history, const QString& groupName, Database* database) { @@ -176,6 +197,11 @@ void EditEntryWidget::loadEntry(Entry* entry, bool create, bool history, const Q m_iconsWidget->setEnabled(!history); m_historyWidget->setEnabled(!history); + m_historyUi->showButton->setEnabled(false); + m_historyUi->restoreButton->setEnabled(false); + m_historyUi->deleteButton->setEnabled(false); + + setForms(entry); setCurrentRow(0); @@ -212,7 +238,9 @@ void EditEntryWidget::setForms(const Entry* entry) iconStruct.uuid = entry->iconUuid(); iconStruct.number = entry->iconNumber(); m_iconsWidget->load(entry->uuid(), m_database, iconStruct); - m_historyModel->setEntries(entry->historyItems()); + if (!m_history) { + m_historyModel->setEntries(entry->historyItems()); + } } void EditEntryWidget::saveEntry() @@ -222,7 +250,6 @@ void EditEntryWidget::saveEntry() m_database = 0; m_entryAttributes->clear(); m_entryAttachments->clear(); - m_historyModel->clear(); Q_EMIT editFinished(false); return; } @@ -240,6 +267,10 @@ void EditEntryWidget::saveEntry() m_currentAttribute = QPersistentModelIndex(); + // must stand before beginUpdate() + // we don't want to create a new history item, if only the history has changed + m_entry->removeHistoryItems(m_historyModel->deletedEntries()); + if (!m_create) { m_entry->beginUpdate(); } @@ -272,6 +303,7 @@ void EditEntryWidget::saveEntry() m_entry->endUpdate(); } + m_entry = 0; m_database = 0; m_entryAttributes->clear(); @@ -288,7 +320,6 @@ void EditEntryWidget::cancel() m_database = 0; m_entryAttributes->clear(); m_entryAttachments->clear(); - m_historyModel->clear(); Q_EMIT editFinished(false); return; } @@ -487,3 +518,19 @@ void EditEntryWidget::removeCurrentAttachment() QString key = m_attachmentsModel->keyByIndex(index); m_entryAttachments->remove(key); } + +void EditEntryWidget::showHistoryEntry() +{ + QModelIndex index = m_historyUi->historyView->currentIndex(); + if (index.isValid()) { + emitHistoryEntryActivated(index); + } +} + +void EditEntryWidget::deleteHistoryEntry() +{ + QModelIndex index = m_historyUi->historyView->currentIndex(); + if (index.isValid()) { + m_historyModel->deleteIndex(index); + } +} diff --git a/src/gui/entry/EditEntryWidget.h b/src/gui/entry/EditEntryWidget.h index 5d88cdd0..111cfffe 100644 --- a/src/gui/entry/EditEntryWidget.h +++ b/src/gui/entry/EditEntryWidget.h @@ -71,7 +71,10 @@ private Q_SLOTS: void insertAttachment(); void saveCurrentAttachment(); void removeCurrentAttachment(); + void showHistoryEntry(); + void deleteHistoryEntry(); void emitHistoryEntryActivated(const QModelIndex &index); + void updateHistoryButtons(const QModelIndex& current, const QModelIndex& previous); private: bool passwordsEqual(); diff --git a/src/gui/entry/EditEntryWidgetHistory.ui b/src/gui/entry/EditEntryWidgetHistory.ui index e92485b6..2edd1446 100644 --- a/src/gui/entry/EditEntryWidgetHistory.ui +++ b/src/gui/entry/EditEntryWidgetHistory.ui @@ -17,6 +17,40 @@ + + + + + + false + + + Show + + + + + + + false + + + Restore + + + + + + + false + + + Delete + + + + + diff --git a/src/gui/entry/EntryHistoryModel.cpp b/src/gui/entry/EntryHistoryModel.cpp index 3e6cbba7..bd22865a 100644 --- a/src/gui/entry/EntryHistoryModel.cpp +++ b/src/gui/entry/EntryHistoryModel.cpp @@ -52,14 +52,12 @@ QVariant EntryHistoryModel::data(const QModelIndex& index, int role) const return QVariant(); } - int row = index.row(); - int column = index.column(); - Entry* entry = m_historyEntries.at(row); + Entry* entry = entryFromIndex(index); TimeInfo timeInfo = entry->timeInfo(); QDateTime lastModificationLocalTime = timeInfo.lastModificationTime().toLocalTime(); if (role == Qt::DisplayRole) { - switch (column) { + switch (index.column()) { case 0: return lastModificationLocalTime.toString(Qt::SystemLocaleShortDate); case 1: @@ -97,6 +95,7 @@ void EntryHistoryModel::setEntries(const QList& entries) beginResetModel(); m_historyEntries = entries; + m_deletedHistoryEntries.clear(); endResetModel(); } @@ -106,6 +105,23 @@ void EntryHistoryModel::clear() beginResetModel(); m_historyEntries.clear(); + m_deletedHistoryEntries.clear(); endResetModel(); } + +QList EntryHistoryModel::deletedEntries() +{ + return m_deletedHistoryEntries; +} + +void EntryHistoryModel::deleteIndex(QModelIndex index) +{ + if (index.isValid()) { + Entry* entry = entryFromIndex(index); + beginRemoveRows(QModelIndex(), m_historyEntries.indexOf(entry), m_historyEntries.indexOf(entry)); + m_historyEntries.removeAll(entry); + m_deletedHistoryEntries << entry; + endRemoveRows(); + } +} diff --git a/src/gui/entry/EntryHistoryModel.h b/src/gui/entry/EntryHistoryModel.h index bb836f2e..404c1035 100644 --- a/src/gui/entry/EntryHistoryModel.h +++ b/src/gui/entry/EntryHistoryModel.h @@ -37,9 +37,12 @@ public: void setEntries(const QList& entries); void clear(); + QList deletedEntries(); + void deleteIndex(QModelIndex index); private: QList m_historyEntries; + QList m_deletedHistoryEntries; }; #endif // KEEPASSX_ENTRYHISTORYMODEL_H