1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256
|
/*
* Copyright (c) 2013 - 2020 Jolla Ltd.
* Copyright (c) 2020 Open Mobile Platform LLC.
*
* You may use this file under the terms of the BSD license as follows:
*
* "Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
* * Neither the name of Nemo Mobile nor Jolla Ltd nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
*/
#include "seasidepropertyhandler.h"
#include <QContactAvatar>
#include <QContactOnlineAccount>
#include <QContactPresence>
#include <QContactSyncTarget>
#include <QCryptographicHash>
#include <QDir>
#include <QImage>
#include <QDebug>
#include <qtcontacts-extensions.h>
namespace {
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.
// The data might be either a URL, a file path, or encoded image data
// It's hard to tell what the content is, because versit removes the encoding
// information in the process of decoding the data...
// Try to interpret the data as a URL
QString path(property.variantValue().toString());
QUrl url(path);
if (url.isValid()) {
// Treat remote URL as a true URL, and reference it in the avatar
if (!url.scheme().isEmpty() && !url.isLocalFile()) {
QContactAvatar newAvatar;
newAvatar.setImageUrl(url);
// we have successfully processed this PHOTO property.
return newAvatar;
}
}
if (!url.isValid()) {
// See if we can resolve the data as a local file path
url = QUrl::fromLocalFile(path);
}
QByteArray photoData;
if (url.isValid()) {
// Try to read the data from the referenced file
const QString filePath(url.path());
if (QFile::exists(filePath)) {
QFile file(filePath);
if (!file.open(QIODevice::ReadOnly)) {
qWarning() << "Unable to process photo data as file:" << path;
return QContactAvatar();
} else {
photoData = file.readAll();
}
}
}
if (photoData.isEmpty()) {
// Try to interpret the encoded property data as the image
photoData = property.variantValue().toByteArray();
if (photoData.isEmpty()) {
qWarning() << "Failed to extract avatar data from vCard PHOTO property";
return QContactAvatar();
}
}
QImage img;
bool loaded = img.loadFromData(photoData);
if (!loaded) {
qWarning() << "Failed to load avatar image from vCard PHOTO data";
return QContactAvatar();
}
// We will save the avatar image to disk in the system's data location
// Since we're importing user data, it should not require privileged access
const QString subdirectory(QString::fromLatin1(".local/share/system/Contacts/avatars"));
const QString photoDirPath(QDir::home().filePath(subdirectory));
// create the photo file dir if it doesn't exist.
QDir photoDir;
if (!photoDir.mkpath(photoDirPath)) {
qWarning() << "Failed to create avatar image directory when loading avatar image from vCard PHOTO data";
return QContactAvatar();
}
// construct the filename of the new avatar image.
QString photoFilePath = QString::fromLatin1(QCryptographicHash::hash(photoData, QCryptographicHash::Md5).toHex());
photoFilePath = photoDirPath + QDir::separator() + photoFilePath + QString::fromLatin1(".jpg");
// save the file to disk
bool saved = img.save(photoFilePath);
if (!saved) {
qWarning() << "Failed to save avatar image from vCard PHOTO data to" << photoFilePath;
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));
// we have successfully processed this PHOTO property.
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)
{
// Create an online account instance for demo purposes; it will not be connected
// to a registered telepathy account, so it won't actually be able to converse
// Try to interpret the data as a stringlist
const QString detail(property.variantValue().toString());
// The format is: URI/path/display-name/icon-path/service-provider/service-provider-display-name
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
const QStringList details(detail.split(QLatin1Char(';'), Qt::KeepEmptyParts));
#else
const QStringList details(detail.split(QLatin1Char(';'), QString::KeepEmptyParts));
#endif
if (details.count() == 6) {
QContactOnlineAccount qcoa;
qcoa.setValue(QContactOnlineAccount::FieldAccountUri, details.at(0));
qcoa.setValue(QContactOnlineAccount__FieldAccountPath, details.at(1));
qcoa.setValue(QContactOnlineAccount__FieldAccountDisplayName, details.at(2));
qcoa.setValue(QContactOnlineAccount__FieldAccountIconPath, details.at(3));
qcoa.setValue(QContactOnlineAccount::FieldServiceProvider, details.at(4));
qcoa.setValue(QContactOnlineAccount__FieldServiceProviderDisplayName, details.at(5));
qcoa.setDetailUri(QString::fromLatin1("%1:%2").arg(details.at(1)).arg(details.at(0)));
updatedDetails->append(qcoa);
// Since it is a demo account, give it a random presence state
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
const int state = (rand() % 4);
#else
const int state = (qrand() % 4);
#endif
QContactPresence presence;
presence.setPresenceState(state == 3 ? QContactPresence::PresenceBusy : (state == 2 ? QContactPresence::PresenceAway : QContactPresence::PresenceAvailable));
presence.setLinkedDetailUris(QStringList() << qcoa.detailUri());
updatedDetails->append(presence);
*alreadyProcessed = true;
} else {
qWarning() << "Invalid online account details:" << details;
}
}
void ignoreDetail(const QContactSyncTarget &detail, QSet<int> * processedFields, QList<QVersitProperty> * toBeRemoved, QList<QVersitProperty> * toBeAdded)
{
Q_UNUSED(detail)
Q_UNUSED(processedFields)
toBeAdded->clear();
toBeRemoved->clear();
}
}
class SeasidePropertyHandlerPrivate
{
public:
QSet<QContactDetail::DetailType> m_nonexportableDetails;
};
SeasidePropertyHandler::SeasidePropertyHandler(const QSet<QContactDetail::DetailType> &nonexportableDetails)
: QVersitContactHandler()
, priv(new SeasidePropertyHandlerPrivate)
{
priv->m_nonexportableDetails = nonexportableDetails;
}
SeasidePropertyHandler::~SeasidePropertyHandler()
{
delete priv;
}
void SeasidePropertyHandler::documentProcessed(const QVersitDocument &, QContact *)
{
// do nothing, have no state to clean.
}
void SeasidePropertyHandler::propertyProcessed(const QVersitDocument &, const QVersitProperty &property,
const QContact &, bool *alreadyProcessed, QList<QContactDetail> * updatedDetails)
{
const QString propertyName(property.name().toLower());
if (propertyName == QLatin1String("photo")) {
processPhoto(property, alreadyProcessed, updatedDetails);
} else if (propertyName == QLatin1String("x-nemomobile-onlineaccount-demo")) {
processOnlineAccount(property, alreadyProcessed, updatedDetails);
}
}
void SeasidePropertyHandler::contactProcessed(const QContact &, QVersitDocument *)
{
}
void SeasidePropertyHandler::detailProcessed(const QContact &, const QContactDetail &detail,
const QVersitDocument &, QSet<int> * processedFields, QList<QVersitProperty> * toBeRemoved, QList<QVersitProperty> * toBeAdded)
{
const QContactDetail::DetailType detailType(detail.type());
if (priv->m_nonexportableDetails.contains(detailType)) {
ignoreDetail(static_cast<const QContactSyncTarget &>(detail), processedFields, toBeRemoved, toBeAdded);
}
}
QContactAvatar SeasidePropertyHandler::avatarFromPhotoProperty(const QVersitProperty &property)
{
return ::avatarFromPhotoProperty(property);
}
|