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 "agentinstancemodel.h"
21
22#include "agentinstance.h"
23#include "agentmanager.h"
24
25#include <QtCore/QStringList>
26#include <QIcon>
27
28#include <klocalizedstring.h>
29
30using namespace Akonadi;
31
32/**
33 * @internal
34 */
35class AgentInstanceModel::Private
36{
37public:
38 Private(AgentInstanceModel *parent)
39 : mParent(parent)
40 {
41 }
42
43 AgentInstanceModel *mParent;
44 AgentInstance::List mInstances;
45
46 void instanceAdded(const AgentInstance &);
47 void instanceRemoved(const AgentInstance &);
48 void instanceChanged(const AgentInstance &);
49};
50
51void AgentInstanceModel::Private::instanceAdded(const AgentInstance &instance)
52{
53 mParent->beginInsertRows(QModelIndex(), mInstances.count(), mInstances.count());
54 mInstances.append(instance);
55 mParent->endInsertRows();
56}
57
58void AgentInstanceModel::Private::instanceRemoved(const AgentInstance &instance)
59{
60 const int index = mInstances.indexOf(instance);
61 if (index == -1) {
62 return;
63 }
64
65 mParent->beginRemoveRows(QModelIndex(), index, index);
66 mInstances.removeAll(instance);
67 mParent->endRemoveRows();
68}
69
70void AgentInstanceModel::Private::instanceChanged(const AgentInstance &instance)
71{
72 const int numberOfInstance(mInstances.count());
73 for (int i = 0; i < numberOfInstance; ++i) {
74 if (mInstances[i] == instance) {
75 mInstances[i] = instance;
76
77 const QModelIndex idx = mParent->index(i, 0);
78 emit mParent->dataChanged(idx, idx);
79
80 return;
81 }
82 }
83}
84
85AgentInstanceModel::AgentInstanceModel(QObject *parent)
86 : QAbstractItemModel(parent)
87 , d(new Private(this))
88{
89 d->mInstances = AgentManager::self()->instances();
90
91 QHash<int, QByteArray> roles = roleNames();
92 roles.insert(StatusRole, "status");
93 roles.insert(StatusMessageRole, "statusMessage");
94 roles.insert(ProgressRole, "progress");
95 roles.insert(OnlineRole, "online");
96 setRoleNames(roles);
97
98 connect(AgentManager::self(), SIGNAL(instanceAdded(Akonadi::AgentInstance)),
99 this, SLOT(instanceAdded(Akonadi::AgentInstance)));
100 connect(AgentManager::self(), SIGNAL(instanceRemoved(Akonadi::AgentInstance)),
101 this, SLOT(instanceRemoved(Akonadi::AgentInstance)));
102 connect(AgentManager::self(), SIGNAL(instanceStatusChanged(Akonadi::AgentInstance)),
103 this, SLOT(instanceChanged(Akonadi::AgentInstance)));
104 connect(AgentManager::self(), SIGNAL(instanceProgressChanged(Akonadi::AgentInstance)),
105 this, SLOT(instanceChanged(Akonadi::AgentInstance)));
106 connect(AgentManager::self(), SIGNAL(instanceNameChanged(Akonadi::AgentInstance)),
107 this, SLOT(instanceChanged(Akonadi::AgentInstance)));
108 connect(AgentManager::self(), SIGNAL(instanceOnline(Akonadi::AgentInstance,bool)),
109 this, SLOT(instanceChanged(Akonadi::AgentInstance)));
110}
111
112AgentInstanceModel::~AgentInstanceModel()
113{
114 delete d;
115}
116
117int AgentInstanceModel::columnCount(const QModelIndex &) const
118{
119 return 1;
120}
121
122int AgentInstanceModel::rowCount(const QModelIndex &) const
123{
124 return d->mInstances.count();
125}
126
127QVariant AgentInstanceModel::data(const QModelIndex &index, int role) const
128{
129 if (!index.isValid()) {
130 return QVariant();
131 }
132
133 if (index.row() < 0 || index.row() >= d->mInstances.count()) {
134 return QVariant();
135 }
136
137 const AgentInstance &instance = d->mInstances[index.row()];
138
139 switch (role) {
140 case Qt::DisplayRole:
141 return instance.name();
142 case Qt::DecorationRole:
143 return instance.type().icon();
144 case InstanceRole: {
145 QVariant var;
146 var.setValue(instance);
147 return var;
148 }
149 case InstanceIdentifierRole:
150 return instance.identifier();
151 case Qt::ToolTipRole:
152 return QString::fromLatin1("<qt><h4>%1</h4>%2</qt>").arg(instance.name(), instance.type().description());
153 case StatusRole:
154 return instance.status();
155 case StatusMessageRole:
156 return instance.statusMessage();
157 case ProgressRole:
158 return instance.progress();
159 case OnlineRole:
160 return instance.isOnline();
161 case TypeRole: {
162 QVariant var;
163 var.setValue(instance.type());
164 return var;
165 }
166 case TypeIdentifierRole:
167 return instance.type().identifier();
168 case DescriptionRole:
169 return instance.type().description();
170 case CapabilitiesRole:
171 return instance.type().capabilities();
172 case MimeTypesRole:
173 return instance.type().mimeTypes();
174 }
175 return QVariant();
176}
177
178QVariant AgentInstanceModel::headerData(int section, Qt::Orientation orientation, int role) const
179{
180 if (orientation == Qt::Vertical) {
181 return QVariant();
182 }
183
184 if (role != Qt::DisplayRole) {
185 return QVariant();
186 }
187
188 switch (section) {
189 case 0:
190 return i18nc("@title:column, name of a thing", "Name");
191 break;
192 default:
193 return QVariant();
194 break;
195 }
196}
197
198QModelIndex AgentInstanceModel::index(int row, int column, const QModelIndex &) const
199{
200 if (row < 0 || row >= d->mInstances.count()) {
201 return QModelIndex();
202 }
203
204 if (column != 0) {
205 return QModelIndex();
206 }
207
208 return createIndex(row, column);
209}
210
211QModelIndex AgentInstanceModel::parent(const QModelIndex &) const
212{
213 return QModelIndex();
214}
215
216Qt::ItemFlags AgentInstanceModel::flags(const QModelIndex &index) const
217{
218 if (!index.isValid() || index.row() < 0 || index.row() >= d->mInstances.count()) {
219 return QAbstractItemModel::flags(index);
220 }
221
222 return QAbstractItemModel::flags(index) | Qt::ItemIsEditable;
223}
224
225bool AgentInstanceModel::setData(const QModelIndex &index, const QVariant &value, int role)
226{
227 if (!index.isValid()) {
228 return false;
229 }
230
231 if (index.row() < 0 || index.row() >= d->mInstances.count()) {
232 return false;
233 }
234
235 AgentInstance &instance = d->mInstances[index.row()];
236
237 switch (role) {
238 case OnlineRole:
239 instance.setIsOnline(value.toBool());
240 emit dataChanged(index, index);
241 return true;
242 default:
243 return false;
244 }
245
246 return false;
247}
248
249#include "moc_agentinstancemodel.cpp"
250