Skip to content
This repository has been archived by the owner on Sep 4, 2021. It is now read-only.

Commit

Permalink
Browse files Browse the repository at this point in the history
[libcontacts] Add API to support PHOTO processing in client code
This commit adds API to allow clients to use libcontacts' standard
implementation of processPhoto() for handling PHOTO vCard properties
in their own vCard handling implementations.
  • Loading branch information
Chris Adams committed Mar 6, 2015
1 parent 5b5c00d commit 98d9c8e
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 11 deletions.
32 changes: 21 additions & 11 deletions src/seasidepropertyhandler.cpp
Expand Up @@ -44,7 +44,7 @@

namespace {

void processPhoto(const QVersitProperty &property, bool *alreadyProcessed, QList<QContactDetail> * updatedDetails)
QContactAvatar avatarFromPhotoProperty(const QVersitProperty &property)
{
// if the property is a PHOTO property, store the data to disk
// and then create an avatar detail which points to it.
Expand All @@ -61,11 +61,9 @@ void processPhoto(const QVersitProperty &property, bool *alreadyProcessed, QList
if (!url.scheme().isEmpty() && !url.isLocalFile()) {
QContactAvatar newAvatar;
newAvatar.setImageUrl(url);
updatedDetails->append(newAvatar);

// we have successfully processed this PHOTO property.
*alreadyProcessed = true;
return;
return newAvatar;
}
}

Expand All @@ -83,7 +81,7 @@ void processPhoto(const QVersitProperty &property, bool *alreadyProcessed, QList
QFile file(filePath);
if (!file.open(QIODevice::ReadOnly)) {
qWarning() << "Unable to process photo data as file:" << path;
return;
return QContactAvatar();
} else {
photoData = file.readAll();
}
Expand All @@ -95,15 +93,15 @@ void processPhoto(const QVersitProperty &property, bool *alreadyProcessed, QList
photoData = property.variantValue().toByteArray();
if (photoData.isEmpty()) {
qWarning() << "Failed to extract avatar data from vCard PHOTO property";
return;
return QContactAvatar();
}
}

QImage img;
bool loaded = img.loadFromData(photoData);
if (!loaded) {
qWarning() << "Failed to load avatar image from vCard PHOTO data";
return;
return QContactAvatar();
}

// We will save the avatar image to disk in the system's data location
Expand All @@ -115,7 +113,7 @@ void processPhoto(const QVersitProperty &property, bool *alreadyProcessed, QList
QDir photoDir;
if (!photoDir.mkpath(photoDirPath)) {
qWarning() << "Failed to create avatar image directory when loading avatar image from vCard PHOTO data";
return;
return QContactAvatar();
}

// construct the filename of the new avatar image.
Expand All @@ -126,18 +124,26 @@ void processPhoto(const QVersitProperty &property, bool *alreadyProcessed, QList
bool saved = img.save(photoFilePath);
if (!saved) {
qWarning() << "Failed to save avatar image from vCard PHOTO data to" << photoFilePath;
return;
return QContactAvatar();
}

qWarning() << "Successfully saved avatar image from vCard PHOTO data to" << photoFilePath;

// save the avatar detail - TODO: mark the avatar as "owned by the contact" (remove on delete)
QContactAvatar newAvatar;
newAvatar.setImageUrl(QUrl::fromLocalFile(photoFilePath));
updatedDetails->append(newAvatar);

// we have successfully processed this PHOTO property.
*alreadyProcessed = true;
return newAvatar;
}

void processPhoto(const QVersitProperty &property, bool *alreadyProcessed, QList<QContactDetail> * updatedDetails)
{
QContactAvatar newAvatar = avatarFromPhotoProperty(property);
if (!newAvatar.isEmpty()) {
updatedDetails->append(newAvatar);
*alreadyProcessed = true;
}
}

void processOnlineAccount(const QVersitProperty &property, bool *alreadyProcessed, QList<QContactDetail> * updatedDetails)
Expand Down Expand Up @@ -286,3 +292,7 @@ void SeasidePropertyHandler::detailProcessed(const QContact &, const QContactDet
}
}

QContactAvatar SeasidePropertyHandler::avatarFromPhotoProperty(const QVersitProperty &property)
{
return ::avatarFromPhotoProperty(property);
}
4 changes: 4 additions & 0 deletions src/seasidepropertyhandler.h
Expand Up @@ -46,6 +46,8 @@
#include <QVersitDocument>
#include <QVersitProperty>

#include <QContactAvatar>

QTCONTACTS_USE_NAMESPACE
QTVERSIT_USE_NAMESPACE

Expand Down Expand Up @@ -80,6 +82,8 @@ class CONTACTCACHE_EXPORT SeasidePropertyHandler : public QVersitContactHandler
void detailProcessed(const QContact &, const QContactDetail &detail,
const QVersitDocument &, QSet<int> * processedFields, QList<QVersitProperty> * toBeRemoved, QList<QVersitProperty> * toBeAdded);

static QContactAvatar avatarFromPhotoProperty(const QVersitProperty &property);

private:
SeasidePropertyHandlerPrivate *priv;
};
Expand Down

0 comments on commit 98d9c8e

Please sign in to comment.