Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rename attachments #5034

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/core/EntryAttachments.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,13 @@ void EntryAttachments::remove(const QStringList& keys)
}
}

void EntryAttachments::rename(const QString& key, const QString& newKey)
{
const QByteArray val = value(key);
remove(key);
set(newKey, val);
}

bool EntryAttachments::isEmpty() const
{
return m_attachments.isEmpty();
Expand Down
1 change: 1 addition & 0 deletions src/core/EntryAttachments.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ class EntryAttachments : public QObject
void set(const QString& key, const QByteArray& value);
void remove(const QString& key);
void remove(const QStringList& keys);
void rename(const QString& key, const QString& newKey);
bool isEmpty() const;
void clear();
void copyDataFrom(const EntryAttachments* other);
Expand Down
27 changes: 27 additions & 0 deletions src/gui/entry/EntryAttachmentsModel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,28 @@ QVariant EntryAttachmentsModel::data(const QModelIndex& index, int role) const
return QVariant();
}

bool EntryAttachmentsModel::setData(const QModelIndex& index, const QVariant& value, int role)
{
if (!m_readOnly && index.column() == Columns::NameColumn) {
const QString key = value.toString().trimmed();
if (key.isEmpty() || m_entryAttachments->hasKey(key)) {
return false;
}
m_entryAttachments->rename(keyByIndex(index), key);
return true;
}
return QAbstractListModel::setData(index, value, role);
}

Qt::ItemFlags EntryAttachmentsModel::flags(const QModelIndex& index) const
{
Qt::ItemFlags flags = QAbstractListModel::flags(index);
if (!m_readOnly && index.column() == Columns::NameColumn) {
flags = flags | Qt::ItemIsEditable;
}
return flags;
}

QString EntryAttachmentsModel::keyByIndex(const QModelIndex& index) const
{
if (!index.isValid()) {
Expand Down Expand Up @@ -150,3 +172,8 @@ void EntryAttachmentsModel::reset()
{
endResetModel();
}

void EntryAttachmentsModel::setReadOnly(bool readOnly)
{
m_readOnly = readOnly;
}
4 changes: 4 additions & 0 deletions src/gui/entry/EntryAttachmentsModel.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ class EntryAttachmentsModel : public QAbstractListModel
int columnCount(const QModelIndex& parent = QModelIndex()) const override;
QVariant headerData(int section, Qt::Orientation orientation, int role) const override;
QVariant data(const QModelIndex& index, int role = Qt::DisplayRole) const override;
bool setData(const QModelIndex& index, const QVariant& value, int role = Qt::EditRole) override;
Qt::ItemFlags flags(const QModelIndex& index) const override;
QString keyByIndex(const QModelIndex& index) const;

private slots:
Expand All @@ -50,10 +52,12 @@ private slots:
void attachmentRemove();
void aboutToReset();
void reset();
void setReadOnly(bool readOnly);

private:
EntryAttachments* m_entryAttachments;
QStringList m_headers;
bool m_readOnly = false;
};

#endif // KEEPASSX_ENTRYATTACHMENTSMODEL_H
8 changes: 8 additions & 0 deletions src/gui/entry/EntryAttachmentsWidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,14 @@ EntryAttachmentsWidget::EntryAttachmentsWidget(QWidget* parent)
SIGNAL(selectionChanged(QItemSelection,QItemSelection)),
SLOT(updateButtonsEnabled()));
// clang-format on
connect(this, SIGNAL(readOnlyChanged(bool)), m_attachmentsModel, SLOT(setReadOnly(bool)));

connect(m_ui->attachmentsView, SIGNAL(doubleClicked(QModelIndex)), SLOT(openAttachment(QModelIndex)));
connect(m_ui->saveAttachmentButton, SIGNAL(clicked()), SLOT(saveSelectedAttachments()));
connect(m_ui->openAttachmentButton, SIGNAL(clicked()), SLOT(openSelectedAttachments()));
connect(m_ui->addAttachmentButton, SIGNAL(clicked()), SLOT(insertAttachments()));
connect(m_ui->removeAttachmentButton, SIGNAL(clicked()), SLOT(removeSelectedAttachments()));
connect(m_ui->renameAttachmentButton, SIGNAL(clicked()), SLOT(renameSelectedAttachments()));

updateButtonsVisible();
updateButtonsEnabled();
Expand Down Expand Up @@ -184,6 +186,11 @@ void EntryAttachmentsWidget::removeSelectedAttachments()
}
}

void EntryAttachmentsWidget::renameSelectedAttachments()
{
m_ui->attachmentsView->edit(m_ui->attachmentsView->selectionModel()->selectedIndexes().first());
}

void EntryAttachmentsWidget::saveSelectedAttachments()
{
const QModelIndexList indexes = m_ui->attachmentsView->selectionModel()->selectedRows(0);
Expand Down Expand Up @@ -289,6 +296,7 @@ void EntryAttachmentsWidget::updateButtonsEnabled()

m_ui->addAttachmentButton->setEnabled(!m_readOnly);
m_ui->removeAttachmentButton->setEnabled(hasSelection && !m_readOnly);
m_ui->renameAttachmentButton->setEnabled(hasSelection && !m_readOnly);

m_ui->saveAttachmentButton->setEnabled(hasSelection);
m_ui->openAttachmentButton->setEnabled(hasSelection);
Expand Down
1 change: 1 addition & 0 deletions src/gui/entry/EntryAttachmentsWidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ public slots:
private slots:
void insertAttachments();
void removeSelectedAttachments();
void renameSelectedAttachments();
void saveSelectedAttachments();
void openAttachment(const QModelIndex& index);
void openSelectedAttachments();
Expand Down
16 changes: 16 additions & 0 deletions src/gui/entry/EntryAttachmentsWidget.ui
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@
<property name="accessibleName">
<string>Attachments</string>
</property>
<property name="editTriggers">
<set>QAbstractItemView::EditKeyPressed|QAbstractItemView::SelectedClicked</set>
</property>
</widget>
</item>
<item>
Expand Down Expand Up @@ -74,6 +77,19 @@
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="renameAttachmentButton">
<property name="enabled">
<bool>false</bool>
</property>
<property name="accessibleName">
<string>Rename selected attachment</string>
</property>
<property name="text">
<string>Rename</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="openAttachmentButton">
<property name="enabled">
Expand Down