/*! * Converts \a contacts into a list of corresponding QVersitDocuments, using the format given by * \a versitType. * * Returns true on success. If any of the contacts could not be exported, false is returned and * errorMap() will return a list describing the errors that occurred. The successfully exported * documents will still be available via documents(). * * \sa documents(), errorMap() */ bool QVersitContactExporter::exportContacts( const QList<QContact>& contacts, QVersitDocument::VersitType versitType) { int contactIndex = 0; d->mDocuments.clear(); d->mErrors.clear(); bool ok = true; foreach (const QContact& contact, contacts) { if (contact.isEmpty()) { d->mErrors[contactIndex] = EmptyContactError; ok = false; continue; } QVersitDocument versitDocument; versitDocument.setType(versitType); versitDocument.setComponentType(QLatin1String("VCARD")); d->exportContact(contact, versitDocument); d->mDocuments.append(versitDocument); contactIndex++; } return ok; }
void tst_QVersitWriter::testWritingVersions() { mWriter->setDevice(mOutputDevice); mOutputDevice->open(QBuffer::ReadWrite); QVersitDocument document; QVersitProperty property; property.setName(QString(QString::fromLatin1("FN"))); property.setValue(QString::fromLatin1("John")); document.addProperty(property); QByteArray vCard30( "BEGIN:VCARD\r\n" "VERSION:3.0\r\n" "FN:John\r\n" "END:VCARD\r\n"); QByteArray vCard21( "BEGIN:VCARD\r\n" "VERSION:2.1\r\n" "FN:John\r\n" "END:VCARD\r\n"); // Given no type or componentType, it should be vCard 3.0 QVERIFY(mWriter->startWriting(document)); mWriter->waitForFinished(); QCOMPARE(mOutputDevice->buffer(), vCard30); // document type should override the guess document.setType(QVersitDocument::VCard21Type); mOutputDevice->buffer().clear(); mOutputDevice->seek(0); QVERIFY(mWriter->startWriting(document)); mWriter->waitForFinished(); QCOMPARE(mOutputDevice->buffer(), vCard21); // param to startWriting should override document type mOutputDevice->buffer().clear(); mOutputDevice->seek(0); QVERIFY(mWriter->startWriting(document, QVersitDocument::VCard30Type)); mWriter->waitForFinished(); QCOMPARE(mOutputDevice->buffer(), vCard30); }
/*! * Converts \a items into a QVersitDocument, using the format given by \a versitType. * Returns true on success. If any of the items could not be exported, false is returned and * errorMap() will return a list describing the errors that occurred. The successfully exported * components will still be available via document(). * * \sa document(), errorMap() */ bool QVersitOrganizerExporter::exportItems( const QList<QOrganizerItem>& items, QVersitDocument::VersitType versitType) { int itemIndex = 0; d->mErrors.clear(); d->mResult.clear(); d->mResult.setType(versitType); d->mResult.setComponentType(QLatin1String("VCALENDAR")); bool ok = true; QList<QVersitDocument> results; foreach (const QOrganizerItem& item, items) { QVersitDocument document; document.setType(versitType); QVersitOrganizerExporter::Error error; if (d->exportItem(item, &document, &error)) { results.append(document); } else { d->mErrors.insert(itemIndex, error); ok = false; } itemIndex++; }