• Skip to content
  • Skip to link menu
KDE 4.2 API Reference
  • KDE API Reference
  • KDE-PIM Libraries
  • Sitemap
  • Contact Us
 

akonadi

collectionmodel.cpp

00001 /*
00002     Copyright (c) 2006 - 2008 Volker Krause <vkrause@kde.org>
00003 
00004     This library is free software; you can redistribute it and/or modify it
00005     under the terms of the GNU Library General Public License as published by
00006     the Free Software Foundation; either version 2 of the License, or (at your
00007     option) any later version.
00008 
00009     This library is distributed in the hope that it will be useful, but WITHOUT
00010     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00011     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Library General Public
00012     License for more details.
00013 
00014     You should have received a copy of the GNU Library General Public License
00015     along with this library; see the file COPYING.LIB.  If not, write to the
00016     Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
00017     02110-1301, USA.
00018 */
00019 
00020 #include "collectionmodel.h"
00021 #include "collectionmodel_p.h"
00022 
00023 #include "collectionutils_p.h"
00024 #include "collectionmodifyjob.h"
00025 #include "entitydisplayattribute.h"
00026 #include "monitor.h"
00027 #include "pastehelper.h"
00028 #include "session.h"
00029 
00030 #include <kdebug.h>
00031 #include <klocale.h>
00032 #include <kurl.h>
00033 #include <kicon.h>
00034 
00035 #include <QtCore/QMimeData>
00036 #include <QtGui/QPixmap>
00037 
00038 using namespace Akonadi;
00039 
00040 CollectionModel::CollectionModel( QObject * parent ) :
00041     QAbstractItemModel( parent ),
00042     d_ptr( new CollectionModelPrivate( this ) )
00043 {
00044   Q_D( CollectionModel );
00045   d->init();
00046 }
00047 
00048 //@cond PRIVATE
00049 CollectionModel::CollectionModel( CollectionModelPrivate *d,
00050                                   QObject *parent )
00051   : QAbstractItemModel( parent ),
00052     d_ptr( d )
00053 {
00054   d->init();
00055 }
00056 //@endcond
00057 
00058 CollectionModel::~CollectionModel()
00059 {
00060   Q_D( CollectionModel );
00061   d->childCollections.clear();
00062   d->collections.clear();
00063 
00064   delete d->monitor;
00065   d->monitor = 0;
00066 
00067   delete d;
00068 }
00069 
00070 int CollectionModel::columnCount( const QModelIndex & parent ) const
00071 {
00072   if (parent.isValid() && parent.column() != 0)
00073     return 0;
00074   return 1;
00075 }
00076 
00077 QVariant CollectionModel::data( const QModelIndex & index, int role ) const
00078 {
00079   Q_D( const CollectionModel );
00080   if ( !index.isValid() )
00081     return QVariant();
00082 
00083   const Collection col = d->collections.value( index.internalId() );
00084   if ( !col.isValid() )
00085     return QVariant();
00086 
00087   if ( index.column() == 0 && (role == Qt::DisplayRole || role == Qt::EditRole) ) {
00088     if ( col.hasAttribute<EntityDisplayAttribute>() &&
00089          !col.attribute<EntityDisplayAttribute>()->displayName().isEmpty() )
00090       return col.attribute<EntityDisplayAttribute>()->displayName();
00091     return col.name();
00092   }
00093 
00094   switch ( role ) {
00095     case Qt::DecorationRole:
00096       if ( index.column() == 0 ) {
00097         if ( col.hasAttribute<EntityDisplayAttribute>() &&
00098              !col.attribute<EntityDisplayAttribute>()->iconName().isEmpty() )
00099           return col.attribute<EntityDisplayAttribute>()->icon();
00100         return KIcon( CollectionUtils::defaultIconName( col ) );
00101       }
00102       break;
00103     case CollectionIdRole:
00104       return col.id();
00105     case CollectionRole:
00106       return QVariant::fromValue( col );
00107   }
00108   return QVariant();
00109 }
00110 
00111 QModelIndex CollectionModel::index( int row, int column, const QModelIndex & parent ) const
00112 {
00113   Q_D( const CollectionModel );
00114   if (column >= columnCount() || column < 0) return QModelIndex();
00115 
00116   QList<Collection::Id> list;
00117   if ( !parent.isValid() )
00118     list = d->childCollections.value( Collection::root().id() );
00119   else
00120   {
00121     if (parent.column() > 0)
00122        return QModelIndex();
00123     list = d->childCollections.value( parent.internalId() );
00124   }
00125 
00126   if ( row < 0 || row >= list.size() )
00127     return QModelIndex();
00128   if ( !d->collections.contains( list.at(row) ) )
00129     return QModelIndex();
00130   return createIndex( row, column, reinterpret_cast<void*>( d->collections.value( list.at(row) ).id() ) );
00131 }
00132 
00133 QModelIndex CollectionModel::parent( const QModelIndex & index ) const
00134 {
00135   Q_D( const CollectionModel );
00136   if ( !index.isValid() )
00137     return QModelIndex();
00138 
00139   Collection col = d->collections.value( index.internalId() );
00140   if ( !col.isValid() )
00141     return QModelIndex();
00142 
00143 
00144   Collection parentCol = d->collections.value( col.parent() );
00145   if ( !parentCol.isValid() )
00146   {
00147     return QModelIndex();
00148 }
00149   QList<Collection::Id> list;
00150   list = d->childCollections.value( parentCol.parent() );
00151 
00152   int parentRow = list.indexOf( parentCol.id() );
00153   if ( parentRow < 0 )
00154     return QModelIndex();
00155 
00156   return createIndex( parentRow, 0, reinterpret_cast<void*>( parentCol.id() ) );
00157 }
00158 
00159 int CollectionModel::rowCount( const QModelIndex & parent ) const
00160 {
00161   const  Q_D( CollectionModel );
00162   QList<Collection::Id> list;
00163   if ( parent.isValid() )
00164     list = d->childCollections.value( parent.internalId() );
00165   else
00166     list = d->childCollections.value( Collection::root().id() );
00167 
00168   return list.size();
00169 }
00170 
00171 QVariant CollectionModel::headerData( int section, Qt::Orientation orientation, int role ) const
00172 {
00173   if ( section == 0 && orientation == Qt::Horizontal && role == Qt::DisplayRole )
00174     return i18nc( "@title:column, name of a thing", "Name" );
00175   return QAbstractItemModel::headerData( section, orientation, role );
00176 }
00177 
00178 bool CollectionModel::setData( const QModelIndex & index, const QVariant & value, int role )
00179 {
00180   Q_D( CollectionModel );
00181   if ( index.column() == 0 && role == Qt::EditRole ) {
00182     // rename collection
00183     Collection col = d->collections.value( index.internalId() );
00184     if ( !col.isValid() || value.toString().isEmpty() )
00185       return false;
00186     col.setName( value.toString() );
00187     CollectionModifyJob *job = new CollectionModifyJob( col, d->session );
00188     connect( job, SIGNAL(result(KJob*)), SLOT(editDone(KJob*)) );
00189     return true;
00190   }
00191   return QAbstractItemModel::setData( index, value, role );
00192 }
00193 
00194 Qt::ItemFlags CollectionModel::flags( const QModelIndex & index ) const
00195 {
00196   Q_D( const CollectionModel );
00197 
00198   // Pass modeltest.
00199   // http://labs.trolltech.com/forums/topic/79
00200   if (!index.isValid())
00201     return 0;
00202 
00203   Qt::ItemFlags flags = QAbstractItemModel::flags( index );
00204 
00205   flags = flags | Qt::ItemIsDragEnabled;
00206 
00207   Collection col;
00208   if ( index.isValid() ) {
00209     col = d->collections.value( index.internalId() );
00210     Q_ASSERT( col.isValid() );
00211   }
00212   else
00213     return flags | Qt::ItemIsDropEnabled; // HACK Workaround for a probable bug in Qt
00214 
00215   if ( col.isValid() ) {
00216     if ( col.rights() & (Collection::CanChangeCollection |
00217                          Collection::CanCreateCollection |
00218                          Collection::CanDeleteCollection |
00219                          Collection::CanCreateItem) )  {
00220       if ( index.column() == 0 )
00221         flags = flags | Qt::ItemIsEditable;
00222 
00223       flags = flags | Qt::ItemIsDropEnabled;
00224     }
00225   }
00226 
00227   return flags;
00228 }
00229 
00230 Qt::DropActions CollectionModel::supportedDropActions() const
00231 {
00232   return Qt::CopyAction | Qt::MoveAction;
00233 }
00234 
00235 QStringList CollectionModel::mimeTypes() const
00236 {
00237   return QStringList() << QLatin1String( "text/uri-list" );
00238 }
00239 
00240 QMimeData *CollectionModel::mimeData(const QModelIndexList &indexes) const
00241 {
00242     QMimeData *data = new QMimeData();
00243     KUrl::List urls;
00244     foreach ( const QModelIndex &index, indexes ) {
00245         if ( index.column() != 0 )
00246           continue;
00247 
00248         urls << Collection( index.internalId() ).url();
00249     }
00250     urls.populateMimeData( data );
00251 
00252     return data;
00253 }
00254 
00255 bool CollectionModel::dropMimeData(const QMimeData * data, Qt::DropAction action, int row, int column, const QModelIndex & parent)
00256 {
00257   Q_D( CollectionModel );
00258   if ( !(action & supportedDropActions()) )
00259     return false;
00260 
00261   // handle drops onto items as well as drops between items
00262   QModelIndex idx;
00263   if ( row >= 0 && column >= 0 )
00264     idx = index( row, column, parent );
00265   else
00266     idx = parent;
00267 
00268   if ( !idx.isValid() )
00269     return false;
00270 
00271   const Collection parentCol = d->collections.value( idx.internalId() );
00272   if (!parentCol.isValid())
00273     return false;
00274 
00275   KJob *job = PasteHelper::paste( data, parentCol, action != Qt::MoveAction );
00276   connect( job, SIGNAL(result(KJob*)), SLOT(dropResult(KJob*)) );
00277   return true;
00278 }
00279 
00280 Collection CollectionModel::collectionForId(Collection::Id id) const
00281 {
00282   Q_D( const CollectionModel );
00283   return d->collections.value( id );
00284 }
00285 
00286 void CollectionModel::fetchCollectionStatistics(bool enable)
00287 {
00288   Q_D( CollectionModel );
00289   d->fetchStatistics = enable;
00290   d->monitor->fetchCollectionStatistics( enable );
00291 }
00292 
00293 void CollectionModel::includeUnsubscribed(bool include)
00294 {
00295   Q_D( CollectionModel );
00296   d->unsubscribed = include;
00297 }
00298 
00299 
00300 
00301 #include "collectionmodel.moc"

akonadi

Skip menu "akonadi"
  • Main Page
  • Modules
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members
  • Related Pages

KDE-PIM Libraries

Skip menu "KDE-PIM Libraries"
  • akonadi
  • kabc
  • kblog
  • kcal
  • kimap
  • kioslave
  •   imap4
  •   mbox
  • kldap
  • kmime
  • kpimidentities
  •   richtextbuilders
  • kpimutils
  • kresources
  • ktnef
  • kxmlrpcclient
  • mailtransport
  • qgpgme
  • syndication
  •   atom
  •   rdf
  •   rss2
Generated for KDE-PIM Libraries by doxygen 1.5.8
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal