26 #include <KLocalizedString>
27 #include <KPixmapSequence>
32 #include <TelepathyQt/Account>
33 #include <TelepathyQt/AccountSet>
35 class KTp::AccountsListModel::Private {
37 QList<Tp::AccountPtr> accounts;
38 Tp::AccountSetPtr accountSet;
59 d->accountSet = accountSet;
60 Q_FOREACH(
const Tp::AccountPtr &account, d->accountSet->accounts()) {
61 onAccountAdded(account);
63 connect(d->accountSet.data(), SIGNAL(accountAdded(Tp::AccountPtr)), SLOT(onAccountAdded(Tp::AccountPtr)));
64 connect(d->accountSet.data(), SIGNAL(accountRemoved(Tp::AccountPtr)), SLOT(onAccountRemoved(Tp::AccountPtr)));
71 if (parent == QModelIndex()) {
72 return d->accounts.size();
91 if (!index.isValid()) {
96 Tp::AccountPtr account = d->accounts.at(index.row());
100 data = QVariant(account->displayName());
103 case Qt::DecorationRole:
104 data = QVariant(KIcon(account->iconName()));
108 data = QVariant(account->connectionStatus());
112 data = QVariant(connectionStateString(account));
116 data = QVariant(connectionStateIcon(account));
120 data = QVariant(connectionStatusReason(account));
124 data = QVariant(account->protocolName());
128 if (account->isEnabled()) {
129 data = QVariant(Qt::Checked);
131 data = QVariant(Qt::Unchecked);
136 data = QVariant::fromValue<Tp::AccountPtr>(account);
148 if (!index.isValid()) {
153 index.data(
AccountRole).value<Tp::AccountPtr>()->setEnabled(value.toInt() == Qt::Checked);
162 if (row < 0 || column < 0 || parent != QModelIndex()) {
163 return QModelIndex();
166 if (row < rowCount() && column < columnCount()) {
167 return createIndex(row, column);
170 return QModelIndex();
173 void KTp::AccountsListModel::onAccountAdded(
const Tp::AccountPtr &account)
175 kDebug() <<
"Creating a new Account from account:" << account.data();
181 Q_FOREACH (
const Tp::AccountPtr &ai, d->accounts) {
190 kWarning() <<
"Requested to add account"
192 <<
"to model, but it is already present. Doing nothing.";
194 kDebug() <<
"Account not already in model. Create new Account from account:"
197 beginInsertRows(QModelIndex(), d->accounts.size(), d->accounts.size());
198 d->accounts.append(account);
201 connect(account.data(),
202 SIGNAL(stateChanged(
bool)),
203 SLOT(onAccountUpdated()));
204 connect(account.data(),
205 SIGNAL(displayNameChanged(QString)),
206 SLOT(onAccountUpdated()));
207 connect(account.data(),
208 SIGNAL(connectionStatusChanged(Tp::ConnectionStatus)),
209 SLOT(onAccountUpdated()));
210 connect(account.data(),
211 SIGNAL(currentPresenceChanged(Tp::Presence)),
212 SLOT(onAccountUpdated()));
213 connect(account.data(),
214 SIGNAL(iconNameChanged(QString)),
215 SLOT(onAccountUpdated()));
216 connect(account.data(),
217 SIGNAL(stateChanged(
bool)),
218 SLOT(onAccountUpdated()));
222 void KTp::AccountsListModel::onAccountRemoved(
const Tp::AccountPtr &account)
224 beginRemoveRows(QModelIndex(), d->accounts.indexOf(account), d->accounts.indexOf(account));
225 d->accounts.removeAll(account);
229 void KTp::AccountsListModel::onAccountUpdated()
231 Tp::AccountPtr item = Tp::AccountPtr(qobject_cast<Tp::Account*>(sender()));
235 kWarning() <<
"Not an Account pointer:" << sender();
239 QModelIndex index = createIndex(d->accounts.lastIndexOf(item), 0);
240 Q_EMIT dataChanged(index, index);
243 const QString KTp::AccountsListModel::connectionStateString(
const Tp::AccountPtr &account)
const
245 if (account->isEnabled()) {
246 switch (account->connectionStatus()) {
247 case Tp::ConnectionStatusConnected:
248 return KTp::Presence(account->currentPresence()).displayString();
249 case Tp::ConnectionStatusConnecting:
250 return i18nc(
"This is a connection state",
"Connecting");
251 case Tp::ConnectionStatusDisconnected:
252 return i18nc(
"This is a connection state",
"Disconnected");
254 return i18nc(
"This is an unknown connection state",
"Unknown");
257 return i18nc(
"This is a disabled account",
"Disabled");
261 const KIcon KTp::AccountsListModel::connectionStateIcon(
const Tp::AccountPtr &account)
const
263 if (account->isEnabled()) {
264 switch (account->connectionStatus()) {
265 case Tp::ConnectionStatusConnected:
267 case Tp::ConnectionStatusConnecting:
269 return KIcon(KPixmapSequence(QLatin1String(
"process-working"), 22).frameAt(0));
270 case Tp::ConnectionStatusDisconnected:
271 return KIcon(QLatin1String(
"user-offline"));
273 return KIcon(QLatin1String(
"user-offline"));
280 const QString KTp::AccountsListModel::connectionStatusReason(
const Tp::AccountPtr &account)
const
282 if (account->connectionStatusReason() == Tp::ConnectionStatusReasonRequested) {
289 #include "accounts-list-model.moc"