File indexing completed on 2024-11-24 04:50:42

0001 /*
0002   This file is part of KAddressBook.
0003 
0004   SPDX-FileCopyrightText: 2009 Tobias Koenig <tokoe@kde.org>
0005 
0006   SPDX-License-Identifier: GPL-2.0-or-later
0007 */
0008 
0009 #include "globalcontactmodel.h"
0010 
0011 #include <Akonadi/ChangeRecorder>
0012 #include <Akonadi/ContactsTreeModel>
0013 #include <Akonadi/EntityDisplayAttribute>
0014 #include <Akonadi/ItemFetchScope>
0015 #include <Akonadi/Session>
0016 
0017 #include <KContacts/Addressee>
0018 #include <KContacts/ContactGroup>
0019 
0020 GlobalContactModel *GlobalContactModel::mInstance = nullptr;
0021 
0022 GlobalContactModel::GlobalContactModel()
0023     : mSession(new Akonadi::Session("KAddressBook::GlobalContactSession"))
0024     , mMonitor(new Akonadi::ChangeRecorder)
0025 {
0026     Akonadi::ItemFetchScope scope;
0027     scope.fetchFullPayload(true);
0028     scope.fetchAttribute<Akonadi::EntityDisplayAttribute>();
0029 
0030     mMonitor->setSession(mSession);
0031     mMonitor->fetchCollection(true);
0032     mMonitor->setItemFetchScope(scope);
0033     mMonitor->setCollectionMonitored(Akonadi::Collection::root());
0034     mMonitor->setMimeTypeMonitored(KContacts::Addressee::mimeType(), true);
0035     mMonitor->setMimeTypeMonitored(KContacts::ContactGroup::mimeType(), true);
0036 
0037     mModel = new Akonadi::ContactsTreeModel(mMonitor);
0038 }
0039 
0040 GlobalContactModel::~GlobalContactModel()
0041 {
0042     delete mModel;
0043     delete mMonitor;
0044     delete mSession;
0045 }
0046 
0047 GlobalContactModel *GlobalContactModel::instance()
0048 {
0049     if (!mInstance) {
0050         mInstance = new GlobalContactModel();
0051     }
0052     return mInstance;
0053 }
0054 
0055 Akonadi::ContactsTreeModel *GlobalContactModel::model() const
0056 {
0057     return mModel;
0058 }