• Skip to content
  • Skip to link menu
  • KDE API Reference
  • KDE Home
  • Contact Us
 

Nepomuk-Core

  • KTp
  • Models
accounts-list-model.cpp
Go to the documentation of this file.
1 /*
2  * This file is part of ktp-common-internals
3  *
4  * Copyright (C) 2009 Collabora Ltd. <info@collabora.com>
5  * Copyright (C) 2012 David Edmundson <kde@davidedmundson.co.uk>
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 
22 #include "accounts-list-model.h"
23 
24 #include <KDebug>
25 #include <KIcon>
26 #include <KLocalizedString>
27 #include <KPixmapSequence>
28 
29 #include <KTp/error-dictionary.h>
30 #include <KTp/presence.h>
31 
32 #include <TelepathyQt/Account>
33 #include <TelepathyQt/AccountSet>
34 
35 class KTp::AccountsListModel::Private {
36 public:
37  QList<Tp::AccountPtr> accounts;
38  Tp::AccountSetPtr accountSet;
39 
40 };
41 
42 
43 KTp::AccountsListModel::AccountsListModel(QObject *parent)
44  : QAbstractListModel(parent),
45  d(new AccountsListModel::Private)
46 {
47 }
48 
49 KTp::AccountsListModel::~AccountsListModel()
50 {
51 }
52 
53 void KTp::AccountsListModel::setAccountSet(const Tp::AccountSetPtr &accountSet)
54 {
55  beginResetModel();
56  d->accounts.clear();
57  endResetModel();
58 
59  d->accountSet = accountSet;
60  Q_FOREACH(const Tp::AccountPtr &account, d->accountSet->accounts()) {
61  onAccountAdded(account);
62  }
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)));
65 
66 }
67 
68 int KTp::AccountsListModel::rowCount(const QModelIndex & parent) const
69 {
70  // If the index is the root item, then return the row count.
71  if (parent == QModelIndex()) {
72  return d->accounts.size();
73  }
74 
75  // Otherwise, return 0 (as this is a list model, so all items
76  // are children of the root item).
77  return 0;
78 }
79 
80 int KTp::AccountsListModel::columnCount(const QModelIndex& parent) const
81 {
82  Q_UNUSED(parent);
83 
84  // Column count is always 1
85  return 1;
86 }
87 
88 
89 QVariant KTp::AccountsListModel::data(const QModelIndex &index, int role) const
90 {
91  if (!index.isValid()) {
92  return QVariant();
93  }
94 
95  QVariant data;
96  Tp::AccountPtr account = d->accounts.at(index.row());
97 
98  switch (role) {
99  case Qt::DisplayRole:
100  data = QVariant(account->displayName());
101  break;
102 
103  case Qt::DecorationRole:
104  data = QVariant(KIcon(account->iconName()));
105  break;
106 
107  case AccountsListModel::ConnectionStateRole:
108  data = QVariant(account->connectionStatus());
109  break;
110 
111  case AccountsListModel::ConnectionStateDisplayRole:
112  data = QVariant(connectionStateString(account));
113  break;
114 
115  case AccountsListModel::ConnectionStateIconRole:
116  data = QVariant(connectionStateIcon(account));
117  break;
118 
119  case AccountsListModel::ConnectionErrorMessageDisplayRole:
120  data = QVariant(connectionStatusReason(account));
121  break;
122 
123  case AccountsListModel::ConnectionProtocolNameRole:
124  data = QVariant(account->protocolName());
125  break;
126 
127  case AccountsListModel::EnabledRole:
128  if (account->isEnabled()) {
129  data = QVariant(Qt::Checked);
130  } else {
131  data = QVariant(Qt::Unchecked);
132  }
133  break;
134 
135  case AccountsListModel::AccountRole:
136  data = QVariant::fromValue<Tp::AccountPtr>(account);
137  break;
138 
139  default:
140  break;
141  }
142 
143  return data;
144 }
145 
146 bool KTp::AccountsListModel::setData(const QModelIndex &index, const QVariant &value, int role)
147 {
148  if (!index.isValid()) {
149  return false;
150  }
151  if (role == AccountsListModel::EnabledRole) {
152  //this is index from QSortFilterProxyModel
153  index.data(AccountRole).value<Tp::AccountPtr>()->setEnabled(value.toInt() == Qt::Checked);
154  return true;
155  }
156 
157  return false;
158 }
159 
160 QModelIndex KTp::AccountsListModel::index(int row, int column, const QModelIndex& parent) const
161 {
162  if (row < 0 || column < 0 || parent != QModelIndex()) {
163  return QModelIndex();
164  }
165 
166  if (row < rowCount() && column < columnCount()) {
167  return createIndex(row, column);
168  }
169 
170  return QModelIndex();
171 }
172 
173 void KTp::AccountsListModel::onAccountAdded(const Tp::AccountPtr &account)
174 {
175  kDebug() << "Creating a new Account from account:" << account.data();
176 
177  // Check if the account is already in the model.
178  bool found = false;
179 
180  if (!found) {
181  Q_FOREACH (const Tp::AccountPtr &ai, d->accounts) {
182  if (ai == account) {
183  found = true;
184  break;
185  }
186  }
187  }
188 
189  if (found) {
190  kWarning() << "Requested to add account"
191  << account.data()
192  << "to model, but it is already present. Doing nothing.";
193  } else {
194  kDebug() << "Account not already in model. Create new Account from account:"
195  << account.data();
196 
197  beginInsertRows(QModelIndex(), d->accounts.size(), d->accounts.size());
198  d->accounts.append(account);
199  endInsertRows();
200 
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()));
219  }
220 }
221 
222 void KTp::AccountsListModel::onAccountRemoved(const Tp::AccountPtr &account)
223 {
224  beginRemoveRows(QModelIndex(), d->accounts.indexOf(account), d->accounts.indexOf(account));
225  d->accounts.removeAll(account);
226  endRemoveRows();
227 }
228 
229 void KTp::AccountsListModel::onAccountUpdated()
230 {
231  Tp::AccountPtr item = Tp::AccountPtr(qobject_cast<Tp::Account*>(sender()));
232 
233  Q_ASSERT(item);
234  if (!item) {
235  kWarning() << "Not an Account pointer:" << sender();
236  return;
237  }
238 
239  QModelIndex index = createIndex(d->accounts.lastIndexOf(item), 0);
240  Q_EMIT dataChanged(index, index);
241 }
242 
243 const QString KTp::AccountsListModel::connectionStateString(const Tp::AccountPtr &account) const
244 {
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");
253  default:
254  return i18nc("This is an unknown connection state", "Unknown");
255  }
256  } else {
257  return i18nc("This is a disabled account", "Disabled");
258  }
259 }
260 
261 const KIcon KTp::AccountsListModel::connectionStateIcon(const Tp::AccountPtr &account) const
262 {
263  if (account->isEnabled()) {
264  switch (account->connectionStatus()) {
265  case Tp::ConnectionStatusConnected:
266  return KTp::Presence(account->currentPresence()).icon();
267  case Tp::ConnectionStatusConnecting:
268  //imho this is not really worth animating, but feel free to play around..
269  return KIcon(KPixmapSequence(QLatin1String("process-working"), 22).frameAt(0));
270  case Tp::ConnectionStatusDisconnected:
271  return KIcon(QLatin1String("user-offline"));
272  default:
273  return KIcon(QLatin1String("user-offline"));
274  }
275  } else {
276  return KIcon();
277  }
278 }
279 
280 const QString KTp::AccountsListModel::connectionStatusReason(const Tp::AccountPtr &account) const
281 {
282  if (account->connectionStatusReason() == Tp::ConnectionStatusReasonRequested) {
283  return QString();
284  } else {
285  return KTp::ErrorDictionary::displayShortErrorMessage(account->connectionError());
286  }
287 }
288 
289 #include "accounts-list-model.moc"
This file is part of the KDE documentation.
Documentation copyright © 1996-2013 The KDE developers.
Generated on Fri Mar 22 2013 10:58:52 by doxygen 1.8.1.2 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

ktp-common-internals API Reference

Skip menu "ktp-common-internals API Reference"
Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal