File indexing completed on 2024-11-10 04:40:33
0001 /* 0002 SPDX-FileCopyrightText: 2006 Tobias Koenig <tokoe@kde.org> 0003 0004 SPDX-License-Identifier: LGPL-2.0-or-later 0005 */ 0006 0007 #include "agentinstancemodel.h" 0008 0009 #include "agentinstance.h" 0010 #include "agentmanager.h" 0011 0012 #include <QIcon> 0013 0014 #include <KLocalizedString> 0015 0016 using namespace Akonadi; 0017 0018 /** 0019 * @internal 0020 */ 0021 class Akonadi::AgentInstanceModelPrivate 0022 { 0023 public: 0024 explicit AgentInstanceModelPrivate(AgentInstanceModel *parent) 0025 : mParent(parent) 0026 { 0027 } 0028 0029 AgentInstanceModel *const mParent; 0030 AgentInstance::List mInstances; 0031 0032 void instanceAdded(const AgentInstance & /*instance*/); 0033 void instanceRemoved(const AgentInstance & /*instance*/); 0034 void instanceChanged(const AgentInstance & /*instance*/); 0035 }; 0036 0037 void AgentInstanceModelPrivate::instanceAdded(const AgentInstance &instance) 0038 { 0039 mParent->beginInsertRows(QModelIndex(), mInstances.count(), mInstances.count()); 0040 mInstances.append(instance); 0041 mParent->endInsertRows(); 0042 } 0043 0044 void AgentInstanceModelPrivate::instanceRemoved(const AgentInstance &instance) 0045 { 0046 const int index = mInstances.indexOf(instance); 0047 if (index == -1) { 0048 return; 0049 } 0050 0051 mParent->beginRemoveRows(QModelIndex(), index, index); 0052 mInstances.removeAll(instance); 0053 mParent->endRemoveRows(); 0054 } 0055 0056 void AgentInstanceModelPrivate::instanceChanged(const AgentInstance &instance) 0057 { 0058 const int numberOfInstance(mInstances.count()); 0059 for (int i = 0; i < numberOfInstance; ++i) { 0060 if (mInstances[i] == instance) { 0061 // TODO why reassign it if equals ? 0062 mInstances[i] = instance; 0063 0064 const QModelIndex idx = mParent->index(i, 0); 0065 Q_EMIT mParent->dataChanged(idx, idx); 0066 0067 return; 0068 } 0069 } 0070 } 0071 0072 AgentInstanceModel::AgentInstanceModel(QObject *parent) 0073 : QAbstractItemModel(parent) 0074 , d(new AgentInstanceModelPrivate(this)) 0075 { 0076 d->mInstances = AgentManager::self()->instances(); 0077 0078 connect(AgentManager::self(), &AgentManager::instanceAdded, this, [this](const Akonadi::AgentInstance &inst) { 0079 d->instanceAdded(inst); 0080 }); 0081 connect(AgentManager::self(), &AgentManager::instanceRemoved, this, [this](const Akonadi::AgentInstance &inst) { 0082 d->instanceRemoved(inst); 0083 }); 0084 connect(AgentManager::self(), &AgentManager::instanceStatusChanged, this, [this](const Akonadi::AgentInstance &inst) { 0085 d->instanceChanged(inst); 0086 }); 0087 connect(AgentManager::self(), &AgentManager::instanceProgressChanged, this, [this](const Akonadi::AgentInstance &inst) { 0088 d->instanceChanged(inst); 0089 }); 0090 connect(AgentManager::self(), &AgentManager::instanceNameChanged, this, [this](const Akonadi::AgentInstance &inst) { 0091 d->instanceChanged(inst); 0092 }); 0093 connect(AgentManager::self(), &AgentManager::instanceOnline, this, [this](const Akonadi::AgentInstance &inst) { 0094 d->instanceChanged(inst); 0095 }); 0096 } 0097 0098 AgentInstanceModel::~AgentInstanceModel() = default; 0099 0100 QHash<int, QByteArray> AgentInstanceModel::roleNames() const 0101 { 0102 QHash<int, QByteArray> roles = QAbstractItemModel::roleNames(); 0103 roles.insert(NameRole, "name"); 0104 roles.insert(StatusRole, "status"); 0105 roles.insert(StatusMessageRole, "statusMessage"); 0106 roles.insert(ProgressRole, "progress"); 0107 roles.insert(OnlineRole, "online"); 0108 roles.insert(IconNameRole, "iconName"); 0109 return roles; 0110 } 0111 0112 int AgentInstanceModel::columnCount(const QModelIndex &index) const 0113 { 0114 return index.isValid() ? 0 : 1; 0115 } 0116 0117 int AgentInstanceModel::rowCount(const QModelIndex &index) const 0118 { 0119 return index.isValid() ? 0 : d->mInstances.count(); 0120 } 0121 0122 QVariant AgentInstanceModel::data(const QModelIndex &index, int role) const 0123 { 0124 if (!index.isValid()) { 0125 return QVariant(); 0126 } 0127 0128 if (index.row() < 0 || index.row() >= d->mInstances.count()) { 0129 return QVariant(); 0130 } 0131 0132 const AgentInstance &instance = d->mInstances[index.row()]; 0133 0134 switch (role) { 0135 case Qt::DisplayRole: 0136 case NameRole: 0137 return instance.name(); 0138 case Qt::DecorationRole: 0139 return instance.type().icon(); 0140 case IconNameRole: 0141 return instance.type().icon().name(); 0142 case InstanceRole: { 0143 QVariant var; 0144 var.setValue(instance); 0145 return var; 0146 } 0147 case InstanceIdentifierRole: 0148 return instance.identifier(); 0149 case Qt::ToolTipRole: 0150 return QStringLiteral("<qt><h4>%1</h4>%2</qt>").arg(instance.name(), instance.type().description()); 0151 case StatusRole: 0152 return instance.status(); 0153 case StatusMessageRole: 0154 return instance.statusMessage(); 0155 case ProgressRole: 0156 return instance.progress(); 0157 case OnlineRole: 0158 return instance.isOnline(); 0159 case TypeRole: { 0160 QVariant var; 0161 var.setValue(instance.type()); 0162 return var; 0163 } 0164 case TypeIdentifierRole: 0165 return instance.type().identifier(); 0166 case DescriptionRole: 0167 return instance.type().description(); 0168 case CapabilitiesRole: 0169 return instance.type().capabilities(); 0170 case MimeTypesRole: 0171 return instance.type().mimeTypes(); 0172 } 0173 return QVariant(); 0174 } 0175 0176 QVariant AgentInstanceModel::headerData(int section, Qt::Orientation orientation, int role) const 0177 { 0178 if (orientation == Qt::Vertical) { 0179 return QVariant(); 0180 } 0181 0182 if (role != Qt::DisplayRole) { 0183 return QVariant(); 0184 } 0185 0186 switch (section) { 0187 case 0: 0188 return i18nc("@title:column, name of a thing", "Name"); 0189 default: 0190 return QVariant(); 0191 } 0192 } 0193 0194 QModelIndex AgentInstanceModel::index(int row, int column, const QModelIndex & /*parent*/) const 0195 { 0196 if (row < 0 || row >= d->mInstances.count()) { 0197 return QModelIndex(); 0198 } 0199 0200 if (column != 0) { 0201 return QModelIndex(); 0202 } 0203 0204 return createIndex(row, column); 0205 } 0206 0207 QModelIndex AgentInstanceModel::parent(const QModelIndex & /*child*/) const 0208 { 0209 return QModelIndex(); 0210 } 0211 0212 Qt::ItemFlags AgentInstanceModel::flags(const QModelIndex &index) const 0213 { 0214 if (!index.isValid() || index.row() < 0 || index.row() >= d->mInstances.count()) { 0215 return QAbstractItemModel::flags(index); 0216 } 0217 0218 return QAbstractItemModel::flags(index) | Qt::ItemIsEditable; 0219 } 0220 0221 bool AgentInstanceModel::setData(const QModelIndex &index, const QVariant &value, int role) 0222 { 0223 if (!index.isValid()) { 0224 return false; 0225 } 0226 0227 if (index.row() < 0 || index.row() >= d->mInstances.count()) { 0228 return false; 0229 } 0230 0231 AgentInstance &instance = d->mInstances[index.row()]; 0232 0233 switch (role) { 0234 case OnlineRole: 0235 instance.setIsOnline(value.toBool()); 0236 Q_EMIT dataChanged(index, index); 0237 return true; 0238 default: 0239 return false; 0240 } 0241 0242 return false; 0243 } 0244 0245 #include "moc_agentinstancemodel.cpp"