From 78516f4546ca6990dbaf4a223c33176d8de8b1ee Mon Sep 17 00:00:00 2001 From: mrdeveloper Date: Wed, 21 Oct 2020 08:25:02 +0200 Subject: [PATCH 1/2] Contacts: import via csv file --- src/mainwindow.cpp | 25 +++++++++++++++++++++++++ src/mainwindow.h | 1 + src/mainwindow.ui | 6 ++++++ src/model/AddressBookModel.cpp | 18 ++++++++++++++++++ src/model/AddressBookModel.h | 1 + 5 files changed, 51 insertions(+) diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp index 6e057d9..ba72029 100644 --- a/src/mainwindow.cpp +++ b/src/mainwindow.cpp @@ -474,6 +474,8 @@ void MainWindow::initMenu() { Utils::showMessageBox("Address book exported", QString("Address book exported to %1").arg(fn), false); }); + connect(ui->actionImportContactsCSV, &QAction::triggered, this, &MainWindow::importContacts); + // Tools connect(ui->actionSignVerify, &QAction::triggered, this, &MainWindow::menuSignVerifyClicked); connect(ui->actionVerifyTxProof, &QAction::triggered, this, &MainWindow::menuVerifyTxProof); @@ -1045,6 +1047,29 @@ void MainWindow::onAddContact(const QString &address, const QString &name) { } } +void MainWindow::importContacts() { + const QString targetFile = QFileDialog::getOpenFileName(this, "Import CSV file", QDir::homePath(), "CSV Files (*.csv)"); + if(targetFile.isEmpty()) return; + + auto *model = m_ctx->currentWallet->addressBookModel(); + QMapIterator i(model->readCSV(targetFile)); + int inserts = 0; + while (i.hasNext()) { + i.next(); + bool addressValid = WalletManager::addressValid(i.value(), m_ctx->currentWallet->nettype()); + if(addressValid) { + m_ctx->currentWallet->addressBook()->addRow(i.value(), "", i.key()); + inserts++; + } + } + + if(inserts > 0) { + m_ctx->storeWallet(); + } + + QMessageBox::information(this, "Contacts imported", QString("Total contacts imported: %1").arg(inserts)); +} + MainWindow *MainWindow::getInstance() { return pMainWindow; } diff --git a/src/mainwindow.h b/src/mainwindow.h index 6832dc7..dafd4f8 100644 --- a/src/mainwindow.h +++ b/src/mainwindow.h @@ -99,6 +99,7 @@ public slots: void onWalletOpenPasswordRequired(bool invalidPassword); void onViewOnBlockExplorer(const QString &txid); void onAddContact(const QString &address, const QString &name); + void importContacts(); void showRestoreHeightDialog(); // offline tx signing diff --git a/src/mainwindow.ui b/src/mainwindow.ui index 0409137..31a5e64 100644 --- a/src/mainwindow.ui +++ b/src/mainwindow.ui @@ -333,6 +333,7 @@ Contacts + @@ -538,6 +539,11 @@ Refresh models + + + Import CSV + + Export CSV diff --git a/src/model/AddressBookModel.cpp b/src/model/AddressBookModel.cpp index a924fec..cb1afd7 100644 --- a/src/model/AddressBookModel.cpp +++ b/src/model/AddressBookModel.cpp @@ -174,3 +174,21 @@ bool AddressBookModel::writeCSV(const QString &path) { csv = QString("address,description\n%1").arg(csv); return Utils::fileWrite(path, csv); } + +QMap AddressBookModel::readCSV(const QString &path) { + if(!Utils::fileExists(path)) { + return QMap(); + } + QString csv = Utils::barrayToString(Utils::fileOpen(path)); + QTextStream stream(&csv); + QMap map; + while(!stream.atEnd()) { + QStringList line = stream.readLine().split(","); + QString name = line.at(0); + QString address = line.at(1); + if(!name.isEmpty() && !address.isEmpty()) { + map[name] = address; + } + } + return map; +} diff --git a/src/model/AddressBookModel.h b/src/model/AddressBookModel.h index 109c897..a766f59 100644 --- a/src/model/AddressBookModel.h +++ b/src/model/AddressBookModel.h @@ -36,6 +36,7 @@ public: bool isShowFullAddresses() const; void setShowFullAddresses(bool show); bool writeCSV(const QString &path); + QMap readCSV(const QString &path); public slots: void startReset(); From 47d965c6cda2bd0a74997c189523fc1a965b42c8 Mon Sep 17 00:00:00 2001 From: mrdeveloper Date: Wed, 21 Oct 2020 10:10:48 +0200 Subject: [PATCH 2/2] Make format csv import as same as export --- src/model/AddressBookModel.cpp | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/src/model/AddressBookModel.cpp b/src/model/AddressBookModel.cpp index cb1afd7..fb8b37d 100644 --- a/src/model/AddressBookModel.cpp +++ b/src/model/AddressBookModel.cpp @@ -182,12 +182,17 @@ QMap AddressBookModel::readCSV(const QString &path) { QString csv = Utils::barrayToString(Utils::fileOpen(path)); QTextStream stream(&csv); QMap map; + while(!stream.atEnd()) { QStringList line = stream.readLine().split(","); - QString name = line.at(0); - QString address = line.at(1); - if(!name.isEmpty() && !address.isEmpty()) { - map[name] = address; + if(line.length() != 2) { + continue; + } + QString address = line.at(0); + QString description = line.at(1); + description = description.replace("\"", ""); + if(!description.isEmpty() && !address.isEmpty()) { + map[description] = address; } } return map;