From 9bf3a397c46be850ec39c00265b5315e3ba3a9e9 Mon Sep 17 00:00:00 2001
From: Sergey Chupligin <s.chupligin@omprussia.ru>
Date: Thu, 8 Nov 2018 13:20:45 +0300
Subject: [PATCH] Fix build with qt 5.9

---
 qtcontacts-sqlite.pro          |  3 +--
 rpm/qtcontacts-sqlite-qt5.spec | 26 ++++++++++++++------------
 src/engine/contactid.cpp       | 26 ++++++++++++++++++++++++++
 src/engine/contactid_p.h       | 16 +++++++++++++---
 src/engine/contactreader.cpp   | 21 ++++++++++++++++++++-
 src/engine/contactsengine.cpp  | 20 ++++++++++++++++++++
 src/engine/contactsengine.h    |  7 ++++++-
 src/engine/contactsplugin.cpp  |  4 ++++
 src/engine/contactwriter.cpp   | 15 ++++++++++++++-
 9 files changed, 118 insertions(+), 20 deletions(-)

diff --git a/qtcontacts-sqlite.pro b/qtcontacts-sqlite.pro
index 6ae3cad..ae42f79 100644
--- a/qtcontacts-sqlite.pro
+++ b/qtcontacts-sqlite.pro
@@ -1,7 +1,6 @@
 TEMPLATE = subdirs
 SUBDIRS = \
-        src \
-        tests
+        src
 OTHER_FILES += rpm/qtcontacts-sqlite-qt5.spec
 
 tests.depends = src
diff --git a/rpm/qtcontacts-sqlite-qt5.spec b/rpm/qtcontacts-sqlite-qt5.spec
index 050ff6b..8bbb896 100644
--- a/rpm/qtcontacts-sqlite-qt5.spec
+++ b/rpm/qtcontacts-sqlite-qt5.spec
@@ -1,3 +1,5 @@
+%define qtpim_verion %(rpm -q --qf "%{VERS""ION}\n" qt5-qtpim-contacts | tr --delete .  | cut -f1 -d"+" | cut -c1-3)
+
 Name: qtcontacts-sqlite-qt5
 Version: 0.2.36
 Release: 0
