24 #include "improtocols.h"
29 IMAddress::IMAddress()
30 : mProtocol( QLatin1String(
"messaging/aim" ) ), mPreferred( false )
34 IMAddress::IMAddress(
const QString &protocol,
const QString &name,
bool preferred )
35 : mProtocol( protocol ), mName( name ), mPreferred( preferred )
39 void IMAddress::setProtocol(
const QString &protocol )
44 QString IMAddress::protocol()
const
49 void IMAddress::setName(
const QString &name )
54 QString IMAddress::name()
const
59 void IMAddress::setPreferred(
bool preferred )
61 mPreferred = preferred;
64 bool IMAddress::preferred()
const
70 IMModel::IMModel( QObject *parent )
71 : QAbstractItemModel( parent )
79 void IMModel::setAddresses(
const IMAddress::List &addresses )
81 emit layoutAboutToBeChanged();
83 mAddresses = addresses;
88 IMAddress::List IMModel::addresses()
const
93 QModelIndex IMModel::index(
int row,
int column,
const QModelIndex& )
const
95 return createIndex( row, column, 0 );
98 QModelIndex IMModel::parent(
const QModelIndex& )
const
100 return QModelIndex();
103 QVariant IMModel::data(
const QModelIndex &index,
int role )
const
105 if ( !index.isValid() )
108 if ( index.row() < 0 || index.row() >= mAddresses.count() )
111 if ( index.column() < 0 || index.column() > 1 )
114 const IMAddress &address = mAddresses[ index.row() ];
116 if ( role == Qt::DisplayRole ) {
117 if ( index.column() == 0 )
118 return IMProtocols::self()->name( address.protocol() );
120 return address.name();
123 if ( role == Qt::DecorationRole ) {
124 if ( index.column() == 1 )
127 return KIcon( IMProtocols::self()->icon( address.protocol() ) );
130 if ( role == Qt::EditRole ) {
131 if ( index.column() == 0 )
132 return address.protocol();
134 return address.name();
137 if ( role == ProtocolRole )
138 return address.protocol();
140 if ( role == IsPreferredRole )
141 return address.preferred();
146 bool IMModel::setData(
const QModelIndex &index,
const QVariant &value,
int role )
148 if ( !index.isValid() )
151 if ( index.row() < 0 || index.row() >= mAddresses.count() )
154 if ( index.column() < 0 || index.column() > 1 )
157 IMAddress &address = mAddresses[ index.row() ];
159 if ( role == Qt::EditRole ) {
160 if ( index.column() == 1 ) {
161 address.setName( value.toString() );
162 emit dataChanged( index, index );
167 if ( role == ProtocolRole ) {
168 address.setProtocol( value.toString() );
169 emit dataChanged( this->index( index.row(), 0 ), this->index( index.row(), 1 ) );
173 if ( role == IsPreferredRole ) {
174 address.setPreferred( value.toBool() );
175 emit dataChanged( this->index( index.row(), 0 ), this->index( index.row(), 1 ) );
182 QVariant IMModel::headerData(
int section, Qt::Orientation orientation,
int role )
const
184 if ( section < 0 || section > 1 )
187 if ( orientation != Qt::Horizontal )
190 if ( role != Qt::DisplayRole )
194 return i18nc(
"instant messaging protocol",
"Protocol" );
196 return i18nc(
"instant messaging address",
"Address" );
199 Qt::ItemFlags IMModel::flags(
const QModelIndex &index )
const
201 if ( !index.isValid() || index.row() < 0 || index.row() >= mAddresses.count() )
202 return QAbstractItemModel::flags( index );
204 const Qt::ItemFlags parentFlags = QAbstractItemModel::flags( index );
205 return (parentFlags | Qt::ItemIsEnabled | Qt::ItemIsEditable);
208 int IMModel::columnCount(
const QModelIndex &parent )
const
210 if ( !parent.isValid() )
216 int IMModel::rowCount(
const QModelIndex &parent )
const
218 if ( !parent.isValid() )
219 return mAddresses.count();
224 bool IMModel::insertRows(
int row,
int count,
const QModelIndex &parent )
226 if ( parent.isValid() )
229 beginInsertRows( parent, row, row + count - 1 );
230 for (
int i = 0; i < count; ++i )
231 mAddresses.insert( row, IMAddress() );
237 bool IMModel::removeRows(
int row,
int count,
const QModelIndex &parent )
239 if ( parent.isValid() )
242 beginRemoveRows( parent, row, row + count - 1 );
243 for (
int i = 0; i < count; ++i )
244 mAddresses.remove( row );