Akonadi

agenttypemodel.cpp
1 /*
2  SPDX-FileCopyrightText: 2006 Tobias Koenig <[email protected]>
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 
13 using namespace Akonadi;
14 
15 /**
16  * @internal
17  */
18 class Akonadi::AgentTypeModelPrivate
19 {
20 public:
21  explicit AgentTypeModelPrivate(AgentTypeModel *parent)
22  : mParent(parent)
23  {
24  mTypes = AgentManager::self()->types();
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 
34 void AgentTypeModelPrivate::typeAdded(const AgentType &agentType)
35 {
36  mTypes.append(agentType);
37 
38  Q_EMIT mParent->layoutChanged();
39 }
40 
41 void 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 
62 int AgentTypeModel::columnCount(const QModelIndex & /*parent*/) const
63 {
64  return 1;
65 }
66 
67 int AgentTypeModel::rowCount(const QModelIndex & /*parent*/) const
68 {
69  return d->mTypes.count();
70 }
71 
72 QHash<int, QByteArray> AgentTypeModel::roleNames() const
73 {
75  roles.insert(TypeRole, QByteArrayLiteral("type"));
76  roles.insert(IdentifierRole, QByteArrayLiteral("identifier"));
77  roles.insert(DescriptionRole, QByteArrayLiteral("description"));
78  roles.insert(MimeTypesRole, QByteArrayLiteral("mimeTypes"));
79  roles.insert(CapabilitiesRole, QByteArrayLiteral("capabilities"));
80  return roles;
81 }
82 
83 QVariant AgentTypeModel::data(const QModelIndex &index, int role) const
84 {
85  if (!index.isValid()) {
86  return QVariant();
87  }
88 
89  if (index.row() < 0 || index.row() >= d->mTypes.count()) {
90  return QVariant();
91  }
92 
93  const AgentType &type = d->mTypes[index.row()];
94 
95  switch (role) {
96  case Qt::DisplayRole:
97  return type.name();
98  case Qt::DecorationRole:
99  return type.icon();
100  case TypeRole: {
101  QVariant var;
102  var.setValue(type);
103  return var;
104  }
105  case IdentifierRole:
106  return type.identifier();
107  case DescriptionRole:
108  return type.description();
109  case MimeTypesRole:
110  return type.mimeTypes();
111  case CapabilitiesRole:
112  return type.capabilities();
113  default:
114  break;
115  }
116  return QVariant();
117 }
118 
119 QModelIndex AgentTypeModel::index(int row, int column, const QModelIndex & /*parent*/) const
120 {
121  if (row < 0 || row >= d->mTypes.count()) {
122  return QModelIndex();
123  }
124 
125  if (column != 0) {
126  return QModelIndex();
127  }
128 
129  return createIndex(row, column);
130 }
131 
132 QModelIndex AgentTypeModel::parent(const QModelIndex & /*child*/) const
133 {
134  return QModelIndex();
135 }
136 
137 Qt::ItemFlags AgentTypeModel::flags(const QModelIndex &index) const
138 {
139  if (!index.isValid() || index.row() < 0 || index.row() >= d->mTypes.count()) {
140  return QAbstractItemModel::flags(index);
141  }
142 
143  const AgentType &type = d->mTypes[index.row()];
144  if (type.capabilities().contains(QLatin1String("Unique")) && AgentManager::self()->instance(type.identifier()).isValid()) {
146  }
147  return QAbstractItemModel::flags(index);
148 }
149 
150 #include "moc_agenttypemodel.cpp"
~AgentTypeModel() override
Destroys the agent type model.
@ CapabilitiesRole
A list of supported capabilities.
@ MimeTypesRole
A list of supported mimetypes.
DisplayRole
A representation of an agent type.
@ TypeRole
The agent type itself.
void setValue(const T &value)
QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
AgentTypeModel(QObject *parent=nullptr)
Creates a new agent type model.
QHash::iterator insert(const Key &key, const T &value)
virtual QHash< int, QByteArray > roleNames() const const
QModelIndex createIndex(int row, int column, void *ptr) const const
void typeRemoved(const Akonadi::AgentType &type)
This signal is emitted whenever an agent type was removed from the system.
typedef ItemFlags
virtual Qt::ItemFlags flags(const QModelIndex &index) const const
@ IdentifierRole
The identifier of the agent type.
Provides a data model for agent types.
bool isValid() const const
int row() const const
VehicleSection::Type type(QStringView coachNumber, QStringView coachClassification)
@ DescriptionRole
A description of the agent type.
static AgentManager * self()
Returns the global instance of the agent manager.
QVector< AgentType > List
Describes a list of agent types.
QObject * parent() const const
void typeAdded(const Akonadi::AgentType &type)
This signal is emitted whenever a new agent type was installed on the system.
AgentType::List types() const
Returns the list of all available agent types.
Helper integration between Akonadi and Qt.
This file is part of the KDE documentation.
Documentation copyright © 1996-2022 The KDE developers.
Generated on Mon Jun 27 2022 04:01:05 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.