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

kopete/libkopete

  • sources
  • kde-4.12
  • kdenetwork
  • kopete
  • libkopete
chatsessionmemberslistmodel.cpp
Go to the documentation of this file.
1 /*
2  ChatSessionMembersListModel
3 
4  Copyright (c) 2007 by Duncan Mac-Vicar Prett <duncan@kde.org>
5 
6  Kopete (c) 2002-2007 by the Kopete developers <kopete-devel@kde.org>
7 
8  *************************************************************************
9  * *
10  * This library is free software; you can redistribute it and/or *
11  * modify it under the terms of the GNU Lesser General Public *
12  * License as published by the Free Software Foundation; either *
13  * version 2 of the License, or (at your option) any later version. *
14  * *
15  *************************************************************************
16 */
17 
18 #include "kopetecontact.h"
19 #include "kopeteonlinestatus.h"
20 #include "chatsessionmemberslistmodel.h"
21 #include "kdebug.h"
22 
23 //own contact comparator, needs sessions contact-online-status weight
24 inline bool lessThan(const Kopete::Contact *c1, int weight1, const Kopete::Contact *c2, int weight2){
25  return (weight1 > weight2 || (weight1 == weight2
26  && c1->nickName().compare(c2->nickName(),Qt::CaseInsensitive)<=0));
27 
28 }
29 
30 //class for more sorting speed
31 class ContactWrapper
32 {
33 public:
34  ContactWrapper(Kopete::Contact *contact, int weight){
35  this->contact = contact;
36  this->weight = weight;
37  }
38  Kopete::Contact *contact;
39  int weight;
40  bool operator <(const ContactWrapper &other) const
41  {
42  return lessThan(this->contact, weight, other.contact, other.weight);
43  }
44 };
45 
46 class Private
47 {
48 public:
49  Kopete::ContactPtrList contacts;
50 
51  Kopete::ChatSession *session;
52 
53  QTimer *timer;
54 
55  //insert into temporary efficient structure for sorting, then copy
56  void insertMultiple(Kopete::ContactPtrList contacts){
57  QMap<ContactWrapper, Kopete::Contact*> map;
58  foreach (Kopete::Contact *c,this->contacts)
59  map.insert(ContactWrapper(c,session->contactOnlineStatus(c).weight()),c);
60  foreach (Kopete::Contact *c,contacts)
61  map.insert(ContactWrapper(c,session->contactOnlineStatus(c).weight()),c);
62  this->contacts.clear();
63  foreach (Kopete::Contact *c, map)
64  this->contacts.append(c);
65  }
66 
67  //get index by binary search
68  int getInsertIndex(const Kopete::Contact *contact) const
69  {
70  int weightOld;
71  int weightNew;
72  int i;
73  int min = 0;
74  int max = contacts.size();
75  int step;
76 
77  while (max>0)
78  {
79  step = max/2;
80  i = min + step;
81 
82  weightOld = session->contactOnlineStatus(contacts[i]).weight();
83  weightNew = session->contactOnlineStatus(contact).weight();
84 
85  if (lessThan(contacts[i], weightOld, contact, weightNew))
86  {
87  min = ++i;
88  max -= step+1;
89  } else
90  max = step;
91  };
92  return min;
93  }
94 };
95 
96 namespace Kopete
97 {
98 
99 ChatSessionMembersListModel::ChatSessionMembersListModel(QObject * parent)
100  : QAbstractListModel(parent)
101 {
102  d = new Private();
103  d->session = 0L;
104 }
105 
106 ChatSessionMembersListModel::~ChatSessionMembersListModel(){
107  delete d;
108 }
109 
110 Kopete::ChatSession * ChatSessionMembersListModel::session(){
111  return d->session;
112 }
113 
114 void ChatSessionMembersListModel::setChatSession(ChatSession *session)
115 {
116  if ( d->session )
117  disconnect( d->session, 0, this, 0 );
118 
119  d->session = session;
120 
121  connect( session, SIGNAL(closing(Kopete::ChatSession*)),
122  this, SLOT(slotSessionClosed()) );
123  connect( session, SIGNAL(contactAdded(const Kopete::Contact*,bool)),
124  this, SLOT(slotContactAdded(const Kopete::Contact*)) );
125  connect( session, SIGNAL(contactRemoved(const Kopete::Contact*,QString,Qt::TextFormat,bool)),
126  this, SLOT(slotContactRemoved(const Kopete::Contact*)) );
127  connect( session, SIGNAL(onlineStatusChanged(Kopete::Contact*,Kopete::OnlineStatus,Kopete::OnlineStatus)),
128  this, SLOT(slotContactStatusChanged(Kopete::Contact*,Kopete::OnlineStatus)) );
129  connect( session, SIGNAL(displayNameChanged()),
130  this, SLOT(slotSessionChanged()) );
131  connect( session, SIGNAL(photoChanged()),
132  this, SLOT(slotSessionChanged()) );
133  connect( session, SIGNAL(nickNameChanged(Kopete::Contact*,QString)),
134  this, SLOT(slotContactNickNameChanged(Kopete::Contact*)) );
135 
136  d->contacts.clear();
137  d->insertMultiple(d->session->members());
138 
139  d->contacts.insert(
140  d->getInsertIndex((Contact*)d->session->myself()),
141  (Contact*)d->session->myself());
142 
143  reset();
144 }
145 
146 Kopete::Contact * ChatSessionMembersListModel::contactAt( const QModelIndex &index ) const
147 {
148  kDebug( 14010 ) << "memberslistmodel contactat";
149  if ( d->session )
150  {
151  if (!index.isValid())
152  return 0L;
153 
154  if (index.row() >= d->contacts.size())
155  return 0L;
156 
157  return d->contacts.at(index.row());
158  }
159 
160  return 0L;
161 }
162 
163 int ChatSessionMembersListModel::rowCount(const QModelIndex &parent) const
164 {
165  Q_UNUSED(parent)
166  if ( d->session )
167  return d->contacts.size();
168 
169  return 0;
170 }
171 
172 QVariant ChatSessionMembersListModel::data(const QModelIndex &index, int role) const
173 {
174  Contact *c = d->contacts.at(index.row());
175  if (!c)
176  return QVariant();
177 
178  if (role == Qt::DisplayRole)
179  {
180 
181  return c->nickName();
182 
183  }
184  else if (role == Qt::DecorationRole)
185  {
186  return d->session->contactOnlineStatus(c).iconFor(c);
187  }
188  else if (role == Qt::ToolTipRole)
189  {
190  return c->toolTip();
191  }
192  else
193  return QVariant();
194 }
195 
196 QVariant ChatSessionMembersListModel::headerData(int section, Qt::Orientation orientation, int role) const
197 {
198  if (role != Qt::DisplayRole)
199  return QVariant();
200 
201  if (orientation == Qt::Horizontal)
202  return QString("Column %1").arg(section);
203  else
204  return QString("Row %1").arg(section);
205 }
206 
207 void ChatSessionMembersListModel::slotContactAdded( const Kopete::Contact *contact )
208 {
209  kDebug( 14010 ) << "memberslistmodel contact added "<< contact->nickName();
210  int index = d->getInsertIndex(contact);
211  beginInsertRows(QModelIndex(),index,index);
212  d->contacts.insert(index,(Contact*)contact);
213  endInsertRows();
214 }
215 
216 void ChatSessionMembersListModel::slotContactRemoved( const Kopete::Contact *contact )
217 {
218  kDebug( 14010 ) << "memberslistmodel contact removed "<< contact->nickName();
219  int index = d->contacts.indexOf((Contact*)contact);
220  if (index == -1) {
221  kDebug( 14010 ) << "Trying to remove contact '" << contact->nickName() << "' which isn't in members list model!!!";
222  return;
223  }
224 
225  beginRemoveRows(QModelIndex(),index, index);
226  d->contacts.removeAt(index);
227  endRemoveRows();
228 }
229 
230 void ChatSessionMembersListModel::slotContactStatusChanged( Kopete::Contact *contact, const Kopete::OnlineStatus &status )
231 {
232  Q_UNUSED(status)
233  kDebug( 14010 ) << "memberslistmodel contact status changed "<< contact->nickName();
234  slotContactRemoved(contact);
235  slotContactAdded(contact);
236 }
237 
238 void ChatSessionMembersListModel::slotSessionChanged()
239 {
240  kDebug( 14010 );
241  reset();
242 }
243 
244 void ChatSessionMembersListModel::slotContactNickNameChanged( Kopete::Contact *contact)
245 {
246  kDebug( 14010 ) << "memberslistmodel nickname changed to "<< contact->nickName();
247  slotContactRemoved(contact);
248  slotContactAdded(contact);
249 }
250 
251 
252 void ChatSessionMembersListModel::slotSessionClosed()
253 {
254  if ( d->session )
255  {
256  disconnect( d->session, 0, this, 0 );
257  d->session = 0;
258  reset();
259  }
260 }
261 
262 }
Kopete::OnlineStatus
Definition: kopeteonlinestatus.h:68
Kopete::ContactPtrList
QList< Contact * > ContactPtrList
Definition: kopetechatsession.h:52
status
OnlineStatus::StatusType status
Definition: kopeteonlinestatus.cpp:103
Kopete::ChatSessionMembersListModel::headerData
QVariant headerData(int section, Qt::Orientation orientation, int role=Qt::DisplayRole) const
Definition: chatsessionmemberslistmodel.cpp:196
Kopete::ChatSessionMembersListModel::rowCount
int rowCount(const QModelIndex &parent=QModelIndex()) const
Definition: chatsessionmemberslistmodel.cpp:163
Kopete::ChatSession::contactOnlineStatus
const OnlineStatus contactOnlineStatus(const Contact *contact) const
get the status of a contact.
Definition: kopetechatsession.cpp:141
Kopete::OnlineStatus::weight
unsigned weight() const
Return the weight.
Definition: kopeteonlinestatus.cpp:252
QAbstractListModel
kopeteonlinestatus.h
QObject
Kopete::Contact::nickName
QString nickName
Definition: kopetecontact.h:73
Kopete::ChatSession
Definition: kopetechatsession.h:74
chatsessionmemberslistmodel.h
Kopete::ChatSessionMembersListModel::contactAt
Kopete::Contact * contactAt(const QModelIndex &index) const
Definition: chatsessionmemberslistmodel.cpp:146
Kopete::ChatSessionMembersListModel::session
Kopete::ChatSession * session()
Definition: chatsessionmemberslistmodel.cpp:110
Kopete::ChatSessionMembersListModel::ChatSessionMembersListModel
ChatSessionMembersListModel(QObject *parent=0)
Definition: chatsessionmemberslistmodel.cpp:99
Kopete::ChatSessionMembersListModel::data
QVariant data(const QModelIndex &index, int role) const
Definition: chatsessionmemberslistmodel.cpp:172
Kopete::Contact
Definition: kopetecontact.h:58
Kopete::ChatSessionMembersListModel::setChatSession
void setChatSession(Kopete::ChatSession *session)
Called when the ChatSession change for this list (eg.
Definition: chatsessionmemberslistmodel.cpp:114
Kopete::Contact::toolTip
QString toolTip
Definition: kopetecontact.h:72
Kopete::ChatSessionMembersListModel::~ChatSessionMembersListModel
~ChatSessionMembersListModel()
Definition: chatsessionmemberslistmodel.cpp:106
lessThan
bool lessThan(const Kopete::Contact *c1, int weight1, const Kopete::Contact *c2, int weight2)
Definition: chatsessionmemberslistmodel.cpp:24
kopetecontact.h
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:53:50 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

kopete/libkopete

Skip menu "kopete/libkopete"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

kdenetwork API Reference

Skip menu "kdenetwork API Reference"
  • kget
  • kopete
  •   kopete
  •   libkopete
  • krdc
  • krfb

Search



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