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() ) {
109 if ( index.row() < 0 || index.row() >= mAddresses.count() ) {
113 if ( index.column() < 0 || index.column() > 1 ) {
117 const IMAddress &address = mAddresses[ index.row() ];
119 if ( role == Qt::DisplayRole ) {
120 if ( index.column() == 0 ) {
121 return IMProtocols::self()->name( address.protocol() );
123 return address.name();
127 if ( role == Qt::DecorationRole ) {
128 if ( index.column() == 1 ) {
132 return KIcon( IMProtocols::self()->icon( address.protocol() ) );
135 if ( role == Qt::EditRole ) {
136 if ( index.column() == 0 ) {
137 return address.protocol();
139 return address.name();
143 if ( role == ProtocolRole ) {
144 return address.protocol();
147 if ( role == IsPreferredRole ) {
148 return address.preferred();
154 bool IMModel::setData(
const QModelIndex &index,
const QVariant &value,
int role )
156 if ( !index.isValid() ) {
160 if ( index.row() < 0 || index.row() >= mAddresses.count() ) {
164 if ( index.column() < 0 || index.column() > 1 ) {
168 IMAddress &address = mAddresses[ index.row() ];
170 if ( role == Qt::EditRole ) {
171 if ( index.column() == 1 ) {
172 address.setName( value.toString() );
173 emit dataChanged( index, index );
178 if ( role == ProtocolRole ) {
179 address.setProtocol( value.toString() );
180 emit dataChanged( this->index( index.row(), 0 ), this->index( index.row(), 1 ) );
184 if ( role == IsPreferredRole ) {
185 address.setPreferred( value.toBool() );
186 emit dataChanged( this->index( index.row(), 0 ), this->index( index.row(), 1 ) );
193 QVariant IMModel::headerData(
int section, Qt::Orientation orientation,
int role )
const
195 if ( section < 0 || section > 1 ) {
199 if ( orientation != Qt::Horizontal ) {
203 if ( role != Qt::DisplayRole ) {
207 if ( section == 0 ) {
208 return i18nc(
"instant messaging protocol",
"Protocol" );
210 return i18nc(
"instant messaging address",
"Address" );
214 Qt::ItemFlags IMModel::flags(
const QModelIndex &index )
const
216 if ( !index.isValid() || index.row() < 0 || index.row() >= mAddresses.count() ) {
217 return QAbstractItemModel::flags( index );
220 const Qt::ItemFlags parentFlags = QAbstractItemModel::flags( index );
221 return ( parentFlags | Qt::ItemIsEnabled | Qt::ItemIsEditable );
224 int IMModel::columnCount(
const QModelIndex &parent )
const
226 if ( !parent.isValid() ) {
233 int IMModel::rowCount(
const QModelIndex &parent )
const
235 if ( !parent.isValid() ) {
236 return mAddresses.count();
242 bool IMModel::insertRows(
int row,
int count,
const QModelIndex &parent )
244 if ( parent.isValid() ) {
248 beginInsertRows( parent, row, row + count - 1 );
249 for (
int i = 0; i < count; ++i ) {
250 mAddresses.insert( row, IMAddress() );
257 bool IMModel::removeRows(
int row,
int count,
const QModelIndex &parent )
259 if ( parent.isValid() ) {
263 beginRemoveRows( parent, row, row + count - 1 );
264 for (
int i = 0; i < count; ++i ) {
265 mAddresses.remove( row );