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();
}
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() {
const Database* db = database();

View File

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

View File

@ -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);
}
}

View File

@ -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();

View File

@ -17,6 +17,40 @@
<item>
<widget class="QTreeView" name="historyView"/>
</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>
</widget>
<resources/>

View File

@ -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<Entry*>& 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<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 clear();
QList<Entry*> deletedEntries();
void deleteIndex(QModelIndex index);
private:
QList<Entry*> m_historyEntries;
QList<Entry*> m_deletedHistoryEntries;
};
#endif // KEEPASSX_ENTRYHISTORYMODEL_H