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

akonadi/contact

  • akonadi
  • contact
  • editor
  • im
immodel.cpp
1/*
2 This file is part of Akonadi Contact.
3
4 Copyright (c) 2010 Tobias Koenig <tokoe@kde.org>
5
6 This library is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Library General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or (at your
9 option) any later version.
10
11 This library is distributed in the hope that it will be useful, but WITHOUT
12 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
14 License for more details.
15
16 You should have received a copy of the GNU Library General Public License
17 along with this library; see the file COPYING.LIB. If not, write to the
18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 02110-1301, USA.
20*/
21
22#include "immodel.h"
23
24#include "improtocols.h"
25
26#include <kicon.h>
27#include <klocalizedstring.h>
28
29IMAddress::IMAddress()
30 : mProtocol(QLatin1String("messaging/aim"))
31 , mPreferred(false)
32{
33}
34
35IMAddress::IMAddress(const QString &protocol, const QString &name, bool preferred)
36 : mProtocol(protocol)
37 , mName(name)
38 , mPreferred(preferred)
39{
40}
41
42void IMAddress::setProtocol(const QString &protocol)
43{
44 mProtocol = protocol;
45}
46
47QString IMAddress::protocol() const
48{
49 return mProtocol;
50}
51
52void IMAddress::setName(const QString &name)
53{
54 mName = name;
55}
56
57QString IMAddress::name() const
58{
59 return mName;
60}
61
62void IMAddress::setPreferred(bool preferred)
63{
64 mPreferred = preferred;
65}
66
67bool IMAddress::preferred() const
68{
69 return mPreferred;
70}
71
72IMModel::IMModel(QObject *parent)
73 : QAbstractItemModel(parent)
74{
75}
76
77IMModel::~IMModel()
78{
79}
80
81void IMModel::setAddresses(const IMAddress::List &addresses)
82{
83 emit layoutAboutToBeChanged();
84
85 mAddresses = addresses;
86
87 emit layoutChanged();
88}
89
90IMAddress::List IMModel::addresses() const
91{
92 return mAddresses;
93}
94
95QModelIndex IMModel::index(int row, int column, const QModelIndex &parent) const
96{
97 Q_UNUSED(parent);
98 return createIndex(row, column);
99}
100
101QModelIndex IMModel::parent(const QModelIndex &child) const
102{
103 Q_UNUSED(child);
104 return QModelIndex();
105}
106
107QVariant IMModel::data(const QModelIndex &index, int role) const
108{
109 if (!index.isValid()) {
110 return QVariant();
111 }
112
113 if (index.row() < 0 || index.row() >= mAddresses.count()) {
114 return QVariant();
115 }
116
117 if (index.column() < 0 || index.column() > 1) {
118 return QVariant();
119 }
120
121 const IMAddress &address = mAddresses[index.row()];
122
123 if (role == Qt::DisplayRole) {
124 if (index.column() == 0) {
125 return IMProtocols::self()->name(address.protocol());
126 } else {
127 return address.name();
128 }
129 }
130
131 if (role == Qt::DecorationRole) {
132 if (index.column() == 1) {
133 return QVariant();
134 }
135
136 return KIcon(IMProtocols::self()->icon(address.protocol()));
137 }
138
139 if (role == Qt::EditRole) {
140 if (index.column() == 0) {
141 return address.protocol();
142 } else {
143 return address.name();
144 }
145 }
146
147 if (role == ProtocolRole) {
148 return address.protocol();
149 }
150
151 if (role == IsPreferredRole) {
152 return address.preferred();
153 }
154
155 return QVariant();
156}
157
158bool IMModel::setData(const QModelIndex &index, const QVariant &value, int role)
159{
160 if (!index.isValid()) {
161 return false;
162 }
163
164 if (index.row() < 0 || index.row() >= mAddresses.count()) {
165 return false;
166 }
167
168 if (index.column() < 0 || index.column() > 1) {
169 return false;
170 }
171
172 IMAddress &address = mAddresses[index.row()];
173
174 if (role == Qt::EditRole) {
175 if (index.column() == 1) {
176 address.setName(value.toString());
177 emit dataChanged(index, index);
178 return true;
179 }
180 }
181
182 if (role == ProtocolRole) {
183 address.setProtocol(value.toString());
184 emit dataChanged(this->index(index.row(), 0), this->index(index.row(), 1));
185 return true;
186 }
187
188 if (role == IsPreferredRole) {
189 address.setPreferred(value.toBool());
190 emit dataChanged(this->index(index.row(), 0), this->index(index.row(), 1));
191 return true;
192 }
193
194 return false;
195}
196
197QVariant IMModel::headerData(int section, Qt::Orientation orientation, int role) const
198{
199 if (section < 0 || section > 1) {
200 return QVariant();
201 }
202
203 if (orientation != Qt::Horizontal) {
204 return QVariant();
205 }
206
207 if (role != Qt::DisplayRole) {
208 return QVariant();
209 }
210
211 if (section == 0) {
212 return i18nc("instant messaging protocol", "Protocol");
213 } else {
214 return i18nc("instant messaging address", "Address");
215 }
216}
217
218Qt::ItemFlags IMModel::flags(const QModelIndex &index) const
219{
220 if (!index.isValid() || index.row() < 0 || index.row() >= mAddresses.count()) {
221 return QAbstractItemModel::flags(index);
222 }
223
224 const Qt::ItemFlags parentFlags = QAbstractItemModel::flags(index);
225 return (parentFlags | Qt::ItemIsEnabled | Qt::ItemIsEditable);
226}
227
228int IMModel::columnCount(const QModelIndex &parent) const
229{
230 if (!parent.isValid()) {
231 return 2;
232 } else {
233 return 0;
234 }
235}
236
237int IMModel::rowCount(const QModelIndex &parent) const
238{
239 if (!parent.isValid()) {
240 return mAddresses.count();
241 } else {
242 return 0;
243 }
244}
245
246bool IMModel::insertRows(int row, int count, const QModelIndex &parent)
247{
248 if (parent.isValid()) {
249 return false;
250 }
251
252 beginInsertRows(parent, row, row + count - 1);
253 for (int i = 0; i < count; ++i) {
254 mAddresses.insert(row, IMAddress());
255 }
256 endInsertRows();
257
258 return true;
259}
260
261bool IMModel::removeRows(int row, int count, const QModelIndex &parent)
262{
263 if (parent.isValid()) {
264 return false;
265 }
266
267 beginRemoveRows(parent, row, row + count - 1);
268 for (int i = 0; i < count; ++i) {
269 mAddresses.remove(row);
270 }
271 endRemoveRows();
272
273 return true;
274}
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Wed Jan 24 2024 00:00:00 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

akonadi/contact

Skip menu "akonadi/contact"
  • Main Page
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • Related Pages

kdepimlibs-4.14.10 API Reference

Skip menu "kdepimlibs-4.14.10 API Reference"
  • akonadi
  •   contact
  •   kmime
  •   socialutils
  • kabc
  • kalarmcal
  • kblog
  • kcal
  • kcalcore
  • kcalutils
  • kholidays
  • kimap
  • kioslave
  •   imap4
  •   mbox
  •   nntp
  • kldap
  • kmbox
  • kmime
  • kontactinterface
  • kpimidentities
  • kpimtextedit
  • kpimutils
  • kresources
  • ktnef
  • kxmlrpcclient
  • mailtransport
  • microblog
  • qgpgme
  • syndication
  •   atom
  •   rdf
  •   rss2
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