1 | /* |
2 | Copyright (c) 2006 Tobias Koenig <tokoe@kde.org> |
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 <QtCore/QStringList> |
25 | #include <QIcon> |
26 | |
27 | using namespace Akonadi; |
28 | |
29 | /** |
30 | * @internal |
31 | */ |
32 | class AgentTypeModel::Private |
33 | { |
34 | public: |
35 | Private(AgentTypeModel *parent) |
36 | : mParent(parent) |
37 | { |
38 | mTypes = AgentManager::self()->types(); |
39 | } |
40 | |
41 | AgentTypeModel *mParent; |
42 | AgentType::List mTypes; |
43 | |
44 | void typeAdded(const AgentType &agentType); |
45 | void typeRemoved(const AgentType &agentType); |
46 | }; |
47 | |
48 | void AgentTypeModel::Private::typeAdded(const AgentType &agentType) |
49 | { |
50 | mTypes.append(agentType); |
51 | |
52 | emit mParent->layoutChanged(); |
53 | } |
54 | |
55 | void AgentTypeModel::Private::typeRemoved(const AgentType &agentType) |
56 | { |
57 | mTypes.removeAll(agentType); |
58 | |
59 | emit mParent->layoutChanged(); |
60 | } |
61 | |
62 | AgentTypeModel::AgentTypeModel(QObject *parent) |
63 | : QAbstractItemModel(parent) |
64 | , d(new Private(this)) |
65 | { |
66 | connect(AgentManager::self(), SIGNAL(typeAdded(Akonadi::AgentType)), |
67 | this, SLOT(typeAdded(Akonadi::AgentType))); |
68 | connect(AgentManager::self(), SIGNAL(typeRemoved(Akonadi::AgentType)), |
69 | this, SLOT(typeRemoved(Akonadi::AgentType))); |
70 | } |
71 | |
72 | AgentTypeModel::~AgentTypeModel() |
73 | { |
74 | delete d; |
75 | } |
76 | |
77 | int AgentTypeModel::columnCount(const QModelIndex &) const |
78 | { |
79 | return 1; |
80 | } |
81 | |
82 | int AgentTypeModel::rowCount(const QModelIndex &) const |
83 | { |
84 | return d->mTypes.count(); |
85 | } |
86 | |
87 | QVariant 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 | return type.name(); |
102 | break; |
103 | case Qt::DecorationRole: |
104 | return type.icon(); |
105 | break; |
106 | case TypeRole: { |
107 | QVariant var; |
108 | var.setValue(type); |
109 | return var; |
110 | break; |
111 | } |
112 | case IdentifierRole: |
113 | return type.identifier(); |
114 | break; |
115 | case DescriptionRole: |
116 | return type.description(); |
117 | break; |
118 | case MimeTypesRole: |
119 | return type.mimeTypes(); |
120 | break; |
121 | case CapabilitiesRole: |
122 | return type.capabilities(); |
123 | break; |
124 | default: |
125 | break; |
126 | } |
127 | return QVariant(); |
128 | } |
129 | |
130 | QModelIndex AgentTypeModel::index(int row, int column, const QModelIndex &) const |
131 | { |
132 | if (row < 0 || row >= d->mTypes.count()) { |
133 | return QModelIndex(); |
134 | } |
135 | |
136 | if (column != 0) { |
137 | return QModelIndex(); |
138 | } |
139 | |
140 | return createIndex(row, column); |
141 | } |
142 | |
143 | QModelIndex AgentTypeModel::parent(const QModelIndex &) const |
144 | { |
145 | return QModelIndex(); |
146 | } |
147 | |
148 | Qt::ItemFlags AgentTypeModel::flags(const QModelIndex &index) const |
149 | { |
150 | if (!index.isValid() || index.row() < 0 || index.row() >= d->mTypes.count()) { |
151 | return QAbstractItemModel::flags(index); |
152 | } |
153 | |
154 | const AgentType &type = d->mTypes[index.row()]; |
155 | if (type.capabilities().contains(QLatin1String("Unique" )) && |
156 | AgentManager::self()->instance(type.identifier()).isValid()) { |
157 | return QAbstractItemModel::flags(index) &~(Qt::ItemIsSelectable | Qt::ItemIsEnabled); |
158 | } |
159 | return QAbstractItemModel::flags(index); |
160 | } |
161 | |
162 | #include "moc_agenttypemodel.cpp" |
163 | |