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

Nepomuk-Core

  • KTp
  • Models
contacts-list-model.cpp
Go to the documentation of this file.
1 /*
2 * Copyright (C) 2012 David Edmundson <kde@davidedmundson.co.uk>
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18 
19 #include "contacts-list-model.h"
20 
21 #include <TelepathyQt/Account>
22 #include <TelepathyQt/Contact>
23 #include <TelepathyQt/ContactCapabilities>
24 #include <TelepathyQt/Connection>
25 #include <TelepathyQt/ContactManager>
26 
27 #include "contact.h"
28 #include "presence.h"
29 #include "types.h"
30 
31 class KTp::ContactsListModel::Private
32 {
33 public:
34  QList<Tp::ContactPtr> contacts;
35  KTp::GlobalContactManager *contactManager;
36 };
37 
38 
39 KTp::ContactsListModel::ContactsListModel(QObject *parent) :
40  QAbstractListModel(parent),
41  d(new KTp::ContactsListModel::Private())
42 {
43  d->contactManager = 0;
44 
45  QHash<int, QByteArray> roles = roleNames();
46  roles[KTp::RowTypeRole]= "type";
47  roles[KTp::IdRole]= "id";
48 
49  roles[KTp::ContactRole]= "contact";
50  roles[KTp::AccountRole]= "account";
51 
52  roles[KTp::ContactClientTypesRole]= "clientTypes";
53  roles[KTp::ContactAvatarPathRole]= "avatar";
54  roles[KTp::ContactAvatarPixmapRole]="avatarPixmap";
55  roles[KTp::ContactGroupsRole]= "groups";
56  roles[KTp::ContactPresenceMessageRole]= "presenceMessage";
57  roles[KTp::ContactPresenceTypeRole]= "presenceType";
58  roles[KTp::ContactPresenceIconRole]= "presenceIcon";
59  roles[KTp::ContactSubscriptionStateRole]= "subscriptionState";
60  roles[KTp::ContactPublishStateRole]= "publishState";
61  roles[KTp::ContactIsBlockedRole]= "blocked";
62  roles[KTp::ContactCanTextChatRole]= "textChat";
63  roles[KTp::ContactCanFileTransferRole]= "fileTransfer";
64  roles[KTp::ContactCanAudioCallRole]= "audioCall";
65  roles[KTp::ContactCanVideoCallRole]= "videoCall";
66  roles[KTp::ContactTubesRole]= "tubes";
67  setRoleNames(roles);
68 }
69 
70 KTp::ContactsListModel::~ContactsListModel()
71 {
72  delete d;
73 }
74 
75 void KTp::ContactsListModel::setAccountManager(const Tp::AccountManagerPtr &accountManager)
76 {
77  d->contactManager = new KTp::GlobalContactManager(accountManager, this);
78  connect(d->contactManager, SIGNAL(allKnownContactsChanged(Tp::Contacts,Tp::Contacts)), SLOT(onContactsChanged(Tp::Contacts,Tp::Contacts)));
79 }
80 
81 int KTp::ContactsListModel::rowCount(const QModelIndex &parent) const
82 {
83  if (!parent.isValid()) {
84  return d->contacts.size();
85  } else {
86  return 0;
87  }
88 }
89 
90 QVariant KTp::ContactsListModel::data(const QModelIndex &index, int role) const
91 {
92  int row = index.row();
93 
94  if (row >=0 && row < d->contacts.size()) {
95  const KTp::ContactPtr contact = KTp::ContactPtr::qObjectCast(d->contacts[row]);
96  Q_ASSERT_X(!contact.isNull(), "KTp::ContactListModel::data()",
97  "Failed to cast Tp::ContactPtr to KTp::ContactPtr. Are you using KTp::ContactFactory?");
98 
99  switch (role) {
100  case KTp::RowTypeRole:
101  return KTp::ContactRowType;
102  case Qt::DisplayRole:
103  return contact->alias();
104  case KTp::IdRole:
105  return contact->id();
106 
107  case KTp::ContactRole:
108  return QVariant::fromValue(contact);
109  case KTp::AccountRole:
110  return QVariant::fromValue(d->contactManager->accountForContact(contact));
111 
112  case KTp::ContactClientTypesRole:
113  return contact->clientTypes();
114  case KTp::ContactAvatarPathRole:
115  return contact->avatarData().fileName;
116  case KTp::ContactAvatarPixmapRole:
117  return contact->avatarPixmap();
118  case KTp::ContactGroupsRole:
119  return contact->groups();
120 
121  case KTp::ContactPresenceNameRole:
122  return contact->presence().displayString();
123  case KTp::ContactPresenceMessageRole:
124  return contact->presence().statusMessage();
125  case KTp::ContactPresenceTypeRole:
126  return contact->presence().type();
127  case KTp::ContactPresenceIconRole:
128  return contact->presence().iconName();
129 
130  case KTp::ContactSubscriptionStateRole:
131  return contact->subscriptionState();
132  case KTp::ContactPublishStateRole:
133  return contact->publishState();
134  case KTp::ContactIsBlockedRole:
135  return contact->isBlocked();
136 
137  case KTp::ContactCanTextChatRole:
138  return true; //FIXME
139  case KTp::ContactCanFileTransferRole:
140  return contact->fileTransferCapability();
141  case KTp::ContactCanAudioCallRole:
142  return contact->audioCallCapability();
143  case KTp::ContactCanVideoCallRole:
144  return contact->videoCallCapability();
145  case KTp::ContactTubesRole:
146  //FIXME this does not check own selfContact caps.
147  return QStringList() << contact->capabilities().streamTubeServices()
148  << contact->capabilities().dbusTubeServices();
149 
150  default:
151  break;
152  }
153  }
154  return QVariant();
155 }
156 
157 void KTp::ContactsListModel::onContactsChanged(const Tp::Contacts &added, const Tp::Contacts &removed)
158 {
159  //add contacts.
160 
161  Q_FOREACH(const Tp::ContactPtr &contact_uncasted, added) {
162  KTp::ContactPtr contact = KTp::ContactPtr::qObjectCast(contact_uncasted);
163 
164  connect(contact.data(),
165  SIGNAL(aliasChanged(QString)),
166  SLOT(onChanged()));
167  connect(contact.data(),
168  SIGNAL(avatarTokenChanged(QString)),
169  SLOT(onChanged()));
170  connect(contact.data(),
171  SIGNAL(avatarDataChanged(Tp::AvatarData)),
172  SLOT(onChanged()));
173  connect(contact.data(),
174  SIGNAL(presenceChanged(Tp::Presence)),
175  SLOT(onChanged()));
176  connect(contact->manager()->connection()->selfContact().data(),
177  SIGNAL(capabilitiesChanged(Tp::ContactCapabilities)),
178  SLOT(onChanged()));
179  connect(contact.data(),
180  SIGNAL(capabilitiesChanged(Tp::ContactCapabilities)),
181  SLOT(onChanged()));
182  connect(contact.data(),
183  SIGNAL(locationUpdated(Tp::LocationInfo)),
184  SLOT(onChanged()));
185  connect(contact.data(),
186  SIGNAL(infoFieldsChanged(Tp::Contact::InfoFields)),
187  SLOT(onChanged()));
188  connect(contact.data(),
189  SIGNAL(subscriptionStateChanged(Tp::Contact::PresenceState)),
190  SLOT(onChanged()));
191  connect(contact.data(),
192  SIGNAL(publishStateChanged(Tp::Contact::PresenceState,QString)),
193  SLOT(onChanged()));
194  connect(contact.data(),
195  SIGNAL(blockStatusChanged(bool)),
196  SLOT(onChanged()));
197  connect(contact.data(),
198  SIGNAL(clientTypesChanged(QStringList)),
199  SLOT(onChanged()));
200  connect(contact.data(),
201  SIGNAL(addedToGroup(QString)),
202  SLOT(onChanged()));
203  connect(contact.data(),
204  SIGNAL(removedFromGroup(QString)),
205  SLOT(onChanged()));
206 
207  connect(contact.data(),
208  SIGNAL(invalidated()),
209  SLOT(onConnectionDropped()));
210  }
211 
212  if (added.size() > 0) {
213  beginInsertRows(QModelIndex(), d->contacts.size(), d->contacts.size() + added.size() -1);
214  d->contacts.append(added.toList());
215  endInsertRows();
216  }
217 
218  //remove contacts
219  Q_FOREACH(const Tp::ContactPtr &contact, removed) {
220  int row = d->contacts.indexOf(contact);
221  if (row >= 0) { //if contact found in list
222  beginRemoveRows(QModelIndex(), row, row);
223  d->contacts.removeOne(contact);
224  endRemoveRows();
225  }
226  }
227 }
228 
229 void KTp::ContactsListModel::onChanged()
230 {
231  KTp::ContactPtr contact(qobject_cast<KTp::Contact*>(sender()));
232  int row = d->contacts.indexOf(contact);
233  if (row > 0) {
234  QModelIndex index = createIndex(row, 0);
235  dataChanged(index, index);
236  }
237 }
238 
239 void KTp::ContactsListModel::onConnectionDropped()
240 {
241  KTp::ContactPtr contact(qobject_cast<KTp::Contact*>(sender()));
242  onContactsChanged(Tp::Contacts(), Tp::Contacts() << contact);
243 }
244 
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