File indexing completed on 2025-01-19 04:51:59

0001 /*
0002     Copyright (c) 2016 Christian Mollekopf <mollekopf@kolabsys.com>
0003 
0004     This library is free software; you can redistribute it and/or modify it
0005     under the terms of the GNU Library General Public License as published by
0006     the Free Software Foundation; either version 2 of the License, or (at your
0007     option) any later version.
0008 
0009     This library is distributed in the hope that it will be useful, but WITHOUT
0010     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0011     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Library General Public
0012     License for more details.
0013 
0014     You should have received a copy of the GNU Library General Public License
0015     along with this library; see the file COPYING.LIB.  If not, write to the
0016     Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
0017     02110-1301, USA.
0018 */
0019 #include "identitiesmodel.h"
0020 #include <sink/store.h>
0021 #include <sink/log.h>
0022 
0023 using namespace Sink;
0024 
0025 IdentitiesModel::IdentitiesModel(QObject *parent) : QIdentityProxyModel(parent)
0026 {
0027     Sink::Query query;
0028     query.setFlags(Query::LiveQuery);
0029     query.request<ApplicationDomain::Identity::Name>()
0030         .request<ApplicationDomain::Identity::Address>()
0031         .request<ApplicationDomain::Identity::Account>();
0032     runQuery(query);
0033 }
0034 
0035 IdentitiesModel::~IdentitiesModel()
0036 {
0037 
0038 }
0039 
0040 QHash< int, QByteArray > IdentitiesModel::roleNames() const
0041 {
0042     QHash<int, QByteArray> roles;
0043 
0044     roles[Name] = "name";
0045     roles[Username] = "username";
0046     roles[Address] = "address";
0047     roles[IdentityId] = "identityId";
0048     roles[AccountId] = "accountId";
0049     roles[AccountName] = "accountName";
0050     roles[AccountIcon] = "accountIcon";
0051     roles[DisplayName] = "displayName";
0052 
0053     return roles;
0054 }
0055 
0056 QVariant IdentitiesModel::data(const QModelIndex &idx, int role) const
0057 {
0058     auto srcIdx = mapToSource(idx);
0059     auto identity = srcIdx.data(Sink::Store::DomainObjectRole).value<Sink::ApplicationDomain::Identity::Ptr>();
0060     switch (role) {
0061         case Name:
0062             return identity->getName();
0063         case Username:
0064             return identity->getName();
0065         case Address:
0066             return identity->getAddress();
0067         case IdentityId:
0068             return identity->identifier();
0069         case AccountId:
0070             return identity->getAccount();
0071         case AccountName: {
0072             const auto accountId = identity->getAccount();
0073             return mAccountNames.value(accountId);
0074         }
0075         case AccountIcon: {
0076             const auto accountId = identity->getAccount();
0077             return mAccountIcons.value(accountId);
0078         }
0079         case DisplayName: {
0080             return data(idx, AccountName).toString() + ": " + data(idx, Username).toString() + ", " + data(idx, Address).toString();
0081         }
0082     }
0083     return QIdentityProxyModel::data(idx, role);
0084 }
0085 
0086 void IdentitiesModel::runQuery(const Sink::Query &query)
0087 {
0088     using namespace Sink::ApplicationDomain;
0089     mModel = Sink::Store::loadModel<Identity>(query);
0090     setSourceModel(mModel.data());
0091 
0092     Sink::Store::fetchAll<SinkAccount>(Sink::Query{}.request<SinkAccount::Icon>().request<SinkAccount::Name>())
0093         .then([this](const QList<SinkAccount::Ptr> &accounts) {
0094             for (const auto &account : accounts) {
0095                 mAccountNames.insert(account->identifier(), account->getName());
0096                 mAccountIcons.insert(account->identifier(), account->getIcon());
0097             }
0098             emit layoutChanged();
0099         }).exec();
0100 }