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

Nepomuk-Core

  • KTp
  • Models
rooms-model.cpp
Go to the documentation of this file.
1 /*
2  * Rooms Model - A model of chatrooms.
3  * Copyright (C) 2012 Dominik Cermak <d.cermak@arcor.de>
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18  */
19 
20 #include "rooms-model.h"
21 #include <KIcon>
22 #include <KLocale>
23 
24 // RoomsModel
25 KTp::RoomsModel::RoomsModel(QObject *parent): QAbstractListModel(parent)
26 {
27 }
28 
29 int KTp::RoomsModel::rowCount(const QModelIndex &parent) const
30 {
31  if (parent.isValid()) {
32  return 0;
33  } else {
34  return m_roomInfoList.size();
35  }
36 }
37 
38 int KTp::RoomsModel::columnCount(const QModelIndex &parent) const
39 {
40  if (parent.isValid()) {
41  return 0;
42  } else {
43  return 4;
44  }
45 }
46 
47 QVariant KTp::RoomsModel::data(const QModelIndex &index, int role) const
48 {
49  if (!index.isValid()) {
50  return QVariant();
51  }
52 
53  if (index.row() >= m_roomInfoList.count()) {
54  return QVariant();
55  }
56 
57  const int row = index.row();
58  const Tp::RoomInfo &roomInfo = m_roomInfoList.at(row);
59 
60  // this is handled here because when putting it in the switch below
61  // all columns get an empty space for the decoration
62  if (index.column() == PasswordColumn) {
63  switch (role) {
64  case Qt::DecorationRole:
65  if (roomInfo.info.value(QLatin1String("password")).toBool()) {
66  return KIcon(QLatin1String("object-locked"));
67  } else {
68  return QVariant();
69  }
70  case Qt::ToolTipRole:
71  if (roomInfo.info.value(QLatin1String("password")).toBool()) {
72  return i18n("Password required");
73  } else {
74  return i18n("No password required");
75  }
76  }
77  }
78 
79  switch(role) {
80  case Qt::DisplayRole:
81  switch (index.column()) {
82  case PasswordColumn:
83  return QVariant();
84  case NameColumn:
85  return roomInfo.info.value(QLatin1String("name"));
86  case DescriptionColumn:
87  return roomInfo.info.value(QLatin1String("description"));
88  case MembersColumn:
89  return roomInfo.info.value(QLatin1String("members"));
90  }
91  case Qt::ToolTipRole:
92  switch (index.column()) {
93  case MembersColumn:
94  return i18n("Member count");
95  }
96  case RoomsModel::HandleNameRole:
97  return roomInfo.info.value(QLatin1String("handle-name"));
98  }
99 
100  return QVariant();
101 }
102 
103 QVariant KTp::RoomsModel::headerData(int section, Qt::Orientation orientation, int role) const
104 {
105  if (role != Qt::DisplayRole && role != Qt::DecorationRole) {
106  return QVariant();
107  }
108 
109  if (orientation == Qt::Horizontal) {
110  switch (role) {
111  case Qt::DisplayRole:
112  switch (section) {
113  case NameColumn:
114  return i18nc("Chatrooms name", "Name");
115  case DescriptionColumn:
116  return i18nc("Chatrooms description", "Description");
117  }
118  case Qt::DecorationRole:
119  switch (section) {
120  case PasswordColumn:
121  return KIcon(QLatin1String("object-locked"));
122  case MembersColumn:
123  return KIcon(QLatin1String("meeting-participant"));
124  }
125  }
126  }
127 
128  return QVariant();
129 }
130 
131 void KTp::RoomsModel::addRooms(const Tp::RoomInfoList newRoomList)
132 {
133  if (newRoomList.size() > 0) {
134  beginInsertRows(QModelIndex(), m_roomInfoList.size(), m_roomInfoList.size() + newRoomList.size() - 1);
135  m_roomInfoList.append(newRoomList);
136  endInsertRows();
137  }
138 }
139 
140 void KTp::RoomsModel::clearRoomInfoList()
141 {
142  if (m_roomInfoList.size() > 0) {
143  beginRemoveRows(QModelIndex(), 0, m_roomInfoList.size() - 1);
144  m_roomInfoList.clear();
145  endRemoveRows();
146  }
147 }
148 
149 // FavoriteRoomsModel
150 KTp::FavoriteRoomsModel::FavoriteRoomsModel(QObject *parent): QAbstractListModel(parent)
151 {
152 }
153 
154 int KTp::FavoriteRoomsModel::rowCount(const QModelIndex &parent) const
155 {
156  if (parent.isValid()) {
157  return 0;
158  } else {
159  return m_favoriteRoomsList.size();
160  }
161 }
162 
163 int KTp::FavoriteRoomsModel::columnCount(const QModelIndex &parent) const
164 {
165  if (parent.isValid()) {
166  return 0;
167  } else {
168  return 3;
169  }
170 }
171 
172 QVariant KTp::FavoriteRoomsModel::data(const QModelIndex &index, int role) const
173 {
174  if (!index.isValid()) {
175  return QVariant();
176  }
177 
178  if (index.row() >= m_favoriteRoomsList.size()) {
179  return QVariant();
180  }
181 
182  const int row = index.row();
183  const QVariantMap &room = m_favoriteRoomsList.at(row);
184 
185  switch(role) {
186  case Qt::DisplayRole:
187  switch (index.column()) {
188  case NameColumn:
189  return room.value(QLatin1String("name"));
190  case HandleNameColumn:
191  return room.value(QLatin1String("handle-name"));
192  case AccountIdentifierColumn:
193  return room.value(QLatin1String("account-identifier"));
194  }
195  case Qt::ToolTipRole:
196  return room.value(QLatin1String("handle-name"));
197  case FavoriteRoomsModel::HandleNameRole:
198  return room.value(QLatin1String("handle-name"));
199  case FavoriteRoomsModel::FavoriteRoomRole:
200  return QVariant::fromValue<QVariantMap>(room);
201  }
202 
203  return QVariant();
204 }
205 
206 void KTp::FavoriteRoomsModel::addRooms(const QList<QVariantMap> newRoomList)
207 {
208  if (newRoomList.size() > 0) {
209  beginInsertRows(QModelIndex(), m_favoriteRoomsList.size(), m_favoriteRoomsList.size() + newRoomList.size() - 1);
210  m_favoriteRoomsList.append(newRoomList);
211  endInsertRows();
212  }
213 }
214 
215 void KTp::FavoriteRoomsModel::addRoom(const QVariantMap &room)
216 {
217  beginInsertRows(QModelIndex(), m_favoriteRoomsList.size(), m_favoriteRoomsList.size());
218  m_favoriteRoomsList.append(room);
219  endInsertRows();
220 }
221 
222 void KTp::FavoriteRoomsModel::removeRoom(const QVariantMap &room)
223 {
224  int row = m_favoriteRoomsList.indexOf(room);
225  beginRemoveRows(QModelIndex(), row, row);
226  m_favoriteRoomsList.removeOne(room);
227  endRemoveRows();
228 }
229 
230 bool KTp::FavoriteRoomsModel::containsRoom(const QString &handle, const QString &account) const
231 {
232  bool contains = false;
233 
234  Q_FOREACH(const QVariantMap &room, m_favoriteRoomsList) {
235  if ((room.value(QLatin1String("handle-name")) == handle) && (room.value(QLatin1String("account-identifier")) == account)) {
236  contains = true;
237  }
238  }
239 
240  return contains;
241 }
242 
243 int KTp::FavoriteRoomsModel::countForAccount(const QString &account) const
244 {
245  int count = 0;
246 
247  Q_FOREACH (const QVariantMap &room, m_favoriteRoomsList) {
248  if (room.value(QLatin1String("account-identifier")) == account) {
249  count++;
250  }
251  }
252 
253  return count;
254 }
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