| 1 |
/* |
| 2 |
* Copyright (C) 2012-2014 Canonical, Ltd. |
| 3 |
* |
| 4 |
* This program is free software; you can redistribute it and/or modify |
| 5 |
* it under the terms of the GNU General Public License as published by |
| 6 |
* the Free Software Foundation; version 3. |
| 7 |
* |
| 8 |
* This program is distributed in the hope that it will be useful, |
| 9 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 |
* GNU General Public License for more details. |
| 12 |
* |
| 13 |
* You should have received a copy of the GNU General Public License |
| 14 |
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 15 |
*/ |
| 16 |
|
| 17 |
#include "contacts.h" |
| 18 |
#include "imagescalethread.h" |
| 19 |
|
| 20 |
#include <QtCore/QCoreApplication> |
| 21 |
#include <QSettings> |
| 22 |
#include <QtCore/QStringList> |
| 23 |
#include <QtCore/QDebug> |
| 24 |
#include <QtCore/QDir> |
| 25 |
#include <QtCore/QUrl> |
| 26 |
#include <QtCore/QMimeType> |
| 27 |
#include <QtCore/QMimeDatabase> |
| 28 |
#include <QtCore/QLockFile> |
| 29 |
#include <QtCore/QThreadPool> |
| 30 |
#include <QtConcurrent/QtConcurrent> |
| 31 |
#include <QContactManager> |
| 32 |
#include <QContactFetchByIdRequest> |
| 33 |
#include <QContactAbstractRequest> |
| 34 |
#include <QContactFetchRequest> |
| 35 |
#include <QContactAvatar> |
| 36 |
#include <QContactRelationship> |
| 37 |
#include <QJSEngine> |
| 38 |
#include <QJSValue> |
| 39 |
#include <QVersitReader> |
| 40 |
#include <QVersitResourceHandler> |
| 41 |
#include <QVersitContactImporter> |
| 42 |
|
| 43 |
#include "config.h" |
| 44 |
|
| 45 |
QTCONTACTS_USE_NAMESPACE |
| 46 |
QTVERSIT_USE_NAMESPACE |
| 47 |
|
| 48 |
|
| 49 |
// Helper class to store contact binary data into a temporary file |
| 50 |
// |
| 51 |
// QContactVcard only supports URL values for images. During a vcard import if the contact cotains |
| 52 |
// an avatar, ringtone or any property formated in a binary data, QVersit will use the |
| 53 |
// ContactExporterResourceHandler to store the binary data. |
| 54 |
// The default implementation of QVersitResourceHandler does not store any data and that |
| 55 |
// causes avatar data loss during the import process. |
| 56 |
// This class will store the data into a temporary file and removes the file when the app gets destroyed. |
| 57 |
// additionally move the avatar to its final location |
| 58 |
class ContactExporterResourceHandler : public QVersitResourceHandler |
| 59 |
{ |
| 60 |
public: |
| 61 |
ContactExporterResourceHandler() |
| 62 |
{ |
| 63 |
} |
| 64 |
|
| 65 |
~ContactExporterResourceHandler() |
| 66 |
{ |
| 67 |
foreach (const QString& fileName, m_files) { |
| 68 |
QFile::remove(fileName); |
| 69 |
} |
| 70 |
|
| 71 |
m_files.clear(); |
| 72 |
} |
| 73 |
|
| 74 |
bool saveResource(const QByteArray& contents, |
| 75 |
const QVersitProperty& property, |
| 76 |
QString* location) |
| 77 |
{ |
| 78 |
const QMimeType mt = QMimeDatabase().mimeTypeForData(contents); |
| 79 |
QString extension(QStringLiteral("data")); |
| 80 |
if (mt.isValid()) |
| 81 |
extension = mt.suffixes()[0]; |
| 82 |
|
| 83 |
// use property.name() to create a new file for each binary property (avatar, ringtone, etc...) |
| 84 |
QTemporaryFile tmpFile(QString::fromLatin1("%1/%2_XXXXXX.%3") |
| 85 |
.arg(QDir::tempPath()) |
| 86 |
.arg(property.name().toLower()) |
| 87 |
.arg(extension)); |
| 88 |
tmpFile.setAutoRemove(false); |
| 89 |
if (tmpFile.open()) { |
| 90 |
|
| 91 |
tmpFile.write(contents); |
| 92 |
tmpFile.close(); |
| 93 |
|
| 94 |
QString destinationFilePath = tmpFile.fileName(); |
| 95 |
// only avatar for now |
| 96 |
if (property.name().toLower() == "photo") { |
| 97 |
|
| 98 |
QFileInfo tmpFileInfo(destinationFilePath); |
| 99 |
QString targetPath = QDir::homePath() + AVATAR_STORAGE_PATH; |
| 100 |
destinationFilePath = targetPath + "/" + tmpFileInfo.fileName(); |
| 101 |
|
| 102 |
if (!tmpFile.copy(destinationFilePath)) { |
| 103 |
qWarning() << "Failed to copy avatar to " << destinationFilePath; |
| 104 |
return false; |
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
// the location expect a string in url format ex: file:///tmp/filename.png |
| 109 |
*location = QUrl::fromLocalFile(destinationFilePath).toString(); |
| 110 |
// store temp file path for future removal |
| 111 |
m_files << tmpFile.fileName(); |
| 112 |
|
| 113 |
return true; |
| 114 |
} |
| 115 |
return false; |
| 116 |
} |
| 117 |
|
| 118 |
bool loadResource(const QString &location, QByteArray *contents, QString *mimeType) |
| 119 |
{ |
| 120 |
if (location.isEmpty()) |
| 121 |
return false; |
| 122 |
|
| 123 |
QFile file(location); |
| 124 |
if (!file.open(QIODevice::ReadOnly)) |
| 125 |
return false; |
| 126 |
|
| 127 |
*contents = file.readAll(); |
| 128 |
const QMimeType mt = QMimeDatabase().mimeTypeForData(*contents); |
| 129 |
if (mt.isValid()) |
| 130 |
*mimeType = mt.suffixes()[0]; |
| 131 |
|
| 132 |
return !contents->isEmpty(); |
| 133 |
} |
| 134 |
|
| 135 |
QStringList m_files; |
| 136 |
}; |
| 137 |
|
| 138 |
class ContactsPrivate |
| 139 |
{ |
| 140 |
public: |
| 141 |
ContactsPrivate(): mManager(0) |
| 142 |
{ |
| 143 |
} |
| 144 |
~ContactsPrivate() |
| 145 |
{ |
| 146 |
if (mManager) |
| 147 |
delete mManager; |
| 148 |
} |
| 149 |
|
| 150 |
QContactManager* mManager; |
| 151 |
QVersitReader mReader; |
| 152 |
ContactExporterResourceHandler mResourceHandler; |
| 153 |
}; |
| 154 |
|
| 155 |
|
| 156 |
LomiriContacts::LomiriContacts(QObject *parent) |
| 157 |
: QObject(parent), |
| 158 |
d(new ContactsPrivate), |
| 159 |
m_fileWatcher(new QFileSystemWatcher) |
| 160 |
{ |
| 161 |
// We need to monitor the tmp dir since the file could not exists at this point |
| 162 |
m_fileWatcher->addPath(QDir::tempPath()); |
| 163 |
connect(m_fileWatcher.data(), |
| 164 |
SIGNAL(directoryChanged(QString)), |
| 165 |
SIGNAL(updateIsRunningChanged())); |
| 166 |
connect(m_fileWatcher.data(), |
| 167 |
SIGNAL(fileChanged(QString)), |
| 168 |
SIGNAL(updateIsRunningChanged())); |
| 169 |
|
| 170 |
connect(&d->mReader, SIGNAL(stateChanged(QVersitReader::State)), this, SLOT(startImport(QVersitReader::State))); |
| 171 |
|
| 172 |
} |
| 173 |
|
| 174 |
LomiriContacts::~LomiriContacts() |
| 175 |
{ |
| 176 |
|
| 177 |
} |
| 178 |
|
| 179 |
QString LomiriContacts::tempPath() const |
| 180 |
{ |
| 181 |
return QDir::tempPath(); |
| 182 |
} |
| 183 |
|
| 184 |
QString LomiriContacts::aggregateCollectionId() const |
| 185 |
{ |
| 186 |
return QString(AGGREGATE_COLLECTION_ID); |
| 187 |
} |
| 188 |
|
| 189 |
QString LomiriContacts::contactInitialsFromString(const QString &value) |
| 190 |
{ |
| 191 |
if (value.isEmpty() || !value.at(0).isLetter()) { |
| 192 |
return QString(); |
| 193 |
} |
| 194 |
|
| 195 |
QString initials; |
| 196 |
QStringList parts = value.split(" "); |
| 197 |
initials = parts.first().at(0).toUpper(); |
| 198 |
if (parts.size() > 1) { |
| 199 |
initials += parts.last().at(0).toUpper(); |
| 200 |
} |
| 201 |
|
| 202 |
return initials; |
| 203 |
} |
| 204 |
|
| 205 |
QString LomiriContacts::normalized(const QString &value) |
| 206 |
{ |
| 207 |
QString s2 = value.normalized(QString::NormalizationForm_D); |
| 208 |
QString out; |
| 209 |
|
| 210 |
for (int i=0, j=s2.length(); i<j; i++) |
| 211 |
{ |
| 212 |
// strip diacritic marks |
| 213 |
if (s2.at(i).category() != QChar::Mark_NonSpacing && |
| 214 |
s2.at(i).category() != QChar::Mark_SpacingCombining) { |
| 215 |
out.append(s2.at(i)); |
| 216 |
} |
| 217 |
} |
| 218 |
return out; |
| 219 |
} |
| 220 |
|
| 221 |
void LomiriContacts::importContacts(const QUrl &url) |
| 222 |
{ |
| 223 |
// Reader is capable of handling only one request at the time. |
| 224 |
qDebug() << "start importing url:" << url; |
| 225 |
|
| 226 |
ImportError importError = ImportNotReadyError; |
| 227 |
if (d->mReader.state() != QtVersit::QVersitReader::ActiveState) { |
| 228 |
|
| 229 |
QFile* file = new QFile(url.toLocalFile()); |
| 230 |
bool ok = file->open(QIODevice::ReadOnly); |
| 231 |
if (ok) { |
| 232 |
d->mReader.setDevice(file); |
| 233 |
if (d->mReader.startReading()) { |
| 234 |
return; |
| 235 |
} |
| 236 |
importError = LomiriContacts::ImportError(d->mReader.error()); |
| 237 |
} else { |
| 238 |
importError = ImportIOError; |
| 239 |
} |
| 240 |
} |
| 241 |
|
| 242 |
qDebug() << "import error" << importError; |
| 243 |
emit importCompleted(importError, QStringList()); |
| 244 |
} |
| 245 |
|
| 246 |
void LomiriContacts::removeAggregateContacts(const QStringList &aggregateIds, const QJSValue &callback) |
| 247 |
{ |
| 248 |
|
| 249 |
QtConcurrent::run([=]() { |
| 250 |
|
| 251 |
|
| 252 |
QContactManager* mManager = new QContactManager(QString(DEFAULT_CONTACT_MANAGER)); |
| 253 |
|
| 254 |
QMap<int, QContactManager::Error> errorMap; |
| 255 |
bool success = true; |
| 256 |
|
| 257 |
|
| 258 |
for (auto &aggregateId: aggregateIds) { |
| 259 |
QList<QContactId> constituentsIds; |
| 260 |
QList<QContactRelationship> rels = mManager->relationships(QContactRelationship::Aggregates(), QContactId::fromString(aggregateId), QContactRelationship::First); |
| 261 |
for (auto &rel: rels) { |
| 262 |
QContactId cid = rel.second(); |
| 263 |
constituentsIds << cid; |
| 264 |
} |
| 265 |
|
| 266 |
success = mManager->removeContacts(constituentsIds, &errorMap); |
| 267 |
if (Q_UNLIKELY(!errorMap.isEmpty())) { |
| 268 |
qWarning() << "Errors while removing contacts:" << errorMap; |
| 269 |
success = false; |
| 270 |
} |
| 271 |
} |
| 272 |
|
| 273 |
mManager->deleteLater(); |
| 274 |
|
| 275 |
QJSValue cbCopy(callback); |
| 276 |
QJSEngine *engine = qjsEngine(this); |
| 277 |
cbCopy.call(QJSValueList { engine->toScriptValue(success) }); |
| 278 |
}); |
| 279 |
|
| 280 |
} |
| 281 |
|
| 282 |
void LomiriContacts::startImport(QVersitReader::State state) |
| 283 |
{ |
| 284 |
|
| 285 |
if (state == QVersitReader::FinishedState || state == QVersitReader::CanceledState) { |
| 286 |
QVersitContactImporter importer; |
| 287 |
importer.setResourceHandler(&d->mResourceHandler); |
| 288 |
importer.importDocuments(d->mReader.results()); |
| 289 |
|
| 290 |
QList<QContact> contacts = importer.contacts(); |
| 291 |
qDebug() << "Found " << contacts.size() << " contacts"; |
| 292 |
|
| 293 |
|
| 294 |
delete d->mReader.device(); |
| 295 |
d->mReader.setDevice(0); |
| 296 |
|
| 297 |
QStringList ids; |
| 298 |
if (!d->mManager) { |
| 299 |
d->mManager = new QContactManager(QString(DEFAULT_CONTACT_MANAGER)); |
| 300 |
} |
| 301 |
|
| 302 |
if (d->mManager) { |
| 303 |
|
| 304 |
// paginating |
| 305 |
int offset = 0; |
| 306 |
int batchSize = 200; |
| 307 |
|
| 308 |
while (offset <= contacts.size() -1) { |
| 309 |
|
| 310 |
batchSize = qMin(contacts.size()-offset, 200); |
| 311 |
|
| 312 |
QMap<int, QContactManager::Error> errorMap; |
| 313 |
QList<QContact> cts = contacts.mid(offset, batchSize); |
| 314 |
d->mManager->saveContacts(&cts, &errorMap); |
| 315 |
if (Q_UNLIKELY(!errorMap.isEmpty())) { |
| 316 |
qWarning() << "Errors while saving contacts:" << errorMap; |
| 317 |
emit importCompleted(LomiriContacts::ImportUnspecifiedError, ids); |
| 318 |
} else { |
| 319 |
foreach (const QContact &c, cts) { |
| 320 |
ids << c.id().toString(); |
| 321 |
} |
| 322 |
} |
| 323 |
qDebug() << "imported batch from " << offset << " to " << offset + batchSize; |
| 324 |
offset += batchSize; |
| 325 |
} |
| 326 |
|
| 327 |
|
| 328 |
} |
| 329 |
|
| 330 |
emit importCompleted(LomiriContacts::ImportError(d->mReader.error()), ids); |
| 331 |
} |
| 332 |
} |
| 333 |
|
| 334 |
QString LomiriContacts::copyImage(const QUrl &imageUrl) |
| 335 |
{ |
| 336 |
ImageScaleThread *imgThread = new ImageScaleThread(imageUrl, this); |
| 337 |
QThreadPool::globalInstance()->start(imgThread); |
| 338 |
return imgThread->id(); |
| 339 |
} |
| 340 |
|
| 341 |
QUrl LomiriContacts::storeAvatarImage(const QUrl &imageUrl) |
| 342 |
{ |
| 343 |
|
| 344 |
QFileInfo tmpFileInfo(imageUrl.toLocalFile()); |
| 345 |
QString targetPath = QDir::homePath() + AVATAR_STORAGE_PATH; |
| 346 |
QString toFile = targetPath + "/" + tmpFileInfo.fileName(); |
| 347 |
|
| 348 |
if (!QFile::copy(imageUrl.toLocalFile(), toFile)) { |
| 349 |
qWarning() << "Failed to copy avatar to " << toFile; |
| 350 |
return imageUrl; |
| 351 |
} else { |
| 352 |
qDebug() << "copied avatar to" << toFile; |
| 353 |
} |
| 354 |
return QUrl(toFile); |
| 355 |
} |
| 356 |
|
| 357 |
bool LomiriContacts::containsLetters(const QString &value) |
| 358 |
{ |
| 359 |
foreach (const QChar &c, value) { |
| 360 |
if (c.isLetter()) { |
| 361 |
return true; |
| 362 |
} |
| 363 |
} |
| 364 |
return false; |
| 365 |
} |
| 366 |
|
| 367 |
bool LomiriContacts::removeFile(const QUrl &file) |
| 368 |
{ |
| 369 |
QString localFile = file.toLocalFile(); |
| 370 |
if (!localFile.isEmpty() && QFile::exists(localFile)) { |
| 371 |
return QFile::remove(localFile); |
| 372 |
} |
| 373 |
return false; |
| 374 |
} |
| 375 |
|
| 376 |
bool LomiriContacts::updateIsRunning() const |
| 377 |
{ |
| 378 |
return QFile::exists(updaterLockFile()); |
| 379 |
} |
| 380 |
|
| 381 |
|
| 382 |
QUrl LomiriContacts::tempFile(const QString &templateName) |
| 383 |
{ |
| 384 |
QTemporaryFile tmp(QString("%1/%2").arg(tempPath()).arg(templateName)); |
| 385 |
tmp.setAutoRemove(false); |
| 386 |
if (tmp.open()) |
| 387 |
return QUrl::fromLocalFile(tmp.fileName()); |
| 388 |
else |
| 389 |
return QUrl(); |
| 390 |
} |
| 391 |
|
| 392 |
uint LomiriContacts::qHash(const QString &str) |
| 393 |
{ |
| 394 |
return ::qHash(str); |
| 395 |
} |
| 396 |
|
| 397 |
QString LomiriContacts::updaterLockFile() |
| 398 |
{ |
| 399 |
return QString("%1/%2").arg(QDir::tempPath()).arg("/address-book-updater.lock"); |
| 400 |
} |
| 401 |
|
| 402 |
QString LomiriContacts::defaultCollectionId() const |
| 403 |
{ |
| 404 |
QSettings settings; |
| 405 |
return settings.value(SETTINGS_DEFAULT_COLLECTION_ID, DEFAULT_COLLECTION_ID).toString(); |
| 406 |
} |
| 407 |
|
| 408 |
void LomiriContacts::setDefaultCollectionId(const QString &newDefaultCollectionId) |
| 409 |
{ |
| 410 |
QSettings settings; |
| 411 |
settings.setValue(SETTINGS_DEFAULT_COLLECTION_ID, newDefaultCollectionId); |
| 412 |
settings.sync(); |
| 413 |
emit defaultCollectionIdChanged(); |
| 414 |
} |