22#include "itemfetchjob.h"
23#include "collectionfetchjob.h"
24#include "itemfetchscope.h"
26#include "pastehelper_p.h"
30#include <klocalizedstring.h>
33#include <QCoreApplication>
34#include <QtCore/QDebug>
35#include <QtCore/QMimeData>
49 ItemContainer(
const Item &i,
int r)
61class ItemModel::Private
68 session =
new Session(QCoreApplication::instance()->applicationName().toUtf8()
69 + QByteArray(
"-ItemModel-") + QByteArray::number(qrand()), mParent);
73 mParent->connect(monitor, SIGNAL(itemChanged(Akonadi::Item,QSet<QByteArray>)),
74 mParent, SLOT(itemChanged(Akonadi::Item,QSet<QByteArray>)));
78 mParent, SLOT(itemAdded(Akonadi::Item)));
79 mParent->connect(monitor, SIGNAL(itemRemoved(Akonadi::Item)),
80 mParent, SLOT(itemRemoved(Akonadi::Item)));
82 mParent, SLOT(itemAdded(Akonadi::Item)));
84 mParent, SLOT(itemRemoved(Akonadi::Item)));
92 void listingDone(KJob *job);
93 void collectionFetchResult(KJob *job);
94 void itemChanged(
const Akonadi::Item &item,
const QSet<QByteArray> &);
95 void itemsAdded(
const Akonadi::Item::List &list);
96 void itemAdded(
const Akonadi::Item &item);
98 void itemRemoved(
const Akonadi::Item &item);
99 int rowForItem(
const Akonadi::Item &item);
100 bool collectionIsCompatible()
const;
104 QList<ItemContainer *> items;
105 QHash<Item, ItemContainer *> itemHash;
112bool ItemModel::Private::collectionIsCompatible()
const
115 if (mParent->mimeTypes() == QStringList(QLatin1String(
"text/uri-list"))) {
120 Q_FOREACH (
const QString &type, mParent->mimeTypes()) {
128void ItemModel::Private::listingDone(KJob *job)
134 kWarning() <<
"Item query failed:" << job->errorString();
138void ItemModel::Private::collectionFetchResult(KJob *job)
150 mParent->setCollection(c);
152 kWarning() <<
"Failed to retrieve the contents mime type of the collection: " << c;
157int ItemModel::Private::rowForItem(
const Akonadi::Item &item)
159 ItemContainer *container = itemHash.value(item);
169 if (container->row < items.count()
170 && items.at(container->row) == container) {
171 return container->row;
175 const int numberOfItems(items.size());
176 for (
int i = 0; i < numberOfItems; ++i) {
177 if (items.at(i)->item == item) {
187void ItemModel::Private::itemChanged(
const Akonadi::Item &item,
const QSet<QByteArray> &)
189 int row = rowForItem(item);
194 items[row]->item = item;
195 itemHash.remove(item);
196 itemHash[item] = items[row];
198 QModelIndex start = mParent->index(row, 0, QModelIndex());
199 QModelIndex end = mParent->index(row, mParent->columnCount(QModelIndex()) - 1 , QModelIndex());
201 mParent->dataChanged(start, end);
206 if (colSrc == collection && colDst != collection) {
212 if (colDst == collection && colSrc != collection) {
218void ItemModel::Private::itemsAdded(
const Akonadi::Item::List &list)
220 if (list.isEmpty()) {
223 mParent->beginInsertRows(QModelIndex(), items.count(), items.count() + list.count() - 1);
224 foreach (
const Item &item, list) {
225 ItemContainer *c =
new ItemContainer(item, items.count());
229 mParent->endInsertRows();
232void ItemModel::Private::itemAdded(
const Akonadi::Item &item)
239void ItemModel::Private::itemRemoved(
const Akonadi::Item &_item)
241 int row = rowForItem(_item);
246 mParent->beginRemoveRows(QModelIndex(), row, row);
247 const Item item = items.at(row)->item;
248 Q_ASSERT(item.isValid());
249 itemHash.remove(item);
250 delete items.takeAt(row);
251 mParent->endRemoveRows();
255 : QAbstractTableModel(parent)
256 , d(new Private(this))
265QVariant ItemModel::data(
const QModelIndex &index,
int role)
const
267 if (!index.isValid()) {
270 if (index.row() >= d->items.count()) {
273 const Item item = d->items.at(index.row())->item;
274 if (!item.isValid()) {
278 if (role == Qt::DisplayRole) {
279 switch (index.column()) {
281 return QString::number(item.id());
283 return item.remoteId();
285 return item.mimeType();
302 return item.mimeType();
308int ItemModel::rowCount(
const QModelIndex &parent)
const
310 if (!parent.isValid()) {
311 return d->items.count();
316int ItemModel::columnCount(
const QModelIndex &parent)
const
318 if (!parent.isValid()) {
324QVariant ItemModel::headerData(
int section, Qt::Orientation orientation,
int role)
const
326 if (orientation == Qt::Horizontal && role == Qt::DisplayRole) {
331 return i18n(
"Remote Id");
333 return i18n(
"MimeType");
338 return QAbstractTableModel::headerData(section, orientation, role);
351 connect(job, SIGNAL(result(KJob*)),
this, SLOT(collectionFetchResult(KJob*)));
355 d->monitor->setCollectionMonitored(d->collection,
false);
359 d->monitor->setCollectionMonitored(d->collection,
true);
362 qDeleteAll(d->items);
370 if (d->collectionIsCompatible()) {
373 connect(job, SIGNAL(itemsReceived(Akonadi::Item::List)),
374 SLOT(itemsAdded(Akonadi::Item::List)));
375 connect(job, SIGNAL(result(KJob*)), SLOT(listingDone(KJob*)));
388 return d->monitor->itemFetchScope();
393 if (!index.isValid()) {
394 return Akonadi::Item();
397 if (index.row() >= d->items.count()) {
398 return Akonadi::Item();
401 Item item = d->items.at(index.row())->item;
402 if (item.isValid()) {
405 return Akonadi::Item();
409Qt::ItemFlags ItemModel::flags(
const QModelIndex &index)
const
411 Qt::ItemFlags defaultFlags = QAbstractTableModel::flags(index);
413 if (index.isValid()) {
414 return Qt::ItemIsDragEnabled | Qt::ItemIsDropEnabled | defaultFlags;
416 return Qt::ItemIsDropEnabled | defaultFlags;
420QStringList ItemModel::mimeTypes()
const
422 return QStringList() << QLatin1String(
"text/uri-list");
430QMimeData *ItemModel::mimeData(
const QModelIndexList &indexes)
const
432 QMimeData *data =
new QMimeData();
435 foreach (
const QModelIndex &index, indexes) {
436 if (index.column() != 0) {
442 urls.populateMimeData(data);
449 return index(d->rowForItem(item), column);
452bool ItemModel::dropMimeData(
const QMimeData *data, Qt::DropAction action,
int row,
int column,
const QModelIndex &parent)
464 return d->collection;
467Qt::DropActions ItemModel::supportedDropActions()
const
469 return Qt::CopyAction | Qt::MoveAction | Qt::LinkAction;
472#include "moc_itemmodel.cpp"
Job that fetches collections from the Akonadi storage.
@ Base
Only fetch the base collection.
Collection::List collections() const
Returns the list of fetched collection.
Represents a collection of PIM items.
QStringList contentMimeTypes() const
Returns a list of possible content mimetypes, e.g.
bool isValid() const
Returns whether the entity is valid.
Job that fetches items from the Akonadi storage.
void setFetchScope(ItemFetchScope &fetchScope)
Sets the item fetch scope.
Specifies which parts of an item should be fetched from the Akonadi storage.
void setFetchScope(const ItemFetchScope &fetchScope)
Sets the item fetch scope.
Session * session() const
Returns the Session object used for all operations by this model.
void setCollection(const Akonadi::Collection &collection)
Sets the collection the model should display.
@ IdRole
The id of the item.
@ MimeTypeRole
The mime type of the item.
@ ItemRole
The item object.
ItemModel(QObject *parent=0)
Creates a new item model.
QModelIndex indexForItem(const Akonadi::Item &item, const int column) const
Returns the model index for the given item, with the given column.
Item itemForIndex(const QModelIndex &index) const
Returns the item at the given index.
Collection collection() const
Returns the collection being displayed in the model.
virtual ~ItemModel()
Destroys the item model.
void collectionChanged(const Akonadi::Collection &collection)
This signal is emitted whenever setCollection is called.
@ RemoteId
The remote identifier.
@ MimeType
The item's mime type.
ItemFetchScope & fetchScope()
Returns the item fetch scope.
Monitors an item or collection for changes.
void ignoreSession(Session *session)
Ignores all change notifications caused by the given session.
A communication session with the Akonadi storage.
KJob * paste(const QMimeData *mimeData, const Collection &collection, bool copy=true, Session *session=0)
Paste/drop the given mime data into the given collection.
FreeBusyManager::Singleton.