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

akonadi

  • sources
  • kde-4.12
  • kdepimlibs
  • 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 
29 IMAddress::IMAddress()
30  : mProtocol( QLatin1String( "messaging/aim" ) ), mPreferred( false )
31 {
32 }
33 
34 IMAddress::IMAddress( const QString &protocol, const QString &name, bool preferred )
35  : mProtocol( protocol ), mName( name ), mPreferred( preferred )
36 {
37 }
38 
39 void IMAddress::setProtocol( const QString &protocol )
40 {
41  mProtocol = protocol;
42 }
43 
44 QString IMAddress::protocol() const
45 {
46  return mProtocol;
47 }
48 
49 void IMAddress::setName( const QString &name )
50 {
51  mName = name;
52 }
53 
54 QString IMAddress::name() const
55 {
56  return mName;
57 }
58 
59 void IMAddress::setPreferred( bool preferred )
60 {
61  mPreferred = preferred;
62 }
63 
64 bool IMAddress::preferred() const
65 {
66  return mPreferred;
67 }
68 
69 IMModel::IMModel( QObject *parent )
70  : QAbstractItemModel( parent )
71 {
72 }
73 
74 IMModel::~IMModel()
75 {
76 }
77 
78 void IMModel::setAddresses( const IMAddress::List &addresses )
79 {
80  emit layoutAboutToBeChanged();
81 
82  mAddresses = addresses;
83 
84  emit layoutChanged();
85 }
86 
87 IMAddress::List IMModel::addresses() const
88 {
89  return mAddresses;
90 }
91 
92 QModelIndex IMModel::index( int row, int column, const QModelIndex& ) const
93 {
94  return createIndex( row, column );
95 }
96 
97 QModelIndex IMModel::parent( const QModelIndex& ) const
98 {
99  return QModelIndex();
100 }
101 
102 QVariant IMModel::data( const QModelIndex &index, int role ) const
103 {
104  if ( !index.isValid() ) {
105  return QVariant();
106  }
107 
108  if ( index.row() < 0 || index.row() >= mAddresses.count() ) {
109  return QVariant();
110  }
111 
112  if ( index.column() < 0 || index.column() > 1 ) {
113  return QVariant();
114  }
115 
116  const IMAddress &address = mAddresses[ index.row() ];
117 
118  if ( role == Qt::DisplayRole ) {
119  if ( index.column() == 0 ) {
120  return IMProtocols::self()->name( address.protocol() );
121  } else {
122  return address.name();
123  }
124  }
125 
126  if ( role == Qt::DecorationRole ) {
127  if ( index.column() == 1 ) {
128  return QVariant();
129  }
130 
131  return KIcon( IMProtocols::self()->icon( address.protocol() ) );
132  }
133 
134  if ( role == Qt::EditRole ) {
135  if ( index.column() == 0 ) {
136  return address.protocol();
137  } else {
138  return address.name();
139  }
140  }
141 
142  if ( role == ProtocolRole ) {
143  return address.protocol();
144  }
145 
146  if ( role == IsPreferredRole ) {
147  return address.preferred();
148  }
149 
150  return QVariant();
151 }
152 
153 bool IMModel::setData( const QModelIndex &index, const QVariant &value, int role )
154 {
155  if ( !index.isValid() ) {
156  return false;
157  }
158 
159  if ( index.row() < 0 || index.row() >= mAddresses.count() ) {
160  return false;
161  }
162 
163  if ( index.column() < 0 || index.column() > 1 ) {
164  return false;
165  }
166 
167  IMAddress &address = mAddresses[ index.row() ];
168 
169  if ( role == Qt::EditRole ) {
170  if ( index.column() == 1 ) {
171  address.setName( value.toString() );
172  emit dataChanged( index, index );
173  return true;
174  }
175  }
176 
177  if ( role == ProtocolRole ) {
178  address.setProtocol( value.toString() );
179  emit dataChanged( this->index( index.row(), 0 ), this->index( index.row(), 1 ) );
180  return true;
181  }
182 
183  if ( role == IsPreferredRole ) {
184  address.setPreferred( value.toBool() );
185  emit dataChanged( this->index( index.row(), 0 ), this->index( index.row(), 1 ) );
186  return true;
187  }
188 
189  return false;
190 }
191 
192 QVariant IMModel::headerData( int section, Qt::Orientation orientation, int role ) const
193 {
194  if ( section < 0 || section > 1 ) {
195  return QVariant();
196  }
197 
198  if ( orientation != Qt::Horizontal ) {
199  return QVariant();
200  }
201 
202  if ( role != Qt::DisplayRole ) {
203  return QVariant();
204  }
205 
206  if ( section == 0 ) {
207  return i18nc( "instant messaging protocol", "Protocol" );
208  } else {
209  return i18nc( "instant messaging address", "Address" );
210  }
211 }
212 
213 Qt::ItemFlags IMModel::flags( const QModelIndex &index ) const
214 {
215  if ( !index.isValid() || index.row() < 0 || index.row() >= mAddresses.count() ) {
216  return QAbstractItemModel::flags( index );
217  }
218 
219  const Qt::ItemFlags parentFlags = QAbstractItemModel::flags( index );
220  return ( parentFlags | Qt::ItemIsEnabled | Qt::ItemIsEditable );
221 }
222 
223 int IMModel::columnCount( const QModelIndex &parent ) const
224 {
225  if ( !parent.isValid() ) {
226  return 2;
227  } else {
228  return 0;
229  }
230 }
231 
232 int IMModel::rowCount( const QModelIndex &parent ) const
233 {
234  if ( !parent.isValid() ) {
235  return mAddresses.count();
236  } else {
237  return 0;
238  }
239 }
240 
241 bool IMModel::insertRows( int row, int count, const QModelIndex &parent )
242 {
243  if ( parent.isValid() ) {
244  return false;
245  }
246 
247  beginInsertRows( parent, row, row + count - 1 );
248  for ( int i = 0; i < count; ++i ) {
249  mAddresses.insert( row, IMAddress() );
250  }
251  endInsertRows();
252 
253  return true;
254 }
255 
256 bool IMModel::removeRows( int row, int count, const QModelIndex &parent )
257 {
258  if ( parent.isValid() ) {
259  return false;
260  }
261 
262  beginRemoveRows( parent, row, row + count - 1 );
263  for ( int i = 0; i < count; ++i ) {
264  mAddresses.remove( row );
265  }
266  endRemoveRows();
267 
268  return true;
269 }
270 
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 23:00:27 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

akonadi

Skip menu "akonadi"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • Modules
  • Related Pages

kdepimlibs API Reference

Skip menu "kdepimlibs API Reference"
  • akonadi
  •   contact
  •   kmime
  •   socialutils
  • kabc
  • kalarmcal
  • kblog
  • kcal
  • kcalcore
  • kcalutils
  • kholidays
  • kimap
  • kldap
  • kmbox
  • kmime
  • kpimidentities
  • kpimtextedit
  • kresources
  • ktnef
  • kxmlrpcclient
  • microblog

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