• 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 <kurl.h>
00032 #include <kicon.h>
00033 
00034 #include <QtCore/QMimeData>
00035 #include <QtGui/QPixmap>
00036 
00037 using namespace Akonadi;
00038 
00039 CollectionModel::CollectionModel( QObject * parent ) :
00040     QAbstractItemModel( parent ),
00041     d_ptr( new CollectionModelPrivate( this ) )
00042 {
00043   Q_D( CollectionModel );
00044   d->init();
00045 }
00046 
00047 //@cond PRIVATE
00048 CollectionModel::CollectionModel( CollectionModelPrivate *d,
00049                                   QObject *parent )
00050   : QAbstractItemModel( parent ),
00051     d_ptr( d )
00052 {
00053   d->init();
00054 }
00055 //@endcond
00056 
00057 CollectionModel::~CollectionModel()
00058 {
00059   Q_D( CollectionModel );
00060   d->childCollections.clear();
00061   d->collections.clear();
00062 
00063   delete d->monitor;
00064   d->monitor = 0;
00065 
00066   delete d;
00067 }
00068 
00069 int CollectionModel::columnCount( const QModelIndex & parent ) const
00070 {
00071   if (parent.isValid() && parent.column() != 0)
00072     return 0;
00073   return 1;
00074 }
00075 
00076 QVariant CollectionModel::data( const QModelIndex & index, int role ) const
00077 {
00078   Q_D( const CollectionModel );
00079   if ( !index.isValid() )
00080     return QVariant();
00081 
00082   const Collection col = d->collections.value( index.internalId() );
00083   if ( !col.isValid() )
00084     return QVariant();
00085 
00086   if ( index.column() == 0 && (role == Qt::DisplayRole || role == Qt::EditRole) ) {
00087     if ( col.hasAttribute<EntityDisplayAttribute>() &&
00088          !col.attribute<EntityDisplayAttribute>()->displayName().isEmpty() )
00089       return col.attribute<EntityDisplayAttribute>()->displayName();
00090     return col.name();
00091   }
00092 
00093   switch ( role ) {
00094     case Qt::DecorationRole:
00095       if ( index.column() == 0 ) {
00096         if ( col.hasAttribute<EntityDisplayAttribute>() &&
00097              !col.attribute<EntityDisplayAttribute>()->iconName().isEmpty() )
00098           return col.attribute<EntityDisplayAttribute>()->icon();
00099         return KIcon( CollectionUtils::defaultIconName( col ) );
00100       }
00101       break;
00102     case CollectionIdRole:
00103       return col.id();
00104     case CollectionRole:
00105       return QVariant::fromValue( col );
00106   }
00107   return QVariant();
00108 }
00109 
00110 QModelIndex CollectionModel::index( int row, int column, const QModelIndex & parent ) const
00111 {
00112   Q_D( const CollectionModel );
00113   if (column >= columnCount() || column < 0) return QModelIndex();
00114 
00115   QList<Collection::Id> list;
00116   if ( !parent.isValid() )
00117     list = d->childCollections.value( Collection::root().id() );
00118   else
00119   {
00120     if (parent.column() > 0)
00121        return QModelIndex();
00122     list = d->childCollections.value( parent.internalId() );
00123   }
00124 
00125   if ( row < 0 || row >= list.size() )
00126     return QModelIndex();
00127   if ( !d->collections.contains( list.at(row) ) )
00128     return QModelIndex();
00129   return createIndex( row, column, reinterpret_cast<void*>( d->collections.value( list.at(row) ).id() ) );
00130 }
00131 
00132 QModelIndex CollectionModel::parent( const QModelIndex & index ) const
00133 {
00134   Q_D( const CollectionModel );
00135   if ( !index.isValid() )
00136     return QModelIndex();
00137 
00138   Collection col = d->collections.value( index.internalId() );
00139   if ( !col.isValid() )
00140     return QModelIndex();
00141 
00142 
00143   Collection parentCol = d->collections.value( col.parent() );
00144   if ( !parentCol.isValid() )
00145   {
00146     return QModelIndex();
00147 }
00148   QList<Collection::Id> list;
00149   list = d->childCollections.value( parentCol.parent() );
00150 
00151   int parentRow = list.indexOf( parentCol.id() );
00152   if ( parentRow < 0 )
00153     return QModelIndex();
00154 
00155   return createIndex( parentRow, 0, reinterpret_cast<void*>( parentCol.id() ) );
00156 }
00157 
00158 int CollectionModel::rowCount( const QModelIndex & parent ) const
00159 {
00160   const  Q_D( CollectionModel );
00161   QList<Collection::Id> list;
00162   if ( parent.isValid() )
00163     list = d->childCollections.value( parent.internalId() );
00164   else
00165     list = d->childCollections.value( Collection::root().id() );
00166 
00167   return list.size();
00168 }
00169 
00170 QVariant CollectionModel::headerData( int section, Qt::Orientation orientation, int role ) const
00171 {
00172   const  Q_D( CollectionModel );
00173 
00174   if ( section == 0 && orientation == Qt::Horizontal && role == Qt::DisplayRole )
00175     return d->headerContent;
00176   return QAbstractItemModel::headerData( section, orientation, role );
00177 }
00178 
00179 bool CollectionModel::setHeaderData( int section, Qt::Orientation orientation, const QVariant &value, int role )
00180 {
00181   Q_D( CollectionModel );
00182 
00183   if ( section == 0 && orientation == Qt::Horizontal && role == Qt::EditRole ) {
00184     d->headerContent = value.toString();
00185     return true;
00186   }
00187 
00188   return false;
00189 }
00190 
00191 bool CollectionModel::setData( const QModelIndex & index, const QVariant & value, int role )
00192 {
00193   Q_D( CollectionModel );
00194   if ( index.column() == 0 && role == Qt::EditRole ) {
00195     // rename collection
00196     Collection col = d->collections.value( index.internalId() );
00197     if ( !col.isValid() || value.toString().isEmpty() )
00198       return false;
00199     col.setName( value.toString() );
00200     CollectionModifyJob *job = new CollectionModifyJob( col, d->session );
00201     connect( job, SIGNAL(result(KJob*)), SLOT(editDone(KJob*)) );
00202     return true;
00203   }
00204   return QAbstractItemModel::setData( index, value, role );
00205 }
00206 
00207 Qt::ItemFlags CollectionModel::flags( const QModelIndex & index ) const
00208 {
00209   Q_D( const CollectionModel );
00210 
00211   // Pass modeltest.
00212   // http://labs.trolltech.com/forums/topic/79
00213   if (!index.isValid())
00214     return 0;
00215 
00216   Qt::ItemFlags flags = QAbstractItemModel::flags( index );
00217 
00218   flags = flags | Qt::ItemIsDragEnabled;
00219 
00220   Collection col;
00221   if ( index.isValid() ) {
00222     col = d->collections.value( index.internalId() );
00223     Q_ASSERT( col.isValid() );
00224   }
00225   else
00226     return flags | Qt::ItemIsDropEnabled; // HACK Workaround for a probable bug in Qt
00227 
00228   if ( col.isValid() ) {
00229     if ( col.rights() & (Collection::CanChangeCollection |
00230                          Collection::CanCreateCollection |
00231                          Collection::CanDeleteCollection |
00232                          Collection::CanCreateItem) )  {
00233       if ( index.column() == 0 )
00234         flags = flags | Qt::ItemIsEditable;
00235 
00236       flags = flags | Qt::ItemIsDropEnabled;
00237     }
00238   }
00239 
00240   return flags;
00241 }
00242 
00243 Qt::DropActions CollectionModel::supportedDropActions() const
00244 {
00245   return Qt::CopyAction | Qt::MoveAction;
00246 }
00247 
00248 QStringList CollectionModel::mimeTypes() const
00249 {
00250   return QStringList() << QLatin1String( "text/uri-list" );
00251 }
00252 
00253 QMimeData *CollectionModel::mimeData(const QModelIndexList &indexes) const
00254 {
00255     QMimeData *data = new QMimeData();
00256     KUrl::List urls;
00257     foreach ( const QModelIndex &index, indexes ) {
00258         if ( index.column() != 0 )
00259           continue;
00260 
00261         urls << Collection( index.internalId() ).url();
00262     }
00263     urls.populateMimeData( data );
00264 
00265     return data;
00266 }
00267 
00268 bool CollectionModel::dropMimeData(const QMimeData * data, Qt::DropAction action, int row, int column, const QModelIndex & parent)
00269 {
00270   Q_D( CollectionModel );
00271   if ( !(action & supportedDropActions()) )
00272     return false;
00273 
00274   // handle drops onto items as well as drops between items
00275   QModelIndex idx;
00276   if ( row >= 0 && column >= 0 )
00277     idx = index( row, column, parent );
00278   else
00279     idx = parent;
00280 
00281   if ( !idx.isValid() )
00282     return false;
00283 
00284   const Collection parentCol = d->collections.value( idx.internalId() );
00285   if (!parentCol.isValid())
00286     return false;
00287 
00288   KJob *job = PasteHelper::paste( data, parentCol, action != Qt::MoveAction );
00289   connect( job, SIGNAL(result(KJob*)), SLOT(dropResult(KJob*)) );
00290   return true;
00291 }
00292 
00293 Collection CollectionModel::collectionForId(Collection::Id id) const
00294 {
00295   Q_D( const CollectionModel );
00296   return d->collections.value( id );
00297 }
00298 
00299 void CollectionModel::fetchCollectionStatistics(bool enable)
00300 {
00301   Q_D( CollectionModel );
00302   d->fetchStatistics = enable;
00303   d->monitor->fetchCollectionStatistics( enable );
00304 }
00305 
00306 void CollectionModel::includeUnsubscribed(bool include)
00307 {
00308   Q_D( CollectionModel );
00309   d->unsubscribed = include;
00310 }
00311 
00312 
00313 
00314 #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.7
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