| •1 |
/* |
| 2 |
* Copyright (C) 2020 UBports Foundation |
| 3 |
* Author(s): Alberto Mardegan <[email protected]> |
| 4 |
* |
| 5 |
* This program is free software; you can redistribute it and/or modify |
| 6 |
* it under the terms of the GNU General Public License as published by |
| 7 |
* the Free Software Foundation; version 3. |
| 8 |
* |
| 9 |
* This program is distributed in the hope that it will be useful, |
| 10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 |
* GNU General Public License for more details. |
| 13 |
* |
| 14 |
* You should have received a copy of the GNU General Public License |
| 15 |
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 16 |
*/ |
| 17 |
|
| 18 |
#include <QCoreApplication> |
| 19 |
#include <QContactAvatar> |
| 20 |
#include <QContactExtendedDetail> |
| 21 |
#include <QDebug> |
| 22 |
#include <QDir> |
| 23 |
#include <QSqlDatabase> |
| 24 |
#include <QSqlQuery> |
| 25 |
#include <qcontact.h> |
| 26 |
#include <qcontactcollectionid.h> |
| 27 |
#include <qcontactmanager.h> |
| 28 |
#include <qversitcontactimporter.h> |
| 29 |
#include <qversitdocument.h> |
| 30 |
#include <qversitreader.h> |
| 31 |
#include <QContactRingtone> |
| 32 |
#include <QContactDisplayLabel> |
| 33 |
|
| 34 |
#define QTCONTACTS_STORAGE_PATH "/.local/share/system/privileged/Contacts/qtcontacts-sqlite" |
| 35 |
|
| 36 |
class EdsReader { |
| 37 |
public: |
| 38 |
EdsReader(const QString &filePath): |
| 39 |
m_db(QSqlDatabase::addDatabase("QSQLITE", "eds")) |
| 40 |
{ |
| 41 |
m_db.setDatabaseName(filePath); |
| 42 |
if (!m_db.open()) { |
| 43 |
qCritical() << "Cannot open DB file" << filePath; |
| 44 |
} |
| 45 |
|
| 46 |
m_query = QSqlQuery(m_db); |
| 47 |
m_query.exec("SELECT vcard FROM 'folder_id'"); |
| 48 |
} |
| 49 |
|
| 50 |
bool next() { |
| 51 |
return m_query.next(); |
| 52 |
} |
| 53 |
|
| 54 |
QByteArray vCard() const { |
| 55 |
return m_query.value(0).toByteArray(); |
| 56 |
} |
| 57 |
|
| 58 |
private: |
| 59 |
QSqlDatabase m_db; |
| 60 |
QSqlQuery m_query; |
| 61 |
}; |
| 62 |
|
| 63 |
class Importer { |
| 64 |
public: |
| 65 |
Importer(const QByteArray &localCollectionId, const QString &photosPath): |
| 66 |
m_manager("org.nemomobile.contacts.sqlite"), |
| 67 |
m_collectionId(m_manager.managerUri(), localCollectionId), |
| 68 |
m_importer("Preserve"), |
| 69 |
m_numCards(0), |
| 70 |
m_photosPath(photosPath) |
| 71 |
{ |
| 72 |
} |
| 73 |
|
| 74 |
void addVCard(const QByteArray &vcard) { |
| 75 |
m_vcards += "\r\n" + vcard; |
| 76 |
m_numCards++; |
| 77 |
if (m_numCards % 200 == 0) { // save every 200 vcards |
| 78 |
importVCards(); |
| 79 |
m_vcards.clear(); |
| 80 |
qDebug() << "imported " << m_numCards << " contacts"; |
| 81 |
} |
| 82 |
} |
| 83 |
|
| 84 |
bool importVCards() { |
| 85 |
QtVersit::QVersitReader reader(m_vcards); |
| 86 |
reader.startReading(); |
| 87 |
reader.waitForFinished(); |
| 88 |
if (Q_UNLIKELY(reader.error() != QtVersit::QVersitReader::NoError)) { |
| 89 |
qWarning() << "Error reading VCARD:" << reader.error(); |
| 90 |
} |
| 91 |
|
| 92 |
bool ok = m_importer.importDocuments(reader.results()); |
| 93 |
|
| 94 |
QMap<int, QContactManager::Error> errorMap; |
| 95 |
QList<QContact> contacts = m_importer.contacts(); |
| 96 |
if (!m_collectionId.isNull()) { |
| 97 |
for (QContact &contact: contacts) { |
| 98 |
|
| 99 |
// handle deleted contact and ringtones |
| 100 |
QString ringToneUrl; |
| 101 |
bool isDeleted = false; |
| 102 |
for(const QContactExtendedDetail det: contact.details(QContactDetail::TypeExtendedDetail)) { |
| 103 |
if (det.name() == "X-RINGTONE-URL") { |
| 104 |
ringToneUrl = det.data().toString(); |
| 105 |
} else if (det.name() == "X-DELETED-AT") { |
| 106 |
isDeleted = true; |
| 107 |
} |
| 108 |
} |
| 109 |
|
| 110 |
if (isDeleted) { |
| 111 |
QContactDisplayLabel displayLabel = contact.detail<QContactDisplayLabel>(); |
| 112 |
qDebug() << "contact " << displayLabel.label() << " marked as deleted, skipped"; |
| 113 |
continue; |
| 114 |
} |
| 115 |
|
| 116 |
if (!ringToneUrl.isEmpty()) { |
| 117 |
QContactRingtone ringTone = contact.detail<QContactRingtone>(); |
| 118 |
ringTone.setAudioRingtoneUrl(ringToneUrl); |
| 119 |
qDebug() << "Found Ringtone" << ringToneUrl; |
| 120 |
contact.saveDetail(&ringTone); |
| 121 |
} |
| 122 |
|
| 123 |
// avatar migration |
| 124 |
QContactAvatar avatar = contact.detail<QContactAvatar>(); |
| 125 |
const QString imageUrl = avatar.imageUrl().toLocalFile(); |
| 126 |
if (!imageUrl.isEmpty()) { |
| 127 |
|
| 128 |
QFileInfo avatarImg(imageUrl); |
| 129 |
if (avatarImg.exists()) { |
| 130 |
QString toFilePath = m_photosPath + "/" +avatarImg.fileName(); |
| 131 |
if (!QFile::copy(imageUrl, toFilePath)) { |
| 132 |
qWarning() << "failed to copy avatar image from " << imageUrl << "to" << toFilePath; |
| 133 |
} else { |
| 134 |
avatar.setImageUrl(QUrl(toFilePath)); |
| 135 |
} |
| 136 |
} |
| 137 |
|
| 138 |
} |
| 139 |
|
| 140 |
contact.setCollectionId(m_collectionId); |
| 141 |
} |
| 142 |
} |
| 143 |
ok &= m_manager.saveContacts(&contacts, &errorMap); |
| 144 |
if (Q_UNLIKELY(!errorMap.isEmpty())) { |
| 145 |
qWarning() << "Errors importing contacts:" << errorMap; |
| 146 |
} |
| 147 |
|
| 148 |
return ok; |
| 149 |
} |
| 150 |
|
| 151 |
private: |
| 152 |
QContactManager m_manager; |
| 153 |
QContactCollectionId m_collectionId; |
| 154 |
QtVersit::QVersitContactImporter m_importer; |
| 155 |
int m_numCards; |
| 156 |
QByteArray m_vcards; |
| 157 |
QString m_photosPath; |
| 158 |
}; |
| 159 |
|
| 160 |
int main(int argc, char **argv) { |
| 161 |
QCoreApplication app(argc, argv); |
| 162 |
|
| 163 |
if (app.arguments().count() < 3) { |
| 164 |
qWarning() << "too few arguments"; |
| 165 |
return EXIT_FAILURE; |
| 166 |
} |
| 167 |
QString databaseFile = app.arguments()[1]; |
| 168 |
EdsReader edsReader(databaseFile); |
| 169 |
|
| 170 |
QByteArray collectionId = app.arguments().count() > 2 ? |
| 171 |
app.arguments()[2].toUtf8() : QByteArray(); |
| 172 |
|
| 173 |
// prepare for photos storage |
| 174 |
QFileInfo fi(databaseFile); |
| 175 |
QString photosDir = fi.absoluteDir().absolutePath() + "/photos"; |
| 176 |
|
| 177 |
QString targetPhotosPath = QDir::homePath() + QTCONTACTS_STORAGE_PATH + "/photos"; |
| 178 |
QDir targetPhotosDir; |
| 179 |
if (!targetPhotosDir.mkpath(targetPhotosPath)) { |
| 180 |
qWarning() << "failed to create " << targetPhotosPath; |
| 181 |
return EXIT_FAILURE; |
| 182 |
} |
| 183 |
qDebug() << "created photos dir " << targetPhotosPath; |
| 184 |
|
| 185 |
Importer importer(collectionId, targetPhotosPath); |
| 186 |
|
| 187 |
while (edsReader.next()) { |
| 188 |
importer.addVCard(edsReader.vCard()); |
| 189 |
} |
| 190 |
|
| 191 |
bool ok = importer.importVCards(); |
| 192 |
return ok ? EXIT_SUCCESS : EXIT_FAILURE; |
| 193 |
} |