int SeasidePeopleModel::importContacts(const QString &path) { MODEL_DEBUG() << QDir::currentPath(); QFile vcf(path); if (!vcf.open(QIODevice::ReadOnly)) { qWarning() << Q_FUNC_INFO << "Cannot open " << path; return 0; } // TODO: thread QVersitReader reader(&vcf); reader.startReading(); reader.waitForFinished(); QVersitContactImporter importer; importer.importDocuments(reader.results()); QList<QContact> newContacts = importer.contacts(); foreach (const QContact &contact, newContacts) priv->contactsPendingSave.append(contact); priv->savePendingContacts(); MODEL_DEBUG() << Q_FUNC_INFO << "Imported " << newContacts.size() << " contacts " << " from " << path; return newContacts.size(); }
void MT_CntVersitMyCardPlugin::importContact() { //import QBuffer input; input.open(QBuffer::ReadWrite); QByteArray inputVCard = "BEGIN:VCARD\r\nVERSION:2.1\r\nFN:John\r\nEND:VCARD\r\n"; input.write(inputVCard); input.seek(0); QVersitReader reader; reader.setDevice(&input); reader.startReading(); reader.waitForFinished(); // Use the resulting document(s)... QVersitContactImporter importer; QList<QVersitDocument> versitDocuments = reader.results(); importer.importDocuments(versitDocuments); QList<QContact> contacts = importer.contacts(); QVERIFY(1 == contacts.count()); // Check if MyCard detail is found QList<QContactDetail> details = contacts.first().details(MYCARD_DEFINTION_NAME); QVERIFY(details.count()==0); }
void completeExample() { // Create the input vCard //! [Complete example - create] QBuffer input; input.open(QBuffer::ReadWrite); QByteArray inputVCard = "BEGIN:VCARD\r\nVERSION:2.1\r\nFN:John Citizen\r\nN:Citizen;John;Q;;\r\nEND:VCARD\r\n"; input.write(inputVCard); input.seek(0); //! [Complete example - create] // Parse the input into QVersitDocuments //! [Complete example - read] // Note: we could also use the more convenient QVersitReader(QByteArray) constructor. QVersitReader reader; reader.setDevice(&input); reader.startReading(); // Remember to check the return value reader.waitForFinished(); QList<QVersitDocument> inputDocuments = reader.results(); //! [Complete example - read] // Convert the QVersitDocuments to QContacts //! [Complete example - import] QVersitContactImporter importer; if (!importer.importDocuments(inputDocuments)) return; QList<QContact> contacts = importer.contacts(); // Note that the QContacts are not saved yet. // Use QContactManager::saveContacts() for saving if necessary //! [Complete example - import] // Export the QContacts back to QVersitDocuments //! [Complete example - export] QVersitContactExporter exporter; if (!exporter.exportContacts(contacts, QVersitDocument::VCard30Type)) return; QList<QVersitDocument> outputDocuments = exporter.documents(); //! [Complete example - export] // Encode the QVersitDocument back to a vCard //! [Complete example - write] // Note: we could also use the more convenient QVersitWriter(QByteArray*) constructor. QBuffer output; output.open(QBuffer::ReadWrite); QVersitWriter writer; writer.setDevice(&output); writer.startWriting(outputDocuments); // Remember to check the return value writer.waitForFinished(); // output.buffer() now contains a vCard //! [Complete example - write] }
void Load_Vcard::addNewContacts() { QFETCH(QString, vcard); QFETCH(QString, result); QList<QContact> contacts; QString res = QString("1"); QContactManager *cm = new QContactManager(); QContactSaveRequest *m_contactSaveRequest = new QContactSaveRequest(); m_contactSaveRequest->setManager(cm); //connect(&m_contactSaveRequest, SIGNAL(resultsAvailable()), this, // SLOT(contactsSaved())); QFile file(vcard); if (file.exists()) { QByteArray cardArr; if (file.open(QFile::ReadOnly)) { while (!file.atEnd()) { cardArr.append(file.readLine()); } } QBuffer input; input.open(QBuffer::ReadWrite); input.write(cardArr); input.seek(0); QVersitReader reader(cardArr); reader.startReading(); reader.waitForFinished(); QList<QVersitDocument> inputDocuments = reader.results(); QVersitContactImporter importer; if (importer.importDocuments(inputDocuments)) { contacts = importer.contacts(); m_contactSaveRequest->setContacts(contacts); m_contactSaveRequest->start(); m_contactSaveRequest->waitForFinished(); res = QString("0"); } input.close(); file.close(); } delete m_contactSaveRequest; delete cm; QCOMPARE(res, result); }
int SeasideCache::importContacts(const QString &path) { QFile vcf(path); if (!vcf.open(QIODevice::ReadOnly)) { qWarning() << Q_FUNC_INFO << "Cannot open " << path; return 0; } // TODO: thread QVersitReader reader(&vcf); reader.startReading(); reader.waitForFinished(); QVersitContactImporter importer; importer.importDocuments(reader.results()); QList<QContact> newContacts = importer.contacts(); instance->m_contactsToCreate += newContacts; instance->requestUpdate(); return newContacts.count(); }
void importExample() { //! [Import example] QVersitContactImporter importer; QVersitDocument document; QVersitProperty property; property.setName(QString::fromAscii("N")); property.setValue("Citizen;John;Q;;"); document.addProperty(property); property.setName(QString::fromAscii("X-UNKNOWN-PROPERTY")); property.setValue("some value"); document.addProperty(property); if (importer.importDocuments(QList<QVersitDocument>() << document)) { QList<QContact> contactList = importer.contacts(); // contactList.first() now contains the "N" property as a QContactName // propertyHandler.mUnknownProperties contains the list of unknown properties } //! [Import example] }
int main(int argc, char **argv) { QCoreApplication app(argc, argv); QStringList args = app.arguments(); QFile vcf, xml; if (args.count() > 1) { vcf.setFileName(args[1]); if (not vcf.open(QFile::ReadOnly)) { qWarning("Cannot open %s for reading: %s", qPrintable(vcf.fileName()), qPrintable(vcf.errorString())); return 1; } } else { if (not vcf.open(stdin, QFile::ReadOnly)) { qWarning("Cannot open STDIN for reading: %s", qPrintable(vcf.errorString())); return 1; } } QVersitReader reader(&vcf); if (not reader.startReading() || not reader.waitForFinished() || QVersitReader::NoError != reader.error()) { qWarning("Cannot read vcard: rc=%d", reader.error()); return 1; } QVersitContactImporter importer; if (not importer.importDocuments(reader.results())) { typedef QMap<int, QVersitContactImporter::Error> ErrorMap; const ErrorMap errors = importer.errors(); for(ErrorMap::ConstIterator it = errors.constBegin(); it != errors.constEnd(); ++it) { qWarning("Cannot convert contact #%d: %d", it.key(), it.value()); } return 1; } if (args.count() > 2) { xml.setFileName(args[2]); if (not xml.open(QFile::WriteOnly)) { qWarning("Cannot open %s for writing: %s", qPrintable(xml.fileName()), qPrintable(xml.errorString())); return 1; } } else { if (not xml.open(stdout, QFile::WriteOnly)) { qWarning("Cannot open STDIN for writing: %s", qPrintable(xml.errorString())); return 1; } } QTextStream out(&xml); out << "<Contacts>\n"; foreach(const QContact &contact, importer.contacts()) { out << " <Contact id=\"" << makeIri(contact) << "\">\n"; foreach(const QContactDetail &detail, contact.details()) { QVariantMap fields = detail.variantValues(); out << " <" << detail.definitionName(); if (not detail.detailUri().isEmpty()) { out << " id=\"" << detail.detailUri() << "\""; } out << ">"; if (fields.count() > 1 || fields.keys().first() != detail.definitionName()) { QVariantMap::ConstIterator it; for(it = fields.constBegin(); it != fields.constEnd(); ++it) { out << "\n "; out << "<" << it.key() << ">"; out << toString(it.value()); out << "</" << it.key() << ">"; } out << "\n "; } else { out << toString(fields.values().first()); } out << "</" << detail.definitionName() << ">\n"; } out << " </Contact>\n"; } out << "</Contacts>\n"; return 0; }