Part 7 - Additional Features

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

Image non disponible

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.

 
Sélectionnez
    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.

 
Sélectionnez
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+"), QString::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.

 
Sélectionnez
    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.

 
Sélectionnez
    out << "BEGIN:VCARD" << '\n';
    out << "VERSION:2.1" <<