From 3b387afa01a7f85136766bd83c7a924b1c0e40e6 Mon Sep 17 00:00:00 2001 From: Sebastian Wolf Date: Tue, 31 Aug 2021 12:15:05 +0200 Subject: [PATCH] Synchronize contacts with Nemomobile QML API --- harbour-fernschreiber.pro | 5 ++- privileges/harbour-fernschreiber.privileges | 1 + qml/pages/NewChatPage.qml | 37 ++++++++++++++++----- rpm/harbour-fernschreiber.spec | 2 ++ rpm/harbour-fernschreiber.yaml | 2 ++ src/contactsmodel.cpp | 31 ++++++++++++++++- src/contactsmodel.h | 4 +++ 7 files changed, 72 insertions(+), 10 deletions(-) create mode 100644 privileges/harbour-fernschreiber.privileges diff --git a/harbour-fernschreiber.pro b/harbour-fernschreiber.pro index bd59210..8af8d3d 100644 --- a/harbour-fernschreiber.pro +++ b/harbour-fernschreiber.pro @@ -201,8 +201,11 @@ fernschreiber.desktop.files = harbour-fernschreiber.desktop database.files = db database.path = /usr/share/$${TARGET} +mapplauncher.path = /usr/share/mapplauncherd/privileges.d +mapplauncher.files = privileges/harbour-fernschreiber.privileges + INSTALLS += telegram 86.png 108.png 128.png 172.png 256.png \ - fernschreiber.desktop gui images database + fernschreiber.desktop gui images database mapplauncher HEADERS += \ src/appsettings.h \ diff --git a/privileges/harbour-fernschreiber.privileges b/privileges/harbour-fernschreiber.privileges new file mode 100644 index 0000000..9865b7c --- /dev/null +++ b/privileges/harbour-fernschreiber.privileges @@ -0,0 +1 @@ +/usr/bin/harbour-fernschreiber,p diff --git a/qml/pages/NewChatPage.qml b/qml/pages/NewChatPage.qml index b9e5aba..54e3f19 100644 --- a/qml/pages/NewChatPage.qml +++ b/qml/pages/NewChatPage.qml @@ -18,6 +18,7 @@ */ import QtQuick 2.6 import Sailfish.Silica 1.0 +import org.nemomobile.contacts 1.0 import "../components" import "../js/twemoji.js" as Emoji import "../js/functions.js" as Functions @@ -29,6 +30,7 @@ Page { property bool isLoading: true; property bool syncSupported: false; + property bool nemoSync: false; function resetFocus() { contactsSearchField.focus = false; @@ -56,31 +58,50 @@ Page { } Component.onCompleted: { - // With Sailfish OS 4 we can't get up-to-date contacts andmore. We might need to enter Sailjail eventually... - // Details see https://forum.sailfishos.org/t/4-0-1-45-non-jailed-contacts-sqlite-database-no-longer-updated/4724 var sailfishOSVersion = fernschreiberUtils.getSailfishOSVersion().split("."); if (parseInt(sailfishOSVersion[0]) < 4) { Debug.log("Sailfish OS version 3.x - contact sync should still be possible...") newChatPage.syncSupported = true; } else { - Debug.log("Sailfish OS version 4.x - contact sync no longer supported...") - newChatPage.syncSupported = false; + // With Sailfish OS 4 we can't get up-to-date contacts with the standard database anymore. We might need to enter Sailjail eventually... + // Details see https://forum.sailfishos.org/t/4-0-1-45-non-jailed-contacts-sqlite-database-no-longer-updated/4724 + // We try it with the inofficial Nemo Contact API... + newChatPage.nemoSync = true; } } + PeopleModel { + id: peopleModel + filterType: PeopleModel.FilterAll + requiredProperty: PeopleModel.PhoneNumberRequired + } + SilicaFlickable { id: newChatContainer contentHeight: newChatPage.height anchors.fill: parent PullDownMenu { - visible: contactsModel.canSynchronizeContacts() && newChatPage.syncSupported MenuItem { onClicked: { newChatPage.isLoading = true; - if (!contactsModel.synchronizeContacts()) { - reloadContacts(); - appNotification.show(qsTr("Could not synchronize your contacts with Telegram.")); + if (newChatPage.nemoSync) { + if (peopleModel.count === 0) { + appNotification.show(qsTr("Could not synchronize your contacts with Telegram.")); + newChatPage.isLoading = false; + } else { + contactsModel.startImportingContacts(); + for (var i = 0; i < peopleModel.count; i++ ) { + contactsModel.importContact(peopleModel.get(i)); + } + contactsModel.stopImportingContacts(); + } + } + if (newChatPage.syncSupported) { + if (!contactsModel.synchronizeContacts()) { + reloadContacts(); + appNotification.show(qsTr("Could not synchronize your contacts with Telegram.")); + } } // Success message is not fired before TDLib returned "Contacts imported" (see above) } diff --git a/rpm/harbour-fernschreiber.spec b/rpm/harbour-fernschreiber.spec index 9abf97f..aa51eba 100644 --- a/rpm/harbour-fernschreiber.spec +++ b/rpm/harbour-fernschreiber.spec @@ -19,6 +19,7 @@ URL: http://werkwolf.eu/ Source0: %{name}-%{version}.tar.bz2 Source100: harbour-fernschreiber.yaml Requires: sailfishsilica-qt5 >= 0.10.9 +Requires: nemo-qml-plugin-contacts-qt5 BuildRequires: pkgconfig(sailfishapp) >= 1.0.2 BuildRequires: pkgconfig(Qt5Core) BuildRequires: pkgconfig(Qt5Qml) @@ -70,5 +71,6 @@ desktop-file-install --delete-original \ %{_datadir}/%{name} %{_datadir}/applications/%{name}.desktop %{_datadir}/icons/hicolor/*/apps/%{name}.png +%{_datadir}/mapplauncherd/privileges.d/harbour-fernschreiber.privileges # >> files # << files diff --git a/rpm/harbour-fernschreiber.yaml b/rpm/harbour-fernschreiber.yaml index 3c853ab..dcaddab 100644 --- a/rpm/harbour-fernschreiber.yaml +++ b/rpm/harbour-fernschreiber.yaml @@ -36,6 +36,7 @@ PkgConfigBR: # Runtime dependencies which are not automatically detected Requires: - sailfishsilica-qt5 >= 0.10.9 + - nemo-qml-plugin-contacts-qt5 # All installed files Files: @@ -43,6 +44,7 @@ Files: - '%{_datadir}/%{name}' - '%{_datadir}/applications/%{name}.desktop' - '%{_datadir}/icons/hicolor/*/apps/%{name}.png' + - '%{_datadir}/mapplauncherd/privileges.d/harbour-fernschreiber.privileges' # For more information about yaml and what's supported in Sailfish OS # build system, please see https://wiki.merproject.org/wiki/Spectacle diff --git a/src/contactsmodel.cpp b/src/contactsmodel.cpp index 59184f5..bc1c4ec 100644 --- a/src/contactsmodel.cpp +++ b/src/contactsmodel.cpp @@ -153,7 +153,7 @@ void ContactsModel::hydrateContacts() bool ContactsModel::synchronizeContacts() { LOG("Synchronizing device contacts"); - QVariantList deviceContacts; + deviceContacts.clear(); QSqlQuery databaseQuery(this->deviceContactsDatabase); databaseQuery.prepare("select distinct c.contactId, c.firstName, c.lastName, n.phoneNumber from Contacts as c inner join PhoneNumbers as n on c.contactId = n.contactId where n.phoneNumber is not null and ( c.firstName is not null or c.lastName is not null );"); if (databaseQuery.exec()) { @@ -182,3 +182,32 @@ bool ContactsModel::canSynchronizeContacts() { return this->canUseDeviceContacts; } + +void ContactsModel::startImportingContacts() +{ + this->deviceContacts.clear(); +} + +void ContactsModel::stopImportingContacts() +{ + if (!deviceContacts.isEmpty()) { + LOG("Importing found contacts" << deviceContacts.size()); + this->tdLibWrapper->importContacts(deviceContacts); + } +} + +void ContactsModel::importContact(const QVariantMap &singlePerson) +{ + QString firstName = singlePerson.value("firstName").toString(); + QVariantList phoneNumbers = singlePerson.value("phoneNumbers").toList(); + if (!firstName.isEmpty() && !phoneNumbers.isEmpty()) { + for (QVariant phoneNumber : phoneNumbers) { + QVariantMap singleContact; + singleContact.insert("first_name", firstName); + singleContact.insert("last_name", singlePerson.value("lastName").toString()); + singleContact.insert("phone_number", phoneNumber.toString()); + deviceContacts.append(singleContact); + LOG("Found contact" << singleContact.value("first_name").toString() << singleContact.value("last_name").toString() << singleContact.value("phone_number").toString()); + } + } +} diff --git a/src/contactsmodel.h b/src/contactsmodel.h index d4af011..3c1c5ee 100644 --- a/src/contactsmodel.h +++ b/src/contactsmodel.h @@ -52,6 +52,9 @@ public: Q_INVOKABLE void hydrateContacts(); Q_INVOKABLE bool synchronizeContacts(); Q_INVOKABLE bool canSynchronizeContacts(); + Q_INVOKABLE void startImportingContacts(); + Q_INVOKABLE void stopImportingContacts(); + Q_INVOKABLE void importContact(const QVariantMap &singlePerson); public slots: void handleUsersReceived(const QString &extra, const QVariantList &userIds, int totalUsers); @@ -63,6 +66,7 @@ private: QString filter; QSqlDatabase deviceContactsDatabase; bool canUseDeviceContacts; + QVariantList deviceContacts; }; #endif // CONTACTSMODEL_H