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

akonadi

favoritecollectionsmodel.cpp

00001 /*
00002     Copyright (c) 2009 Kevin Ottens <ervin@kde.org>
00003 
00004 
00005     This library is free software; you can redistribute it and/or modify it
00006     under the terms of the GNU Library General Public License as published by
00007     the Free Software Foundation; either version 2 of the License, or (at your
00008     option) any later version.
00009 
00010     This library is distributed in the hope that it will be useful, but WITHOUT
00011     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00012     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Library General Public
00013     License for more details.
00014 
00015     You should have received a copy of the GNU Library General Public License
00016     along with this library; see the file COPYING.LIB.  If not, write to the
00017     Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
00018     02110-1301, USA.
00019 */
00020 
00021 #include "favoritecollectionsmodel.h"
00022 
00023 #include <QtGui/QItemSelectionModel>
00024 
00025 #include <kconfiggroup.h>
00026 #include <klocale.h>
00027 
00028 #include "entitytreemodel.h"
00029 
00030 using namespace Akonadi;
00031 
00035 class FavoriteCollectionsModel::Private
00036 {
00037   public:
00038     Private( const KConfigGroup &group, FavoriteCollectionsModel *parent )
00039       : q( parent ), configGroup( group )
00040     {
00041     }
00042 
00043     QString labelForCollection( const Collection &collection )
00044     {
00045       if ( labelMap.contains( collection.id() ) ) {
00046         return labelMap[ collection.id() ];
00047       }
00048 
00049       const QModelIndexList indexList = q->sourceModel()->match( QModelIndex(), EntityTreeModel::CollectionIdRole, collection.id() );
00050       Q_ASSERT( indexList.size() == 1 );
00051       return indexList.at( 0 ).data().toString();
00052     }
00053 
00054     void clearAndUpdateSelection()
00055     {
00056       q->selectionModel()->clear();
00057       updateSelection();
00058     }
00059 
00060     void updateSelection()
00061     {
00062       foreach ( const Collection &collection, collections ) {
00063         const QModelIndexList indexList = q->sourceModel()->match( QModelIndex(), EntityTreeModel::CollectionIdRole, collection.id() );
00064         if ( indexList.isEmpty() )
00065           continue;
00066 
00067         Q_ASSERT( indexList.size() == 1 );
00068         q->selectionModel()->select( indexList.at( 0 ),
00069                                      QItemSelectionModel::Select );
00070       }
00071     }
00072 
00073     void loadConfig()
00074     {
00075       const QList<qint64> ids = configGroup.readEntry( "FavoriteCollectionIds", QList<qint64>() );
00076       const QStringList labels = configGroup.readEntry( "FavoriteCollectionLabels", QStringList() );
00077 
00078       for ( int i = 0; i < ids.size(); ++i ) {
00079         collections << Collection( ids[i] );
00080         if ( i<labels.size() ) {
00081           labelMap[ ids[i] ] = labels[i];
00082         }
00083       }
00084     }
00085 
00086     void saveConfig()
00087     {
00088       QList<qint64> ids;
00089       QStringList labels;
00090 
00091       foreach ( const Collection &collection, collections ) {
00092         ids << collection.id();
00093         labels << labelForCollection( collection );
00094       }
00095 
00096       configGroup.writeEntry( "FavoriteCollectionIds", ids );
00097       configGroup.writeEntry( "FavoriteCollectionLabels", labels );
00098       configGroup.config()->sync();
00099     }
00100 
00101     FavoriteCollectionsModel * const q;
00102 
00103     Collection::List collections;
00104     QHash<qint64, QString> labelMap;
00105     KConfigGroup configGroup;
00106 };
00107 
00108 FavoriteCollectionsModel::FavoriteCollectionsModel( QAbstractItemModel *source, const KConfigGroup &group, QObject *parent )
00109   : Akonadi::SelectionProxyModel( new QItemSelectionModel( source, parent ), parent ),
00110     d( new Private( group, this ) )
00111 {
00112   setSourceModel( source );
00113   setFilterBehavior( ExactSelection );
00114 
00115   connect( source, SIGNAL( modelReset() ), this, SLOT( clearAndUpdateSelection() ) );
00116   connect( source, SIGNAL( layoutChanged() ), this, SLOT( clearAndUpdateSelection() ) );
00117   connect( source, SIGNAL( rowsInserted( const QModelIndex&, int, int ) ), this, SLOT( updateSelection() ) );
00118 
00119   d->loadConfig();
00120   d->clearAndUpdateSelection();
00121 }
00122 
00123 FavoriteCollectionsModel::~FavoriteCollectionsModel()
00124 {
00125   delete d;
00126 }
00127 
00128 void FavoriteCollectionsModel::setCollections( const Collection::List &collections )
00129 {
00130   d->collections = collections;
00131   d->labelMap.clear();
00132   d->clearAndUpdateSelection();
00133   d->saveConfig();
00134 }
00135 
00136 void FavoriteCollectionsModel::addCollection( const Collection &collection )
00137 {
00138   d->collections << collection;
00139   d->updateSelection();
00140   d->saveConfig();
00141 }
00142 
00143 void FavoriteCollectionsModel::removeCollection( const Collection &collection )
00144 {
00145   d->collections.removeAll( collection );
00146   d->labelMap.remove( collection.id() );
00147 
00148   const QModelIndexList indexList = sourceModel()->match( QModelIndex(), EntityTreeModel::CollectionIdRole, collection.id() );
00149   if ( indexList.isEmpty() )
00150     return;
00151 
00152   Q_ASSERT( indexList.size() == 1 );
00153   selectionModel()->select( indexList.at( 0 ),
00154                             QItemSelectionModel::Deselect );
00155 
00156   d->updateSelection();
00157   d->saveConfig();
00158 }
00159 
00160 Collection::List FavoriteCollectionsModel::collections() const
00161 {
00162   return d->collections;
00163 }
00164 
00165 void Akonadi::FavoriteCollectionsModel::setFavoriteLabel( const Collection &collection, const QString &label )
00166 {
00167   Q_ASSERT( d->collections.contains( collection ) );
00168   d->labelMap[ collection.id() ] = label;
00169   d->saveConfig();
00170 
00171   const QModelIndexList indexList = sourceModel()->match( QModelIndex(), EntityTreeModel::CollectionIdRole, collection.id() );
00172   if ( indexList.isEmpty() )
00173     return;
00174 
00175   Q_ASSERT( indexList.size() == 1 );
00176 
00177   const QModelIndex index = mapFromSource( indexList.at( 0 ) );
00178   emit dataChanged( index, index );
00179 }
00180 
00181 QVariant Akonadi::FavoriteCollectionsModel::data( const QModelIndex &index, int role ) const
00182 {
00183   if ( index.column() == 0 && role == Qt::DisplayRole ) {
00184     const QModelIndex sourceIndex = mapToSource( index );
00185     const Collection collection = sourceModel()->data( sourceIndex, EntityTreeModel::CollectionRole ).value<Collection>();
00186 
00187     return d->labelForCollection( collection );
00188   } else {
00189     return KSelectionProxyModel::data( index, role );
00190   }
00191 }
00192 
00193 QString Akonadi::FavoriteCollectionsModel::favoriteLabel( const Akonadi::Collection & collection )
00194 {
00195   if ( !collection.isValid() )
00196     return QString();
00197   return d->labelForCollection( collection );
00198 }
00199 
00200 QVariant FavoriteCollectionsModel::headerData( int section, Qt::Orientation orientation, int role ) const
00201 {
00202   if ( section == 0
00203     && orientation == Qt::Horizontal
00204     && role == Qt::DisplayRole ) {
00205     return i18n( "Favorite Folders" );
00206   } else {
00207     return KSelectionProxyModel::headerData( section, orientation, role );
00208   }
00209 }
00210 
00211 #include "favoritecollectionsmodel.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
  •   contact
  •   kmime
  • kabc
  • kblog
  • kcal
  • kholidays
  • kimap
  • kioslave
  •   imap4
  •   mbox
  •   nntp
  • kldap
  • kmime
  • kontactinterface
  • kpimidentities
  • kpimtextedit
  •   richtextbuilders
  • kpimutils
  • kresources
  • ktnef
  • kxmlrpcclient
  • mailtransport
  • microblog
  • qgpgme
  • syndication
  •   atom
  •   rdf
  •   rss2
Generated for KDE-PIM Libraries by doxygen 1.7.1
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