History items can now be removed.

Refs #23
This commit is contained in:
Florian Geyer 2012-05-18 19:22:22 +02:00
parent e4a5cd214f
commit 4797926be6
7 changed files with 126 additions and 7 deletions

View file

@ -367,6 +367,21 @@ void Entry::addHistoryItem(Entry* entry)
Q_EMIT modified(); Q_EMIT modified();
} }
void Entry::removeHistoryItems(QList<Entry*> 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() { void Entry::truncateHistory() {
const Database* db = database(); const Database* db = database();

View file

@ -116,6 +116,7 @@ public:
QList<Entry*> historyItems(); QList<Entry*> historyItems();
const QList<Entry*>& historyItems() const; const QList<Entry*>& historyItems() const;
void addHistoryItem(Entry* entry); void addHistoryItem(Entry* entry);
void removeHistoryItems(QList<Entry*> historyEntries);
void truncateHistory(); void truncateHistory();
Entry* clone() const; Entry* clone() const;

View file

@ -98,6 +98,11 @@ EditEntryWidget::EditEntryWidget(QWidget* parent)
connect(m_historyUi->historyView, SIGNAL(activated(const QModelIndex&)), connect(m_historyUi->historyView, SIGNAL(activated(const QModelIndex&)),
SLOT(emitHistoryEntryActivated(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(accepted()), SLOT(saveEntry()));
connect(this, SIGNAL(rejected()), SLOT(cancel())); connect(this, SIGNAL(rejected()), SLOT(cancel()));
@ -119,6 +124,22 @@ void EditEntryWidget::emitHistoryEntryActivated(const QModelIndex& index)
Q_EMIT historyEntryActivated(entry); 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, void EditEntryWidget::loadEntry(Entry* entry, bool create, bool history, const QString& groupName,
Database* database) Database* database)
{ {
@ -176,6 +197,11 @@ void EditEntryWidget::loadEntry(Entry* entry, bool create, bool history, const Q
m_iconsWidget->setEnabled(!history); m_iconsWidget->setEnabled(!history);
m_historyWidget->setEnabled(!history); m_historyWidget->setEnabled(!history);
m_historyUi->showButton->setEnabled(false);
m_historyUi->restoreButton->setEnabled(false);
m_historyUi->deleteButton->setEnabled(false);
setForms(entry); setForms(entry);
setCurrentRow(0); setCurrentRow(0);
@ -212,8 +238,10 @@ void EditEntryWidget::setForms(const Entry* entry)
iconStruct.uuid = entry->iconUuid(); iconStruct.uuid = entry->iconUuid();
iconStruct.number = entry->iconNumber(); iconStruct.number = entry->iconNumber();
m_iconsWidget->load(entry->uuid(), m_database, iconStruct); m_iconsWidget->load(entry->uuid(), m_database, iconStruct);
if (!m_history) {
m_historyModel->setEntries(entry->historyItems()); m_historyModel->setEntries(entry->historyItems());
} }
}
void EditEntryWidget::saveEntry() void EditEntryWidget::saveEntry()
{ {
@ -222,7 +250,6 @@ void EditEntryWidget::saveEntry()
m_database = 0; m_database = 0;
m_entryAttributes->clear(); m_entryAttributes->clear();
m_entryAttachments->clear(); m_entryAttachments->clear();
m_historyModel->clear();
Q_EMIT editFinished(false); Q_EMIT editFinished(false);
return; return;
} }
@ -240,6 +267,10 @@ void EditEntryWidget::saveEntry()
m_currentAttribute = QPersistentModelIndex(); 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) { if (!m_create) {
m_entry->beginUpdate(); m_entry->beginUpdate();
} }
@ -272,6 +303,7 @@ void EditEntryWidget::saveEntry()
m_entry->endUpdate(); m_entry->endUpdate();
} }
m_entry = 0; m_entry = 0;
m_database = 0; m_database = 0;
m_entryAttributes->clear(); m_entryAttributes->clear();
@ -288,7 +320,6 @@ void EditEntryWidget::cancel()
m_database = 0; m_database = 0;
m_entryAttributes->clear(); m_entryAttributes->clear();
m_entryAttachments->clear(); m_entryAttachments->clear();
m_historyModel->clear();
Q_EMIT editFinished(false); Q_EMIT editFinished(false);
return; return;
} }
@ -487,3 +518,19 @@ void EditEntryWidget::removeCurrentAttachment()
QString key = m_attachmentsModel->keyByIndex(index); QString key = m_attachmentsModel->keyByIndex(index);
m_entryAttachments->remove(key); 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);
}
}

