mirror of
				https://git.wownero.com/wowlet/wowlet.git
				synced 2024-08-15 01:03:14 +00:00 
			
		
		
		
	Merge pull request 'Contacts: import via csv file' (#100) from mrdeveloper/feather:contacts-import-csv into master
Reviewed-on: https://git.wownero.com/feather/feather/pulls/100 Reviewed-by: tobtoht <tobtoht@noreply.gitgud.wownero.nl>
This commit is contained in:
		
						commit
						06f649cbb7
					
				
					 5 changed files with 56 additions and 0 deletions
				
			
		| 
						 | 
				
			
			@ -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<QString, QString> 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;
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -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
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -333,6 +333,7 @@
 | 
			
		|||
      <string>Contacts</string>
 | 
			
		||||
     </property>
 | 
			
		||||
     <addaction name="actionExportContactsCSV"/>
 | 
			
		||||
     <addaction name="actionImportContactsCSV"/>
 | 
			
		||||
    </widget>
 | 
			
		||||
    <widget class="QMenu" name="menuAdvanced">
 | 
			
		||||
     <property name="title">
 | 
			
		||||
| 
						 | 
				
			
			@ -538,6 +539,11 @@
 | 
			
		|||
    <string>Refresh models</string>
 | 
			
		||||
   </property>
 | 
			
		||||
  </action>
 | 
			
		||||
  <action name="actionImportContactsCSV">
 | 
			
		||||
   <property name="text">
 | 
			
		||||
    <string>Import CSV</string>
 | 
			
		||||
   </property>
 | 
			
		||||
  </action>
 | 
			
		||||
  <action name="actionExportContactsCSV">
 | 
			
		||||
   <property name="text">
 | 
			
		||||
    <string>Export CSV</string>
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -174,3 +174,26 @@ bool AddressBookModel::writeCSV(const QString &path) {
 | 
			
		|||
    csv = QString("address,description\n%1").arg(csv);
 | 
			
		||||
    return Utils::fileWrite(path, csv);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
QMap<QString, QString> AddressBookModel::readCSV(const QString &path) {
 | 
			
		||||
    if(!Utils::fileExists(path)) {
 | 
			
		||||
        return QMap<QString, QString>();
 | 
			
		||||
    }
 | 
			
		||||
    QString csv = Utils::barrayToString(Utils::fileOpen(path));
 | 
			
		||||
    QTextStream stream(&csv);
 | 
			
		||||
    QMap<QString, QString> map;
 | 
			
		||||
 | 
			
		||||
    while(!stream.atEnd()) {
 | 
			
		||||
        QStringList line = stream.readLine().split(",");
 | 
			
		||||
        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;
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -36,6 +36,7 @@ public:
 | 
			
		|||
    bool isShowFullAddresses() const;
 | 
			
		||||
    void setShowFullAddresses(bool show);
 | 
			
		||||
    bool writeCSV(const QString &path);
 | 
			
		||||
    QMap<QString, QString> readCSV(const QString &path);
 | 
			
		||||
 | 
			
		||||
public slots:
 | 
			
		||||
    void startReset();
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue