Part 7 - Additional Features

Describes how to export data in VCard format.

This part covers some additional features that make the address book more convenient for the frequent user.

Although our address book is useful in isolation, it would be better if we could exchange contact data with other applications. The vCard format is a popular file format that can be used for this purpose. Here we extend our address book client to allow contacts to be exported to vCard .vcf files.

Defining the AddressBook Class

We add a QPushButton object, exportButton, and a corresponding public slot, exportAsVCard() to our AddressBook class in the addressbook.h file.

     void exportAsVCard();
     ...
     QPushButton *exportButton;

Implementing the AddressBook Class

Within the AddressBook constructor, we connect exportButton's clicked() signal to exportAsVCard(). We also add this button to our buttonLayout1, the layout responsible for our panel of buttons on the right.

In our exportAsVCard() function, we start by extracting the contact's name into name. We declare firstName, lastName and nameList. Next, we look for the index of the first white space in name. If there is a white space, we split the contact's name into firstName and lastName. Then, we replace the space with an underscore ("_"). Alternately, if there is no white space, we assume that the contact only has a first name.

 void AddressBook::exportAsVCard()
 {
     QString name = nameLine->text();
     QString address = addressText->toPlainText();
     QString firstName;
     QString lastName;
     QStringList nameList;

     int index = name.indexOf(" ");

     if (index != -1) {
         nameList = name.split(QRegularExpression("\\s+"), Qt::SkipEmptyParts);
         firstName = nameList.first();
         lastName = nameList.last();
     } else {
         firstName = name;
         lastName = "";
     }

     QString fileName = QFileDialog::getSaveFileName(this,
         tr("Export Contact"), "",
         tr("vCard Files (*.vcf);;All Files (*)"));

     if (fileName.isEmpty())
         return;

     QFile file(fileName);

As with the saveToFile() function, we open a file dialog to let the user choose a location for the file. Using the file name chosen, we create an instance of QFile to write to.

We attempt to open the file in WriteOnly mode. If this process fails, we display a QMessageBox to inform the user about the problem and return. Otherwise, we pass the file as a parameter to a QTextStream object, out. Like QDataStream, the QTextStream class provides functionality to read and write plain text to files. As a result, the .vcf file generated can be opened for editing in a text editor.

     if (!file.open(QIODevice::WriteOnly)) {
         QMessageBox::information(this, tr("Unable to open file"),
             file.errorString());
         return;
     }

     QTextStream out(&file);

We then write out a vCard file with the BEGIN:VCARD tag, followed by the VERSION:2.1 tag. The contact's name is written with the N: tag. For the FN: tag, which fills in the "File as" property of a vCard, we have to check whether the contact has a last name or not. If the contact does, we use the details in nameList to fill it. Otherwise, we write firstName only.

     out << "BEGIN:VCARD" << '\n';
     out << "VERSION:2.1" << '\n';
     out << "N:" << lastName << ';' << firstName << '\n';

     if (!nameList.isEmpty())
        out << "FN:" << nameList.join(' ') << '\n';
     else
        out << "FN:" << firstName << '\n';

We proceed to write the contact's address. The semicolons in the address are escaped with "\", the newlines are replaced with semicolons, and the commas are replaced with spaces. Lastly, we write the ADR;HOME:; tag, followed by address and then the END:VCARD tag.

     address.replace(";", "\\;", Qt::CaseInsensitive);
     address.replace('\n', ";", Qt::CaseInsensitive);
     address.replace(",", " ", Qt::CaseInsensitive);

     out << "ADR;HOME:;" << address << '\n';
     out << "END:VCARD" << '\n';

     QMessageBox::information(this, tr("Export Successful"),
         tr("\"%1\" has been exported as a vCard.").arg(name));
 }

In the end, a QMessageBox is displayed to inform the user that the vCard has been successfully exported.

vCard is a trademark of the Internet Mail Consortium.

Example project @ code.qt.io