00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
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