20 #include "collectionmodel.h"
21 #include "collectionmodel_p.h"
23 #include "collectionutils_p.h"
24 #include "collectionmodifyjob.h"
25 #include "entitydisplayattribute.h"
27 #include "pastehelper_p.h"
34 #include <QtCore/QMimeData>
37 using namespace Akonadi;
40 QAbstractItemModel( parent ),
50 : QAbstractItemModel( parent ),
60 d->childCollections.clear();
61 d->collections.clear();
69 int CollectionModel::columnCount(
const QModelIndex & parent )
const
71 if ( parent.isValid() && parent.column() != 0 ) {
77 QVariant CollectionModel::data(
const QModelIndex & index,
int role )
const
80 if ( !index.isValid() ) {
84 const Collection col = d->collections.value( index.internalId() );
89 if ( index.column() == 0 && ( role == Qt::DisplayRole || role == Qt::EditRole ) ) {
98 case Qt::DecorationRole:
99 if ( index.column() == 0 ) {
104 return KIcon( CollectionUtils::defaultIconName( col ) );
112 return QVariant::fromValue( col );
117 QModelIndex CollectionModel::index(
int row,
int column,
const QModelIndex & parent )
const
120 if ( column >= columnCount() || column < 0 ) {
121 return QModelIndex();
124 QVector<Collection::Id> list;
125 if ( !parent.isValid() ) {
128 if ( parent.column() > 0 ) {
129 return QModelIndex();
131 list = d->childCollections.value( parent.internalId() );
134 if ( row < 0 || row >= list.size() ) {
135 return QModelIndex();
137 if ( !d->collections.contains( list.at( row ) ) ) {
138 return QModelIndex();
140 return createIndex( row, column, reinterpret_cast<void*>( d->collections.value( list.at(row) ).
id() ) );
143 QModelIndex CollectionModel::parent(
const QModelIndex & index )
const
146 if ( !index.isValid() ) {
147 return QModelIndex();
150 Collection col = d->collections.value( index.internalId() );
152 return QModelIndex();
158 return QModelIndex();
160 QVector<Collection::Id> list;
163 int parentRow = list.indexOf( parentCol.
id() );
164 if ( parentRow < 0 ) {
165 return QModelIndex();
168 return createIndex( parentRow, 0, reinterpret_cast<void*>( parentCol.
id() ) );
171 int CollectionModel::rowCount(
const QModelIndex & parent )
const
174 QVector<Collection::Id> list;
175 if ( parent.isValid() ) {
176 list = d->childCollections.value( parent.internalId() );
184 QVariant CollectionModel::headerData(
int section, Qt::Orientation orientation,
int role )
const
188 if ( section == 0 && orientation == Qt::Horizontal && role == Qt::DisplayRole ) {
189 return d->headerContent;
191 return QAbstractItemModel::headerData( section, orientation, role );
194 bool CollectionModel::setHeaderData(
int section, Qt::Orientation orientation,
const QVariant &value,
int role )
198 if ( section == 0 && orientation == Qt::Horizontal && role == Qt::EditRole ) {
199 d->headerContent = value.toString();
206 bool CollectionModel::setData(
const QModelIndex & index,
const QVariant & value,
int role )
209 if ( index.column() == 0 && role == Qt::EditRole ) {
211 Collection col = d->collections.value( index.internalId() );
212 if ( !col.
isValid() || value.toString().isEmpty() ) {
215 col.
setName( value.toString() );
217 connect( job, SIGNAL(result(KJob*)), SLOT(editDone(KJob*)) );
220 return QAbstractItemModel::setData( index, value, role );
223 Qt::ItemFlags CollectionModel::flags(
const QModelIndex & index )
const
229 if ( !index.isValid() ) {
233 Qt::ItemFlags flags = QAbstractItemModel::flags( index );
235 flags = flags | Qt::ItemIsDragEnabled;
238 if ( index.isValid() ) {
239 col = d->collections.value( index.internalId() );
242 return flags | Qt::ItemIsDropEnabled;
250 if ( index.column() == 0 ) {
251 flags = flags | Qt::ItemIsEditable;
253 flags = flags | Qt::ItemIsDropEnabled;
260 Qt::DropActions CollectionModel::supportedDropActions()
const
262 return Qt::CopyAction | Qt::MoveAction;
265 QStringList CollectionModel::mimeTypes()
const
267 return QStringList() << QLatin1String(
"text/uri-list" );
270 QMimeData *CollectionModel::mimeData(
const QModelIndexList &indexes)
const
272 QMimeData *data =
new QMimeData();
274 foreach (
const QModelIndex &index, indexes ) {
275 if ( index.column() != 0 ) {
279 urls <<
Collection( index.internalId() ).url();
281 urls.populateMimeData( data );
286 bool CollectionModel::dropMimeData(
const QMimeData * data, Qt::DropAction action,
int row,
int column,
const QModelIndex & parent)
289 if ( !( action & supportedDropActions() ) ) {
295 if ( row >= 0 && column >= 0 ) {
296 idx = index( row, column, parent );
301 if ( !idx.isValid() ) {
305 const Collection parentCol = d->collections.value( idx.internalId() );
311 connect( job, SIGNAL(result(KJob*)), SLOT(dropResult(KJob*)) );
318 return d->collections.value(
id );
324 d->fetchStatistics = enable;
331 d->unsubscribed = include;
336 #include "moc_collectionmodel.cpp"
Job that modifies a collection in the Akonadi storage.
void fetchCollectionStatistics(bool enable)
Enables automatic fetching of changed collection statistics information from the Akonadi storage...
QString name() const
Returns the i18n'ed name of the collection.
Represents a collection of PIM items.
KJob * paste(const QMimeData *mimeData, const Collection &collection, bool copy=true, Session *session=0)
Paste/drop the given mime data into the given collection.
qint64 Id
Describes the unique id type.
Can create new subcollections in this collection.
void setName(const QString &name)
Sets the i18n'ed name of the collection.
The actual collection object. For binary compatibility to <4.3.
Attribute * attribute(const QByteArray &name) const
Returns the attribute of the given type name if available, 0 otherwise.
Collection collectionForId(Collection::Id id) const
Returns the collection for a given collection id.
Collection parentCollection() const
Returns the parent collection of this object.
void fetchCollectionStatistics(bool enable)
Sets whether collection statistics information shall be provided by the model.
Can create new items in this collection.
static Collection root()
Returns the root collection.
Can delete this collection.
Id id() const
Returns the unique identifier of the entity.
Rights rights() const
Returns the rights the user has on the collection.
void includeUnsubscribed(bool include=true)
Sets whether unsubscribed collections shall be listed in the model.
virtual ~CollectionModel()
Destroys the collection model.
The actual collection object.
CollectionModel(QObject *parent=0)
Creates a new collection model.
bool hasAttribute(const QByteArray &name) const
Returns true if the entity has an attribute of the given type name, false otherwise.
The collection identifier. For binary compatibility to <4.3.
bool isValid() const
Returns whether the entity is valid.
The collection identifier.
Attribute that stores the properties that are used to display an entity.
Can change this collection.