View file

@ -71,7 +71,10 @@ private Q_SLOTS:
void insertAttachment(); void insertAttachment();
void saveCurrentAttachment(); void saveCurrentAttachment();
void removeCurrentAttachment(); void removeCurrentAttachment();
void showHistoryEntry();
void deleteHistoryEntry();
void emitHistoryEntryActivated(const QModelIndex &index); void emitHistoryEntryActivated(const QModelIndex &index);
void updateHistoryButtons(const QModelIndex& current, const QModelIndex& previous);
private: private:
bool passwordsEqual(); bool passwordsEqual();

View file

@ -17,6 +17,40 @@
<item> <item>
<widget class="QTreeView" name="historyView"/> <widget class="QTreeView" name="historyView"/>
</item> </item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<widget class="QPushButton" name="showButton">
<property name="enabled">
<bool>false</bool>
</property>
<property name="text">
<string>Show</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="restoreButton">
<property name="enabled">
<bool>false</bool>
</property>
<property name="text">
<string>Restore</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="deleteButton">
<property name="enabled">
<bool>false</bool>
</property>
<property name="text">
<string>Delete</string>
</property>
</widget>
</item>
</layout>
</item>
</layout> </layout>
</widget> </widget>
<resources/> <resources/>

View file

@ -52,14 +52,12 @@ QVariant EntryHistoryModel::data(const QModelIndex& index, int role) const
return QVariant(); return QVariant();
} }
int row = index.row(); Entry* entry = entryFromIndex(index);
int column = index.column();
Entry* entry = m_historyEntries.at(row);
TimeInfo timeInfo = entry->timeInfo(); TimeInfo timeInfo = entry->timeInfo();
QDateTime lastModificationLocalTime = timeInfo.lastModificationTime().toLocalTime(); QDateTime lastModificationLocalTime = timeInfo.lastModificationTime().toLocalTime();
if (role == Qt::DisplayRole) { if (role == Qt::DisplayRole) {
switch (column) { switch (index.column()) {
case 0: case 0:
return lastModificationLocalTime.toString(Qt::SystemLocaleShortDate); return lastModificationLocalTime.toString(Qt::SystemLocaleShortDate);
case 1: case 1:
@ -97,6 +95,7 @@ void EntryHistoryModel::setEntries(const QList<Entry*>& entries)
beginResetModel(); beginResetModel();
m_historyEntries = entries; m_historyEntries = entries;
m_deletedHistoryEntries.clear();
endResetModel(); endResetModel();
} }
@ -106,6 +105,23 @@ void EntryHistoryModel::clear()
beginResetModel(); beginResetModel();
m_historyEntries.clear(); m_historyEntries.clear();
m_deletedHistoryEntries.clear();
endResetModel(); endResetModel();
} }
QList<Entry*> 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();
}
}

View file

@ -37,9 +37,12 @@ public:
void setEntries(const QList<Entry*>& entries); void setEntries(const QList<Entry*>& entries);
void clear(); void clear();
QList<Entry*> deletedEntries();
void deleteIndex(QModelIndex index);
private: private:
QList<Entry*> m_historyEntries; QList<Entry*> m_historyEntries;
QList<Entry*> m_deletedHistoryEntries;
}; };
#endif // KEEPASSX_ENTRYHISTORYMODEL_H #endif // KEEPASSX_ENTRYHISTORYMODEL_H