File indexing completed on 2025-01-05 04:54:55
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 "accountsmodel.h" 0020 #include <sink/store.h> 0021 0022 using namespace Sink; 0023 using namespace Sink::ApplicationDomain; 0024 0025 AccountsModel::AccountsModel(QObject *parent) : QIdentityProxyModel(parent) 0026 { 0027 Sink::Query query; 0028 query.setFlags(Query::LiveQuery); 0029 query.request<SinkAccount::Name>(); 0030 query.request<SinkAccount::Icon>(); 0031 query.request<SinkAccount::Status>(); 0032 runQuery(query); 0033 } 0034 0035 AccountsModel::~AccountsModel() 0036 { 0037 0038 } 0039 0040 QHash< int, QByteArray > AccountsModel::roleNames() const 0041 { 0042 return { 0043 {Name, "name"}, 0044 {Icon, "icon"}, 0045 {AccountId, "accountId"}, 0046 {Status, "status"} 0047 }; 0048 } 0049 0050 QVariant AccountsModel::data(const QModelIndex &idx, int role) const 0051 { 0052 auto srcIdx = mapToSource(idx); 0053 auto account = srcIdx.data(Sink::Store::DomainObjectRole).value<SinkAccount::Ptr>(); 0054 switch (role) { 0055 case Name: 0056 return account->getName(); 0057 case Icon: 0058 return account->getIcon(); 0059 case AccountId: 0060 return account->identifier(); 0061 case Status: 0062 switch (account->getStatus()) { 0063 case Sink::ApplicationDomain::ErrorStatus: 0064 return ErrorStatus; 0065 case Sink::ApplicationDomain::BusyStatus: 0066 return BusyStatus; 0067 case Sink::ApplicationDomain::ConnectedStatus: 0068 return ConnectedStatus; 0069 case Sink::ApplicationDomain::OfflineStatus: 0070 return OfflineStatus; 0071 } 0072 return NoStatus; 0073 } 0074 return QIdentityProxyModel::data(idx, role); 0075 } 0076 0077 void AccountsModel::runQuery(const Sink::Query &query) 0078 { 0079 mModel = Sink::Store::loadModel<SinkAccount>(query); 0080 setSourceModel(mModel.data()); 0081 } 0082 0083 void AccountsModel::setAccountId(const QByteArray &accountId) 0084 { 0085 if (accountId.isEmpty()) { 0086 setSourceModel(nullptr); 0087 mModel.clear(); 0088 return; 0089 } 0090 Sink::Query query; 0091 query.filter(accountId); 0092 query.setFlags(Query::LiveQuery); 0093 query.request<SinkAccount::Name>(); 0094 query.request<SinkAccount::Icon>(); 0095 query.request<SinkAccount::Status>(); 0096 runQuery(query); 0097 } 0098 0099 QByteArray AccountsModel::accountId() const 0100 { 0101 return {}; 0102 } 0103 0104 void AccountsModel::setResourceId(const QByteArray &resourceId) 0105 { 0106 if (resourceId.isEmpty()) { 0107 setSourceModel(nullptr); 0108 mModel.clear(); 0109 return; 0110 } 0111 0112 Sink::Store::fetchOne<SinkResource>(Sink::Query{}.filter(resourceId)).guard(this).then([this] (const Sink::ApplicationDomain::SinkResource &resource) { 0113 Sink::Query query; 0114 query.filter(resource.getAccount()); 0115 query.request<SinkAccount::Name>(); 0116 query.request<SinkAccount::Icon>(); 0117 query.request<SinkAccount::Status>(); 0118 runQuery(query); 0119 }).exec(); 0120 } 0121 0122 QByteArray AccountsModel::resourceId() const 0123 { 0124 return {}; 0125 }