akonadi
agenttypemodel.cpp
00001 /* 00002 Copyright (c) 2006 Tobias Koenig <tokoe@kde.org> 00003 00004 This library is free software; you can redistribute it and/or modify it 00005 under the terms of the GNU Library General Public License as published by 00006 the Free Software Foundation; either version 2 of the License, or (at your 00007 option) any later version. 00008 00009 This library is distributed in the hope that it will be useful, but WITHOUT 00010 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 00011 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public 00012 License for more details. 00013 00014 You should have received a copy of the GNU Library General Public License 00015 along with this library; see the file COPYING.LIB. If not, write to the 00016 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 00017 02110-1301, USA. 00018 */ 00019 00020 #include "agenttypemodel.h" 00021 #include "agenttype.h" 00022 #include "agentmanager.h" 00023 00024 #include <QtCore/QStringList> 00025 #include <QtGui/QIcon> 00026 00027 using namespace Akonadi; 00028 00032 class AgentTypeModel::Private 00033 { 00034 public: 00035 Private( AgentTypeModel *parent ) 00036 : mParent( parent ) 00037 { 00038 mTypes = AgentManager::self()->types(); 00039 } 00040 00041 AgentTypeModel *mParent; 00042 AgentType::List mTypes; 00043 00044 void typeAdded( const AgentType &agentType ); 00045 void typeRemoved( const AgentType &agentType ); 00046 }; 00047 00048 void AgentTypeModel::Private::typeAdded( const AgentType &agentType ) 00049 { 00050 mTypes.append( agentType ); 00051 00052 emit mParent->layoutChanged(); 00053 } 00054 00055 void AgentTypeModel::Private::typeRemoved( const AgentType &agentType ) 00056 { 00057 mTypes.removeAll( agentType ); 00058 00059 emit mParent->layoutChanged(); 00060 } 00061 00062 AgentTypeModel::AgentTypeModel( QObject *parent ) 00063 : QAbstractItemModel( parent ), d( new Private( this ) ) 00064 { 00065 connect( AgentManager::self(), SIGNAL(typeAdded(Akonadi::AgentType)), 00066 this, SLOT(typeAdded(Akonadi::AgentType)) ); 00067 connect( AgentManager::self(), SIGNAL(typeRemoved(Akonadi::AgentType)), 00068 this, SLOT(typeRemoved(Akonadi::AgentType)) ); 00069 } 00070 00071 AgentTypeModel::~AgentTypeModel() 00072 { 00073 delete d; 00074 } 00075 00076 int AgentTypeModel::columnCount( const QModelIndex& ) const 00077 { 00078 return 1; 00079 } 00080 00081 int AgentTypeModel::rowCount( const QModelIndex& ) const 00082 { 00083 return d->mTypes.count(); 00084 } 00085 00086 QVariant AgentTypeModel::data( const QModelIndex &index, int role ) const 00087 { 00088 if ( !index.isValid() ) 00089 return QVariant(); 00090 00091 if ( index.row() < 0 || index.row() >= d->mTypes.count() ) 00092 return QVariant(); 00093 00094 const AgentType &type = d->mTypes[ index.row() ]; 00095 00096 switch ( role ) { 00097 case Qt::DisplayRole: 00098 return type.name(); 00099 break; 00100 case Qt::DecorationRole: 00101 return type.icon(); 00102 break; 00103 case TypeRole: 00104 { 00105 QVariant var; 00106 var.setValue( type ); 00107 return var; 00108 } 00109 break; 00110 case IdentifierRole: 00111 return type.identifier(); 00112 break; 00113 case DescriptionRole: 00114 return type.description(); 00115 break; 00116 case MimeTypesRole: 00117 return type.mimeTypes(); 00118 break; 00119 case CapabilitiesRole: 00120 return type.capabilities(); 00121 break; 00122 default: 00123 break; 00124 } 00125 return QVariant(); 00126 } 00127 00128 QModelIndex AgentTypeModel::index( int row, int column, const QModelIndex& ) const 00129 { 00130 if ( row < 0 || row >= d->mTypes.count() ) 00131 return QModelIndex(); 00132 00133 if ( column != 0 ) 00134 return QModelIndex(); 00135 00136 return createIndex( row, column, 0 ); 00137 } 00138 00139 QModelIndex AgentTypeModel::parent( const QModelIndex& ) const 00140 { 00141 return QModelIndex(); 00142 } 00143 00144 Qt::ItemFlags AgentTypeModel::flags(const QModelIndex& index) const 00145 { 00146 if ( !index.isValid() || index.row() < 0 || index.row() >= d->mTypes.count() ) 00147 return QAbstractItemModel::flags( index ); 00148 00149 const AgentType &type = d->mTypes[ index.row() ]; 00150 if ( type.capabilities().contains( QLatin1String( "Unique" ) ) && 00151 AgentManager::self()->instance( type.identifier() ).isValid() ) 00152 { 00153 return QAbstractItemModel::flags( index ) & ~(Qt::ItemIsSelectable | Qt::ItemIsEnabled); 00154 } 00155 return QAbstractItemModel::flags(index); 00156 } 00157 00158 #include "agenttypemodel.moc"
This file is part of the KDE documentation.
Documentation copyright © 1996-2012 The KDE developers.
Generated on Thu May 10 2012 22:18:30 by doxygen 1.8.0 written by Dimitri van Heesch, © 1997-2006
Documentation copyright © 1996-2012 The KDE developers.
Generated on Thu May 10 2012 22:18:30 by doxygen 1.8.0 written by Dimitri van Heesch, © 1997-2006
KDE's Doxygen guidelines are available online.