Author: Lionel Duboeuf <lduboeuf@ouvaton.org>
Description: add gsettings for qtcontacts-sqlite

--- qtcontacts-sqlite.orig/config.pri
+++ qtcontacts-sqlite/config.pri
@@ -11,4 +11,9 @@ packagesExist(mlite5) {
     PKGCONFIG += mlite5
 }
 
+packagesExist(gsettings-qt) {
+    PKGCONFIG += gsettings-qt
+    DEFINES += HAS_GSETTINGS
+} 
+
 DEFINES += CONTACTS_DATABASE_PATH=\"\\\"$$[QT_INSTALL_LIBS]/qtcontacts-sqlite-qt5/\\\"\"
--- qtcontacts-sqlite.orig/qtcontacts-sqlite.pro
+++ qtcontacts-sqlite/qtcontacts-sqlite.pro
@@ -4,4 +4,11 @@ SUBDIRS = \
         tests
 OTHER_FILES += rpm/qtcontacts-sqlite-qt5.spec
 
+packagesExist(gsettings-qt) {
+    schemas.path = $${PREFIX}/share/glib-2.0/schemas
+    schemas.files = schemas/org.nemomobile.contacts.gschema.xml
+    
+    INSTALLS += schemas
+}
+
 tests.depends = src
--- /dev/null
+++ qtcontacts-sqlite/schemas/org.nemomobile.contacts.gschema.xml
@@ -0,0 +1,20 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<schemalist>
+  <schema id="org.nemomobile.contacts" path="/org/nemomobile/contacts/">
+    <key name="group-property" type="s">
+      <summary>Contact group property</summary>
+      <description>Allow to group contact by property.</description>
+      <default>"firstName"</default>
+    </key>
+    <key name="display-label-order" type="i">
+      <summary>Contact displayLabel policy</summary>
+      <description>Allow to change display label structure, 0=FirstName First, 1=LastName First.</description>
+      <default>0</default>
+    </key>
+    <key name="sort-property" type="s">
+      <summary>Contact property sort</summary>
+      <description>Allow sorting contact by a property.</description>
+      <default>"firstName"</default>
+    </key>
+  </schema>
+</schemalist>
--- qtcontacts-sqlite.orig/src/engine/contactsdatabase.cpp
+++ qtcontacts-sqlite/src/engine/contactsdatabase.cpp
@@ -2311,7 +2311,7 @@ static bool executeDisplayLabelGroupLoca
         }
     }
 
-#ifndef HAS_MLITE
+#if !defined(HAS_MLITE) && !defined(HAS_GSETTINGS)
     bool sameGroupProperty = true;
 #else
     // also determine if the current system setting for deriving the group from the first vs last
@@ -3268,6 +3268,9 @@ ContactsDatabase::ContactsDatabase(Conta
 #ifdef HAS_MLITE
     , m_groupPropertyConf(QStringLiteral("/org/nemomobile/contacts/group_property"))
 #endif // HAS_MLITE
+#ifdef HAS_GSETTINGS
+    , m_groupPropertyConf(new QGSettings("org.nemomobile.contacts", "/org/nemomobile/contacts/"))
+#endif // HAS_GSETTINGS
 {
 #ifdef HAS_MLITE
     QObject::connect(&m_groupPropertyConf, &MGConfItem::valueChanged, [this, engine] {
@@ -3279,6 +3282,19 @@ ContactsDatabase::ContactsDatabase(Conta
         QMetaObject::invokeMethod(engine, "dataChanged", Qt::QueuedConnection);
     });
 #endif // HAS_MLITE
+#ifdef HAS_GSETTINGS
+    QObject::connect(m_groupPropertyConf, &QGSettings::changed, [this, engine](const QString &key) {
+        if (key == QLatin1String("group-property")) {
+            qDebug() << "group-property changed, regenerate display Label group";
+            this->regenerateDisplayLabelGroups();
+            // expensive, but if we don't do it, in multi-process case some clients may not get updated...
+            // if contacts backend were daemonised, this problem would go away...
+            // Emit some engine signals asynchronously.
+            QMetaObject::invokeMethod(engine, "_q_displayLabelGroupsChanged", Qt::QueuedConnection);
+            QMetaObject::invokeMethod(engine, "dataChanged", Qt::QueuedConnection);
+        }
+    });
+#endif
 }
 
 ContactsDatabase::~ContactsDatabase()
@@ -3480,6 +3496,8 @@ bool ContactsDatabase::open(const QStrin
         }
     }
 
+
+
     // Attach to the transient store - any process can create it, but only the primary connection of each
     if (!m_transientStore.open(nonprivileged, !secondaryConnection, !databasePreexisting)) {
         QTCONTACTS_SQLITE_WARNING(QString::fromLatin1("Failed to open contacts transient store"));
@@ -3894,8 +3912,16 @@ void ContactsDatabase::regenerateDisplay
 QString ContactsDatabase::displayLabelGroupPreferredProperty() const
 {
     QString retn(QStringLiteral("QContactName::FieldFirstName"));
+
+
 #ifdef HAS_MLITE
     const QVariant groupPropertyConf = m_groupPropertyConf.value();
+#endif
+#ifdef HAS_GSETTINGS
+    const QVariant groupPropertyConf = m_groupPropertyConf->get(QStringLiteral("group-property"));
+#endif
+
+#if defined(HAS_MLITE) || defined(HAS_GSETTINGS)
     if (groupPropertyConf.isValid()) {
         const QString gpcString = groupPropertyConf.toString();
         if (gpcString.compare(QStringLiteral("FirstName"), Qt::CaseInsensitive) == 0) {
--- qtcontacts-sqlite.orig/src/engine/contactsdatabase.h
+++ qtcontacts-sqlite/src/engine/contactsdatabase.h
@@ -40,6 +40,9 @@
 #ifdef HAS_MLITE
 #include <mgconfitem.h>
 #endif
+#ifdef HAS_GSETTINGS
+#include <QGSettings>
+#endif
 
 #include <QHash>
 #include <QMutex>
@@ -208,6 +211,9 @@ private:
 #ifdef HAS_MLITE
     MGConfItem m_groupPropertyConf;
 #endif // HAS_MLITE
+#ifdef HAS_GSETTINGS
+    QGSettings *m_groupPropertyConf;
+#endif // HAS_GSETTINGS
 };
 
 #endif
