• Skip to content
  • Skip to link menu
KDE 4.1 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 #include "collectionutils_p.h"
00023 
00024 #include "collectionmodifyjob.h"
00025 #include "monitor.h"
00026 #include "pastehelper.h"
00027 #include "session.h"
00028 
00029 #include <kdebug.h>
00030 #include <klocale.h>
00031 #include <kurl.h>
00032 #include <kiconloader.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 CollectionModel::CollectionModel( CollectionModelPrivate *d,
00048                                   QObject *parent )
00049   : QAbstractItemModel( parent ),
00050     d_ptr( d )
00051 {
00052   d->init();
00053 }
00054 
00055 CollectionModel::~CollectionModel()
00056 {
00057   Q_D( CollectionModel );
00058   d->childCollections.clear();
00059   d->collections.clear();
00060 
00061   delete d->monitor;
00062   d->monitor = 0;
00063 
00064   delete d;
00065 }
00066 
00067 int CollectionModel::columnCount( const QModelIndex & parent ) const
00068 {
00069   if (parent.isValid() && parent.column() != 0)
00070     return 0;
00071   return 1;
00072 }
00073 
00074 QVariant CollectionModel::data( const QModelIndex & index, int role ) const
00075 {
00076   Q_D( const CollectionModel );
00077   if ( !index.isValid() )
00078     return QVariant();
00079 
00080   Collection col = d->collections.value( index.internalId() );
00081   if ( !col.isValid() )
00082     return QVariant();
00083 
00084   if ( index.column() == 0 && (role == Qt::DisplayRole || role == Qt::EditRole) ) {
00085     return col.name();
00086   }
00087 
00088   switch ( role ) {
00089     case Qt::DecorationRole:
00090       if ( index.column() == 0 ) {
00091         if ( CollectionUtils::isVirtualParent( col ) )
00092           return SmallIcon( QLatin1String( "edit-find" ) );
00093         if ( CollectionUtils::isVirtual( col ) )
00094           return SmallIcon( QLatin1String( "folder-violet" ) );
00095         if ( CollectionUtils::isResource( col ) )
00096           return SmallIcon( QLatin1String( "network-wired" ) );
00097         if ( CollectionUtils::isStructural( col ) )
00098           return SmallIcon( QLatin1String( "folder-grey" ) );
00099 
00100         const QStringList content = col.contentMimeTypes();
00101         if ( content.size() == 1 || (content.size() == 2 && content.contains( Collection::mimeType() )) ) {
00102           if ( content.contains( QLatin1String( "text/x-vcard" ) ) || content.contains( QLatin1String( "text/directory" ) )
00103                                                                    || content.contains( QLatin1String( "text/vcard" ) ) )
00104             return SmallIcon( QLatin1String( "kmgroupware_folder_contacts" ) );
00105           // TODO: add all other content types and/or fix their mimetypes
00106           if ( content.contains( QLatin1String( "akonadi/event" ) ) || content.contains( QLatin1String( "text/ical" ) ) )
00107             return SmallIcon( QLatin1String( "kmgroupware_folder_calendar" ) );
00108           if ( content.contains( QLatin1String( "akonadi/task" ) ) )
00109             return SmallIcon( QLatin1String( "kmgroupware_folder_tasks" ) );
00110           return SmallIcon( QLatin1String( "folder" ) );
00111         } else if ( content.isEmpty() ) {
00112           return SmallIcon( QLatin1String( "folder-grey" ) );
00113         } else
00114           return SmallIcon( QLatin1String( "folder-orange" ) ); // mixed stuff
00115       }
00116       break;
00117     case CollectionIdRole:
00118       return col.id();
00119     case CollectionRole:
00120       return QVariant::fromValue( col );
00121   }
00122   return QVariant();
00123 }
00124 
00125 QModelIndex CollectionModel::index( int row, int column, const QModelIndex & parent ) const
00126 {
00127   Q_D( const CollectionModel );
00128   if (column >= columnCount() || column < 0) return QModelIndex();
00129 
00130   QList<Collection::Id> list;
00131   if ( !parent.isValid() )
00132     list = d->childCollections.value( Collection::root().id() );
00133   else
00134   {
00135     if (parent.column() > 0)
00136        return QModelIndex();
00137     list = d->childCollections.value( parent.internalId() );
00138   }
00139 
00140   if ( row < 0 || row >= list.size() )
00141     return QModelIndex();
00142   if ( !d->collections.contains( list.at(row) ) )
00143     return QModelIndex();
00144   return createIndex( row, column, reinterpret_cast<void*>( d->collections.value( list.at(row) ).id() ) );
00145 }
00146 
00147 QModelIndex CollectionModel::parent( const QModelIndex & index ) const
00148 {
00149   Q_D( const CollectionModel );
00150   if ( !index.isValid() )
00151     return QModelIndex();
00152 
00153   Collection col = d->collections.value( index.internalId() );
00154   if ( !col.isValid() )
00155     return QModelIndex();
00156 
00157   Collection parentCol = d->collections.value( col.parent() );
00158   if ( !parentCol.isValid() )
00159     return QModelIndex();
00160 
00161   QList<Collection::Id> list;
00162   list = d->childCollections.value( parentCol.parent() );
00163 
00164   int parentRow = list.indexOf( parentCol.id() );
00165   if ( parentRow < 0 )
00166     return QModelIndex();
00167 
00168   return createIndex( parentRow, 0, reinterpret_cast<void*>( parentCol.id() ) );
00169 }
00170 
00171 int CollectionModel::rowCount( const QModelIndex & parent ) const
00172 {
00173   const  Q_D( CollectionModel );
00174   QList<Collection::Id> list;
00175   if ( parent.isValid() )
00176     list = d->childCollections.value( parent.internalId() );
00177   else
00178     list = d->childCollections.value( Collection::root().id() );
00179 
00180   return list.size();
00181 }
00182 
00183 QVariant CollectionModel::headerData( int section, Qt::Orientation orientation, int role ) const
00184 {
00185   if ( section == 0 && orientation == Qt::Horizontal && role == Qt::DisplayRole )
00186     return i18nc( "@title:column, name of a thing", "Name" );
00187   return QAbstractItemModel::headerData( section, orientation, role );
00188 }
00189 
00190 bool CollectionModel::setData( const QModelIndex & index, const QVariant & value, int role )
00191 {
00192   Q_D( CollectionModel );
00193   if ( index.column() == 0 && role == Qt::EditRole ) {
00194     // rename collection
00195     Collection col = d->collections.value( index.internalId() );
00196     if ( !col.isValid() || value.toString().isEmpty() )
00197       return false;
00198     col.setName( value.toString() );
00199     CollectionModifyJob *job = new CollectionModifyJob( col, d->session );
00200     connect( job, SIGNAL(result(KJob*)), SLOT(editDone(KJob*)) );
00201     return true;
00202   }
00203   return QAbstractItemModel::setData( index, value, role );
00204 }
00205 
00206 Qt::ItemFlags CollectionModel::flags( const QModelIndex & index ) const
00207 {
00208   Q_D( const CollectionModel );
00209   Qt::ItemFlags flags = QAbstractItemModel::flags( index );
00210 
00211   flags = flags | Qt::ItemIsDragEnabled;
00212 
00213   Collection col;
00214   if ( index.isValid() ) {
00215     col = d->collections.value( index.internalId() );
00216     Q_ASSERT( col.isValid() );
00217   }
00218   else
00219     return flags | Qt::ItemIsDropEnabled; // HACK Workaround for a probable bug in Qt
00220 
00221   if ( col.isValid() ) {
00222     if ( col.rights() & (Collection::CanChangeCollection |
00223                          Collection::CanCreateCollection |
00224                          Collection::CanDeleteCollection |
00225                          Collection::CanCreateItem) )  {
00226       if ( index.column() == 0 )
00227         flags = flags | Qt::ItemIsEditable;
00228 
00229       flags = flags | Qt::ItemIsDropEnabled;
00230     }
00231   }
00232 
00233   return flags;
00234 }
00235 
00236 Qt::DropActions CollectionModel::supportedDropActions() const
00237 {
00238   return Qt::CopyAction | Qt::MoveAction;
00239 }
00240 
00241 QStringList CollectionModel::mimeTypes() const
00242 {
00243   return QStringList() << QLatin1String( "text/uri-list" );
00244 }
00245 
00246 QMimeData *CollectionModel::mimeData(const QModelIndexList &indexes) const
00247 {
00248     QMimeData *data = new QMimeData();
00249     KUrl::List urls;
00250     foreach ( const QModelIndex &index, indexes ) {
00251         if ( index.column() != 0 )
00252           continue;
00253 
00254         urls << Collection( index.internalId() ).url();
00255     }
00256     urls.populateMimeData( data );
00257 
00258     return data;
00259 }
00260 
00261 bool CollectionModel::dropMimeData(const QMimeData * data, Qt::DropAction action, int row, int column, const QModelIndex & parent)
00262 {
00263   Q_D( CollectionModel );
00264   if ( !(action & supportedDropActions()) )
00265     return false;
00266 
00267   // handle drops onto items as well as drops between items
00268   QModelIndex idx;
00269   if ( row >= 0 && column >= 0 )
00270     idx = index( row, column, parent );
00271   else
00272     idx = parent;
00273 
00274   if ( !idx.isValid() )
00275     return false;
00276 
00277   const Collection parentCol = d->collections.value( idx.internalId() );
00278   if (!parentCol.isValid())
00279     return false;
00280 
00281   KJob *job = PasteHelper::paste( data, parentCol, action != Qt::MoveAction );
00282   connect( job, SIGNAL(result(KJob*)), SLOT(dropResult(KJob*)) );
00283   return true;
00284 }
00285 
00286 Collection CollectionModel::collectionForId(Collection::Id id) const
00287 {
00288   Q_D( const CollectionModel );
00289   return d->collections.value( id );
00290 }
00291 
00292 void CollectionModel::fetchCollectionStatistics(bool enable)
00293 {
00294   Q_D( CollectionModel );
00295   d->fetchStatistics = enable;
00296   d->monitor->fetchCollectionStatistics( enable );
00297 }
00298 
00299 void CollectionModel::includeUnsubscribed(bool include)
00300 {
00301   Q_D( CollectionModel );
00302   d->unsubscribed = include;
00303 }
00304 
00305 
00306 
00307 #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
  • kpimutils
  • kresources
  • ktnef
  • kxmlrpcclient
  • mailtransport
  • qgpgme
  • syndication
  •   atom
  •   rdf
  •   rss2
Generated for KDE-PIM Libraries by doxygen 1.5.6
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