@@ -20,19 +22,19 @@ Requires: qt5-plugin-sqldriver-sqlite
 %defattr(-,root,root,-)
 %{_libdir}/qt5/plugins/contacts/*.so*
 
-%package tests
-Summary:    Unit tests for qtcontacts-sqlite-qt5
-Group:      System/Libraries
-BuildRequires:  pkgconfig(Qt5Test)
-Requires:   %{name} = %{version}-%{release}
+#%package tests
+#Summary:    Unit tests for qtcontacts-sqlite-qt5
+#Group:      System/Libraries
+#BuildRequires:  pkgconfig(Qt5Test)
+#Requires:   %{name} = %{version}-%{release}
 
-%description tests
-This package contains unit tests for the qtcontacts-sqlite-qt5 library.
+#%description tests
+#This package contains unit tests for the qtcontacts-sqlite-qt5 library.
 
-%files tests
-%defattr(-,root,root,-)
-/opt/tests/qtcontacts-sqlite-qt5/*
-%{_libdir}/qtcontacts-sqlite-qt5/libtestdlgg.so
+#%files tests
+#%defattr(-,root,root,-)
+#/opt/tests/qtcontacts-sqlite-qt5/*
+#%{_libdir}/qtcontacts-sqlite-qt5/libtestdlgg.so
 
 %package extensions
 Summary:    QtContacts extension headers for qtcontacts-sqlite-qt5
@@ -52,7 +54,7 @@ This package contains extension headers for the qtcontacts-sqlite-qt5 library.
 %setup -q -n %{name}-%{version}
 
 %build
-%qmake5 "VERSION=%{version}"
+%qmake5 "VERSION=%{version}" QTPIM_VERSION=%{qtpim_verion}
 make %{?_smp_mflags}
 
 %install
diff --git a/src/engine/contactid.cpp b/src/engine/contactid.cpp
index 7f7c733..47025e8 100644
--- a/src/engine/contactid.cpp
+++ b/src/engine/contactid.cpp
@@ -52,6 +52,15 @@ quint32 dbIdFromString(const QString &s)
     return 0;
 }
 
+QByteArray dbIdToLocalId(quint32 dbId)
+{
+    return QByteArray::number(dbId);
+}
+
+quint32 dbIdFromLocalId(const QByteArray &s)
+{
+    return quint32(s.toLongLong());
+}
 }
 
 #include <QContactManagerEngine>
@@ -63,10 +72,16 @@ QContactId ContactId::apiId(const QContact &contact)
 
 QContactId ContactId::apiId(quint32 dbId)
 {
+#if QTPIM_VERSION < 59
     ContactId *eid = new ContactId(dbId);
     return QContactId(eid);
+#else
+    return QContactId(QContactManager::buildUri(default_uri, QMap<QString, QString>()),
+                          dbIdToLocalId(dbId));
+#endif
 }
 
+
 quint32 ContactId::databaseId(const QContact &contact)
 {
     return databaseId(contact.id());
@@ -74,13 +89,18 @@ quint32 ContactId::databaseId(const QContact &contact)
 
 quint32 ContactId::databaseId(const QContactId &apiId)
 {
+#if QTPIM_VERSION < 59
     if (const QContactEngineId *eid = QContactManagerEngine::engineId(apiId)) {
         const ContactId *iid = static_cast<const ContactId*>(eid);
         return iid->m_databaseId;
     }
     return 0;
+#else
+    return dbIdFromLocalId(apiId.localId());
+#endif
 }
 
+
 const QContactId &ContactId::contactId(const QContactId &apiId)
 {
     return apiId;
@@ -88,9 +108,14 @@ const QContactId &ContactId::contactId(const QContactId &apiId)
 
 QContactId ContactId::fromString(const QString &s)
 {
+#if QTPIM_VERSION < 59
     return apiId(dbIdFromString(s));
+#else
+    return apiId(dbIdFromLocalId(s.toUtf8()));
+#endif
 }
 
+#if QTPIM_VERSION < 59
 ContactId::ContactId(quint32 dbId)
     : QContactEngineId()
     , m_databaseId(dbId)
@@ -122,6 +147,7 @@ QContactEngineId* ContactId::clone() const
 {
     return new ContactId(m_databaseId);
 }
+#endif
 
 QString ContactId::toString() const
 {
diff --git a/src/engine/contactid_p.h b/src/engine/contactid_p.h
index 99ca31f..3cd708f 100644
--- a/src/engine/contactid_p.h
+++ b/src/engine/contactid_p.h
@@ -36,17 +36,25 @@
 #include <QContactId>
 #include <QContact>
 
+#if QTPIM_VERSION < 59
 #include <QContactEngineId>
+#else
+#include <QSharedDataPointer>
+#endif
 
 QTCONTACTS_USE_NAMESPACE
 
+#if QTPIM_VERSION < 59
 class ContactId : public QContactEngineId
+#else
+class ContactId : public QSharedData
+#endif
 {
 public:
     static QContactId apiId(const QContact &contact);
-    static QContactId apiId(quint32 databaseId);
-
     static quint32 databaseId(const QContact &contact);
+
+    static QContactId apiId(quint32 databaseId);
     static quint32 databaseId(const QContactId &apiId);
 
     static const QContactId &contactId(const QContactId &apiId);
@@ -63,11 +71,13 @@ public:
     ContactId(quint32 databaseId);
     ContactId(const QString &s);
 
+    QString managerUri() const;
+#if QTPIM_VERSION < 59
     // implementing QContactEngineId:
     bool isEqualTo(const QContactEngineId *other) const;
     bool isLessThan(const QContactEngineId *other) const;
-    QString managerUri() const;
     QContactEngineId* clone() const;
+#endif
     QString toString() const;
 
 #ifndef QT_NO_DEBUG_STREAM
diff --git a/src/engine/contactreader.cpp b/src/engine/contactreader.cpp
index 4d47ef6..2f17bda 100644
--- a/src/engine/contactreader.cpp
+++ b/src/engine/contactreader.cpp
@@ -658,10 +658,15 @@ static QContactRelationship makeRelationship(const QString &type, quint32 firstI
 {
     QContactRelationship relationship;
     relationship.setRelationshipType(type);
-
+#if QTPIM_VERSION < 59
     QContact first, second;
     first.setId(ContactId::apiId(firstId));
     second.setId(ContactId::apiId(secondId));
+#else
+    QContactId first, second;
+    first = QContactId::fromString(QString::number(firstId));
+    second = QContactId::fromString(QString::number(secondId));
+#endif
     relationship.setFirst(first);
     relationship.setSecond(second);
 
@@ -1227,7 +1232,11 @@ static QString buildWhere(const QContactIdFilter &filter, ContactsDatabase &db,
 
 static QString buildWhere(const QContactRelationshipFilter &filter, QVariantList *bindings, bool *failed)
 {
+#if QTPIM_VERSION < 59
     QContactId rci = filter.relatedContact().id();
+#else
+    QContactId rci = filter.relatedContactId();
+#endif
 
     QContactRelationship::Role rcr = filter.relatedContactRole();
     QString rt = filter.relationshipType();
@@ -2175,7 +2184,17 @@ QContactManager::Error ContactReader::queryContacts(
         QContactGender gender;
         // Gender is an enum in qtpim
         QString genderText = contactQuery.value(col++).toString();
+#if QTPIM_VERSION < 59
         gender.setGender(static_cast<QContactGender::GenderField>(genderText.toInt()));
+#else
+        if(genderText == "Male") {
+            gender.setGender(QContactGender::GenderType::GenderMale);
+        } else if (genderText == "Female") {
+            gender.setGender(QContactGender::GenderType::GenderFemale);
+        } else {
+            gender.setGender(QContactGender::GenderType::GenderUnspecified);
+        }
+#endif
         contact.saveDetail(&gender);
 
         QContactFavorite favorite;
diff --git a/src/engine/contactsengine.cpp b/src/engine/contactsengine.cpp
index eaf7a1e..301ace8 100644
--- a/src/engine/contactsengine.cpp
+++ b/src/engine/contactsengine.cpp
@@ -466,8 +466,13 @@ public:
     RelationshipFetchJob(QContactRelationshipFetchRequest *request)
         : TemplateJob(request)
         , m_type(request->relationshipType())
+#if QTPIM_VERSION < 59
         , m_first(request->first().id())
         , m_second(request->second().id())
+#else
+        , m_first(request->first().managerUri(), request->first().localId())
+        , m_second(request->second().managerUri(), request->second().localId())
+#endif
     {
     }
 
@@ -1390,6 +1395,13 @@ static QList<QContactId> idList(const QVector<quint32> &contactIds)
     return ids;
 }
 
+static QList<QContactDetail::DetailType> detalId()
+{
+//TODO add list of changed types of contacts id. Now return all changes;
+    QList<QContactDetail::DetailType> dId;
+    return dId;
+}
+
 void ContactsEngine::_q_contactsAdded(const QVector<quint32> &contactIds)
 {
     emit contactsAdded(idList(contactIds));
@@ -1397,13 +1409,21 @@ void ContactsEngine::_q_contactsAdded(const QVector<quint32> &contactIds)
 
 void ContactsEngine::_q_contactsChanged(const QVector<quint32> &contactIds)
 {
+#if QTPIM_VERSION < 59
     emit contactsChanged(idList(contactIds));
+#else
+    emit contactsChanged(idList(contactIds), detalId());
+#endif
 }
 
 void ContactsEngine::_q_contactsPresenceChanged(const QVector<quint32> &contactIds)
 {
     if (m_mergePresenceChanges) {
+#if QTPIM_VERSION < 59
         emit contactsChanged(idList(contactIds));
+#else
+        emit contactsChanged(idList(contactIds), detalId());
+#endif
     } else {
         emit contactsPresenceChanged(idList(contactIds));
     }
diff --git a/src/engine/contactsengine.h b/src/engine/contactsengine.h
index b79dece..781df49 100644
--- a/src/engine/contactsengine.h
+++ b/src/engine/contactsengine.h
@@ -118,7 +118,12 @@ public:
             const QString &relationshipType,
             const QContact &participant,
             QContactRelationship::Role role,
-            QContactManager::Error *error) const override;
+            QContactManager::Error *error) const
+#if QTPIM_VERSION < 59
+    override
+#endif
+    ;
+
     bool saveRelationships(
             QList<QContactRelationship> *relationships,
             QMap<int, QContactManager::Error> *errorMap,
diff --git a/src/engine/contactsplugin.cpp b/src/engine/contactsplugin.cpp
index d5a1a5e..d662211 100644
--- a/src/engine/contactsplugin.cpp
+++ b/src/engine/contactsplugin.cpp
@@ -48,8 +48,10 @@ public:
     QContactManagerEngine *engine(
             const QMap<QString, QString> &parameters, QContactManager::Error* error);
     QString managerName() const;
+#if QTPIM_VERSION < 59
     QContactEngineId *createContactEngineId(
             const QMap<QString, QString> &parameters, const QString &engineIdString) const;
+#endif
 };
 
 
@@ -77,11 +79,13 @@ QString ContactsFactory::managerName() const
     return QString::fromLatin1("org.nemomobile.contacts.sqlite");
 }
 
+#if QTPIM_VERSION < 59
 QContactEngineId *ContactsFactory::createContactEngineId(
         const QMap<QString, QString> &parameters, const QString &engineIdString) const
 {
     Q_UNUSED(parameters)
     return new ContactId(engineIdString);
 }
+#endif
 
 #include "contactsplugin.moc"
diff --git a/src/engine/contactwriter.cpp b/src/engine/contactwriter.cpp
index 2835e6f..f2aaef9 100644
--- a/src/engine/contactwriter.cpp
+++ b/src/engine/contactwriter.cpp
@@ -413,9 +413,13 @@ QContactManager::Error ContactWriter::saveRelationships(
     for (int i = 0; i < relationships.size(); ++i) {
         const QContactRelationship &relationship = relationships.at(i);
 
+#if QTPIM_VERSION < 59
         QContactId first(relationship.first().id());
         QContactId second(relationship.second().id());
-
+#else
+        QContactId first = relationship.first();
+        QContactId second = relationship.second();
+#endif
         const quint32 firstId = ContactId::databaseId(first);
         const quint32 secondId = ContactId::databaseId(second);
         const QString &type = relationship.relationshipType();
@@ -2693,7 +2697,11 @@ static void promoteDetailsToLocal(const QList<QContactDetail> addDelta, const QL
                 localContact->saveDetail(&lts);
             } else if (type == detailType<QContactGender>()) {
                 QContactGender lg = localContact->detail<QContactGender>();
+#if QTPIM_VERSION < 59
                 lg.setGender(static_cast<QContactGender::GenderField>(original.value<int>(QContactGender::FieldGender)));
+#else
+                lg.setGender(lg.gender());
+#endif
                 localContact->saveDetail(&lg);
             } else if (type == detailType<QContactFavorite>()) {
                 QContactFavorite lf = localContact->detail<QContactFavorite>();
@@ -2735,11 +2743,16 @@ static QContactRelationship makeRelationship(const QString &type, const QContact
     QContactRelationship relationship;
     relationship.setRelationshipType(type);
 
+#if QTPIM_VERSION < 59
     QContact first, second;
     first.setId(firstId);
     second.setId(secondId);
     relationship.setFirst(first);
     relationship.setSecond(second);
+#else
+    relationship.setFirst(firstId);
+    relationship.setSecond(secondId);
+#endif
 
     return relationship;
 }
-- 
2.23.0

