Skip to content

Commit

Permalink
Merge branch 'jb52745' into 'master'
Browse files Browse the repository at this point in the history
[nemo-qml-plugin-contacts] Hide disable account collections from combo. Contributes to JB#52745

See merge request mer-core/nemo-qml-plugin-contacts!53
  • Loading branch information
chriadam committed Feb 2, 2021
2 parents dd8df04 + dae4127 commit 82f2913
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 4 deletions.
1 change: 1 addition & 0 deletions rpm/nemo-qml-plugin-contacts-qt5.spec
Expand Up @@ -19,6 +19,7 @@ BuildRequires: pkgconfig(qtcontacts-sqlite-qt5-extensions) >= 0.3.0
BuildRequires: pkgconfig(mlocale5)
BuildRequires: pkgconfig(mce)
BuildRequires: pkgconfig(mlite5)
BuildRequires: pkgconfig(accounts-qt5)
BuildRequires: libphonenumber-devel
BuildRequires: qt5-qttools-linguist
BuildRequires: qt5-qttools
Expand Down
52 changes: 49 additions & 3 deletions src/seasideaddressbookmodel.cpp
Expand Up @@ -33,15 +33,57 @@

#include <seasidecache.h>

#include <qtcontacts-extensions_manager_impl.h>
#include <QContactCollection>

#include <Accounts/Manager>
#include <Accounts/AccountService>
#include <Accounts/Account>
#include <Accounts/Service>

#include <QtDebug>
#include <QQmlInfo>

namespace {
bool accountIsEnabled(Accounts::Account *account)
{
Accounts::Service srv;
const Accounts::ServiceList &services = account->services();
for (const Accounts::Service &s : services) {
if (s.serviceType().toLower() == QStringLiteral("carddav")
|| s.name().toLower().contains(QStringLiteral("carddav"))
|| s.name().toLower().contains(QStringLiteral("contacts"))) {
srv = s;
break;
}
}

Accounts::AccountService globalSrv(account, Accounts::Service());
if (srv.isValid()) {
Accounts::AccountService accSrv(account, srv);
return globalSrv.isEnabled() && accSrv.isEnabled();
} else {
return globalSrv.isEnabled();
}
}

bool addressBookIsEnabled(const QContactCollection &col, Accounts::Manager *accountManager)
{
const Accounts::AccountId accountId = col.extendedMetaData(COLLECTION_EXTENDEDMETADATA_KEY_ACCOUNTID).toInt();
Accounts::Account *account = (accountId > 0) ? accountManager->account(accountId) : nullptr;
return !account || accountIsEnabled(account);
}
}

SeasideAddressBookModel::SeasideAddressBookModel(QObject *parent)
: QAbstractListModel(parent)
, m_accountManager(new Accounts::Manager(this))
{
const QList<QContactCollection> collections = SeasideCache::manager()->collections();
for (const QContactCollection &collection : collections) {
m_addressBooks.append(SeasideAddressBook::fromCollectionId(collection.id()));
if (addressBookIsEnabled(collection, m_accountManager)) {
m_addressBooks.append(SeasideAddressBook::fromCollectionId(collection.id()));
}
}

connect(SeasideCache::manager(), &QContactManager::collectionsAdded,
Expand Down Expand Up @@ -119,7 +161,9 @@ void SeasideAddressBookModel::collectionsAdded(const QList<QContactCollectionId>
QList<QContactCollectionId> collectionsMatchingFilter;
for (const QContactCollectionId &id : collectionIds) {
if (matchesFilter(id)) {
collectionsMatchingFilter.append(id);
if (addressBookIsEnabled(SeasideCache::manager()->collection(id), m_accountManager)) {
collectionsMatchingFilter.append(id);
}
}
}

Expand All @@ -132,7 +176,9 @@ void SeasideAddressBookModel::collectionsAdded(const QList<QContactCollectionId>
m_filteredAddressBooks.append(SeasideAddressBook::fromCollectionId(id));
}
for (const QContactCollectionId &id : collectionIds) {
m_addressBooks.append(SeasideAddressBook::fromCollectionId(id));
if (addressBookIsEnabled(SeasideCache::manager()->collection(id), m_accountManager)) {
m_addressBooks.append(SeasideAddressBook::fromCollectionId(id));
}
}
if (collectionsMatchingFilter.count() > 0) {
endInsertRows();
Expand Down
3 changes: 3 additions & 0 deletions src/seasideaddressbookmodel.h
Expand Up @@ -40,6 +40,8 @@
#include <QList>
#include <QQmlParserStatus>

#include <Accounts/Manager>

QTCONTACTS_USE_NAMESPACE

class SeasideAddressBookModel : public QAbstractListModel, public QQmlParserStatus
Expand Down Expand Up @@ -89,6 +91,7 @@ class SeasideAddressBookModel : public QAbstractListModel, public QQmlParserStat
QList<SeasideAddressBook> m_filteredAddressBooks;
QList<QContactCollectionId> m_allowedCollections;
QContactRelationshipFetchRequest *m_relationshipsFetch = nullptr;
Accounts::Manager *m_accountManager = nullptr;
int m_contactId = -1;
bool m_complete = false;
};
Expand Down
2 changes: 1 addition & 1 deletion src/src.pro
Expand Up @@ -9,7 +9,7 @@ CONFIG += qt plugin hide_symbols
QT = \
core \
qml
PKGCONFIG += mlocale5
PKGCONFIG += mlocale5 accounts-qt5

packagesExist(mlite5) {
PKGCONFIG += mlite5
Expand Down

0 comments on commit 82f2913

Please sign in to comment.