Akonadi

agenttypemodel.cpp
1/*
2 SPDX-FileCopyrightText: 2006 Tobias Koenig <tokoe@kde.org>
3
4 SPDX-License-Identifier: LGPL-2.0-or-later
5*/
6
7#include "agenttypemodel.h"
8#include "agentmanager.h"
9#include "agenttype.h"
10
11#include <QIcon>
12
13using namespace Akonadi;
14
15/**
16 * @internal
17 */
18class Akonadi::AgentTypeModelPrivate
19{
20public:
21 explicit AgentTypeModelPrivate(AgentTypeModel *parent)
22 : mParent(parent)
23 , mTypes(AgentManager::self()->types())
24 {
25 }
26
27 AgentTypeModel *const mParent;
28 AgentType::List mTypes;
29
30 void typeAdded(const AgentType &agentType);
31 void typeRemoved(const AgentType &agentType);
32};
33
34void AgentTypeModelPrivate::typeAdded(const AgentType &agentType)
35{
36 mTypes.append(agentType);
37
38 Q_EMIT mParent->layoutChanged();
39}
40
41void AgentTypeModelPrivate::typeRemoved(const AgentType &agentType)
42{
43 mTypes.removeAll(agentType);
44
45 Q_EMIT mParent->layoutChanged();
46}
47
49 : QAbstractItemModel(parent)
50 , d(new AgentTypeModelPrivate(this))
51{
53 d->typeAdded(type);
54 });
56 d->typeRemoved(type);
57 });
58}
59
61
62int AgentTypeModel::columnCount(const QModelIndex & /*parent*/) const
63{
64 return 1;
65}
66
67int AgentTypeModel::rowCount(const QModelIndex &parent) const
68{
69 if (parent.isValid()) // flat model
70 return 0;
71 return d->mTypes.count();
72}
73
74QHash<int, QByteArray> AgentTypeModel::roleNames() const
75{
76 QHash<int, QByteArray> roles = QAbstractItemModel::roleNames();
77 roles.insert(NameRole, QByteArrayLiteral("name"));
78 roles.insert(TypeRole, QByteArrayLiteral("type"));
79 roles.insert(IdentifierRole, QByteArrayLiteral("identifier"));
80 roles.insert(DescriptionRole, QByteArrayLiteral("description"));
81 roles.insert(MimeTypesRole, QByteArrayLiteral("mimeTypes"));
82 roles.insert(CapabilitiesRole, QByteArrayLiteral("capabilities"));
83 roles.insert(IconNameRole, QByteArrayLiteral("iconName"));
84 return roles;
85}
86
87QVariant AgentTypeModel::data(const QModelIndex &index, int role) const
88{
89 if (!index.isValid()) {
90 return QVariant();
91 }
92
93 if (index.row() < 0 || index.row() >= d->mTypes.count()) {
94 return QVariant();
95 }
96
97 const AgentType &type = d->mTypes[index.row()];
98
99 switch (role) {
100 case Qt::DisplayRole:
101 case NameRole:
102 return type.name();
104 return type.icon();
105 case IconNameRole:
106 return type.icon().name();
107 case TypeRole: {
108 QVariant var;
109 var.setValue(type);
110 return var;
111 }
112 case IdentifierRole:
113 return type.identifier();
114 case DescriptionRole:
115 return type.description();
116 case MimeTypesRole:
117 return type.mimeTypes();
118 case CapabilitiesRole:
119 return type.capabilities();
120 default:
121 break;
122 }
123 return QVariant();
124}
125
126QModelIndex AgentTypeModel::index(int row, int column, const QModelIndex & /*parent*/) const
127{
128 if (row < 0 || row >= d->mTypes.count()) {
129 return QModelIndex();
130 }
131
132 if (column != 0) {
133 return QModelIndex();
134 }
135
136 return createIndex(row, column);
137}
138
139QModelIndex AgentTypeModel::parent(const QModelIndex & /*child*/) const
140{
141 return QModelIndex();
142}
143
144Qt::ItemFlags AgentTypeModel::flags(const QModelIndex &index) const
145{
146 if (!index.isValid() || index.row() < 0 || index.row() >= d->mTypes.count()) {
147 return QAbstractItemModel::flags(index);
148 }
149
150 const AgentType &type = d->mTypes[index.row()];
151 if (type.capabilities().contains(QLatin1StringView("Unique")) && AgentManager::self()->instance(type.identifier()).isValid()) {
153 }
154 return QAbstractItemModel::flags(index);
155}
156
157#include "moc_agenttypemodel.cpp"
static AgentManager * self()
Returns the global instance of the agent manager.
void typeAdded(const Akonadi::AgentType &type)
This signal is emitted whenever a new agent type was installed on the system.
void typeRemoved(const Akonadi::AgentType &type)
This signal is emitted whenever an agent type was removed from the system.
~AgentTypeModel() override
Destroys the agent type model.
@ MimeTypesRole
A list of supported mimetypes.
@ TypeRole
The agent type itself.
@ IdentifierRole
The icon name of the agent.
@ DescriptionRole
A description of the agent type.
@ IconNameRole
The display nme of the agent type.
@ CapabilitiesRole
A list of supported capabilities.
AgentTypeModel(QObject *parent=nullptr)
Creates a new agent type model.
A representation of an agent type.
QList< AgentType > List
Describes a list of agent types.
Helper integration between Akonadi and Qt.
VehicleSection::Type type(QStringView coachNumber, QStringView coachClassification)
QAbstractItemModel(QObject *parent)
QModelIndex createIndex(int row, int column, const void *ptr) const const
virtual Qt::ItemFlags flags(const QModelIndex &index) const const
virtual QHash< int, QByteArray > roleNames() const const
iterator insert(const Key &key, const T &value)
QObject(QObject *parent)
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
QObject * parent() const const
DisplayRole
typedef ItemFlags
void setValue(QVariant &&value)
This file is part of the KDE documentation.
Documentation copyright © 1996-2025 The KDE developers.
Generated on Fri Apr 11 2025 11:52:14 by doxygen 1.13.2 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.