• Skip to content
  • Skip to link menu
  • KDE API Reference
  • kdepimlibs-4.9.4 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 <klocale.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, 0 );
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  if ( index.row() < 0 || index.row() >= mAddresses.count() )
109  return QVariant();
110 
111  if ( index.column() < 0 || index.column() > 1 )
112  return QVariant();
113 
114  const IMAddress &address = mAddresses[ index.row() ];
115 
116  if ( role == Qt::DisplayRole ) {
117  if ( index.column() == 0 )
118  return IMProtocols::self()->name( address.protocol() );
119  else
120  return address.name();
121  }
122 
123  if ( role == Qt::DecorationRole ) {
124  if ( index.column() == 1 )
125  return QVariant();
126 
127  return KIcon( IMProtocols::self()->icon( address.protocol() ) );
128  }
129 
130  if ( role == Qt::EditRole ) {
131  if ( index.column() == 0 )
132  return address.protocol();
133  else
134  return address.name();
135  }
136 
137  if ( role == ProtocolRole )
138  return address.protocol();
139 
140  if ( role == IsPreferredRole )
141  return address.preferred();
142 
143  return QVariant();
144 }
145 
146 bool IMModel::setData( const QModelIndex &index, const QVariant &value, int role )
147 {
148  if ( !index.isValid() )
149  return false;
150 
151  if ( index.row() < 0 || index.row() >= mAddresses.count() )
152  return false;
153 
154  if ( index.column() < 0 || index.column() > 1 )
155  return false;
156 
157  IMAddress &address = mAddresses[ index.row() ];
158 
159  if ( role == Qt::EditRole ) {
160  if ( index.column() == 1 ) {
161  address.setName( value.toString() );
162  emit dataChanged( index, index );
163  return true;
164  }
165  }
166 
167  if ( role == ProtocolRole ) {
168  address.setProtocol( value.toString() );
169  emit dataChanged( this->index( index.row(), 0 ), this->index( index.row(), 1 ) );
170  return true;
171  }
172 
173  if ( role == IsPreferredRole ) {
174  address.setPreferred( value.toBool() );
175  emit dataChanged( this->index( index.row(), 0 ), this->index( index.row(), 1 ) );
176  return true;
177  }
178 
179  return false;
180 }
181 
182 QVariant IMModel::headerData( int section, Qt::Orientation orientation, int role ) const
183 {
184  if ( section < 0 || section > 1 )
185  return QVariant();
186 
187  if ( orientation != Qt::Horizontal )
188  return QVariant();
189 
190  if ( role != Qt::DisplayRole )
191  return QVariant();
192 
193  if ( section == 0 )
194  return i18nc( "instant messaging protocol", "Protocol" );
195  else
196  return i18nc( "instant messaging address", "Address" );
197 }
198 
199 Qt::ItemFlags IMModel::flags( const QModelIndex &index ) const
200 {
201  if ( !index.isValid() || index.row() < 0 || index.row() >= mAddresses.count() )
202  return QAbstractItemModel::flags( index );
203 
204  const Qt::ItemFlags parentFlags = QAbstractItemModel::flags( index );
205  return (parentFlags | Qt::ItemIsEnabled | Qt::ItemIsEditable);
206 }
207 
208 int IMModel::columnCount( const QModelIndex &parent ) const
209 {
210  if ( !parent.isValid() )
211  return 2;
212  else
213  return 0;
214 }
215 
216 int IMModel::rowCount( const QModelIndex &parent ) const
217 {
218  if ( !parent.isValid() )
219  return mAddresses.count();
220  else
221  return 0;
222 }
223 
224 bool IMModel::insertRows( int row, int count, const QModelIndex &parent )
225 {
226  if ( parent.isValid() )
227  return false;
228 
229  beginInsertRows( parent, row, row + count - 1 );
230  for ( int i = 0; i < count; ++i )
231  mAddresses.insert( row, IMAddress() );
232  endInsertRows();
233 
234  return true;
235 }
236 
237 bool IMModel::removeRows( int row, int count, const QModelIndex &parent )
238 {
239  if ( parent.isValid() )
240  return false;
241 
242  beginRemoveRows( parent, row, row + count - 1 );
243  for ( int i = 0; i < count; ++i )
244  mAddresses.remove( row );
245  endRemoveRows();
246 
247  return true;
248 }
249 
This file is part of the KDE documentation.
Documentation copyright © 1996-2012 The KDE developers.
Generated on Tue Dec 11 2012 12:15:28 by doxygen 1.8.1.2 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.9.4 API Reference

Skip menu "kdepimlibs-4.9.4 API Reference"
  • akonadi
  •   contact
  •   kmime
  • kabc
  • kalarmcal
  • kblog
  • kcal
  • kcalcore
  • kcalutils
  • kholidays
  • kimap
  • kioslave
  •   imap4
  •   mbox
  •   nntp
  • kldap
  • kmbox
  • kmime
  • kontactinterface
  • kpimidentities
  • kpimtextedit
  •   richtextbuilders
  • 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