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

Nepomuk-Core

  • KTp
  • Declarative
pinned-contacts-model.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2012 Aleix Pol <aleixpol@kde.org>
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 Street, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18 
19 #include "pinned-contacts-model.h"
20 #include "conversations-model.h"
21 #include "conversation.h"
22 
23 #include <TelepathyQt/Types>
24 #include <TelepathyQt/AvatarData>
25 #include <TelepathyQt/Presence>
26 #include <TelepathyQt/Account>
27 #include <TelepathyQt/AccountManager>
28 #include <TelepathyQt/PendingReady>
29 #include <TelepathyQt/ContactManager>
30 #include <TelepathyQt/PendingContacts>
31 
32 #include <KIcon>
33 #include <KGlobal>
34 #include <KComponentData>
35 #include <KConfigGroup>
36 #include <KStandardDirs>
37 #include <KDebug>
38 
39 #include "KTp/presence.h"
40 #include "KTp/contact.h"
41 #include "KTp/persistent-contact.h"
42 
43 class PinnedContactsModelPrivate {
44 public:
45  PinnedContactsModelPrivate() {
46  conversations = 0;
47  }
48 
49  QList<KTp::PersistentContactPtr> m_pins;
50  Tp::AccountManagerPtr accountManager;
51  ConversationsModel *conversations;
52 
53  QStringList pinsToString() const {
54  QStringList ret;
55  Q_FOREACH(const KTp::PersistentContactPtr &p, m_pins) {
56  ret += p->accountId();
57  ret += p->contactId();
58  }
59  return ret;
60  }
61 };
62 
63 PinnedContactsModel::PinnedContactsModel(QObject *parent)
64  : QAbstractListModel(parent)
65  , d(new PinnedContactsModelPrivate)
66 {
67  QHash<int, QByteArray> roles = roleNames();
68  roles[PresenceIconRole] = "presenceIcon";
69  roles[AvailabilityRole] = "available";
70  roles[ContactRole] = "contact";
71  roles[AccountRole] = "account";
72  roles[AlreadyChattingRole] = "alreadyChatting";
73  setRoleNames(roles);
74 
75  connect(this, SIGNAL(rowsInserted(QModelIndex,int,int)), SIGNAL(countChanged()));
76  connect(this, SIGNAL(rowsRemoved(QModelIndex,int,int)), SIGNAL(countChanged()));
77 }
78 
79 PinnedContactsModel::~PinnedContactsModel()
80 {
81  delete d;
82 }
83 
84 QStringList PinnedContactsModel::state() const
85 {
86  return d->pinsToString();
87 }
88 
89 void PinnedContactsModel::setState(const QStringList &pins)
90 {
91  for (int i = 0; i < pins.count(); i += 2) {
92  appendContactPin(KTp::PersistentContact::create(pins[0], pins[1]));
93  }
94 }
95 
96 QModelIndex PinnedContactsModel::indexForContact(const KTp::ContactPtr &contact) const
97 {
98  for (int i=0; i<d->m_pins.size();i++) {
99  if (d->m_pins[i]->contact() == contact) {
100  return index(i);
101  }
102  }
103  return QModelIndex();
104 }
105 
106 void PinnedContactsModel::setPinning(const Tp::AccountPtr &account, const KTp::ContactPtr &contact, bool newState)
107 {
108  QModelIndex idx = indexForContact(contact);
109  bool found = idx.isValid();
110  if (newState && !found) {
111  KTp::PersistentContactPtr p = KTp::PersistentContact::create(account->uniqueIdentifier(), contact->id());
112  appendContactPin(p);
113  } else if (!newState && found) {
114  removeRow(idx.row());
115  }
116 }
117 
118 QVariant PinnedContactsModel::data(const QModelIndex &index, int role) const
119 {
120  if (index.isValid()) {
121  KTp::PersistentContactPtr p = d->m_pins[index.row()];
122  switch(role) {
123  case Qt::DisplayRole:
124  if (p->contact()) {
125  return p->contact()->alias();
126  }
127  break;
128  case PresenceIconRole:
129  if (p->contact()) {
130  return KTp::Presence(p->contact()->presence()).icon();
131  } else {
132  return KTp::Presence(Tp::Presence::offline()).icon();
133  }
134  break;
135  case AvailabilityRole:
136  if (!p->contact()) {
137  return false;
138  }
139  else {
140  return p->contact()->presence().type()!=Tp::ConnectionPresenceTypeOffline
141  && p->contact()->presence().type()!=Tp::ConnectionPresenceTypeError
142  && p->contact()->presence().type()!=Tp::ConnectionPresenceTypeUnset
143  && p->contact()->presence().type()!=Tp::ConnectionPresenceTypeUnknown;
144  }
145  case ContactRole:
146  return QVariant::fromValue<KTp::ContactPtr>(p->contact());
147  case AccountRole:
148  return QVariant::fromValue<Tp::AccountPtr>(p->account());
149  case AlreadyChattingRole: {
150  if (!p->contact()) {
151  return false;
152  }
153  bool found = false;
154  for (int i = 0; !found && i < d->conversations->rowCount(); i++) {
155  QModelIndex idx = d->conversations->index(i, 0);
156  KTp::ContactPtr contact = idx.data(ConversationsModel::ConversationRole).value<Conversation*>()->target()->contact();
157  found |= contact->id() == p->contact()->id();
158  }
159  return found;
160  }
161  case Qt::DecorationRole: {
162  KIcon icon;
163  if (p->contact()) {
164  QString file = p->contact()->avatarData().fileName;
165  if (!file.isEmpty()) {
166  icon = KIcon(file);
167  }
168  }
169  if (icon.isNull()) {
170  icon = KIcon(QLatin1String("im-user"));
171  }
172  return icon;
173  }
174  }
175  }
176  return QVariant();
177 }
178 
179 int PinnedContactsModel::rowCount(const QModelIndex &parent) const
180 {
181  if (parent.isValid()) {
182  return 0;
183  }
184  return d->m_pins.count();
185 }
186 
187 void PinnedContactsModel::removeContactPin(const KTp::PersistentContactPtr &pin)
188 {
189  int row = d->m_pins.indexOf(pin);
190  beginRemoveRows(QModelIndex(), row, row);
191  d->m_pins.removeAt(row);
192  endRemoveRows();
193 }
194 
195 void PinnedContactsModel::appendContactPin(const KTp::PersistentContactPtr &pin)
196 {
197  int s = d->m_pins.size();
198  beginInsertRows(QModelIndex(), s, s);
199  d->m_pins += pin;
200  endInsertRows();
201 
202  if (d->accountManager && d->accountManager->isReady()) {
203  pin->setAccountManager(d->accountManager);
204  }
205 
206  if (pin->contact()) {
207  contactChanged(pin->contact());
208  }
209  connect(pin.data(), SIGNAL(contactChanged(KTp::ContactPtr)), SLOT(contactChanged(KTp::ContactPtr)));
210 }
211 
212 void PinnedContactsModel::contactChanged(const KTp::ContactPtr &contact)
213 {
214  if (contact) {
215  connect(contact.data(),
216  SIGNAL(avatarDataChanged(Tp::AvatarData)),
217  SLOT(contactDataChanged()));
218  connect(contact.data(),
219  SIGNAL(aliasChanged(QString)),
220  SLOT(contactDataChanged()));
221  connect(contact.data(),
222  SIGNAL(presenceChanged(Tp::Presence)),
223  SLOT(contactDataChanged()));
224  }
225 
226  QModelIndex index = indexForContact(contact);
227  Q_EMIT dataChanged(index, index);
228 }
229 
230 void PinnedContactsModel::contactDataChanged()
231 {
232  KTp::Contact *c = qobject_cast<KTp::Contact*>(sender());
233  QModelIndex index = indexForContact(KTp::ContactPtr(c));
234  Q_EMIT dataChanged(index, index);
235 }
236 
237 void PinnedContactsModel::setConversationsModel(ConversationsModel *model)
238 {
239  beginResetModel();
240  d->conversations = model;
241  connect(d->conversations, SIGNAL(rowsAboutToBeRemoved(QModelIndex, int, int)), SLOT(conversationsStateChanged(QModelIndex, int, int)));
242  connect(d->conversations, SIGNAL(rowsInserted(QModelIndex, int, int)), SLOT(conversationsStateChanged(QModelIndex, int, int)));
243  endResetModel();
244 }
245 
246 void PinnedContactsModel::conversationsStateChanged(const QModelIndex &parent, int start, int end)
247 {
248  for (int i = start; i <= end; i++) {
249  QModelIndex idx = d->conversations->index(i, 0, parent);
250  Conversation* conv = idx.data(ConversationsModel::ConversationRole).value<Conversation*>();
251  KTp::ContactPtr contact = conv->target()->contact();
252  Q_FOREACH(const KTp::PersistentContactPtr &p, d->m_pins) {
253  if (p->contact() == contact) {
254  QModelIndex contactIdx = indexForContact(p->contact());
255  //We need to delay the dataChanged until the next event loop, when endRowsRemoved has been called
256  QMetaObject::invokeMethod(this, "dataChanged", Qt::QueuedConnection, Q_ARG(QModelIndex, contactIdx), Q_ARG(QModelIndex, contactIdx));
257  }
258  }
259  }
260 }
261 
262 ConversationsModel* PinnedContactsModel::conversationsModel() const
263 {
264  return d->conversations;
265 }
266 
267 Tp::AccountManagerPtr PinnedContactsModel::accountManager() const
268 {
269  return d->accountManager;
270 }
271 
272 void PinnedContactsModel::setAccountManager(const Tp::AccountManagerPtr &accounts)
273 {
274  d->accountManager = accounts;
275 
276  connect(d->accountManager->becomeReady(), SIGNAL(finished(Tp::PendingOperation*)), SLOT(onAccountManagerReady()));
277 }
278 
279 void PinnedContactsModel::onAccountManagerReady()
280 {
281  Q_FOREACH(const KTp::PersistentContactPtr &p, d->m_pins) {
282  p->setAccountManager(d->accountManager);
283  }
284 }
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