/* * Copyright (C) 2020 UBports Foundation * Author(s): Alberto Mardegan * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; version 3. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #define QTCONTACTS_STORAGE_PATH "/.local/share/system/privileged/Contacts/qtcontacts-sqlite" class EdsReader { public: EdsReader(const QString &filePath): m_db(QSqlDatabase::addDatabase("QSQLITE", "eds")) { m_db.setDatabaseName(filePath); if (!m_db.open()) { qCritical() << "Cannot open DB file" << filePath; } m_query = QSqlQuery(m_db); m_query.exec("SELECT vcard FROM 'folder_id'"); } bool next() { return m_query.next(); } QByteArray vCard() const { return m_query.value(0).toByteArray(); } private: QSqlDatabase m_db; QSqlQuery m_query; }; class Importer { public: Importer(const QByteArray &localCollectionId, const QString &photosPath): m_manager("org.nemomobile.contacts.sqlite"), m_collectionId(m_manager.managerUri(), localCollectionId), m_importer("Preserve"), m_numCards(0), m_photosPath(photosPath) { } void addVCard(const QByteArray &vcard) { m_vcards += "\r\n" + vcard; m_numCards++; if (m_numCards % 200 == 0) { // save every 200 vcards importVCards(); m_vcards.clear(); qDebug() << "imported " << m_numCards << " contacts"; } } bool importVCards() { QtVersit::QVersitReader reader(m_vcards); reader.startReading(); reader.waitForFinished(); if (Q_UNLIKELY(reader.error() != QtVersit::QVersitReader::NoError)) { qWarning() << "Error reading VCARD:" << reader.error(); } bool ok = m_importer.importDocuments(reader.results()); QMap errorMap; QList contacts = m_importer.contacts(); if (!m_collectionId.isNull()) { for (QContact &contact: contacts) { // handle deleted contact and ringtones QString ringToneUrl; bool isDeleted = false; for(const QContactExtendedDetail det: contact.details(QContactDetail::TypeExtendedDetail)) { if (det.name() == "X-RINGTONE-URL") { ringToneUrl = det.data().toString(); } else if (det.name() == "X-DELETED-AT") { isDeleted = true; } } if (isDeleted) { QContactDisplayLabel displayLabel = contact.detail(); qDebug() << "contact " << displayLabel.label() << " marked as deleted, skipped"; continue; } if (!ringToneUrl.isEmpty()) { QContactRingtone ringTone = contact.detail(); ringTone.setAudioRingtoneUrl(ringToneUrl); qDebug() << "Found Ringtone" << ringToneUrl; contact.saveDetail(&ringTone); } // avatar migration QContactAvatar avatar = contact.detail(); const QString imageUrl = avatar.imageUrl().toLocalFile(); if (!imageUrl.isEmpty()) { QFileInfo avatarImg(imageUrl); if (avatarImg.exists()) { QString toFilePath = m_photosPath + "/" +avatarImg.fileName(); if (!QFile::copy(imageUrl, toFilePath)) { qWarning() << "failed to copy avatar image from " << imageUrl << "to" << toFilePath; } else { avatar.setImageUrl(QUrl(toFilePath)); } } } contact.setCollectionId(m_collectionId); } } ok &= m_manager.saveContacts(&contacts, &errorMap); if (Q_UNLIKELY(!errorMap.isEmpty())) { qWarning() << "Errors importing contacts:" << errorMap; } return ok; } private: QContactManager m_manager; QContactCollectionId m_collectionId; QtVersit::QVersitContactImporter m_importer; int m_numCards; QByteArray m_vcards; QString m_photosPath; }; int main(int argc, char **argv) { QCoreApplication app(argc, argv); if (app.arguments().count() < 3) { qWarning() << "too few arguments"; return EXIT_FAILURE; } QString databaseFile = app.arguments()[1]; EdsReader edsReader(databaseFile); QByteArray collectionId = app.arguments().count() > 2 ? app.arguments()[2].toUtf8() : QByteArray(); // prepare for photos storage QFileInfo fi(databaseFile); QString photosDir = fi.absoluteDir().absolutePath() + "/photos"; QString targetPhotosPath = QDir::homePath() + QTCONTACTS_STORAGE_PATH + "/photos"; QDir targetPhotosDir; if (!targetPhotosDir.mkpath(targetPhotosPath)) { qWarning() << "failed to create " << targetPhotosPath; return EXIT_FAILURE; } qDebug() << "created photos dir " << targetPhotosPath; Importer importer(collectionId, targetPhotosPath); while (edsReader.next()) { importer.addVCard(edsReader.vCard()); } bool ok = importer.importVCards(); return ok ? EXIT_SUCCESS : EXIT_FAILURE; }