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 return d->mTypes.count();
70}
71
72QHash<int, QByteArray> AgentTypeModel::roleNames() const
73{
75 roles.insert(NameRole, QByteArrayLiteral("name"));
76 roles.insert(TypeRole, QByteArrayLiteral("type"));
77 roles.insert(IdentifierRole, QByteArrayLiteral("identifier"));
78 roles.insert(DescriptionRole, QByteArrayLiteral("description"));
79 roles.insert(MimeTypesRole, QByteArrayLiteral("mimeTypes"));
80 roles.insert(CapabilitiesRole, QByteArrayLiteral("capabilities"));
81 roles.insert(IconNameRole, QByteArrayLiteral("iconName"));
82 return roles;
83}
84
85QVariant AgentTypeModel::data(const QModelIndex &index, int role) const
86{
87 if (!index.isValid()) {
88 return QVariant();
89 }
90
91 if (index.row() < 0 || index.row() >= d->mTypes.count()) {
92 return QVariant();
93 }
94
95 const AgentType &type = d->mTypes[index.row()];
96
97 switch (role) {
98 case Qt::DisplayRole:
99 case NameRole:
100 return type.name();
102 return type.icon();
103 case IconNameRole:
104 return type.icon().name();
105 case TypeRole: {
106 QVariant var;
107 var.setValue(type);
108 return var;
109 }
110 case IdentifierRole:
111 return type.identifier();
112 case DescriptionRole:
113 return type.description();
114 case MimeTypesRole:
115 return type.mimeTypes();
116 case CapabilitiesRole:
117 return type.capabilities();
118 default:
119 break;
120 }
121 return QVariant();
122}
123
124QModelIndex AgentTypeModel::index(int row, int column, const QModelIndex & /*parent*/) const
125{
126 if (row < 0 || row >= d->mTypes.count()) {
127 return QModelIndex();
128 }
129
130 if (column != 0) {
131 return QModelIndex();
132 }
133
134 return createIndex(row, column);
135}
136
137QModelIndex AgentTypeModel::parent(const QModelIndex & /*child*/) const
138{
139 return QModelIndex();
140}
141
142Qt::ItemFlags AgentTypeModel::flags(const QModelIndex &index) const
143{
144 if (!index.isValid() || index.row() < 0 || index.row() >= d->mTypes.count()) {
145 return QAbstractItemModel::flags(index);
146 }
147
148 const AgentType &type = d->mTypes[index.row()];
149 if (type.capabilities().contains(QLatin1StringView("Unique")) && AgentManager::self()->instance(type.identifier()).isValid()) {
151 }
152 return QAbstractItemModel::flags(index);
153}
154
155#include "moc_agenttypemodel.cpp"
The agent manager has knowledge about all available agents (it scans for .desktop files in the agent ...
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.
Provides a data model for agent types.
~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.
Helper integration between Akonadi and Qt.
VehicleSection::Type type(QStringView coachNumber, QStringView coachClassification)
QModelIndex createIndex(int row, int column, const void *ptr) const const
virtual Qt::ItemFlags flags(const QModelIndex &index) const const
void layoutChanged(const QList< QPersistentModelIndex > &parents, QAbstractItemModel::LayoutChangeHint hint)
virtual QHash< int, QByteArray > roleNames() const const
iterator insert(const Key &key, const T &value)
void append(QList< T > &&value)
qsizetype removeAll(const AT &t)
bool isValid() const const
int row() const const
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-2024 The KDE developers.
Generated on Mon Nov 4 2024 16:31:58 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.