akonadi/contact
immodel.cpp
00001 /* 00002 This file is part of Akonadi Contact. 00003 00004 Copyright (c) 2010 Tobias Koenig <tokoe@kde.org> 00005 00006 This library is free software; you can redistribute it and/or modify it 00007 under the terms of the GNU Library General Public License as published by 00008 the Free Software Foundation; either version 2 of the License, or (at your 00009 option) any later version. 00010 00011 This library is distributed in the hope that it will be useful, but WITHOUT 00012 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 00013 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public 00014 License for more details. 00015 00016 You should have received a copy of the GNU Library General Public License 00017 along with this library; see the file COPYING.LIB. If not, write to the 00018 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 00019 02110-1301, USA. 00020 */ 00021 00022 #include "immodel.h" 00023 00024 #include "improtocols.h" 00025 00026 #include <kicon.h> 00027 #include <klocale.h> 00028 00029 IMAddress::IMAddress() 00030 : mProtocol( QLatin1String( "messaging/aim" ) ), mPreferred( false ) 00031 { 00032 } 00033 00034 IMAddress::IMAddress( const QString &protocol, const QString &name, bool preferred ) 00035 : mProtocol( protocol ), mName( name ), mPreferred( preferred ) 00036 { 00037 } 00038 00039 void IMAddress::setProtocol( const QString &protocol ) 00040 { 00041 mProtocol = protocol; 00042 } 00043 00044 QString IMAddress::protocol() const 00045 { 00046 return mProtocol; 00047 } 00048 00049 void IMAddress::setName( const QString &name ) 00050 { 00051 mName = name; 00052 } 00053 00054 QString IMAddress::name() const 00055 { 00056 return mName; 00057 } 00058 00059 void IMAddress::setPreferred( bool preferred ) 00060 { 00061 mPreferred = preferred; 00062 } 00063 00064 bool IMAddress::preferred() const 00065 { 00066 return mPreferred; 00067 } 00068 00069 00070 IMModel::IMModel( QObject *parent ) 00071 : QAbstractItemModel( parent ) 00072 { 00073 } 00074 00075 IMModel::~IMModel() 00076 { 00077 } 00078 00079 void IMModel::setAddresses( const IMAddress::List &addresses ) 00080 { 00081 emit layoutAboutToBeChanged(); 00082 00083 mAddresses = addresses; 00084 00085 emit layoutChanged(); 00086 } 00087 00088 IMAddress::List IMModel::addresses() const 00089 { 00090 return mAddresses; 00091 } 00092 00093 QModelIndex IMModel::index( int row, int column, const QModelIndex& ) const 00094 { 00095 return createIndex( row, column, 0 ); 00096 } 00097 00098 QModelIndex IMModel::parent( const QModelIndex& ) const 00099 { 00100 return QModelIndex(); 00101 } 00102 00103 QVariant IMModel::data( const QModelIndex &index, int role ) const 00104 { 00105 if ( !index.isValid() ) 00106 return QVariant(); 00107 00108 if ( index.row() < 0 || index.row() >= mAddresses.count() ) 00109 return QVariant(); 00110 00111 if ( index.column() < 0 || index.column() > 1 ) 00112 return QVariant(); 00113 00114 const IMAddress &address = mAddresses[ index.row() ]; 00115 00116 if ( role == Qt::DisplayRole ) { 00117 if ( index.column() == 0 ) 00118 return IMProtocols::self()->name( address.protocol() ); 00119 else 00120 return address.name(); 00121 } 00122 00123 if ( role == Qt::DecorationRole ) { 00124 if ( index.column() == 1 ) 00125 return QVariant(); 00126 00127 return KIcon( IMProtocols::self()->icon( address.protocol() ) ); 00128 } 00129 00130 if ( role == Qt::EditRole ) { 00131 if ( index.column() == 0 ) 00132 return address.protocol(); 00133 else 00134 return address.name(); 00135 } 00136 00137 if ( role == ProtocolRole ) 00138 return address.protocol(); 00139 00140 if ( role == IsPreferredRole ) 00141 return address.preferred(); 00142 00143 return QVariant(); 00144 } 00145 00146 bool IMModel::setData( const QModelIndex &index, const QVariant &value, int role ) 00147 { 00148 if ( !index.isValid() ) 00149 return false; 00150 00151 if ( index.row() < 0 || index.row() >= mAddresses.count() ) 00152 return false; 00153 00154 if ( index.column() < 0 || index.column() > 1 ) 00155 return false; 00156 00157 IMAddress &address = mAddresses[ index.row() ]; 00158 00159 if ( role == Qt::EditRole ) { 00160 if ( index.column() == 1 ) { 00161 address.setName( value.toString() ); 00162 emit dataChanged( index, index ); 00163 return true; 00164 } 00165 } 00166 00167 if ( role == ProtocolRole ) { 00168 address.setProtocol( value.toString() ); 00169 emit dataChanged( this->index( index.row(), 0 ), this->index( index.row(), 1 ) ); 00170 return true; 00171 } 00172 00173 if ( role == IsPreferredRole ) { 00174 address.setPreferred( value.toBool() ); 00175 emit dataChanged( this->index( index.row(), 0 ), this->index( index.row(), 1 ) ); 00176 return true; 00177 } 00178 00179 return false; 00180 } 00181 00182 QVariant IMModel::headerData( int section, Qt::Orientation orientation, int role ) const 00183 { 00184 if ( section < 0 || section > 1 ) 00185 return QVariant(); 00186 00187 if ( orientation != Qt::Horizontal ) 00188 return QVariant(); 00189 00190 if ( role != Qt::DisplayRole ) 00191 return QVariant(); 00192 00193 if ( section == 0 ) 00194 return i18nc( "instant messaging protocol", "Protocol" ); 00195 else 00196 return i18nc( "instant messaging address", "Address" ); 00197 } 00198 00199 Qt::ItemFlags IMModel::flags( const QModelIndex &index ) const 00200 { 00201 if ( !index.isValid() || index.row() < 0 || index.row() >= mAddresses.count() ) 00202 return QAbstractItemModel::flags( index ); 00203 00204 const Qt::ItemFlags parentFlags = QAbstractItemModel::flags( index ); 00205 return (parentFlags | Qt::ItemIsEnabled | Qt::ItemIsEditable); 00206 } 00207 00208 int IMModel::columnCount( const QModelIndex &parent ) const 00209 { 00210 if ( !parent.isValid() ) 00211 return 2; 00212 else 00213 return 0; 00214 } 00215 00216 int IMModel::rowCount( const QModelIndex &parent ) const 00217 { 00218 if ( !parent.isValid() ) 00219 return mAddresses.count(); 00220 else 00221 return 0; 00222 } 00223 00224 bool IMModel::insertRows( int row, int count, const QModelIndex &parent ) 00225 { 00226 if ( parent.isValid() ) 00227 return false; 00228 00229 beginInsertRows( parent, row, row + count - 1 ); 00230 for ( int i = 0; i < count; ++i ) 00231 mAddresses.insert( row, IMAddress() ); 00232 endInsertRows(); 00233 00234 return true; 00235 } 00236 00237 bool IMModel::removeRows( int row, int count, const QModelIndex &parent ) 00238 { 00239 if ( parent.isValid() ) 00240 return false; 00241 00242 beginRemoveRows( parent, row, row + count - 1 ); 00243 for ( int i = 0; i < count; ++i ) 00244 mAddresses.remove( row ); 00245 endRemoveRows(); 00246 00247 return true; 00248 } 00249
This file is part of the KDE documentation.
Documentation copyright © 1996-2012 The KDE developers.
Generated on Tue May 8 2012 00:02:51 by doxygen 1.8.0 written by Dimitri van Heesch, © 1997-2006
Documentation copyright © 1996-2012 The KDE developers.
Generated on Tue May 8 2012 00:02:51 by doxygen 1.8.0 written by Dimitri van Heesch, © 1997-2006
KDE's Doxygen guidelines are available online.