Akonadi

agenttypemodel.cpp
1 /*
2  Copyright (c) 2006 Tobias Koenig <[email protected]>
3 
4  This library is free software; you can redistribute it and/or modify it
5  under the terms of the GNU Library General Public License as published by
6  the Free Software Foundation; either version 2 of the License, or (at your
7  option) any later version.
8 
9  This library is distributed in the hope that it will be useful, but WITHOUT
10  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
12  License for more details.
13 
14  You should have received a copy of the GNU Library General Public License
15  along with this library; see the file COPYING.LIB. If not, write to the
16  Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17  02110-1301, USA.
18 */
19 
20 #include "agenttypemodel.h"
21 #include "agenttype.h"
22 #include "agentmanager.h"
23 
24 #include <QIcon>
25 
26 using namespace Akonadi;
27 
31 class Q_DECL_HIDDEN AgentTypeModel::Private
32 {
33 public:
34  Private(AgentTypeModel *parent)
35  : mParent(parent)
36  {
37  mTypes = AgentManager::self()->types();
38  }
39 
40  AgentTypeModel *mParent = nullptr;
41  AgentType::List mTypes;
42 
43  void typeAdded(const AgentType &agentType);
44  void typeRemoved(const AgentType &agentType);
45 };
46 
47 void AgentTypeModel::Private::typeAdded(const AgentType &agentType)
48 {
49  mTypes.append(agentType);
50 
51  Q_EMIT mParent->layoutChanged();
52 }
53 
54 void AgentTypeModel::Private::typeRemoved(const AgentType &agentType)
55 {
56  mTypes.removeAll(agentType);
57 
58  Q_EMIT mParent->layoutChanged();
59 }
60 
62  : QAbstractItemModel(parent)
63  , d(new Private(this))
64 {
65  connect(AgentManager::self(), &AgentManager::typeAdded, this, [this](const Akonadi::AgentType &type ) { d->typeAdded(type); });
66  connect(AgentManager::self(), &AgentManager::typeRemoved, this, [this](const Akonadi::AgentType &type ) { d->typeRemoved(type); });
67 }
68 
70 {
71  delete d;
72 }
73 
74 int AgentTypeModel::columnCount(const QModelIndex &) const
75 {
76  return 1;
77 }
78 
79 int AgentTypeModel::rowCount(const QModelIndex &) const
80 {
81  return d->mTypes.count();
82 }
83 
84 QVariant AgentTypeModel::data(const QModelIndex &index, int role) const
85 {
86  if (!index.isValid()) {
87  return QVariant();
88  }
89 
90  if (index.row() < 0 || index.row() >= d->mTypes.count()) {
91  return QVariant();
92  }
93 
94  const AgentType &type = d->mTypes[index.row()];
95 
96  switch (role) {
97  case Qt::DisplayRole:
98  return type.name();
99  case Qt::DecorationRole:
100  return type.icon();
101  case TypeRole: {
102  QVariant var;
103  var.setValue(type);
104  return var;
105  }
106  case IdentifierRole:
107  return type.identifier();
108  case DescriptionRole:
109  return type.description();
110  case MimeTypesRole:
111  return type.mimeTypes();
112  case CapabilitiesRole:
113  return type.capabilities();
114  default:
115  break;
116  }
117  return QVariant();
118 }
119 
120 QModelIndex AgentTypeModel::index(int row, int column, const QModelIndex &) const
121 {
122  if (row < 0 || row >= d->mTypes.count()) {
123  return QModelIndex();
124  }
125 
126  if (column != 0) {
127  return QModelIndex();
128  }
129 
130  return createIndex(row, column);
131 }
132 
134 {
135  return QModelIndex();
136 }
137 
138 Qt::ItemFlags AgentTypeModel::flags(const QModelIndex &index) const
139 {
140  if (!index.isValid() || index.row() < 0 || index.row() >= d->mTypes.count()) {
141  return QAbstractItemModel::flags(index);
142  }
143 
144  const AgentType &type = d->mTypes[index.row()];
145  if (type.capabilities().contains(QLatin1String("Unique")) &&
146  AgentManager::self()->instance(type.identifier()).isValid()) {
147  return QAbstractItemModel::flags(index) & ~(Qt::ItemIsSelectable | Qt::ItemIsEnabled);
148  }
149  return QAbstractItemModel::flags(index);
150 }
151 
152 #include "moc_agenttypemodel.cpp"
QVector< AgentType > List
Describes a list of agent types.
QIcon icon() const
Returns the icon of the agent type.
QString description() const
Returns the description of the agent type.
QString name() const
Returns the i18n&#39;ed name of the agent type.
bool contains(const QString &str, Qt::CaseSensitivity cs) const const
QString identifier() const
Returns the unique identifier of the agent type.
The agent type itself.
void typeRemoved(const Akonadi::AgentType &type)
This signal is emitted whenever an agent type was removed from the system.
bool isValid() const const
A representation of an agent type.
QStringList mimeTypes() const
Returns the list of supported mime types of the agent type.
AgentInstance instance(const QString &identifier) const
Returns the agent instance with the given identifier or an invalid agent instance if the identifier d...
~AgentTypeModel() override
Destroys the agent type model.
The identifier of the agent type.
int row() const const
QStringList capabilities() const
Returns the list of supported capabilities of the agent type.
A description of the agent type.
QModelIndex createIndex(int row, int column, void *ptr) const const
Provides a data model for agent types.
void setValue(const T &value)
AgentType::List types() const
Returns the list of all available agent types.
Helper integration between Akonadi and Qt.
AgentTypeModel(QObject *parent=nullptr)
Creates a new agent type model.
static AgentManager * self()
Returns the global instance of the agent manager.
virtual Qt::ItemFlags flags(const QModelIndex &index) const const
A list of supported capabilities.
QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
QObject * parent() const const
void typeAdded(const Akonadi::AgentType &type)
This signal is emitted whenever a new agent type was installed on the system.
typedef ItemFlags
A list of supported mimetypes.
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Wed May 27 2020 22:43:36 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.