24 #include "improtocols.h" 
   27 #include <klocalizedstring.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 
   69 IMModel::IMModel( QObject *parent )
 
   70   : QAbstractItemModel( parent )
 
   78 void IMModel::setAddresses( 
const IMAddress::List &addresses )
 
   80   emit layoutAboutToBeChanged();
 
   82   mAddresses = addresses;
 
   87 IMAddress::List IMModel::addresses()
 const 
   92 QModelIndex IMModel::index( 
int row, 
int column, 
const QModelIndex& )
 const 
   94   return createIndex( row, column );
 
   97 QModelIndex IMModel::parent( 
const QModelIndex& )
 const 
  102 QVariant IMModel::data( 
const QModelIndex &index, 
int role )
 const 
  104   if ( !index.isValid() ) {
 
  108   if ( index.row() < 0 || index.row() >= mAddresses.count() ) {
 
  112   if ( index.column() < 0 || index.column() > 1 ) {
 
  116   const IMAddress &address = mAddresses[ index.row() ];
 
  118   if ( role == Qt::DisplayRole ) {
 
  119     if ( index.column() == 0 ) {
 
  120       return IMProtocols::self()->name( address.protocol() );
 
  122       return address.name();
 
  126   if ( role == Qt::DecorationRole ) {
 
  127     if ( index.column() == 1 ) {
 
  131     return KIcon( IMProtocols::self()->icon( address.protocol() ) );
 
  134   if ( role == Qt::EditRole ) {
 
  135     if ( index.column() == 0 ) {
 
  136       return address.protocol();
 
  138       return address.name();
 
  142   if ( role == ProtocolRole ) {
 
  143     return address.protocol();
 
  146   if ( role == IsPreferredRole ) {
 
  147     return address.preferred();
 
  153 bool IMModel::setData( 
const QModelIndex &index, 
const QVariant &value, 
int role )
 
  155   if ( !index.isValid() ) {
 
  159   if ( index.row() < 0 || index.row() >= mAddresses.count() ) {
 
  163   if ( index.column() < 0 || index.column() > 1 ) {
 
  167   IMAddress &address = mAddresses[ index.row() ];
 
  169   if ( role == Qt::EditRole ) {
 
  170     if ( index.column() == 1 ) {
 
  171       address.setName( value.toString() );
 
  172       emit dataChanged( index, index );
 
  177   if ( role == ProtocolRole ) {
 
  178     address.setProtocol( value.toString() );
 
  179     emit dataChanged( this->index( index.row(), 0 ), this->index( index.row(), 1 ) );
 
  183   if ( role == IsPreferredRole ) {
 
  184     address.setPreferred( value.toBool() );
 
  185     emit dataChanged( this->index( index.row(), 0 ), this->index( index.row(), 1 ) );
 
  192 QVariant IMModel::headerData( 
int section, Qt::Orientation orientation, 
int role )
 const 
  194   if ( section < 0 || section > 1 ) {
 
  198   if ( orientation != Qt::Horizontal ) {
 
  202   if ( role != Qt::DisplayRole ) {
 
  206   if ( section == 0 ) {
 
  207     return i18nc( 
"instant messaging protocol", 
"Protocol" );
 
  209     return i18nc( 
"instant messaging address", 
"Address" );
 
  213 Qt::ItemFlags IMModel::flags( 
const QModelIndex &index )
 const 
  215   if ( !index.isValid() || index.row() < 0 || index.row() >= mAddresses.count() ) {
 
  216     return QAbstractItemModel::flags( index );
 
  219   const Qt::ItemFlags parentFlags = QAbstractItemModel::flags( index );
 
  220   return ( parentFlags | Qt::ItemIsEnabled | Qt::ItemIsEditable );
 
  223 int IMModel::columnCount( 
const QModelIndex &parent )
 const 
  225   if ( !parent.isValid() ) {
 
  232 int IMModel::rowCount( 
const QModelIndex &parent )
 const 
  234   if ( !parent.isValid() ) {
 
  235     return mAddresses.count();
 
  241 bool IMModel::insertRows( 
int row, 
int count, 
const QModelIndex &parent )
 
  243   if ( parent.isValid() ) {
 
  247   beginInsertRows( parent, row, row + count - 1 );
 
  248   for ( 
int i = 0; i < count; ++i ) {
 
  249     mAddresses.insert( row, IMAddress() );
 
  256 bool IMModel::removeRows( 
int row, 
int count, 
const QModelIndex &parent )
 
  258   if ( parent.isValid() ) {
 
  262   beginRemoveRows( parent, row, row + count - 1 );
 
  263   for ( 
int i = 0; i < count; ++i ) {
 
  264     mAddresses.remove( row );