From 8870e52a59d7688c4fea3dea3b8297701a87b64b Mon Sep 17 00:00:00 2001 From: Aditya Kulkarni Date: Wed, 2 Jan 2019 11:30:02 -0800 Subject: [PATCH] #85 Import address book --- src/addressbook.cpp | 48 +++++++++++++++++++++++++++++++++++++++++++++ src/addressbook.ui | 27 +++++++++++++++++-------- 2 files changed, 67 insertions(+), 8 deletions(-) diff --git a/src/addressbook.cpp b/src/addressbook.cpp index 4b2908f..7d74cbd 100644 --- a/src/addressbook.cpp +++ b/src/addressbook.cpp @@ -131,6 +131,40 @@ void AddressBook::open(MainWindow* parent, QLineEdit* target) { } }); + // Import Button + QObject::connect(ab.btnImport, &QPushButton::clicked, [&] () { + // Get the import file name. + auto fileName = QFileDialog::getOpenFileUrl(&d, QObject::tr("Import Address Book"), QUrl(), + "CSV file (*.csv)"); + if (fileName.isEmpty()) + return; + + QFile file(fileName.toLocalFile()); + if (!file.open(QIODevice::ReadOnly)) { + QMessageBox::information(&d, QObject::tr("Unable to open file"), file.errorString()); + return; + } + + QTextStream in(&file); + QString line; + int numImported = 0; + while (in.readLineInto(&line)) { + QStringList items = line.split(","); + if (items.size() != 2) + continue; + + if (!Settings::isValidAddress(items.at(0))) + continue; + + // Add label, address. + model.addNewLabel(items.at(1), items.at(0)); + numImported++; + } + + QMessageBox::information(&d, QObject::tr("Address Book Import Done"), + QObject::tr("Imported %1 new Address book entries").arg(numImported)); + }); + auto fnSetTargetLabelAddr = [=] (QLineEdit* target, QString label, QString addr) { target->setText(label % "/" % addr); }; @@ -220,6 +254,9 @@ void AddressBook::readFromStorage() { } void AddressBook::writeToStorage() { + if (allLabels.isEmpty()) + return; + QFile file(AddressBook::writeableFile()); file.open(QIODevice::ReadWrite | QIODevice::Truncate); QDataStream out(&file); // we will serialize the data into the file @@ -246,6 +283,14 @@ QString AddressBook::writeableFile() { void AddressBook::addAddressLabel(QString label, QString address) { Q_ASSERT(Settings::isValidAddress(address)); + // First, remove any existing label + // Iterate over the list and remove the label/address + for (int i=0; i < allLabels.size(); i++) { + if (allLabels[i].first == label) { + removeAddressLabel(allLabels[i].first, allLabels[i].second); + } + } + allLabels.push_back(QPair(label, address)); writeToStorage(); } @@ -275,6 +320,9 @@ void AddressBook::updateLabel(QString oldlabel, QString address, QString newlabe // Read all addresses const QList>& AddressBook::getAllAddressLabels() { + if (allLabels.isEmpty()) { + readFromStorage(); + } return allLabels; } diff --git a/src/addressbook.ui b/src/addressbook.ui index a1d9611..a20ee8f 100644 --- a/src/addressbook.ui +++ b/src/addressbook.ui @@ -88,14 +88,25 @@ - - - Qt::Horizontal - - - QDialogButtonBox::Close|QDialogButtonBox::Ok - - + + + + + Import Address Book + + + + + + + Qt::Horizontal + + + QDialogButtonBox::Close|QDialogButtonBox::Ok + + + +