Improve CSV import when title field isn't specified

* Fixes #10433
This commit is contained in:
Jonathan White 2024-06-01 15:24:55 -04:00
parent 3662f6aa77
commit f80e79f71a
4 changed files with 35 additions and 6 deletions

View File

@ -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>

View File

@ -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>
@ -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);
@ -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());
@ -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();
@ -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+$"))) {

View File

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

View File

@ -45,8 +45,8 @@ public:
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;