File indexing completed on 2023-11-26 08:16:43
0001 /* 0002 * Copyright (C) 2012 David Edmundson <kde@davidedmundson.co.uk> 0003 * 0004 * This library is free software; you can redistribute it and/or 0005 * modify it under the terms of the GNU Lesser General Public 0006 * License as published by the Free Software Foundation; either 0007 * version 2.1 of the License, or (at your option) any later version. 0008 * 0009 * This library is distributed in the hope that it will be useful, 0010 * but WITHOUT ANY WARRANTY; without even the implied warranty of 0011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 0012 * Lesser General Public License for more details. 0013 * 0014 * You should have received a copy of the GNU Lesser General Public 0015 * License along with this library; if not, write to the Free Software 0016 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 0017 */ 0018 0019 #include "accounts-tree-proxy-model.h" 0020 0021 #include "types.h" 0022 0023 #include <TelepathyQt/Account> 0024 #include <TelepathyQt/AccountManager> 0025 #include <TelepathyQt/AccountSet> 0026 0027 class KTp::AccountsTreeProxyModel::Private 0028 { 0029 public: 0030 Tp::AccountManagerPtr accountManager; 0031 Tp::AccountSetPtr accountSet; 0032 }; 0033 0034 KTp::AccountsTreeProxyModel::AccountsTreeProxyModel(QAbstractItemModel *sourceModel, const Tp::AccountManagerPtr &accountManager) : 0035 AbstractGroupingProxyModel(sourceModel), 0036 d(new Private()) 0037 { 0038 d->accountManager = accountManager; 0039 0040 d->accountSet = accountManager->enabledAccounts(); 0041 connect(d->accountSet.data(), SIGNAL(accountAdded(Tp::AccountPtr)), SLOT(onAccountAdded(Tp::AccountPtr))); 0042 connect(d->accountSet.data(), SIGNAL(accountRemoved(Tp::AccountPtr)), SLOT(onAccountRemoved(Tp::AccountPtr))); 0043 Q_FOREACH(const Tp::AccountPtr &account, d->accountSet->accounts()) { 0044 onAccountAdded(account); 0045 } 0046 } 0047 0048 KTp::AccountsTreeProxyModel::~AccountsTreeProxyModel() 0049 { 0050 delete d; 0051 } 0052 0053 QSet<QString> KTp::AccountsTreeProxyModel::groupsForIndex(const QModelIndex &sourceIndex) const 0054 { 0055 const Tp::AccountPtr account = sourceIndex.data(KTp::AccountRole).value<Tp::AccountPtr>(); 0056 if (account) { 0057 return QSet<QString>() << account->objectPath(); 0058 } else { 0059 return QSet<QString>() << QLatin1String("Unknown"); 0060 } 0061 } 0062 0063 0064 QVariant KTp::AccountsTreeProxyModel::dataForGroup(const QString &group, int role) const 0065 { 0066 Tp::AccountPtr account; 0067 switch(role) { 0068 case Qt::DisplayRole: 0069 account = d->accountManager->accountForObjectPath(group); 0070 if (account) { 0071 return account->displayName(); 0072 } 0073 break; 0074 case Qt::DecorationRole: 0075 account = d->accountManager->accountForObjectPath(group); 0076 if (account) { 0077 return QIcon::fromTheme(account->iconName()); 0078 } 0079 break; 0080 case KTp::AccountRole: 0081 return QVariant::fromValue(d->accountManager->accountForObjectPath(group)); 0082 case KTp::RowTypeRole: 0083 return KTp::AccountRowType; 0084 case KTp::IdRole: 0085 account = d->accountManager->accountForObjectPath(group); 0086 if (account) { 0087 return account->uniqueIdentifier(); 0088 } 0089 break; 0090 } 0091 0092 return QVariant(); 0093 } 0094 0095 void KTp::AccountsTreeProxyModel::onAccountChanged() 0096 { 0097 Tp::AccountPtr account(qobject_cast<Tp::Account*>(sender())); 0098 groupChanged(account->objectPath()); 0099 } 0100 0101 void KTp::AccountsTreeProxyModel::onAccountAdded(const Tp::AccountPtr &account) 0102 { 0103 if (account->isValidAccount()) { 0104 forceGroup(account->objectPath()); 0105 connect(account.data(), SIGNAL(normalizedNameChanged(QString)), SLOT(onAccountChanged())); 0106 connect(account.data(), SIGNAL(iconNameChanged(QString)), SLOT(onAccountChanged())); 0107 } 0108 } 0109 0110 void KTp::AccountsTreeProxyModel::onAccountRemoved(const Tp::AccountPtr &account) 0111 { 0112 unforceGroup(account->objectPath()); 0113 }