Skip to content
New issue

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

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

Already on GitHub? # to your account

Improve CSV import when title field isn't specified #10843

Merged
merged 1 commit into from
Jun 16, 2024
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
9 changes: 9 additions & 0 deletions share/translations/keepassxc_en.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1391,6 +1391,15 @@ Do you want to overwrite the passkey in %1 - %2?</source>
<source>Imported from CSV file: %1</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>No Title Selected</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>No title column was selected, entries will be hard to tell apart.
Are you sure you want to import?</source>
<translation type="unfinished"></translation>
</message>
</context>
<context>
<name>CsvParserModel</name>
Expand Down
25 changes: 20 additions & 5 deletions src/gui/csvImport/CsvImportWidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include "core/Totp.h"
#include "format/CsvParser.h"
#include "format/KeePass2Writer.h"
#include "gui/MessageBox.h"
#include "gui/csvImport/CsvParserModel.h"

#include <QStringListModel>
Expand Down Expand Up @@ -213,19 +214,29 @@ void CsvImportWidget::parse()

QSharedPointer<Database> CsvImportWidget::buildDatabase()
{
// Warn if the title column wasn't specified
if (m_combos[1]->currentIndex() == 0) {
auto ans = MessageBox::question(
this,
tr("No Title Selected"),
tr("No title column was selected, entries will be hard to tell apart.\nAre you sure you want to import?"),
MessageBox::Continue | MessageBox::Cancel);
if (ans == MessageBox::Cancel) {
return {};
}
}

auto db = QSharedPointer<Database>::create();
db->rootGroup()->setNotes(tr("Imported from CSV file: %1").arg(m_filename));

for (int r = 0; r < m_parserModel->rowCount(); ++r) {
// use validity of second column as a GO/NOGO for all others fields
if (!m_parserModel->data(m_parserModel->index(r, 1)).isValid()) {
continue;
}
auto rows = m_parserModel->rowCount() - m_parserModel->skippedRows();
for (int r = 0; r < rows; ++r) {
auto group = createGroupStructure(db.data(), m_parserModel->data(m_parserModel->index(r, 0)).toString());
if (!group) {
continue;
}

// Standard entry fields
auto entry = new Entry();
entry->setUuid(QUuid::createUuid());
entry->setGroup(group);
Expand All @@ -235,6 +246,7 @@ QSharedPointer<Database> CsvImportWidget::buildDatabase()
entry->setUrl(m_parserModel->data(m_parserModel->index(r, 4)).toString());
entry->setNotes(m_parserModel->data(m_parserModel->index(r, 5)).toString());

// TOTP
auto otpString = m_parserModel->data(m_parserModel->index(r, 6));
if (otpString.isValid() && !otpString.toString().isEmpty()) {
auto totp = Totp::parseSettings(otpString.toString());
Expand All @@ -245,12 +257,14 @@ QSharedPointer<Database> CsvImportWidget::buildDatabase()
entry->setTotp(totp);
}

// Icon
bool ok;
int icon = m_parserModel->data(m_parserModel->index(r, 7)).toInt(&ok);
if (ok) {
entry->setIcon(icon);
}

// Modified Time
TimeInfo timeInfo;
if (m_parserModel->data(m_parserModel->index(r, 8)).isValid()) {
auto datetime = m_parserModel->data(m_parserModel->index(r, 8)).toString();
Expand All @@ -270,6 +284,7 @@ QSharedPointer<Database> CsvImportWidget::buildDatabase()
}
}
}
// Creation Time
if (m_parserModel->data(m_parserModel->index(r, 9)).isValid()) {
auto datetime = m_parserModel->data(m_parserModel->index(r, 9)).toString();
if (datetime.contains(QRegularExpression("^\\d+$"))) {
Expand Down
5 changes: 5 additions & 0 deletions src/gui/csvImport/CsvParserModel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,11 @@ void CsvParserModel::setSkippedRows(int skipped)
emit layoutChanged();
}

int CsvParserModel::skippedRows() const
{
return m_skipped;
}

void CsvParserModel::setHeaderLabels(const QStringList& labels)
{
m_columnHeader = labels;
Expand Down
2 changes: 1 addition & 1 deletion src/gui/csvImport/CsvParserModel.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ class CsvParserModel : public QAbstractTableModel
QVariant data(const QModelIndex& index, int role = Qt::DisplayRole) const override;
QVariant headerData(int section, Qt::Orientation orientation, int role) const override;

public slots:
void setSkippedRows(int skipped);
int skippedRows() const;

private:
CsvParser* m_parser;
Expand Down
Loading