From bdde4a61b2c2409ee35d77a802bd629906e21068 Mon Sep 17 00:00:00 2001 From: Alberto Mardegan Date: Thu, 22 Oct 2020 22:19:04 +0200 Subject: [PATCH 07/33] Restore qtcontacts-sqlite new backend This reverts commit fb071498 and rebases on top of latest contact removal changes. Signed-off-by: Mike Gabriel --- data/CMakeLists.txt | 4 ++ data/lomiri-addressbook-app.application | 14 ++++ src/app/addressbookapp.cpp | 2 +- src/imports/ABContactViewPage.qml | 15 +++- .../ContactEditor/ContactEditorPage.qml | 1 + .../ContactView/ContactDetailAvatarView.qml | 8 ++- .../ContactView/ContactViewPage.qml | 37 +++++----- src/imports/Lomiri/Contacts/ContactFetch.qml | 66 ++++++++++++++---- src/imports/Lomiri/Contacts/ContactList.js | 3 +- .../Lomiri/Contacts/ContactListModel.qml | 69 +++++++++++++++++-- .../Lomiri/Contacts/ContactListView.qml | 10 +-- src/imports/Lomiri/Contacts/ContactManager.js | 2 +- .../Lomiri/Contacts/ContactSimpleListView.qml | 10 ++- src/imports/Lomiri/Contacts/Contacts.js | 8 +++ .../lomiri_addressbook_app/tests/__init__.py | 2 +- 15 files changed, 191 insertions(+), 60 deletions(-) create mode 100644 data/lomiri-addressbook-app.application --- a/data/CMakeLists.txt +++ b/data/CMakeLists.txt @@ -31,3 +31,7 @@ install(FILES "lomiri-addressbook-app.url-dispatcher" DESTINATION ${CMAKE_INSTALL_DATADIR}/lomiri-url-dispatcher/urls ) + +install(FILES "lomiri-addressbook-app.application" + DESTINATION ${CMAKE_INSTALL_DATADIR}/accounts/applications +) --- /dev/null +++ b/data/lomiri-addressbook-app.application @@ -0,0 +1,14 @@ + + + Contacts + lomiri-addressbook-app.desktop + + + + Synchronize your contacts + + + Synchronize your contacts + + + --- a/src/app/addressbookapp.cpp +++ b/src/app/addressbookapp.cpp @@ -128,7 +128,7 @@ QString contactKey; QStringList arguments = this->arguments(); - QByteArray defaultManager("galera"); + QByteArray defaultManager("org.nemomobile.contacts.sqlite"); QByteArray testData; // use galare as default QtContacts Manager --- a/src/imports/ABContactViewPage.qml +++ b/src/imports/ABContactViewPage.qml @@ -46,8 +46,17 @@ function editContact(contact) { - root._editPage = pageStack.addComponentToCurrentColumnSync(root, Qt.resolvedUrl("ABContactEditorPage.qml"), - { model: root.model, contact: contact, backIconName: 'back'}) + root._editPage = pageStack.addComponentToCurrentColumnSync(root, Qt.resolvedUrl("ABContactEditorPage.qml"), { + model: root.model, + contact: contact, + backIconName: 'back', + }) + root._editPage.removalRequested.connect(function() { + root._editPage.close() + root._editPage = null + pageStack.removePages(root) + ContactsJS.removeContacts(root.model, [contact]) + }) root._editPage.Component.onDestruction(function() { root._editPage = null }) @@ -88,7 +97,7 @@ iconName: "edit" enabled: root.active && !root.editing shortcut: "Ctrl+e" - onTriggered: root.editContact(root.contact) + onTriggered: root.editContact(root.contactMainConstituent) } ] --- a/src/imports/Lomiri/AddressBook/ContactEditor/ContactEditorPage.qml +++ b/src/imports/Lomiri/AddressBook/ContactEditor/ContactEditorPage.qml @@ -42,6 +42,7 @@ readonly property alias editorFlickable: scrollArea signal contactSaved(var contact); + signal removalRequested() signal canceled() function close() { --- a/src/imports/Lomiri/AddressBook/ContactView/ContactDetailAvatarView.qml +++ b/src/imports/Lomiri/AddressBook/ContactView/ContactDetailAvatarView.qml @@ -26,6 +26,7 @@ id: root property alias editable: favImage.enabled + property var constituents: [] implicitHeight: units.gu(12) implicitWidth: parent.width @@ -106,8 +107,11 @@ } onClicked: { - root.contact.favorite.favorite = !root.contact.favorite.favorite - root.contact.save() + for (var i = 0; i < constituents.length; i++) { + var constituent = constituents[i] + constituent.favorite.favorite = !root.contact.favorite.favorite + constituent.save() + } } } } --- a/src/imports/Lomiri/AddressBook/ContactView/ContactViewPage.qml +++ b/src/imports/Lomiri/AddressBook/ContactView/ContactViewPage.qml @@ -25,6 +25,8 @@ id: root property QtObject contact: null + property QtObject contactMainConstituent: null + property var constituents: [] property string contactId property alias extensions: extensionsContents.children property alias model: contactFetch.model @@ -35,10 +37,11 @@ signal contactRemoved() signal actionTrigerred(string action, QtObject contact, QtObject detail) - function fetchContact(contactId) - { - if (contactId !== "") { - contactFetch.fetchContact(contactId) + function _ensureFullContact() { + if (contact) contactId = contact.contactId + else if (contactId == "") return + if (contact == null || constituents.length == 0) { + contactFetch.fetchContactFull(contactId) } } @@ -59,10 +62,12 @@ } } - // Pop page if the contact get removed onContactChanged: { if (!contact) { + // Pop page if the contact get removed root.contactRemoved() + } else { + _ensureFullContact() } } @@ -89,6 +94,8 @@ onContactNotFound: PopupUtils.open(Qt.resolvedUrl("ContactFetchError.qml"), root) onContactFetched: { root.contact = contact + root.contactMainConstituent = mainConstituent + root.constituents = constituents root.contactFetched(root.contact) } } @@ -123,6 +130,8 @@ anchors.left: parent.left height: implicitHeight width: implicitWidth + + constituents: root.constituents } ContactDetailPhoneNumbersView { @@ -239,19 +248,7 @@ } } - Component.onCompleted: { - if (contact == null) { - fetchContact(root.contactId) - } - } - onContactIdChanged: { - if (contact == null) { - fetchContact(root.contactId) - } - } - onModelChanged: { - if (contact == null) { - fetchContact(root.contactId) - } - } + Component.onCompleted: _ensureFullContact() + onContactIdChanged: _ensureFullContact() + onModelChanged: _ensureFullContact() } --- a/src/imports/Lomiri/Contacts/ContactFetch.qml +++ b/src/imports/Lomiri/Contacts/ContactFetch.qml @@ -26,18 +26,39 @@ property bool running: false property bool contactIsDirty: false - property string _pendingId: "" - property bool _ready: false + property string pendingId: "" + property bool withConstituents: false + property bool completed: false + property bool ready: completed && model !== null - signal contactFetched(QtObject contact) + signal contactFetched(QtObject contact, var constituents, + QtObject mainConstituent) signal contactRemoved() signal contactNotFound() + onReadyChanged: { + if (ready && (root.pendingId != "")) { + root._fetchContact(root.pendingId) + root.pendingId = "" + } + } + + function fetchContact(contactId) { - if (root._ready) { + root.withConstituents = false + if (root.ready) { + root._fetchContact(contactId) + } else { + root.pendingId = contactId + } + } + + function fetchContactFull(contactId) { + root.withConstituents = true + if (root.ready) { root._fetchContact(contactId) } else { - root._pendingId = contactId + root.pendingId = contactId } } @@ -46,9 +67,13 @@ console.warn("Fetch already running!") return } - - if (contact && !contactIsDirty && contact.contacId == contactId) { - contactFetched(contact) + if (contact && !contactIsDirty && contact.contactId == contactId) { + if (root.withConstituents) { + root._fetchConstituents(contact) + } else { + running = false + contactFetched(contact, [], null) + } } else if (model) { contactIsDirty = true running = true @@ -66,6 +91,19 @@ } } + function _fetchConstituents(contact) { + model.enumerateConstituents(contact, function(contacts) { + var mainConstituent = null + for (var i = 0; i < contacts.length; i++) { + /* TODO: add support for the case when there are multiple + * constituents */ + mainConstituent = contacts[i] + } + running = false + root.contactFetched(root.contact, contacts, mainConstituent) + }) + } + onContactChanged: { if (contact == null) { contactRemoved() @@ -88,7 +126,11 @@ currentQueryId = -1 if (fetchedContacts.length > 0) { contact = fetchedContacts[0] - root.contactFetched(fetchedContacts[0]) + if (root.withConstituents) { + root._fetchConstituents(contact) + } else { + root.contactFetched(fetchedContacts[0], [], null) + } } else { contactNotFound() } @@ -97,10 +139,6 @@ } Component.onCompleted: { - root._ready = true - if (root._pendingId != "") { - root._fetchContact(root._pendingId) - root._pendingId = "" - } + root.completed = true } } --- a/src/imports/Lomiri/Contacts/ContactList.js +++ b/src/imports/Lomiri/Contacts/ContactList.js @@ -20,7 +20,8 @@ function sectionValueForContact(contact) { if (contact) { - var section = contact.tag.tag.charAt(0).toUpperCase() + var text = ContactsJS.formatToDisplay(contact, "") + var section = text.charAt(0).toUpperCase() return (section === "" ? "#" : section) } else { return null --- a/src/imports/Lomiri/Contacts/ContactListModel.qml +++ b/src/imports/Lomiri/Contacts/ContactListModel.qml @@ -30,7 +30,8 @@ property bool _clearModel: false property list _extraFilters property QtObject _timeout - + property string _realManager: ContactManager.defaultManager + property var _defaultFilter: root.manager == root._realManager ? collectionFilter : null function changeFilter(newFilter) { @@ -47,7 +48,7 @@ } else if (contactsFilter.active) { return contactsFilter } else { - return null + return root._defaultFilter } } @@ -63,6 +64,10 @@ value: true matchFlags: DetailFilter.MatchExactly }, + CollectionFilter { + id: collectionFilter + ids: ["qtcontacts:org.nemomobile.contacts.sqlite::636f6c2d31"] // aggregate + }, UnionFilter { id: contactTermFilter @@ -77,9 +82,9 @@ filters: [ DetailFilter { id: nameFilter - detail: (root.manager === "galera" ? ContactDetail.ExtendedDetail : ContactDetail.DisplayLabel) - field: (root.manager === "galera" ? ExtendedDetail.Data : DisplayLabel.Label) - value: (root.manager === "galera" ? Contacts.normalized(contactTermFilter.value) : contactTermFilter.value) + detail: ContactDetail.DisplayLabel + field: DisplayLabel.Label + value: (root.manager === root._realManager ? Contacts.normalized(contactTermFilter.value) : contactTermFilter.value) matchFlags: DetailFilter.MatchContains } ] @@ -102,6 +107,9 @@ property bool active: { var filters_ = [] if (contactTermFilter.value.length > 0) { + if (root._defaultFilter) { + filters_.push(root._defaultFilter) + } filters_.push(contactTermFilter) } else if (root.onlyFavorites) { filters_.push(favouritesFilter) @@ -171,5 +179,56 @@ } } + + /* TODO: once https://bugreports.qt.io/browse/QTBUG-87807 is fixed, we can + * remove both these models and use the new relatedContacts() method. + */ + property var _relationshipModel: Component { + RelationshipModel { + property var callback: null + manager: root.manager + relationshipType: "Aggregates" + role: Relationship.First + onRelationshipsChanged: inspectModel() + function inspectModel() { + var ids = [] + for (var i = 0; i < relationships.length; i++) { + ids.push(relationships[i].second) + } + callback(ids) + } + } + } + + property var relationshipModel: null + function enumerateConstituentIds(contact, callback) { + var relationshipCb = function(contactIds) { + callback(contactIds) + relationshipModel.destroy() + } + relationshipModel = _relationshipModel.createObject(null, { + "callback": relationshipCb, + "participant": contact, + }) + } + + function enumerateConstituents(contact, callback) { + var requestId = 0 + var onConstituentIds = function(contactIds) { + requestId = root.fetchContacts(contactIds) + root.onContactsFetched.connect(function(reqId, contacts) { + if (reqId != requestId) return + callback(contacts) + }) + } + enumerateConstituentIds(contact, onConstituentIds) + } + + function removeAggregateContact(contact) { + console.log("Removing aggregate contact " + contact) + enumerateConstituentIds(contact, function(contactIds) { + removeContacts(contactIds) + }) + } } --- a/src/imports/Lomiri/Contacts/ContactListView.qml +++ b/src/imports/Lomiri/Contacts/ContactListView.qml @@ -133,7 +133,7 @@ \qmlproperty string manager This property holds the manager uri of the contact backend engine. - By default this is set to "galera" + By default this is set to "org.nemomobile.contacts.sqlite" */ property alias manager: view.manager /*! @@ -421,14 +421,6 @@ property alias favouritesIsSelected: contactsModel.onlyFavorites property bool contactsLoaded: false - function getSectionText(index) { - var tag = listModel.contacts[index].tag.tag - if (tag == "") - return "#" - else - return tag - } - // if the favorite header became invisible we should move back to all contacts. onShowFavouritesChanged: { if (!showFavourites && view.favouritesIsSelected) { --- a/src/imports/Lomiri/Contacts/ContactManager.js +++ b/src/imports/Lomiri/Contacts/ContactManager.js @@ -1,4 +1,4 @@ -var defaultManager = "galera" +var defaultManager = "org.nemomobile.contacts.sqlite" function manager() { --- a/src/imports/Lomiri/Contacts/ContactSimpleListView.qml +++ b/src/imports/Lomiri/Contacts/ContactSimpleListView.qml @@ -127,7 +127,7 @@ \qmlproperty string manager This property holds the manager uri of the contact backend engine. - By default this is set to "galera" + By default this is set to "org.nemomobile.contacts.sqlite" */ property string manager: ContactManager.manager() @@ -227,6 +227,10 @@ } } + function getSectionText(index) { + return Sections.sectionValueForContact(listModel.contacts[index]) + } + /*! private Fetch contact and emit contact clicked signal @@ -258,7 +262,7 @@ highlightFollowsCurrentItem: true section { - property: showSections ? "contact.tag.tag" : "" + property: showSections ? "contact.displayLabel.label" : "" criteria: ViewSection.FirstCharacter labelPositioning: ViewSection.InlineLabels delegate: SectionDelegate { @@ -267,7 +271,7 @@ right: parent.right margins: units.gu(2) } - text: section != "" ? section : "#" + text: section != "" ? section.toUpperCase() : "#" } } --- a/src/imports/Lomiri/Contacts/Contacts.js +++ b/src/imports/Lomiri/Contacts/Contacts.js @@ -159,3 +159,11 @@ var hash = Contacts.qHash(name) return contactColors[(hash % contactColors.length)] } + +function removeContacts(model, contacts) { + var ids = [] + for(var i=0, iMax=contacts.length; i < iMax; i++) { + ids.push(contacts[i].contactId) + } + model.removeContacts(ids) +} --- a/tests/autopilot/lomiri_addressbook_app/tests/__init__.py +++ b/tests/autopilot/lomiri_addressbook_app/tests/__init__.py @@ -61,7 +61,7 @@ if AddressBookAppTestCase.MEMORY_BACKEND: os.environ['QTCONTACTS_MANAGER_OVERRIDE'] = 'memory' else: - os.environ['QTCONTACTS_MANAGER_OVERRIDE'] = 'galera' + os.environ['QTCONTACTS_MANAGER_OVERRIDE'] = 'org.nemomobile.contacts.sqlite' os.environ['ADDRESS_BOOK_APP_ICON_THEME'] = 'ubuntu-mobile' vcard_data = "" if self.PRELOAD_VCARD: