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

Latest commit

 

History

History
298 lines (250 loc) · 11.7 KB

seasidepropertyhandler.cpp

File metadata and controls

298 lines (250 loc) · 11.7 KB
 
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
/*
* Copyright (C) 2013 Jolla Ltd.
* Contact: Chris Adams <chris.adams@jollamobile.com>
*
* 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."
*/
Dec 4, 2013
Dec 4, 2013
33
#include "seasidepropertyhandler.h"
34
35
#include <QContactAvatar>
Dec 4, 2013
Dec 4, 2013
36
37
#include <QContactOnlineAccount>
#include <QContactPresence>
Jun 30, 2014
Jun 30, 2014
38
#include <QContactSyncTarget>
39
40
41
42
#include <QCryptographicHash>
#include <QDir>
#include <QImage>
Dec 4, 2013
Dec 4, 2013
43
44
45
46
#include <qtcontacts-extensions.h>
namespace {
Mar 6, 2015
Mar 6, 2015
47
QContactAvatar avatarFromPhotoProperty(const QVersitProperty &property)
48
49
50
51
52
{
// 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
Sep 9, 2013
Sep 9, 2013
53
54
55
56
57
58
59
60
61
// 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()) {
62
63
64
65
QContactAvatar newAvatar;
newAvatar.setImageUrl(url);
// we have successfully processed this PHOTO property.
Mar 6, 2015
Mar 6, 2015
66
return newAvatar;
Sep 9, 2013
Sep 9, 2013
70
71
72
73
74
if (!url.isValid()) {
// See if we can resolve the data as a local file path
url = QUrl::fromLocalFile(path);
}
75
76
QByteArray photoData;
Sep 9, 2013
Sep 9, 2013
77
78
79
80
81
82
83
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;
Mar 6, 2015
Mar 6, 2015
84
return QContactAvatar();
Sep 9, 2013
Sep 9, 2013
85
86
87
} else {
photoData = file.readAll();
}
88
89
90
91
}
}
if (photoData.isEmpty()) {
Sep 9, 2013
Sep 9, 2013
92
93
94
95
// 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";
Mar 6, 2015
Mar 6, 2015
96
return QContactAvatar();
Sep 9, 2013
Sep 9, 2013
97
}
98
99
100
101
102
103
}
QImage img;
bool loaded = img.loadFromData(photoData);
if (!loaded) {
qWarning() << "Failed to load avatar image from vCard PHOTO data";
Mar 6, 2015
Mar 6, 2015
104
return QContactAvatar();
105
106
107
108
109
110
111
112
113
114
115
}
// 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";
Mar 6, 2015
Mar 6, 2015
116
return QContactAvatar();
117
118
119
120
121
122
123
124
125
126
}
// 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;
Mar 6, 2015
Mar 6, 2015
127
return QContactAvatar();
128
129
130
131
132
133
134
135
136
}
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.
Mar 6, 2015
Mar 6, 2015
137
138
139
140
141
142
143
144
145
146
return newAvatar;
}
void processPhoto(const QVersitProperty &property, bool *alreadyProcessed, QList<QContactDetail> * updatedDetails)
{
QContactAvatar newAvatar = avatarFromPhotoProperty(property);
if (!newAvatar.isEmpty()) {
updatedDetails->append(newAvatar);
*alreadyProcessed = true;
}
Dec 4, 2013
Dec 4, 2013
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
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
const QStringList details(detail.split(QLatin1Char(';'), QString::KeepEmptyParts));
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
const int state = (qrand() % 4);
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;
}
}
Jun 30, 2014
Jun 30, 2014
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
void processSyncTarget(const QVersitProperty &property, bool *alreadyProcessed, QList<QContactDetail> * updatedDetails)
{
// Set the sync target for this contact, if appropriate
// Try to interpret the data as a string
const QString data(property.variantValue().toString().toLower());
if (data == QString::fromLatin1("bluetooth") || data == QString::fromLatin1("was_local")) {
QList<QContactDetail>::iterator it = updatedDetails->begin(), end = updatedDetails->end();
for ( ; it != end; ++it) {
if ((*it).type() == QContactSyncTarget::Type)
break;
}
if (it != end) {
QContactSyncTarget &syncTarget(static_cast<QContactSyncTarget &>(*it));
syncTarget.setSyncTarget(data);
} else {
QContactSyncTarget syncTarget;
syncTarget.setSyncTarget(data);
updatedDetails->append(syncTarget);
}
*alreadyProcessed = true;
} else {
qWarning() << "Invalid syncTarget data:" << data;
}
Dec 4, 2013
Dec 4, 2013
211
212
}
Jun 30, 2014
Jun 30, 2014
213
void processSyncTarget(const QContactSyncTarget &detail, QSet<int> * processedFields, QList<QVersitProperty> * toBeRemoved, QList<QVersitProperty> * toBeAdded)
Dec 4, 2013
Dec 4, 2013
214
{
Jun 30, 2014
Jun 30, 2014
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
Q_UNUSED(processedFields)
Q_UNUSED(toBeRemoved)
// Include the sync target in the export if it is not 'local' but is exportable
const QString syncTarget(detail.syncTarget());
if (syncTarget == QString::fromLatin1("bluetooth") || syncTarget == QString::fromLatin1("was_local")) {
QVersitProperty stProperty;
stProperty.setName(QString::fromLatin1("X-NEMOMOBILE-SYNCTARGET"));
stProperty.setValue(syncTarget);
toBeAdded->append(stProperty);
} else if (!syncTarget.isEmpty() && syncTarget != QString::fromLatin1("local")) {
qWarning() << "Invalid syncTarget for export:" << syncTarget;
}
}
Feb 16, 2015
Feb 16, 2015
232
233
234
235
236
237
238
239
void ignoreDetail(const QContactSyncTarget &detail, QSet<int> * processedFields, QList<QVersitProperty> * toBeRemoved, QList<QVersitProperty> * toBeAdded)
{
Q_UNUSED(detail)
Q_UNUSED(processedFields)
toBeAdded->clear();
toBeRemoved->clear();
}
Jun 30, 2014
Jun 30, 2014
240
241
}
Feb 16, 2015
Feb 16, 2015
242
243
244
245
246
247
248
249
250
class SeasidePropertyHandlerPrivate
{
public:
QSet<QContactDetail::DetailType> m_nonexportableDetails;
};
SeasidePropertyHandler::SeasidePropertyHandler(const QSet<QContactDetail::DetailType> &nonexportableDetails)
: QVersitContactHandler()
, priv(new SeasidePropertyHandlerPrivate)
Jun 30, 2014
Jun 30, 2014
251
{
Feb 16, 2015
Feb 16, 2015
252
priv->m_nonexportableDetails = nonexportableDetails;
Jun 30, 2014
Jun 30, 2014
253
254
255
256
}
SeasidePropertyHandler::~SeasidePropertyHandler()
{
Feb 16, 2015
Feb 16, 2015
257
delete priv;
Jun 30, 2014
Jun 30, 2014
258
259
260
261
262
263
264
265
266
267
268
269
270
}
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")) {
Dec 4, 2013
Dec 4, 2013
271
processPhoto(property, alreadyProcessed, updatedDetails);
Jun 30, 2014
Jun 30, 2014
272
} else if (propertyName == QLatin1String("x-nemomobile-onlineaccount-demo")) {
Dec 4, 2013
Dec 4, 2013
273
processOnlineAccount(property, alreadyProcessed, updatedDetails);
Jun 30, 2014
Jun 30, 2014
274
275
276
277
278
} else if (propertyName == QLatin1String("x-nemomobile-synctarget")) {
processSyncTarget(property, alreadyProcessed, updatedDetails);
}
}
Feb 16, 2015
Feb 16, 2015
279
void SeasidePropertyHandler::contactProcessed(const QContact &, QVersitDocument *)
Jun 30, 2014
Jun 30, 2014
280
281
282
283
284
285
286
287
{
}
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());
Feb 16, 2015
Feb 16, 2015
288
289
290
if (priv->m_nonexportableDetails.contains(detailType)) {
ignoreDetail(static_cast<const QContactSyncTarget &>(detail), processedFields, toBeRemoved, toBeAdded);
} else if (detailType == QContactSyncTarget::Type) {
Jun 30, 2014
Jun 30, 2014
291
processSyncTarget(static_cast<const QContactSyncTarget &>(detail), processedFields, toBeRemoved, toBeAdded);
Dec 4, 2013
Dec 4, 2013
292
293
294
}
}
Mar 6, 2015
Mar 6, 2015
295
296
297
298
QContactAvatar SeasidePropertyHandler::avatarFromPhotoProperty(const QVersitProperty &property)
{
return ::avatarFromPhotoProperty(property);
}