Verified Commit 4975a22c authored by Daniel Vrátil's avatar Daniel Vrátil 🤖
Browse files

Remove the deprecated CollectionModel

The CollectionModel header wasn't even installed so all usages
were only internal and have been ported to ETM.
parent b6b12f7e
......@@ -141,8 +141,6 @@ set(akonadicore_models_SRCS
models/agentinstancemodel.cpp
models/agenttypemodel.cpp
models/collectionfilterproxymodel.cpp
models/collectionmodel.cpp
models/collectionmodel_p.cpp
models/entitymimetypefiltermodel.cpp
models/entityorderproxymodel.cpp
models/entityrightsfiltermodel.cpp
......
/*
Copyright (c) 2006 - 2008 Volker Krause <vkrause@kde.org>
This library is free software; you can redistribute it and/or modify it
under the terms of the GNU Library General Public License as published by
the Free Software Foundation; either version 2 of the License, or (at your
option) any later version.
This library is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
License for more details.
You should have received a copy of the GNU Library General Public License
along with this library; see the file COPYING.LIB. If not, write to the
Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301, USA.
*/
#include "collectionmodel.h"
#include "collectionmodel_p.h"
#include "collectionutils.h"
#include "collectionmodifyjob.h"
#include "entitydisplayattribute.h"
#include "monitor.h"
#include "pastehelper_p.h"
#include "session.h"
#include <QUrl>
#include <QMimeData>
using namespace Akonadi;
CollectionModel::CollectionModel(QObject *parent)
: QAbstractItemModel(parent)
, d_ptr(new CollectionModelPrivate(this))
{
Q_D(CollectionModel);
d->init();
}
//@cond PRIVATE
CollectionModel::CollectionModel(CollectionModelPrivate *d, QObject *parent)
: QAbstractItemModel(parent)
, d_ptr(d)
{
d->init();
}
//@endcond
CollectionModel::~CollectionModel()
{
Q_D(CollectionModel);
d->childCollections.clear();
d->collections.clear();
delete d->monitor;
d->monitor = nullptr;
delete d;
}
int CollectionModel::columnCount(const QModelIndex &parent) const
{
if (parent.isValid() && parent.column() != 0) {
return 0;
}
return 1;
}
QVariant CollectionModel::data(const QModelIndex &index, int role) const
{
Q_D(const CollectionModel);
if (!index.isValid()) {
return QVariant();
}
const Collection col = d->collections.value(index.internalId());
if (!col.isValid()) {
return QVariant();
}
if (index.column() == 0 && (role == Qt::DisplayRole || role == Qt::EditRole)) {
return col.displayName();
}
switch (role) {
case Qt::DecorationRole:
if (index.column() == 0) {
return d->iconForCollection(col);
}
break;
case CollectionIdRole:
return col.id();
case CollectionRole:
return QVariant::fromValue(col);
}
return QVariant();
}
QModelIndex CollectionModel::index(int row, int column, const QModelIndex &parent) const
{
Q_D(const CollectionModel);
if (column >= columnCount() || column < 0) {
return QModelIndex();
}
QVector<Collection::Id> list;
if (!parent.isValid()) {
list = d->childCollections.value(Collection::root().id());
} else {
if (parent.column() > 0) {
return QModelIndex();
}
list = d->childCollections.value(parent.internalId());
}
if (row < 0 || row >= list.size()) {
return QModelIndex();
}
if (!d->collections.contains(list.at(row))) {
return QModelIndex();
}
return createIndex(row, column, reinterpret_cast<void *>(d->collections.value(list.at(row)).id()));
}
QModelIndex CollectionModel::parent(const QModelIndex &index) const
{
Q_D(const CollectionModel);
if (!index.isValid()) {
return QModelIndex();
}
const Collection col = d->collections.value(index.internalId());
if (!col.isValid()) {
return QModelIndex();
}
const Collection parentCol = d->collections.value(col.parentCollection().id());
if (!parentCol.isValid()) {
return QModelIndex();
}
const QVector<Collection::Id> list = d->childCollections.value(parentCol.parentCollection().id());
int parentRow = list.indexOf(parentCol.id());
if (parentRow < 0) {
return QModelIndex();
}
return createIndex(parentRow, 0, reinterpret_cast<void *>(parentCol.id()));
}
int CollectionModel::rowCount(const QModelIndex &parent) const
{
const Q_D(CollectionModel);
QVector<Collection::Id> list;
if (parent.isValid()) {
list = d->childCollections.value(parent.internalId());
} else {
list = d->childCollections.value(Collection::root().id());
}
return list.size();
}
QVariant CollectionModel::headerData(int section, Qt::Orientation orientation, int role) const
{
const Q_D(CollectionModel);
if (section == 0 && orientation == Qt::Horizontal && role == Qt::DisplayRole) {
return d->headerContent;
}
return QAbstractItemModel::headerData(section, orientation, role);
}
bool CollectionModel::setHeaderData(int section, Qt::Orientation orientation, const QVariant &value, int role)
{
Q_D(CollectionModel);
if (section == 0 && orientation == Qt::Horizontal && role == Qt::EditRole) {
d->headerContent = value.toString();
return true;
}
return false;
}
bool CollectionModel::setData(const QModelIndex &index, const QVariant &value, int role)
{
Q_D(CollectionModel);
if (index.column() == 0 && role == Qt::EditRole) {
// rename collection
Collection col = d->collections.value(index.internalId());
if (!col.isValid() || value.toString().isEmpty()) {
return false;
}
col.setName(value.toString());
CollectionModifyJob *job = new CollectionModifyJob(col, d->session);
connect(job, &CollectionModifyJob::result, this, [d](KJob* job) { d->editDone(job); });
return true;
}
return QAbstractItemModel::setData(index, value, role);
}
Qt::ItemFlags CollectionModel::flags(const QModelIndex &index) const
{
Q_D(const CollectionModel);
// Pass modeltest.
if (!index.isValid()) {
return {};
}
Qt::ItemFlags flags = QAbstractItemModel::flags(index);
flags = flags | Qt::ItemIsDragEnabled;
Collection col;
if (index.isValid()) {
col = d->collections.value(index.internalId());
Q_ASSERT(col.isValid());
} else {
return flags | Qt::ItemIsDropEnabled; // HACK Workaround for a probable bug in Qt
}
if (col.isValid()) {
if (col.rights() & (Collection::CanChangeCollection |
Collection::CanCreateCollection |
Collection::CanDeleteCollection |
Collection::CanCreateItem)) {
if (index.column() == 0) {
flags = flags | Qt::ItemIsEditable;
}
flags = flags | Qt::ItemIsDropEnabled;
}
}
return flags;
}
Qt::DropActions CollectionModel::supportedDropActions() const
{
return Qt::CopyAction | Qt::MoveAction;
}
QStringList CollectionModel::mimeTypes() const
{
return {QStringLiteral("text/uri-list")};
}
QMimeData *CollectionModel::mimeData(const QModelIndexList &indexes) const
{
QMimeData *data = new QMimeData();
QList<QUrl> urls;
for (const QModelIndex &index : indexes) {
if (index.column() != 0) {
continue;
}
urls << Collection(index.internalId()).url();
}
data->setUrls(urls);
return data;
}
bool CollectionModel::dropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent)
{
Q_D(CollectionModel);
if (!(action & supportedDropActions())) {
return false;
}
// handle drops onto items as well as drops between items
QModelIndex idx;
if (row >= 0 && column >= 0) {
idx = index(row, column, parent);
} else {
idx = parent;
}
if (!idx.isValid()) {
return false;
}
const Collection parentCol = d->collections.value(idx.internalId());
if (!parentCol.isValid()) {
return false;
}
KJob *job = PasteHelper::paste(data, parentCol, action != Qt::MoveAction);
connect(job, SIGNAL(result(KJob*)), SLOT(dropResult(KJob*)));
return true;
}
Collection CollectionModel::collectionForId(Collection::Id id) const
{
Q_D(const CollectionModel);
return d->collections.value(id);
}
void CollectionModel::fetchCollectionStatistics(bool enable)
{
Q_D(CollectionModel);
d->fetchStatistics = enable;
d->monitor->fetchCollectionStatistics(enable);
}
void CollectionModel::includeUnsubscribed(bool include)
{
Q_D(CollectionModel);
d->unsubscribed = include;
}
#include "moc_collectionmodel.cpp"
/*
Copyright (c) 2006 - 2008 Volker Krause <vkrause@kde.org>
This library is free software; you can redistribute it and/or modify it
under the terms of the GNU Library General Public License as published by
the Free Software Foundation; either version 2 of the License, or (at your
option) any later version.
This library is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
License for more details.
You should have received a copy of the GNU Library General Public License
along with this library; see the file COPYING.LIB. If not, write to the
Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301, USA.
*/
#ifndef AKONADI_COLLECTIONMODEL_H
#define AKONADI_COLLECTIONMODEL_H
#include "akonadicore_export.h"
#include "collection.h"
#include <QAbstractItemModel>
namespace Akonadi
{
class CollectionModelPrivate;
/**
* @short A model for collections.
*
* This class provides the interface of QAbstractItemModel for the
* collection tree of the Akonadi storage.
*
* @code
*
* Akonadi::CollectionModel *model = new Akonadi::CollectionModel( this );
*
* QTreeView *view = new QTreeView( this );
* view->setModel( model );
*
* @endcode
*
* If you want to list only collections of a special mime type, use
* CollectionFilterProxyModel on top of this model.
*
* @author Volker Krause <vkrause@kde.org>
* @deprecated Use Akonadi::EntityTreeModel instead
*/
class AKONADICORE_DEPRECATED_EXPORT CollectionModel : public QAbstractItemModel
{
Q_OBJECT
public:
/**
* Describes the roles for collections.
*/
enum Roles {
CollectionIdRole = Qt::UserRole + 10, ///< The collection identifier.
CollectionRole = Qt::UserRole + 11, ///< The actual collection object.
UserRole = Qt::UserRole + 42 ///< Role for user extensions.
};
/**
* Creates a new collection model.
*
* @param parent The parent object.
*/
explicit CollectionModel(QObject *parent = nullptr);
/**
* Destroys the collection model.
*/
~CollectionModel() override;
/**
* Sets whether collection statistics information shall be provided
* by the model.
*
* @see CollectionStatistics.
* @param enable whether to fetch collection statistics
*/
void fetchCollectionStatistics(bool enable);
/**
* Sets whether unsubscribed collections shall be listed in the model.
*/
void includeUnsubscribed(bool include = true);
Q_REQUIRED_RESULT int columnCount(const QModelIndex &parent = QModelIndex()) const override;
Q_REQUIRED_RESULT QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
Q_REQUIRED_RESULT QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const override;
Q_REQUIRED_RESULT QModelIndex parent(const QModelIndex &index) const override;
Q_REQUIRED_RESULT int rowCount(const QModelIndex &parent = QModelIndex()) const override;
Q_REQUIRED_RESULT QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const override;
Q_REQUIRED_RESULT bool setHeaderData(int section, Qt::Orientation orientation, const QVariant &value, int role = Qt::EditRole) override;
Q_REQUIRED_RESULT bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole) override;
Q_REQUIRED_RESULT Qt::ItemFlags flags(const QModelIndex &index) const override;
Q_REQUIRED_RESULT Qt::DropActions supportedDropActions() const override;
Q_REQUIRED_RESULT QMimeData *mimeData(const QModelIndexList &indexes) const override;
Q_REQUIRED_RESULT bool dropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent) override;
Q_REQUIRED_RESULT QStringList mimeTypes() const override;
protected:
/**
* Returns the collection for a given collection @p id.
*/
Collection collectionForId(Collection::Id id) const;
//@cond PRIVATE
Akonadi::CollectionModelPrivate *d_ptr;
explicit CollectionModel(CollectionModelPrivate *d, QObject *parent = nullptr);
//@endcond
private:
Q_DECLARE_PRIVATE(CollectionModel)
//@cond PRIVATE
Q_PRIVATE_SLOT(d_func(), void startFirstListJob())
Q_PRIVATE_SLOT(d_func(), void collectionRemoved(const Akonadi::Collection &))
Q_PRIVATE_SLOT(d_func(), void collectionChanged(const Akonadi::Collection &))
Q_PRIVATE_SLOT(d_func(), void updateDone(KJob *))
Q_PRIVATE_SLOT(d_func(), void collectionStatisticsChanged(Akonadi::Collection::Id,
const Akonadi::CollectionStatistics &))
Q_PRIVATE_SLOT(d_func(), void listDone(KJob *))
Q_PRIVATE_SLOT(d_func(), void dropResult(KJob *))
Q_PRIVATE_SLOT(d_func(), void collectionsChanged(const Akonadi::Collection::List &))
//@endcond
};
}
#endif
/*
Copyright (c) 2006 - 2008 Volker Krause <vkrause@kde.org>
This library is free software; you can redistribute it and/or modify it
under the terms of the GNU Library General Public License as published by
the Free Software Foundation; either version 2 of the License, or (at your
option) any later version.
This library is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
License for more details.
You should have received a copy of the GNU Library General Public License
along with this library; see the file COPYING.LIB. If not, write to the
Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301, USA.
*/
//@cond PRIVATE
#include "collectionmodel_p.h"
#include "collectionmodel.h"
#include "collectionutils.h"
#include "collectionfetchjob.h"
#include "collectionstatistics.h"
#include "collectionstatisticsjob.h"
#include "monitor.h"
#include "session.h"
#include "collectionfetchscope.h"
#include "akonadicore_debug.h"
#include <KJob>
#include <QCoreApplication>
#include <QTimer>
using namespace Akonadi;
void CollectionModelPrivate::collectionRemoved(const Akonadi::Collection &collection)
{
Q_Q(CollectionModel);
QModelIndex colIndex = indexForId(collection.id());
if (colIndex.isValid()) {
QModelIndex parentIndex = q->parent(colIndex);
// collection is still somewhere in the hierarchy
removeRowFromModel(colIndex.row(), parentIndex);
} else {
if (collections.contains(collection.id())) {
// collection is orphan, ie. the parent has been removed already
collections.remove(collection.id());
childCollections.remove(collection.id());
}
}
}
void CollectionModelPrivate::collectionChanged(const Akonadi::Collection &collection)
{
Q_Q(CollectionModel);
// What kind of change is it ?
Collection::Id oldParentId = collections.value(collection.id()).parentCollection().id();
Collection::Id newParentId = collection.parentCollection().id();
if (newParentId != oldParentId && oldParentId >= 0) { // It's a move
removeRowFromModel(indexForId(collections[collection.id()].id()).row(), indexForId(oldParentId));
Collection newParent;
if (newParentId == Collection::root().id()) {
newParent = Collection::root();
} else {
newParent = collections.value(newParentId);
}
CollectionFetchJob *job = new CollectionFetchJob(newParent, CollectionFetchJob::Recursive, session);
job->fetchScope().setListFilter(unsubscribed ? CollectionFetchScope::NoFilter : CollectionFetchScope::Enabled);
job->fetchScope().setIncludeStatistics(fetchStatistics);
q->connect(job, SIGNAL(collectionsReceived(Akonadi::Collection::List)),
q, SLOT(collectionsChanged(Akonadi::Collection::List)));
q->connect(job, SIGNAL(result(KJob*)),
q, SLOT(listDone(KJob*)));
} else { // It's a simple change
CollectionFetchJob *job = new CollectionFetchJob(collection, CollectionFetchJob::Base, session);
job->fetchScope().setListFilter(unsubscribed ? CollectionFetchScope::NoFilter : CollectionFetchScope::Enabled);
job->fetchScope().setIncludeStatistics(fetchStatistics);
q->connect(job, SIGNAL(collectionsReceived(Akonadi::Collection::List)),
q, SLOT(collectionsChanged(Akonadi::Collection::List)));
q->connect(job, SIGNAL(result(KJob*)),
q, SLOT(listDone(KJob*)));
}
}
void CollectionModelPrivate::updateDone(KJob *job)
{
if (job->error()) {
// TODO: handle job errors
qCWarning(AKONADICORE_LOG) << "Job error:" << job->errorString();
} else {
CollectionStatisticsJob *csjob = static_cast<CollectionStatisticsJob *>(job);
Collection result = csjob->collection();
collectionStatisticsChanged(result.id(), csjob->statistics());
}
}
void CollectionModelPrivate::collectionStatisticsChanged(Collection::Id collection,
const Akonadi::CollectionStatistics &statistics)
{
Q_Q(CollectionModel);
if (collections.contains(collection)) {
collections[collection].setStatistics(statistics);
Collection col = collections.value(collection);
QModelIndex startIndex = indexForId(col.id());
QModelIndex endIndex = indexForId(col.id(), q->columnCount(q->parent(startIndex)) - 1);
Q_EMIT q->dataChanged(startIndex, endIndex);
} else {
qCWarning(AKONADICORE_LOG) << "Got statistics response for non-existing collection:" << collection;
}
}
void CollectionModelPrivate::listDone(KJob *job)
{
if (job->error()) {
qCWarning(AKONADICORE_LOG) << "Job error: " << job->errorString();
}
}
void CollectionModelPrivate::editDone(KJob *job)
{
if (job->error()) {
qCWarning(AKONADICORE_LOG) << "Edit failed: " << job->errorString();
}
}
void CollectionModelPrivate::dropResult(KJob *job)
{
if (job->error()) {
qCWarning(AKONADICORE_LOG) << "Paste failed:" << job->errorString();
// TODO: error handling