diff options
author | Renato Araujo Oliveira Filho <renato.filho@canonical.com> | 2014-06-25 15:00:25 -0300 |
---|---|---|
committer | Alex Blasche <alexander.blasche@theqtcompany.com> | 2014-11-20 07:51:16 +0100 |
commit | b198d0a205a485ed591a89b2d299a0b909ea5567 (patch) | |
tree | 8ae1db16136637833665ceb31e57531d5431ee84 | |
parent | 4c0b26cf7fa81c04aba5a1874a8f700a3acbe2ce (diff) |
Fixed contact import into QDeclarativeContactModel.
Create a ContactExporterResourceHandler which store the contact
binary data into a temporary file while converting to QContact.
Change-Id: Ia532f43a08e871efb290c675c81dc03b09c96658
Reviewed-by: Alex Blasche <alexander.blasche@theqtcompany.com>
-rw-r--r-- | src/imports/contacts/qdeclarativecontactmodel.cpp | 77 |
1 files changed, 77 insertions, 0 deletions
diff --git a/src/imports/contacts/qdeclarativecontactmodel.cpp b/src/imports/contacts/qdeclarativecontactmodel.cpp index 43a2cea1d..7c9fb5565 100644 --- a/src/imports/contacts/qdeclarativecontactmodel.cpp +++ b/src/imports/contacts/qdeclarativecontactmodel.cpp @@ -46,6 +46,10 @@ #include <QtCore/qmap.h> #include <QtCore/qpointer.h> #include <QtCore/qurl.h> +#include <QtCore/qmimedatabase.h> +#include <QtCore/qmimetype.h> +#include <QtCore/qtemporaryfile.h> +#include <QtCore/qdir.h> #include <QtGui/qcolor.h> #include <QtGui/qpixmap.h> @@ -95,6 +99,76 @@ QT_BEGIN_NAMESPACE \sa RelationshipModel, Contact, {QContactManager} */ +// Helper class to store contact binary data into a temporary file +// +// QContactVcard only supports URL values for images. During a vcard import if the contact cotains +// an avatar, ringtone or any property formated in a binary data, QVersit will use the +// ContactExporterResourceHandler to store the binary data. +// The default implementation of QVersitResourceHandler does not store any data and that +// causes avatar data loss during the import process. +// This class will store the data into a temporary file and removes the file when the model gets destroyed. +class ContactExporterResourceHandler : public QVersitResourceHandler +{ +public: + ContactExporterResourceHandler() + { + } + + ~ContactExporterResourceHandler() + { + foreach (const QString& fileName, m_files) + QFile::remove(fileName); + + m_files.clear(); + } + + bool saveResource(const QByteArray& contents, + const QVersitProperty& property, + QString* location) + { + const QMimeType mt = QMimeDatabase().mimeTypeForData(contents); + QString extension(QStringLiteral("data")); + if (mt.isValid()) + extension = mt.suffixes()[0]; + + // use property.name() to create a new file for each binary property (avatar, ringtone, etc...) + QTemporaryFile tmpFile(QString::fromLatin1("%1/%2_XXXXXX.%3") + .arg(QDir::tempPath()) + .arg(property.name().toLower()) + .arg(extension)); + tmpFile.setAutoRemove(false); + if (tmpFile.open()) { + // the location expect a string in url format ex: file:///tmp/filename.png + *location = QUrl::fromLocalFile(tmpFile.fileName()).toString(); + m_files << *location; + tmpFile.write(contents); + tmpFile.close(); + return true; + } + return false; + } + + bool loadResource(const QString &location, QByteArray *contents, QString *mimeType) + { + if (location.isEmpty()) + return false; + + QFile file(location); + if (!file.open(QIODevice::ReadOnly)) + return false; + + *contents = file.readAll(); + const QMimeType mt = QMimeDatabase().mimeTypeForData(*contents); + if (mt.isValid()) + *mimeType = mt.suffixes()[0]; + + return !contents->isEmpty(); + } + + QStringList m_files; +}; + + class QDeclarativeContactModelPrivate { public: @@ -125,6 +199,7 @@ public: QVersitReader m_reader; QVersitWriter m_writer; QStringList m_importProfiles; + ContactExporterResourceHandler m_resourceHandler; QContactManager::Error m_error; @@ -404,6 +479,7 @@ void QDeclarativeContactModel::exportContacts(const QUrl& url, const QStringList QString profile = profiles.isEmpty()? QString() : profiles.at(0); //only one profile string supported now. QVersitContactExporter exporter(profile); + exporter.setResourceHandler(&d->m_resourceHandler); QList<QContact> contacts; if (declarativeContacts.isEmpty()) { @@ -574,6 +650,7 @@ void QDeclarativeContactModel::startImport(QVersitReader::State state) { if (state == QVersitReader::FinishedState || state == QVersitReader::CanceledState) { QVersitContactImporter importer(d->m_importProfiles); + importer.setResourceHandler(&d->m_resourceHandler); importer.importDocuments(d->m_reader.results()); QList<QContact> contacts = importer.contacts(); |