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

akonadi

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

Skip menu "kdepimlibs-4.11.3 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