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>
34 #include <KComponentData>
35 #include <KConfigGroup>
36 #include <KStandardDirs>
43 class PinnedContactsModelPrivate {
45 PinnedContactsModelPrivate() {
49 QList<KTp::PersistentContactPtr> m_pins;
50 Tp::AccountManagerPtr accountManager;
53 QStringList pinsToString()
const {
56 ret += p->accountId();
57 ret += p->contactId();
65 , d(new PinnedContactsModelPrivate)
67 QHash<int, QByteArray> roles = roleNames();
75 connect(
this, SIGNAL(rowsInserted(QModelIndex,
int,
int)), SIGNAL(
countChanged()));
76 connect(
this, SIGNAL(rowsRemoved(QModelIndex,
int,
int)), SIGNAL(
countChanged()));
86 return d->pinsToString();
91 for (
int i = 0; i < pins.count(); i += 2) {
98 for (
int i=0; i<d->m_pins.size();i++) {
99 if (d->m_pins[i]->contact() == contact) {
103 return QModelIndex();
109 bool found = idx.isValid();
110 if (newState && !found) {
113 }
else if (!newState && found) {
114 removeRow(idx.row());
120 if (index.isValid()) {
123 case Qt::DisplayRole:
125 return p->contact()->alias();
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;
146 return QVariant::fromValue<KTp::ContactPtr>(p->contact());
148 return QVariant::fromValue<Tp::AccountPtr>(p->account());
154 for (
int i = 0; !found && i < d->conversations->rowCount(); i++) {
155 QModelIndex idx = d->conversations->index(i, 0);
157 found |= contact->id() == p->contact()->id();
161 case Qt::DecorationRole: {
164 QString file = p->contact()->avatarData().fileName;
165 if (!file.isEmpty()) {
170 icon = KIcon(QLatin1String(
"im-user"));
181 if (parent.isValid()) {
184 return d->m_pins.count();
189 int row = d->m_pins.indexOf(pin);
190 beginRemoveRows(QModelIndex(), row, row);
191 d->m_pins.removeAt(row);
197 int s = d->m_pins.size();
198 beginInsertRows(QModelIndex(), s, s);
202 if (d->accountManager && d->accountManager->isReady()) {
203 pin->setAccountManager(d->accountManager);
206 if (pin->contact()) {
207 contactChanged(pin->contact());
212 void PinnedContactsModel::contactChanged(
const KTp::ContactPtr &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()));
227 Q_EMIT dataChanged(index, index);
230 void PinnedContactsModel::contactDataChanged()
234 Q_EMIT dataChanged(index, index);
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)));
246 void PinnedContactsModel::conversationsStateChanged(
const QModelIndex &parent,
int start,
int end)
248 for (
int i = start; i <= end; i++) {
249 QModelIndex idx = d->conversations->index(i, 0, parent);
253 if (p->contact() == contact) {
256 QMetaObject::invokeMethod(
this,
"dataChanged", Qt::QueuedConnection, Q_ARG(QModelIndex, contactIdx), Q_ARG(QModelIndex, contactIdx));
264 return d->conversations;
269 return d->accountManager;
274 d->accountManager = accounts;
276 connect(d->accountManager->becomeReady(), SIGNAL(finished(Tp::PendingOperation*)), SLOT(onAccountManagerReady()));
279 void PinnedContactsModel::onAccountManagerReady()
282 p->setAccountManager(d->accountManager);